Skip to content

Commit

Permalink
parse YAML strictly, failing on unknown fields
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Hall <jason@chainguard.dev>
  • Loading branch information
imjasonh committed Sep 27, 2023
1 parent 96cd9f5 commit a46fd25
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/mega-module.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
- uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version-file: 'go.mod'

- uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0
with:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version-file: 'go.mod'
- run: go generate ./...
- name: git diff
run: |
Expand All @@ -34,7 +34,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version-file: 'go.mod'

- uses: hashicorp/setup-terraform@v2
with:
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/chainguard-dev/terraform-provider-apko

go 1.20
go 1.21

toolchain go1.21.0

require (
chainguard.dev/apko v0.10.1-0.20230906171757-7c910a4b75ca
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/config_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (d *ConfigDataSource) Read(ctx context.Context, req datasource.ReadRequest,
}

var ic apkotypes.ImageConfiguration
if err := yaml.Unmarshal([]byte(data.ConfigContents.ValueString()), &ic); err != nil {
if err := yaml.UnmarshalStrict([]byte(data.ConfigContents.ValueString()), &ic); err != nil {
resp.Diagnostics.AddError("Unable to parse apko configuration", err.Error())
return
}
Expand Down
20 changes: 20 additions & 0 deletions internal/provider/config_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,3 +488,23 @@ func TestUnify(t *testing.T) {
})
}
}

func TestAccDataSourceConfig_Invalid(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{{
Config: `
data "apko_config" "this" {
config_contents = <<EOF
contents:
repositories:
- ./packages
unknown-field: 'blah'
EOF
}`,
ExpectError: regexp.MustCompile("field unknown-field not found in type types.ImageConfiguration"),
}},
})
}

0 comments on commit a46fd25

Please sign in to comment.