github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/boom/application/applications/reconciling/config/auth/auth.go (about)

     1  package auth
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/caos/orbos/internal/operator/boom/api/latest/reconciling"
     8  	"github.com/caos/orbos/mntr"
     9  )
    10  
    11  type Connectors struct {
    12  	Connectors []*connector `yaml:"connectors,omitempty"`
    13  }
    14  
    15  type connector struct {
    16  	Type   string
    17  	Name   string
    18  	ID     string
    19  	Config interface{}
    20  }
    21  
    22  func GetDexConfigFromSpec(monitor mntr.Monitor, spec *reconciling.Reconciling) *Connectors {
    23  	logFields := map[string]interface{}{
    24  		"application": "argocd",
    25  	}
    26  
    27  	connectors := make([]*connector, 0)
    28  
    29  	if spec.Auth == nil ||
    30  		((spec.Auth.OIDC == nil || (spec.Auth.OIDC.ClientSecret == nil || spec.Auth.OIDC.ClientSecret.Value == "") && (spec.Auth.OIDC.ExistingClientSecretSecret == nil || spec.Auth.OIDC.ExistingClientSecretSecret.Name == "")) &&
    31  			(spec.Auth.GithubConnector == nil || (spec.Auth.GithubConnector.Config.ClientSecret == nil || spec.Auth.GithubConnector.Config.ClientSecret.Value == "") && (spec.Auth.GithubConnector.Config.ExistingClientSecretSecret == nil || spec.Auth.GithubConnector.Config.ExistingClientSecretSecret.Name == "")) &&
    32  			(spec.Auth.GitlabConnector == nil || (spec.Auth.GitlabConnector.Config.ClientSecret == nil || spec.Auth.GitlabConnector.Config.ClientSecret.Value == "") && (spec.Auth.GitlabConnector.Config.ExistingClientSecretSecret == nil || spec.Auth.GitlabConnector.Config.ExistingClientSecretSecret.Name == "")) &&
    33  			(spec.Auth.GoogleConnector == nil || (spec.Auth.GoogleConnector.Config.ClientSecret == nil || spec.Auth.GoogleConnector.Config.ClientSecret.Value == "") && (spec.Auth.GoogleConnector.Config.ExistingClientSecretSecret == nil || spec.Auth.GoogleConnector.Config.ExistingClientSecretSecret.Name == ""))) {
    34  		return &Connectors{Connectors: connectors}
    35  	}
    36  
    37  	if spec.Network == nil || spec.Network.Domain == "" {
    38  		monitor.WithFields(logFields).Info("No auth connectors configured as no rootUrl is defined")
    39  		return &Connectors{Connectors: connectors}
    40  	}
    41  	redirect := strings.Join([]string{"https://", spec.Network.Domain, "/api/dex/callback"}, "")
    42  
    43  	if spec.Auth.GithubConnector != nil {
    44  		github, err := getGithub(spec.Auth.GithubConnector, redirect)
    45  		if err == nil && github != nil {
    46  			connectors = append(connectors, &connector{
    47  				Name:   spec.Auth.GithubConnector.Name,
    48  				ID:     spec.Auth.GithubConnector.ID,
    49  				Type:   "github",
    50  				Config: github,
    51  			})
    52  		} else {
    53  			monitor.WithFields(logFields).Error(fmt.Errorf("error while creating configuration for github connector: %w", err))
    54  		}
    55  	}
    56  
    57  	if spec.Auth.GitlabConnector != nil {
    58  		gitlab, err := getGitlab(spec.Auth.GitlabConnector, redirect)
    59  		if err == nil && gitlab != nil {
    60  			connectors = append(connectors, &connector{
    61  				Name:   spec.Auth.GitlabConnector.Name,
    62  				ID:     spec.Auth.GitlabConnector.ID,
    63  				Type:   "gitlab",
    64  				Config: gitlab,
    65  			})
    66  		} else {
    67  			monitor.WithFields(logFields).Error(fmt.Errorf("error while creating configuration for gitlab connector: %w", err))
    68  		}
    69  	}
    70  
    71  	if spec.Auth.GoogleConnector != nil {
    72  		google, err := getGoogle(spec.Auth.GoogleConnector, redirect)
    73  		if err == nil && google != nil {
    74  			connectors = append(connectors, &connector{
    75  				Name:   spec.Auth.GoogleConnector.Name,
    76  				ID:     spec.Auth.GoogleConnector.ID,
    77  				Type:   "oidc",
    78  				Config: google,
    79  			})
    80  		} else {
    81  			monitor.WithFields(logFields).Error(fmt.Errorf("error while creating configuration for google connector: %w", err))
    82  		}
    83  	}
    84  
    85  	if len(connectors) > 0 {
    86  		logFields["connectors"] = len(connectors)
    87  		monitor.WithFields(logFields).Debug("Created dex configuration")
    88  		return &Connectors{Connectors: connectors}
    89  	}
    90  	return nil
    91  }