Skip to content

Commit 08cd26f

Browse files
authored
chore: download SBOMs using CAS (#52)
Signed-off-by: Miguel Martinez Trivino <[email protected]>
1 parent b85d45c commit 08cd26f

File tree

9 files changed

+46
-15
lines changed

9 files changed

+46
-15
lines changed

app/controlplane/cmd/wire_gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/controlplane/internal/biz/integration/dependencytrack/dependencytrack.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/chainloop-dev/chainloop/app/controlplane/internal/biz"
3030
"github.com/chainloop-dev/chainloop/app/controlplane/internal/integrations/dependencytrack"
3131
"github.com/chainloop-dev/chainloop/internal/attestation/renderer"
32-
"github.com/chainloop-dev/chainloop/internal/blobmanager/oci"
3332
"github.com/chainloop-dev/chainloop/internal/credentials"
3433
"github.com/chainloop-dev/chainloop/internal/servicelogger"
3534
"github.com/go-kratos/kratos/v2/log"
@@ -41,13 +40,14 @@ type Integration struct {
4140
integrationUC *biz.IntegrationUseCase
4241
ociUC *biz.OCIRepositoryUseCase
4342
credentialsProvider credentials.ReaderWriter
43+
casClient biz.CASClient
4444
log *log.Helper
4545
}
4646

4747
const Kind = "Dependency-Track"
4848

49-
func New(integrationUC *biz.IntegrationUseCase, ociUC *biz.OCIRepositoryUseCase, creds credentials.ReaderWriter, l log.Logger) *Integration {
50-
return &Integration{integrationUC, ociUC, creds, servicelogger.ScopedHelper(l, "biz/integration/deptrack")}
49+
func New(integrationUC *biz.IntegrationUseCase, ociUC *biz.OCIRepositoryUseCase, creds credentials.ReaderWriter, c biz.CASClient, l log.Logger) *Integration {
50+
return &Integration{integrationUC, ociUC, creds, c, servicelogger.ScopedHelper(l, "biz/integration/deptrack")}
5151
}
5252

5353
func (uc *Integration) Add(ctx context.Context, orgID, host, apiKey string, enableProjectCreation bool) (*biz.Integration, error) {
@@ -105,6 +105,7 @@ func (uc *Integration) UploadSBOMs(envelope *dsse.Envelope, orgID, workflowID st
105105
return nil
106106
}
107107

108+
// There is at least one enabled integration, extract the SBOMs
108109
predicate, err := renderer.ExtractPredicate(envelope)
109110
if err != nil {
110111
return err
@@ -117,11 +118,6 @@ func (uc *Integration) UploadSBOMs(envelope *dsse.Envelope, orgID, workflowID st
117118
return errors.NotFound("not found", "main repository not found")
118119
}
119120

120-
backend, err := oci.NewBackendProvider(uc.credentialsProvider).FromCredentials(ctx, repo.SecretName)
121-
if err != nil {
122-
return err
123-
}
124-
125121
for _, m := range predicate.Materials {
126122
if m.Type != contractAPI.CraftingSchema_Material_SBOM_CYCLONEDX_JSON.String() {
127123
continue
@@ -133,11 +129,14 @@ func (uc *Integration) UploadSBOMs(envelope *dsse.Envelope, orgID, workflowID st
133129
continue
134130
}
135131

132+
digest = "sha256:" + digest
133+
136134
uc.log.Infow("msg", "SBOM present, downloading", "workflowID", workflowID, "integration", Kind, "name", m.Name)
137135
// Download SBOM
138-
if err := backend.Download(ctx, buf, digest); err != nil {
139-
return err
136+
if err := uc.casClient.Download(ctx, repo.SecretName, buf, digest); err != nil {
137+
return fmt.Errorf("downloading from CAS: %w", err)
140138
}
139+
141140
uc.log.Infow("msg", "SBOM downloaded", "digest", digest, "workflowID", workflowID, "integration", Kind, "name", m.Name)
142141

143142
// Run integrations with that sbom

app/controlplane/internal/biz/integration/dependencytrack/dependencytrack_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (s *testSuite) TestAdd() {
3434
org, err := s.Organization.Create(ctx, "testing org")
3535
assert.NoError(err)
3636

37-
i := dependencytrack.New(s.Integration, s.OCIRepo, credsReader, nil)
37+
i := dependencytrack.New(s.Integration, s.OCIRepo, credsReader, nil, nil)
3838

3939
credsReader.On("SaveCredentials", ctx, org.ID, mock.Anything).Return("secret-key", nil)
4040

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-----BEGIN EC PRIVATE KEY-----
2+
MIHcAgEBBEIAH5EPbqm6m8XzqQOao85bpZR+X+mjJNEdeC46PuW9hvJVzXxQl7hK
3+
O4QNGanrmKIItYklBrX7YOLqzaFYXNL/zQKgBwYFK4EEACOhgYkDgYYABAB4jcRn
4+
lZuECX6QvUAOCGiMVCbxYv+mOXgn2X0EDx+MEhHcSN2jjf0IinRpVMtufrFjG+A0
5+
dhF2wK0O9HxPvcVP9gAnjlu5gak7H4SjM9JkCTdWIMpcXrW3lzRAVbb0niYv0Wbc
6+
sAQhsfOVcKEo88Zaqlisw0edRb+BtZsFMWIniyzb0Q==
7+
-----END EC PRIVATE KEY-----
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-----BEGIN EC PRIVATE KEY-----
2+
MIHcAgEBBEIAH5EPbqm6m8XzqQOao85bpZR+X+mjJNEdeC46PuW9hvJVzXxQl7hK
3+
O4QNGanrmKIItYklBrX7YOLqzaFYXNL/zQKgBwYFK4EEACOhgYkDgYYABAB4jcRn
4+
lZuECX6QvUAOCGiMVCbxYv+mOXgn2X0EDx+MEhHcSN2jjf0IinRpVMtufrFjG+A0
5+
dhF2wK0O9HxPvcVP9gAnjlu5gak7H4SjM9JkCTdWIMpcXrW3lzRAVbb0niYv0Wbc
6+
sAQhsfOVcKEo88Zaqlisw0edRb+BtZsFMWIniyzb0Q==
7+
-----END EC PRIVATE KEY-----

app/controlplane/internal/biz/testhelpers/database.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ func NewTestingUseCases(t *testing.T, opts ...NewTestingUCOpt) *TestingUseCases
8484

8585
db := NewTestDatabase(t)
8686
log := log.NewStdLogger(io.Discard)
87-
testData, _, err := WireTestData(db, t, log, newArgs.credsReaderWriter, &robotaccount.Builder{}, &conf.Auth{GeneratedJwsHmacSecret: "test"})
87+
testData, _, err := WireTestData(db, t, log, newArgs.credsReaderWriter, &robotaccount.Builder{}, &conf.Auth{
88+
GeneratedJwsHmacSecret: "test",
89+
CasRobotAccountPrivateKeyPath: "./testdata/test-key.ec.pem",
90+
})
8891
assert.NoError(t, err)
8992
return testData
9093
}
@@ -140,6 +143,10 @@ func newConfData(db *TestDatabase, t *testing.T) *conf.Data {
140143
return &conf.Data{Database: &conf.Data_Database{Driver: "pgx", Source: db.ConnectionString(t)}}
141144
}
142145

146+
func newConfCAS() *conf.Bootstrap_CASServer {
147+
return &conf.Bootstrap_CASServer{Grpc: &conf.Server_GRPC{}}
148+
}
149+
143150
func (db *TestDatabase) Close(t *testing.T) {
144151
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
145152
defer cancel()

app/controlplane/internal/biz/testhelpers/suite.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616
package testhelpers
1717

18-
import "github.com/stretchr/testify/suite"
18+
import (
19+
"github.com/stretchr/testify/suite"
20+
)
1921

2022
// Suite that creates a database and sets the schema before each test
2123
type UseCasesEachTestSuite struct {

app/controlplane/internal/biz/testhelpers/wire.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ func WireTestData(*TestDatabase, *testing.T, log.Logger, credentials.ReaderWrite
4343
integration.ProviderSet,
4444
wire.Bind(new(backend.Provider), new(*oci.BackendProvider)),
4545
wire.Bind(new(credentials.Reader), new(credentials.ReaderWriter)),
46+
wire.Bind(new(biz.CASClient), new(*biz.CASClientUseCase)),
4647
oci.NewBackendProvider,
4748
wire.Struct(new(TestingUseCases), "*"),
4849
newConfData,
50+
newConfCAS,
4951
),
5052
)
5153
}

app/controlplane/internal/biz/testhelpers/wire_gen.go

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)