Skip to content

Commit

Permalink
base/v0_6_exp: Validate merged/replaced Ingition Configs
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam0Brien committed Aug 10, 2023
1 parent abcbbb5 commit 3d444ad
Show file tree
Hide file tree
Showing 18 changed files with 1,092 additions and 52 deletions.
18 changes: 17 additions & 1 deletion base/v0_6_exp/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package v0_6_exp
import (
baseutil "github.com/coreos/butane/base/util"
"github.com/coreos/butane/config/common"
exp "github.com/coreos/ignition/v2/config/v3_5_experimental"

"github.com/coreos/ignition/v2/config/util"
"github.com/coreos/vcontext/path"
Expand All @@ -29,10 +30,24 @@ func (rs Resource) Validate(c path.ContextPath) (r report.Report) {
if rs.Local != nil {
sources++
field = "local"
config := *rs.Local
_, report, err := exp.Parse([]byte(config))
if err != nil {
r.Merge(report)
} else {
r.AddOnError(c.Append("ignition", "config", "merge", "local"), common.ErrEmptyReport)
}
}
if rs.Inline != nil {
sources++
field = "inline"
config := *rs.Inline
_, report, err := exp.Parse([]byte(config))
if err != nil {
r.Merge(report)
} else {
r.AddOnError(c.Append("ignition", "config", "merge", "local"), common.ErrEmptyReport)
}
}
if rs.Source != nil {
sources++
Expand All @@ -41,7 +56,8 @@ func (rs Resource) Validate(c path.ContextPath) (r report.Report) {
if sources > 1 {
r.AddOnError(c.Append(field), common.ErrTooManyResourceSources)
}
return

return r
}

func (fs Filesystem) Validate(c path.ContextPath) (r report.Report) {
Expand Down
2 changes: 1 addition & 1 deletion config/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ 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/")

// resources and trees
ErrEmptyReport = errors.New("error merging report, report empty")
ErrTooManyResourceSources = errors.New("only one of the following can be set: inline, local, source")
ErrFilesDirEscape = errors.New("local file path traverses outside the files directory")
ErrFileType = errors.New("trees may only contain files, directories, and symlinks")
Expand Down
13 changes: 0 additions & 13 deletions config/fcos/v1_6_exp/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ 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 @@ -46,18 +45,6 @@ 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: 0 additions & 35 deletions config/fcos/v1_6_exp/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,38 +479,3 @@ 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")
})
}
}
3 changes: 1 addition & 2 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ 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)_
- Validate merged/replaced ignition configs if they are local/inline _(fcos 1.6.0-exp)_

### Bug fixes

Expand Down
187 changes: 187 additions & 0 deletions vendor/github.com/coreos/ignition/v2/config/translate/translate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions vendor/github.com/coreos/ignition/v2/config/v3_0/config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3d444ad

Please sign in to comment.