Skip to content

Commit

Permalink
base/v0_6_exp/validate.go: Added switch statement for Validate Resource
Browse files Browse the repository at this point in the history
base/v0_6_exp/validate.go: Add mapper function and fix tests
  • Loading branch information
Adam0Brien committed Aug 22, 2023
1 parent 0a1b18e commit a69e98f
Show file tree
Hide file tree
Showing 17 changed files with 1,137 additions and 14 deletions.
59 changes: 52 additions & 7 deletions base/v0_6_exp/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,77 @@ import (
baseutil "github.com/coreos/butane/base/util"
"github.com/coreos/butane/config/common"

"github.com/coreos/ignition/v2/config/shared/errors"
"github.com/coreos/ignition/v2/config/util"
exp "github.com/coreos/ignition/v2/config/v3_5_experimental"
"github.com/coreos/vcontext/path"
"github.com/coreos/vcontext/report"
)

func (rs Resource) Validate(c path.ContextPath) (r report.Report) {
var field string
sources := 0
if rs.Local != nil {
var config string
var butaneReport report.Report
switch {
case rs.Local != nil:
sources++
field = "local"
}
if rs.Inline != nil {
if field == "" {
field = "local"
config = *rs.Local
}
fallthrough
case rs.Inline != nil:
sources++
field = "inline"
}
if rs.Source != nil {
if field == "" {
field = "inline"
config = *rs.Inline
}
fallthrough
case rs.Source != nil:
sources++
field = "source"
}
if sources > 1 {
r.AddOnError(c.Append(field), common.ErrTooManyResourceSources)
}
if field == "local" || field == "inline" {
// check if it's an ignition config, will fix error on fcos
if len(config) > 0 && config[0] != '{' {
_, report, err := exp.Parse([]byte(config))
if len(report.Entries) > 0 {
butaneReport = MapIgnitionReportToButane(report)
// convert report into butane specific report then merge it
r.Merge(butaneReport)
}
if err != nil {
if err == errors.ErrUnknownVersion {
r.AddOnError(c.Append(field), errors.ErrUnknownVersion)
}
}
} else {
r.AddOnError(c.Append(field), common.ErrNoFilesDir)
}

}

return
}

func MapIgnitionReportToButane(ignitionReport report.Report) report.Report {
var butaneRep report.Report
for _, entry := range ignitionReport.Entries {
butaneEntry := report.Entry{
Kind: entry.Kind,
Message: entry.Message,
Context: entry.Context,
Marker: entry.Marker,
}
butaneRep.Entries = append(butaneRep.Entries, butaneEntry)
}
return butaneRep
}

func (fs Filesystem) Validate(c path.ContextPath) (r report.Report) {
if !util.IsTrue(fs.WithMountUnit) {
return
Expand Down
17 changes: 10 additions & 7 deletions base/v0_6_exp/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestValidateResource(t *testing.T) {
// inline specified
{
Resource{
Inline: util.StrToPtr("hello"),
// Inline: util.StrToPtr("hello"),
Compression: util.StrToPtr("gzip"),
Verification: Verification{
Hash: util.StrToPtr("this isn't validated"),
Expand All @@ -64,7 +64,7 @@ func TestValidateResource(t *testing.T) {
// local specified
{
Resource{
Local: util.StrToPtr("hello"),
// Local: util.StrToPtr("hello"),
Compression: util.StrToPtr("gzip"),
Verification: Verification{
Hash: util.StrToPtr("this isn't validated"),
Expand All @@ -90,7 +90,7 @@ func TestValidateResource(t *testing.T) {
{
Resource{
Source: util.StrToPtr("data:,hello"),
Local: util.StrToPtr("hello"),
Local: util.StrToPtr("{\"ignition\": {\"version\": \"3.5.0\"}}"),
Compression: util.StrToPtr("gzip"),
Verification: Verification{
Hash: util.StrToPtr("this isn't validated"),
Expand All @@ -103,21 +103,21 @@ func TestValidateResource(t *testing.T) {
{
Resource{
Inline: util.StrToPtr("hello"),
Local: util.StrToPtr("hello"),
Local: util.StrToPtr("{\"ignition\": {\"version\": \"3.5.0\"}}"),
Compression: util.StrToPtr("gzip"),
Verification: Verification{
Hash: util.StrToPtr("this isn't validated"),
},
},
common.ErrTooManyResourceSources,
path.New("yaml", "inline"),
path.New("yaml", "source"),
},
// source + inline + local, invalid
{
Resource{
Source: util.StrToPtr("data:,hello"),
Inline: util.StrToPtr("hello"),
Local: util.StrToPtr("hello"),
Inline: util.StrToPtr("{\"ignition\": {\"version\": \"3.5.0\"}}"),
Local: util.StrToPtr("{\"ignition\": {\"version\": \"3.5.0\"}}"),
Compression: util.StrToPtr("gzip"),
Verification: Verification{
Hash: util.StrToPtr("this isn't validated"),
Expand All @@ -139,6 +139,9 @@ func TestValidateResource(t *testing.T) {
}
}

func TestMapperFunction(t *testing.T) {

}
func TestValidateTree(t *testing.T) {
tests := []struct {
in Tree
Expand Down
1 change: 1 addition & 0 deletions config/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (
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
1 change: 1 addition & 0 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +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)_
- 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.

Loading

0 comments on commit a69e98f

Please sign in to comment.