MCPcopy Create free account
hub / github.com/chainloop-dev/chainloop / VerifyMaterial

Method VerifyMaterial

pkg/policies/policies.go:241–297  ·  view source on GitHub ↗

VerifyMaterial applies all required policies to a material

(ctx context.Context, material *v12.Attestation_Material, artifactPath string)

Source from the content-addressed store, hash-verified

239
240// VerifyMaterial applies all required policies to a material
241func (pv *PolicyVerifier) VerifyMaterial(ctx context.Context, material *v12.Attestation_Material, artifactPath string) ([]*v12.PolicyEvaluation, error) {
242 result := make([]*v12.PolicyEvaluation, 0)
243
244 attachments, err := pv.requiredPoliciesForMaterial(ctx, material)
245 if err != nil {
246 return nil, NewPolicyError(err)
247 }
248
249 if len(attachments) == 0 {
250 return result, nil
251 }
252
253 // Load material content
254 subject, err := material.GetEvaluableContent(artifactPath)
255 if err != nil {
256 return nil, NewPolicyError(err)
257 }
258
259 // Track which scoped runtime inputs matched a policy so we can warn about
260 // scopes that matched nothing (e.g. a typo in the policy name).
261 tracker := newScopeTracker()
262
263 results := make([]*v12.PolicyEvaluation, len(attachments))
264 g, gCtx := errgroup.WithContext(ctx)
265 g.SetLimit(pv.maxConcurrency)
266
267 for i, attachment := range attachments {
268 g.Go(func() error {
269 ev, err := pv.evaluatePolicyAttachment(gCtx, attachment, subject,
270 &evalOpts{kind: material.MaterialType, name: material.GetId(), runtimeInputs: pv.runtimeInputs, scopeTracker: tracker},
271 )
272 if err != nil {
273 return NewPolicyError(err)
274 }
275 results[i] = ev
276 return nil
277 })
278 }
279
280 if err := g.Wait(); err != nil {
281 return nil, err
282 }
283
284 for _, scope := range tracker.unmatched(pv.runtimeInputs) {
285 pv.logger.Warn().Str("scope", scope).Str("material", material.GetId()).
286 Msg("policy input scoped to a policy that matched no attachment for this material — check the policy name")
287 }
288
289 // Filter nil entries (skipped policies)
290 for _, ev := range results {
291 if ev != nil {
292 result = append(result, ev)
293 }
294 }
295
296 return result, nil
297}
298

Calls 7

NewPolicyErrorFunction · 0.85
newScopeTrackerFunction · 0.85
GetEvaluableContentMethod · 0.80
unmatchedMethod · 0.80
GetIdMethod · 0.45