Skip to content
Closed
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
14 changes: 8 additions & 6 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7254,7 +7254,7 @@ func testExportAttestations(t *testing.T, sb integration.Sandbox) {
Type: ExporterLocal,
OutputDir: dir,
Attrs: map[string]string{
"attestation-prefix": "test.",
"attestations-prefix": "test.",
},
},
},
Expand Down Expand Up @@ -7306,7 +7306,7 @@ func testExportAttestations(t *testing.T, sb integration.Sandbox) {
Type: ExporterTar,
Output: fixedWriteCloser(outW),
Attrs: map[string]string{
"attestation-prefix": "test.",
"attestations-prefix": "test.",
},
},
},
Expand All @@ -7321,8 +7321,9 @@ func testExportAttestations(t *testing.T, sb integration.Sandbox) {

for _, p := range ps {
var attest intoto.Statement
dt := m[path.Join(strings.ReplaceAll(platforms.Format(p), "/", "_"), "test.attestation.json")].Data
require.NoError(t, json.Unmarshal(dt, &attest))
item := m[path.Join(strings.ReplaceAll(platforms.Format(p), "/", "_"), "test.attestation.json")]
require.NotNil(t, item)
require.NoError(t, json.Unmarshal(item.Data, &attest))

require.Equal(t, "https://in-toto.io/Statement/v0.1", attest.Type)
require.Equal(t, "https://example.com/attestations/v1.0", attest.PredicateType)
Expand All @@ -7334,8 +7335,9 @@ func testExportAttestations(t *testing.T, sb integration.Sandbox) {
}}, attest.Subject)

var attest2 intoto.Statement
dt = m[path.Join(strings.ReplaceAll(platforms.Format(p), "/", "_"), "test.attestation2.json")].Data
require.NoError(t, json.Unmarshal(dt, &attest2))
item = m[path.Join(strings.ReplaceAll(platforms.Format(p), "/", "_"), "test.attestation2.json")]
require.NotNil(t, item)
require.NoError(t, json.Unmarshal(item.Data, &attest2))

require.Equal(t, "https://in-toto.io/Statement/v0.1", attest2.Type)
require.Equal(t, "https://example.com/attestations2/v1.0", attest2.PredicateType)
Expand Down
49 changes: 18 additions & 31 deletions exporter/attestation/filter.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,32 @@
package attestation

import (
"bytes"

"github.com/moby/buildkit/exporter"
"github.com/moby/buildkit/solver/result"
)

