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
+34
View File
@@ -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")
+6 -1
View File
@@ -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": [
+1
View File
@@ -11,6 +11,7 @@ import (
"net/http"
"net/url"
"os"
"strings"
"time"
+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,
}