Skip to content

Commit

Permalink
add "optional" option to gcpsecrets provider
Browse files Browse the repository at this point in the history
  • Loading branch information
koenpunt committed Sep 5, 2022
1 parent b5ed179 commit 9c1a24f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
22 changes: 18 additions & 4 deletions pkg/providers/gcpsecrets/gcpsecrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"strconv"
"strings"

"gopkg.in/yaml.v3"
Expand All @@ -16,16 +17,18 @@ import (

// Format: ref+gcpsecrets://project/mykey[?version=VERSION]#/yaml_or_json_key/in/secret
type provider struct {
client *sm.Client
ctx context.Context
version string
client *sm.Client
ctx context.Context
version string
optional bool
}

func New(cfg api.StaticConfig) *provider {
ctx := context.Background()

p := &provider{
ctx: ctx,
ctx: ctx,
optional: false,
}

version := cfg.String("version")
Expand All @@ -35,6 +38,14 @@ func New(cfg api.StaticConfig) *provider {
p.version = version
}

optional := cfg.String("optional")
if optional != "" {
val, err := strconv.ParseBool(optional)
if err == nil {
p.optional = val
}
}

return p
}

Expand Down Expand Up @@ -80,6 +91,9 @@ func (p *provider) getSecretBytes(key string) ([]byte, error) {
},
)
if err != nil {
if p.optional {
return []byte(""), nil
}
return nil, fmt.Errorf("failed to get secret: %w", err)
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/providers/gcpsecrets/gcpsecrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ func Test_New(t *testing.T) {
options map[string]interface{}
want provider
}{
{"latest", map[string]interface{}{"version": "latest"}, provider{version: "latest"}},
{"latest", map[string]interface{}{"version": "latest"}, provider{version: "latest", optional: false}},
{"optional", map[string]interface{}{"version": "latest", "optional": true}, provider{version: "latest", optional: true}},
}

for _, tt := range tests {
Expand Down
17 changes: 17 additions & 0 deletions vals_gcpsecrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ func TestValues_GCPSecretsManager(t *testing.T) {
},
map[string]interface{}{"valstestvar": "foo: bar"},
},
{
"optional string",
map[string]string{},
map[string]interface{}{
"provider": map[string]interface{}{
"name": "gcpsecrets",
"version": "1",
"type": "string",
"path": projectId,
"optional": true,
},
"inline": map[string]interface{}{
"missingvar": "",
},
},
map[string]interface{}{"missingvar": ""},
},
{
"v1 map",
map[string]string{"valstestvar": "foo: bar"},
Expand Down

0 comments on commit 9c1a24f

Please sign in to comment.