Skip to content

Commit 9449f4f

Browse files
authored
fix(attestation): do not load cas-backend on dry-mode (#927)
Signed-off-by: Miguel Martinez Trivino <[email protected]>
1 parent 588375f commit 9449f4f

File tree

2 files changed

+42
-53
lines changed

2 files changed

+42
-53
lines changed

app/cli/internal/action/attestation_add.go

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -78,41 +78,45 @@ func (action *AttestationAdd) Run(ctx context.Context, attestationID, materialNa
7878
return err
7979
}
8080

81-
// Get upload creds and CASbackend for the current attestation and set up CAS client
82-
client := pb.NewAttestationServiceClient(action.CPConnection)
83-
creds, err := client.GetUploadCreds(ctx,
84-
&pb.AttestationServiceGetUploadCredsRequest{
85-
WorkflowRunId: action.c.CraftingState.GetAttestation().GetWorkflow().GetWorkflowRunId(),
86-
},
87-
)
88-
if err != nil {
89-
return err
90-
}
91-
92-
b := creds.GetResult().GetBackend()
93-
if b == nil {
94-
return fmt.Errorf("no backend found in upload creds")
95-
}
96-
97-
// Define CASbackend information based on the API response
81+
// Default to inline CASBackend and override if we are not in dry-run mode
9882
casBackend := &casclient.CASBackend{
99-
Name: b.Provider,
100-
MaxSize: b.GetLimits().MaxBytes,
83+
Name: "not-set",
10184
}
10285

103-
// Some CASBackends will actually upload information to the CAS server
104-
// in such case we need to set up a connection
105-
if !b.IsInline && creds.Result.Token != "" {
106-
artifactCASConn, err := grpcconn.New(action.casURI, creds.Result.Token, action.connectionInsecure)
86+
// Define CASbackend information based on the API response
87+
if !action.c.CraftingState.GetDryRun() {
88+
// Get upload creds and CASbackend for the current attestation and set up CAS client
89+
client := pb.NewAttestationServiceClient(action.CPConnection)
90+
creds, err := client.GetUploadCreds(ctx,
91+
&pb.AttestationServiceGetUploadCredsRequest{
92+
WorkflowRunId: action.c.CraftingState.GetAttestation().GetWorkflow().GetWorkflowRunId(),
93+
},
94+
)
10795
if err != nil {
10896
return err
10997
}
110-
defer artifactCASConn.Close()
111-
112-
casBackend.Uploader = casclient.New(artifactCASConn, casclient.WithLogger(action.Logger))
98+
b := creds.GetResult().GetBackend()
99+
if b == nil {
100+
return fmt.Errorf("no backend found in upload creds")
101+
}
102+
casBackend.Name = b.Provider
103+
casBackend.MaxSize = b.GetLimits().MaxBytes
104+
// Some CASBackends will actually upload information to the CAS server
105+
// in such case we need to set up a connection
106+
if !b.IsInline && creds.Result.Token != "" {
107+
artifactCASConn, err := grpcconn.New(action.casURI, creds.Result.Token, action.connectionInsecure)
108+
if err != nil {
109+
return err
110+
}
111+
defer artifactCASConn.Close()
112+
113+
casBackend.Uploader = casclient.New(artifactCASConn, casclient.WithLogger(action.Logger))
114+
}
113115
}
116+
114117
// Add material to the attestation crafting state based on if the material is contract free or not.
115118
// By default, try to detect the material kind automatically
119+
var err error
116120
switch {
117121
case materialName == "" && materialType == "":
118122
var kind schemaapi.CraftingSchema_Material_MaterialType

app/controlplane/internal/service/attestation.go

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -318,36 +318,21 @@ func (s *AttestationService) GetUploadCreds(ctx context.Context, req *cpAPI.Atte
318318

319319
// Find the CAS backend associated with this workflowRun, that's the one that will be used to upload the materials
320320
// NOTE: currently we only support one backend per workflowRun but this will change in the future
321+
// This is the new mode, where the CAS backend ref is stored in the workflow run since initialization
322+
wRun, err := s.wrUseCase.GetByIDInOrgOrPublic(ctx, robotAccount.OrgID, req.WorkflowRunId)
323+
if err != nil {
324+
return nil, handleUseCaseErr(err, s.log)
325+
} else if wRun == nil {
326+
return nil, errors.NotFound("not found", "workflow run not found")
327+
}
321328

322-
// DEPRECATED: if no workflow run is provided, we use the default repository
323-
// Maintained for compatibility reasons with older versions of the CLI
324-
var backend *biz.CASBackend
325-
if req.WorkflowRunId == "" {
326-
s.log.Warn("DEPRECATED: using main repository to get upload creds")
327-
backend, err := s.casUC.FindDefaultBackend(ctx, robotAccount.OrgID)
328-
if err != nil && !biz.IsNotFound(err) {
329-
return nil, handleUseCaseErr(err, s.log)
330-
} else if backend == nil {
331-
return nil, errors.NotFound("not found", "main repository not found")
332-
}
333-
} else {
334-
// This is the new mode, where the CAS backend ref is stored in the workflow run since initialization
335-
wRun, err := s.wrUseCase.GetByIDInOrgOrPublic(ctx, robotAccount.OrgID, req.WorkflowRunId)
336-
if err != nil {
337-
return nil, handleUseCaseErr(err, s.log)
338-
} else if wRun == nil {
339-
return nil, errors.NotFound("not found", "workflow run not found")
340-
}
341-
342-
if len(wRun.CASBackends) == 0 {
343-
return nil, errors.NotFound("not found", "workflow run has no CAS backend")
344-
}
345-
346-
s.log.Infow("msg", "generating upload credentials for CAS backend", "ID", wRun.CASBackends[0].ID, "name", wRun.CASBackends[0].Location, "workflowRun", req.WorkflowRunId)
347-
348-
backend = wRun.CASBackends[0]
329+
if len(wRun.CASBackends) == 0 {
330+
return nil, errors.NotFound("not found", "workflow run has no CAS backend")
349331
}
350332

333+
backend := wRun.CASBackends[0]
334+
s.log.Infow("msg", "generating upload credentials for CAS backend", "ID", wRun.CASBackends[0].ID, "name", wRun.CASBackends[0].Location, "workflowRun", req.WorkflowRunId)
335+
351336
// Return the backend information and associated credentials (if applicable)
352337
resp := &cpAPI.AttestationServiceGetUploadCredsResponse_Result{Backend: bizCASBackendToPb(backend)}
353338
if backend.SecretName != "" {

0 commit comments

Comments
 (0)