Skip to content

Commit d0c6f3e

Browse files
committed
fix: add hashicorp vault
KVv2 secrets engine with token auth only
1 parent c14be56 commit d0c6f3e

File tree

15 files changed

+315
-99
lines changed

15 files changed

+315
-99
lines changed

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
OWNER := dnitsch
33
NAME := configmanager
4-
GIT_TAG := "1.13.0"
5-
VERSION := "v1.13.0"
4+
GIT_TAG := "1.14.0"
5+
VERSION := "v1.14.0"
66
# VERSION := "$(shell git describe --tags --abbrev=0)"
77
REVISION := $(shell git rev-parse --short HEAD)
88

@@ -51,3 +51,6 @@ echo:
5151
echo $(REVISION)
5252

5353
tagbuildrelease: tag cross-build release
54+
55+
coverage_interactive: test
56+
go tool cover -html=.coverage/out

configmanager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (c *ConfigManager) RetrieveWithInputReplaced(input string, config generator
3939
func retrieveWithInputReplaced(input string, gv generator.Generatoriface) (string, error) {
4040
tokens := []string{}
4141
for k := range generator.VarPrefix {
42-
matches := regexp.MustCompile(`(?s)`+regexp.QuoteMeta(k)+`.(`+TERMINATING_CHAR+`+)`).FindAllString(input, -1)
42+
matches := regexp.MustCompile(`(?s)`+regexp.QuoteMeta(string(k))+`.(`+TERMINATING_CHAR+`+)`).FindAllString(input, -1)
4343
tokens = append(tokens, matches...)
4444
}
4545

configmanager_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ func Test_retrieve(t *testing.T) {
6161
t.Run(tt.name, func(t *testing.T) {
6262
pm, err := retrieve(tt.tokens, tt.genvar)
6363
if err != nil {
64-
t.Errorf(testutils.TestPhrase, nil, err)
64+
t.Errorf(testutils.TestPhrase, err, nil)
6565
}
6666
for k, v := range pm {
6767
if k != tt.expectKey {
68-
t.Errorf(testutils.TestPhrase, tt.expectKey, k)
68+
t.Errorf(testutils.TestPhrase, k, tt.expectKey)
6969
}
7070
if v != tt.expectVal {
71-
t.Errorf(testutils.TestPhrase, tt.expectVal, k)
71+
t.Errorf(testutils.TestPhrase, v, tt.expectVal)
7272
}
7373
}
7474
})
@@ -180,7 +180,7 @@ foo23 = val1
180180
t.Errorf("failed with %v", err)
181181
}
182182
if got != tt.expect {
183-
t.Errorf(testutils.TestPhrase, tt.expect, got)
183+
t.Errorf(testutils.TestPhrase, got, tt.expect)
184184
}
185185
})
186186
}
@@ -218,7 +218,7 @@ db:
218218
t.Run(tt.name, func(t *testing.T) {
219219
got := replaceString(tt.parsedMap, tt.inputStr)
220220
if got != tt.expectStr {
221-
t.Errorf(testutils.TestPhrase, tt.expectStr, got)
221+
t.Errorf(testutils.TestPhrase, got, tt.expectStr)
222222
}
223223
})
224224
}

internal/cmdutils/cmdutils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type confMgrRetrieveWithInputReplacediface interface {
1919
}
2020

2121
type CmdUtils struct {
22-
cfgmgr configmanager.CMRetrieveWithInputReplacediface
22+
cfgmgr confMgrRetrieveWithInputReplacediface
2323
generator generator.GenVarsiface
2424
}
2525

internal/cmdutils/cmdutils_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func Test_generateStrOutFromInput(t *testing.T) {
8888
t.Fatal(err)
8989
}
9090
if string(got) != string(want) {
91-
t.Errorf(testutils.TestPhrase, string(want), string(got))
91+
t.Errorf(testutils.TestPhrase, string(got), string(want))
9292
}
9393
})
9494
}
@@ -137,7 +137,7 @@ func Test_generateFromStrOutOverwrite(t *testing.T) {
137137
t.Fatal(err)
138138
}
139139
if string(got) != string(want) {
140-
t.Errorf(testutils.TestPhrase, string(want), string(got))
140+
t.Errorf(testutils.TestPhrase, string(got), string(want))
141141
}
142142
})
143143
}

internal/testutils/testutils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package testutils
22

33
const (
4-
TestPhrase string = "Want: %v\nGot: %v"
4+
TestPhrase string = "got: %v want: %v\n"
55
)

pkg/generator/generator.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,32 @@ import (
1212
"github.com/dnitsch/configmanager/pkg/log"
1313
)
1414

15+
type ImplementationPrefix string
16+
17+
const (
18+
// AWS SecretsManager prefix
19+
SecretMgrPrefix ImplementationPrefix = "AWSSECRETS"
20+
// AWS Parameter Store prefix
21+
ParamStorePrefix ImplementationPrefix = "AWSPARAMSTR"
22+
// Azure Key Vault Secrets prefix
23+
AzKeyVaultSecretsPrefix ImplementationPrefix = "AZKVSECRET"
24+
// Hashicorp Vault prefix
25+
HashicorpVaultPrefix ImplementationPrefix = "VAULT"
26+
)
27+
1528
const (
1629
// tokenSeparator used for identifying the end of a prefix and beginning of token
1730
// see notes about special consideration for AZKVSECRET tokens
1831
tokenSeparator = "#"
1932
// keySeparator used for accessing nested objects within the retrieved map
2033
keySeparator = "|"
21-
// AWS SecretsManager prefix
22-
SecretMgrPrefix = "AWSSECRETS"
23-
// AWS Parameter Store prefix
24-
ParamStorePrefix = "AWSPARAMSTR"
25-
// Azure Key Vault Secrets prefix
26-
AzKeyVaultSecretsPrefix = "AZKVSECRET"
2734
)
2835

2936
var (
3037
// default varPrefix used by the replacer function
31-
// any token msut beging with one of these else
38+
// any token must beging with one of these else
3239
// it will be skipped as not a replaceable token
33-
VarPrefix = map[string]bool{SecretMgrPrefix: true, ParamStorePrefix: true, AzKeyVaultSecretsPrefix: true}
40+
VarPrefix = map[ImplementationPrefix]bool{SecretMgrPrefix: true, ParamStorePrefix: true, AzKeyVaultSecretsPrefix: true, HashicorpVaultPrefix: true}
3441
)
3542

