diff --git a/.env.test b/.env.test index da3e4c8..0df0238 100644 --- a/.env.test +++ b/.env.test @@ -1,7 +1,6 @@ ENV=test -DATABASE_URL=http://localhost:8000 -DATABASE_TYPE=dynamodb -REGION=us-east-1 +DATABASE_URL=test.db +DATABASE_TYPE=sqlite CUSTOM_ACCESS_TOKEN_SCRIPT="function(user,tokenPayload){var data = tokenPayload;data.extra = {'x-extra-id': user.id};return data;}" SMTP_HOST=smtp.mailtrap.io SMTP_PORT=2525 diff --git a/server/db/providers/dynamodb/provider.go b/server/db/providers/dynamodb/provider.go index 962a222..7457613 100644 --- a/server/db/providers/dynamodb/provider.go +++ b/server/db/providers/dynamodb/provider.go @@ -1,12 +1,16 @@ package dynamodb import ( + "os" + + "github.com/authorizerdev/authorizer/server/constants" "github.com/authorizerdev/authorizer/server/db/models" "github.com/authorizerdev/authorizer/server/memorystore" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/session" "github.com/guregu/dynamo" + log "github.com/sirupsen/logrus" ) type provider struct { @@ -15,13 +19,18 @@ type provider struct { // NewProvider returns a new Dynamo provider func NewProvider() (*provider, error) { - region := memorystore.RequiredEnvStoreObj.GetRequiredEnv().REGION dbURL := memorystore.RequiredEnvStoreObj.GetRequiredEnv().DatabaseURL - accessKey := memorystore.RequiredEnvStoreObj.GetRequiredEnv().AWS_ACCESS_KEY - secretKey := memorystore.RequiredEnvStoreObj.GetRequiredEnv().AWS_SECRET_KEY + awsRegion := os.Getenv(constants.EnvAwsRegion) + accessKey := os.Getenv(constants.EnvAwsAccessKey) + secretKey := os.Getenv(constants.EnvAwsSecretKey) + config := aws.Config{ - Region: aws.String(region), - MaxRetries: aws.Int(3), + MaxRetries: aws.Int(3), + CredentialsChainVerboseErrors: aws.Bool(true), // for full error logs + } + + if awsRegion != "" { + config.Region = aws.String(awsRegion) } // custom accessKey, secretkey took first priority, if not then fetch config from aws credentials @@ -31,6 +40,8 @@ func NewProvider() (*provider, error) { // static config in case of testing or local-setup config.Credentials = credentials.NewStaticCredentials("key", "key", "") config.Endpoint = aws.String(dbURL) + } else { + log.Info("REGION, AWS_ACCESS_KEY and AWS_SECRET_KEY not found in .env, trying to load default profile from aws credentials") } session := session.Must(session.NewSession(&config)) diff --git a/server/env/env.go b/server/env/env.go index 62b572f..491e4ee 100644 --- a/server/env/env.go +++ b/server/env/env.go @@ -77,6 +77,9 @@ func InitAllEnv() error { osResetPasswordURL := os.Getenv(constants.EnvKeyResetPasswordURL) osOrganizationName := os.Getenv(constants.EnvKeyOrganizationName) osOrganizationLogo := os.Getenv(constants.EnvKeyOrganizationLogo) + osAwsRegion := os.Getenv(constants.EnvAwsRegion) + osAwsAccessKey := os.Getenv(constants.EnvAwsAccessKey) + osAwsSecretKey := os.Getenv(constants.EnvAwsSecretKey) // os bool vars osAppCookieSecure := os.Getenv(constants.EnvKeyAppCookieSecure) @@ -119,6 +122,27 @@ func InitAllEnv() error { } } + if val, ok := envData[constants.EnvAwsRegion]; !ok || val == "" { + envData[constants.EnvAwsRegion] = osAwsRegion + } + if osAwsRegion != "" && envData[constants.EnvAwsRegion] != osAwsRegion { + envData[constants.EnvAwsRegion] = osAwsRegion + } + + if val, ok := envData[constants.EnvAwsAccessKey]; !ok || val == "" { + envData[constants.EnvAwsAccessKey] = osAwsAccessKey + } + if osAwsAccessKey != "" && envData[constants.EnvAwsAccessKey] != osAwsRegion { + envData[constants.EnvAwsAccessKey] = osAwsAccessKey + } + + if val, ok := envData[constants.EnvAwsSecretKey]; !ok || val == "" { + envData[constants.EnvAwsSecretKey] = osAwsSecretKey + } + if osAwsSecretKey != "" && envData[constants.EnvAwsSecretKey] != osAwsRegion { + envData[constants.EnvAwsSecretKey] = osAwsSecretKey + } + if val, ok := envData[constants.EnvKeyAppURL]; !ok || val == "" { envData[constants.EnvKeyAppURL] = osAppURL } diff --git a/server/memorystore/required_env_store.go b/server/memorystore/required_env_store.go index 2be8559..13166ac 100644 --- a/server/memorystore/required_env_store.go +++ b/server/memorystore/required_env_store.go @@ -16,9 +16,6 @@ import ( // RequiredEnv holds information about required envs type RequiredEnv struct { EnvPath string `json:"ENV_PATH"` - REGION string `json:"REGION"` - AWS_ACCESS_KEY string `json:"AWS_ACCESS_KEY"` - AWS_SECRET_KEY string `json:"AWS_SECRET_KEY"` DatabaseURL string `json:"DATABASE_URL"` DatabaseType string `json:"DATABASE_TYPE"` DatabaseName string `json:"DATABASE_NAME"` @@ -76,9 +73,6 @@ func InitRequiredEnv() error { log.Infof("using OS env instead of %s file", envPath) } - region := os.Getenv(constants.EnvAwsRegion) - awsAccessKey := os.Getenv(constants.EnvAwsAccessKey) - awsSecretKey := os.Getenv(constants.EnvAwsSecretKey) dbURL := os.Getenv(constants.EnvKeyDatabaseURL) dbType := os.Getenv(constants.EnvKeyDatabaseType) dbName := os.Getenv(constants.EnvKeyDatabaseName) @@ -134,9 +128,6 @@ func InitRequiredEnv() error { requiredEnv := RequiredEnv{ EnvPath: envPath, - REGION: region, - AWS_ACCESS_KEY: awsAccessKey, - AWS_SECRET_KEY: awsSecretKey, DatabaseURL: dbURL, DatabaseType: dbType, DatabaseName: dbName,