validate yaml for extraneous fields in pkg build

Signed-off-by: Avi Deitcher <avi@deitcher.net>
This commit is contained in:
Avi Deitcher
2025-09-05 11:30:07 +03:00
committed by Avi Deitcher
parent 9da6903609
commit 358803fcc7
5 changed files with 34 additions and 3 deletions

View File

@@ -1,3 +1,4 @@
image: memlogd image: memlogd
binds: config:
binds:
- /var/run:/var/run - /var/run:/var/run

View File

@@ -10,7 +10,7 @@ import (
"strings" "strings"
"text/template" "text/template"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v3"
"github.com/linuxkit/linuxkit/src/cmd/linuxkit/moby" "github.com/linuxkit/linuxkit/src/cmd/linuxkit/moby"
"github.com/linuxkit/linuxkit/src/cmd/linuxkit/util" "github.com/linuxkit/linuxkit/src/cmd/linuxkit/util"
@@ -157,7 +157,9 @@ func NewFromConfig(cfg PkglibConfig, args ...string) ([]Pkg, error) {
return nil, err return nil, err
} }
if err := yaml.Unmarshal(b, &pi); err != nil { dec := yaml.NewDecoder(bytes.NewReader(b))
dec.KnownFields(true)
if err := dec.Decode(&pi); err != nil {
return nil, err return nil, err
} }

View File

@@ -0,0 +1,2 @@
FROM alpine:3.22

View File

@@ -0,0 +1,7 @@
image: build-args-test
network: true
arches:
- amd64
- arm64
# should error out on this unknown field
unknownField: "abc"

View File

@@ -0,0 +1,19 @@
#!/bin/sh
# SUMMARY: Check that the build-args are correctly passed to Dockerfiles
# LABELS:
# REPEAT:
set -ex
# Source libraries. Uncomment if needed/defined
#. "${RT_LIB}"
. "${RT_PROJECT_ROOT}/_lib/lib.sh"
# Test code goes here
RESULT=$(linuxkit pkg build --force . 2>&1 || echo FAILED)
if [ "${RESULT}" != "FAILED" ]; then
echo "Build should have failed with invalid yaml, instead was ${RESULT}"
fi
echo "Summary: correctly detected invalid yaml"
exit 0