From f057f6b48d4990fcfdb507b6fc310f021041ff2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20D=C3=B6ll?= Date: Sat, 19 Oct 2024 19:03:39 +0000 Subject: [PATCH] feat: conditionally enable auth providers --- cmd/root.go | 11 +++++++++-- internal/cfg/config.go | 2 ++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index c2127da..ee35213 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -58,6 +58,8 @@ func init() { Root.PersistentFlags().StringVar(&config.Flags.Environment, "environment", config.Flags.Environment, "environment") Root.PersistentFlags().StringVar(&config.Flags.DatabaseURI, "db-rul", config.Flags.DatabaseURI, "Database URI") Root.PersistentFlags().StringVar(&config.Flags.DatabaseTablePrefix, "db-table-prefix", config.Flags.DatabaseTablePrefix, "Database table prefix") + Root.PersistentFlags().BoolVar(&config.Flags.GitHubEnabled, "github-enabled", config.Flags.GitHubEnabled, "GitHub enabled") + Root.PersistentFlags().BoolVar(&config.Flags.EntraIDEnabled, "entraid-enabled", config.Flags.EntraIDEnabled, "EntraID enabled") Root.SilenceUsage = true } @@ -89,8 +91,13 @@ func NewWebSrv(cfg *cfg.Config) *WebSrv { // Start starts the server. func (s *WebSrv) Start(ctx context.Context, ready server.ReadyFunc, run server.RunFunc) func() error { return func() error { - providers.RegisterProvider(github.New(s.cfg.Flags.GitHubClientID, s.cfg.Flags.GitHubClientSecret, s.cfg.Flags.GitHubCallbackURL)) - providers.RegisterProvider(entraid.New(s.cfg.Flags.EntraIDClientID, s.cfg.Flags.EntraIDClientSecret, s.cfg.Flags.EntraIDCallbackURL, entraid.TenantType(s.cfg.Flags.EntraIDTenantID))) + if s.cfg.Flags.GitHubEnabled { + providers.RegisterProvider(github.New(s.cfg.Flags.GitHubClientID, s.cfg.Flags.GitHubClientSecret, s.cfg.Flags.GitHubCallbackURL)) + } + + if s.cfg.Flags.EntraIDEnabled { + providers.RegisterProvider(entraid.New(s.cfg.Flags.EntraIDClientID, s.cfg.Flags.EntraIDClientSecret, s.cfg.Flags.EntraIDCallbackURL, entraid.TenantType(s.cfg.Flags.EntraIDTenantID))) + } conn, err := gorm.Open(postgres.Open(config.Flags.DatabaseURI), &gorm.Config{ NamingStrategy: schema.NamingStrategy{ diff --git a/internal/cfg/config.go b/internal/cfg/config.go index 166fc58..48987ce 100644 --- a/internal/cfg/config.go +++ b/internal/cfg/config.go @@ -10,9 +10,11 @@ type Flags struct { Addr string `envconfig:"SERVICE_LENS_ADDR" default:":8084"` DatabaseURI string `envconfig:"POSTGRES_URL" default:""` DatabaseTablePrefix string `envconfig:"SERVICE_LENS_DATABASE_TABLE_PREFIX" default:"service_lens_"` + GitHubEnabled bool `envconfig:"SERVICE_LENS_GITHUB_ENABLED" default:"false"` GitHubCallbackURL string `envconfig:"SERVICE_LENS_GITHUB_CALLBACK_URL" default:""` GitHubClientID string `envconfig:"SERVICE_LENS_GITHUB_CLIENT_ID" default:""` GitHubClientSecret string `envconfig:"SERVICE_LENS_GITHUB_CLIENT_SECRET" default:""` + EntraIDEnabled bool `envconfig:"SERVICE_LENS_ENTRAID_ENABLED" default:"true"` EntraIDClientID string `envconfig:"SERVICE_LENS_ENTRAID_CLIENT_ID" default:""` EntraIDClientSecret string `envconfig:"SERVICE_LENS_ENTRAID_CLIENT_SECRET" default:""` EntraIDCallbackURL string `envconfig:"SERVICE_LENS_ENTRAID_CALLBACK_URL" default:""`