Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 <giacomo.consonni@cloudacademy.com>
  • Loading branch information
Giaco9 and giacomo-ca committed May 10, 2022
1 parent 06ff551 commit a857bcd
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -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.

Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions config/config_test.go
@@ -1,6 +1,7 @@
package config

import (
"os"
"reflect"
"testing"

Expand Down Expand Up @@ -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{
Expand All @@ -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/",
Expand Down
1 change: 1 addition & 0 deletions config/config_test.yml
Expand Up @@ -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/
Expand Down
4 changes: 2 additions & 2 deletions test/multiple-minio-buckets/config.yml
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions test/multiple-minio-buckets/docker-compose.yml
Expand Up @@ -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
Expand Down

0 comments on commit a857bcd

Please sign in to comment.