From ba6f62429923bf33819c549dffadef38736c0283 Mon Sep 17 00:00:00 2001 From: jay brown Date: Fri, 11 Apr 2025 15:23:53 -0700 Subject: [PATCH] auth.configprovider --- cmd/cognito_test/cognito.auth.harness/main.go | 2 +- internal/serviceconfig/auth/config.go | 234 ++++++++++++++++-- internal/serviceconfig/common.go | 9 +- 3 files changed, 220 insertions(+), 25 deletions(-) diff --git a/cmd/cognito_test/cognito.auth.harness/main.go b/cmd/cognito_test/cognito.auth.harness/main.go index 016f19d8..babb3ed9 100644 --- a/cmd/cognito_test/cognito.auth.harness/main.go +++ b/cmd/cognito_test/cognito.auth.harness/main.go @@ -47,7 +47,7 @@ func main() { "/settings": {"exporters"}, "/api/inventory/update": {"exporters", "uploaders"}, } - config.SetRoutePermissions(routePermissions) + config.SetAuthRoutePermissions(routePermissions) // Register authentication routes cognitoauth.RegisterRoutes(e, config) diff --git a/internal/serviceconfig/auth/config.go b/internal/serviceconfig/auth/config.go index e9b085d4..e8f1c85d 100644 --- a/internal/serviceconfig/auth/config.go +++ b/internal/serviceconfig/auth/config.go @@ -5,6 +5,7 @@ import ( "log" "log/slog" "os" + "reflect" "strings" ) @@ -30,8 +31,74 @@ type CognitoConfig struct { AuthRoutePermissions map[string][]string // Map of routes to required permissions } +type ConfigProvider interface { + // Env annotated fields + GetAuthRegion() string + SetAuthRegion(string) + GetAuthUserPoolID() string + SetAuthUserPoolID(string) + GetAuthClientSecret() string + SetAuthClientSecret(string) + GetAuthDomain() string + SetAuthDomain(string) + GetAuthClientID() string + SetAuthClientID(string) + + // Non-env fields + GetAuthRedirectURI() string + SetAuthRedirectURI(string) + GetAuthTokenURL() string + SetAuthTokenURL(string) + GetAuthJwksURL() string + SetAuthJwksURL(string) + GetAuthURL() string + SetAuthURL(string) + GetAuthLoginPath() string + SetAuthLoginPath(string) + GetAuthCallbackPath() string + SetAuthCallbackPath(string) + GetAuthHomePath() string + SetAuthHomePath(string) + GetAuthLogoutPath() string + SetAuthLogoutPath(string) + GetAuthLogger() *slog.Logger + SetAuthLogger(*slog.Logger) + GetAuthRoutePermissions() map[string][]string + SetAuthRoutePermissions(map[string][]string) + + // Initialize method + InitializeAuthConfig(baseURL string, logger *slog.Logger) error +} + +// InitializeAuthConfig initializes the receiver CognitoConfig instance with values from a newly created config. +// It takes a baseURL string used for redirect URI construction and a logger instance for logging operations. +// The function creates a temporary config using InitializeConfig() and copies all fields to the receiver using reflection. +// Returns an error if initialization fails, nil otherwise. +func (c *CognitoConfig) InitializeAuthConfig(baseURL string, logger *slog.Logger) error { + tempConfig := InitializeConfig(baseURL, logger) + if tempConfig == nil { + return fmt.Errorf("could not initialize auth config") + } + + // Use reflection to copy all fields + dst := reflect.ValueOf(c).Elem() + src := reflect.ValueOf(tempConfig).Elem() + + for i := 0; i < src.NumField(); i++ { + dstField := dst.Field(i) + srcField := src.Field(i) + + if dstField.CanSet() { + dstField.Set(srcField) + } + } + + return nil +} + // InitializeConfig creates a new CognitoConfig from environment variables -// and sets the values that are computed from other values +// and sets the values that are computed from other values and +// sets all default values for fields that will infrequently change. func InitializeConfig(baseURL string, logger *slog.Logger) *CognitoConfig { // Set default logger if not provided if logger == nil { @@ -115,33 +182,154 @@ func InitializeConfig(baseURL string, logger *slog.Logger) *CognitoConfig { AuthRoutePermissions: make(map[string][]string), } - config.PrettyPrint() + config.PrettyPrintDebugOnly() return config } -func (c *CognitoConfig) PrettyPrint() { - fmt.Printf("CognitoConfig:\n") - fmt.Printf(" AuthClientID: %s\n", c.AuthClientID) - fmt.Printf(" AuthClientSecret: %s\n", c.AuthClientSecret) - fmt.Printf(" AuthRedirectURI: %s\n", c.AuthRedirectURI) - fmt.Printf(" AuthTokenURL: %s\n", c.AuthTokenURL) - fmt.Printf(" AuthURL: %s\n", c.AuthURL) - fmt.Printf(" AuthJwksURL: %s\n", c.AuthJwksURL) - fmt.Printf(" AuthUserPoolID: %s\n", c.AuthUserPoolID) - fmt.Printf(" AuthRegion: %s\n", c.AuthRegion) - fmt.Printf(" AuthLoginPath: %s\n", c.AuthLoginPath) - fmt.Printf(" AuthCallbackPath: %s\n", c.AuthCallbackPath) - fmt.Printf(" AuthHomePath: %s\n", c.AuthHomePath) - fmt.Printf(" AuthLogoutPath: %s\n", c.AuthLogoutPath) +func (c *CognitoConfig) PrettyPrintDebugOnly() { + if os.Getenv("DEBUG") == "true" { + fmt.Printf("CognitoConfig (debug on):\n") + fmt.Printf(" AuthClientID: %s\n", c.AuthClientID) + fmt.Printf(" AuthClientSecret: %s\n", c.AuthClientSecret) + fmt.Printf(" AuthRedirectURI: %s\n", c.AuthRedirectURI) + fmt.Printf(" AuthTokenURL: %s\n", c.AuthTokenURL) + fmt.Printf(" AuthURL: %s\n", c.AuthURL) + fmt.Printf(" AuthJwksURL: %s\n", c.AuthJwksURL) + fmt.Printf(" AuthUserPoolID: %s\n", c.AuthUserPoolID) + fmt.Printf(" AuthRegion: %s\n", c.AuthRegion) + fmt.Printf(" AuthLoginPath: %s\n", c.AuthLoginPath) + fmt.Printf(" AuthCallbackPath: %s\n", c.AuthCallbackPath) + fmt.Printf(" AuthHomePath: %s\n", c.AuthHomePath) + fmt.Printf(" AuthLogoutPath: %s\n", c.AuthLogoutPath) - // pretty print the route permissions map - for route, permissions := range c.AuthRoutePermissions { - fmt.Printf(" Route: %s, Permissions: %v\n", route, permissions) + // pretty print the route permissions map + for route, permissions := range c.AuthRoutePermissions { + fmt.Printf(" Route: %s, Permissions: %v\n", route, permissions) + } } - } -// SetRoutePermissions sets the route permissions map -func (c *CognitoConfig) SetRoutePermissions(permissions map[string][]string) { - c.AuthRoutePermissions = permissions +//// SetRoutePermissions sets the route permissions map +//func (c *CognitoConfig) SetRoutePermissions(permissions map[string][]string) { +// c.AuthRoutePermissions = permissions +//} + +func (c *CognitoConfig) GetAuthRegion() string { + return c.AuthRegion +} + +func (c *CognitoConfig) SetAuthRegion(val string) { + c.AuthRegion = val +} + +func (c *CognitoConfig) GetAuthUserPoolID() string { + return c.AuthUserPoolID +} + +func (c *CognitoConfig) SetAuthUserPoolID(val string) { + c.AuthUserPoolID = val +} + +func (c *CognitoConfig) GetAuthClientSecret() string { + return c.AuthClientSecret +} + +func (c *CognitoConfig) SetAuthClientSecret(val string) { + c.AuthClientSecret = val +} + +func (c *CognitoConfig) GetAuthDomain() string { + return c.AuthDomain +} + +func (c *CognitoConfig) SetAuthDomain(val string) { + c.AuthDomain = val +} + +func (c *CognitoConfig) GetAuthClientID() string { + return c.AuthClientID +} + +func (c *CognitoConfig) SetAuthClientID(val string) { + c.AuthClientID = val +} + +func (c *CognitoConfig) GetAuthRedirectURI() string { + return c.AuthRedirectURI +} + +func (c *CognitoConfig) SetAuthRedirectURI(val string) { + c.AuthRedirectURI = val +} + +func (c *CognitoConfig) GetAuthTokenURL() string { + return c.AuthTokenURL +} + +func (c *CognitoConfig) SetAuthTokenURL(val string) { + c.AuthTokenURL = val +} + +func (c *CognitoConfig) GetAuthJwksURL() string { + return c.AuthJwksURL +} + +func (c *CognitoConfig) SetAuthJwksURL(val string) { + c.AuthJwksURL = val +} + +func (c *CognitoConfig) GetAuthURL() string { + return c.AuthURL +} + +func (c *CognitoConfig) SetAuthURL(val string) { + c.AuthURL = val +} + +func (c *CognitoConfig) GetAuthLoginPath() string { + return c.AuthLoginPath +} + +func (c *CognitoConfig) SetAuthLoginPath(val string) { + c.AuthLoginPath = val +} + +func (c *CognitoConfig) GetAuthCallbackPath() string { + return c.AuthCallbackPath +} + +func (c *CognitoConfig) SetAuthCallbackPath(val string) { + c.AuthCallbackPath = val +} + +func (c *CognitoConfig) GetAuthHomePath() string { + return c.AuthHomePath +} + +func (c *CognitoConfig) SetAuthHomePath(val string) { + c.AuthHomePath = val +} + +func (c *CognitoConfig) GetAuthLogoutPath() string { + return c.AuthLogoutPath +} + +func (c *CognitoConfig) SetAuthLogoutPath(val string) { + c.AuthLogoutPath = val +} + +func (c *CognitoConfig) GetAuthLogger() *slog.Logger { + return c.AuthLogger +} + +func (c *CognitoConfig) SetAuthLogger(val *slog.Logger) { + c.AuthLogger = val +} + +func (c *CognitoConfig) GetAuthRoutePermissions() map[string][]string { + return c.AuthRoutePermissions +} + +func (c *CognitoConfig) SetAuthRoutePermissions(val map[string][]string) { + c.AuthRoutePermissions = val } diff --git a/internal/serviceconfig/common.go b/internal/serviceconfig/common.go index 4766db70..aaf9a91c 100644 --- a/internal/serviceconfig/common.go +++ b/internal/serviceconfig/common.go @@ -54,7 +54,7 @@ type ConfigProvider interface { logger.ConfigProvider aws.ConfigProvider queue.ConfigProvider - //rbac.ConfigProvider + auth.ConfigProvider } // Configuration Methods @@ -159,6 +159,13 @@ func initializeConfigPrivate(cfg ConfigProvider) error { // Reinitialize with environment level cfg.SetDefaultLogger() + // replace the path here with value from ENV + baseUrl := "http://localhost:8080" + errorInitializingAuthConfig := cfg.InitializeAuthConfig(baseUrl, cfg.GetLogger()) + // join initErr with errorInitializingAuthConfig + if errorInitializingAuthConfig != nil { + initErr = fmt.Errorf("%w; %v", initErr, errorInitializingAuthConfig) + } return initErr }