Skip to content

Commit

Permalink
fix: Referencing an unknown parameter in a compound parameter should …
Browse files Browse the repository at this point in the history
…produce an error (#1761)

This PR ensures that referencing an unknown parameter within a compound
parameter produces an error.
  • Loading branch information
arthurpitman authored Mar 4, 2025
1 parent 66722b3 commit 796a6a5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
12 changes: 9 additions & 3 deletions pkg/config/parameter/compound/compound.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ package compound
import (
"bytes"
"fmt"
templ "text/template" // nosemgrep: go.lang.security.audit.xss.import-text-template.import-text-template

"github.com/google/go-cmp/cmp"

"github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/strings"
template2 "github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/template"
"github.com/google/go-cmp/cmp"
templ "text/template" // nosemgrep: go.lang.security.audit.xss.import-text-template.import-text-template

"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/parameter"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/template"
Expand Down Expand Up @@ -68,7 +70,11 @@ func (p *CompoundParameter) ResolveValue(context parameter.ResolveContext) (inte
compoundData := make(map[string]interface{})

for _, param := range p.referencedParameters {
compoundData[param.Property] = context.ResolvedParameterValues[param.Property]
value, ok := context.ResolvedParameterValues[param.Property]
if !ok {
return nil, fmt.Errorf("unknown parameter '%s'", param.Property)
}
compoundData[param.Property] = value
}

out := bytes.Buffer{}
Expand Down
33 changes: 29 additions & 4 deletions pkg/config/parameter/compound/compound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
package compound

import (
"github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"

"github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/strings"

"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/parameter"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/parameter/value"
Expand Down Expand Up @@ -145,8 +147,13 @@ func TestResolveComplexValue(t *testing.T) {
assert.Equal(t, "Hansi is 12 years old", strings.ToString(result))
}

// TestResolveValueErrorOnUndefinedReference tests that resolving a compound parameter using an undefined reference results in an error.
func TestResolveValueErrorOnUndefinedReference(t *testing.T) {
testFormat := "{{ .firstName }} {{ .lastName }}"
testFormat := "Hi {{ .name }} "
compoundParameter, err := New("testName", testFormat,
[]parameter.ParameterReference{{Property: "firstName"}})
require.NoError(t, err)

context := parameter.ResolveContext{
ResolvedParameterValues: parameter.Properties{
"person": map[string]interface{}{
Expand All @@ -155,13 +162,31 @@ func TestResolveValueErrorOnUndefinedReference(t *testing.T) {
},
},
}

_, err = compoundParameter.ResolveValue(context)

require.Error(t, err, "expected an error resolving undefined references")
}

// TestResolveValueErrorOnUnknownParameter tests that resolving a compound parameter that references an unknown parameter results in an error.
func TestResolveValueErrorOnUnknownParameter(t *testing.T) {
testFormat := "Hi {{ .firstName }}"
compoundParameter, err := New("testName", testFormat,
[]parameter.ParameterReference{{Property: "firstName"}})
require.NoError(t, err)

context := parameter.ResolveContext{
ResolvedParameterValues: parameter.Properties{
"person": map[string]interface{}{
"age": 12,
"name": "Hansi",
},
},
}

_, err = compoundParameter.ResolveValue(context)

require.Error(t, err, "expected an error resolving undefined references")
require.Error(t, err, "expected an error resolving undefined parameter")
}

func TestWriteCompoundParameter(t *testing.T) {
Expand Down

0 comments on commit 796a6a5

Please sign in to comment.