From 22c600788051c4f65836accf7a9391932d407002 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Sun, 17 Dec 2023 20:43:57 -0800 Subject: [PATCH] exporter: add validation for platforms key value Signed-off-by: Tonis Tiigi (cherry picked from commit 432ece72ae124ce8a29ced6854a08206f09f3a73) (cherry picked from commit e4bd60baf77b4ec92aba60f568831fb3076fc158) Signed-off-by: Andrey Epifanov # Conflicts: # exporter/containerimage/exptypes/parse.go --- client/client_test.go | 2 + client/validation_test.go | 108 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) diff --git a/client/client_test.go b/client/client_test.go index 8ddcd78138740..ac89723ce6225 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -164,6 +164,8 @@ func TestIntegration(t *testing.T) { testValidateDigestOrigin, testValidateNullConfig, testValidateInvalidConfig, + testValidatePlatformsEmpty, + testValidatePlatformsInvalid, ) tests = append(tests, diffOpTestCases()...) integration.Run(t, tests, mirrors) diff --git a/client/validation_test.go b/client/validation_test.go index e9ca8e4a3c210..cd84c74d72b38 100644 --- a/client/validation_test.go +++ b/client/validation_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/moby/buildkit/client/llb" + "github.com/moby/buildkit/exporter/containerimage/exptypes" "github.com/moby/buildkit/frontend/gateway/client" "github.com/moby/buildkit/util/testutil/integration" ocispecs "github.com/opencontainers/image-spec/specs-go/v1" @@ -94,3 +95,110 @@ func testValidateInvalidConfig(t *testing.T, sb integration.Sandbox) { require.Error(t, err) require.Contains(t, err.Error(), "invalid image config for export: missing os") } + +func testValidatePlatformsEmpty(t *testing.T, sb integration.Sandbox) { + requiresLinux(t) + + ctx := sb.Context() + + c, err := New(ctx, sb.Address()) + require.NoError(t, err) + defer c.Close() + + b := func(ctx context.Context, c client.Client) (*client.Result, error) { + def, err := llb.Scratch().Marshal(ctx) + if err != nil { + return nil, err + } + + res, err := c.Solve(ctx, client.SolveRequest{ + Evaluate: true, + Definition: def.ToPB(), + }) + if err != nil { + return nil, err + } + res.AddMeta("refs.platforms", []byte("null")) + return res, nil + } + + _, err = c.Build(ctx, SolveOpt{ + Exports: []ExportEntry{ + { + Type: ExporterOCI, + Output: fixedWriteCloser(nopWriteCloser{io.Discard}), + }, + }, + }, "", b, nil) + require.Error(t, err) + require.Contains(t, err.Error(), "invalid empty platforms index for exporter") +} + +func testValidatePlatformsInvalid(t *testing.T, sb integration.Sandbox) { + requiresLinux(t) + + ctx := sb.Context() + + c, err := New(ctx, sb.Address()) + require.NoError(t, err) + defer c.Close() + + tcases := []struct { + name string + value []exptypes.Platform + exp string + }{ + { + name: "emptyID", + value: []exptypes.Platform{{}}, + exp: "invalid empty platform key for exporter", + }, + { + name: "missingOS", + value: []exptypes.Platform{ + { + ID: "foo", + }, + }, + exp: "invalid platform value", + }, + } + + for _, tc := range tcases { + t.Run(tc.name, func(t *testing.T) { + b := func(ctx context.Context, c client.Client) (*client.Result, error) { + def, err := llb.Scratch().Marshal(ctx) + if err != nil { + return nil, err + } + + res, err := c.Solve(ctx, client.SolveRequest{ + Evaluate: true, + Definition: def.ToPB(), + }) + if err != nil { + return nil, err + } + + dt, err := json.Marshal(exptypes.Platforms{Platforms: tc.value}) + if err != nil { + return nil, err + } + + res.AddMeta("refs.platforms", dt) + return res, nil + } + + _, err = c.Build(ctx, SolveOpt{ + Exports: []ExportEntry{ + { + Type: ExporterOCI, + Output: fixedWriteCloser(nopWriteCloser{io.Discard}), + }, + }, + }, "", b, nil) + require.Error(t, err) + require.Contains(t, err.Error(), tc.exp) + }) + } +}