github.com/argoproj/argo-cd@v1.8.7/server/settings/settings.go (about)

     1  package settings
     2  
     3  import (
     4  	"github.com/ghodss/yaml"
     5  	"golang.org/x/net/context"
     6  
     7  	sessionmgr "github.com/argoproj/argo-cd/util/session"
     8  
     9  	settingspkg "github.com/argoproj/argo-cd/pkg/apiclient/settings"
    10  	"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
    11  	"github.com/argoproj/argo-cd/util/settings"
    12  )
    13  
    14  // Server provides a Settings service
    15  type Server struct {
    16  	mgr           *settings.SettingsManager
    17  	authenticator Authenticator
    18  	disableAuth   bool
    19  }
    20  
    21  type Authenticator interface {
    22  	Authenticate(ctx context.Context) (context.Context, error)
    23  }
    24  
    25  // NewServer returns a new instance of the Settings service
    26  func NewServer(mgr *settings.SettingsManager, authenticator Authenticator, disableAuth bool) *Server {
    27  	return &Server{mgr: mgr, authenticator: authenticator, disableAuth: disableAuth}
    28  }
    29  
    30  // Get returns Argo CD settings
    31  func (s *Server) Get(ctx context.Context, q *settingspkg.SettingsQuery) (*settingspkg.Settings, error) {
    32  	resourceOverrides, err := s.mgr.GetResourceOverrides()
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	overrides := make(map[string]*v1alpha1.ResourceOverride)
    37  	for k := range resourceOverrides {
    38  		val := resourceOverrides[k]
    39  		overrides[k] = &val
    40  	}
    41  	appInstanceLabelKey, err := s.mgr.GetAppInstanceLabelKey()
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	argoCDSettings, err := s.mgr.GetSettings()
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	gaSettings, err := s.mgr.GetGoogleAnalytics()
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	help, err := s.mgr.GetHelp()
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  	plugins, err := s.plugins()
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	userLoginsDisabled := true
    62  	accounts, err := s.mgr.GetAccounts()
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	for _, account := range accounts {
    67  		if account.Enabled && account.HasCapability(settings.AccountCapabilityLogin) {
    68  			userLoginsDisabled = false
    69  			break
    70  		}
    71  	}
    72  
    73  	kustomizeSettings, err := s.mgr.GetKustomizeSettings()
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  	var kustomizeVersions []string
    78  	for i := range kustomizeSettings.Versions {
    79  		kustomizeVersions = append(kustomizeVersions, kustomizeSettings.Versions[i].Name)
    80  	}
    81  
    82  	set := settingspkg.Settings{
    83  		URL:                argoCDSettings.URL,
    84  		AppLabelKey:        appInstanceLabelKey,
    85  		ResourceOverrides:  overrides,
    86  		StatusBadgeEnabled: argoCDSettings.StatusBadgeEnabled,
    87  		KustomizeOptions: &v1alpha1.KustomizeOptions{
    88  			BuildOptions: argoCDSettings.KustomizeBuildOptions,
    89  		},
    90  		GoogleAnalytics: &settingspkg.GoogleAnalyticsConfig{
    91  			TrackingID:     gaSettings.TrackingID,
    92  			AnonymizeUsers: gaSettings.AnonymizeUsers,
    93  		},
    94  		Help: &settingspkg.Help{
    95  			ChatUrl:  help.ChatURL,
    96  			ChatText: help.ChatText,
    97  		},
    98  		Plugins:            plugins,
    99  		UserLoginsDisabled: userLoginsDisabled,
   100  		KustomizeVersions:  kustomizeVersions,
   101  		UiCssURL:           argoCDSettings.UiCssURL,
   102  	}
   103  
   104  	if sessionmgr.LoggedIn(ctx) || s.disableAuth {
   105  		configManagementPlugins, err := s.mgr.GetConfigManagementPlugins()
   106  		if err != nil {
   107  			return nil, err
   108  		}
   109  		tools := make([]*v1alpha1.ConfigManagementPlugin, len(configManagementPlugins))
   110  		for i := range configManagementPlugins {
   111  			tools[i] = &configManagementPlugins[i]
   112  		}
   113  		set.ConfigManagementPlugins = tools
   114  	}
   115  	if argoCDSettings.DexConfig != "" {
   116  		var cfg settingspkg.DexConfig
   117  		err = yaml.Unmarshal([]byte(argoCDSettings.DexConfig), &cfg)
   118  		if err == nil {
   119  			set.DexConfig = &cfg
   120  		}
   121  	}
   122  	if oidcConfig := argoCDSettings.OIDCConfig(); oidcConfig != nil {
   123  		set.OIDCConfig = &settingspkg.OIDCConfig{
   124  			Name:        oidcConfig.Name,
   125  			Issuer:      oidcConfig.Issuer,
   126  			ClientID:    oidcConfig.ClientID,
   127  			CLIClientID: oidcConfig.CLIClientID,
   128  			Scopes:      oidcConfig.RequestedScopes,
   129  		}
   130  		if len(argoCDSettings.OIDCConfig().RequestedIDTokenClaims) > 0 {
   131  			set.OIDCConfig.IDTokenClaims = argoCDSettings.OIDCConfig().RequestedIDTokenClaims
   132  		}
   133  	}
   134  	return &set, nil
   135  }
   136  
   137  func (s *Server) plugins() ([]*settingspkg.Plugin, error) {
   138  	in, err := s.mgr.GetConfigManagementPlugins()
   139  	if err != nil {
   140  		return nil, err
   141  	}
   142  	out := make([]*settingspkg.Plugin, len(in))
   143  	for i, p := range in {
   144  		out[i] = &settingspkg.Plugin{Name: p.Name}
   145  
   146  	}
   147  	return out, nil
   148  }
   149  
   150  // AuthFuncOverride disables authentication for settings service
   151  func (s *Server) AuthFuncOverride(ctx context.Context, fullMethodName string) (context.Context, error) {
   152  	// this authenticates the user, but ignores any error, so that we have claims populated
   153  	ctx, _ = s.authenticator.Authenticate(ctx)
   154  	return ctx, nil
   155  }