Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce new attestation exporter options #3403

Closed
wants to merge 5 commits into from
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 @@ -7402,7 +7402,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 @@ -7454,7 +7454,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 @@ -7469,8 +7469,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 @@ -7482,8 +7483,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
64 changes: 34 additions & 30 deletions exporter/attestation/filter.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,49 @@
package attestation

import (
"bytes"
"strconv"

"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
}

result := []exporter.Attestation{}
func FilterInline(attestations []exporter.Attestation) (matching []exporter.Attestation, nonMatching []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
v, ok := att.Metadata[result.AttestationInlineOnlyKey]
if ok {
b, err := strconv.ParseBool(string(v))
if b && err == nil {
matching = append(matching, att)
continue
}
}
if !match {
continue
}
nonMatching = append(nonMatching, att)
}
return matching, nonMatching
}

for k, v := range exclude {
if bytes.Equal(meta[k], v) {
match = false
break
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
}

for _, att := range attestations {
target, ok := att.Metadata[result.AttestationReasonKey]
Copy link
Member

Choose a reason for hiding this comment

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

Not from this PR but I don't understand why this is called a "reason". It is attestation type. So why can't we just use the already existing predicate type field?

if ok {
matched := false
for _, reason := range reasons {
if string(target) == reason {
matched = true
break
}
}
if matched {
matching = append(matching, att)
continue
}
}
if !match {
continue
}

result = append(result, att)
nonMatching = append(nonMatching, att)
}
return result
return matching, nonMatching
}
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
18 changes: 9 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,19 @@ 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.FilterInline(atts)
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 +107,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 +158,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 +237,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
58 changes: 48 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,14 @@ 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.FilterInline(attestations)
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