Skip to content

Commit e770fae

Browse files
authored
refactor: consolidate credentials service interface (#25)
--------- Signed-off-by: Miguel Martinez Trivino <[email protected]>
1 parent a027e86 commit e770fae

File tree

20 files changed

+248
-362
lines changed

20 files changed

+248
-362
lines changed

app/cli/cmd/workflow_contract_describe.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ func encodeContractOutput(run *action.WorkflowContractWithVersionItem) error {
7070

7171
switch flagOutputFormat {
7272
case formatContract:
73-
marshaller := protojson.MarshalOptions{Indent: " "}
74-
rawBody, err := marshaller.Marshal(run.Revision.BodyV1)
73+
marshaler := protojson.MarshalOptions{Indent: " "}
74+
rawBody, err := marshaler.Marshal(run.Revision.BodyV1)
7575
if err != nil {
7676
return err
7777
}
@@ -86,8 +86,8 @@ func encodeContractOutput(run *action.WorkflowContractWithVersionItem) error {
8686
func contractDescribeTableOutput(contractWithVersion *action.WorkflowContractWithVersionItem) error {
8787
revision := contractWithVersion.Revision
8888

89-
marshaller := protojson.MarshalOptions{Indent: " "}
90-
rawBody, err := marshaller.Marshal(revision.BodyV1)
89+
marshaler := protojson.MarshalOptions{Indent: " "}
90+
rawBody, err := marshaler.Marshal(revision.BodyV1)
9191
if err != nil {
9292
return err
9393
}

app/cli/internal/action/workflow_run_describe.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func (action *WorkflowRunDescribe) Run(runID string, verify bool, publicKey stri
122122
}
123123

124124
if err := json.Unmarshal(decodedPayload, statement); err != nil {
125-
return nil, fmt.Errorf("unmarshalling predicate: %w", err)
125+
return nil, fmt.Errorf("un-marshaling predicate: %w", err)
126126
}
127127

128128
var predicate *renderer.ChainloopProvenancePredicateV1
@@ -159,12 +159,12 @@ func (action *WorkflowRunDescribe) Run(runID string, verify bool, publicKey stri
159159
func extractPredicateV1(statement *in_toto.Statement) (*renderer.ChainloopProvenancePredicateV1, error) {
160160
jsonPredicate, err := json.Marshal(statement.Predicate)
161161
if err != nil {
162-
return nil, fmt.Errorf("unmarshalling predicate: %w", err)
162+
return nil, fmt.Errorf("un-marshaling predicate: %w", err)
163163
}
164164

165165
predicate := &renderer.ChainloopProvenancePredicateV1{}
166166
if err := json.Unmarshal(jsonPredicate, predicate); err != nil {
167-
return nil, fmt.Errorf("unmarshalling predicate: %w", err)
167+
return nil, fmt.Errorf("un-marshaling predicate: %w", err)
168168
}
169169

170170
return predicate, nil

app/controlplane/internal/biz/attestation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func doUploadToOCI(ctx context.Context, backend backend.Uploader, runID string,
8585
fileName := fmt.Sprintf("attestation-%s.json", runID)
8686
jsonContent, err := json.Marshal(envelope)
8787
if err != nil {
88-
return "", fmt.Errorf("marshalling the envelope: %w", err)
88+
return "", fmt.Errorf("marshaling the envelope: %w", err)
8989
}
9090

9191
hash := sha256.New()

app/controlplane/internal/biz/integration.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,14 @@ func (uc *IntegrationUseCase) AddDependencyTrack(ctx context.Context, orgID, hos
9797
return nil, NewErrInvalidUUID(err)
9898
}
9999

100+
// Validate Credentials before saving them
101+
creds := &credentials.APICreds{Host: host, Key: apiKey}
102+
if err := creds.Validate(); err != nil {
103+
return nil, newErrValidation(err)
104+
}
105+
100106
// Create the secret in the external secrets manager
101-
secretID, err := uc.credsRW.SaveAPICreds(ctx, orgID, &credentials.APICreds{Host: host, Key: apiKey})
107+
secretID, err := uc.credsRW.SaveCredentials(ctx, orgID, creds)
102108
if err != nil {
103109
return nil, fmt.Errorf("storing the credentials: %w", err)
104110
}
@@ -160,7 +166,7 @@ func (uc *IntegrationUseCase) Delete(ctx context.Context, orgID, integrationID s
160166

161167
if integration.SecretName != "" {
162168
uc.logger.Infow("msg", "deleting integration external secrets", "ID", integrationID, "secretName", integration.SecretName)
163-
if err := uc.credsRW.DeleteCreds(ctx, integration.SecretName); err != nil {
169+
if err := uc.credsRW.DeleteCredentials(ctx, integration.SecretName); err != nil {
164170
return fmt.Errorf("deleting the credentials: %w", err)
165171
}
166172
}
@@ -274,10 +280,14 @@ func validateAttachment(ctx context.Context, integration *Integration, credsR cr
274280

275281
// Check with the actual remote data that an upload would be possible
276282
creds := &credentials.APICreds{}
277-
if err := credsR.ReadAPICreds(ctx, integration.SecretName, creds); err != nil {
283+
if err := credsR.ReadCredentials(ctx, integration.SecretName, creds); err != nil {
278284
return err
279285
}
280286

287+
if err := creds.Validate(); err != nil {
288+
return newErrValidation(err)
289+
}
290+
281291
// Instantiate an actual uploader to see if it would work with the current configuration
282292
d, err := dependencytrack.NewSBOMUploader(c.DependencyTrack.GetDomain(), creds.Key,
283293
nil, ac.GetDependencyTrack().GetProjectId(), ac.GetDependencyTrack().GetProjectName())

app/controlplane/internal/biz/ocirepository.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,13 @@ func (uc *OCIRepositoryUseCase) CreateOrUpdate(ctx context.Context, orgID, repoU
113113
return nil, NewErrInvalidUUID(err)
114114
}
115115

116-
// Create the secret in the external secrets manager
117-
secretName, err := uc.credsRW.SaveOCICreds(ctx, orgID, &credentials.OCIKeypair{Repo: repoURL, Username: username, Password: password})
116+
// Validate and store the secret in the external secrets manager
117+
creds := &credentials.OCIKeypair{Repo: repoURL, Username: username, Password: password}
118+
if err := creds.Validate(); err != nil {
119+
return nil, newErrValidation(err)
120+
}
121+
122+
secretName, err := uc.credsRW.SaveCredentials(ctx, orgID, creds)
118123
if err != nil {
119124
return nil, fmt.Errorf("storing the credentials: %w", err)
120125
}
@@ -167,7 +172,7 @@ func (uc *OCIRepositoryUseCase) Delete(ctx context.Context, id string) error {
167172

168173
uc.logger.Infow("msg", "deleting OCI repository external secrets", "ID", id, "secretName", repo.SecretName)
169174
// Delete the secret in the external secrets manager
170-
if err := uc.credsRW.DeleteCreds(ctx, repo.SecretName); err != nil {
175+
if err := uc.credsRW.DeleteCredentials(ctx, repo.SecretName); err != nil {
171176
return fmt.Errorf("deleting the credentials: %w", err)
172177
}
173178

app/controlplane/internal/biz/ocirepository_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (s *ociRepositoryTestSuite) TestSaveMainRepoAlreadyExist() {
8585
r := &biz.OCIRepository{ID: s.validUUID.String()}
8686
ctx := context.Background()
8787
s.repo.On("FindMainRepo", ctx, s.validUUID).Return(r, nil)
88-
s.credsRW.On("SaveOCICreds", ctx, s.validUUID.String(), mock.Anything).Return("secret-key", nil)
88+
s.credsRW.On("SaveCredentials", ctx, s.validUUID.String(), mock.Anything).Return("secret-key", nil)
8989
s.repo.On("Update", ctx, &biz.OCIRepoUpdateOpts{
9090
ID: s.validUUID,
9191
OCIRepoOpts: &biz.OCIRepoOpts{
@@ -105,7 +105,7 @@ func (s *ociRepositoryTestSuite) TestSaveMainRepoOk() {
105105
const repo, username, password = "repo", "username", "pass"
106106

107107
s.repo.On("FindMainRepo", ctx, s.validUUID).Return(nil, nil)
108-
s.credsRW.On("SaveOCICreds", ctx, s.validUUID.String(), mock.Anything).Return("secret-key", nil)
108+
s.credsRW.On("SaveCredentials", ctx, s.validUUID.String(), mock.Anything).Return("secret-key", nil)
109109

110110
newRepo := &biz.OCIRepository{}
111111
s.repo.On("Create", ctx, &biz.OCIRepoCreateOpts{

app/controlplane/internal/biz/organization_integration_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ func (s *OrgIntegrationTestSuite) TestDeleteOrg() {
5353

5454
s.T().Run("org, integrations and repositories deletion", func(t *testing.T) {
5555
// Mock calls to credentials deletion for both the integration and the OCI repository
56-
s.mockedCredsReaderWriter.On("DeleteCreds", ctx, "stored-integration-secret").Return(nil)
57-
s.mockedCredsReaderWriter.On("DeleteCreds", ctx, "stored-OCI-secret").Return(nil)
56+
s.mockedCredsReaderWriter.On("DeleteCredentials", ctx, "stored-integration-secret").Return(nil)
57+
s.mockedCredsReaderWriter.On("DeleteCredentials", ctx, "stored-OCI-secret").Return(nil)
5858

5959
err := s.Organization.Delete(ctx, s.org.ID)
6060
assert.NoError(err)
@@ -102,12 +102,12 @@ func (s *OrgIntegrationTestSuite) SetupTest() {
102102

103103
// Dependency-track integration credentials
104104
s.mockedCredsReaderWriter.On(
105-
"SaveAPICreds", ctx, mock.Anything, &credentials.APICreds{Host: "host", Key: "key"},
105+
"SaveCredentials", ctx, mock.Anything, &credentials.APICreds{Host: "host", Key: "key"},
106106
).Return("stored-integration-secret", nil)
107107

108108
// OCI repository credentials
109109
s.mockedCredsReaderWriter.On(
110-
"SaveOCICreds", ctx, mock.Anything, &credentials.OCIKeypair{Repo: "repo", Username: "username", Password: "pass"},
110+
"SaveCredentials", ctx, mock.Anything, &credentials.OCIKeypair{Repo: "repo", Username: "username", Password: "pass"},
111111
).Return("stored-OCI-secret", nil)
112112

113113
s.TestingUseCases = testhelpers.NewTestingUseCases(t, testhelpers.WithCredsReaderWriter(s.mockedCredsReaderWriter))

app/controlplane/internal/service/attestation.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ func extractPredicate(envelope *dsse.Envelope) (*renderer.ChainloopProvenancePre
312312

313313
statement := &in_toto.Statement{}
314314
if err := json.Unmarshal(decodedPayload, statement); err != nil {
315-
return nil, fmt.Errorf("unmarshalling predicate: %w", err)
315+
return nil, fmt.Errorf("un-marshaling predicate: %w", err)
316316
}
317317

318318
var predicate *renderer.ChainloopProvenancePredicateV1
@@ -370,12 +370,12 @@ func extractMaterials(in []*renderer.ChainloopProvenanceMaterial) []*cpAPI.Attes
370370
func extractPredicateV1(statement *in_toto.Statement) (*renderer.ChainloopProvenancePredicateV1, error) {
371371
jsonPredicate, err := json.Marshal(statement.Predicate)
372372
if err != nil {
373-
return nil, fmt.Errorf("unmarshalling predicate: %w", err)
373+
return nil, fmt.Errorf("un-marshaling predicate: %w", err)
374374
}
375375

376376
predicate := &renderer.ChainloopProvenancePredicateV1{}
377377
if err := json.Unmarshal(jsonPredicate, predicate); err != nil {
378-
return nil, fmt.Errorf("unmarshalling predicate: %w", err)
378+
return nil, fmt.Errorf("un-marshaling predicate: %w", err)
379379
}
380380

381381
return predicate, nil
@@ -503,7 +503,7 @@ func doSendToDependencyTrack(ctx context.Context, credsReader credentials.Reader
503503
attachmentConfig := i.IntegrationAttachment.Config.GetDependencyTrack()
504504

505505
creds := &credentials.APICreds{}
506-
if err := credsReader.ReadAPICreds(ctx, i.SecretName, creds); err != nil {
506+
if err := credsReader.ReadCredentials(ctx, i.SecretName, creds); err != nil {
507507
return err
508508
}
509509

internal/attestation/crafter/crafter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func NewCrafter(opts ...NewOpt) *Crafter {
8787
type InitOpts struct {
8888
// Control plane workflow metadata
8989
WfInfo *api.WorkflowMetadata
90-
// already marshalled schema
90+
// already marshaled schema
9191
SchemaV1 *schemaapi.CraftingSchema
9292
// do not record, upload or push attestation
9393
DryRun bool
@@ -243,8 +243,8 @@ func initialCraftingState(schema *schemaapi.CraftingSchema, wf *api.WorkflowMeta
243243
}
244244

245245
func persistCraftingState(craftState *api.CraftingState, stateFilePath string) error {
246-
marshaller := protojson.MarshalOptions{Indent: " "}
247-
raw, err := marshaller.Marshal(craftState)
246+
marshaler := protojson.MarshalOptions{Indent: " "}
247+
raw, err := marshaler.Marshal(craftState)
248248
if err != nil {
249249
return err
250250
}

internal/blobmanager/oci/provider.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package oci
1717

1818
import (
1919
"context"
20+
"fmt"
2021

2122
backend "github.com/chainloop-dev/chainloop/internal/blobmanager"
2223
"github.com/chainloop-dev/chainloop/internal/credentials"
@@ -35,10 +36,14 @@ func NewBackendProvider(cReader credentials.Reader) *BackendProvider {
3536

3637
func (p *BackendProvider) FromCredentials(ctx context.Context, secretName string) (backend.UploaderDownloader, error) {
3738
creds := &credentials.OCIKeypair{}
38-
if err := p.cReader.ReadOCICreds(ctx, secretName, creds); err != nil {
39+
if err := p.cReader.ReadCredentials(ctx, secretName, creds); err != nil {
3940
return nil, err
4041
}
4142

43+
if err := creds.Validate(); err != nil {
44+
return nil, fmt.Errorf("invalid credentials retrieved from storage: %w", err)
45+
}
46+
4247
k, err := ociauth.NewCredentials(creds.Repo, creds.Username, creds.Password)
4348
if err != nil {
4449
return nil, err

0 commit comments

Comments
 (0)