From a857bcd796ef50c1bee218ff94805579ce2a6efa Mon Sep 17 00:00:00 2001 From: Giacomo Consonni Date: Tue, 10 May 2022 09:30:02 +0200 Subject: [PATCH] feat: add ability to resolve environment variables in yaml config (#240) * feat: add ability to resolve environment variables in yaml config * test: test when a configuration in the yaml configuration file is an environment variable * docs: add in README the ability to set configurations in YAML using environment variables * test: resolve yaml configuration using environment variables also in multiple minimo buckets example Co-authored-by: Giacomo Consonni --- README.md | 4 ++-- config/config.go | 1 + config/config_test.go | 4 ++++ config/config_test.yml | 1 + test/multiple-minio-buckets/config.yml | 4 ++-- test/multiple-minio-buckets/docker-compose.yml | 2 ++ 6 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 18c733d9..37965565 100644 --- a/README.md +++ b/README.md @@ -152,7 +152,7 @@ Terraboard currently supports configuration in three different ways: 2. CLI parameters **(only usable for single provider configuration)** 3. Configuration file (YAML). A configuration file example can be found in the root directory of this repository and in the `test/` subdirectory. -**Important: all flags/environment variables related to the providers settings aren't compatible with multi-provider configuration! Instead, you must use the YAML config file to be able to configure multiples buckets/providers.** +**Important: all flags/environment variables related to the providers settings aren't compatible with multi-provider configuration! Instead, you must use the YAML config file to be able to configure multiples buckets/providers. YAML config is able to load values from environments variables.** The precedence of configurations is as described below. @@ -171,7 +171,7 @@ provider: aws: - endpoint: http://minio:9000/ - region: eu-west-1 + region: ${AWS_DEFAULT_REGION} s3: - bucket: test-bucket force-path-style: true diff --git a/config/config.go b/config/config.go index b80d6c90..2a9f0fd6 100644 --- a/config/config.go +++ b/config/config.go @@ -141,6 +141,7 @@ func (c *Config) LoadConfigFromYaml(filename string) *Config { log.Printf("yamlFile.Get err #%v ", err) } + yamlFile = []byte(os.ExpandEnv(string(yamlFile))) err = yaml.Unmarshal(yamlFile, c) if err != nil { log.Fatalf("Unmarshal err: %v", err) diff --git a/config/config_test.go b/config/config_test.go index 93f69c4b..1145a595 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,6 +1,7 @@ package config import ( + "os" "reflect" "testing" @@ -93,6 +94,8 @@ func TestLoadConfig(t *testing.T) { func TestLoadConfigFromYaml(t *testing.T) { var config Config + os.Setenv("AWS_DEFAULT_REGION", "test-region") + defer os.Unsetenv("AWS_DEFAULT_REGION") config.LoadConfigFromYaml("config_test.yml") compareConfig := Config{ Log: LogConfig{ @@ -115,6 +118,7 @@ func TestLoadConfigFromYaml(t *testing.T) { AccessKey: "root", SecretAccessKey: "mypassword", DynamoDBTable: "terraboard-dynamodb", + Region: "test-region", S3: []S3BucketConfig{{ Bucket: "terraboard-bucket", KeyPrefix: "test/", diff --git a/config/config_test.yml b/config/config_test.yml index 0b3251a6..2acf015e 100644 --- a/config/config_test.yml +++ b/config/config_test.yml @@ -14,6 +14,7 @@ aws: - access-key: root secret-access-key: mypassword dynamodb-table: terraboard-dynamodb + region: ${AWS_DEFAULT_REGION} s3: - bucket: terraboard-bucket key-prefix: test/ diff --git a/test/multiple-minio-buckets/config.yml b/test/multiple-minio-buckets/config.yml index acb12c42..608f5252 100644 --- a/test/multiple-minio-buckets/config.yml +++ b/test/multiple-minio-buckets/config.yml @@ -10,8 +10,8 @@ provider: no-versioning: true aws: - - access-key: root - secret-access-key: mypassword + - access-key: ${AWS_ACCESS_KEY_ID} + secret-access-key: ${AWS_SECRET_ACCESS_KEY} endpoint: http://minio:9000/ region: eu-west-1 s3: diff --git a/test/multiple-minio-buckets/docker-compose.yml b/test/multiple-minio-buckets/docker-compose.yml index 682b4742..5b70ca59 100644 --- a/test/multiple-minio-buckets/docker-compose.yml +++ b/test/multiple-minio-buckets/docker-compose.yml @@ -9,6 +9,8 @@ services: DB_SSLMODE: disable CONFIG_FILE: config/config.yml GODEBUG: netdns=go + AWS_ACCESS_KEY_ID: root + AWS_SECRET_ACCESS_KEY: mypassword depends_on: db: condition: service_healthy