Skip to content

Commit

Permalink
exporter: add validation for invalid platorm
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <[email protected]>
(cherry picked from commit d293ec3208f87fefab7a1caadffa3f3f50604796)
(cherry picked from commit 42b95935d606b262a33374eeeb452bb7c299c729)
Signed-off-by: Andrey Epifanov <[email protected]>

# Conflicts:
#	client/client_test.go
  • Loading branch information
tonistiigi authored and aepifanov committed Feb 14, 2024
1 parent 0a96db9 commit 5a0d94c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func TestIntegration(t *testing.T) {
testStargzLazyRegistryCacheImportExport,
testValidateDigestOrigin,
testValidateNullConfig,
testValidateInvalidConfig,
)
tests = append(tests, diffOpTestCases()...)
integration.Run(t, tests, mirrors)
Expand Down
46 changes: 46 additions & 0 deletions client/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package client

import (
"context"
"encoding/json"
"io"
"testing"

"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/frontend/gateway/client"
"github.com/moby/buildkit/util/testutil/integration"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -48,3 +50,47 @@ func testValidateNullConfig(t *testing.T, sb integration.Sandbox) {
require.Error(t, err)
require.Contains(t, err.Error(), "invalid null image config for export")
}

func testValidateInvalidConfig(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
}
var img ocispecs.Image
img.Architecture = "amd64"
dt, err := json.Marshal(img)
if err != nil {
return nil, err
}
res.AddMeta("containerimage.config", 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(), "invalid image config for export: missing os")
}
7 changes: 7 additions & 0 deletions exporter/containerimage/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,13 @@ func patchImageConfig(dt []byte, descs []ocispecs.Descriptor, history []ocispecs
return nil, errors.Errorf("invalid null image config for export")
}

if img.OS == "" {
return nil, errors.Errorf("invalid image config for export: missing os")
}
if img.Architecture == "" {
return nil, errors.Errorf("invalid image config for export: missing architecture")
}

var rootFS ocispecs.RootFS
rootFS.Type = "layers"
for _, desc := range descs {
Expand Down

0 comments on commit 5a0d94c

Please sign in to comment.