Skip to content

Commit

Permalink
fcos/v1_6_exp: Fail if user provides invalid file type
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam0Brien committed Jul 3, 2023
1 parent dba19fb commit 9e622f7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var (
// high-level errors for fatal reports
ErrInvalidSourceConfig = errors.New("source config is invalid")
ErrInvalidGeneratedConfig = errors.New("config generated was invalid")
ErrInvalidFileExtension = errors.New("file type provided was invalid; use .ign for local/inline instead")

// deprecated variant/version
ErrRhcosVariantUnsupported = errors.New("rhcos variant has been removed; use openshift variant instead: https://coreos.github.io/butane/upgrading-openshift/")
Expand Down
13 changes: 13 additions & 0 deletions config/fcos/v1_6_exp/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
const rootDevice = "/dev/disk/by-id/coreos-boot-disk"

var allowedMountpoints = regexp.MustCompile(`^/(etc|var)(/|$)`)
var fileExt = regexp.MustCompile("^(.ign)$")

// We can't define a Validate function directly on Disk because that's defined in base,
// so we use a Validate function on the top-level Config instead.
Expand All @@ -45,6 +46,18 @@ func (conf Config) Validate(c path.ContextPath) (r report.Report) {
r.AddOnError(c.Append("storage", "filesystems", i, "path"), common.ErrMountPointForbidden)
}
}

if conf.Ignition.Config.Merge != nil {
for i, conf := range conf.Ignition.Config.Merge {
if conf.Inline != nil && !fileExt.MatchString(string(*conf.Inline)) {
r.AddOnError(c.Append("ignition", "config", "merge", i, "inline"), common.ErrInvalidFileExtension)
}

if conf.Local != nil && !fileExt.MatchString(string(*conf.Local)) {
r.AddOnError(c.Append("ignition", "config", "merge", i, "local"), common.ErrInvalidFileExtension)
}
}
}
return
}

Expand Down
35 changes: 35 additions & 0 deletions config/fcos/v1_6_exp/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,3 +479,38 @@ func TestValidateConfig(t *testing.T) {
})
}
}

func TestValidateFileExtension(t *testing.T) {
tests := []struct {
in Config
out error
errPath path.ContextPath
}{
{
in: Config{
Config: base.Config{
Ignition: base.Ignition{
Config: base.IgnitionConfig{
Merge: []base.Resource{
{
Inline: util.StrToPtr("config.bu"),
},
},
},
},
},
},
out: common.ErrInvalidFileExtension,
errPath: path.New("yaml", "ignition", "config", "merge", 0, "inline"),
},
}
for i, test := range tests {
t.Run(fmt.Sprintf("validate %d", i), func(t *testing.T) {
actual := test.in.Validate(path.New("yaml"))
baseutil.VerifyReport(t, test.in, actual)
expected := report.Report{}
expected.AddOnError(test.errPath, test.out)
assert.Equal(t, expected, actual, "invalid report")
})
}
}
2 changes: 2 additions & 0 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ key](https://getfedora.org/security/).
openshift 4.14.0-exp)_
- Require `storage.filesystems.path` to start with `/etc` or `/var` if
`with_mount_unit` is true _(fcos 1.6.0-exp, openshift 4.14.0-exp)_
- Require local/inline fields from Ignition.Config.Merge to have the .ign file
extension _(fcos 1.6.0-exp)_

### Bug fixes

Expand Down

0 comments on commit 9e622f7

Please sign in to comment.