Skip to content

Commit 4727cc5

Browse files
committed
Validate yaml with yqlib and not with go-yaml v3
Just in case that yqlib starts using another library... Signed-off-by: Anders F Björklund <[email protected]>
1 parent 52f5ad3 commit 4727cc5

File tree

5 files changed

+46
-15
lines changed

5 files changed

+46
-15
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ require (
4545
google.golang.org/grpc v1.66.0
4646
google.golang.org/protobuf v1.34.2
4747
gopkg.in/op/go-logging.v1 v1.0.0-20160211212156-b2cb9fa56473
48-
gopkg.in/yaml.v3 v3.0.1
4948
gotest.tools/v3 v3.5.1
5049
inet.af/tcpproxy v0.0.0-20221017015627-91f861402626
5150
k8s.io/api v0.31.0
@@ -121,6 +120,7 @@ require (
121120
gopkg.in/inf.v0 v0.9.1 // indirect
122121
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
123122
gopkg.in/yaml.v2 v2.4.0 // indirect
123+
gopkg.in/yaml.v3 v3.0.1 // indirect
124124
gvisor.dev/gvisor v0.0.0-20231023213702-2691a8f9b1cf // indirect
125125
k8s.io/klog/v2 v2.130.1 // indirect
126126
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect

pkg/limayaml/load.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"github.com/goccy/go-yaml"
1010
"github.com/lima-vm/lima/pkg/store/dirnames"
1111
"github.com/lima-vm/lima/pkg/store/filenames"
12+
"github.com/lima-vm/lima/pkg/yqutil"
1213
"github.com/sirupsen/logrus"
13-
yamlv3 "gopkg.in/yaml.v3"
1414
)
1515

1616
func unmarshalDisk(dst *Disk, b []byte) error {
@@ -22,24 +22,13 @@ func unmarshalDisk(dst *Disk, b []byte) error {
2222
return yaml.Unmarshal(b, dst)
2323
}
2424

25-
func (d *Disk) UnmarshalYAML(value *yamlv3.Node) error {
26-
var v interface{}
27-
if err := value.Decode(&v); err != nil {
28-
return err
29-
}
30-
if s, ok := v.(string); ok {
31-
*d = Disk{Name: s}
32-
}
33-
return nil
34-
}
35-
3625
func unmarshalYAML(data []byte, v interface{}, comment string) error {
3726
if err := yaml.UnmarshalWithOptions(data, v, yaml.DisallowDuplicateKey(), yaml.CustomUnmarshaler[Disk](unmarshalDisk)); err != nil {
3827
return fmt.Errorf("failed to unmarshal YAML (%s): %w", comment, err)
3928
}
4029
// the go-yaml library doesn't catch all markup errors, unfortunately
4130
// make sure to get a "second opinion", using the same library as "yq"
42-
if err := yamlv3.Unmarshal(data, v); err != nil {
31+
if err := yqutil.ValidateContent(data); err != nil {
4332
return fmt.Errorf("failed to unmarshal YAML (%s): %w", comment, err)
4433
}
4534
if err := yaml.UnmarshalWithOptions(data, v, yaml.Strict(), yaml.CustomUnmarshaler[Disk](unmarshalDisk)); err != nil {

pkg/limayaml/load_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ provision:
2828
echo three
2929
`
3030
_, err := Load([]byte(s), "error.yaml")
31-
assert.ErrorContains(t, err, "failed to unmarshal YAML")
31+
assert.ErrorContains(t, err, "did not find expected key")
3232
}
3333

3434
func TestLoadDiskString(t *testing.T) {

pkg/yqutil/yqutil.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package yqutil
22

33
import (
44
"bytes"
5+
"errors"
56
"fmt"
7+
"io"
68
"os"
79
"strings"
810

@@ -11,6 +13,27 @@ import (
1113
logging "gopkg.in/op/go-logging.v1"
1214
)
1315

16+
// ValidateContent decodes the content yaml, to check it for syntax errors.
17+
func ValidateContent(content []byte) error {
18+
memory := logging.NewMemoryBackend(0)
19+
backend := logging.AddModuleLevel(memory)
20+
logging.SetBackend(backend)
21+
yqlib.InitExpressionParser()
22+
23+
decoder := yqlib.NewYamlDecoder(yqlib.ConfiguredYamlPreferences)
24+
25+
reader := bytes.NewReader(content)
26+
err := decoder.Init(reader)
27+
if err != nil {
28+
return err
29+
}
30+
_, err = decoder.Decode()
31+
if errors.Is(err, io.EOF) {
32+
return nil
33+
}
34+
return err
35+
}
36+
1437
// EvaluateExpression evaluates the yq expression, and returns the modified yaml.
1538
func EvaluateExpression(expression string, content []byte) ([]byte, error) {
1639
logrus.Debugf("Evaluating yq expression: %q", expression)

pkg/yqutil/yqutil_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@ import (
66
"gotest.tools/v3/assert"
77
)
88

9+
func TestValidateContent(t *testing.T) {
10+
content := `
11+
# comment
12+
foo: bar
13+
`
14+
err := ValidateContent([]byte(content))
15+
assert.NilError(t, err)
16+
}
17+
18+
func TestValidateContentError(t *testing.T) {
19+
content := `
20+
- foo: bar
21+
foo
22+
bar
23+
`
24+
err := ValidateContent([]byte(content))
25+
assert.ErrorContains(t, err, "could not find expected")
26+
}
27+
928
func TestEvaluateExpressionSimple(t *testing.T) {
1029
expression := `.cpus = 2 | .memory = "2GiB"`
1130
content := `

0 commit comments

Comments
 (0)