-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #186 from catsby/vault_kv2
Fix panic with Vault KV 2 secrets
- Loading branch information
Showing
2 changed files
with
145 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/consul-template/config" | ||
"github.com/hashicorp/consul-template/dependency" | ||
) | ||
|
||
func TestRunner_appendSecrets(t *testing.T) { | ||
t.Parallel() | ||
|
||
secretValue := "somevalue" | ||
|
||
cases := map[string]struct { | ||
path string | ||
data *dependency.Secret | ||
notFound bool | ||
}{ | ||
"kv1_secret": { | ||
"kv/bar", | ||
&dependency.Secret{ | ||
Data: map[string]interface{}{ | ||
"key_field": secretValue, | ||
}, | ||
}, | ||
false, | ||
}, | ||
"kv2_secret": { | ||
"secret/data/foo", | ||
&dependency.Secret{ | ||
Data: map[string]interface{}{ | ||
"metadata": map[string]interface{}{ | ||
"destroyed": bool(false), | ||
"version": "1", | ||
}, | ||
"data": map[string]interface{}{ | ||
"key_field": secretValue, | ||
}, | ||
}, | ||
}, | ||
false, | ||
}, | ||
"kv2_secret_destroyed": { | ||
"secret/data/foo", | ||
&dependency.Secret{ | ||
Data: map[string]interface{}{ | ||
"metadata": map[string]interface{}{ | ||
"destroyed": bool(true), | ||
"version": "2", | ||
}, | ||
"data": nil, | ||
}, | ||
}, | ||
true, | ||
}, | ||
} | ||
|
||
for name, tc := range cases { | ||
t.Run(fmt.Sprintf("%s", name), func(t *testing.T) { | ||
cfg := Config{ | ||
Secrets: &PrefixConfigs{ | ||
&PrefixConfig{ | ||
Path: config.String(tc.path), | ||
}, | ||
}, | ||
} | ||
c := DefaultConfig().Merge(&cfg) | ||
r, err := NewRunner(c, true) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
vrq, err := dependency.NewVaultReadQuery(tc.path) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
env := make(map[string]string) | ||
appendError := r.appendSecrets(env, vrq, tc.data) | ||
if appendError != nil { | ||
t.Fatalf("got err: %s", appendError) | ||
} | ||
|
||
if len(env) > 1 { | ||
t.Fatalf("Expected only 1 value in this test") | ||
} | ||
|
||
keyName := tc.path + "_key_field" | ||
keyName = strings.Replace(keyName, "/", "_", -1) | ||
|
||
var value string | ||
value, ok := env[keyName] | ||
if !ok && !tc.notFound { | ||
t.Fatalf("expected (%s) key, but was not found", keyName) | ||
} | ||
if ok && tc.notFound { | ||
t.Fatalf("expected to not find key, but (%s) was found", keyName) | ||
} | ||
if ok && value != secretValue { | ||
t.Fatalf("values didn't match, expected (%s), got (%s)", secretValue, value) | ||
} | ||
}) | ||
} | ||
} |