1616package action
1717
1818import (
19+ "context"
1920 "fmt"
2021 "os"
2122 "path/filepath"
@@ -25,6 +26,9 @@ import (
2526 "github.com/chainloop-dev/chainloop/pkg/attestation/crafter"
2627 "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/statemanager/filesystem"
2728 "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/statemanager/remote"
29+ "github.com/chainloop-dev/chainloop/pkg/casclient"
30+ "github.com/chainloop-dev/chainloop/pkg/grpcconn"
31+
2832 "github.com/rs/zerolog"
2933 "google.golang.org/grpc"
3034)
@@ -94,3 +98,52 @@ func newCrafter(stateOpts *newCrafterStateOpts, conn *grpc.ClientConn, opts ...c
9498
9599 return crafter .NewCrafter (stateManager , attClient , opts ... )
96100}
101+
102+ // getCASBackend tries to get CAS upload credentials and set up a CAS client
103+ // If strict is true, errors are returned; if false, warnings are logged and nil error is returned
104+ func getCASBackend (ctx context.Context , client pb.AttestationServiceClient , workflowRunID , casCAPath , casURI string , casConnectionInsecure bool , logger zerolog.Logger , casBackend * casclient.CASBackend ) (func () error , error ) {
105+ credsResp , err := client .GetUploadCreds (ctx , & pb.AttestationServiceGetUploadCredsRequest {
106+ WorkflowRunId : workflowRunID ,
107+ })
108+ if err != nil {
109+ // Log warning but don't fail - will fall back to inline storage
110+ logger .Warn ().Err (err ).Msg ("failed to get CAS credentials for PR metadata, will store inline" )
111+ return nil , fmt .Errorf ("getting upload creds: %w" , err )
112+ }
113+
114+ if credsResp == nil || credsResp .GetResult () == nil {
115+ logger .Debug ().Msg ("no upload creds result, will store inline" )
116+ return nil , fmt .Errorf ("getting upload creds: %w" , err )
117+ }
118+
119+ result := credsResp .GetResult ()
120+ backend := result .GetBackend ()
121+ if backend == nil {
122+ logger .Debug ().Msg ("no backend info in upload creds, will store inline" )
123+ return nil , fmt .Errorf ("no backend found in upload creds" )
124+ }
125+
126+ casBackend .Name = backend .Provider
127+ if backend .GetLimits () != nil {
128+ casBackend .MaxSize = backend .GetLimits ().MaxBytes
129+ }
130+
131+ // Only attempt to create a CAS connection when not inline and token is present
132+ if backend .IsInline || result .Token == "" {
133+ return nil , nil
134+ }
135+
136+ opts := []grpcconn.Option {grpcconn .WithInsecure (casConnectionInsecure )}
137+ if casCAPath != "" {
138+ opts = append (opts , grpcconn .WithCAFile (casCAPath ))
139+ }
140+
141+ artifactCASConn , err := grpcconn .New (casURI , result .Token , opts ... )
142+ if err != nil {
143+ logger .Warn ().Err (err ).Msg ("failed to create CAS connection, will store inline" )
144+ return nil , fmt .Errorf ("creating CAS connection: %w" , err )
145+ }
146+
147+ casBackend .Uploader = casclient .New (artifactCASConn , casclient .WithLogger (logger ))
148+ return artifactCASConn .Close , nil
149+ }
0 commit comments