-
Notifications
You must be signed in to change notification settings - Fork 616
policy: add image.provenance input type #3652
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
Merged
+708
−15
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,27 @@ | ||
| package policy | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| gwpb "github.com/moby/buildkit/frontend/gateway/pb" | ||
| ocispecs "github.com/opencontainers/image-spec/specs-go/v1" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestSourceResolverOptIncludesResolveAttestations(t *testing.T) { | ||
| req := &gwpb.ResolveSourceMetaRequest{ | ||
| ResolveMode: "default", | ||
| Image: &gwpb.ResolveSourceImageRequest{ | ||
| NoConfig: true, | ||
| ResolveAttestations: []string{"https://slsa.dev/provenance/v0.2"}, | ||
| }, | ||
| } | ||
| platform := &ocispecs.Platform{OS: "linux", Architecture: "amd64"} | ||
|
|
||
| opt := sourceResolverOpt(req, platform) | ||
| require.NotNil(t, opt.ImageOpt) | ||
| require.True(t, opt.ImageOpt.NoConfig) | ||
| require.Equal(t, []string{"https://slsa.dev/provenance/v0.2"}, opt.ImageOpt.ResolveAttestations) | ||
| require.Equal(t, "default", opt.ImageOpt.ResolveMode) | ||
| require.Equal(t, platform, opt.ImageOpt.Platform) | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,175 @@ | ||
| package policy | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "slices" | ||
| "strings" | ||
| "time" | ||
|
|
||
| slsa02 "github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/v0.2" | ||
| slsa1 "github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/v1" | ||
| gwpb "github.com/moby/buildkit/frontend/gateway/pb" | ||
| provenancetypes "github.com/moby/buildkit/solver/llbsolver/provenance/types" | ||
| ) | ||
|
|
||
| const predicateTypeAnnotation = "in-toto.io/predicate-type" | ||
|
|
||
| var resolveProvenanceAttestations = []string{ | ||
| slsa02.PredicateSLSAProvenance, | ||
| slsa1.PredicateSLSAProvenance, | ||
| } | ||
|
|
||
| type inTotoStatement struct { | ||
| PredicateType string `json:"predicateType"` | ||
| Predicate json.RawMessage `json:"predicate"` | ||
| } | ||
|
|
||
| func parseProvenance(ac *gwpb.AttestationChain) (*ImageProvenance, error) { | ||
| if ac == nil || len(ac.Blobs) == 0 { | ||
| return nil, nil | ||
| } | ||
|
|
||
| // Prefer blobs that explicitly declare a provenance predicate type. | ||
| for _, b := range ac.Blobs { | ||
| if b == nil || b.Descriptor_ == nil || len(b.Data) == 0 { | ||
| continue | ||
| } | ||
| pt := b.Descriptor_.Annotations[predicateTypeAnnotation] | ||
| if pt == "" { | ||
| continue | ||
| } | ||
| if !slices.Contains(resolveProvenanceAttestations, pt) { | ||
| continue | ||
| } | ||
| prv, err := parseProvenanceBlob(b.Data, pt) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if prv != nil { | ||
| return prv, nil | ||
| } | ||
| } | ||
|
|
||
| return nil, nil | ||
| } | ||
|
|
||
| func parseProvenanceBlob(dt []byte, pt string) (*ImageProvenance, error) { | ||
| var stmt inTotoStatement | ||
| if err := json.Unmarshal(dt, &stmt); err != nil || len(stmt.Predicate) == 0 { | ||
| return nil, nil | ||
| } | ||
| if stmt.PredicateType != "" && stmt.PredicateType != pt { | ||
| return nil, nil | ||
| } | ||
|
|
||
| switch pt { | ||
| case slsa1.PredicateSLSAProvenance: | ||
| return parseSLSA1Provenance(stmt.Predicate) | ||
| case slsa02.PredicateSLSAProvenance: | ||
| return parseSLSA02Provenance(stmt.Predicate) | ||
| } | ||
| return nil, nil | ||
| } | ||
|
|
||
| func parseSLSA1Provenance(dt []byte) (*ImageProvenance, error) { | ||
| var pred provenancetypes.ProvenancePredicateSLSA1 | ||
| if err := json.Unmarshal(dt, &pred); err != nil { | ||
| return nil, nil | ||
| } | ||
| if pred.BuildDefinition.BuildType == "" && pred.RunDetails.Builder.ID == "" { | ||
| return nil, nil | ||
| } | ||
| prv := &ImageProvenance{ | ||
| PredicateType: slsa1.PredicateSLSAProvenance, | ||
| BuildType: pred.BuildDefinition.BuildType, | ||
| BuilderID: pred.RunDetails.Builder.ID, | ||
| ConfigSource: &ImageProvenanceConfigSource{ | ||
| URI: pred.BuildDefinition.ExternalParameters.ConfigSource.URI, | ||
| Digest: pred.BuildDefinition.ExternalParameters.ConfigSource.Digest, | ||
| Path: pred.BuildDefinition.ExternalParameters.ConfigSource.Path, | ||
| }, | ||
| Frontend: pred.BuildDefinition.ExternalParameters.Request.Frontend, | ||
| BuildArgs: extractBuildArgs(pred.BuildDefinition.ExternalParameters.Request.Args), | ||
| RawArgs: pred.BuildDefinition.ExternalParameters.Request.Args, | ||
| } | ||
|
|
||
| if md := pred.RunDetails.Metadata; md != nil { | ||
| prv.InvocationID = md.InvocationID | ||
| prv.StartedOn = formatProvenanceTime(md.StartedOn) | ||
| prv.FinishedOn = formatProvenanceTime(md.FinishedOn) | ||
| prv.Reproducible = boolPtr(md.Reproducible) | ||
| prv.Hermetic = boolPtr(md.Hermetic) | ||
| prv.Completeness = &ImageProvenanceCompleteness{ | ||
| Parameters: boolPtr(md.Completeness.Request), | ||
| Materials: boolPtr(md.Completeness.ResolvedDependencies), | ||
| } | ||
| } | ||
|
|
||
| return prv, nil | ||
| } | ||
|
|
||
| func parseSLSA02Provenance(dt []byte) (*ImageProvenance, error) { | ||
| var pred provenancetypes.ProvenancePredicateSLSA02 | ||
| if err := json.Unmarshal(dt, &pred); err != nil { | ||
| return nil, nil | ||
| } | ||
| if pred.BuildType == "" && pred.Builder.ID == "" { | ||
| return nil, nil | ||
| } | ||
| prv := &ImageProvenance{ | ||
| PredicateType: slsa02.PredicateSLSAProvenance, | ||
| BuildType: pred.BuildType, | ||
| BuilderID: pred.Builder.ID, | ||
| ConfigSource: &ImageProvenanceConfigSource{ | ||
| URI: pred.Invocation.ConfigSource.URI, | ||
| Digest: pred.Invocation.ConfigSource.Digest, | ||
| Path: pred.Invocation.ConfigSource.EntryPoint, | ||
| }, | ||
| Frontend: pred.Invocation.Parameters.Frontend, | ||
| BuildArgs: extractBuildArgs(pred.Invocation.Parameters.Args), | ||
| RawArgs: pred.Invocation.Parameters.Args, | ||
| } | ||
|
|
||
| if md := pred.Metadata; md != nil { | ||
| prv.InvocationID = md.BuildInvocationID | ||
| prv.StartedOn = formatProvenanceTime(md.BuildStartedOn) | ||
| prv.FinishedOn = formatProvenanceTime(md.BuildFinishedOn) | ||
| prv.Reproducible = boolPtr(md.Reproducible) | ||
| prv.Hermetic = boolPtr(md.Hermetic) | ||
| prv.Completeness = &ImageProvenanceCompleteness{ | ||
| Parameters: boolPtr(md.Completeness.Parameters), | ||
| Environment: boolPtr(md.Completeness.Environment), | ||
| Materials: boolPtr(md.Completeness.Materials), | ||
| } | ||
| } | ||
|
|
||
| return prv, nil | ||
| } | ||
|
|
||
| func boolPtr(v bool) *bool { | ||
| return &v | ||
| } | ||
|
|
||
| func formatProvenanceTime(t *time.Time) string { | ||
| if t == nil { | ||
| return "" | ||
| } | ||
| return t.UTC().Format(time.RFC3339) | ||
| } | ||
|
|
||
| func extractBuildArgs(args map[string]string) map[string]string { | ||
| if len(args) == 0 { | ||
| return nil | ||
| } | ||
| const prefix = "build-arg:" | ||
| out := make(map[string]string) | ||
| for k, v := range args { | ||
| if name, ok := strings.CutPrefix(k, prefix); ok && name != "" { | ||
| out[name] = v | ||
| } | ||
| } | ||
| if len(out) == 0 { | ||
| return nil | ||
| } | ||
| return out | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is intentionally missing materials atm.
I wanted to make every material type
Inputso that policy conditions can run in any level of source material in c2164fb .But I missed that it is not allowed to return different source from a policy callback. So the current patch only works in
policy evaland needs a complete redesign.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes policy callback can't return a different source. Looks good for follow-up.