Skip to content

Commit fca19fb

Browse files
authored
fix: add the ability to specify the version (#19)
* fix: add the ability to specify the version use implementation's own version designation e.g. `1` in VAULT or `latest` in GCPSECRETS +semver: feature * fix: add tests to cover the version specifier
1 parent 5663804 commit fca19fb

File tree

9 files changed

+76
-25
lines changed

9 files changed

+76
-25
lines changed

pkg/generator/gcpsecrets.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ type gcpSecretsApi interface {
1515
}
1616

1717
type GcpSecrets struct {
18-
svc gcpSecretsApi
19-
ctx context.Context
20-
close func() error
21-
token string
18+
svc gcpSecretsApi
19+
ctx context.Context
20+
config TokenConfigVars
21+
close func() error
22+
token string
2223
}
2324

2425
func NewGcpSecrets(ctx context.Context) (*GcpSecrets, error) {
@@ -37,18 +38,26 @@ func NewGcpSecrets(ctx context.Context) (*GcpSecrets, error) {
3738
}
3839

3940
func (imp *GcpSecrets) setToken(token string) {
40-
imp.token = token
41+
ct := (GenVarsConfig{}).ParseTokenVars(token)
42+
imp.config = ct
43+
imp.token = ct.Token
4144
}
4245

4346
func (imp *GcpSecrets) getTokenValue(v *retrieveStrategy) (string, error) {
4447
defer imp.close()
45-
4648
log.Infof("%s", "Concrete implementation GcpSecrets")
47-
log.Infof("Getting Secret: %s", imp.token)
49+
50+
version := "latest"
51+
if imp.config.Version != "" {
52+
version = imp.config.Version
53+
}
54+
55+
log.Infof("Getting Secret: %s @version: %s", imp.token, version)
4856

4957
input := &gcpsecretspb.AccessSecretVersionRequest{
50-
Name: fmt.Sprintf("%s/versions/latest", v.stripPrefix(imp.token, GcpSecretsPrefix)),
58+
Name: fmt.Sprintf("%s/versions/%s", v.stripPrefix(imp.token, GcpSecretsPrefix), version),
5159
}
60+
5261
ctx, cancel := context.WithCancel(imp.ctx)
5362
defer cancel()
5463

pkg/generator/gcpsecrets_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ func Test_GetGcpSecretVarHappy(t *testing.T) {
7979
})
8080
}, NewConfig().WithTokenSeparator("#").WithKeySeparator("|"),
8181
},
82+
"success with version": {"GCPSECRETS#/token/1[version:123]", "someValue", func(t *testing.T) gcpSecretsApi {
83+
return mockGcpSecretsApi(func(ctx context.Context, req *gcpsecretspb.AccessSecretVersionRequest, opts ...gax.CallOption) (*gcpsecretspb.AccessSecretVersionResponse, error) {
84+
t.Helper()
85+
gcpSecretsGetChecker(t, req)
86+
return &gcpsecretspb.AccessSecretVersionResponse{
87+
Payload: &gcpsecretspb.SecretPayload{Data: []byte("someValue")},
88+
}, nil
89+
})
90+
}, NewConfig().WithTokenSeparator("#").WithKeySeparator("|"),
91+
},
8292
"error": {"GCPSECRETS#/token/1", "unable to retrieve secret", func(t *testing.T) gcpSecretsApi {
8393
return mockGcpSecretsApi(func(ctx context.Context, req *gcpsecretspb.AccessSecretVersionRequest, opts ...gax.CallOption) (*gcpsecretspb.AccessSecretVersionResponse, error) {
8494
t.Helper()

pkg/generator/keyvault.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ func (imp *KvScrtStore) getTokenValue(v *retrieveStrategy) (string, error) {
7575
defer cancel()
7676

7777
// secretVersion as "" => latest
78-
s, err := imp.svc.GetSecret(ctx, imp.token, "", nil)
78+
// imp.config.Version will default `""` if not specified
79+
s, err := imp.svc.GetSecret(ctx, imp.token, imp.config.Version, nil)
7980
if err != nil {
8081
log.Errorf(implementationNetworkErr, AzKeyVaultSecretsPrefix, err, imp.token)
8182
return "", err

pkg/generator/keyvault_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (m mockAzKvSecretApi) GetSecret(ctx context.Context, name string, version s
8989
return m(ctx, name, version, options)
9090
}
9191

92-
func Test_GetAzKeyVaultSecretVarHappy(t *testing.T) {
92+
func TestAzKeyVault(t *testing.T) {
9393

9494
tests := map[string]struct {
9595
token string
@@ -107,6 +107,16 @@ func Test_GetAzKeyVaultSecretVarHappy(t *testing.T) {
107107
})
108108
}, NewConfig().WithKeySeparator("|").WithTokenSeparator("#"),
109109
},
110+
"successVal with version": {"AZKVSECRET#/test-vault//token/1[version:123]", tsuccessParam, func(t *testing.T) kvApi {
111+
return mockAzKvSecretApi(func(ctx context.Context, name string, version string, options *azsecrets.GetSecretOptions) (azsecrets.GetSecretResponse, error) {
112+
t.Helper()
113+
azKvCommonGetSecretChecker(t, name, "", "/token/1")
114+
resp := azsecrets.GetSecretResponse{}
115+
resp.Value = &tsuccessParam
116+
return resp, nil
117+
})
118+
}, NewConfig().WithKeySeparator("|").WithTokenSeparator("#"),
119+
},
110120
"successVal with keyseparator": {"AZKVSECRET#/test-vault/token/1|somekey", tsuccessParam, func(t *testing.T) kvApi {
111121
return mockAzKvSecretApi(func(ctx context.Context, name string, version string, options *azsecrets.GetSecretOptions) (azsecrets.GetSecretResponse, error) {
112122
t.Helper()

pkg/generator/paramstore.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ type paramStoreApi interface {
1414
}
1515

1616
type ParamStore struct {
17-
svc paramStoreApi
18-
ctx context.Context
19-
token string
17+
svc paramStoreApi
18+
ctx context.Context
19+
config TokenConfigVars
20+
token string
2021
}
2122

2223
func NewParamStore(ctx context.Context) (*ParamStore, error) {
@@ -34,7 +35,9 @@ func NewParamStore(ctx context.Context) (*ParamStore, error) {
3435
}
3536

3637
func (imp *ParamStore) setToken(token string) {
37-
imp.token = token
38+
ct := (GenVarsConfig{}).ParseTokenVars(token)
39+
imp.config = ct
40+
imp.token = ct.Token
3841
}
3942

4043
func (imp *ParamStore) getTokenValue(v *retrieveStrategy) (string, error) {

pkg/generator/secretsmanager.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ type secretsMgrApi interface {
1414
}
1515

1616
type SecretsMgr struct {
17-
svc secretsMgrApi
18-
ctx context.Context
19-
tokenConfig TokenConfigVars
20-
token string
17+
svc secretsMgrApi
18+
ctx context.Context
19+
config TokenConfigVars
20+
token string
2121
}
2222

23-
func NewSecretsMgr(ctx context.Context, conf GenVarsConfig) (*SecretsMgr, error) {
23+
func NewSecretsMgr(ctx context.Context) (*SecretsMgr, error) {
2424
cfg, err := config.LoadDefaultConfig(ctx)
2525
if err != nil {
2626
log.Errorf("unable to load SDK config, %v", err)
@@ -40,17 +40,25 @@ func NewSecretsMgr(ctx context.Context, conf GenVarsConfig) (*SecretsMgr, error)
4040
// }
4141

4242
func (imp *SecretsMgr) setToken(token string) {
43-
imp.token = token
43+
ct := (GenVarsConfig{}).ParseTokenVars(token)
44+
imp.config = ct
45+
imp.token = ct.Token
4446
}
4547

4648
func (imp *SecretsMgr) getTokenValue(v *retrieveStrategy) (string, error) {
4749

4850
log.Infof("%s", "Concrete implementation SecretsManager")
49-
log.Infof("Getting Secret: %s", imp.token)
51+
52+
version := "AWSCURRENT"
53+
if imp.config.Version != "" {
54+
version = imp.config.Version
55+
}
56+
57+
log.Infof("Getting Secret: %s @version: %s", imp.token, version)
5058

5159
input := &secretsmanager.GetSecretValueInput{
5260
SecretId: aws.String(v.stripPrefix(imp.token, SecretMgrPrefix)),
53-
VersionStage: aws.String("AWSCURRENT"),
61+
VersionStage: aws.String(version),
5462
}
5563

5664
ctx, cancel := context.WithCancel(imp.ctx)

pkg/generator/secretsmanager_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ func Test_GetSecretMgr(t *testing.T) {
5353
})
5454
}, NewConfig(),
5555
},
56+
"success with version": {"AWSSECRETS#/token/1[version:123]", "|", "#", tsuccessParam, func(t *testing.T) secretsMgrApi {
57+
return mockSecretsApi(func(ctx context.Context, params *secretsmanager.GetSecretValueInput, optFns ...func(*secretsmanager.Options)) (*secretsmanager.GetSecretValueOutput, error) {
58+
t.Helper()
59+
awsSecretsMgrGetChecker(t, params)
60+
return &secretsmanager.GetSecretValueOutput{
61+
SecretString: &tsuccessSecret,
62+
}, nil
63+
})
64+
}, NewConfig(),
65+
},
5666
"success with binary": {"AWSSECRETS#/token/1", "|", "#", tsuccessParam, func(t *testing.T) secretsMgrApi {
5767
return mockSecretsApi(func(ctx context.Context, params *secretsmanager.GetSecretValueInput, optFns ...func(*secretsmanager.Options)) (*secretsmanager.GetSecretValueOutput, error) {
5868
t.Helper()
@@ -85,7 +95,7 @@ func Test_GetSecretMgr(t *testing.T) {
8595
for name, tt := range tests {
8696
t.Run(name, func(t *testing.T) {
8797
tt.config.WithTokenSeparator(tt.tokenSeparator).WithKeySeparator(tt.keySeparator)
88-
impl, _ := NewSecretsMgr(context.TODO(), *tt.config)
98+
impl, _ := NewSecretsMgr(context.TODO())
8999
impl.svc = tt.mockClient(t)
90100
rs := newRetrieveStrategy(NewDefatultStrategy(), *tt.config)
91101

pkg/generator/strategy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (rs *retrieveStrategy) RetrieveByToken(ctx context.Context, impl genVarsStr
5656
func (rs *retrieveStrategy) SelectImplementation(ctx context.Context, prefix ImplementationPrefix, in string, config GenVarsConfig) (genVarsStrategy, error) {
5757
switch prefix {
5858
case SecretMgrPrefix:
59-
return NewSecretsMgr(ctx, config)
59+
return NewSecretsMgr(ctx)
6060
case ParamStorePrefix:
6161
return NewParamStore(ctx)
6262
case AzKeyVaultSecretsPrefix:

pkg/generator/strategy_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func TestSelectImpl(t *testing.T) {
100100
context.TODO(),
101101
SecretMgrPrefix, "AWSSECRETS://foo/bar", (&GenVarsConfig{}).WithKeySeparator("|").WithTokenSeparator("://"),
102102
func(t *testing.T, ctx context.Context, conf GenVarsConfig) genVarsStrategy {
103-
imp, err := NewSecretsMgr(ctx, conf)
103+
imp, err := NewSecretsMgr(ctx)
104104
if err != nil {
105105
t.Errorf(testutils.TestPhraseWithContext, "aws secrets init impl error", err.Error(), nil)
106106
}

0 commit comments

Comments
 (0)