@@ -24,8 +24,11 @@ import (
2424 "github.com/chainloop-dev/chainloop/app/controlplane/internal/biz"
2525 "github.com/chainloop-dev/chainloop/app/controlplane/internal/biz/testhelpers"
2626 "github.com/chainloop-dev/chainloop/app/controlplane/internal/conf"
27+ "github.com/chainloop-dev/chainloop/internal/credentials"
28+ creds "github.com/chainloop-dev/chainloop/internal/credentials/mocks"
2729 "github.com/google/uuid"
2830 "github.com/secure-systems-lab/go-securesystemslib/dsse"
31+ "github.com/stretchr/testify/mock"
2932 "github.com/stretchr/testify/require"
3033 "github.com/stretchr/testify/suite"
3134)
@@ -108,12 +111,39 @@ func (s *referrerIntegrationTestSuite) TestGetFromRootInPublicSharedIndex() {
108111 })
109112}
110113
114+ func (s * referrerIntegrationTestSuite ) TestExtractAndPersistsDependentAttestation () {
115+ envelope := testEnvelope (s .T (), "testdata/attestations/with-dependent-attestation.json" )
116+ ctx := context .Background ()
117+
118+ const (
119+ wantReferrerAtt = "sha256:950c7b4c65447a3b86b6f769515005e7c44a67c8193bff790750eadf13207fbb"
120+ wantDependentAtt = "sha256:2dc17f7c933d20e06b49250a582a3d19bdfbadba9c4e5f3f856af6f261db79d4"
121+ )
122+
123+ s .Run ("creation fails because the dependent attestation doesn't exist yet" , func () {
124+ err := s .Referrer .ExtractAndPersist (ctx , envelope , s .workflow1 .ID .String ())
125+ s .ErrorContains (err , "attestation material does not exist" )
126+ })
127+
128+ s .Run ("if the dependent attestation exists we ingest it" , func () {
129+ // We store the dependent attestation
130+ dependentAtt := testEnvelope (s .T (), "testdata/attestations/dependent-attestation.json" )
131+ err := s .Referrer .ExtractAndPersist (ctx , dependentAtt , s .workflow1 .ID .String ())
132+ require .NoError (s .T (), err )
133+
134+ err = s .Referrer .ExtractAndPersist (ctx , envelope , s .workflow1 .ID .String ())
135+ s .NoError (err )
136+ got , err := s .Referrer .GetFromRootUser (ctx , wantReferrerAtt , "ATTESTATION" , s .user .ID )
137+ s .NoError (err )
138+ // It has a commit and an attestation
139+ require .Len (s .T (), got .References , 2 )
140+ s .Equal (wantDependentAtt , got .References [1 ].Digest )
141+ })
142+ }
143+
111144func (s * referrerIntegrationTestSuite ) TestExtractAndPersists () {
112145 // Load attestation
113- attJSON , err := os .ReadFile ("testdata/attestations/with-git-subject.json" )
114- require .NoError (s .T (), err )
115- var envelope * dsse.Envelope
116- require .NoError (s .T (), json .Unmarshal (attJSON , & envelope ))
146+ envelope := testEnvelope (s .T (), "testdata/attestations/with-git-subject.json" )
117147
118148 wantReferrerAtt := & biz.Referrer {
119149 Digest : "sha256:de36d470d792499b1489fc0e6623300fc8822b8f0d2981bb5ec563f8dde723c7" ,
@@ -218,7 +248,7 @@ func (s *referrerIntegrationTestSuite) TestExtractAndPersists() {
218248 })
219249
220250 s .T ().Run ("but another workflow can be attached" , func (t * testing.T ) {
221- err = s .Referrer .ExtractAndPersist (ctx , envelope , s .workflow2 .ID .String ())
251+ err : = s .Referrer .ExtractAndPersist (ctx , envelope , s .workflow2 .ID .String ())
222252 s .NoError (err )
223253 got , err := s .Referrer .GetFromRootUser (ctx , wantReferrerAtt .Digest , "" , s .user .ID )
224254 s .NoError (err )
@@ -232,12 +262,12 @@ func (s *referrerIntegrationTestSuite) TestExtractAndPersists() {
232262 got , err = s .Referrer .GetFromRootUser (ctx , wantReferrerAtt .Digest , "" , s .user .ID )
233263 s .NoError (err )
234264 require .Len (t , got .OrgIDs , 2 )
235- s .Equal ([]uuid.UUID {s .org1UUID , s .org2UUID }, got .OrgIDs )
236- s .Equal ([]uuid.UUID {s .workflow1 .ID , s .workflow2 .ID }, got .WorkflowIDs )
265+ s .Equal ([]uuid.UUID {s .org2UUID , s .org1UUID }, got .OrgIDs )
266+ s .Equal ([]uuid.UUID {s .workflow2 .ID , s .workflow1 .ID }, got .WorkflowIDs )
237267 })
238268
239269 s .T ().Run ("and now user2 has access to it since it has access to workflow2 in org2" , func (t * testing.T ) {
240- err = s .Referrer .ExtractAndPersist (ctx , envelope , s .workflow2 .ID .String ())
270+ err : = s .Referrer .ExtractAndPersist (ctx , envelope , s .workflow2 .ID .String ())
241271 s .NoError (err )
242272 got , err := s .Referrer .GetFromRootUser (ctx , wantReferrerAtt .Digest , "" , s .user2 .ID )
243273 s .NoError (err )
@@ -274,19 +304,14 @@ func (s *referrerIntegrationTestSuite) TestExtractAndPersists() {
274304 })
275305
276306 s .T ().Run ("it should NOT fail storing the attestation with the same material twice with different types" , func (t * testing.T ) {
277- attJSON , err = os .ReadFile ("testdata/attestations/with-duplicated-sha.json" )
278- require .NoError (s .T (), err )
279- require .NoError (s .T (), json .Unmarshal (attJSON , & envelope ))
307+ envelope := testEnvelope (s .T (), "testdata/attestations/with-duplicated-sha.json" )
280308
281309 err := s .Referrer .ExtractAndPersist (ctx , envelope , s .workflow1 .ID .String ())
282310 s .NoError (err )
283311 })
284312
285313 s .T ().Run ("it should fail on retrieval if we have stored two referrers with same digest (for two different types)" , func (t * testing.T ) {
286- // this attestation contains a material with same digest than the container image from git-subject.json
287- attJSON , err = os .ReadFile ("testdata/attestations/same-digest-than-git-subject.json" )
288- require .NoError (s .T (), err )
289- require .NoError (s .T (), json .Unmarshal (attJSON , & envelope ))
314+ envelope := testEnvelope (s .T (), "testdata/attestations/same-digest-than-git-subject.json" )
290315
291316 // storing will not fail since it's the a different artifact type
292317 err := s .Referrer .ExtractAndPersist (ctx , envelope , s .workflow1 .ID .String ())
@@ -329,7 +354,7 @@ func (s *referrerIntegrationTestSuite) TestExtractAndPersists() {
329354 got , err := s .Referrer .GetFromRootUser (ctx , wantReferrerAtt .Digest , "" , s .user .ID )
330355 s .NoError (err )
331356 s .False (got .InPublicWorkflow )
332- s .Equal ([]uuid.UUID {s .workflow1 .ID , s .workflow2 .ID }, got .WorkflowIDs )
357+ s .Equal ([]uuid.UUID {s .workflow2 .ID , s .workflow1 .ID }, got .WorkflowIDs )
333358 for _ , r := range got .References {
334359 s .False (r .InPublicWorkflow )
335360 }
@@ -356,11 +381,15 @@ type referrerIntegrationTestSuite struct {
356381 org1UUID , org2UUID uuid.UUID
357382 user , user2 * biz.User
358383 sharedEnabledUC * biz.ReferrerUseCase
384+ run * biz.WorkflowRun
359385}
360386
361387func (s * referrerIntegrationTestSuite ) SetupTest () {
362- s . TestingUseCases = testhelpers . NewTestingUseCases (s .T ())
388+ credsWriter := creds . NewReaderWriter (s .T ())
363389 ctx := context .Background ()
390+ credsWriter .On ("SaveCredentials" , ctx , mock .Anything , & credentials.OCIKeypair {Repo : "repo" , Username : "username" , Password : "pass" }).Return ("stored-OCI-secret" , nil )
391+
392+ s .TestingUseCases = testhelpers .NewTestingUseCases (s .T (), testhelpers .WithCredsReaderWriter (credsWriter ))
364393
365394 var err error
366395 s .org1 , err = s .Organization .CreateWithRandomName (ctx )
@@ -398,6 +427,24 @@ func (s *referrerIntegrationTestSuite) SetupTest() {
398427 AllowedOrgs : []string {s .org1 .ID },
399428 }, nil )
400429 require .NoError (s .T (), err )
430+
431+ // Robot account
432+ robotAccount , err := s .RobotAccount .Create (ctx , "name" , s .org1 .ID , s .workflow1 .ID .String ())
433+ require .NoError (s .T (), err )
434+
435+ // Find contract revision
436+ contractVersion , err := s .WorkflowContract .Describe (ctx , s .org1 .ID , s .workflow1 .ContractID .String (), 0 )
437+ require .NoError (s .T (), err )
438+
439+ casBackend , err := s .CASBackend .CreateOrUpdate (ctx , s .org1 .ID , "repo" , "username" , "pass" , backendType , true )
440+ require .NoError (s .T (), err )
441+
442+ s .run , err = s .WorkflowRun .Create (ctx ,
443+ & biz.WorkflowRunCreateOpts {
444+ WorkflowID : s .workflow1 .ID .String (), RobotaccountID : robotAccount .ID .String (), ContractRevision : contractVersion , CASBackendID : casBackend .ID ,
445+ })
446+
447+ require .NoError (s .T (), err )
401448}
402449
403450func TestReferrerIntegration (t * testing.T ) {
0 commit comments