Skip to content
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
9 changes: 9 additions & 0 deletions base/v0_2/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ func translateResource(from Resource, options common.TranslateOptions) (to types
r.AddOnError(c, err)
return
}
// Validating the contents of the local file from here since there is no way to
// get both the filename and filedirectory in the Validate context
if strings.HasPrefix(c.String(), "$.ignition.config") {
rp, err := ValidateIgnitionConfig(c, contents)
r.Merge(rp)
if err != nil {
return
}
}
src, compression, err := baseutil.MakeDataURL(contents, to.Compression, !options.NoResourceAutoCompression)
if err != nil {
r.AddOnError(c, err)
Expand Down
33 changes: 33 additions & 0 deletions base/v0_2/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@
package v0_2

import (
common "github.com/coreos/butane/config/common"
"github.com/coreos/ignition/v2/config/shared/errors"
"github.com/coreos/ignition/v2/config/util"
"github.com/coreos/ignition/v2/config/v3_1/types"
"github.com/coreos/ignition/v2/config/validate"
"github.com/coreos/vcontext/path"
"github.com/coreos/vcontext/report"
vvalidate "github.com/coreos/vcontext/validate"
)

type nodeTracker struct {
Expand Down Expand Up @@ -123,3 +130,29 @@ func (t *nodeTracker) AddLink(l types.Link) (int, *types.Link) {
t.linkMap[l.Path] = i
return i, &(*t.links)[i]
}

func ValidateIgnitionConfig(c path.ContextPath, rawConfig []byte) (report.Report, error) {
r := report.Report{}
var config types.Config
rp, err := util.HandleParseErrors(rawConfig, &config)
if err != nil {
return rp, err
}
vrep := vvalidate.Validate(config.Ignition, "json")
skipValidate := false
if vrep.IsFatal() {
for _, e := range vrep.Entries {
// warn user with ErrUnknownVersion when version is unkown and skip the validation.
if e.Message == errors.ErrUnknownVersion.Error() {
skipValidate = true
r.AddOnWarn(c.Append("version"), common.ErrUnkownIgnitionVersion)
break
}
}
}
if !skipValidate {
report := validate.ValidateWithContext(config, rawConfig)
r.Merge(report)
}
return r, nil
}
13 changes: 13 additions & 0 deletions base/v0_2/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package v0_2
import (
baseutil "github.com/coreos/butane/base/util"
"github.com/coreos/butane/config/common"
"strings"

"github.com/coreos/ignition/v2/config/util"
"github.com/coreos/vcontext/path"
Expand All @@ -26,6 +27,7 @@ import (
func (rs Resource) Validate(c path.ContextPath) (r report.Report) {
var field string
sources := 0
// Local files are validated in the translateResource function
if rs.Local != nil {
sources++
field = "local"
Expand All @@ -40,6 +42,17 @@ func (rs Resource) Validate(c path.ContextPath) (r report.Report) {
}
if sources > 1 {
r.AddOnError(c.Append(field), common.ErrTooManyResourceSources)
return
}
if strings.HasPrefix(c.String(), "$.ignition.config") {
if field == "inline" {
rp, err := ValidateIgnitionConfig(c, []byte(*rs.Inline))
r.Merge(rp)
if err != nil {
r.AddOnError(c.Append(field), err)
return
}
}
}
return
}
Expand Down
23 changes: 23 additions & 0 deletions base/v0_2/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,26 @@ func TestValidateDirMode(t *testing.T) {
})
}
}

// TestUnkownIgnitionVersion tests that butane will raise a warning but will not fail when an ignition config with an unkown version is specified
func TestUnkownIgnitionVersion(t *testing.T) {
test := struct {
in Resource
out error
errPath path.ContextPath
}{
Resource{
Inline: util.StrToPtr(`{"ignition": {"version": "10.0.0"}}`),
},
common.ErrUnkownIgnitionVersion,
path.New("yaml", "ignition", "config", "version"),
}
path := path.New("yaml", "ignition", "config")
// Skipping baseutil.VerifyReport because it expects all referenced paths to exist in the struct.
// In this test, "ignition.config" doesn't exist, so VerifyReport would fail. However, we still need
// to pass this path to Validate() to trigger the unknown Ignition version warning we're testing for.
actual := test.in.Validate(path)
expected := report.Report{}
expected.AddOnWarn(test.errPath, test.out)
assert.Equal(t, expected, actual, "bad report")
}
9 changes: 9 additions & 0 deletions base/v0_3/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ func translateResource(from Resource, options common.TranslateOptions) (to types
r.AddOnError(c, err)
return
}
// Validating the contents of the local file from here since there is no way to
// get both the filename and filedirectory in the Validate context
if strings.HasPrefix(c.String(), "$.ignition.config") {
rp, err := ValidateIgnitionConfig(c, contents)
r.Merge(rp)
if err != nil {
return
}
}
src, compression, err := baseutil.MakeDataURL(contents, to.Compression, !options.NoResourceAutoCompression)
if err != nil {
r.AddOnError(c, err)
Expand Down
33 changes: 33 additions & 0 deletions base/v0_3/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@
package v0_3

import (
common "github.com/coreos/butane/config/common"
"github.com/coreos/ignition/v2/config/shared/errors"
"github.com/coreos/ignition/v2/config/util"
"github.com/coreos/ignition/v2/config/v3_2/types"
"github.com/coreos/ignition/v2/config/validate"
"github.com/coreos/vcontext/path"
"github.com/coreos/vcontext/report"
vvalidate "github.com/coreos/vcontext/validate"
)

type nodeTracker struct {
Expand Down Expand Up @@ -123,3 +130,29 @@ func (t *nodeTracker) AddLink(l types.Link) (int, *types.Link) {
t.linkMap[l.Path] = i
return i, &(*t.links)[i]
}

func ValidateIgnitionConfig(c path.ContextPath, rawConfig []byte) (report.Report, error) {
r := report.Report{}
var config types.Config
rp, err := util.HandleParseErrors(rawConfig, &config)
if err != nil {
return rp, err
}
vrep := vvalidate.Validate(config.Ignition, "json")
skipValidate := false
if vrep.IsFatal() {
for _, e := range vrep.Entries {
// warn user with ErrUnknownVersion when version is unkown and skip the validation.
if e.Message == errors.ErrUnknownVersion.Error() {
skipValidate = true
r.AddOnWarn(c.Append("version"), common.ErrUnkownIgnitionVersion)
break
}
}
}
if !skipValidate {
report := validate.ValidateWithContext(config, rawConfig)
r.Merge(report)
}
return r, nil
}
13 changes: 13 additions & 0 deletions base/v0_3/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package v0_3
import (
baseutil "github.com/coreos/butane/base/util"
"github.com/coreos/butane/config/common"
"strings"

"github.com/coreos/ignition/v2/config/util"
"github.com/coreos/vcontext/path"
Expand All @@ -26,6 +27,7 @@ import (
func (rs Resource) Validate(c path.ContextPath) (r report.Report) {
var field string
sources := 0
// Local files are validated in the translateResource function
if rs.Local != nil {
sources++
field = "local"
Expand All @@ -40,6 +42,17 @@ func (rs Resource) Validate(c path.ContextPath) (r report.Report) {
}
if sources > 1 {
r.AddOnError(c.Append(field), common.ErrTooManyResourceSources)
return
}
if strings.HasPrefix(c.String(), "$.ignition.config") {
if field == "inline" {
rp, err := ValidateIgnitionConfig(c, []byte(*rs.Inline))
r.Merge(rp)
if err != nil {
r.AddOnError(c.Append(field), err)
return
}
}
}
return
}
Expand Down
23 changes: 23 additions & 0 deletions base/v0_3/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,26 @@ func TestValidateDirMode(t *testing.T) {
})
}
}

// TestUnkownIgnitionVersion tests that butane will raise a warning but will not fail when an ignition config with an unkown version is specified
func TestUnkownIgnitionVersion(t *testing.T) {
test := struct {
in Resource
out error
errPath path.ContextPath
}{
Resource{
Inline: util.StrToPtr(`{"ignition": {"version": "10.0.0"}}`),
},
common.ErrUnkownIgnitionVersion,
path.New("yaml", "ignition", "config", "version"),
}
path := path.New("yaml", "ignition", "config")
// Skipping baseutil.VerifyReport because it expects all referenced paths to exist in the struct.
// In this test, "ignition.config" doesn't exist, so VerifyReport would fail. However, we still need
// to pass this path to Validate() to trigger the unknown Ignition version warning we're testing for.
actual := test.in.Validate(path)
expected := report.Report{}
expected.AddOnWarn(test.errPath, test.out)
assert.Equal(t, expected, actual, "bad report")
}
9 changes: 9 additions & 0 deletions base/v0_4/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ func translateResource(from Resource, options common.TranslateOptions) (to types
r.AddOnError(c, err)
return
}
// Validating the contents of the local file from here since there is no way to
// get both the filename and filedirectory in the Validate context
if strings.HasPrefix(c.String(), "$.ignition.config") {
rp, err := ValidateIgnitionConfig(c, contents)
r.Merge(rp)
if err != nil {
return
}
}
src, compression, err := baseutil.MakeDataURL(contents, to.Compression, !options.NoResourceAutoCompression)
if err != nil {
r.AddOnError(c, err)
Expand Down
33 changes: 33 additions & 0 deletions base/v0_4/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@
package v0_4

import (
common "github.com/coreos/butane/config/common"
"github.com/coreos/ignition/v2/config/shared/errors"
"github.com/coreos/ignition/v2/config/util"
"github.com/coreos/ignition/v2/config/v3_3/types"
"github.com/coreos/ignition/v2/config/validate"
"github.com/coreos/vcontext/path"
"github.com/coreos/vcontext/report"
vvalidate "github.com/coreos/vcontext/validate"
)

type nodeTracker struct {
Expand Down Expand Up @@ -123,3 +130,29 @@ func (t *nodeTracker) AddLink(l types.Link) (int, *types.Link) {
t.linkMap[l.Path] = i
return i, &(*t.links)[i]
}

func ValidateIgnitionConfig(c path.ContextPath, rawConfig []byte) (report.Report, error) {
r := report.Report{}
var config types.Config
rp, err := util.HandleParseErrors(rawConfig, &config)
if err != nil {
return rp, err
}
vrep := vvalidate.Validate(config.Ignition, "json")
skipValidate := false
if vrep.IsFatal() {
for _, e := range vrep.Entries {
// warn user with ErrUnknownVersion when version is unkown and skip the validation.
if e.Message == errors.ErrUnknownVersion.Error() {
skipValidate = true
r.AddOnWarn(c.Append("version"), common.ErrUnkownIgnitionVersion)
break
}
}
}
if !skipValidate {
report := validate.ValidateWithContext(config, rawConfig)
r.Merge(report)
}
return r, nil
}
13 changes: 13 additions & 0 deletions base/v0_4/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package v0_4
import (
baseutil "github.com/coreos/butane/base/util"
"github.com/coreos/butane/config/common"
"strings"

"github.com/coreos/ignition/v2/config/util"
"github.com/coreos/vcontext/path"
Expand All @@ -26,6 +27,7 @@ import (
func (rs Resource) Validate(c path.ContextPath) (r report.Report) {
var field string
sources := 0
// Local files are validated in the translateResource function
if rs.Local != nil {
sources++
field = "local"
Expand All @@ -40,6 +42,17 @@ func (rs Resource) Validate(c path.ContextPath) (r report.Report) {
}
if sources > 1 {
r.AddOnError(c.Append(field), common.ErrTooManyResourceSources)
return
}
if strings.HasPrefix(c.String(), "$.ignition.config") {
if field == "inline" {
rp, err := ValidateIgnitionConfig(c, []byte(*rs.Inline))
r.Merge(rp)
if err != nil {
r.AddOnError(c.Append(field), err)
return
}
}
}
return
}
Expand Down
23 changes: 23 additions & 0 deletions base/v0_4/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,26 @@ func TestValidateFilesystem(t *testing.T) {
})
}
}

// TestUnkownIgnitionVersion tests that butane will raise a warning but will not fail when an ignition config with an unkown version is specified
func TestUnkownIgnitionVersion(t *testing.T) {
test := struct {
in Resource
out error
errPath path.ContextPath
}{
Resource{
Inline: util.StrToPtr(`{"ignition": {"version": "10.0.0"}}`),
},
common.ErrUnkownIgnitionVersion,
path.New("yaml", "ignition", "config", "version"),
}
path := path.New("yaml", "ignition", "config")
// Skipping baseutil.VerifyReport because it expects all referenced paths to exist in the struct.
// In this test, "ignition.config" doesn't exist, so VerifyReport would fail. However, we still need
// to pass this path to Validate() to trigger the unknown Ignition version warning we're testing for.
actual := test.in.Validate(path)
expected := report.Report{}
expected.AddOnWarn(test.errPath, test.out)
assert.Equal(t, expected, actual, "bad report")
}
9 changes: 9 additions & 0 deletions base/v0_5/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ func translateResource(from Resource, options common.TranslateOptions) (to types
r.AddOnError(c, err)
return
}
// Validating the contents of the local file from here since there is no way to
// get both the filename and filedirectory in the Validate context
if strings.HasPrefix(c.String(), "$.ignition.config") {
rp, err := ValidateIgnitionConfig(c, contents)
r.Merge(rp)
if err != nil {
return
}
}
src, compression, err := baseutil.MakeDataURL(contents, to.Compression, !options.NoResourceAutoCompression)
if err != nil {
r.AddOnError(c, err)
Expand Down
Loading
Loading