Skip to content

Commit 481d9c4

Browse files
committed
exporter: add validation for invalid platorm
Signed-off-by: Tonis Tiigi <[email protected]> (cherry picked from commit d293ec3208f87fefab7a1caadffa3f3f50604796) (cherry picked from commit 42b95935d606b262a33374eeeb452bb7c299c729)
1 parent 83edaef commit 481d9c4

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

client/client_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ func TestIntegration(t *testing.T) {
207207
testExportLocalNoPlatformSplit,
208208
testExportLocalNoPlatformSplitOverwrite,
209209
testValidateNullConfig,
210+
testValidateInvalidConfig,
210211
)
211212
}
212213

@@ -8490,6 +8491,7 @@ cat <<EOF > $BUILDKIT_SCAN_DESTINATION/spdx.json
84908491
EOF
84918492
`
84928493
img.Config.Cmd = []string{"/bin/sh", "-c", cmd}
8494+
img.Platform = p
84938495
config, err := json.Marshal(img)
84948496
if err != nil {
84958497
return nil, errors.Wrapf(err, "failed to marshal image config")
@@ -8768,6 +8770,7 @@ cat <<EOF > $BUILDKIT_SCAN_DESTINATION/spdx.json
87688770
EOF
87698771
`
87708772
img.Config.Cmd = []string{"/bin/sh", "-c", cmd}
8773+
img.Platform = p
87718774
config, err := json.Marshal(img)
87728775
if err != nil {
87738776
return nil, errors.Wrapf(err, "failed to marshal image config")
@@ -8820,6 +8823,7 @@ EOF
88208823

88218824
var img ocispecs.Image
88228825
img.Config.Cmd = []string{"/bin/sh", "-c", "cat /greeting"}
8826+
img.Platform = p
88238827
config, err := json.Marshal(img)
88248828
if err != nil {
88258829
return nil, errors.Wrapf(err, "failed to marshal image config")

client/validation_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package client
22

33
import (
44
"context"
5+
"encoding/json"
56
"io"
67
"testing"
78

89
"github.com/moby/buildkit/client/llb"
910
"github.com/moby/buildkit/frontend/gateway/client"
1011
"github.com/moby/buildkit/util/testutil/integration"
12+
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
1113
"github.com/stretchr/testify/require"
1214
)
1315

@@ -48,3 +50,49 @@ func testValidateNullConfig(t *testing.T, sb integration.Sandbox) {
4850
require.Error(t, err)
4951
require.Contains(t, err.Error(), "invalid null image config for export")
5052
}
53+
54+
func testValidateInvalidConfig(t *testing.T, sb integration.Sandbox) {
55+
requiresLinux(t)
56+
57+
ctx := sb.Context()
58+
59+
c, err := New(ctx, sb.Address())
60+
require.NoError(t, err)
61+
defer c.Close()
62+
63+
b := func(ctx context.Context, c client.Client) (*client.Result, error) {
64+
def, err := llb.Scratch().Marshal(ctx)
65+
if err != nil {
66+
return nil, err
67+
}
68+
69+
res, err := c.Solve(ctx, client.SolveRequest{
70+
Evaluate: true,
71+
Definition: def.ToPB(),
72+
})
73+
if err != nil {
74+
return nil, err
75+
}
76+
var img ocispecs.Image
77+
img.Platform = ocispecs.Platform{
78+
Architecture: "amd64",
79+
}
80+
dt, err := json.Marshal(img)
81+
if err != nil {
82+
return nil, err
83+
}
84+
res.AddMeta("containerimage.config", dt)
85+
return res, nil
86+
}
87+
88+
_, err = c.Build(ctx, SolveOpt{
89+
Exports: []ExportEntry{
90+
{
91+
Type: ExporterOCI,
92+
Output: fixedWriteCloser(nopWriteCloser{io.Discard}),
93+
},
94+
},
95+
}, "", b, nil)
96+
require.Error(t, err)
97+
require.Contains(t, err.Error(), "invalid image config for export: missing os")
98+
}

exporter/containerimage/writer.go

+7
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,13 @@ func patchImageConfig(dt []byte, descs []ocispecs.Descriptor, history []ocispecs
583583
return nil, errors.Errorf("invalid null image config for export")
584584
}
585585

586+
if img.OS == "" {
587+
return nil, errors.Errorf("invalid image config for export: missing os")
588+
}
589+
if img.Architecture == "" {
590+
return nil, errors.Errorf("invalid image config for export: missing architecture")
591+
}
592+
586593
var rootFS ocispecs.RootFS
587594
rootFS.Type = "layers"
588595
for _, desc := range descs {

0 commit comments

Comments
 (0)