pr cleanup
config consolidation
This commit is contained in:
@@ -94,9 +94,9 @@ func main() {
|
|||||||
return swagger, nil
|
return swagger, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Both of these operations (InitializeConfig and InitializeAuthProvider)
|
// Both of these operations (InitializeConfig and InitializeAuthConfig)
|
||||||
// would be better off in the service.new but there are code dependencies
|
// would be better off in one function but since there are services
|
||||||
// that make this not possible right now without significant refactoring.
|
// that need the config but do not use auth we will keep them separate for now.
|
||||||
|
|
||||||
// This must be done before the rbac.InitializeAuthProvider
|
// This must be done before the rbac.InitializeAuthProvider
|
||||||
errInitializingConfig := serviceconfig.InitializeConfig(cfg)
|
errInitializingConfig := serviceconfig.InitializeConfig(cfg)
|
||||||
@@ -106,10 +106,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Authentication specific config.
|
// Authentication specific config.
|
||||||
// This may move to another function after refactoring is done.
|
cfg.BaseURL = fmt.Sprintf("%s:%d", cfg.BaseURL, cfg.Port)
|
||||||
// replace the path here with value from ENV
|
errorInitializingAuthConfig := cfg.InitializeAuthConfig(cfg.BaseURL, cfg.GetLogger())
|
||||||
baseUrl := "http://localhost:8080"
|
fmt.Printf("base url after InitializeAuthConfig: %s\n", cfg.BaseURL)
|
||||||
errorInitializingAuthConfig := cfg.InitializeAuthConfig(baseUrl, cfg.GetLogger())
|
|
||||||
// join initErr with errorInitializingAuthConfig
|
// join initErr with errorInitializingAuthConfig
|
||||||
if errorInitializingAuthConfig != nil {
|
if errorInitializingAuthConfig != nil {
|
||||||
slog.Error(errorInitializingAuthConfig.Error())
|
slog.Error(errorInitializingAuthConfig.Error())
|
||||||
|
|||||||
@@ -2,11 +2,12 @@ package auth
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/caarlos0/env/v11"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CognitoConfig holds the configuration for AWS Cognito
|
// CognitoConfig holds the configuration for AWS Cognito
|
||||||
@@ -18,15 +19,16 @@ type CognitoConfig struct {
|
|||||||
AuthClientSecret string `env:"COGNITO_CLIENT_SECRET,required,notEmpty"`
|
AuthClientSecret string `env:"COGNITO_CLIENT_SECRET,required,notEmpty"`
|
||||||
AuthDomain string `env:"COGNITO_DOMAIN,required,notEmpty"`
|
AuthDomain string `env:"COGNITO_DOMAIN,required,notEmpty"`
|
||||||
AuthClientID string `env:"COGNITO_CLIENT_ID,required,notEmpty"`
|
AuthClientID string `env:"COGNITO_CLIENT_ID,required,notEmpty"`
|
||||||
|
DisableAuth string `env:"DISABLE_AUTH" envDefault:"false"`
|
||||||
|
|
||||||
AuthRedirectURI string // URL where Cognito redirects after authentication
|
AuthRedirectURI string // URL where Cognito redirects after authentication
|
||||||
AuthTokenURL string // Cognito endpoint for token operations
|
AuthTokenURL string // Cognito endpoint for token operations
|
||||||
AuthJwksURL string // URL for JSON Web Key Set (for token verification)
|
AuthJwksURL string // URL for JSON Web Key Set (for token verification)
|
||||||
AuthURL string // Cognito authorization endpoint for initiating login
|
AuthURL string // Cognito authorization endpoint for initiating login
|
||||||
AuthLoginPath string // Path for initiating login
|
AuthLoginPath string `env:"COGNITO_LOGIN_PATH" envDefault:"/login"`
|
||||||
AuthCallbackPath string // Path for OAuth callback
|
AuthCallbackPath string `env:"COGNITO_CALLBACK_PATH" envDefault:"/login-callback"`
|
||||||
AuthHomePath string // Path for home page
|
AuthHomePath string `env:"COGNITO_HOME_PATH" envDefault:"/home"`
|
||||||
AuthLogoutPath string // Path for logout
|
AuthLogoutPath string `env:"COGNITO_LOGOUT_PATH" envDefault:"/logout"`
|
||||||
AuthLogger *slog.Logger // Logger instance ( normally just the same logger as main)
|
AuthLogger *slog.Logger // Logger instance ( normally just the same logger as main)
|
||||||
AuthRoutePermissions map[string][]string // Map of routes to required permissions
|
AuthRoutePermissions map[string][]string // Map of routes to required permissions
|
||||||
}
|
}
|
||||||
@@ -76,6 +78,7 @@ type ConfigProvider interface {
|
|||||||
// Returns an error if initialization fails, nil otherwise.
|
// Returns an error if initialization fails, nil otherwise.
|
||||||
func (c *CognitoConfig) InitializeAuthConfig(baseURL string, logger *slog.Logger) error {
|
func (c *CognitoConfig) InitializeAuthConfig(baseURL string, logger *slog.Logger) error {
|
||||||
tempConfig := InitializeConfig(baseURL, logger)
|
tempConfig := InitializeConfig(baseURL, logger)
|
||||||
|
//fmt.Printf("base url after InitializeAuthConfig: %s\n", tempConfig.)
|
||||||
if tempConfig == nil {
|
if tempConfig == nil {
|
||||||
return fmt.Errorf("could not initialize auth config")
|
return fmt.Errorf("could not initialize auth config")
|
||||||
}
|
}
|
||||||
@@ -100,7 +103,8 @@ func (c *CognitoConfig) InitializeAuthConfig(baseURL string, logger *slog.Logger
|
|||||||
// and sets the values that are computed from other values and
|
// and sets the values that are computed from other values and
|
||||||
// sets all default values for fields that will infrequently change.
|
// sets all default values for fields that will infrequently change.
|
||||||
func InitializeConfig(baseURL string, logger *slog.Logger) *CognitoConfig {
|
func InitializeConfig(baseURL string, logger *slog.Logger) *CognitoConfig {
|
||||||
// Set default logger if not provided
|
|
||||||
|
// Set default logger to text slog if not provided
|
||||||
if logger == nil {
|
if logger == nil {
|
||||||
var logLevel slog.Level
|
var logLevel slog.Level
|
||||||
if os.Getenv("DEBUG") == "true" {
|
if os.Getenv("DEBUG") == "true" {
|
||||||
@@ -115,76 +119,28 @@ func InitializeConfig(baseURL string, logger *slog.Logger) *CognitoConfig {
|
|||||||
logger = slog.New(logHandler)
|
logger = slog.New(logHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set paths with defaults
|
|
||||||
loginPath := os.Getenv("COGNITO_LOGIN_PATH")
|
|
||||||
if loginPath == "" {
|
|
||||||
loginPath = "/login"
|
|
||||||
}
|
|
||||||
|
|
||||||
callbackPath := os.Getenv("COGNITO_CALLBACK_PATH")
|
|
||||||
if callbackPath == "" {
|
|
||||||
callbackPath = "/login-callback"
|
|
||||||
}
|
|
||||||
|
|
||||||
homePath := os.Getenv("COGNITO_HOME_PATH")
|
|
||||||
if homePath == "" {
|
|
||||||
homePath = "/home"
|
|
||||||
}
|
|
||||||
|
|
||||||
logoutPath := os.Getenv("COGNITO_LOGOUT_PATH")
|
|
||||||
if logoutPath == "" {
|
|
||||||
logoutPath = "/logout"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get AWS region with fallback
|
|
||||||
region := os.Getenv("AWS_REGION")
|
|
||||||
if region == "" {
|
|
||||||
region = "us-east-2" // Fallback to default value
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get User Pool ID with fallback
|
|
||||||
userPoolID := os.Getenv("COGNITO_USER_POOL_ID")
|
|
||||||
if userPoolID == "" {
|
|
||||||
userPoolID = "" // No default, will need to be provided
|
|
||||||
logger.Warn("COGNITO_USER_POOL_ID environment variable is not set")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get Cognito domain
|
|
||||||
domain := os.Getenv("COGNITO_DOMAIN")
|
|
||||||
if domain == "" {
|
|
||||||
//logger.Warn("COGNITO_DOMAIN environment variable is not set, using default")
|
|
||||||
//// Use the user pool ID directly as part of the domain
|
|
||||||
//domain = fmt.Sprintf("%s.auth.%s.amazoncognito.com", userPoolID, region)
|
|
||||||
log.Fatalf("COGNITO_DOMAIN environment variable is not set")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure domain format is correct (no protocol prefix)
|
|
||||||
domain = strings.TrimPrefix(domain, "https://")
|
|
||||||
domain = strings.TrimPrefix(domain, "http://")
|
|
||||||
|
|
||||||
// Sanitize base URL (remove trailing slash)
|
// Sanitize base URL (remove trailing slash)
|
||||||
baseURL = strings.TrimSuffix(baseURL, "/")
|
baseURL = strings.TrimSuffix(baseURL, "/")
|
||||||
|
|
||||||
config := &CognitoConfig{
|
tempConfig := &CognitoConfig{}
|
||||||
AuthClientID: os.Getenv("COGNITO_CLIENT_ID"),
|
errorParsingCognitoConfig := env.Parse(tempConfig)
|
||||||
AuthClientSecret: os.Getenv("COGNITO_CLIENT_SECRET"),
|
if errorParsingCognitoConfig != nil {
|
||||||
AuthRedirectURI: baseURL + callbackPath,
|
logger.Error("Error parsing Cognito config", "error", errorParsingCognitoConfig)
|
||||||
AuthTokenURL: fmt.Sprintf("https://%s/oauth2/token", domain),
|
return nil
|
||||||
AuthURL: fmt.Sprintf("https://%s/oauth2/authorize", domain),
|
|
||||||
AuthJwksURL: fmt.Sprintf("https://cognito-idp.%s.amazonaws.com/%s/.well-known/jwks.json", region, userPoolID),
|
|
||||||
AuthUserPoolID: userPoolID,
|
|
||||||
AuthDomain: domain,
|
|
||||||
AuthRegion: region,
|
|
||||||
AuthLoginPath: loginPath,
|
|
||||||
AuthCallbackPath: callbackPath,
|
|
||||||
AuthHomePath: homePath,
|
|
||||||
AuthLogoutPath: logoutPath,
|
|
||||||
AuthLogger: logger,
|
|
||||||
AuthRoutePermissions: make(map[string][]string),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
config.PrettyPrintDebugOnly()
|
tempConfig.AuthRedirectURI = baseURL + tempConfig.AuthCallbackPath
|
||||||
return config
|
tempConfig.AuthRedirectURI = baseURL + tempConfig.AuthCallbackPath
|
||||||
|
tempConfig.AuthTokenURL = fmt.Sprintf("https://%s/oauth2/token", tempConfig.AuthDomain)
|
||||||
|
tempConfig.AuthURL = fmt.Sprintf("https://%s/oauth2/authorize", tempConfig.AuthDomain)
|
||||||
|
tempConfig.AuthJwksURL = fmt.Sprintf("https://cognito-idp.%s.amazonaws.com/%s/.well-known/jwks.json",
|
||||||
|
tempConfig.AuthRegion,
|
||||||
|
tempConfig.AuthUserPoolID)
|
||||||
|
tempConfig.AuthLogger = logger
|
||||||
|
tempConfig.AuthRoutePermissions = make(map[string][]string)
|
||||||
|
|
||||||
|
tempConfig.PrettyPrintDebugOnly()
|
||||||
|
return tempConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CognitoConfig) PrettyPrintDebugOnly() {
|
func (c *CognitoConfig) PrettyPrintDebugOnly() {
|
||||||
@@ -202,6 +158,8 @@ func (c *CognitoConfig) PrettyPrintDebugOnly() {
|
|||||||
fmt.Printf(" AuthCallbackPath: %s\n", c.AuthCallbackPath)
|
fmt.Printf(" AuthCallbackPath: %s\n", c.AuthCallbackPath)
|
||||||
fmt.Printf(" AuthHomePath: %s\n", c.AuthHomePath)
|
fmt.Printf(" AuthHomePath: %s\n", c.AuthHomePath)
|
||||||
fmt.Printf(" AuthLogoutPath: %s\n", c.AuthLogoutPath)
|
fmt.Printf(" AuthLogoutPath: %s\n", c.AuthLogoutPath)
|
||||||
|
fmt.Printf(" DisableAuth: %s\n", c.DisableAuth)
|
||||||
|
fmt.Printf(" AuthDomain: %s\n", c.AuthDomain)
|
||||||
|
|
||||||
// pretty print the route permissions map
|
// pretty print the route permissions map
|
||||||
for route, permissions := range c.AuthRoutePermissions {
|
for route, permissions := range c.AuthRoutePermissions {
|
||||||
|
|||||||
@@ -32,9 +32,10 @@ type BaseConfig struct {
|
|||||||
// Always place non struct fields at the top of the struct
|
// Always place non struct fields at the top of the struct
|
||||||
// this is a requirement for the PrintAuthConfig function.
|
// this is a requirement for the PrintAuthConfig function.
|
||||||
|
|
||||||
Pwd string `env:"PWD,required,notEmpty"`
|
// These are the fields that are common to all services.
|
||||||
|
//Pwd string `env:"PWD,required,notEmpty"`
|
||||||
//rbac.AuthConfig
|
Port int `env:"PORT" envDefault:"8080"`
|
||||||
|
BaseURL string `env:"BASE_URL" envDefault:"http://localhost"`
|
||||||
auth.CognitoConfig
|
auth.CognitoConfig
|
||||||
logger.LogConfig
|
logger.LogConfig
|
||||||
observability.ObsConfig
|
observability.ObsConfig
|
||||||
|
|||||||
Reference in New Issue
Block a user