This commit is contained in:
jay brown
2025-03-18 13:46:59 -07:00
parent 2b1c33429f
commit 31082c0ff5
5 changed files with 75 additions and 21 deletions
+33 -19
View File
@@ -9,20 +9,33 @@ import (
// AuthConfig holds the RBAC configuration and KeyProvider
type AuthConfig struct {
Provider rbac.KeyProvider
ProviderType string `env:"RBAC_PROVIDER" envDefault:"cognito"`
AuthProvider rbac.KeyProvider
AuthType string `env:"RBAC_PROVIDER" envDefault:"cognito"`
// Cognito Config
Region string `env:"COGNITO_REGION" envDefault:"us-east-1"`
UserPoolID string `env:"COGNITO_USER_POOL_ID"`
CognitoRegion string `env:"COGNITO_REGION" envDefault:"us-east-1"`
CognitoUserPoolID string `env:"COGNITO_USER_POOL_ID"`
// Local Key Config
PrivateKeyPath string `env:"RBAC_PRIVATE_KEY_PATH"`
PublicKeyPath string `env:"RBAC_PUBLIC_KEY_PATH"`
}
//func IdentifyAuthProviderAndCreate(config rbac.AuthConfig) (KeyProvider, error) {
// var keyProvider KeyProvider
// if config.AuthType == "local" {
// keyProvider = NewLocalKeyProvider(config.PrivateKeyPath, config.PublicKeyPath)
// } else if config.AuthType == "cognito" {
// keyProvider = NewCognitoKeyProvider(config.CognitoRegion, config.CognitoUserPoolID)
// }
// if keyProvider == nil {
// return nil, fmt.Errorf("invalid auth provider: %s", config.AuthProvider)
// }
// return keyProvider, nil
//}
// NewAuthConfig creates a new AuthConfig with the specified provider type
func NewAuthConfig(providerType string) (*AuthConfig, error) {
config := &AuthConfig{
ProviderType: providerType,
AuthType: providerType,
}
if err := config.Initialize(); err != nil {
return nil, fmt.Errorf("failed to initialize auth config: %w", err)
@@ -33,7 +46,7 @@ func NewAuthConfig(providerType string) (*AuthConfig, error) {
// NewLocalAuthConfig creates a new AuthConfig configured for local key provider
func NewLocalAuthConfig(privateKeyPath, publicKeyPath string) (*AuthConfig, error) {
config := &AuthConfig{
ProviderType: "local",
AuthType: "local",
PrivateKeyPath: privateKeyPath,
PublicKeyPath: publicKeyPath,
}
@@ -52,39 +65,40 @@ type ConfigProvider interface {
// GetKeyProvider returns the configured KeyProvider
func (r *AuthConfig) GetKeyProvider() rbac.KeyProvider {
return r.Provider
return r.AuthProvider
}
// Initialize initializes the appropriate KeyProvider based on configuration
func (r *AuthConfig) Initialize() error {
switch r.ProviderType {
switch r.AuthType {
case "local":
if r.PrivateKeyPath == "" || r.PublicKeyPath == "" {
return fmt.Errorf("local key provider requires both private and public key paths")
}
r.Provider = rbac.NewLocalKeyProvider(r.PrivateKeyPath, r.PublicKeyPath)
r.AuthProvider = rbac.NewLocalKeyProvider(r.PrivateKeyPath, r.PublicKeyPath)
slog.Info("RBAC initialized with local key provider")
case "cognito":
if r.UserPoolID == "" {
if r.CognitoUserPoolID == "" {
return fmt.Errorf("cognito provider requires user pool ID")
}
r.Provider = rbac.NewCognitoKeyProvider(r.Region, r.UserPoolID)
r.AuthProvider = rbac.NewCognitoKeyProvider(r.CognitoRegion, r.CognitoUserPoolID)
slog.Info("RBAC initialized with Cognito key provider")
default:
return fmt.Errorf("unknown RBAC provider type: %s", r.ProviderType)
return fmt.Errorf("unknown RBAC provider type: %s", r.AuthType)
}
r.PrintConfig()
return nil
}
// PrintConfig logs the current configuration, masking sensitive values
func (r *AuthConfig) PrintConfig() {
slog.Info("RBAC Configuration",
"provider_type", r.ProviderType,
"region", r.Region,
"user_pool_id", r.UserPoolID,
slog.Info("RBAC AuthConfig:",
"provider_type", r.AuthType,
"region", r.CognitoRegion,
"user_pool_id", r.CognitoUserPoolID,
"private_key_path", r.PrivateKeyPath,
"public_key_path", r.PublicKeyPath,
)
@@ -92,17 +106,17 @@ func (r *AuthConfig) PrintConfig() {
// Validate checks if the configuration is valid
func (r *AuthConfig) Validate() error {
switch r.ProviderType {
switch r.AuthType {
case "local":
if r.PrivateKeyPath == "" || r.PublicKeyPath == "" {
return fmt.Errorf("local key provider requires both private and public key paths")
}
case "cognito":
if r.UserPoolID == "" {
if r.CognitoUserPoolID == "" {
return fmt.Errorf("cognito provider requires user pool ID")
}
default:
return fmt.Errorf("invalid provider type: %s", r.ProviderType)
return fmt.Errorf("invalid provider type: %s", r.AuthType)
}
return nil
}
+1 -1
View File
@@ -9,7 +9,7 @@ import (
// TestKeyProvider creates a test key provider for testing
func TestKeyProvider(t *testing.T, privateKeyPath, publicKeyPath string) rbac.KeyProvider {
config := &AuthConfig{
ProviderType: "local",
AuthType: "local",
PrivateKeyPath: privateKeyPath,
PublicKeyPath: publicKeyPath,
}