Skip to content

Commit 9ad42df

Browse files
committed
Throw validation error when a template digest is specified
because digest validation is not yet implemented. Also adds missing unmarshalling for `base: {file: URL}` and related tests. Signed-off-by: Jan Dubois <[email protected]>
1 parent 4174183 commit 9ad42df

File tree

7 files changed

+55
-6
lines changed

7 files changed

+55
-6
lines changed

pkg/limatmpl/abs.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ func (tmpl *Template) useAbsLocators() error {
3333
if i == 0 {
3434
// base can either be a single string, or a list of strings
3535
tmpl.expr.WriteString(fmt.Sprintf("| ($a.base | select(type == \"!!str\")) |= %q\n", locator))
36-
tmpl.expr.WriteString(fmt.Sprintf("| ($a.base | select(type == \"!!seq\") | .[0]) |= %q\n", locator))
36+
tmpl.expr.WriteString(fmt.Sprintf("| ($a.base | select(type == \"!!seq\" and (.[0] | type) == \"!!str\") | .[0]) |= %q\n", locator))
37+
tmpl.expr.WriteString(fmt.Sprintf("| ($a.base | select(type == \"!!seq\" and (.[0] | type) == \"!!seq\") | .[0].url) |= %q\n", locator))
3738
} else {
3839
tmpl.expr.WriteString(fmt.Sprintf("| $a.base[%d] = %q\n", i, locator))
3940
}
@@ -90,6 +91,9 @@ func basePath(locator string) (string, error) {
9091

9192
// absPath either returns the locator directly, or combines it with the basePath if the locator is a relative path.
9293
func absPath(locator, basePath string) (string, error) {
94+
if locator == "" {
95+
return "", errors.New("locator is empty")
96+
}
9397
u, err := url.Parse(locator)
9498
if err == nil && len(u.Scheme) > 1 {
9599
return locator, nil

pkg/limatmpl/abs_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,14 @@ func TestAbsPath(t *testing.T) {
203203
assert.ErrorContains(t, err, "'../'")
204204
})
205205

206+
t.Run("locator must not be empty", func(t *testing.T) {
207+
_, err = absPath("", "foo")
208+
assert.ErrorContains(t, err, "locator is empty")
209+
})
210+
206211
t.Run("basePath must not be empty", func(t *testing.T) {
207212
_, err = absPath("foo", "")
208-
assert.ErrorContains(t, err, "empty")
213+
assert.ErrorContains(t, err, "basePath is empty")
209214
})
210215

211216
t.Run("", func(t *testing.T) {

pkg/limatmpl/embed.go

+4
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ func (tmpl *Template) embedAllBases(ctx context.Context, embedAll, defaultBase b
7272
break
7373
}
7474
baseLocator := tmpl.Config.Base[0]
75+
logrus.Infof("Base[0] is %+v", baseLocator)
76+
if baseLocator.Digest != nil {
77+
return fmt.Errorf("base %q in %q has specified a digest; digest support is not yet implemented", baseLocator.URL, tmpl.Locator)
78+
}
7579
isTemplate, _ := SeemsTemplateURL(baseLocator.URL)
7680
if isTemplate && !embedAll {
7781
// Once we skip a template:// URL we can no longer embed any other base template

pkg/limatmpl/embed_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,20 @@ provision:
359359
"#! my script",
360360
`provision: [{script: "#! my script"}]`,
361361
},
362+
{
363+
// This test also verifies that the alternate format for `base` are transformed correctly by useAbsLocators
364+
"ERROR base digest is not yet implemented (1)",
365+
"",
366+
"base: {url: base.yaml, digest: deafbad}",
367+
"not yet implemented",
368+
},
369+
{
370+
// This test also verifies that the alternate format for `base` are transformed correctly by useAbsLocators
371+
"ERROR base digest is not yet implemented (2)",
372+
"",
373+
"base: [{url: base.yaml, digest: deafbad}]",
374+
"not yet implemented",
375+
},
362376
}
363377

364378
func TestEmbed(t *testing.T) {

pkg/limayaml/marshal.go

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ func unmarshalBaseTemplates(dst *BaseTemplates, b []byte) error {
4242
*dst = BaseTemplates{LocatorWithDigest{URL: s}}
4343
return nil
4444
}
45+
var locator LocatorWithDigest
46+
if err := yaml.Unmarshal(b, &locator); err == nil {
47+
*dst = BaseTemplates{locator}
48+
return nil
49+
}
4550
return yaml.UnmarshalWithOptions(b, dst, yaml.CustomUnmarshaler[LocatorWithDigest](unmarshalLocatorWithDigest))
4651
}
4752

pkg/limayaml/validate.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,13 @@ func Validate(y *LimaYAML, warn bool) error {
206206
// y.Firmware.LegacyBIOS is ignored for aarch64, but not a fatal error.
207207

208208
for i, p := range y.Provision {
209-
if p.File != nil && p.File.URL != "" {
210-
return fmt.Errorf("field `provision[%d].file.url` must be empty during validation (script should already be embedded)", i)
209+
if p.File != nil {
210+
if p.File.URL != "" {
211+
return fmt.Errorf("field `provision[%d].file.url` must be empty during validation (script should already be embedded)", i)
212+
}
213+
if p.File.Digest != nil {
214+
return fmt.Errorf("field `provision[%d].file.digest` support is not yet implemented", i)
215+
}
211216
}
212217
switch p.Mode {
213218
case ProvisionModeSystem, ProvisionModeUser, ProvisionModeBoot:
@@ -249,8 +254,13 @@ func Validate(y *LimaYAML, warn bool) error {
249254
}
250255
}
251256
for i, p := range y.Probes {
252-
if p.File != nil && p.File.URL != "" {
253-
return fmt.Errorf("field `probes[%d].file.url` must be empty during validation (script should already be embedded)", i)
257+
if p.File != nil {
258+
if p.File.URL != "" {
259+
return fmt.Errorf("field `probe[%d].file.url` must be empty during validation (script should already be embedded)", i)
260+
}
261+
if p.File.Digest != nil {
262+
return fmt.Errorf("field `probe[%d].file.digest` support is not yet implemented", i)
263+
}
254264
}
255265
if !strings.HasPrefix(p.Script, "#!") {
256266
return fmt.Errorf("field `probe[%d].script` must start with a '#!' line", i)

pkg/limayaml/validate_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ func TestValidateProbes(t *testing.T) {
4646

4747
err = Validate(y, false)
4848
assert.Error(t, err, "field `probe[0].script` must start with a '#!' line")
49+
50+
invalidProbe = `probes: [{file: {digest: decafbad}}]`
51+
y, err = Load([]byte(invalidProbe+"\n"+images), "lima.yaml")
52+
assert.NilError(t, err)
53+
54+
err = Validate(y, false)
55+
assert.Error(t, err, "field `probe[0].file.digest` support is not yet implemented")
4956
}
5057

5158
func TestValidateAdditionalDisks(t *testing.T) {

0 commit comments

Comments
 (0)