@@ -38,6 +38,18 @@ import (
3838 "github.com/chainloop-dev/chainloop/pkg/policies/engine/rego"
3939)
4040
41+ type PolicyError struct {
42+ err error
43+ }
44+
45+ func NewPolicyError (err error ) * PolicyError {
46+ return & PolicyError {err : err }
47+ }
48+
49+ func (e * PolicyError ) Error () string {
50+ return fmt .Sprintf ("policy error: %s" , e .err .Error ())
51+ }
52+
4153type PolicyVerifier struct {
4254 schema * v1.CraftingSchema
4355 logger * zerolog.Logger
@@ -53,26 +65,26 @@ func (pv *PolicyVerifier) VerifyMaterial(ctx context.Context, material *v12.Atte
5365
5466 policies , err := pv .requiredPoliciesForMaterial (material )
5567 if err != nil {
56- return nil , fmt . Errorf ( "error getting required policies for material: %w" , err )
68+ return nil , NewPolicyError ( err )
5769 }
5870
5971 for _ , policy := range policies {
6072 // 1. load the policy spec
6173 spec , err := LoadPolicySpec (policy )
6274 if err != nil {
63- return nil , fmt . Errorf ( "failed to load policy spec: %w" , err )
75+ return nil , NewPolicyError ( err )
6476 }
6577
6678 // load the policy script (rego)
6779 script , err := LoadPolicyScriptFromSpec (spec )
6880 if err != nil {
69- return nil , fmt . Errorf ( "failed to load policy content: %w" , err )
81+ return nil , NewPolicyError ( err )
7082 }
7183
7284 // Load material content
7385 subject , err := getMaterialContent (material , artifactPath )
7486 if err != nil {
75- return nil , fmt . Errorf ( "failed to load material content: %w" , err )
87+ return nil , NewPolicyError ( err )
7688 }
7789
7890 pv .logger .Info ().Msgf ("evaluating policy '%s' against material '%s'" , spec .Metadata .Name , material .GetArtifact ().GetId ())
@@ -81,7 +93,7 @@ func (pv *PolicyVerifier) VerifyMaterial(ctx context.Context, material *v12.Atte
8193 ng := getPolicyEngine (spec )
8294 violations , err := ng .Verify (ctx , script , subject )
8395 if err != nil {
84- return nil , fmt . Errorf ( "failed to verify policy: %w" , err )
96+ return nil , NewPolicyError ( err )
8597 }
8698
8799 result = append (result , & v12.PolicyEvaluation {
@@ -103,7 +115,7 @@ func (pv *PolicyVerifier) VerifyStatement(ctx context.Context, statement *intoto
103115 // 1. load the policy spec
104116 spec , err := LoadPolicySpec (policyAtt )
105117 if err != nil {
106- return nil , fmt . Errorf ( "failed to load policy spec: %w" , err )
118+ return nil , NewPolicyError ( err )
107119 }
108120
109121 // it's expected statements can only be validated by policy of type ATTESTATION
@@ -114,21 +126,21 @@ func (pv *PolicyVerifier) VerifyStatement(ctx context.Context, statement *intoto
114126 // 2. load the policy script (rego)
115127 script , err := LoadPolicyScriptFromSpec (spec )
116128 if err != nil {
117- return nil , fmt . Errorf ( "failed to load policy content: %w" , err )
129+ return nil , NewPolicyError ( err )
118130 }
119131
120132 pv .logger .Info ().Msgf ("evaluating policy '%s' on attestation" , spec .Metadata .Name )
121133
122134 material , err := protojson .Marshal (statement )
123135 if err != nil {
124- return nil , fmt . Errorf ( "failed to load material content: %w" , err )
136+ return nil , NewPolicyError ( err )
125137 }
126138
127139 // 4. verify the policy
128140 ng := getPolicyEngine (spec )
129141 res , err := ng .Verify (ctx , script , material )
130142 if err != nil {
131- return nil , fmt . Errorf ( "failed to verify policy: %w" , err )
143+ return nil , NewPolicyError ( err )
132144 }
133145
134146 // 5. Store result in the attestation itself (for the renderer to include them in the predicate)
0 commit comments