From 31082c0ff5253eb18598ed52eecc0fea9186d0eb Mon Sep 17 00:00:00 2001 From: jay brown Date: Tue, 18 Mar 2025 13:46:59 -0700 Subject: [PATCH] wip --- cmd/cognito_test/jwtstuff/main.go | 34 ++++++++++++++++ devbox.json | 7 +++- internal/rbac/cognito.go | 1 + internal/serviceconfig/rbac/config.go | 52 ++++++++++++++++--------- internal/serviceconfig/rbac/testutil.go | 2 +- 5 files changed, 75 insertions(+), 21 deletions(-) diff --git a/cmd/cognito_test/jwtstuff/main.go b/cmd/cognito_test/jwtstuff/main.go index d5c72438..9c09e4c6 100644 --- a/cmd/cognito_test/jwtstuff/main.go +++ b/cmd/cognito_test/jwtstuff/main.go @@ -2,11 +2,45 @@ package main import ( "fmt" + "os" + "sort" "queryorchestration/internal/rbac" // Replace with your actual module name ) +//type DummyServiceConfig struct { +// service.BaseConfig +//} + +func PrintEnvironmentVariables() { + // Get all environment variables + envVars := os.Environ() + + // Sort them for consistent output + sort.Strings(envVars) + + // Print each variable in "key = value" format + for _, env := range envVars { + // Find the first equals sign to split the string + for i := 0; i < len(env); i++ { + if env[i] == '=' { + key := env[:i] + value := env[i+1:] + fmt.Printf("%s = %s\n", key, value) + break + } + } + } +} + func main() { + PrintEnvironmentVariables() + //cfg := &DummyServiceConfig{} + //errGettingConfig := serviceconfig.InitializeConfig(cfg) + //if errGettingConfig != nil { + // log.Fatal(errGettingConfig) + //} + // use the config here for the key provider parameters // Example with local key provider for testing localProvider := rbac.NewLocalKeyProvider("private_key.pem", "public_key.pem") diff --git a/devbox.json b/devbox.json index b9141d42..dc583b20 100644 --- a/devbox.json +++ b/devbox.json @@ -9,6 +9,8 @@ "AWS_SESSION_TOKEN": "", "BUCKET_IN": "documentin", "CLIENT_SYNC_URL": "http://localstack:4566/queue/us-east-1/000000000000/client_sync", + "COGNITO_REGION": "us-east-1", + "COGNITO_USER_POOL_ID": "NA", "DB_NOSSL": "true", "DOCUMENT_CLEAN_URL": "http://localstack:4566/queue/us-east-1/000000000000/document_clean", "DOCUMENT_INIT_URL": "http://localstack:4566/queue/us-east-1/000000000000/document_init", @@ -30,7 +32,10 @@ "QNAME_QUERY_VERSION_SYNC": "query_version_sync", "QUERY_SYNC_URL": "http://localstack:4566/queue/us-east-1/000000000000/query_sync", "QUERY_URL": "http://localstack:4566/queue/us-east-1/000000000000/query_runner", - "QUERY_VERSION_SYNC_URL": "http://localstack:4566/queue/us-east-1/000000000000/query_version_sync" + "QUERY_VERSION_SYNC_URL": "http://localstack:4566/queue/us-east-1/000000000000/query_version_sync", + "RBAC_PRIVATE_KEY_PATH": "./private_key.pem", + "RBAC_PROVIDER": "local", + "RBAC_PUBLIC_KEY_PATH": "./public_key.pem" }, "env_from": ".env", "packages": [ diff --git a/internal/rbac/cognito.go b/internal/rbac/cognito.go index a7f25852..de6f93e6 100644 --- a/internal/rbac/cognito.go +++ b/internal/rbac/cognito.go @@ -11,6 +11,7 @@ import ( "net/http" "net/url" "os" + "strings" "time" diff --git a/internal/serviceconfig/rbac/config.go b/internal/serviceconfig/rbac/config.go index 01eab763..9c072d27 100644 --- a/internal/serviceconfig/rbac/config.go +++ b/internal/serviceconfig/rbac/config.go @@ -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 } diff --git a/internal/serviceconfig/rbac/testutil.go b/internal/serviceconfig/rbac/testutil.go index a94b1f92..145a9d4b 100644 --- a/internal/serviceconfig/rbac/testutil.go +++ b/internal/serviceconfig/rbac/testutil.go @@ -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, }