Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
binfotypes "github.com/moby/buildkit/util/buildinfo/types"
"github.com/moby/buildkit/util/contentutil"
"github.com/moby/buildkit/util/entitlements"
"github.com/moby/buildkit/util/purl"
"github.com/moby/buildkit/util/testutil"
"github.com/moby/buildkit/util/testutil/echoserver"
"github.com/moby/buildkit/util/testutil/httpserver"
Expand Down Expand Up @@ -6527,21 +6528,24 @@ func testExportAttestations(t *testing.T, sb integration.Sandbox) {
return res, nil
}

target := registry + "/buildkit/testattestations:latest"
targets := []string{
registry + "/buildkit/testattestationsfoo:latest",
registry + "/buildkit/testattestationsbar:latest",
}
_, err = c.Build(sb.Context(), SolveOpt{
Exports: []ExportEntry{
{
Type: ExporterImage,
Attrs: map[string]string{
"name": target,
"name": strings.Join(targets, ","),
"push": "true",
},
},
},
}, "", frontend, nil)
require.NoError(t, err)

desc, provider, err := contentutil.ProviderFromRef(target)
desc, provider, err := contentutil.ProviderFromRef(targets[0])
require.NoError(t, err)

imgs, err := testutil.ReadImages(sb.Context(), provider, desc)
Expand Down Expand Up @@ -6574,15 +6578,29 @@ func testExportAttestations(t *testing.T, sb integration.Sandbox) {
var attest intoto.Statement
require.NoError(t, json.Unmarshal(att.LayersRaw[0], &attest))

purls := map[string]string{}
for _, k := range targets {
p, _ := purl.RefToPURL(k, &ps[i])
purls[k] = p
}

require.Equal(t, "https://in-toto.io/Statement/v0.1", attest.Type)
require.Equal(t, "https://example.com/attestations/v1.0", attest.PredicateType)
require.Equal(t, map[string]interface{}{"success": true}, attest.Predicate)
subjects := []intoto.Subject{{
Name: "_",
Digest: map[string]string{
"sha256": bases[i].Desc.Digest.Encoded(),
subjects := []intoto.Subject{
{
Name: purls[targets[0]],
Digest: map[string]string{
"sha256": bases[i].Desc.Digest.Encoded(),
},
},
}}
{
Name: purls[targets[1]],
Digest: map[string]string{
"sha256": bases[i].Desc.Digest.Encoded(),
},
},
}
require.Equal(t, subjects, attest.Subject)

var attest2 intoto.Statement
Expand All @@ -6609,9 +6627,10 @@ func testExportAttestations(t *testing.T, sb integration.Sandbox) {
defer client.Close()
ctx := namespaces.WithNamespace(sb.Context(), "buildkit")

err = client.ImageService().Delete(ctx, target, images.SynchronousDelete())
require.NoError(t, err)

for _, target := range targets {
err = client.ImageService().Delete(ctx, target, images.SynchronousDelete())
require.NoError(t, err)
}
checkAllReleasable(t, c, sb, true)
}

Expand Down Expand Up @@ -6737,8 +6756,11 @@ func testAttestationDefaultSubject(t *testing.T, sb integration.Sandbox) {
require.Equal(t, "https://in-toto.io/Statement/v0.1", attest.Type)
require.Equal(t, "https://example.com/attestations/v1.0", attest.PredicateType)
require.Equal(t, map[string]interface{}{"success": true}, attest.Predicate)

name, _ := purl.RefToPURL(target, &ps[0])

subjects := []intoto.Subject{{
Name: "_",
Name: name,
Digest: map[string]string{
"sha256": bases[i].Desc.Digest.Encoded(),
},
Expand Down
73 changes: 48 additions & 25 deletions exporter/containerimage/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
binfotypes "github.com/moby/buildkit/util/buildinfo/types"
"github.com/moby/buildkit/util/compression"
"github.com/moby/buildkit/util/progress"
"github.com/moby/buildkit/util/purl"
"github.com/moby/buildkit/util/system"
"github.com/moby/buildkit/util/tracing"
digest "github.com/opencontainers/go-digest"
Expand Down Expand Up @@ -85,7 +86,7 @@ func (ic *ImageWriter) Commit(ctx context.Context, inp *exporter.Source, session
}
}

mfstDesc, configDesc, err := ic.commitDistributionManifest(ctx, inp.Ref, inp.Metadata[exptypes.ExporterImageConfigKey], &remotes[0], opts.Annotations.Platform(nil), opts.OCITypes, inp.Metadata[exptypes.ExporterInlineCache], dtbi)
mfstDesc, configDesc, err := ic.commitDistributionManifest(ctx, opts, inp.Ref, inp.Metadata[exptypes.ExporterImageConfigKey], &remotes[0], opts.Annotations.Platform(nil), inp.Metadata[exptypes.ExporterInlineCache], dtbi)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -165,7 +166,7 @@ func (ic *ImageWriter) Commit(ctx context.Context, inp *exporter.Source, session
}
}

desc, _, err := ic.commitDistributionManifest(ctx, r, config, &remotes[remotesMap[p.ID]], opts.Annotations.Platform(&p.Platform), opts.OCITypes, inlineCache, dtbi)
desc, _, err := ic.commitDistributionManifest(ctx, opts, r, config, &remotes[remotesMap[p.ID]], opts.Annotations.Platform(&p.Platform), inlineCache, dtbi)
if err != nil {
return nil, err
}
Expand All @@ -176,12 +177,12 @@ func (ic *ImageWriter) Commit(ctx context.Context, inp *exporter.Source, session
labels[fmt.Sprintf("containerd.io/gc.ref.content.%d", i)] = desc.Digest.String()

if attestations, ok := inp.Attestations[p.ID]; ok {
inTotos, err := ic.extractAttestations(ctx, session.NewGroup(sessionID), desc, inp.Refs, attestations)
inTotos, err := ic.extractAttestations(ctx, opts, session.NewGroup(sessionID), desc, inp.Refs, attestations)
if err != nil {
return nil, err
}

desc, err := ic.commitAttestationsManifest(ctx, p, desc.Digest.String(), opts.OCITypes, inTotos)
desc, err := ic.commitAttestationsManifest(ctx, opts, p, desc.Digest.String(), inTotos)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -254,7 +255,7 @@ func (ic *ImageWriter) exportLayers(ctx context.Context, refCfg cacheconfig.RefC
return out, err
}

func (ic *ImageWriter) extractAttestations(ctx context.Context, s session.Group, desc *ocispecs.Descriptor, refs map[string]cache.ImmutableRef, attestations []result.Attestation) ([]intoto.Statement, error) {
func (ic *ImageWriter) extractAttestations(ctx context.Context, opts *ImageCommitOpts, s session.Group, desc *ocispecs.Descriptor, refs map[string]cache.ImmutableRef, attestations []result.Attestation) ([]intoto.Statement, error) {
eg, ctx := errgroup.WithContext(ctx)
statements := make([]intoto.Statement, len(attestations))

Expand Down Expand Up @@ -290,35 +291,57 @@ func (ic *ImageWriter) extractAttestations(ctx context.Context, s session.Group,
if len(predicate) == 0 {
predicate = nil
}
statements[i] = intoto.Statement{
StatementHeader: intoto.StatementHeader{
Type: intoto.StatementInTotoV01,
PredicateType: att.InToto.PredicateType,
},
Predicate: json.RawMessage(predicate),
}

if len(att.InToto.Subjects) == 0 {
att.InToto.Subjects = []result.InTotoSubject{{
Kind: gatewaypb.InTotoSubjectKindSelf,
}}
}

statements[i].Subject = make([]intoto.Subject, len(att.InToto.Subjects))
for j, subject := range att.InToto.Subjects {
statements[i].Subject[j].Name = "_"
subjects := make([]intoto.Subject, 0, len(att.InToto.Subjects))
for _, subject := range att.InToto.Subjects {
name := "_"
if subject.Name != "" {
statements[i].Subject[j].Name = subject.Name
name = subject.Name
}
switch subject.Kind {
case gatewaypb.InTotoSubjectKindSelf:
statements[i].Subject[j].Digest = result.DigestMap(desc.Digest)
var names []string
if opts.ImageName != "" {
for _, name := range strings.Split(opts.ImageName, ",") {
name, err := purl.RefToPURL(name, desc.Platform)
if err != nil {
return err
}
names = append(names, name)
}
} else {
names = []string{name}
}
for _, name := range names {
subjects = append(subjects, intoto.Subject{
Name: name,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. I'm not entirely sure, in-toto doesn't require it, so it's up to us. I do think we want to make sure that the platform is present in the name though (to ensure uniqueness), since then we'll have a 1-to-1 relationship between digests and names.

So maybe PURL would be the right choice here, with the version field containing the image tag, and the image repository location and architecture/os data in the qualifiers? Not sure whether we should use the sha256: in the name though, since that would be duplicated.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have cherry-picked your PURL util package (with a fixup to use reference for the distribution reference package, like we have in the other parts of the codebase).

Digest: result.DigestMap(desc.Digest),
})
}
case gatewaypb.InTotoSubjectKindRaw:
statements[i].Subject[j].Digest = result.DigestMap(subject.Digest...)
subjects = append(subjects, intoto.Subject{
Name: name,
Digest: result.DigestMap(subject.Digest...),
})

default:
return errors.Errorf("unknown attestation subject kind %q", subject.Kind)
}
}
statements[i] = intoto.Statement{
StatementHeader: intoto.StatementHeader{
Type: intoto.StatementInTotoV01,
PredicateType: att.InToto.PredicateType,
Subject: subjects,
},
Predicate: json.RawMessage(predicate),
}
}
return nil
})
Expand All @@ -330,7 +353,7 @@ func (ic *ImageWriter) extractAttestations(ctx context.Context, s session.Group,
return statements, nil
}

func (ic *ImageWriter) commitDistributionManifest(ctx context.Context, ref cache.ImmutableRef, config []byte, remote *solver.Remote, annotations *Annotations, oci bool, inlineCache []byte, buildInfo []byte) (*ocispecs.Descriptor, *ocispecs.Descriptor, error) {
func (ic *ImageWriter) commitDistributionManifest(ctx context.Context, opts *ImageCommitOpts, ref cache.ImmutableRef, config []byte, remote *solver.Remote, annotations *Annotations, inlineCache []byte, buildInfo []byte) (*ocispecs.Descriptor, *ocispecs.Descriptor, error) {
if len(config) == 0 {
var err error
config, err = defaultImageConfig()
Expand All @@ -350,7 +373,7 @@ func (ic *ImageWriter) commitDistributionManifest(ctx context.Context, ref cache
return nil, nil, err
}

remote, history = normalizeLayersAndHistory(ctx, remote, history, ref, oci)
remote, history = normalizeLayersAndHistory(ctx, remote, history, ref, opts.OCITypes)

config, err = patchImageConfig(config, remote.Descriptors, history, inlineCache, buildInfo)
if err != nil {
Expand All @@ -364,7 +387,7 @@ func (ic *ImageWriter) commitDistributionManifest(ctx context.Context, ref cache
)

// Use docker media types for older Docker versions and registries
if !oci {
if !opts.OCITypes {
manifestType = images.MediaTypeDockerSchema2Manifest
configType = images.MediaTypeDockerSchema2Config
}
Expand Down Expand Up @@ -395,7 +418,7 @@ func (ic *ImageWriter) commitDistributionManifest(ctx context.Context, ref cache
}

for i, desc := range remote.Descriptors {
removeInternalLayerAnnotations(&desc, oci)
removeInternalLayerAnnotations(&desc, opts.OCITypes)
mfst.Layers = append(mfst.Layers, desc)
labels[fmt.Sprintf("containerd.io/gc.ref.content.%d", i+1)] = desc.Digest.String()
}
Expand Down Expand Up @@ -437,12 +460,12 @@ func (ic *ImageWriter) commitDistributionManifest(ctx context.Context, ref cache
}, &configDesc, nil
}

func (ic *ImageWriter) commitAttestationsManifest(ctx context.Context, p exptypes.Platform, target string, oci bool, statements []intoto.Statement) (*ocispecs.Descriptor, error) {
func (ic *ImageWriter) commitAttestationsManifest(ctx context.Context, opts *ImageCommitOpts, p exptypes.Platform, target string, statements []intoto.Statement) (*ocispecs.Descriptor, error) {
var (
manifestType = ocispecs.MediaTypeImageManifest
configType = ocispecs.MediaTypeImageConfig
)
if !oci {
if !opts.OCITypes {
manifestType = images.MediaTypeDockerSchema2Manifest
configType = images.MediaTypeDockerSchema2Config
}
Expand Down Expand Up @@ -507,7 +530,7 @@ func (ic *ImageWriter) commitAttestationsManifest(ctx context.Context, p exptype
"containerd.io/gc.ref.content.0": configDigest.String(),
}
for i, desc := range layers {
removeInternalLayerAnnotations(&desc, oci)
removeInternalLayerAnnotations(&desc, opts.OCITypes)
mfst.Layers = append(mfst.Layers, desc)
labels[fmt.Sprintf("containerd.io/gc.ref.content.%d", i+1)] = desc.Digest.String()
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ require (
github.com/opencontainers/runc v1.1.3
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417
github.com/opencontainers/selinux v1.10.1
github.com/package-url/packageurl-go v0.1.0
github.com/pelletier/go-toml v1.9.4
github.com/pkg/errors v0.9.1
github.com/pkg/profile v1.5.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,8 @@ github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYr
github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8=
github.com/openzipkin/zipkin-go v0.1.3/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8=
github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw=
github.com/package-url/packageurl-go v0.1.0 h1:efWBc98O/dBZRg1pw2xiDzovnlMjCa9NPnfaiBduh8I=
github.com/package-url/packageurl-go v0.1.0/go.mod h1:C/ApiuWpmbpni4DIOECf6WCjFUZV7O1Fx7VAzrZHgBw=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
Expand Down
Loading