Skip to content

Commit

Permalink
Merge pull request #1535 from hashicorp/issue-1418-nil-pointer-eval
Browse files Browse the repository at this point in the history
fix issue returning typed nil pointers
  • Loading branch information
eikenb authored Dec 3, 2021
2 parents 123fbe9 + db9aff9 commit 4b10501
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
23 changes: 10 additions & 13 deletions template/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ func lsFunc(b *Brain, used, missing *dep.Set, emptyIsSafe bool) func(string) ([]
}

// nodeFunc returns or accumulates catalog node dependency.
func nodeFunc(b *Brain, used, missing *dep.Set) func(...string) (*dep.CatalogNode, error) {
return func(s ...string) (*dep.CatalogNode, error) {
func nodeFunc(b *Brain, used, missing *dep.Set) func(...string) (interface{}, error) {
return func(s ...string) (interface{}, error) {

d, err := dep.NewCatalogNodeQuery(strings.Join(s, ""))
if err != nil {
Expand Down Expand Up @@ -334,12 +334,10 @@ func nodesFunc(b *Brain, used, missing *dep.Set) func(...string) ([]*dep.Node, e
}

// secretFunc returns or accumulates secret dependencies from Vault.
func secretFunc(b *Brain, used, missing *dep.Set) func(...string) (*dep.Secret, error) {
return func(s ...string) (*dep.Secret, error) {
var result *dep.Secret

func secretFunc(b *Brain, used, missing *dep.Set) func(...string) (interface{}, error) {
return func(s ...string) (interface{}, error) {
if len(s) == 0 {
return result, nil
return nil, nil
}

path, rest := s[0], s[1:]
Expand All @@ -350,7 +348,7 @@ func secretFunc(b *Brain, used, missing *dep.Set) func(...string) (*dep.Secret,
}
parts := strings.SplitN(str, "=", 2)
if len(parts) != 2 {
return result, fmt.Errorf("not k=v pair %q", str)
return nil, fmt.Errorf("not k=v pair %q", str)
}

k, v := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
Expand All @@ -374,13 +372,12 @@ func secretFunc(b *Brain, used, missing *dep.Set) func(...string) (*dep.Secret,
used.Add(d)

if value, ok := b.Recall(d); ok {
result = value.(*dep.Secret)
return result, nil
return value.(*dep.Secret), nil
}

missing.Add(d)

return result, nil
return nil, nil
}
}

Expand Down Expand Up @@ -542,8 +539,8 @@ func connectCARootsFunc(b *Brain, used, missing *dep.Set,
}

func connectLeafFunc(b *Brain, used, missing *dep.Set,
) func(...string) (*api.LeafCert, error) {
return func(s ...string) (*api.LeafCert, error) {
) func(...string) (interface{}, error) {
return func(s ...string) (interface{}, error) {
if len(s) == 0 || s[0] == "" {
return nil, nil
}
Expand Down
33 changes: 33 additions & 0 deletions template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,17 @@ func TestTemplate_Execute(t *testing.T) {
"node1service1",
false,
},
{
"func_node_nil_pointer_evaluation",
&NewTemplateInput{
Contents: `{{ $v := node }}{{ $v.Node }}`,
},
&ExecuteInput{
Brain: NewBrain(),
},
"<no value>",
false,
},
{
"func_nodes",
&NewTemplateInput{
Expand Down Expand Up @@ -475,6 +486,17 @@ func TestTemplate_Execute(t *testing.T) {
"zap",
false,
},
{
"func_secret_nil_pointer_evaluation",
&NewTemplateInput{
Contents: `{{ $v := secret "secret/foo" }}{{ $v.Data.zip }}`,
},
&ExecuteInput{
Brain: NewBrain(),
},
"<no value>",
false,
},
{
"func_secret_read_versions",
&NewTemplateInput{
Expand Down Expand Up @@ -1844,6 +1866,17 @@ func TestTemplate_Execute(t *testing.T) {
"PEMKEY",
false,
},
{
"leaf_cert_nil_pointer_evaluation",
&NewTemplateInput{
Contents: `{{ $v := caLeaf "foo" }}{{ $v.CertPEM }}`,
},
&ExecuteInput{
Brain: NewBrain(),
},
"<no value>",
false,
},
{
"root_ca",
&NewTemplateInput{
Expand Down

0 comments on commit 4b10501

Please sign in to comment.