Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions app/controlplane/pkg/biz/mocks/WorkflowRunRepo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 20 additions & 12 deletions app/controlplane/pkg/biz/workflowrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -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")
}

Expand Down
56 changes: 56 additions & 0 deletions app/controlplane/pkg/biz/workflowrun_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 13 additions & 3 deletions app/controlplane/pkg/data/workflowrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
if err != nil && !ent.IsNotFound(err) {
return nil, err
} else if run == nil {
Expand Down
Loading