diff --git a/app/controlplane/pkg/biz/mocks/WorkflowRunRepo.go b/app/controlplane/pkg/biz/mocks/WorkflowRunRepo.go index c7b4a51cb..fb51ebace 100644 --- a/app/controlplane/pkg/biz/mocks/WorkflowRunRepo.go +++ b/app/controlplane/pkg/biz/mocks/WorkflowRunRepo.go @@ -167,70 +167,76 @@ func (_c *WorkflowRunRepo_Expire_Call) RunAndReturn(run func(ctx context.Context return _c } -// FindByAttestationDigest provides a mock function for the type WorkflowRunRepo -func (_mock *WorkflowRunRepo) FindByAttestationDigest(ctx context.Context, digest string) (*biz.WorkflowRun, error) { - ret := _mock.Called(ctx, digest) +// FindByAttestationDigestInOrg provides a mock function for the type WorkflowRunRepo +func (_mock *WorkflowRunRepo) FindByAttestationDigestInOrg(ctx context.Context, orgID uuid.UUID, digest string) (*biz.WorkflowRun, error) { + ret := _mock.Called(ctx, orgID, digest) if len(ret) == 0 { - panic("no return value specified for FindByAttestationDigest") + panic("no return value specified for FindByAttestationDigestInOrg") } var r0 *biz.WorkflowRun var r1 error - if returnFunc, ok := ret.Get(0).(func(context.Context, string) (*biz.WorkflowRun, error)); ok { - return returnFunc(ctx, digest) + if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, string) (*biz.WorkflowRun, error)); ok { + return returnFunc(ctx, orgID, digest) } - if returnFunc, ok := ret.Get(0).(func(context.Context, string) *biz.WorkflowRun); ok { - r0 = returnFunc(ctx, digest) + if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, string) *biz.WorkflowRun); ok { + r0 = returnFunc(ctx, orgID, digest) } else { if ret.Get(0) != nil { r0 = ret.Get(0).(*biz.WorkflowRun) } } - if returnFunc, ok := ret.Get(1).(func(context.Context, string) error); ok { - r1 = returnFunc(ctx, digest) + if returnFunc, ok := ret.Get(1).(func(context.Context, uuid.UUID, string) error); ok { + r1 = returnFunc(ctx, orgID, digest) } else { r1 = ret.Error(1) } return r0, r1 } -// WorkflowRunRepo_FindByAttestationDigest_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'FindByAttestationDigest' -type WorkflowRunRepo_FindByAttestationDigest_Call struct { +// WorkflowRunRepo_FindByAttestationDigestInOrg_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'FindByAttestationDigestInOrg' +type WorkflowRunRepo_FindByAttestationDigestInOrg_Call struct { *mock.Call } -// FindByAttestationDigest is a helper method to define mock.On call +// FindByAttestationDigestInOrg is a helper method to define mock.On call // - ctx context.Context +// - orgID uuid.UUID // - digest string -func (_e *WorkflowRunRepo_Expecter) FindByAttestationDigest(ctx interface{}, digest interface{}) *WorkflowRunRepo_FindByAttestationDigest_Call { - return &WorkflowRunRepo_FindByAttestationDigest_Call{Call: _e.mock.On("FindByAttestationDigest", ctx, digest)} +func (_e *WorkflowRunRepo_Expecter) FindByAttestationDigestInOrg(ctx interface{}, orgID interface{}, digest interface{}) *WorkflowRunRepo_FindByAttestationDigestInOrg_Call { + return &WorkflowRunRepo_FindByAttestationDigestInOrg_Call{Call: _e.mock.On("FindByAttestationDigestInOrg", ctx, orgID, digest)} } -func (_c *WorkflowRunRepo_FindByAttestationDigest_Call) Run(run func(ctx context.Context, digest string)) *WorkflowRunRepo_FindByAttestationDigest_Call { +func (_c *WorkflowRunRepo_FindByAttestationDigestInOrg_Call) Run(run func(ctx context.Context, orgID uuid.UUID, digest string)) *WorkflowRunRepo_FindByAttestationDigestInOrg_Call { _c.Call.Run(func(args mock.Arguments) { var arg0 context.Context if args[0] != nil { arg0 = args[0].(context.Context) } - var arg1 string + var arg1 uuid.UUID if args[1] != nil { - arg1 = args[1].(string) + arg1 = args[1].(uuid.UUID) + } + var arg2 string + if args[2] != nil { + arg2 = args[2].(string) } run( arg0, arg1, + arg2, ) }) return _c } -func (_c *WorkflowRunRepo_FindByAttestationDigest_Call) Return(workflowRun *biz.WorkflowRun, err error) *WorkflowRunRepo_FindByAttestationDigest_Call { +func (_c *WorkflowRunRepo_FindByAttestationDigestInOrg_Call) Return(workflowRun *biz.WorkflowRun, err error) *WorkflowRunRepo_FindByAttestationDigestInOrg_Call { _c.Call.Return(workflowRun, err) return _c } -func (_c *WorkflowRunRepo_FindByAttestationDigest_Call) RunAndReturn(run func(ctx context.Context, digest string) (*biz.WorkflowRun, error)) *WorkflowRunRepo_FindByAttestationDigest_Call { +func (_c *WorkflowRunRepo_FindByAttestationDigestInOrg_Call) RunAndReturn(run func(ctx context.Context, orgID uuid.UUID, digest string) (*biz.WorkflowRun, error)) *WorkflowRunRepo_FindByAttestationDigestInOrg_Call { _c.Call.Return(run) return _c } diff --git a/app/controlplane/pkg/biz/workflowrun.go b/app/controlplane/pkg/biz/workflowrun.go index 53c163cbb..a1e371d60 100644 --- a/app/controlplane/pkg/biz/workflowrun.go +++ b/app/controlplane/pkg/biz/workflowrun.go @@ -102,7 +102,8 @@ type WorkflowRunRepoCreateResult struct { type WorkflowRunRepo interface { Create(ctx context.Context, opts *WorkflowRunRepoCreateOpts) (*WorkflowRunRepoCreateResult, error) FindByID(ctx context.Context, ID uuid.UUID) (*WorkflowRun, error) - FindByAttestationDigest(ctx context.Context, digest string) (*WorkflowRun, error) + // FindByAttestationDigestInOrg returns nil when no run in the organization holds the digest + FindByAttestationDigestInOrg(ctx context.Context, orgID uuid.UUID, digest string) (*WorkflowRun, error) FindByIDInOrg(ctx context.Context, orgID, ID uuid.UUID) (*WorkflowRun, error) MarkAsFinished(ctx context.Context, ID uuid.UUID, status WorkflowRunStatus, reason string) error // SaveAttestationBundle persists the digest and bundle. When @@ -668,16 +669,23 @@ func (uc *WorkflowRunUseCase) SaveAttestation(ctx context.Context, id string, bu } // Run some validations on the predicate - // Attestations can include dependent attestations, and we want to make sure they exist in the system - // Find any material of kind attestation and make sure they exist already + // Attestations can include dependent attestations, and we want to make sure they exist in the system. + // A dependent attestation must belong to the organization of this run. One in another organization + // is reported exactly like a nonexistent one, so the response does not tell that it exists. for _, m := range predicate.GetMaterials() { - if m.Type == schemaapi.CraftingSchema_Material_ATTESTATION.String() { - run, err := uc.wfRunRepo.FindByAttestationDigest(ctx, m.Hash.String()) - if err != nil { - return nil, fmt.Errorf("finding attestation: %w", err) - } else if run == nil { - return nil, NewErrValidation(fmt.Errorf("dependent attestation not found: %s", m.Hash)) - } + if m.Type != schemaapi.CraftingSchema_Material_ATTESTATION.String() { + continue + } + + if run.Workflow == nil { + return nil, fmt.Errorf("workflow run %s has no workflow", runID) + } + + dependentRun, err := uc.wfRunRepo.FindByAttestationDigestInOrg(ctx, run.Workflow.OrgID, m.Hash.String()) + if err != nil { + return nil, fmt.Errorf("finding attestation: %w", err) + } else if dependentRun == nil { + return nil, NewErrValidation(fmt.Errorf("dependent attestation not found: %s", m.Hash)) } } @@ -954,12 +962,12 @@ func (uc *WorkflowRunUseCase) GetByDigestInOrg(ctx context.Context, orgID, diges return nil, NewErrValidation(fmt.Errorf("invalid digest format: %w", err)) } - wfrun, err := uc.wfRunRepo.FindByAttestationDigest(ctx, digest) + wfrun, err := uc.wfRunRepo.FindByAttestationDigestInOrg(ctx, orgUUID, digest) if err != nil { return nil, fmt.Errorf("finding workflow run: %w", err) } - if wfrun == nil || wfrun.Workflow.OrgID != orgUUID { + if wfrun == nil { return nil, NewErrNotFound("workflow run") } diff --git a/app/controlplane/pkg/biz/workflowrun_integration_test.go b/app/controlplane/pkg/biz/workflowrun_integration_test.go index 36309e509..05eb76d9f 100644 --- a/app/controlplane/pkg/biz/workflowrun_integration_test.go +++ b/app/controlplane/pkg/biz/workflowrun_integration_test.go @@ -292,6 +292,62 @@ func (s *workflowRunIntegrationTestSuite) TestSaveAttestation() { }) } +// A dependent attestation must belong to the organization of the run that references it. +// A digest owned by another organization is reported exactly like a nonexistent one, so +// the store path does not leak or link attestations across tenants. +func (s *workflowRunIntegrationTestSuite) TestSaveAttestationDependentAttestationOrgScope() { + ctx := context.Background() + // Digest of the ATTESTATION material referenced by the fixture below. + const dependentDigest = "sha256:2dc17f7c933d20e06b49250a582a3d19bdfbadba9c4e5f3f856af6f261db79d4" + bundleBytes := testhelpers.BundleBytesFromEnvelope(s.T(), "testdata/attestations/with-dependent-attestation.json") + + testCases := []struct { + name string + // workflow that owns the dependent attestation, nil when it does not exist + ownerWorkflow *biz.Workflow + wantErr bool + }{ + {name: "dependent attestation in the same org", ownerWorkflow: s.workflowOrg2}, + {name: "dependent attestation in another org", ownerWorkflow: s.workflowOrg1, wantErr: true}, + {name: "dependent attestation does not exist", wantErr: true}, + } + + for _, tc := range testCases { + s.Run(tc.name, func() { + if tc.ownerWorkflow != nil { + owner, err := s.WorkflowRun.Create(ctx, &biz.WorkflowRunCreateOpts{ + WorkflowID: tc.ownerWorkflow.ID.String(), ContractRevision: s.contractVersion, CASBackendID: s.casBackend.ID, + }) + s.Require().NoError(err) + s.Require().NoError(s.Data.DB.WorkflowRun.UpdateOneID(owner.ID).SetAttestationDigest(dependentDigest).Exec(ctx)) + s.T().Cleanup(func() { + s.Require().NoError(s.Data.DB.WorkflowRun.UpdateOneID(owner.ID).ClearAttestationDigest().Exec(ctx)) + }) + } + + run, err := s.WorkflowRun.Create(ctx, &biz.WorkflowRunCreateOpts{ + WorkflowID: s.workflowOrg2.ID.String(), ContractRevision: s.contractVersion, CASBackendID: s.casBackend.ID, + }) + s.Require().NoError(err) + + _, err = s.WorkflowRun.SaveAttestation(ctx, run.ID.String(), bundleBytes) + stored, getErr := s.Data.DB.WorkflowRun.Get(ctx, run.ID) + s.Require().NoError(getErr) + + if !tc.wantErr { + s.NoError(err) + s.NotEmpty(stored.AttestationDigest) + return + } + + s.Require().Error(err) + s.True(biz.IsErrValidation(err), "unexpected error type: %v", err) + s.ErrorContains(err, "dependent attestation not found") + s.Empty(stored.AttestationDigest, "the attestation must not be persisted") + }) + } +} + // setOrgBlockReleasedVersions toggles the org-level // BlockAttestationsOnReleasedVersions setting directly through the repo. func (s *workflowRunIntegrationTestSuite) setOrgBlockReleasedVersions(ctx context.Context, orgID string, enabled bool) { diff --git a/app/controlplane/pkg/data/workflowrun.go b/app/controlplane/pkg/data/workflowrun.go index e00b5d7bc..db6d152bc 100644 --- a/app/controlplane/pkg/data/workflowrun.go +++ b/app/controlplane/pkg/data/workflowrun.go @@ -218,11 +218,21 @@ func (r *WorkflowRunRepo) FindByID(ctx context.Context, id uuid.UUID) (*biz.Work return entWrToBizWr(ctx, run) } -func (r *WorkflowRunRepo) FindByAttestationDigest(ctx context.Context, digest string) (*biz.WorkflowRun, error) { - ctx, span := otelx.Start(ctx, workflowRunRepoTracer, "WorkflowRunRepo.FindByAttestationDigest") +// FindByAttestationDigestInOrg returns the workflow run that holds the given attestation digest +// in the given organization, or nil if there is none. The digest index is not unique, so the +// most recent match is returned. The organization is matched on the denormalized +// organization_id column, as in List. +func (r *WorkflowRunRepo) FindByAttestationDigestInOrg(ctx context.Context, orgID uuid.UUID, digest string) (*biz.WorkflowRun, error) { + ctx, span := otelx.Start(ctx, workflowRunRepoTracer, "WorkflowRunRepo.FindByAttestationDigestInOrg") defer span.End() - run, err := eagerLoadWorkflowRun(r.data.DB).Where(workflowrun.AttestationDigest(digest)).Only(ctx) + run, err := eagerLoadWorkflowRun(r.data.DB). + Where( + workflowrun.AttestationDigest(digest), + workflowrun.OrganizationID(orgID), + ). + Order(ent.Desc(workflowrun.FieldCreatedAt)). + First(ctx) if err != nil && !ent.IsNotFound(err) { return nil, err } else if run == nil {