@@ -27,7 +27,6 @@ import (
2727 pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1"
2828 schemaapi "github.com/chainloop-dev/chainloop/app/controlplane/api/workflowcontract/v1"
2929 v1 "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/api/attestation/v1"
30- "github.com/chainloop-dev/chainloop/pkg/policies"
3130 crv1 "github.com/google/go-containerregistry/pkg/v1"
3231 intoto "github.com/in-toto/attestation/go/v1"
3332 "github.com/rs/zerolog"
@@ -86,25 +85,7 @@ func NewChainloopRendererV02(att *v1.Attestation, schema *schemaapi.CraftingSche
8685 }
8786}
8887
89- type RenderOptions struct {
90- evaluatePolicies bool
91- }
92-
93- type RenderOpt func (* RenderOptions )
94-
95- func WithSkipPolicyEvaluation (skip bool ) RenderOpt {
96- return func (o * RenderOptions ) {
97- o .evaluatePolicies = ! skip
98- }
99- }
100-
101- func (r * RendererV02 ) Statement (ctx context.Context , opts ... RenderOpt ) (* intoto.Statement , error ) {
102- var evaluations []* v1.PolicyEvaluation
103- options := & RenderOptions {evaluatePolicies : true }
104- for _ , opt := range opts {
105- opt (options )
106- }
107-
88+ func (r * RendererV02 ) Statement (_ context.Context ) (* intoto.Statement , error ) {
10889 subject , err := r .subject ()
10990 if err != nil {
11091 return nil , fmt .Errorf ("error creating subject: %w" , err )
@@ -122,88 +103,9 @@ func (r *RendererV02) Statement(ctx context.Context, opts ...RenderOpt) (*intoto
122103 Predicate : predicate ,
123104 }
124105
125- if options .evaluatePolicies {
126- // Validate policy groups
127- pgv := policies .NewPolicyGroupVerifier (r .schema , r .attClient , r .logger )
128- policyGroupResults , err := pgv .VerifyStatement (ctx , statement )
129- if err != nil {
130- return nil , fmt .Errorf ("error applying policy groups to statement: %w" , err )
131- }
132- evaluations = append (evaluations , policyGroupResults ... )
133-
134- // validate attestation-level policies
135- pv := policies .NewPolicyVerifier (r .schema , r .attClient , r .logger )
136- policyResults , err := pv .VerifyStatement (ctx , statement )
137- if err != nil {
138- return nil , fmt .Errorf ("applying policies to statement: %w" , err )
139- }
140- evaluations = append (evaluations , policyResults ... )
141- // log policy violations
142- policies .LogPolicyEvaluations (evaluations , r .logger )
143-
144- // insert attestation level policy results into statement
145- if err = addPolicyResults (statement , evaluations ); err != nil {
146- return nil , fmt .Errorf ("adding policy results to statement: %w" , err )
147- }
148- }
149-
150106 return statement , nil
151107}
152108
153- // addPolicyResults adds policy evaluation results to the statement. It does it by deserializing the predicate from a structpb.Struct,
154- // filling PolicyEvaluations, and serializing it again to a structpb.Struct object, using JSON as an intermediate representation.
155- // Note that this is needed because intoto predicates are generic structpb.Struct
156- func addPolicyResults (statement * intoto.Statement , policyResults []* v1.PolicyEvaluation ) error {
157- if len (policyResults ) == 0 {
158- return nil
159- }
160-
161- predicate := statement .Predicate
162- // marshall to json
163- jsonPredicate , err := protojson .Marshal (predicate )
164- if err != nil {
165- return fmt .Errorf ("marshalling predicate: %w" , err )
166- }
167-
168- // unmarshall to our typed predicate object
169- var p ProvenancePredicateV02
170- err = json .Unmarshal (jsonPredicate , & p )
171- if err != nil {
172- return fmt .Errorf ("unmarshalling predicate: %w" , err )
173- }
174-
175- // insert policy evaluations for attestation
176- if p .PolicyEvaluations == nil {
177- p .PolicyEvaluations = make (map [string ][]* PolicyEvaluation )
178- }
179- attEvaluations := make ([]* PolicyEvaluation , 0 , len (policyResults ))
180- for _ , ev := range policyResults {
181- renderedEv , err := renderEvaluation (ev )
182- if err != nil {
183- return fmt .Errorf ("rendering evaluation: %w" , err )
184- }
185- attEvaluations = append (attEvaluations , renderedEv )
186- }
187- p .PolicyEvaluations [AttPolicyEvaluation ] = attEvaluations
188-
189- // marshall back to JSON
190- jsonPredicate , err = json .Marshal (p )
191- if err != nil {
192- return fmt .Errorf ("marshalling predicate: %w" , err )
193- }
194-
195- // finally unmarshal from JSON to structpb.Struct.
196- var finalPredicate structpb.Struct
197- err = protojson .Unmarshal (jsonPredicate , & finalPredicate )
198- if err != nil {
199- return fmt .Errorf ("unmarshalling predicate: %w" , err )
200- }
201-
202- statement .Predicate = & finalPredicate
203-
204- return nil
205- }
206-
207109func commitAnnotations (c * v1.Commit ) (* structpb.Struct , error ) {
208110 annotationsRaw := map [string ]interface {}{
209111 subjectGitAnnotationWhen : c .GetDate ().AsTime ().Format (time .RFC3339 ),
@@ -285,7 +187,7 @@ func (r *RendererV02) predicate() (*structpb.Struct, error) {
285187 return nil , fmt .Errorf ("error normalizing materials: %w" , err )
286188 }
287189
288- policies , err := policyEvaluationsFromMaterials (r .att )
190+ policies , err := mappedPolicyEvaluations (r .att )
289191 if err != nil {
290192 return nil , fmt .Errorf ("error rendering policy evaluations: %w" , err )
291193 }
@@ -313,14 +215,19 @@ func (r *RendererV02) predicate() (*structpb.Struct, error) {
313215}
314216
315217// collect all policy evaluations grouped by material
316- func policyEvaluationsFromMaterials (att * v1.Attestation ) (map [string ][]* PolicyEvaluation , error ) {
218+ func mappedPolicyEvaluations (att * v1.Attestation ) (map [string ][]* PolicyEvaluation , error ) {
317219 result := map [string ][]* PolicyEvaluation {}
318220 for _ , p := range att .GetPolicyEvaluations () {
221+ keyName := p .MaterialName
222+ if keyName == "" {
223+ keyName = AttPolicyEvaluation
224+ }
225+
319226 ev , err := renderEvaluation (p )
320227 if err != nil {
321228 return nil , err
322229 }
323- result [p . MaterialName ] = append (result [p . MaterialName ], ev )
230+ result [keyName ] = append (result [keyName ], ev )
324231 }
325232
326233 return result , nil
0 commit comments