-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: set record provenance in response
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
136 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package build | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/base64" | ||
"io" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/containerd/containerd/content" | ||
"github.com/containerd/containerd/content/proxy" | ||
"github.com/docker/buildx/util/progress" | ||
controlapi "github.com/moby/buildkit/api/services/control" | ||
"github.com/moby/buildkit/client" | ||
"github.com/opencontainers/go-digest" | ||
ocispecs "github.com/opencontainers/image-spec/specs-go/v1" | ||
"github.com/pkg/errors" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
func setRecordProvenance(ctx context.Context, c *client.Client, sr *client.SolveResponse, ref string, pw progress.Writer) error { | ||
var mu sync.Mutex | ||
pw = progress.ResetTime(pw) | ||
return progress.Wrap("fetching build record provenance", pw.Write, func(l progress.SubLogger) error { | ||
cl, err := c.ControlClient().ListenBuildHistory(ctx, &controlapi.BuildHistoryRequest{ | ||
Ref: ref, | ||
EarlyExit: true, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
eg, ctx := errgroup.WithContext(ctx) | ||
for { | ||
ev, err := cl.Recv() | ||
if errors.Is(err, io.EOF) { | ||
break | ||
} else if err != nil { | ||
return err | ||
} | ||
if ev.Record == nil { | ||
continue | ||
} | ||
if ev.Record.Result != nil { | ||
provenanceDgst := provenanceDigest(ev.Record.Result) | ||
if provenanceDgst == nil { | ||
continue | ||
} | ||
eg.Go(func() error { | ||
buf := &bytes.Buffer{} | ||
if err := writeBlob(ctx, c, *provenanceDgst, buf); err != nil { | ||
return errors.Wrapf(err, "failed to load provenance from build record") | ||
} | ||
mu.Lock() | ||
sr.ExporterResponse["buildx.build.provenance"] = base64.StdEncoding.EncodeToString(buf.Bytes()) | ||
mu.Unlock() | ||
return nil | ||
}) | ||
} else if ev.Record.Results != nil { | ||
for platform, res := range ev.Record.Results { | ||
platform := platform | ||
provenanceDgst := provenanceDigest(res) | ||
if provenanceDgst == nil { | ||
continue | ||
} | ||
eg.Go(func() error { | ||
buf := &bytes.Buffer{} | ||
if err := writeBlob(ctx, c, *provenanceDgst, buf); err != nil { | ||
return errors.Wrapf(err, "failed to load provenance from build record") | ||
} | ||
mu.Lock() | ||
sr.ExporterResponse["buildx.build.provenance/"+platform] = base64.StdEncoding.EncodeToString(buf.Bytes()) | ||
mu.Unlock() | ||
return nil | ||
}) | ||
} | ||
} | ||
} | ||
return eg.Wait() | ||
}) | ||
} | ||
|
||
func provenanceDigest(res *controlapi.BuildResultInfo) *digest.Digest { | ||
for _, a := range res.Attestations { | ||
if a.MediaType == "application/vnd.in-toto+json" && strings.HasPrefix(a.Annotations["in-toto.io/predicate-type"], "https://slsa.dev/provenance/") { | ||
return &a.Digest | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func writeBlob(ctx context.Context, c *client.Client, dgst digest.Digest, w io.Writer) error { | ||
store := proxy.NewContentStore(c.ContentClient()) | ||
ra, err := store.ReaderAt(ctx, ocispecs.Descriptor{ | ||
Digest: dgst, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
defer ra.Close() | ||
_, err = io.Copy(w, content.NewReader(ra)) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters