Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Env backend support for keys with underscore #567

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions backends/env/client.go
Expand Up @@ -28,10 +28,12 @@ func (c *Client) GetValues(keys []string) (map[string]string, error) {
}
vars := make(map[string]string)
for _, key := range keys {
k := transform(key)
keyNoSlashPrefix := strings.TrimPrefix(key, "/")
k := transform(keyNoSlashPrefix)
for envKey, envValue := range envMap {
if strings.HasPrefix(envKey, k) {
vars[clean(envKey)] = envValue
envKeyPostfix := envKey[len(k):]
vars["/" + keyNoSlashPrefix + clean(envKeyPostfix)] = envValue
}
}
}
Expand All @@ -42,15 +44,13 @@ func (c *Client) GetValues(keys []string) (map[string]string, error) {
}

func transform(key string) string {
k := strings.TrimPrefix(key, "/")
return strings.ToUpper(replacer.Replace(k))
return strings.ToUpper(replacer.Replace(key))
}

var cleanReplacer = strings.NewReplacer("_", "/")

func clean(key string) string {
newKey := "/" + key
return cleanReplacer.Replace(strings.ToLower(newKey))
return cleanReplacer.Replace(strings.ToLower(key))
}

func (c *Client) WatchPrefix(prefix string, keys []string, waitIndex uint64, stopChan chan bool) (uint64, error) {
Expand Down
8 changes: 8 additions & 0 deletions integration/confdir/conf.d/underscore_keys.toml
@@ -0,0 +1,8 @@
[template]
mode = "0644"
src = "underscore_keys.conf.tmpl"
dest = "/tmp/confd-underscore_keys-test.conf"
keys = [
"/with_under_scores",
"/path_here/with/under_scores",
]
2 changes: 2 additions & 0 deletions integration/confdir/templates/underscore_keys.conf.tmpl
@@ -0,0 +1,2 @@
underscore_key: {{getv "/with_under_scores"}}
path_underscore_key={{getv "/path_here/with/under_scores"}}
2 changes: 2 additions & 0 deletions integration/consul/test.sh
Expand Up @@ -7,6 +7,8 @@ curl -X PUT http://127.0.0.1:8500/v1/kv/database/port -d '3306'
curl -X PUT http://127.0.0.1:8500/v1/kv/database/username -d 'confd'
curl -X PUT http://127.0.0.1:8500/v1/kv/upstream/app1 -d '10.0.1.10:8080'
curl -X PUT http://127.0.0.1:8500/v1/kv/upstream/app2 -d '10.0.1.11:8080'
curl -X PUT http://127.0.0.1:8500/v1/kv/with_under_scores -d 'value_with_underscores'
curl -X PUT http://127.0.0.1:8500/v1/kv/path_here/with/under_scores -d 'value_path_with_underscores'

# Run confd
confd --onetime --log-level debug --confdir ./integration/confdir --backend consul --node 127.0.0.1:8500
7 changes: 7 additions & 0 deletions integration/dynamodb/test.sh
Expand Up @@ -57,6 +57,13 @@ aws dynamodb put-item --table-name confd --region eu-west-1 \
--item '{ "key": { "S": "/prefix/upstream/app2" }, "value": {"S": "10.0.1.11:8080"}}' \
--endpoint-url http://localhost:8000

aws dynamodb put-item --table-name confd --region eu-west-1 \
--item '{ "key": { "S": "/with_under_scores" }, "value": {"S": "value_with_underscores"}}' \
--endpoint-url http://localhost:8000
aws dynamodb put-item --table-name confd --region eu-west-1 \
--item '{ "key": { "S": "/path_here/with/under_scores" }, "value": {"S": "value_path_with_underscores"}}' \
--endpoint-url http://localhost:8000

# Run confd, expect it to work
confd --onetime --log-level debug --confdir ./integration/confdir --interval 5 --backend dynamodb --table confd
if [ $? -ne 0 ]
Expand Down
2 changes: 2 additions & 0 deletions integration/env/test.sh 100644 → 100755
Expand Up @@ -13,5 +13,7 @@ export PREFIX_DATABASE_PORT="3306"
export PREFIX_DATABASE_USERNAME="confd"
export PREFIX_UPSTREAM_APP1="10.0.1.10:8080"
export PREFIX_UPSTREAM_APP2="10.0.1.11:8080"
export WITH_UNDER_SCORES="value_with_underscores"
export PATH_HERE_WITH_UNDER_SCORES="value_path_with_underscores"

confd --onetime --log-level debug --confdir ./integration/confdir --interval 5 --backend env
2 changes: 2 additions & 0 deletions integration/etcd/test.sh
Expand Up @@ -13,6 +13,8 @@ curl -L -X PUT http://127.0.0.1:4001/v2/keys/prefix/database/port -d value=3306
curl -L -X PUT http://127.0.0.1:4001/v2/keys/prefix/database/username -d value=confd
curl -L -X PUT http://127.0.0.1:4001/v2/keys/prefix/upstream/app1 -d value=10.0.1.10:8080
curl -L -X PUT http://127.0.0.1:4001/v2/keys/prefix/upstream/app2 -d value=10.0.1.11:8080
curl -L -X PUT http://127.0.0.1:4001/v2/keys/with_under_scores -d value=value_with_underscores
curl -L -X PUT http://127.0.0.1:4001/v2/keys/path_here/with/under_scores -d value=value_path_with_underscores


# Run confd
Expand Down
8 changes: 7 additions & 1 deletion integration/rancher/test.sh
Expand Up @@ -25,7 +25,13 @@ cat > ./rancher-answers.json<<EOF
"app1": "10.0.1.10:8080",
"app2": "10.0.1.11:8080"
}
}
},
"with_under_scores": "value_with_underscores",
"path_here": {
"with": {
"under_scores": "value_path_with_underscores"
}
}
}
}
EOF
Expand Down
2 changes: 2 additions & 0 deletions integration/redis/test.sh
Expand Up @@ -13,6 +13,8 @@ redis-cli set /prefix/database/port 3306
redis-cli set /prefix/database/username confd
redis-cli set /prefix/upstream/app1 10.0.1.10:8080
redis-cli set /prefix/upstream/app2 10.0.1.11:8080
redis-cli set /with_under_scores value_with_underscores
redis-cli set /path_here/with/under_scores value_path_with_underscores

# Run confd with --watch, expecting it to fail
confd --onetime --log-level debug --confdir ./integration/confdir --interval 5 --backend redis --node 127.0.0.1:6379 --watch
Expand Down
5 changes: 4 additions & 1 deletion integration/vault/test.sh
Expand Up @@ -5,14 +5,17 @@ ROOT_TOKEN=$(vault read -field id auth/token/lookup-self)
vault mount -path database generic
vault mount -path key generic
vault mount -path upstream generic
vault mount -path with_under_scores generic
vault mount -path path_here generic

vault write key value=foobar
vault write database/host value=127.0.0.1
vault write database/port value=3306
vault write database/username value=confd
vault write database/password value=p@sSw0rd
vault write upstream app1=10.0.1.10:8080 app2=10.0.1.11:8080

vault write with_under_scores value=value_with_underscores
vault write path_here/with/under_scores value=value_path_with_underscores

# Run confd
confd --onetime --log-level debug \
Expand Down
8 changes: 7 additions & 1 deletion integration/zookeeper/test.json
Expand Up @@ -21,5 +21,11 @@
"app1": "10.0.1.10:8080",
"app2": "10.0.1.11:8080"
}
}
},
"with_under_scores": "value_with_underscores",
"path_here": {
"with": {
"under_scores": "value_path_with_underscores"
}
}
}