3643
// Generatoriface describes the exported methods
@@ -163,7 +170,7 @@ func (c *GenVars) Generate(tokens []string) (ParsedMap, error) {
163170
rawTokenPrefixMap := map[string]string{}
164171
for _, token := range tokens {
165172
prefix := strings.Split(token, c.config.tokenSeparator)[0]
166-
if found := VarPrefix[prefix]; found {
173+
if found := VarPrefix[ImplementationPrefix(prefix)]; found {
167174
rawTokenPrefixMap[token] = prefix
168175
}
169176
}
@@ -200,11 +207,11 @@ func (c *GenVars) generate(rawMap map[string]string) (ParsedMap, error) {
200207

201208
wg.Add(initChanLen)
202209
for token, prefix := range rawMap {
203-
go func(a, p string) {
210+
go func(a string, p ImplementationPrefix) {
204211
defer wg.Done()
205212
rs := newRetrieveStrategy(NewDefatultStrategy(), c.config)
206213
outCh <- rs.retrieveSpecificCh(c.ctx, p, a)
207-
}(token, prefix)
214+
}(token, ImplementationPrefix(prefix))
208215
}
209216

210217
go func() {

pkg/generator/generator_test.go

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func newFixture(t *testing.T) *fixture {
2727
return f
2828
}
2929

30-
func (f *fixture) goodGenVars(op, ts string) {
30+
func (f *fixture) configGenVars(op, ts string) {
3131
conf := NewConfig().WithOutputPath(op).WithTokenSeparator(ts)
3232
gv := NewGenerator().WithConfig(conf)
3333
f.rs = newRetrieveStrategy(NewDefatultStrategy(), *conf)
@@ -38,40 +38,46 @@ func TestGenVarsWithConfig(t *testing.T) {
3838

3939
f := newFixture(t)
4040

41-
f.goodGenVars(customop, customts)
41+
f.configGenVars(customop, customts)
4242
if f.c.config.outpath != customop {
43-
f.t.Errorf(testutils.TestPhrase, customop, f.c.config.outpath)
43+
f.t.Errorf(testutils.TestPhrase, f.c.config.outpath, customop)
4444
}
4545
if f.c.config.tokenSeparator != customts {
46-
f.t.Errorf(testutils.TestPhrase, customts, f.c.config.tokenSeparator)
46+
f.t.Errorf(testutils.TestPhrase, f.c.config.tokenSeparator, customts)
4747
}
4848
}
4949

5050
func TestStripPrefixNormal(t *testing.T) {
51-
52-
want := "/normal/without/prefix"
53-
prefix := SecretMgrPrefix
54-
f := newFixture(t)
55-
f.goodGenVars(standardop, standardts)
56-
57-
got := f.rs.stripPrefix(fmt.Sprintf("%s#%s", prefix, want), prefix)
58-
if got != want {
59-
f.t.Errorf(testutils.TestPhrase, want, got)
51+
ttests := map[string]struct {
52+
prefix ImplementationPrefix
53+
token string
54+
keySeparator string
55+
tokenSeparator string
56+
f *fixture
57+
expect string
58+
}{
59+
"standard azkv": {AzKeyVaultSecretsPrefix, "AZKVSECRET://vault1/secret2", "|", "://", newFixture(t), "vault1/secret2"},
60+
"standard hashivault": {HashicorpVaultPrefix, "VAULT://vault1/secret2", "|", "://", newFixture(t), "vault1/secret2"},
61+
"custom separator hashivault": {HashicorpVaultPrefix, "VAULT#vault1/secret2", "|", "#", newFixture(t), "vault1/secret2"},
6062
}
61-
62-
gotNegative := f.rs.stripPrefix(fmt.Sprintf("%s___%s", prefix, want), prefix)
63-
if gotNegative == want {
64-
f.t.Errorf(testutils.TestPhrase, want, gotNegative)
63+
for name, tt := range ttests {
64+
t.Run(name, func(t *testing.T) {
65+
tt.f.configGenVars(tt.keySeparator, tt.tokenSeparator)
66+
got := tt.f.rs.stripPrefix(tt.token, tt.prefix)
67+
if got != tt.expect {
68+
t.Errorf(testutils.TestPhrase, got, tt.expect)
69+
}
70+
})
6571
}
6672
}
6773

6874
func Test_stripPrefix(t *testing.T) {
6975
f := newFixture(t)
70-
f.goodGenVars(standardop, standardts)
76+
f.configGenVars(standardop, standardts)
7177
tests := []struct {
7278
name string
7379
token string
74-
prefix string
80+
prefix ImplementationPrefix
7581
expect string
7682
}{
7783
{
@@ -99,7 +105,7 @@ func Test_stripPrefix(t *testing.T) {
99105

100106
func Test_NormaliseMap(t *testing.T) {
101107
f := newFixture(t)
102-
f.goodGenVars(standardop, standardts)
108+
f.configGenVars(standardop, standardts)
103109
tests := []struct {
104110
name string
105111
gv *GenVars
@@ -133,7 +139,7 @@ func Test_NormaliseMap(t *testing.T) {
133139

134140
func Test_KeyLookup(t *testing.T) {
135141
f := newFixture(t)
136-
f.goodGenVars(standardop, standardts)
142+
f.configGenVars(standardop, standardts)
137143

138144
tests := []struct {
139145
name string
@@ -206,7 +212,7 @@ func Test_ConvertToExportVars(t *testing.T) {
206212
for _, tt := range tests {
207213
t.Run(tt.name, func(t *testing.T) {
208214
f := newFixture(t)
209-
f.goodGenVars(standardop, standardts)
215+
f.configGenVars(standardop, standardts)
210216
f.c.rawMap = tt.rawMap
211217
f.c.ConvertToExportVar()
212218
got := f.c.outString

0 commit comments

Comments
 (0)