func Filter(attestations []exporter.Attestation, include map[string][]byte, exclude map[string][]byte) []exporter.Attestation {
if len(include) == 0 && len(exclude) == 0 {
return attestations
func FilterReasons(attestations []exporter.Attestation, reasons []string) (matching []exporter.Attestation, nonMatching []exporter.Attestation) {
if reasons == nil {
// don't filter if no filter provided
return attestations, nil
}

result := []exporter.Attestation{}
for _, att := range attestations {
meta := att.Metadata
if meta == nil {
meta = map[string][]byte{}
}

match := true
for k, v := range include {
if !bytes.Equal(meta[k], v) {
match = false
break
target, ok := att.Metadata[result.AttestationReasonKey]
if ok {
matched := false
for _, reason := range reasons {
if string(target) == reason {
matched = true
break
}
}
}
if !match {
continue
}

for k, v := range exclude {
if bytes.Equal(meta[k], v) {
match = false
break
if matched {
matching = append(matching, att)
continue
}
}
if !match {
continue
}

result = append(result, att)
nonMatching = append(nonMatching, att)
}
return result
return matching, nonMatching
}
1 change: 1 addition & 0 deletions exporter/attestation/unbundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func unbundle(ctx context.Context, root string, bundle exporter.Attestation) ([]
}
unbundled = append(unbundled, exporter.Attestation{
Kind: gatewaypb.AttestationKindInToto,
Metadata: bundle.Metadata,
Path: path.Join(bundle.Path, entry.Name()),
ContentFunc: func() ([]byte, error) { return predicate, nil },
InToto: result.InTotoAttestation{
Expand Down
3 changes: 2 additions & 1 deletion exporter/containerimage/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ func (e *imageExporter) Resolve(ctx context.Context, opt map[string]string) (exp
RefCfg: cacheconfig.RefConfig{
Compression: compression.New(compression.Default),
},
BuildInfo: true,
BuildInfo: true,
Attestations: true,
},
store: true,
}
Expand Down
23 changes: 16 additions & 7 deletions exporter/containerimage/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package containerimage

import (
"strconv"
"strings"
"time"

cacheconfig "github.com/moby/buildkit/cache/config"
Expand All @@ -19,6 +20,7 @@ const (
keyOCITypes = "oci-mediatypes"
keyBuildInfo = "buildinfo"
keyBuildInfoAttrs = "buildinfo-attrs"
keyAttestations = "attestations"

// preferNondistLayersKey is an exporter option which can be used to mark a layer as non-distributable if the layer reference was
// already found to use a non-distributable media type.
Expand All @@ -27,13 +29,15 @@ const (
)

type ImageCommitOpts struct {
ImageName string
RefCfg cacheconfig.RefConfig
OCITypes bool
BuildInfo bool
BuildInfoAttrs bool
Annotations AnnotationsGroup
Epoch *time.Time
ImageName string
RefCfg cacheconfig.RefConfig
OCITypes bool
BuildInfo bool
BuildInfoAttrs bool
Annotations AnnotationsGroup
Epoch *time.Time
Attestations bool
AttestationsFilter []string
}

func (c *ImageCommitOpts) Load(opt map[string]string) (map[string]string, error) {
Expand Down Expand Up @@ -73,6 +77,11 @@ func (c *ImageCommitOpts) Load(opt map[string]string) (map[string]string, error)
err = parseBoolWithDefault(&c.BuildInfo, k, v, true)
case keyBuildInfoAttrs:
err = parseBoolWithDefault(&c.BuildInfoAttrs, k, v, false)
case keyAttestations:
if parseBool(&c.Attestations, k, v) != nil {
c.Attestations = true
c.AttestationsFilter = strings.Split(v, ",")
}
case keyPreferNondistLayers:
err = parseBool(&c.RefCfg.PreferNonDistributable, k, v)
default:
Expand Down
17 changes: 8 additions & 9 deletions exporter/containerimage/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -69,19 +68,18 @@ func (ic *ImageWriter) Commit(ctx context.Context, inp *exporter.Source, session
return nil, err
}

requiredAttestations := false
hasAttestations := false
for _, p := range ps.Platforms {
if atts, ok := inp.Attestations[p.ID]; ok {
atts = attestation.Filter(atts, nil, map[string][]byte{
result.AttestationInlineOnlyKey: []byte(strconv.FormatBool(true)),
})
atts, _ = attestation.FilterReasons(atts, opts.AttestationsFilter)
if len(atts) > 0 {
requiredAttestations = true
hasAttestations = true
break
}
}
}
if requiredAttestations {
hasAttestations = opts.Attestations && hasAttestations
if hasAttestations {
isMap = true
}

Expand All @@ -108,7 +106,7 @@ func (ic *ImageWriter) Commit(ctx context.Context, inp *exporter.Source, session
if len(ps.Platforms) > 1 {
return nil, errors.Errorf("cannot export multiple platforms without multi-platform enabled")
}
if requiredAttestations {
if hasAttestations {
return nil, errors.Errorf("cannot export attestations without multi-platform enabled")
}

Expand Down Expand Up @@ -159,7 +157,7 @@ func (ic *ImageWriter) Commit(ctx context.Context, inp *exporter.Source, session
return mfstDesc, nil
}

if len(inp.Attestations) > 0 {
if hasAttestations {
opts.EnableOCITypes("attestations")
}

Expand Down Expand Up @@ -238,6 +236,7 @@ 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 {
attestations, _ = attestation.FilterReasons(attestations, opts.AttestationsFilter)
attestations, err := attestation.Unbundle(ctx, session.NewGroup(sessionID), attestations)
if err != nil {
return nil, err
Expand Down
21 changes: 5 additions & 16 deletions exporter/local/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ import (
"golang.org/x/time/rate"
)

const (
keyAttestationPrefix = "attestation-prefix"
)

type Opt struct {
SessionManager *session.Manager
}
Expand All @@ -39,24 +35,17 @@ func New(opt Opt) (exporter.Exporter, error) {
}

func (e *localExporter) Resolve(ctx context.Context, opt map[string]string) (exporter.ExporterInstance, error) {
tm, _, err := epoch.ParseExporterAttrs(opt)
if err != nil {
return nil, err
}

i := &localExporterInstance{
localExporter: e,
opts: CreateFSOpts{
Epoch: tm,
Attestations: true,
},
}

for k, v := range opt {
switch k {
case keyAttestationPrefix:
i.opts.AttestationPrefix = v
}
opt, err := i.opts.Load(opt)
if err != nil {
return nil, err
}
_ = opt

return i, nil
}
Expand Down
57 changes: 47 additions & 10 deletions exporter/local/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"os"
"path"
"strconv"
"strings"
"time"

"github.com/docker/docker/pkg/idtools"
intoto "github.com/in-toto/in-toto-golang/in_toto"
"github.com/moby/buildkit/cache"
"github.com/moby/buildkit/exporter"
"github.com/moby/buildkit/exporter/attestation"
"github.com/moby/buildkit/exporter/util/epoch"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/snapshot"
"github.com/moby/buildkit/solver/result"
Expand All @@ -25,9 +27,45 @@ import (
fstypes "github.com/tonistiigi/fsutil/types"
)

const (
keyAttestations = "attestations"
keyAttestationsPrefix = "attestations-prefix"
)

type CreateFSOpts struct {
Epoch *time.Time
AttestationPrefix string
Epoch *time.Time
Attestations bool
AttestationsFilter []string
AttestationPrefix string
}

func (c *CreateFSOpts) Load(opt map[string]string) (map[string]string, error) {
rest := make(map[string]string)

var err error
c.Epoch, opt, err = epoch.ParseExporterAttrs(opt)
if err != nil {
return nil, err
}

for k, v := range opt {
switch k {
case keyAttestations:
b, err := strconv.ParseBool(v)
if err == nil {
c.Attestations = b
} else {
c.Attestations = true
c.AttestationsFilter = strings.Split(v, ",")
}
case keyAttestationsPrefix:
c.AttestationPrefix = v
default:
rest[k] = v
}
}

return rest, nil
}

func CreateFS(ctx context.Context, sessionID string, k string, ref cache.ImmutableRef, attestations []exporter.Attestation, defaultTime time.Time, opt CreateFSOpts) (fsutil.FS, func() error, error) {
Expand Down Expand Up @@ -89,14 +127,13 @@ func CreateFS(ctx context.Context, sessionID string, k string, ref cache.Immutab
}

outputFS := fsutil.NewFS(src, walkOpt)
attestations = attestation.Filter(attestations, nil, map[string][]byte{
result.AttestationInlineOnlyKey: []byte(strconv.FormatBool(true)),
})
attestations, err = attestation.Unbundle(ctx, session.NewGroup(sessionID), attestations)
if err != nil {
return nil, nil, err
}
if len(attestations) > 0 {
attestations, _ = attestation.FilterReasons(attestations, opt.AttestationsFilter)
if opt.Attestations && len(attestations) > 0 {
attestations, err = attestation.Unbundle(ctx, session.NewGroup(sessionID), attestations)
if err != nil {
return nil, nil, err
}

subjects := []intoto.Subject{}
err = outputFS.Walk(ctx, func(path string, info fs.FileInfo, err error) error {
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions exporter/oci/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ func (e *imageExporter) Resolve(ctx context.Context, opt map[string]string) (exp
RefCfg: cacheconfig.RefConfig{
Compression: compression.New(compression.Default),
},
BuildInfo: true,
OCITypes: e.opt.Variant == VariantOCI,
BuildInfo: true,
OCITypes: e.opt.Variant == VariantOCI,
Attestations: true,
},
}

Expand Down
Loading