Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions pkg/generator/gcpsecrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ type gcpSecretsApi interface {
}

type GcpSecrets struct {
svc gcpSecretsApi
ctx context.Context
close func() error
token string
svc gcpSecretsApi
ctx context.Context
config TokenConfigVars
close func() error
token string
}

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

func (imp *GcpSecrets) setToken(token string) {
imp.token = token
ct := (GenVarsConfig{}).ParseTokenVars(token)
imp.config = ct
imp.token = ct.Token
}

func (imp *GcpSecrets) getTokenValue(v *retrieveStrategy) (string, error) {
defer imp.close()

log.Infof("%s", "Concrete implementation GcpSecrets")
log.Infof("Getting Secret: %s", imp.token)

version := "latest"
if imp.config.Version != "" {
version = imp.config.Version
}

log.Infof("Getting Secret: %s @version: %s", imp.token, version)

input := &gcpsecretspb.AccessSecretVersionRequest{
Name: fmt.Sprintf("%s/versions/latest", v.stripPrefix(imp.token, GcpSecretsPrefix)),
Name: fmt.Sprintf("%s/versions/%s", v.stripPrefix(imp.token, GcpSecretsPrefix), version),
}

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

Expand Down
10 changes: 10 additions & 0 deletions pkg/generator/gcpsecrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ func Test_GetGcpSecretVarHappy(t *testing.T) {
})
}, NewConfig().WithTokenSeparator("#").WithKeySeparator("|"),
},
"success with version": {"GCPSECRETS#/token/1[version:123]", "someValue", func(t *testing.T) gcpSecretsApi {
return mockGcpSecretsApi(func(ctx context.Context, req *gcpsecretspb.AccessSecretVersionRequest, opts ...gax.CallOption) (*gcpsecretspb.AccessSecretVersionResponse, error) {
t.Helper()
gcpSecretsGetChecker(t, req)
return &gcpsecretspb.AccessSecretVersionResponse{
Payload: &gcpsecretspb.SecretPayload{Data: []byte("someValue")},
}, nil
})
}, NewConfig().WithTokenSeparator("#").WithKeySeparator("|"),
},
"error": {"GCPSECRETS#/token/1", "unable to retrieve secret", func(t *testing.T) gcpSecretsApi {
return mockGcpSecretsApi(func(ctx context.Context, req *gcpsecretspb.AccessSecretVersionRequest, opts ...gax.CallOption) (*gcpsecretspb.AccessSecretVersionResponse, error) {
t.Helper()
Expand Down
3 changes: 2 additions & 1 deletion pkg/generator/keyvault.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ func (imp *KvScrtStore) getTokenValue(v *retrieveStrategy) (string, error) {
defer cancel()

// secretVersion as "" => latest
s, err := imp.svc.GetSecret(ctx, imp.token, "", nil)
// imp.config.Version will default `""` if not specified
s, err := imp.svc.GetSecret(ctx, imp.token, imp.config.Version, nil)
if err != nil {
log.Errorf(implementationNetworkErr, AzKeyVaultSecretsPrefix, err, imp.token)
return "", err
Expand Down
12 changes: 11 additions & 1 deletion pkg/generator/keyvault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (m mockAzKvSecretApi) GetSecret(ctx context.Context, name string, version s
return m(ctx, name, version, options)
}

func Test_GetAzKeyVaultSecretVarHappy(t *testing.T) {
func TestAzKeyVault(t *testing.T) {

tests := map[string]struct {
token string
Expand All @@ -107,6 +107,16 @@ func Test_GetAzKeyVaultSecretVarHappy(t *testing.T) {
})
}, NewConfig().WithKeySeparator("|").WithTokenSeparator("#"),
},
"successVal with version": {"AZKVSECRET#/test-vault//token/1[version:123]", tsuccessParam, func(t *testing.T) kvApi {
return mockAzKvSecretApi(func(ctx context.Context, name string, version string, options *azsecrets.GetSecretOptions) (azsecrets.GetSecretResponse, error) {
t.Helper()
azKvCommonGetSecretChecker(t, name, "", "/token/1")
resp := azsecrets.GetSecretResponse{}
resp.Value = &tsuccessParam
return resp, nil
})
}, NewConfig().WithKeySeparator("|").WithTokenSeparator("#"),
},
"successVal with keyseparator": {"AZKVSECRET#/test-vault/token/1|somekey", tsuccessParam, func(t *testing.T) kvApi {
return mockAzKvSecretApi(func(ctx context.Context, name string, version string, options *azsecrets.GetSecretOptions) (azsecrets.GetSecretResponse, error) {
t.Helper()
Expand Down
11 changes: 7 additions & 4 deletions pkg/generator/paramstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ type paramStoreApi interface {
}

type ParamStore struct {
svc paramStoreApi
ctx context.Context
token string
svc paramStoreApi
ctx context.Context
config TokenConfigVars
token string
}

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

func (imp *ParamStore) setToken(token string) {
imp.token = token
ct := (GenVarsConfig{}).ParseTokenVars(token)
imp.config = ct
imp.token = ct.Token
}

func (imp *ParamStore) getTokenValue(v *retrieveStrategy) (string, error) {
Expand Down
24 changes: 16 additions & 8 deletions pkg/generator/secretsmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ type secretsMgrApi interface {
}

type SecretsMgr struct {
svc secretsMgrApi
ctx context.Context
tokenConfig TokenConfigVars
token string
svc secretsMgrApi
ctx context.Context
config TokenConfigVars
token string
}

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

func (imp *SecretsMgr) setToken(token string) {
imp.token = token
ct := (GenVarsConfig{}).ParseTokenVars(token)
imp.config = ct
imp.token = ct.Token
}

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

log.Infof("%s", "Concrete implementation SecretsManager")
log.Infof("Getting Secret: %s", imp.token)

version := "AWSCURRENT"
if imp.config.Version != "" {
version = imp.config.Version
}

log.Infof("Getting Secret: %s @version: %s", imp.token, version)

input := &secretsmanager.GetSecretValueInput{
SecretId: aws.String(v.stripPrefix(imp.token, SecretMgrPrefix)),
VersionStage: aws.String("AWSCURRENT"),
VersionStage: aws.String(version),
}

ctx, cancel := context.WithCancel(imp.ctx)
Expand Down
12 changes: 11 additions & 1 deletion pkg/generator/secretsmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ func Test_GetSecretMgr(t *testing.T) {
})
}, NewConfig(),
},
"success with version": {"AWSSECRETS#/token/1[version:123]", "|", "#", tsuccessParam, func(t *testing.T) secretsMgrApi {
return mockSecretsApi(func(ctx context.Context, params *secretsmanager.GetSecretValueInput, optFns ...func(*secretsmanager.Options)) (*secretsmanager.GetSecretValueOutput, error) {
t.Helper()
awsSecretsMgrGetChecker(t, params)
return &secretsmanager.GetSecretValueOutput{
SecretString: &tsuccessSecret,
}, nil
})
}, NewConfig(),
},
"success with binary": {"AWSSECRETS#/token/1", "|", "#", tsuccessParam, func(t *testing.T) secretsMgrApi {
return mockSecretsApi(func(ctx context.Context, params *secretsmanager.GetSecretValueInput, optFns ...func(*secretsmanager.Options)) (*secretsmanager.GetSecretValueOutput, error) {
t.Helper()
Expand Down Expand Up @@ -85,7 +95,7 @@ func Test_GetSecretMgr(t *testing.T) {
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
tt.config.WithTokenSeparator(tt.tokenSeparator).WithKeySeparator(tt.keySeparator)
impl, _ := NewSecretsMgr(context.TODO(), *tt.config)
impl, _ := NewSecretsMgr(context.TODO())
impl.svc = tt.mockClient(t)
rs := newRetrieveStrategy(NewDefatultStrategy(), *tt.config)

Expand Down
2 changes: 1 addition & 1 deletion pkg/generator/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (rs *retrieveStrategy) RetrieveByToken(ctx context.Context, impl genVarsStr
func (rs *retrieveStrategy) SelectImplementation(ctx context.Context, prefix ImplementationPrefix, in string, config GenVarsConfig) (genVarsStrategy, error) {
switch prefix {
case SecretMgrPrefix:
return NewSecretsMgr(ctx, config)
return NewSecretsMgr(ctx)
case ParamStorePrefix:
return NewParamStore(ctx)
case AzKeyVaultSecretsPrefix:
Expand Down
2 changes: 1 addition & 1 deletion pkg/generator/strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestSelectImpl(t *testing.T) {
context.TODO(),
SecretMgrPrefix, "AWSSECRETS://foo/bar", (&GenVarsConfig{}).WithKeySeparator("|").WithTokenSeparator("://"),
func(t *testing.T, ctx context.Context, conf GenVarsConfig) genVarsStrategy {
imp, err := NewSecretsMgr(ctx, conf)
imp, err := NewSecretsMgr(ctx)
if err != nil {
t.Errorf(testutils.TestPhraseWithContext, "aws secrets init impl error", err.Error(), nil)
}
Expand Down