@@ -22,18 +22,21 @@ import (
2222 "errors"
2323 "fmt"
2424 "sort"
25+ "strings"
2526
2627 pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1"
2728 "github.com/chainloop-dev/chainloop/pkg/attestation/renderer/chainloop"
29+ "github.com/chainloop-dev/chainloop/pkg/attestation/verifier"
30+ intoto "github.com/in-toto/attestation/go/v1"
31+ "github.com/secure-systems-lab/go-securesystemslib/dsse"
2832 "github.com/sigstore/cosign/v2/pkg/blob"
2933 "github.com/sigstore/cosign/v2/pkg/cosign"
3034 sigs "github.com/sigstore/cosign/v2/pkg/signature"
3135 "github.com/sigstore/sigstore/pkg/cryptoutils"
3236 "github.com/sigstore/sigstore/pkg/signature"
33-
34- intoto "github.com/in-toto/attestation/go/v1"
35- "github.com/secure-systems-lab/go-securesystemslib/dsse"
3637 sigdsee "github.com/sigstore/sigstore/pkg/signature/dsse"
38+ "google.golang.org/grpc/codes"
39+ "google.golang.org/grpc/status"
3740)
3841
3942type WorkflowRunDescribe struct {
@@ -157,17 +160,43 @@ func (action *WorkflowRunDescribe) Run(ctx context.Context, opts *WorkflowRunDes
157160 item .WorkflowRun .FinishedAt = toTimePtr (wr .FinishedAt .AsTime ())
158161 }
159162
160- attestation := resp .GetResult ().GetAttestation ()
163+ att := resp .GetResult ().GetAttestation ()
161164 // The item does not have associated attestation
162- if attestation == nil {
165+ if att == nil {
163166 return item , nil
164167 }
165168
166- envelope , err := decodeEnvelope (attestation .Envelope )
169+ envelope , err := decodeEnvelope (att .Envelope )
167170 if err != nil {
168171 return nil , err
169172 }
170173
174+ if att .Bundle != nil {
175+ sc := pb .NewSigningServiceClient (action .cfg .CPConnection )
176+ trResp , err := sc .GetTrustedRoot (ctx , & pb.GetTrustedRootRequest {})
177+ if err != nil {
178+ // if trusted root is not implemented, skip verification
179+ if status .Code (err ) != codes .Unimplemented {
180+ return nil , fmt .Errorf ("failed getting trusted root: %w" , err )
181+ }
182+ }
183+
184+ if trResp != nil {
185+ tr , err := trustedRootPbToVerifier (trResp )
186+ if err != nil {
187+ return nil , fmt .Errorf ("getting roots: %w" , err )
188+ }
189+ if err = verifier .VerifyBundle (ctx , att .Bundle , tr ); err != nil {
190+ if ! errors .Is (err , verifier .ErrMissingVerificationMaterial ) {
191+ action .cfg .Logger .Debug ().Err (err ).Msg ("bundle verification failed" )
192+ return nil , errors .New ("bundle verification failed" )
193+ }
194+ } else {
195+ item .Verified = true
196+ }
197+ }
198+ }
199+
171200 if opts .Verify {
172201 if err := verifyEnvelope (ctx , envelope , opts ); err != nil {
173202 action .cfg .Logger .Debug ().Err (err ).Msg ("verifying the envelope" )
@@ -182,48 +211,48 @@ func (action *WorkflowRunDescribe) Run(ctx context.Context, opts *WorkflowRunDes
182211 return nil , fmt .Errorf ("extracting statement: %w" , err )
183212 }
184213
185- envVars := make ([]* EnvVar , 0 , len (attestation .GetEnvVars ()))
186- for _ , v := range attestation .GetEnvVars () {
214+ envVars := make ([]* EnvVar , 0 , len (att .GetEnvVars ()))
215+ for _ , v := range att .GetEnvVars () {
187216 envVars = append (envVars , & EnvVar {Name : v .Name , Value : v .Value })
188217 }
189218
190- materials := make ([]* Material , 0 , len (attestation .GetMaterials ()))
191- for _ , v := range attestation .GetMaterials () {
219+ materials := make ([]* Material , 0 , len (att .GetMaterials ()))
220+ for _ , v := range att .GetMaterials () {
192221 materials = append (materials , materialPBToAction (v ))
193222 }
194223
195- keys := make ([]string , 0 , len (attestation .GetAnnotations ()))
196- for k := range attestation .GetAnnotations () {
224+ keys := make ([]string , 0 , len (att .GetAnnotations ()))
225+ for k := range att .GetAnnotations () {
197226 keys = append (keys , k )
198227 }
199228 sort .Strings (keys )
200229
201- annotations := make ([]* Annotation , 0 , len (attestation .GetAnnotations ()))
230+ annotations := make ([]* Annotation , 0 , len (att .GetAnnotations ()))
202231 for _ , k := range keys {
203232 annotations = append (annotations , & Annotation {
204- Name : k , Value : attestation .GetAnnotations ()[k ],
233+ Name : k , Value : att .GetAnnotations ()[k ],
205234 })
206235 }
207236
208237 evaluations := make (map [string ][]* PolicyEvaluation )
209- for k , v := range attestation .GetPolicyEvaluations () {
238+ for k , v := range att .GetPolicyEvaluations () {
210239 evs := make ([]* PolicyEvaluation , 0 )
211240 for _ , ev := range v .Evaluations {
212241 evs = append (evs , policyEvaluationPBToAction (ev ))
213242 }
214243 evaluations [k ] = evs
215244 }
216245
217- policyEvaluationStatus := attestation .GetPolicyEvaluationStatus ()
246+ policyEvaluationStatus := att .GetPolicyEvaluationStatus ()
218247
219248 item .Attestation = & WorkflowRunAttestationItem {
220249 Envelope : envelope ,
221- Bundle : attestation .GetBundle (),
250+ Bundle : att .GetBundle (),
222251 statement : statement ,
223252 EnvVars : envVars ,
224253 Materials : materials ,
225254 Annotations : annotations ,
226- Digest : attestation .DigestInCasBackend ,
255+ Digest : att .DigestInCasBackend ,
227256 PolicyEvaluations : evaluations ,
228257 PolicyEvaluationStatus : & PolicyEvaluationStatus {
229258 Strategy : policyEvaluationStatus .Strategy ,
@@ -236,6 +265,20 @@ func (action *WorkflowRunDescribe) Run(ctx context.Context, opts *WorkflowRunDes
236265 return item , nil
237266}
238267
268+ func trustedRootPbToVerifier (resp * pb.GetTrustedRootResponse ) (* verifier.TrustedRoot , error ) {
269+ tr := & verifier.TrustedRoot {Keys : make (map [string ][]* x509.Certificate )}
270+ for k , v := range resp .GetKeys () {
271+ for _ , c := range v .Certificates {
272+ cert , err := cryptoutils .LoadCertificatesFromPEM (strings .NewReader (c ))
273+ if err != nil {
274+ return nil , fmt .Errorf ("loading certificate from PEM: %w" , err )
275+ }
276+ tr .Keys [k ] = append (tr .Keys [k ], cert [0 ])
277+ }
278+ }
279+ return tr , nil
280+ }
281+
239282func policyEvaluationPBToAction (in * pb.PolicyEvaluation ) * PolicyEvaluation {
240283 var pr * PolicyReference
241284 if in .PolicyReference != nil {
0 commit comments