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

Switch to BurntSushi/toml and error out on unknown toml keys #549

Merged
merged 2 commits into from
Jul 29, 2024
Merged
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
3 changes: 1 addition & 2 deletions bib/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module github.com/osbuild/bootc-image-builder/bib
go 1.21

require (
github.com/BurntSushi/toml v1.4.0
github.com/aws/aws-sdk-go v1.55.3
github.com/cheggaaa/pb/v3 v3.1.5
github.com/google/uuid v1.6.0
github.com/osbuild/images v0.72.0
github.com/pelletier/go-toml v1.9.5
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
Expand All @@ -19,7 +19,6 @@ require (

require (
dario.cat/mergo v1.0.0 // indirect
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/Microsoft/hcsshim v0.12.3 // indirect
github.com/VividCortex/ewma v1.2.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions bib/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,6 @@ github.com/osbuild/images v0.72.0 h1:WKRXokWCpkS6zusc2yadXbW5jQD4cNvBMHZUV/8L4jM
github.com/osbuild/images v0.72.0/go.mod h1:McWtOOsi/2gH2J4tQ8Vnvg+O3jVf44XIj3jUWJZt47E=
github.com/ostreedev/ostree-go v0.0.0-20210805093236-719684c64e4f h1:/UDgs8FGMqwnHagNDPGOlts35QkhAZ8by3DR7nMih7M=
github.com/ostreedev/ostree-go v0.0.0-20210805093236-719684c64e4f/go.mod h1:J6OG6YJVEWopen4avK3VNQSnALmmjvniMmni/YFYAwc=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
10 changes: 8 additions & 2 deletions bib/internal/buildconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"os"
"path/filepath"

"github.com/pelletier/go-toml"
"github.com/BurntSushi/toml"
"github.com/sirupsen/logrus"

"github.com/osbuild/images/pkg/blueprint"
Expand Down Expand Up @@ -59,9 +59,15 @@ func decodeTomlBuildConfig(r io.Reader, what string) (*BuildConfig, error) {
dec := toml.NewDecoder(r)

var conf BuildConfig
if err := dec.Decode(&conf); err != nil {
metadata, err := dec.Decode(&conf)
if err != nil {
return nil, fmt.Errorf("cannot decode %q: %w", what, err)
}

if len(metadata.Undecoded()) > 0 {
return nil, fmt.Errorf("cannot decode %q: unknown keys found: %v", what, metadata.Undecoded())
}

return &conf, nil
}

Expand Down
25 changes: 25 additions & 0 deletions bib/internal/buildconfig/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,28 @@ func TestReadLegacyJSONConfig(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, expectedBuildConfig, cfg)
}

func TestTomlUnknownKeysError(t *testing.T) {
fakeUserCnfPath := makeFakeConfig(t, "config.toml", `
[[birds]]
name = "toucan"
`)
_, err := buildconfig.ReadWithFallback(fakeUserCnfPath)

assert.ErrorContains(t, err, "unknown keys found: [birds birds.name]")
}

func TestJsonUnknownKeysError(t *testing.T) {
fakeUserCnfPath := makeFakeConfig(t, "config.json", `
{
"birds": [
{
"name": "toucan"
}
]
}
`)
_, err := buildconfig.ReadWithFallback(fakeUserCnfPath)

assert.ErrorContains(t, err, `json: unknown field "birds"`)
}
Loading