From 546d1ff37c299a8cc81bb747ff1c90fcb1610d9d Mon Sep 17 00:00:00 2001 From: Adam0Brien Date: Mon, 3 Jul 2023 15:41:50 +0100 Subject: [PATCH] fcos/v1_6_exp: Fail if user provides invalid file type --- config/common/errors.go | 1 + config/fcos/v1_6_exp/validate.go | 13 ++++++++++ config/fcos/v1_6_exp/validate_test.go | 35 +++++++++++++++++++++++++++ docs/release-notes.md | 2 ++ 4 files changed, 51 insertions(+) diff --git a/config/common/errors.go b/config/common/errors.go index a5f8d5b2..9622a5b6 100644 --- a/config/common/errors.go +++ b/config/common/errors.go @@ -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/") diff --git a/config/fcos/v1_6_exp/validate.go b/config/fcos/v1_6_exp/validate.go index 4c3ae9de..58df0230 100644 --- a/config/fcos/v1_6_exp/validate.go +++ b/config/fcos/v1_6_exp/validate.go @@ -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. @@ -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 } diff --git a/config/fcos/v1_6_exp/validate_test.go b/config/fcos/v1_6_exp/validate_test.go index 2c850580..e313e396 100644 --- a/config/fcos/v1_6_exp/validate_test.go +++ b/config/fcos/v1_6_exp/validate_test.go @@ -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") + }) + } +} diff --git a/docs/release-notes.md b/docs/release-notes.md index f7596971..16755739 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -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