Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support complex types as parameter set values #3340

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/schema/parameter-set.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"value": {
"description": "Hard-coded value",
"type": "string"
"type": ["string", "number", "object", "array", "boolean"]
}
},
"additionalProperties": true
Expand Down Expand Up @@ -86,7 +86,7 @@
"schemaVersion": {
"description": "Version of the parameter set schema to which this document adheres",
"type": "string",
"default": "1.0.1"
"default": "1.1.0"
}
},
"required": [
Expand Down
22 changes: 22 additions & 0 deletions pkg/secrets/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ func (s *Source) UnmarshalRaw(raw map[string]interface{}) error {
s.Strategy = k
if value, ok := v.(string); ok {
s.Hint = value

} else if s.Strategy == "value" {
value, err := unmarshalRawValue(v)
if err != nil {
return err
}
s.Hint = value
} else {
s.Hint = fmt.Sprintf("%v", s.Hint)
Copy link
Author

@erikced erikced Feb 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit confused by this, as the hint should be an empty string at this point? I left it in nonetheless.

}
Expand All @@ -79,6 +86,21 @@ func (s *Source) UnmarshalRaw(raw map[string]interface{}) error {
}
}

func unmarshalRawValue(rawValue interface{}) (string, error) {
switch value := rawValue.(type) {
case []interface{}, map[string]interface{}:
encodedValue, err := json.Marshal(value)
if err != nil {
return "", fmt.Errorf("unable to convert %T into a string: %w", value, err)
}
return string(encodedValue), nil
case nil:
return "", nil
default:
return fmt.Sprintf("%v", value), nil
}
}

var (
_ json.Marshaler = Source{}
_ json.Unmarshaler = &Source{}
Expand Down
108 changes: 108 additions & 0 deletions pkg/secrets/strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,111 @@ func TestSet_Merge(t *testing.T) {
err = set.Merge(Set{"second": "bis"})
is.EqualError(err, `ambiguous value resolution: "second" is already present in base sets, cannot merge`)
}

func TestSource_UnmarshalRaw(t *testing.T) {
tests := []struct {
name string
raw map[string]interface{}
want Source
err string
}{
{
name: "empty map",
raw: map[string]interface{}{},
want: Source{},
},
{
name: "string",
raw: map[string]interface{}{
"env": "SOME_VALUE",
},
want: Source{
Strategy: "env",
Hint: "SOME_VALUE",
},
},
{
name: "array value",
raw: map[string]interface{}{
"value": []interface{}{1, 2, "3"},
},
want: Source{
Strategy: "value",
Hint: "[1,2,\"3\"]",
},
},
{
name: "map value",
raw: map[string]interface{}{
"value": map[string]interface{}{
"abc": "def",
},
},
want: Source{
Strategy: "value",
Hint: `{"abc":"def"}`,
},
},
{
name: "integer value",
raw: map[string]interface{}{
"value": 10,
},
want: Source{
Strategy: "value",
Hint: "10",
},
},
{
name: "float value",
raw: map[string]interface{}{
"value": 3.1415,
},
want: Source{
Strategy: "value",
Hint: "3.1415",
},
},
{
name: "boolean value",
raw: map[string]interface{}{
"value": true,
},
want: Source{
Strategy: "value",
Hint: "true",
},
},
{
name: "null value",
raw: map[string]interface{}{
"value": nil,
},
want: Source{
Strategy: "value",
Hint: "",
},
},
{
name: "multiple keys",
raw: map[string]interface{}{
"env": "abc",
"value": "def",
},
err: "multiple key/value pairs specified for source but only one may be defined",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
s := Source{}
err := s.UnmarshalRaw(tc.raw)

if tc.err != "" {
assert.EqualError(t, err, tc.err)
} else if err != nil {
t.Errorf("unexpected error: %v", err)
}
assert.Equal(t, tc.want, s)
})
}
}
4 changes: 2 additions & 2 deletions pkg/storage/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (

// DefaultParameterSetSchemaVersion represents the version associated with the schema
// // for parameter set documents.
DefaultParameterSetSchemaVersion = cnab.SchemaVersion("1.0.1")
DefaultParameterSetSchemaVersion = cnab.SchemaVersion("1.1.0")
)

var (
Expand All @@ -48,7 +48,7 @@ var (
SupportedInstallationSchemaVersions = schema.MustParseConstraint("1.0.2")

// SupportedParameterSetSchemaVersions represents the set of allowed schema versions for ParameterSet documents.
SupportedParameterSetSchemaVersions = schema.MustParseConstraint("1.0.1")
SupportedParameterSetSchemaVersions = schema.MustParseConstraint("1.0.1 || 1.1.0")
)

type Schema struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"schemaType": "ParameterSet",
"schemaVersion": "1.0.1",
"schemaVersion": "1.1.0",
"name": "NAME",
"namespace": "NAMESPACE",
"labels": {
Expand Down Expand Up @@ -38,4 +38,4 @@
}
}
]
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# See the Parameter Set file format documentation at https://porter.sh/reference/file-formats/#parameter-set
schemaType: ParameterSet
schemaVersion: 1.0.1
schemaVersion: 1.1.0
# Name of the parameter set.
name: NAME
# The namespace in which the parameter set is defined.
Expand Down Expand Up @@ -31,4 +31,4 @@ parameters:
- name: parameter-secret
source:
# Resolve the parameter value from the secret store specified in your Porter configuration file.
secret: parameter-secret
secret: parameter-secret