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
5 changes: 4 additions & 1 deletion app/controlplane/pkg/data/referrer.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ func isReferrerVisible(projectsByOrg map[uuid.UUID][]uuid.UUID, allowedOrgs []uu
}

// hydrate the referrer with the following information:
// - workflowIDs: the list of associated workflows
// - workflowIDs: the list of associated live workflows
// - orgIDs: the list of associated organizations
// - projectIDs: the list of associated projects
// It also returns the associated projects grouped by organization, for the visibility check.
Expand All @@ -662,6 +662,9 @@ func hydrateWorkflowsInfo(root *ent.Referrer, out *biz.StoredReferrer) map[uuid.
// a set per organization keeps the deduplication linear in the number of workflows
projectSetsByOrg := make(map[uuid.UUID]map[uuid.UUID]struct{}, 0)
for _, wf := range root.Edges.Workflows {
if !wf.DeletedAt.IsZero() {
continue
}
workflowIDs = append(workflowIDs, wf.ID)
projectSet, ok := projectSetsByOrg[wf.OrganizationID]
if !ok {
Expand Down
37 changes: 37 additions & 0 deletions app/controlplane/pkg/data/referrer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"strings"
"testing"
"time"

"entgo.io/ent/dialect"
"entgo.io/ent/dialect/sql"
Expand Down Expand Up @@ -349,3 +350,39 @@ func BenchmarkHydrateWorkflowsInfo(b *testing.B) {
hydrateWorkflowsInfo(root, &biz.StoredReferrer{})
}
}

func TestHydrateWorkflowsInfoSkipsDeletedWorkflows(t *testing.T) {
orgID, otherOrgID := uuid.New(), uuid.New()
live := &ent.Workflow{ID: uuid.New(), OrganizationID: orgID, ProjectID: uuid.New()}
deleted := &ent.Workflow{ID: uuid.New(), OrganizationID: otherOrgID, ProjectID: uuid.New(), DeletedAt: time.Now()}
for _, tc := range []struct {
name string
workflows []*ent.Workflow
wantLive bool
}{
{name: "deleted only", workflows: []*ent.Workflow{deleted}},
{name: "deleted before live", workflows: []*ent.Workflow{deleted, live}, wantLive: true},
{name: "deleted after live", workflows: []*ent.Workflow{live, deleted}, wantLive: true},
} {
t.Run(tc.name, func(t *testing.T) {
root := &ent.Referrer{Edges: ent.ReferrerEdges{Workflows: tc.workflows}}
out := &biz.StoredReferrer{}
got := hydrateWorkflowsInfo(root, out)
assert.NotContains(t, got, otherOrgID, "deleted workflows must not contribute visibility")
assert.NotContains(t, out.WorkflowIDs, deleted.ID)
assert.NotContains(t, out.ProjectIDs, deleted.ProjectID)
assert.NotContains(t, out.OrgIDs, otherOrgID)
if tc.wantLive {
assert.Equal(t, map[uuid.UUID][]uuid.UUID{orgID: {live.ProjectID}}, got)
assert.Equal(t, []uuid.UUID{live.ID}, out.WorkflowIDs)
assert.Equal(t, []uuid.UUID{live.ProjectID}, out.ProjectIDs)
assert.Equal(t, []uuid.UUID{orgID}, out.OrgIDs)
} else {
assert.Empty(t, got)
assert.Empty(t, out.WorkflowIDs)
assert.Empty(t, out.ProjectIDs)
assert.Empty(t, out.OrgIDs)
}
})
}
}
63 changes: 63 additions & 0 deletions app/controlplane/pkg/data/referrer_visibility_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"
"time"

"github.com/chainloop-dev/chainloop/app/controlplane/pkg/biz"
"github.com/chainloop-dev/chainloop/app/controlplane/pkg/biz/testhelpers"
"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data"
"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/organization"
Expand Down Expand Up @@ -246,3 +247,65 @@ func sortIDs(ids []uuid.UUID) []uuid.UUID {
sort.Slice(out, func(i, j int) bool { return out[i].String() < out[j].String() })
return out
}

func (s *referrerVisibilityIntegrationTestSuite) TestDeletedWorkflowsNeverGrantProjectVisibility() {
ctx := context.Background()
client := s.Data.DB
live := client.Workflow.Query().Where(workflow.Name("live-allowed")).OnlyX(ctx)
otherLive := client.Workflow.Query().Where(workflow.Name("live-allowed-2")).OnlyX(ctx)
deleted := client.Workflow.Query().Where(workflow.Name("deleted-allowed")).OnlyX(ctx)
// This root is visible through otherLive even when live's project is hidden.
root := client.Referrer.GetX(ctx, s.twoLiveInAllowed)
client.Referrer.UpdateOneID(root.ID).
AddReferenceIDs(s.deletedAndLiveInAllowed, s.deletedAllowedLiveOther).ExecX(ctx)

for _, tc := range []struct {
name string
orgs []uuid.UUID
projects map[uuid.UUID][]uuid.UUID
hidden uuid.UUID
wantReferences []uuid.UUID
}{
{
name: "deleted workflow in a granted project",
orgs: []uuid.UUID{s.orgAllowed},
projects: map[uuid.UUID][]uuid.UUID{s.orgAllowed: {otherLive.ProjectID, deleted.ProjectID}},
hidden: s.deletedAndLiveInAllowed,
},
{
name: "deleted workflow in an unrestricted organization",
orgs: []uuid.UUID{s.orgAllowed, s.orgOther},
projects: map[uuid.UUID][]uuid.UUID{s.orgOther: {}},
hidden: s.deletedAllowedLiveOther,
wantReferences: []uuid.UUID{s.deletedAndLiveInAllowed},
},
} {
s.Run(tc.name, func() {
filters := []biz.GetFromRootFilter{biz.WithVisibleProjectIDs(tc.projects)}
hidden := client.Referrer.GetX(ctx, tc.hidden)
got, cursor, err := s.Repos.Referrer.GetFromRoot(ctx, hidden.Digest, tc.orgs, nil, filters...)
s.Require().NoError(err)
s.Nil(got, "deleted workflows must not grant root visibility")
s.Empty(cursor)

got, _, err = s.Repos.Referrer.GetFromRoot(ctx, root.Digest, tc.orgs, nil, filters...)
s.Require().NoError(err)
s.Require().NotNil(got)
ids := make([]uuid.UUID, 0, len(got.References))
for _, ref := range got.References {
ids = append(ids, ref.ID)
}
s.ElementsMatch(tc.wantReferences, ids, "deleted workflows must not grant reference visibility")
})
}

// A live workflow in a granted project still grants access, even beside a deleted one.
got, _, err := s.Repos.Referrer.GetFromRoot(ctx,
client.Referrer.GetX(ctx, s.deletedAndLiveInAllowed).Digest, []uuid.UUID{s.orgAllowed}, nil,
biz.WithVisibleProjectIDs(map[uuid.UUID][]uuid.UUID{s.orgAllowed: {live.ProjectID}}))
s.Require().NoError(err)
s.Require().NotNil(got)
s.Equal([]uuid.UUID{live.ID}, got.WorkflowIDs)
s.Equal([]uuid.UUID{live.ProjectID}, got.ProjectIDs)
s.Equal([]uuid.UUID{s.orgAllowed}, got.OrgIDs)
}
Loading