Skip to content

Commit

Permalink
support secret write queries w/ an empty write
Browse files Browse the repository at this point in the history
Vault uses the write API for create+read and the create call doesn't
always take write key/value pairs (requires no fields to be provided)
but these cases would error out on absense of k/v pair. This change
simply skips the k/v check if the parameter string field is empty.
  • Loading branch information
eikenb committed Nov 8, 2021
1 parent 0400fa2 commit a1e9af8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
7 changes: 5 additions & 2 deletions template/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,12 @@ func secretFunc(b *Brain, used, missing *dep.Set) func(...string) (*dep.Secret,
return result, nil
}

// TODO: Refactor into separate template functions
path, rest := s[0], s[1:]
data := make(map[string]interface{})
for _, str := range rest {
if len(str) == 0 {
continue
}
parts := strings.SplitN(str, "=", 2)
if len(parts) != 2 {
return result, fmt.Errorf("not k=v pair %q", str)
Expand All @@ -358,7 +360,8 @@ func secretFunc(b *Brain, used, missing *dep.Set) func(...string) (*dep.Secret,
var d dep.Dependency
var err error

if len(rest) == 0 {
isReadQuery := len(rest) == 0
if isReadQuery {
d, err = dep.NewVaultReadQuery(path)
} else {
d, err = dep.NewVaultWriteQuery(path, data)
Expand Down
21 changes: 21 additions & 0 deletions template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,27 @@ func TestTemplate_Execute(t *testing.T) {
"encrypted",
false,
},
{
"func_secret_write_empty",
&NewTemplateInput{
Contents: `{{ with secret "transit/encrypt/foo" "" }}{{ .Data.ciphertext }}{{ end }}`,
},
&ExecuteInput{
Brain: func() *Brain {
b := NewBrain()
d, err := dep.NewVaultWriteQuery("transit/encrypt/foo", nil)
if err != nil {
t.Fatal(err)
}
b.Remember(d, &dep.Secret{
Data: map[string]interface{}{"ciphertext": "encrypted"},
})
return b
}(),
},
"encrypted",
false,
},
{
"func_secret_write_no_exist",
&NewTemplateInput{
Expand Down

0 comments on commit a1e9af8

Please sign in to comment.