Skip to content

Commit

Permalink
feat(gcpsecrets): add trim_nl parameter (#432)
Browse files Browse the repository at this point in the history
Signed-off-by: Zadkiel AHARONIAN <[email protected]>
  • Loading branch information
aslafy-z authored Jun 21, 2024
1 parent 2a4e64c commit d0702b4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ Examples:
- `ref+gcpsecrets://PROJECT/SECRET[?version=VERSION]`
- `ref+gcpsecrets://PROJECT/SECRET[?version=VERSION]#/yaml_or_json_key/in/secret`
- `ref+gcpsecrets://PROJECT/SECRET[?version=VERSION][&fallback=valuewhenkeyisnotfound][&optional=true][&trim_nl=true]#/yaml_or_json_key/in/secret`
Examples:
Expand Down
14 changes: 12 additions & 2 deletions pkg/providers/gcpsecrets/gcpsecrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ import (
"github.com/helmfile/vals/pkg/api"
)

// Format: ref+gcpsecrets://project/mykey[?version=VERSION][&fallback=value=valuewhenkeyisnotfound][&optional=true]#/yaml_or_json_key/in/secret
// Format: ref+gcpsecrets://project/mykey[?version=VERSION][&fallback=valuewhenkeyisnotfound][&optional=true][&trim_nl=true]#/yaml_or_json_key/in/secret
type provider struct {
version string
optional bool
fallback *string
trim_nl bool
}

func New(cfg api.StaticConfig) *provider {
p := &provider{
version: "latest",
optional: false,
fallback: nil,
trim_nl: false,
}
if v := cfg.String("version"); v != "" {
p.version = v
Expand All @@ -36,6 +38,9 @@ func New(cfg api.StaticConfig) *provider {
if v := cfg.String("fallback_value"); cfg.Exists("fallback_value") {
p.fallback = &v
}
if v := cfg.String("trim_nl"); v != "" {
p.trim_nl, _ = strconv.ParseBool(v)
}
return p
}

Expand Down Expand Up @@ -80,5 +85,10 @@ func (p *provider) getSecret(ctx context.Context, key string) ([]byte, error) {

return nil, fmt.Errorf("failed to get secret: %w", err)
}
return secret.GetPayload().GetData(), nil

buf := secret.GetPayload().GetData()
if p.trim_nl {
buf = []byte(strings.TrimSuffix(string(buf), "\n"))
}
return buf, nil
}
17 changes: 17 additions & 0 deletions vals_gcpsecrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,23 @@ func TestValues_GCPSecretsManager(t *testing.T) {
},
},
},
{
"trim_nl string",
map[string]string{"valstestvar": "foo: bar\n"},
map[string]interface{}{
"provider": map[string]interface{}{
"name": "gcpsecrets",
"version": "latest",
"type": "string",
"path": projectId,
"trim_nl": true,
},
"inline": map[string]interface{}{
"valstestvar": "valstestvar",
},
},
map[string]interface{}{"valstestvar": "foo: bar"},
},
}

for i := range tests {
Expand Down

0 comments on commit d0702b4

Please sign in to comment.