Skip to content

Commit 820d294

Browse files
committed
comments resolved for requireEnv, Provider and test env
1 parent e37472d commit 820d294

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

.env.test

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
ENV=test
2-
DATABASE_URL=http://localhost:8000
3-
DATABASE_TYPE=dynamodb
4-
REGION=us-east-1
2+
DATABASE_URL=test.db
3+
DATABASE_TYPE=sqlite
54
CUSTOM_ACCESS_TOKEN_SCRIPT="function(user,tokenPayload){var data = tokenPayload;data.extra = {'x-extra-id': user.id};return data;}"
65
SMTP_HOST=smtp.mailtrap.io
76
SMTP_PORT=2525

server/db/providers/dynamodb/provider.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package dynamodb
22

33
import (
4+
"os"
5+
6+
"github.com/authorizerdev/authorizer/server/constants"
47
"github.com/authorizerdev/authorizer/server/db/models"
58
"github.com/authorizerdev/authorizer/server/memorystore"
69
"github.com/aws/aws-sdk-go/aws"
710
"github.com/aws/aws-sdk-go/aws/credentials"
811
"github.com/aws/aws-sdk-go/aws/session"
912
"github.com/guregu/dynamo"
13+
log "github.com/sirupsen/logrus"
1014
)
1115

1216
type provider struct {
@@ -15,13 +19,18 @@ type provider struct {
1519

1620
// NewProvider returns a new Dynamo provider
1721
func NewProvider() (*provider, error) {
18-
region := memorystore.RequiredEnvStoreObj.GetRequiredEnv().REGION
1922
dbURL := memorystore.RequiredEnvStoreObj.GetRequiredEnv().DatabaseURL
20-
accessKey := memorystore.RequiredEnvStoreObj.GetRequiredEnv().AWS_ACCESS_KEY
21-
secretKey := memorystore.RequiredEnvStoreObj.GetRequiredEnv().AWS_SECRET_KEY
23+
awsRegion := os.Getenv(constants.EnvAwsRegion)
24+
accessKey := os.Getenv(constants.EnvAwsAccessKey)
25+
secretKey := os.Getenv(constants.EnvAwsSecretKey)
26+
2227
config := aws.Config{
23-
Region: aws.String(region),
24-
MaxRetries: aws.Int(3),
28+
MaxRetries: aws.Int(3),
29+
CredentialsChainVerboseErrors: aws.Bool(true), // for full error logs
30+
}
31+
32+
if awsRegion != "" {
33+
config.Region = aws.String(awsRegion)
2534
}
2635

2736
// custom accessKey, secretkey took first priority, if not then fetch config from aws credentials
@@ -31,6 +40,8 @@ func NewProvider() (*provider, error) {
3140
// static config in case of testing or local-setup
3241
config.Credentials = credentials.NewStaticCredentials("key", "key", "")
3342
config.Endpoint = aws.String(dbURL)
43+
} else {
44+
log.Info("REGION, AWS_ACCESS_KEY and AWS_SECRET_KEY not found in .env, trying to load default profile from aws credentials")
3445
}
3546

3647
session := session.Must(session.NewSession(&config))

server/env/env.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ func InitAllEnv() error {
7777
osResetPasswordURL := os.Getenv(constants.EnvKeyResetPasswordURL)
7878
osOrganizationName := os.Getenv(constants.EnvKeyOrganizationName)
7979
osOrganizationLogo := os.Getenv(constants.EnvKeyOrganizationLogo)
80+
osAwsRegion := os.Getenv(constants.EnvAwsRegion)
81+
osAwsAccessKey := os.Getenv(constants.EnvAwsAccessKey)
82+
osAwsSecretKey := os.Getenv(constants.EnvAwsSecretKey)
8083

8184
// os bool vars
8285
osAppCookieSecure := os.Getenv(constants.EnvKeyAppCookieSecure)
@@ -119,6 +122,27 @@ func InitAllEnv() error {
119122
}
120123
}
121124

125+
if val, ok := envData[constants.EnvAwsRegion]; !ok || val == "" {
126+
envData[constants.EnvAwsRegion] = osAwsRegion
127+
}
128+
if osAwsRegion != "" && envData[constants.EnvAwsRegion] != osAwsRegion {
129+
envData[constants.EnvAwsRegion] = osAwsRegion
130+
}
131+
132+
if val, ok := envData[constants.EnvAwsAccessKey]; !ok || val == "" {
133+
envData[constants.EnvAwsAccessKey] = osAwsAccessKey
134+
}
135+
if osAwsAccessKey != "" && envData[constants.EnvAwsAccessKey] != osAwsRegion {
136+
envData[constants.EnvAwsAccessKey] = osAwsAccessKey
137+
}
138+
139+
if val, ok := envData[constants.EnvAwsSecretKey]; !ok || val == "" {
140+
envData[constants.EnvAwsSecretKey] = osAwsSecretKey
141+
}
142+
if osAwsSecretKey != "" && envData[constants.EnvAwsSecretKey] != osAwsRegion {
143+
envData[constants.EnvAwsSecretKey] = osAwsSecretKey
144+
}
145+
122146
if val, ok := envData[constants.EnvKeyAppURL]; !ok || val == "" {
123147
envData[constants.EnvKeyAppURL] = osAppURL
124148
}

server/memorystore/required_env_store.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ import (
1616
// RequiredEnv holds information about required envs
1717
type RequiredEnv struct {
1818
EnvPath string `json:"ENV_PATH"`
19-
REGION string `json:"REGION"`
20-
AWS_ACCESS_KEY string `json:"AWS_ACCESS_KEY"`
21-
AWS_SECRET_KEY string `json:"AWS_SECRET_KEY"`
2219
DatabaseURL string `json:"DATABASE_URL"`
2320
DatabaseType string `json:"DATABASE_TYPE"`
2421
DatabaseName string `json:"DATABASE_NAME"`
@@ -76,9 +73,6 @@ func InitRequiredEnv() error {
7673
log.Infof("using OS env instead of %s file", envPath)
7774
}
7875

79-
region := os.Getenv(constants.EnvAwsRegion)
80-
awsAccessKey := os.Getenv(constants.EnvAwsAccessKey)
81-
awsSecretKey := os.Getenv(constants.EnvAwsSecretKey)
8276
dbURL := os.Getenv(constants.EnvKeyDatabaseURL)
8377
dbType := os.Getenv(constants.EnvKeyDatabaseType)
8478
dbName := os.Getenv(constants.EnvKeyDatabaseName)
@@ -134,9 +128,6 @@ func InitRequiredEnv() error {
134128

135129
requiredEnv := RequiredEnv{
136130
EnvPath: envPath,
137-
REGION: region,
138-
AWS_ACCESS_KEY: awsAccessKey,
139-
AWS_SECRET_KEY: awsSecretKey,
140131
DatabaseURL: dbURL,
141132
DatabaseType: dbType,
142133
DatabaseName: dbName,

0 commit comments

Comments
 (0)