-
Notifications
You must be signed in to change notification settings - Fork 39
refactor: run attestation-level policy evaluations in crafting layer #1689
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
migmartri
merged 2 commits into
chainloop-dev:main
from
migmartri:1685-refactor-policy-evaluations
Jan 2, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -141,12 +141,12 @@ func (action *AttestationStatus) Run(ctx context.Context, attestationID string, | |
| } | ||
|
|
||
| // We do not want to evaluate policies here during render since we want to do it in a separate step | ||
| statement, err := renderer.RenderStatement(ctx, chainloop.WithSkipPolicyEvaluation(true)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. renderer never evaluates policies anymore |
||
| statement, err := renderer.RenderStatement(ctx) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("rendering statement: %w", err) | ||
| } | ||
|
|
||
| res.PolicyEvaluations, err = action.getPolicyEvaluations(ctx, c, statement) | ||
| res.PolicyEvaluations, err = action.getPolicyEvaluations(ctx, c, attestationID, statement) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("getting policy evaluations: %w", err) | ||
| } | ||
|
|
@@ -201,30 +201,26 @@ func (action *AttestationStatus) Run(ctx context.Context, attestationID string, | |
| } | ||
|
|
||
| // getPolicyEvaluations retrieves both material-level and attestation-level policy evaluations | ||
| func (action *AttestationStatus) getPolicyEvaluations(ctx context.Context, c *crafter.Crafter, statement *intoto.Statement) (map[string][]*PolicyEvaluation, error) { | ||
| func (action *AttestationStatus) getPolicyEvaluations(ctx context.Context, c *crafter.Crafter, attestationID string, statement *intoto.Statement) (map[string][]*PolicyEvaluation, error) { | ||
| // grouped by material name | ||
| evaluations := make(map[string][]*PolicyEvaluation) | ||
|
|
||
| // Add material-level policy evaluations | ||
| for _, v := range c.CraftingState.Attestation.GetPolicyEvaluations() { | ||
| if existing, ok := evaluations[v.MaterialName]; ok { | ||
| evaluations[v.MaterialName] = append(existing, policyEvaluationStateToActionForStatus(v)) | ||
| } else { | ||
| evaluations[v.MaterialName] = []*PolicyEvaluation{policyEvaluationStateToActionForStatus(v)} | ||
| } | ||
| } | ||
|
|
||
| // Add attestation-level policy evaluations | ||
| attestationEvaluations, err := c.EvaluateAttestationPolicies(ctx, statement) | ||
| if err != nil { | ||
| if err := c.EvaluateAttestationPolicies(ctx, attestationID, statement); err != nil { | ||
| return nil, fmt.Errorf("evaluating attestation policies: %w", err) | ||
| } | ||
|
|
||
| for _, v := range attestationEvaluations { | ||
| if existing, ok := evaluations[chainloop.AttPolicyEvaluation]; ok { | ||
| evaluations[chainloop.AttPolicyEvaluation] = append(existing, policyEvaluationStateToActionForStatus(v)) | ||
| // map evaluations | ||
| for _, v := range c.CraftingState.Attestation.GetPolicyEvaluations() { | ||
| keyName := v.MaterialName | ||
| if keyName == "" { | ||
| keyName = chainloop.AttPolicyEvaluation | ||
| } | ||
|
|
||
| if existing, ok := evaluations[keyName]; ok { | ||
| evaluations[keyName] = append(existing, policyEvaluationStateToActionForStatus(v)) | ||
| } else { | ||
| evaluations[chainloop.AttPolicyEvaluation] = []*PolicyEvaluation{policyEvaluationStateToActionForStatus(v)} | ||
| evaluations[keyName] = []*PolicyEvaluation{policyEvaluationStateToActionForStatus(v)} | ||
| } | ||
| } | ||
|
|
||
|
|
||
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 |
|---|---|---|
|
|
@@ -27,7 +27,6 @@ import ( | |
| pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1" | ||
| schemaapi "github.com/chainloop-dev/chainloop/app/controlplane/api/workflowcontract/v1" | ||
| v1 "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/api/attestation/v1" | ||
| "github.com/chainloop-dev/chainloop/pkg/policies" | ||
| crv1 "github.com/google/go-containerregistry/pkg/v1" | ||
| intoto "github.com/in-toto/attestation/go/v1" | ||
| "github.com/rs/zerolog" | ||
|
|
@@ -86,25 +85,7 @@ func NewChainloopRendererV02(att *v1.Attestation, schema *schemaapi.CraftingSche | |
| } | ||
| } | ||
|
|
||
| type RenderOptions struct { | ||
| evaluatePolicies bool | ||
| } | ||
|
|
||
| type RenderOpt func(*RenderOptions) | ||
|
|
||
| func WithSkipPolicyEvaluation(skip bool) RenderOpt { | ||
| return func(o *RenderOptions) { | ||
| o.evaluatePolicies = !skip | ||
| } | ||
| } | ||
|
|
||
| func (r *RendererV02) Statement(ctx context.Context, opts ...RenderOpt) (*intoto.Statement, error) { | ||
| var evaluations []*v1.PolicyEvaluation | ||
| options := &RenderOptions{evaluatePolicies: true} | ||
| for _, opt := range opts { | ||
| opt(options) | ||
| } | ||
|
|
||
| func (r *RendererV02) Statement(_ context.Context) (*intoto.Statement, error) { | ||
| subject, err := r.subject() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error creating subject: %w", err) | ||
|
|
@@ -122,88 +103,9 @@ func (r *RendererV02) Statement(ctx context.Context, opts ...RenderOpt) (*intoto | |
| Predicate: predicate, | ||
| } | ||
|
|
||
| if options.evaluatePolicies { | ||
| // Validate policy groups | ||
| pgv := policies.NewPolicyGroupVerifier(r.schema, r.attClient, r.logger) | ||
| policyGroupResults, err := pgv.VerifyStatement(ctx, statement) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error applying policy groups to statement: %w", err) | ||
| } | ||
| evaluations = append(evaluations, policyGroupResults...) | ||
|
|
||
| // validate attestation-level policies | ||
| pv := policies.NewPolicyVerifier(r.schema, r.attClient, r.logger) | ||
| policyResults, err := pv.VerifyStatement(ctx, statement) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("applying policies to statement: %w", err) | ||
| } | ||
| evaluations = append(evaluations, policyResults...) | ||
| // log policy violations | ||
| policies.LogPolicyEvaluations(evaluations, r.logger) | ||
|
|
||
| // insert attestation level policy results into statement | ||
| if err = addPolicyResults(statement, evaluations); err != nil { | ||
| return nil, fmt.Errorf("adding policy results to statement: %w", err) | ||
| } | ||
| } | ||
|
|
||
| return statement, nil | ||
| } | ||
|
|
||
| // addPolicyResults adds policy evaluation results to the statement. It does it by deserializing the predicate from a structpb.Struct, | ||
| // filling PolicyEvaluations, and serializing it again to a structpb.Struct object, using JSON as an intermediate representation. | ||
| // Note that this is needed because intoto predicates are generic structpb.Struct | ||
| func addPolicyResults(statement *intoto.Statement, policyResults []*v1.PolicyEvaluation) error { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I love this change. |
||
| if len(policyResults) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| predicate := statement.Predicate | ||
| // marshall to json | ||
| jsonPredicate, err := protojson.Marshal(predicate) | ||
| if err != nil { | ||
| return fmt.Errorf("marshalling predicate: %w", err) | ||
| } | ||
|
|
||
| // unmarshall to our typed predicate object | ||
| var p ProvenancePredicateV02 | ||
| err = json.Unmarshal(jsonPredicate, &p) | ||
| if err != nil { | ||
| return fmt.Errorf("unmarshalling predicate: %w", err) | ||
| } | ||
|
|
||
| // insert policy evaluations for attestation | ||
| if p.PolicyEvaluations == nil { | ||
| p.PolicyEvaluations = make(map[string][]*PolicyEvaluation) | ||
| } | ||
| attEvaluations := make([]*PolicyEvaluation, 0, len(policyResults)) | ||
| for _, ev := range policyResults { | ||
| renderedEv, err := renderEvaluation(ev) | ||
| if err != nil { | ||
| return fmt.Errorf("rendering evaluation: %w", err) | ||
| } | ||
| attEvaluations = append(attEvaluations, renderedEv) | ||
| } | ||
| p.PolicyEvaluations[AttPolicyEvaluation] = attEvaluations | ||
|
|
||
| // marshall back to JSON | ||
| jsonPredicate, err = json.Marshal(p) | ||
| if err != nil { | ||
| return fmt.Errorf("marshalling predicate: %w", err) | ||
| } | ||
|
|
||
| // finally unmarshal from JSON to structpb.Struct. | ||
| var finalPredicate structpb.Struct | ||
| err = protojson.Unmarshal(jsonPredicate, &finalPredicate) | ||
| if err != nil { | ||
| return fmt.Errorf("unmarshalling predicate: %w", err) | ||
| } | ||
|
|
||
| statement.Predicate = &finalPredicate | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func commitAnnotations(c *v1.Commit) (*structpb.Struct, error) { | ||
| annotationsRaw := map[string]interface{}{ | ||
| subjectGitAnnotationWhen: c.GetDate().AsTime().Format(time.RFC3339), | ||
|
|
@@ -285,7 +187,7 @@ func (r *RendererV02) predicate() (*structpb.Struct, error) { | |
| return nil, fmt.Errorf("error normalizing materials: %w", err) | ||
| } | ||
|
|
||
| policies, err := policyEvaluationsFromMaterials(r.att) | ||
| policies, err := mappedPolicyEvaluations(r.att) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error rendering policy evaluations: %w", err) | ||
| } | ||
|
|
@@ -313,14 +215,19 @@ func (r *RendererV02) predicate() (*structpb.Struct, error) { | |
| } | ||
|
|
||
| // collect all policy evaluations grouped by material | ||
| func policyEvaluationsFromMaterials(att *v1.Attestation) (map[string][]*PolicyEvaluation, error) { | ||
| func mappedPolicyEvaluations(att *v1.Attestation) (map[string][]*PolicyEvaluation, error) { | ||
| result := map[string][]*PolicyEvaluation{} | ||
| for _, p := range att.GetPolicyEvaluations() { | ||
| keyName := p.MaterialName | ||
| if keyName == "" { | ||
| keyName = AttPolicyEvaluation | ||
| } | ||
|
|
||
| ev, err := renderEvaluation(p) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| result[p.MaterialName] = append(result[p.MaterialName], ev) | ||
| result[keyName] = append(result[keyName], ev) | ||
| } | ||
|
|
||
| return result, nil | ||
|
|
||
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.
I thought about also running it on att.add but I am not sure it's needed yet.
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.
Probably not, since we want to block only on "push"