Skip to content

Commit 46ae5ba

Browse files
committed
refactor
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
1 parent 5d84403 commit 46ae5ba

3 files changed

Lines changed: 14 additions & 15 deletions

File tree

app/cli/pkg/action/action.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,34 +101,32 @@ func newCrafter(stateOpts *newCrafterStateOpts, conn *grpc.ClientConn, opts ...c
101101
}
102102

103103
// getCASBackend tries to get CAS upload credentials and set up a CAS client
104-
func getCASBackend(ctx context.Context, client pb.AttestationServiceClient, workflowRunID, casCAPath, casURI string, casConnectionInsecure bool, logger zerolog.Logger, casBackend *casclient.CASBackend, casBackendInfo **clientAPI.Attestation_CASBackend) (func() error, error) {
104+
func getCASBackend(ctx context.Context, client pb.AttestationServiceClient, workflowRunID, casCAPath, casURI string, casConnectionInsecure bool, logger zerolog.Logger, casBackend *casclient.CASBackend) (*clientAPI.Attestation_CASBackend, func() error, error) {
105105
credsResp, err := client.GetUploadCreds(ctx, &pb.AttestationServiceGetUploadCredsRequest{
106106
WorkflowRunId: workflowRunID,
107107
})
108108
if err != nil {
109109
// Log warning but don't fail - will fall back to inline storage
110110
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)
111+
return nil, nil, fmt.Errorf("getting upload creds: %w", err)
112112
}
113113

114114
if credsResp == nil || credsResp.GetResult() == nil {
115115
logger.Debug().Msg("no upload creds result, will store inline")
116-
return nil, fmt.Errorf("getting upload creds: %w", err)
116+
return nil, nil, fmt.Errorf("getting upload creds: %w", err)
117117
}
118118

119119
result := credsResp.GetResult()
120120
backend := result.GetBackend()
121121
if backend == nil {
122122
logger.Debug().Msg("no backend info in upload creds, will store inline")
123-
return nil, fmt.Errorf("no backend found in upload creds")
123+
return nil, nil, fmt.Errorf("no backend found in upload creds")
124124
}
125125

126-
if casBackendInfo != nil {
127-
*casBackendInfo = &clientAPI.Attestation_CASBackend{
128-
CasBackendId: backend.Id,
129-
CasBackendName: backend.Name,
130-
Fallback: backend.Fallback,
131-
}
126+
casBackendInfo := &clientAPI.Attestation_CASBackend{
127+
CasBackendId: backend.Id,
128+
CasBackendName: backend.Name,
129+
Fallback: backend.Fallback,
132130
}
133131

134132
casBackend.Name = backend.Provider
@@ -138,7 +136,7 @@ func getCASBackend(ctx context.Context, client pb.AttestationServiceClient, work
138136

139137
// Only attempt to create a CAS connection when not inline and token is present
140138
if backend.IsInline || result.Token == "" {
141-
return nil, nil
139+
return casBackendInfo, nil, nil
142140
}
143141

144142
opts := []grpcconn.Option{grpcconn.WithInsecure(casConnectionInsecure)}
@@ -149,9 +147,9 @@ func getCASBackend(ctx context.Context, client pb.AttestationServiceClient, work
149147
artifactCASConn, err := grpcconn.New(casURI, result.Token, opts...)
150148
if err != nil {
151149
logger.Warn().Err(err).Msg("failed to create CAS connection, will store inline")
152-
return nil, fmt.Errorf("creating CAS connection: %w", err)
150+
return nil, nil, fmt.Errorf("creating CAS connection: %w", err)
153151
}
154152

155153
casBackend.Uploader = casclient.New(artifactCASConn, casclient.WithLogger(logger))
156-
return artifactCASConn.Close, nil
154+
return casBackendInfo, artifactCASConn.Close, nil
157155
}

app/cli/pkg/action/attestation_add.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (action *AttestationAdd) Run(ctx context.Context, attestationID, materialNa
9999
if !crafter.CraftingState.GetDryRun() {
100100
client := pb.NewAttestationServiceClient(action.CPConnection)
101101
workflowRunID := crafter.CraftingState.GetAttestation().GetWorkflow().GetWorkflowRunId()
102-
connectionCloserFn, getCASBackendErr := getCASBackend(ctx, client, workflowRunID, action.casCAPath, action.casURI, action.connectionInsecure, action.Logger, casBackend, nil)
102+
_, connectionCloserFn, getCASBackendErr := getCASBackend(ctx, client, workflowRunID, action.casCAPath, action.casURI, action.connectionInsecure, action.Logger, casBackend)
103103
if getCASBackendErr != nil {
104104
return nil, fmt.Errorf("failed to get CAS backend: %w", getCASBackendErr)
105105
}

app/cli/pkg/action/attestation_init.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ func (action *AttestationInit) Run(ctx context.Context, opts *AttestationInitRun
233233
var casBackend = &casclient.CASBackend{Name: "not-set"}
234234
var casBackendInfo *clientAPI.Attestation_CASBackend
235235
if !action.dryRun && attestationID != "" {
236-
connectionCloserFn, err := getCASBackend(ctx, client, attestationID, action.casCAPath, action.casURI, action.connectionInsecure, action.Logger, casBackend, &casBackendInfo)
236+
var connectionCloserFn func() error
237+
casBackendInfo, connectionCloserFn, err = getCASBackend(ctx, client, attestationID, action.casCAPath, action.casURI, action.connectionInsecure, action.Logger, casBackend)
237238
if err != nil {
238239
// We don't want to fail the attestation initialization if CAS setup fails, it's a best-effort feature for PR/MR metadata
239240
action.Logger.Warn().Err(err).Msg("unexpected error getting CAS backend")

0 commit comments

Comments
 (0)