From 03a450a5d1d337ad61b42ee009019a4be0dbb7af Mon Sep 17 00:00:00 2001 From: Andre Staats Date: Thu, 29 Oct 2026 00:30:22 +0100 Subject: [PATCH 1/3] fleet: bind group assignments to the granting org A group assignment now records the org that made it and grants viewer access only while the collector's cluster is in that org. Claim, unclaim and auto-claim remove the cluster's assignments the new org did not make in the same statement; the Access tab and delete are org-scoped; an empty group id is refused. Migration 0053 backfills the org, removes rows whose cluster has no org or changed org after the grant, and audits each removal. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01TAAVzyeV4HSSNjU8xVWWtE --- CHANGELOG.md | 8 + deploy/helm/shepherd/UPGRADING.md | 49 +++++- docs/spec.md | 7 +- internal/auth/authz.go | 2 +- internal/auth/authz_roles_test.go | 2 +- internal/mgmtapi/cross_org_test.go | 83 +++++++++ internal/mgmtapi/rpc_fleet.go | 37 +++- .../sql/0053_group_assignment_org.down.sql | 9 + .../sql/0053_group_assignment_org.up.sql | 101 +++++++++++ .../group_assignment_org_migration_test.go | 162 ++++++++++++++++++ internal/store/group_assignment_org_test.go | 120 +++++++++++++ internal/store/queries/clusters.sql | 49 +++++- internal/store/queries/group_assignments.sql | 32 +++- internal/store/sqlc/clusters.sql.go | 57 +++++- internal/store/sqlc/group_assignments.sql.go | 60 +++++-- internal/store/sqlc/models.go | 1 + 16 files changed, 729 insertions(+), 50 deletions(-) create mode 100644 internal/migrations/sql/0053_group_assignment_org.down.sql create mode 100644 internal/migrations/sql/0053_group_assignment_org.up.sql create mode 100644 internal/store/group_assignment_org_migration_test.go create mode 100644 internal/store/group_assignment_org_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 06d26973..46f81fe5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,14 @@ Categories used here: ### Changed +- **Shipped — group access is bound to the org that granted it.** A group assigned to a + collector on its Access tab gives its members viewer access only while the collector's cluster + stays in the org that made the assignment. Claiming a cluster into another org removes the + previous org's assignments on its collectors, unclaiming removes all of them, and the Access tab + lists only the current org's. An assignment with an empty group id is refused. Migration `0053` + removes grants made by a previous owner of a cluster (and those on clusters with no org) and + records each removal in the audit log (`assignment.delete` by `migration:0053`); see + UPGRADING.md. - **Shipped — database query logs omit bound parameters.** When a query fails, the server's log line names the statement, the error and the timing, but no longer lists the values bound to it. The error text itself is logged as before, and some database errors quote the offending diff --git a/deploy/helm/shepherd/UPGRADING.md b/deploy/helm/shepherd/UPGRADING.md index d0eee942..c7ca4427 100644 --- a/deploy/helm/shepherd/UPGRADING.md +++ b/deploy/helm/shepherd/UPGRADING.md @@ -1,13 +1,15 @@ # Upgrading the Shepherd chart -## Unreleased: migration `0052` +## Unreleased + +### Migration `0052`: sessions are stored as digests The session table now keeps a SHA-256 digest of each `shepherd_session` cookie instead of the cookie value. Migration `0052` deletes every existing session rather than converting it, so **everyone signs in again once after the upgrade**. Service-account tokens, agent tokens and collectors are not affected. -### During the rollout +**During the rollout:** Old and new pods do not recognise each other's sessions: an old pod looks a cookie up by its value, a new pod by its digest. While both run, a user whose requests reach the other kind of pod @@ -16,14 +18,53 @@ after the rollout is enough. Until the last old pod is gone, an old pod would al copied out of the session table as a cookie, and the rows old pods write are keyed by the cookie value until they expire (`auth.session_ttl`), so keep the rollout short. -### Rolling back +**Rolling back:** -Run `shepherd migrate down` once **with the new image** (back to `0051`) before `helm rollback`. +Run `shepherd migrate down` **with the new image** until the schema is back at `0051` (once +for `0053`, once for `0052`) before `helm rollback`. This step is required. The down migration deletes every session, so everyone signs in again after the rollback too. Skipping it leaves the digest-keyed rows in place, and an older server looks a cookie up by its value, so it would accept a value copied out of the session table as a session cookie until those rows expire. +### Migration `0053`: group assignments are bound to the granting org + +A group assignment (a collector's Access tab) now belongs to the org that made it and grants +viewer access only while the collector's cluster is in that org. `0053` binds each existing +assignment to its collector's current org, **except** that it removes: + +- assignments on a cluster that has no org, and +- assignments on a cluster whose audit log shows a `cluster.claim` or `cluster.unclaim` after the + assignment was created. The audit log cannot tell a move from a re-claim into the same org, so + a re-claim counts too. Claims made before v0.14.0 were not audited and are not seen. + +Each removal is written to the audit log first: action `assignment.delete`, actor +`migration:0053` (actor type `system`), resource the collector, with `group_id`, +`group_display_name`, `cluster_id`, `current_org_id`, `previous_org_id` (the org whose +`assignment.create` entry granted it, when the log has one), `assignment_created_at` and `reason` +(`org changed` or `cluster has no org`) in the detail. An entry is filed under the granting org, +so that org's admins see it on the Audit page filtered by action `assignment.delete` and actor +`migration`; one whose granting org is unknown is in the application admin's global view only. +From SQL: + +```sql +SELECT at, org_id, resource_id AS collector_id, detail +FROM audit_log +WHERE actor = 'migration:0053' AND action = 'assignment.delete' +ORDER BY resource_id; +``` + +To re-create a grant that is still wanted, an admin of the org that now owns the cluster assigns +the group again from the collector's Access tab. + +During the rolling update a v0.21 pod can still create assignments (the migration binds them to +the collector's current org), but its cluster claim and unclaim leave the previous org's +assignments in place; they grant nothing on an upgraded pod and are removed by the cluster's next +claim or unclaim. + +**Rolling back:** the down migration drops the `org_id` column. It does not restore the +assignments `0053` removed; their audit entries remain. + ## 0.24.x → 0.25.0 Shepherd v0.21.0 (chart 0.25.1 is v0.21.1, which adds migration `0051`; upgrade straight to it, diff --git a/docs/spec.md b/docs/spec.md index 0197e555..7f8b63db 100644 --- a/docs/spec.md +++ b/docs/spec.md @@ -325,7 +325,10 @@ collector_instances(id text PRIMARY KEY, -- the remotecfg wire id group_assignments(id, collector_id uuid REFERENCES collectors, group_id text, group_display_name text, -- Entra GUID + cached name + org_id uuid NOT NULL REFERENCES orgs, -- the granting org (0053) UNIQUE(collector_id, group_id)) + -- grants only while the collector's cluster is in org_id; every + -- claim/unclaim deletes the cluster's rows the new org did not make destinations(id, org_id uuid REFERENCES orgs, name text, type text CHECK (type IN ('prometheus','loki','otlp')), @@ -517,7 +520,9 @@ The resulting roles are the same either way: - **Viewer** of org O: member of `O.viewer_group_id` (`reader_group_id` until migration 0037), or `org_members.role = 'viewer'`, or holds a higher role. Also reached without either: for any collector C, a member of any group in - `group_assignments` for C is a viewer of C's org, and a member of any team of + `group_assignments` for C is a viewer of C's org while that org is the one + that made the assignment (`group_assignments.org_id`; a cluster's change of + org removes the previous org's assignments), and a member of any team of O — local or IdP-group-backed — is a viewer of O, on top of whatever `org_members.role` (if any) separately grants. A viewer reads; it writes nothing org-wide — no destination, route, wizard or unowned pipeline. diff --git a/internal/auth/authz.go b/internal/auth/authz.go index ed1abebb..99814542 100644 --- a/internal/auth/authz.go +++ b/internal/auth/authz.go @@ -228,7 +228,7 @@ func authorizeOrgAccess(ctx context.Context, st *store.Store, sess *Session, org // viewer floor in every org to anyone holding one assignment in any // org -- see the query's own comment. collectorIDs, err := st.Queries.ListCollectorIDsByGroupMembershipInOrg(ctx, - sqlc.ListCollectorIDsByGroupMembershipInOrgParams{Column1: sess.GroupIDs, OrgID: org.ID}) + sqlc.ListCollectorIDsByGroupMembershipInOrgParams{GroupIds: sess.GroupIDs, OrgID: org.ID}) if err == nil && len(collectorIDs) > 0 { return nil } diff --git a/internal/auth/authz_roles_test.go b/internal/auth/authz_roles_test.go index 85da9cb1..8fd2bccd 100644 --- a/internal/auth/authz_roles_test.go +++ b/internal/auth/authz_roles_test.go @@ -415,7 +415,7 @@ var _ = Describe("Org role ladder", Label("integration"), func() { }) Expect(err).NotTo(HaveOccurred()) _, err = st.Queries.CreateGroupAssignment(ctx, sqlc.CreateGroupAssignmentParams{ - CollectorID: collector.ID, GroupID: "grp-assigned", GroupDisplayName: "Assigned", + CollectorID: collector.ID, GroupID: "grp-assigned", GroupDisplayName: "Assigned", OrgID: oid, }) Expect(err).NotTo(HaveOccurred()) diff --git a/internal/mgmtapi/cross_org_test.go b/internal/mgmtapi/cross_org_test.go index b1cf660b..15185617 100644 --- a/internal/mgmtapi/cross_org_test.go +++ b/internal/mgmtapi/cross_org_test.go @@ -139,6 +139,89 @@ var _ = Describe("cross-org isolation of by-id handlers", Label("integration"), Expect(code).NotTo(Equal(http.StatusOK), "org A assigned a group to org B's collector: %s", body) }) + // A group assignment is bound to the org that granted it. Before 0053 the + // viewer-floor check read only the collector's current org, so when a + // cluster moved, the previous owner's grants moved with it and gave that + // org's group members viewer access inside the new owner. + Describe("group assignments when a cluster changes org", func() { + appAdmin := func() *http.Cookie { + id := fmt.Sprintf("cross-org-app-admin-%d", time.Now().UnixNano()) + _, err := st.Queries.CreateSession(ctx, sqlc.CreateSessionParams{ + ID: id, UserOid: "app-admin", Email: "root@example.com", DisplayName: "Root", + GroupIds: []byte(`[]`), IsAppAdmin: true, + ExpiresAt: pgtype.Timestamptz{Time: time.Now().Add(time.Hour), Valid: true}, + Source: "test", + }) + Expect(err).NotTo(HaveOccurred()) + return &http.Cookie{Name: "shepherd_session", Value: id} + } + + // grantInA claims a fresh cluster into org A and has A's admin grant + // group "a-readers" access to its collector over the RPC. + grantInA := func(name string) { + cluster, err := st.Queries.UpsertCluster(ctx, name) + Expect(err).NotTo(HaveOccurred()) + Expect(st.Queries.ClaimCluster(ctx, sqlc.ClaimClusterParams{ID: cluster.ID, OrgID: orgA})).To(Succeed()) + coll, err := st.Queries.UpsertCollector(ctx, sqlc.UpsertCollectorParams{ClusterID: cluster.ID, Role: "metrics"}) + Expect(err).NotTo(HaveOccurred()) + code, body := post("/shepherd.mgmt.v1.FleetService/CreateAssignment", + map[string]any{"orgId": orgA.String(), "collectorId": coll.ID.String(), "groupId": "a-readers"}, + sessionIn("a-admin")) + Expect(code).To(Equal(http.StatusOK), body) + } + + viewerCode := func(org pgtype.UUID) int { + code, _ := post("/shepherd.mgmt.v1.FleetService/ListCollectors", + map[string]any{"orgId": org.String()}, sessionIn("a-readers")) + return code + } + + It("keeps working within the org that granted it", func() { + grantInA("ga-same-org") + Expect(viewerCode(orgA)).To(Equal(http.StatusOK)) + Expect(viewerCode(orgB)).To(Equal(http.StatusForbidden)) + }) + + It("gives the previous org's group no access after the cluster is claimed by another org", func() { + grantInA("ga-moved") + Expect(viewerCode(orgA)).To(Equal(http.StatusOK)) + + code, body := post("/shepherd.mgmt.v1.AdminService/ClaimCluster", + map[string]any{"cluster": "ga-moved", "orgId": orgB.String()}, appAdmin()) + Expect(code).To(Equal(http.StatusOK), body) + + Expect(viewerCode(orgB)).To(Equal(http.StatusForbidden), + "org A's grant carried over into org B with the cluster") + }) + + It("gives the previous org's group no access after an unclaim and a claim by another org", func() { + grantInA("ga-reclaimed") + admin := appAdmin() + code, body := post("/shepherd.mgmt.v1.AdminService/UnclaimCluster", + map[string]any{"cluster": "ga-reclaimed"}, admin) + Expect(code).To(Equal(http.StatusOK), body) + code, body = post("/shepherd.mgmt.v1.AdminService/ClaimCluster", + map[string]any{"cluster": "ga-reclaimed", "orgId": orgB.String()}, admin) + Expect(code).To(Equal(http.StatusOK), body) + + Expect(viewerCode(orgB)).To(Equal(http.StatusForbidden), + "org A's grant survived the unclaim and reached org B") + }) + + It("refuses an assignment with an empty group id", func() { + cluster, err := st.Queries.UpsertCluster(ctx, "ga-empty-group") + Expect(err).NotTo(HaveOccurred()) + Expect(st.Queries.ClaimCluster(ctx, sqlc.ClaimClusterParams{ID: cluster.ID, OrgID: orgA})).To(Succeed()) + coll, err := st.Queries.UpsertCollector(ctx, sqlc.UpsertCollectorParams{ClusterID: cluster.ID, Role: "metrics"}) + Expect(err).NotTo(HaveOccurred()) + code, body := post("/shepherd.mgmt.v1.FleetService/CreateAssignment", + map[string]any{"orgId": orgA.String(), "collectorId": coll.ID.String(), "groupId": ""}, + sessionIn("a-admin")) + Expect(code).To(Equal(http.StatusBadRequest), body) + Expect(body).To(ContainSubstring("invalid_argument")) + }) + }) + It("refuses to delete another org's git credential", func() { cred, err := st.Queries.CreateGitCredential(ctx, sqlc.CreateGitCredentialParams{ OrgID: orgB, Name: "b-cred", Kind: "pat", diff --git a/internal/mgmtapi/rpc_fleet.go b/internal/mgmtapi/rpc_fleet.go index fac4d3b0..e6b57952 100644 --- a/internal/mgmtapi/rpc_fleet.go +++ b/internal/mgmtapi/rpc_fleet.go @@ -610,7 +610,12 @@ func (s *FleetService) ListAssignments(ctx context.Context, req *connect.Request if err != nil { return nil, err } - rows, err := s.store.Queries.ListGroupAssignmentsByCollector(ctx, id) + // loadOwnedCollector has already parsed and matched org_id. + orgID, _ := parseUUID(req.Msg.GetOrgId()) + rows, err := s.store.Queries.ListGroupAssignmentsByCollector(ctx, sqlc.ListGroupAssignmentsByCollectorParams{ + CollectorID: id, + OrgID: orgID, + }) if err != nil { return nil, connect.NewError(connect.CodeInternal, errors.New("failed to list assignments")) } @@ -627,26 +632,41 @@ func (s *FleetService) ListAssignments(ctx context.Context, req *connect.Request return connect.NewResponse(&mgmtv1.ListAssignmentsResponse{Items: items, Total: int32(len(items))}), nil //nolint:gosec // assignment counts per collector never approach int32 overflow } -// CreateAssignment assigns a group to a collector. +// CreateAssignment assigns a group to a collector. The grant is bound to the +// org the request is scoped to (group_assignments.org_id, 0053): it grants +// access only while the collector stays in that org, and a change of the +// cluster's org removes it. func (s *FleetService) CreateAssignment(ctx context.Context, req *connect.Request[mgmtv1.CreateAssignmentRequest]) (*connect.Response[mgmtv1.CreateAssignmentResponse], error) { if err := requireWriteAuthorized(ctx); err != nil { return nil, err } + // An empty group id matches no session's groups, so the row could only + // ever be dead weight on the Access tab; hasGroup in internal/auth treats + // "" as "no group configured" for the same reason. + if strings.TrimSpace(req.Msg.GetGroupId()) == "" { + return nil, connect.NewError(connect.CodeInvalidArgument, errors.New("group_id is required")) + } id, err := s.loadOwnedCollector(ctx, req.Msg.GetOrgId(), req.Msg.GetCollectorId()) if err != nil { return nil, err } + // loadOwnedCollector has already parsed and matched org_id. + orgID, _ := parseUUID(req.Msg.GetOrgId()) a, err := s.store.Queries.CreateGroupAssignment(ctx, sqlc.CreateGroupAssignmentParams{ CollectorID: id, GroupID: req.Msg.GetGroupId(), GroupDisplayName: req.Msg.GetGroupDisplayName(), + OrgID: orgID, }) + if errors.Is(err, pgx.ErrNoRows) { + // The cluster left this org between the ownership check and the + // insert, which only writes while the collector is in orgID. + return nil, connect.NewError(connect.CodeNotFound, errors.New("collector not found")) + } if err != nil { return nil, connect.NewError(connect.CodeInternal, errors.New("failed to create assignment")) } - if orgID, err := scanUUID(req.Msg.GetOrgId()); err == nil { - auditLogDetail(ctx, s.store, actorFromCtx(ctx), "user", orgID, "assignment.create", "collector", id.String(), map[string]string{"group_id": a.GroupID}) - } + auditLogDetail(ctx, s.store, actorFromCtx(ctx), "user", orgID, "assignment.create", "collector", id.String(), map[string]string{"group_id": a.GroupID}) return connect.NewResponse(&mgmtv1.CreateAssignmentResponse{Id: a.ID.String(), GroupId: a.GroupID}), nil } @@ -659,15 +679,16 @@ func (s *FleetService) DeleteAssignment(ctx context.Context, req *connect.Reques if err != nil { return nil, err } + // loadOwnedCollector has already parsed and matched org_id. + orgID, _ := parseUUID(req.Msg.GetOrgId()) if err := s.store.Queries.DeleteGroupAssignment(ctx, sqlc.DeleteGroupAssignmentParams{ CollectorID: collID, GroupID: req.Msg.GetGroupId(), + OrgID: orgID, }); err != nil { return nil, connect.NewError(connect.CodeInternal, errors.New("failed to delete assignment")) } - if orgID, err := scanUUID(req.Msg.GetOrgId()); err == nil { - auditLogDetail(ctx, s.store, actorFromCtx(ctx), "user", orgID, "assignment.delete", "collector", collID.String(), map[string]string{"group_id": req.Msg.GetGroupId()}) - } + auditLogDetail(ctx, s.store, actorFromCtx(ctx), "user", orgID, "assignment.delete", "collector", collID.String(), map[string]string{"group_id": req.Msg.GetGroupId()}) return connect.NewResponse(&mgmtv1.DeleteAssignmentResponse{}), nil } diff --git a/internal/migrations/sql/0053_group_assignment_org.down.sql b/internal/migrations/sql/0053_group_assignment_org.down.sql new file mode 100644 index 00000000..ae2bcf3f --- /dev/null +++ b/internal/migrations/sql/0053_group_assignment_org.down.sql @@ -0,0 +1,9 @@ +-- 0053_group_assignment_org.down.sql +-- Reverts 0053_group_assignment_org.up.sql by dropping the org binding. The +-- grants 0053 deleted are NOT restored (their audit_log entries, actor +-- 'migration:0053', remain the record of them); re-create any that are still +-- wanted from the collector's Access tab. +DROP TRIGGER IF EXISTS group_assignments_default_org ON group_assignments; +DROP FUNCTION IF EXISTS group_assignments_default_org(); +DROP INDEX IF EXISTS idx_group_assignments_org_group; +ALTER TABLE group_assignments DROP COLUMN org_id; diff --git a/internal/migrations/sql/0053_group_assignment_org.up.sql b/internal/migrations/sql/0053_group_assignment_org.up.sql new file mode 100644 index 00000000..09a54992 --- /dev/null +++ b/internal/migrations/sql/0053_group_assignment_org.up.sql @@ -0,0 +1,101 @@ +-- 0053_group_assignment_org.up.sql +-- Binds every group assignment to the org that granted it. Before 0053 a row +-- carried only (collector_id, group_id), and access checks read the +-- collector's CURRENT org, so a grant made while a cluster belonged to one org +-- kept working after the cluster moved to another. From 0053 a grant counts +-- only while group_assignments.org_id is the collector's current org, and +-- every path that changes a cluster's org removes the grants the new org did +-- not make (internal/store/queries/clusters.sql). +-- +-- Backfill (hybrid): a row is bound to its collector's current org unless +-- * the collector's cluster has no org, or +-- * the audit log records a cluster.claim or cluster.unclaim of that +-- cluster after the row was created (the only actions that change a +-- cluster's org and are audited: AdminService ClaimCluster/UnclaimCluster; +-- agent auto-claim only binds an UNclaimed cluster, which has no grants +-- to carry). A re-claim into the same org also counts: the audit log +-- cannot tell it apart from a move, and a removed grant can be re-created +-- while a carried-over one cannot be told apart from a legitimate one. +-- Unbound rows are recorded in the audit log, then deleted. Each audit row is +-- written as the system actor 'migration:0053' with the shape +-- FleetService.DeleteAssignment writes (action assignment.delete, resource +-- the collector, detail.group_id), plus the reason and, when the log knows +-- it, the org that granted the row (its latest assignment.create entry); the +-- row is filed under that org so its admins see it. +ALTER TABLE group_assignments ADD COLUMN org_id uuid REFERENCES orgs(id); + +UPDATE group_assignments ga +SET org_id = cl.org_id +FROM collectors c +JOIN clusters cl ON cl.id = c.cluster_id +WHERE c.id = ga.collector_id + AND cl.org_id IS NOT NULL + AND NOT EXISTS ( + SELECT 1 FROM audit_log a + WHERE a.resource_type = 'cluster' + AND a.resource_id = cl.id::text + AND a.action IN ('cluster.claim', 'cluster.unclaim') + AND a.at > ga.created_at + ); + +INSERT INTO audit_log (actor, actor_type, org_id, action, resource_type, resource_id, detail) +SELECT 'migration:0053', + 'system', + granted.org_id, + 'assignment.delete', + 'collector', + ga.collector_id::text, + jsonb_build_object( + 'group_id', ga.group_id, + 'group_display_name', ga.group_display_name, + 'cluster_id', c.cluster_id::text, + 'previous_org_id', granted.org_id::text, + 'current_org_id', cl.org_id::text, + 'assignment_created_at', ga.created_at, + 'reason', CASE WHEN cl.org_id IS NULL THEN 'cluster has no org' ELSE 'org changed' END + ) +FROM group_assignments ga +JOIN collectors c ON c.id = ga.collector_id +JOIN clusters cl ON cl.id = c.cluster_id +LEFT JOIN LATERAL ( + SELECT a.org_id + FROM audit_log a + WHERE a.action = 'assignment.create' + AND a.resource_type = 'collector' + AND a.resource_id = ga.collector_id::text + AND a.detail ->> 'group_id' = ga.group_id + ORDER BY a.at DESC, a.id DESC + LIMIT 1 +) granted ON true +WHERE ga.org_id IS NULL; + +DELETE FROM group_assignments WHERE org_id IS NULL; + +ALTER TABLE group_assignments ALTER COLUMN org_id SET NOT NULL; + +-- The viewer-floor check (ListCollectorIDsByGroupMembershipInOrg) filters on +-- (org_id, group_id). +CREATE INDEX idx_group_assignments_org_group ON group_assignments (org_id, group_id); + +-- Rolling-update and rollback compatibility: a v0.21 binary's +-- CreateGroupAssignment names no org_id, and NOT NULL would refuse it. That +-- binary has just checked the collector belongs to the caller's org, so an +-- insert without an org is bound to the collector's current one. A collector +-- with no org still gets no row (NOT NULL refuses it). New binaries always +-- name the org, so this never fires for them. +CREATE FUNCTION group_assignments_default_org() RETURNS trigger +LANGUAGE plpgsql AS $$ +BEGIN + IF NEW.org_id IS NULL THEN + SELECT cl.org_id INTO NEW.org_id + FROM collectors c + JOIN clusters cl ON cl.id = c.cluster_id + WHERE c.id = NEW.collector_id; + END IF; + RETURN NEW; +END; +$$; + +CREATE TRIGGER group_assignments_default_org + BEFORE INSERT ON group_assignments + FOR EACH ROW EXECUTE FUNCTION group_assignments_default_org(); diff --git a/internal/store/group_assignment_org_migration_test.go b/internal/store/group_assignment_org_migration_test.go new file mode 100644 index 00000000..44653e5a --- /dev/null +++ b/internal/store/group_assignment_org_migration_test.go @@ -0,0 +1,162 @@ +package store_test + +import ( + "context" + "encoding/json" + + "github.com/jackc/pgx/v5/pgxpool" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "shepherd/internal/store" +) + +// Migration 0053_group_assignment_org: a group assignment is bound to the org +// that granted it. The backfill binds a row to its collector's current org +// unless the cluster has no org, or the audit log records a cluster.claim / +// cluster.unclaim of the cluster after the row was created; those rows are +// written to the audit log, then deleted. +var _ = Describe("Migration: 0053_group_assignment_org", Label("integration"), func() { + var ( + db *pgxpool.Pool + url string + ) + + reopen := func(ctx context.Context) { + var err error + db, err = pgxpool.New(ctx, url) + Expect(err).NotTo(HaveOccurred()) + } + + // RAW-SQL-OK: migration specs drive the schema directly. + scalar := func(ctx context.Context, q string, args ...any) string { + var s string + Expect(db.QueryRow(ctx, q, args...).Scan(&s)).To(Succeed(), q) + return s + } + + var ( + orgA, orgB string + collStill, collMoved, collNo string + collEarly string + ) + + BeforeEach(func(ctx context.Context) { + url = sharedPG.IsolatedDB(ctx, GinkgoTB()) + Expect(store.MigrateTo(ctx, url, 51)).To(Succeed()) + reopen(ctx) + + orgA = scalar(ctx, `INSERT INTO orgs (name, display_name, admin_group_id) VALUES ('m53-a', 'A', 'a-admin') RETURNING id::text`) + orgB = scalar(ctx, `INSERT INTO orgs (name, display_name, admin_group_id) VALUES ('m53-b', 'B', 'b-admin') RETURNING id::text`) + + cluster := func(name string, org any) string { + return scalar(ctx, `INSERT INTO clusters (name, org_id) VALUES ($1, $2) RETURNING id::text`, name, org) + } + collector := func(clusterID string) string { + return scalar(ctx, `INSERT INTO collectors (cluster_id, role) VALUES ($1, 'metrics') RETURNING id::text`, clusterID) + } + grant := func(collectorID, group, at string) { + _, err := db.Exec(ctx, `INSERT INTO group_assignments (collector_id, group_id, group_display_name, created_at) + VALUES ($1, $2, $2, $3::timestamptz)`, collectorID, group, at) + Expect(err).NotTo(HaveOccurred()) + } + audit := func(org any, action, resType, resID, detail, at string) { + _, err := db.Exec(ctx, `INSERT INTO audit_log (at, actor, actor_type, org_id, action, resource_type, resource_id, detail) + VALUES ($1::timestamptz, 'admin@example.com', 'user', $2, $3, $4, $5, $6::jsonb)`, + at, org, action, resType, resID, detail) + Expect(err).NotTo(HaveOccurred()) + } + + // (i) never moved: claimed into A before the grant, nothing after. + still := cluster("m53-still", orgA) + collStill = collector(still) + audit(orgA, "cluster.claim", "cluster", still, `{}`, "2026-01-01T00:00:00Z") + grant(collStill, "grp-still", "2026-02-01T00:00:00Z") + + // (ii) granted while in B, then claimed into A: the claim is audited + // after the row was created. + moved := cluster("m53-moved", orgA) + collMoved = collector(moved) + audit(orgB, "assignment.create", "collector", collMoved, `{"group_id":"grp-moved"}`, "2026-02-01T00:00:00Z") + grant(collMoved, "grp-moved", "2026-02-01T00:00:00Z") + audit(orgA, "cluster.claim", "cluster", moved, `{}`, "2026-03-01T00:00:00Z") + + // (iii) the cluster has no org. + none := cluster("m53-none", nil) + collNo = collector(none) + grant(collNo, "grp-none", "2026-02-01T00:00:00Z") + + // An unclaim BEFORE the grant is not a change after it. + early := cluster("m53-early", orgB) + collEarly = collector(early) + audit(orgA, "cluster.unclaim", "cluster", early, `{}`, "2026-01-01T00:00:00Z") + grant(collEarly, "grp-early", "2026-02-01T00:00:00Z") + + db.Close() + Expect(store.MigrateUp(ctx, url)).To(Succeed()) + reopen(ctx) + DeferCleanup(func() { db.Close() }) + }) + + It("binds rows on clusters whose org has not changed since the grant", func(ctx context.Context) { + Expect(scalar(ctx, `SELECT org_id::text FROM group_assignments WHERE collector_id = $1`, collStill)).To(Equal(orgA)) + Expect(scalar(ctx, `SELECT org_id::text FROM group_assignments WHERE collector_id = $1`, collEarly)).To(Equal(orgB)) + }) + + It("deletes rows whose cluster changed org after the grant, or has no org", func(ctx context.Context) { + for _, c := range []string{collMoved, collNo} { + Expect(scalar(ctx, `SELECT count(*)::text FROM group_assignments WHERE collector_id = $1`, c)).To(Equal("0"), c) + } + Expect(scalar(ctx, `SELECT count(*)::text FROM group_assignments`)).To(Equal("2")) + }) + + It("records one audit entry per deleted row", func(ctx context.Context) { + rows, err := db.Query(ctx, `SELECT coalesce(org_id::text, ''), resource_id, detail FROM audit_log + WHERE actor = 'migration:0053' AND actor_type = 'system' AND action = 'assignment.delete' + AND resource_type = 'collector' ORDER BY resource_id`) + Expect(err).NotTo(HaveOccurred()) + defer rows.Close() + got := map[string]map[string]any{} + orgOf := map[string]string{} + for rows.Next() { + var org, res string + var raw []byte + Expect(rows.Scan(&org, &res, &raw)).To(Succeed()) + d := map[string]any{} + Expect(json.Unmarshal(raw, &d)).To(Succeed()) + got[res] = d + orgOf[res] = org + } + Expect(rows.Err()).NotTo(HaveOccurred()) + Expect(got).To(HaveLen(2)) + + Expect(got[collMoved]).To(HaveKeyWithValue("group_id", "grp-moved")) + Expect(got[collMoved]).To(HaveKeyWithValue("reason", "org changed")) + Expect(got[collMoved]).To(HaveKeyWithValue("previous_org_id", orgB)) + Expect(got[collMoved]).To(HaveKeyWithValue("current_org_id", orgA)) + Expect(orgOf[collMoved]).To(Equal(orgB), "filed under the org that granted it") + + Expect(got[collNo]).To(HaveKeyWithValue("group_id", "grp-none")) + Expect(got[collNo]).To(HaveKeyWithValue("reason", "cluster has no org")) + Expect(got[collNo]).To(HaveKeyWithValue("previous_org_id", BeNil())) + Expect(orgOf[collNo]).To(BeEmpty(), "no granting org is known, so the entry is platform-level") + }) + + It("binds a v0.21 binary's insert (no org_id) to the collector's org, and refuses one with no org", func(ctx context.Context) { + _, err := db.Exec(ctx, `INSERT INTO group_assignments (collector_id, group_id, group_display_name) VALUES ($1, 'g-old', 'g-old')`, collStill) + Expect(err).NotTo(HaveOccurred()) + Expect(scalar(ctx, `SELECT org_id::text FROM group_assignments WHERE group_id = 'g-old'`)).To(Equal(orgA)) + + _, err = db.Exec(ctx, `INSERT INTO group_assignments (collector_id, group_id, group_display_name) VALUES ($1, 'g-none', 'g-none')`, collNo) + Expect(err).To(MatchError(ContainSubstring("not-null"))) + }) + + It("drops the column on the down migration and keeps the bound rows", func(ctx context.Context) { + db.Close() + Expect(store.MigrateTo(ctx, url, 51)).To(Succeed()) + reopen(ctx) + Expect(scalar(ctx, `SELECT count(*)::text FROM information_schema.columns + WHERE table_name = 'group_assignments' AND column_name = 'org_id'`)).To(Equal("0")) + Expect(scalar(ctx, `SELECT count(*)::text FROM group_assignments`)).To(Equal("2")) + }) +}) diff --git a/internal/store/group_assignment_org_test.go b/internal/store/group_assignment_org_test.go new file mode 100644 index 00000000..6dfc100f --- /dev/null +++ b/internal/store/group_assignment_org_test.go @@ -0,0 +1,120 @@ +package store_test + +import ( + "context" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgtype" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "shepherd/internal/config" + "shepherd/internal/store" + "shepherd/internal/store/sqlc" +) + +// A group assignment is bound to the org that granted it (0053), and every +// query that changes a cluster's org removes, in the same statement, the +// grants on its collectors that the new org did not make. These specs drive +// the queries every caller (AdminService ClaimCluster/UnclaimCluster, agent +// auto-claim, the dev seed) goes through. +var _ = Describe("group assignments across cluster org changes", Label("integration"), func() { + var ( + ctx context.Context + cancel context.CancelFunc + st *store.Store + orgA, orgB pgtype.UUID + cluster sqlc.Cluster + collector sqlc.Collector + ) + + BeforeEach(func() { + ctx, cancel = context.WithCancel(context.Background()) + dbURL := sharedPG.IsolatedDB(ctx, GinkgoTB()) + Expect(store.MigrateUp(ctx, dbURL)).To(Succeed()) + var err error + st, err = store.New(ctx, &config.DatabaseConfig{URL: dbURL, MaxConns: 5}) + Expect(err).NotTo(HaveOccurred()) + + a, err := st.Queries.CreateOrg(ctx, sqlc.CreateOrgParams{Name: "ga-a", DisplayName: "A", AdminGroupID: "a-admin"}) + Expect(err).NotTo(HaveOccurred()) + b, err := st.Queries.CreateOrg(ctx, sqlc.CreateOrgParams{Name: "ga-b", DisplayName: "B", AdminGroupID: "b-admin"}) + Expect(err).NotTo(HaveOccurred()) + orgA, orgB = a.ID, b.ID + + cluster, err = st.Queries.UpsertCluster(ctx, "ga-cluster") + Expect(err).NotTo(HaveOccurred()) + Expect(st.Queries.ClaimCluster(ctx, sqlc.ClaimClusterParams{ID: cluster.ID, OrgID: orgA})).To(Succeed()) + collector, err = st.Queries.UpsertCollector(ctx, sqlc.UpsertCollectorParams{ClusterID: cluster.ID, Role: "metrics"}) + Expect(err).NotTo(HaveOccurred()) + _, err = st.Queries.CreateGroupAssignment(ctx, sqlc.CreateGroupAssignmentParams{ + CollectorID: collector.ID, GroupID: "grp-a", GroupDisplayName: "A readers", OrgID: orgA, + }) + Expect(err).NotTo(HaveOccurred()) + }) + + AfterEach(func() { + st.Close() + cancel() + }) + + grantsIn := func(org pgtype.UUID) []pgtype.UUID { + ids, err := st.Queries.ListCollectorIDsByGroupMembershipInOrg(ctx, sqlc.ListCollectorIDsByGroupMembershipInOrgParams{ + GroupIds: []string{"grp-a"}, OrgID: org, + }) + Expect(err).NotTo(HaveOccurred()) + return ids + } + rowCount := func() int { + rows, err := st.Queries.ListGroupAssignmentsByCollector(ctx, sqlc.ListGroupAssignmentsByCollectorParams{CollectorID: collector.ID, OrgID: orgA}) + Expect(err).NotTo(HaveOccurred()) + rowsB, err := st.Queries.ListGroupAssignmentsByCollector(ctx, sqlc.ListGroupAssignmentsByCollectorParams{CollectorID: collector.ID, OrgID: orgB}) + Expect(err).NotTo(HaveOccurred()) + return len(rows) + len(rowsB) + } + + It("grants within the org that made the grant", func() { + Expect(grantsIn(orgA)).To(ConsistOf(collector.ID)) + Expect(grantsIn(orgB)).To(BeEmpty()) + }) + + It("ClaimCluster into another org removes the previous org's grants", func() { + Expect(st.Queries.ClaimCluster(ctx, sqlc.ClaimClusterParams{ID: cluster.ID, OrgID: orgB})).To(Succeed()) + Expect(grantsIn(orgB)).To(BeEmpty()) + Expect(rowCount()).To(BeZero()) + }) + + It("ClaimCluster into the same org keeps its grants", func() { + Expect(st.Queries.ClaimCluster(ctx, sqlc.ClaimClusterParams{ID: cluster.ID, OrgID: orgA})).To(Succeed()) + Expect(grantsIn(orgA)).To(ConsistOf(collector.ID)) + }) + + It("UnclaimCluster removes every grant, so a later claim inherits none", func() { + Expect(st.Queries.UnclaimCluster(ctx, cluster.ID)).To(Succeed()) + Expect(rowCount()).To(BeZero()) + claimed, err := st.Queries.ClaimClusterForOrg(ctx, sqlc.ClaimClusterForOrgParams{ID: cluster.ID, OrgID: orgB}) + Expect(err).NotTo(HaveOccurred()) + Expect(claimed).To(Equal(orgB)) + Expect(grantsIn(orgB)).To(BeEmpty()) + }) + + It("a refused ClaimClusterForOrg leaves the owner's grants alone", func() { + _, err := st.Queries.ClaimClusterForOrg(ctx, sqlc.ClaimClusterForOrgParams{ID: cluster.ID, OrgID: orgB}) + Expect(err).To(MatchError(pgx.ErrNoRows)) + Expect(grantsIn(orgA)).To(ConsistOf(collector.ID)) + }) + + It("a confirming ClaimClusterForOrg keeps the owner's grants", func() { + _, err := st.Queries.ClaimClusterForOrg(ctx, sqlc.ClaimClusterForOrgParams{ID: cluster.ID, OrgID: orgA}) + Expect(err).NotTo(HaveOccurred()) + Expect(grantsIn(orgA)).To(ConsistOf(collector.ID)) + }) + + It("CreateGroupAssignment writes nothing for a collector outside the named org", func() { + _, err := st.Queries.CreateGroupAssignment(ctx, sqlc.CreateGroupAssignmentParams{ + CollectorID: collector.ID, GroupID: "grp-b", GroupDisplayName: "B readers", OrgID: orgB, + }) + Expect(err).To(MatchError(pgx.ErrNoRows)) + Expect(rowCount()).To(Equal(1)) + }) +}) diff --git a/internal/store/queries/clusters.sql b/internal/store/queries/clusters.sql index d3afd684..592365f3 100644 --- a/internal/store/queries/clusters.sql +++ b/internal/store/queries/clusters.sql @@ -17,10 +17,36 @@ SELECT * FROM clusters WHERE org_id IS NULL ORDER BY created_at; SELECT * FROM clusters ORDER BY created_at; -- name: ClaimCluster :exec -UPDATE clusters SET org_id = $2, updated_at = now() WHERE id = $1; +-- Every statement that changes a cluster's org also removes, in the same +-- statement (so in the same transaction as the change), the group +-- assignments on the cluster's collectors that the new org did not make +-- (0053). A grant is bound to the org that made it; one left behind would +-- keep its group's members' viewer access inside the new org. +WITH claimed AS ( + UPDATE clusters SET org_id = sqlc.arg('org_id'), updated_at = now() WHERE clusters.id = sqlc.arg('id') + RETURNING clusters.id, clusters.org_id +), revoked AS ( + DELETE FROM group_assignments ga + USING collectors c, claimed + WHERE c.id = ga.collector_id + AND c.cluster_id = claimed.id + AND ga.org_id IS DISTINCT FROM claimed.org_id +) +SELECT count(*) FROM claimed; -- name: UnclaimCluster :exec -UPDATE clusters SET org_id = NULL, updated_at = now() WHERE id = $1; +-- Unclaiming removes every group assignment on the cluster's collectors: an +-- unclaimed cluster belongs to no org, so no grant on it can be the owner's. +WITH unclaimed AS ( + UPDATE clusters SET org_id = NULL, updated_at = now() WHERE clusters.id = sqlc.arg('id') + RETURNING clusters.id +), revoked AS ( + DELETE FROM group_assignments ga + USING collectors c, unclaimed + WHERE c.id = ga.collector_id + AND c.cluster_id = unclaimed.id +) +SELECT count(*) FROM unclaimed; -- name: ClaimClusterForOrg :one -- Auto-claim for an OIDC-authenticated collector (agent OIDC, resolution @@ -29,6 +55,19 @@ UPDATE clusters SET org_id = NULL, updated_at = now() WHERE id = $1; -- matches no row and returns pgx.ErrNoRows, which the caller turns into a -- cross-org PermissionDenied — the token can never silently reassign another -- org's cluster. -UPDATE clusters SET org_id = $2, updated_at = now() -WHERE id = $1 AND (org_id IS NULL OR org_id = $2) -RETURNING org_id; +-- +-- Like ClaimCluster, a successful claim removes the cluster's group +-- assignments the claiming org did not make; a refused claim (no row) removes +-- nothing, because the delete joins on the UPDATE's RETURNING. +WITH claimed AS ( + UPDATE clusters SET org_id = sqlc.arg('org_id'), updated_at = now() + WHERE clusters.id = sqlc.arg('id') AND (clusters.org_id IS NULL OR clusters.org_id = sqlc.arg('org_id')) + RETURNING clusters.id, clusters.org_id +), revoked AS ( + DELETE FROM group_assignments ga + USING collectors c, claimed + WHERE c.id = ga.collector_id + AND c.cluster_id = claimed.id + AND ga.org_id IS DISTINCT FROM claimed.org_id +) +SELECT claimed.org_id FROM claimed; diff --git a/internal/store/queries/group_assignments.sql b/internal/store/queries/group_assignments.sql index 71ecfb19..fc8a3a4a 100644 --- a/internal/store/queries/group_assignments.sql +++ b/internal/store/queries/group_assignments.sql @@ -1,14 +1,26 @@ -- name: CreateGroupAssignment :one -INSERT INTO group_assignments (collector_id, group_id, group_display_name) -VALUES ($1, $2, $3) -ON CONFLICT (collector_id, group_id) DO UPDATE SET group_display_name = EXCLUDED.group_display_name +-- A grant belongs to the org that made it (0053). The insert happens only +-- while the collector's cluster is in that org, so a cluster that moved +-- between the caller's ownership check and this statement gets no row (the +-- caller sees pgx.ErrNoRows) rather than a grant bound to an org that no +-- longer owns it. +INSERT INTO group_assignments (collector_id, group_id, group_display_name, org_id) +SELECT c.id, sqlc.arg('group_id')::text, sqlc.arg('group_display_name')::text, cl.org_id +FROM collectors c +JOIN clusters cl ON cl.id = c.cluster_id +WHERE c.id = sqlc.arg('collector_id') AND cl.org_id = sqlc.arg('org_id') +ON CONFLICT (collector_id, group_id) DO UPDATE +SET group_display_name = EXCLUDED.group_display_name, + org_id = EXCLUDED.org_id, + updated_at = now() RETURNING *; -- name: DeleteGroupAssignment :exec -DELETE FROM group_assignments WHERE collector_id = $1 AND group_id = $2; +DELETE FROM group_assignments WHERE collector_id = $1 AND group_id = $2 AND org_id = $3; -- name: ListGroupAssignmentsByCollector :many -SELECT * FROM group_assignments WHERE collector_id = $1 ORDER BY group_display_name; +-- Org-scoped: the Access tab lists only the grants the viewing org made. +SELECT * FROM group_assignments WHERE collector_id = $1 AND org_id = $2 ORDER BY group_display_name; -- name: ListCollectorIDsByGroupMembershipInOrg :many -- Used by internal/auth.authorizeOrgAccess's collector-assignment fallback. @@ -19,9 +31,15 @@ SELECT * FROM group_assignments WHERE collector_id = $1 ORDER BY group_display_n -- because GetMe computes its org list from group matches and never shows the -- extra orgs. The sibling ListTeamsByOrgAndGroups has always been org-scoped; -- this one was not. +-- +-- Both halves are required: the grant must have been made by this org +-- (ga.org_id, 0053) AND the collector must still be in it. Checking only the +-- collector's current org let a grant made by a cluster's previous owner keep +-- working after the cluster moved. SELECT DISTINCT ga.collector_id FROM group_assignments ga JOIN collectors c ON c.id = ga.collector_id JOIN clusters cl ON cl.id = c.cluster_id -WHERE ga.group_id = ANY($1::text[]) - AND cl.org_id = $2; +WHERE ga.group_id = ANY(sqlc.arg('group_ids')::text[]) + AND ga.org_id = sqlc.arg('org_id') + AND cl.org_id = sqlc.arg('org_id'); diff --git a/internal/store/sqlc/clusters.sql.go b/internal/store/sqlc/clusters.sql.go index 85653bab..793c5bc9 100644 --- a/internal/store/sqlc/clusters.sql.go +++ b/internal/store/sqlc/clusters.sql.go @@ -12,28 +12,52 @@ import ( ) const claimCluster = `-- name: ClaimCluster :exec -UPDATE clusters SET org_id = $2, updated_at = now() WHERE id = $1 +WITH claimed AS ( + UPDATE clusters SET org_id = $1, updated_at = now() WHERE clusters.id = $2 + RETURNING clusters.id, clusters.org_id +), revoked AS ( + DELETE FROM group_assignments ga + USING collectors c, claimed + WHERE c.id = ga.collector_id + AND c.cluster_id = claimed.id + AND ga.org_id IS DISTINCT FROM claimed.org_id +) +SELECT count(*) FROM claimed ` type ClaimClusterParams struct { - ID pgtype.UUID `json:"id"` OrgID pgtype.UUID `json:"org_id"` + ID pgtype.UUID `json:"id"` } +// Every statement that changes a cluster's org also removes, in the same +// statement (so in the same transaction as the change), the group +// assignments on the cluster's collectors that the new org did not make +// (0053). A grant is bound to the org that made it; one left behind would +// keep its group's members' viewer access inside the new org. func (q *Queries) ClaimCluster(ctx context.Context, arg ClaimClusterParams) error { - _, err := q.db.Exec(ctx, claimCluster, arg.ID, arg.OrgID) + _, err := q.db.Exec(ctx, claimCluster, arg.OrgID, arg.ID) return err } const claimClusterForOrg = `-- name: ClaimClusterForOrg :one -UPDATE clusters SET org_id = $2, updated_at = now() -WHERE id = $1 AND (org_id IS NULL OR org_id = $2) -RETURNING org_id +WITH claimed AS ( + UPDATE clusters SET org_id = $1, updated_at = now() + WHERE clusters.id = $2 AND (clusters.org_id IS NULL OR clusters.org_id = $1) + RETURNING clusters.id, clusters.org_id +), revoked AS ( + DELETE FROM group_assignments ga + USING collectors c, claimed + WHERE c.id = ga.collector_id + AND c.cluster_id = claimed.id + AND ga.org_id IS DISTINCT FROM claimed.org_id +) +SELECT claimed.org_id FROM claimed ` type ClaimClusterForOrgParams struct { - ID pgtype.UUID `json:"id"` OrgID pgtype.UUID `json:"org_id"` + ID pgtype.UUID `json:"id"` } // Auto-claim for an OIDC-authenticated collector (agent OIDC, resolution @@ -42,8 +66,12 @@ type ClaimClusterForOrgParams struct { // matches no row and returns pgx.ErrNoRows, which the caller turns into a // cross-org PermissionDenied — the token can never silently reassign another // org's cluster. +// +// Like ClaimCluster, a successful claim removes the cluster's group +// assignments the claiming org did not make; a refused claim (no row) removes +// nothing, because the delete joins on the UPDATE's RETURNING. func (q *Queries) ClaimClusterForOrg(ctx context.Context, arg ClaimClusterForOrgParams) (pgtype.UUID, error) { - row := q.db.QueryRow(ctx, claimClusterForOrg, arg.ID, arg.OrgID) + row := q.db.QueryRow(ctx, claimClusterForOrg, arg.OrgID, arg.ID) var org_id pgtype.UUID err := row.Scan(&org_id) return org_id, err @@ -144,9 +172,20 @@ func (q *Queries) ListUnclaimedClusters(ctx context.Context) ([]Cluster, error) } const unclaimCluster = `-- name: UnclaimCluster :exec -UPDATE clusters SET org_id = NULL, updated_at = now() WHERE id = $1 +WITH unclaimed AS ( + UPDATE clusters SET org_id = NULL, updated_at = now() WHERE clusters.id = $1 + RETURNING clusters.id +), revoked AS ( + DELETE FROM group_assignments ga + USING collectors c, unclaimed + WHERE c.id = ga.collector_id + AND c.cluster_id = unclaimed.id +) +SELECT count(*) FROM unclaimed ` +// Unclaiming removes every group assignment on the cluster's collectors: an +// unclaimed cluster belongs to no org, so no grant on it can be the owner's. func (q *Queries) UnclaimCluster(ctx context.Context, id pgtype.UUID) error { _, err := q.db.Exec(ctx, unclaimCluster, id) return err diff --git a/internal/store/sqlc/group_assignments.sql.go b/internal/store/sqlc/group_assignments.sql.go index 1b9d241c..018ed9d8 100644 --- a/internal/store/sqlc/group_assignments.sql.go +++ b/internal/store/sqlc/group_assignments.sql.go @@ -12,20 +12,37 @@ import ( ) const createGroupAssignment = `-- name: CreateGroupAssignment :one -INSERT INTO group_assignments (collector_id, group_id, group_display_name) -VALUES ($1, $2, $3) -ON CONFLICT (collector_id, group_id) DO UPDATE SET group_display_name = EXCLUDED.group_display_name -RETURNING id, collector_id, group_id, group_display_name, created_at, updated_at +INSERT INTO group_assignments (collector_id, group_id, group_display_name, org_id) +SELECT c.id, $1::text, $2::text, cl.org_id +FROM collectors c +JOIN clusters cl ON cl.id = c.cluster_id +WHERE c.id = $3 AND cl.org_id = $4 +ON CONFLICT (collector_id, group_id) DO UPDATE +SET group_display_name = EXCLUDED.group_display_name, + org_id = EXCLUDED.org_id, + updated_at = now() +RETURNING id, collector_id, group_id, group_display_name, created_at, updated_at, org_id ` type CreateGroupAssignmentParams struct { - CollectorID pgtype.UUID `json:"collector_id"` GroupID string `json:"group_id"` GroupDisplayName string `json:"group_display_name"` + CollectorID pgtype.UUID `json:"collector_id"` + OrgID pgtype.UUID `json:"org_id"` } +// A grant belongs to the org that made it (0053). The insert happens only +// while the collector's cluster is in that org, so a cluster that moved +// between the caller's ownership check and this statement gets no row (the +// caller sees pgx.ErrNoRows) rather than a grant bound to an org that no +// longer owns it. func (q *Queries) CreateGroupAssignment(ctx context.Context, arg CreateGroupAssignmentParams) (GroupAssignment, error) { - row := q.db.QueryRow(ctx, createGroupAssignment, arg.CollectorID, arg.GroupID, arg.GroupDisplayName) + row := q.db.QueryRow(ctx, createGroupAssignment, + arg.GroupID, + arg.GroupDisplayName, + arg.CollectorID, + arg.OrgID, + ) var i GroupAssignment err := row.Scan( &i.ID, @@ -34,21 +51,23 @@ func (q *Queries) CreateGroupAssignment(ctx context.Context, arg CreateGroupAssi &i.GroupDisplayName, &i.CreatedAt, &i.UpdatedAt, + &i.OrgID, ) return i, err } const deleteGroupAssignment = `-- name: DeleteGroupAssignment :exec -DELETE FROM group_assignments WHERE collector_id = $1 AND group_id = $2 +DELETE FROM group_assignments WHERE collector_id = $1 AND group_id = $2 AND org_id = $3 ` type DeleteGroupAssignmentParams struct { CollectorID pgtype.UUID `json:"collector_id"` GroupID string `json:"group_id"` + OrgID pgtype.UUID `json:"org_id"` } func (q *Queries) DeleteGroupAssignment(ctx context.Context, arg DeleteGroupAssignmentParams) error { - _, err := q.db.Exec(ctx, deleteGroupAssignment, arg.CollectorID, arg.GroupID) + _, err := q.db.Exec(ctx, deleteGroupAssignment, arg.CollectorID, arg.GroupID, arg.OrgID) return err } @@ -58,12 +77,13 @@ FROM group_assignments ga JOIN collectors c ON c.id = ga.collector_id JOIN clusters cl ON cl.id = c.cluster_id WHERE ga.group_id = ANY($1::text[]) + AND ga.org_id = $2 AND cl.org_id = $2 ` type ListCollectorIDsByGroupMembershipInOrgParams struct { - Column1 []string `json:"column_1"` - OrgID pgtype.UUID `json:"org_id"` + GroupIds []string `json:"group_ids"` + OrgID pgtype.UUID `json:"org_id"` } // Used by internal/auth.authorizeOrgAccess's collector-assignment fallback. @@ -74,8 +94,13 @@ type ListCollectorIDsByGroupMembershipInOrgParams struct { // because GetMe computes its org list from group matches and never shows the // extra orgs. The sibling ListTeamsByOrgAndGroups has always been org-scoped; // this one was not. +// +// Both halves are required: the grant must have been made by this org +// (ga.org_id, 0053) AND the collector must still be in it. Checking only the +// collector's current org let a grant made by a cluster's previous owner keep +// working after the cluster moved. func (q *Queries) ListCollectorIDsByGroupMembershipInOrg(ctx context.Context, arg ListCollectorIDsByGroupMembershipInOrgParams) ([]pgtype.UUID, error) { - rows, err := q.db.Query(ctx, listCollectorIDsByGroupMembershipInOrg, arg.Column1, arg.OrgID) + rows, err := q.db.Query(ctx, listCollectorIDsByGroupMembershipInOrg, arg.GroupIds, arg.OrgID) if err != nil { return nil, err } @@ -95,11 +120,17 @@ func (q *Queries) ListCollectorIDsByGroupMembershipInOrg(ctx context.Context, ar } const listGroupAssignmentsByCollector = `-- name: ListGroupAssignmentsByCollector :many -SELECT id, collector_id, group_id, group_display_name, created_at, updated_at FROM group_assignments WHERE collector_id = $1 ORDER BY group_display_name +SELECT id, collector_id, group_id, group_display_name, created_at, updated_at, org_id FROM group_assignments WHERE collector_id = $1 AND org_id = $2 ORDER BY group_display_name ` -func (q *Queries) ListGroupAssignmentsByCollector(ctx context.Context, collectorID pgtype.UUID) ([]GroupAssignment, error) { - rows, err := q.db.Query(ctx, listGroupAssignmentsByCollector, collectorID) +type ListGroupAssignmentsByCollectorParams struct { + CollectorID pgtype.UUID `json:"collector_id"` + OrgID pgtype.UUID `json:"org_id"` +} + +// Org-scoped: the Access tab lists only the grants the viewing org made. +func (q *Queries) ListGroupAssignmentsByCollector(ctx context.Context, arg ListGroupAssignmentsByCollectorParams) ([]GroupAssignment, error) { + rows, err := q.db.Query(ctx, listGroupAssignmentsByCollector, arg.CollectorID, arg.OrgID) if err != nil { return nil, err } @@ -114,6 +145,7 @@ func (q *Queries) ListGroupAssignmentsByCollector(ctx context.Context, collector &i.GroupDisplayName, &i.CreatedAt, &i.UpdatedAt, + &i.OrgID, ); err != nil { return nil, err } diff --git a/internal/store/sqlc/models.go b/internal/store/sqlc/models.go index 04fa78c9..b9f72bb7 100644 --- a/internal/store/sqlc/models.go +++ b/internal/store/sqlc/models.go @@ -170,6 +170,7 @@ type GroupAssignment struct { GroupDisplayName string `json:"group_display_name"` CreatedAt pgtype.Timestamptz `json:"created_at"` UpdatedAt pgtype.Timestamptz `json:"updated_at"` + OrgID pgtype.UUID `json:"org_id"` } type OidcSetting struct { From bc100286c938e2924670509f57913f1496aa873f Mon Sep 17 00:00:00 2001 From: Andre Staats Date: Thu, 29 Oct 2026 00:41:45 +0100 Subject: [PATCH 2/3] test: hash the app-admin session id in the org-move specs Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01TAAVzyeV4HSSNjU8xVWWtE --- internal/mgmtapi/cross_org_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/mgmtapi/cross_org_test.go b/internal/mgmtapi/cross_org_test.go index 15185617..57999320 100644 --- a/internal/mgmtapi/cross_org_test.go +++ b/internal/mgmtapi/cross_org_test.go @@ -147,7 +147,7 @@ var _ = Describe("cross-org isolation of by-id handlers", Label("integration"), appAdmin := func() *http.Cookie { id := fmt.Sprintf("cross-org-app-admin-%d", time.Now().UnixNano()) _, err := st.Queries.CreateSession(ctx, sqlc.CreateSessionParams{ - ID: id, UserOid: "app-admin", Email: "root@example.com", DisplayName: "Root", + ID: auth.HashSessionToken(id), UserOid: "app-admin", Email: "root@example.com", DisplayName: "Root", GroupIds: []byte(`[]`), IsAppAdmin: true, ExpiresAt: pgtype.Timestamptz{Time: time.Now().Add(time.Hour), Valid: true}, Source: "test", From 4a07b94980cd2caa2220dcb3eadaaf473a9d78b1 Mon Sep 17 00:00:00 2001 From: Andre Staats Date: Thu, 29 Oct 2026 00:52:39 +0100 Subject: [PATCH 3/3] fleet: cascade grants on org delete, lock the cluster on create, no insert default Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01TAAVzyeV4HSSNjU8xVWWtE --- deploy/helm/shepherd/UPGRADING.md | 9 ++++-- .../sql/0053_group_assignment_org.down.sql | 2 -- .../sql/0053_group_assignment_org.up.sql | 30 +++++-------------- .../group_assignment_org_migration_test.go | 12 ++++---- internal/store/group_assignment_org_test.go | 8 +++++ internal/store/queries/group_assignments.sql | 4 ++- internal/store/sqlc/group_assignments.sql.go | 4 ++- 7 files changed, 33 insertions(+), 36 deletions(-) diff --git a/deploy/helm/shepherd/UPGRADING.md b/deploy/helm/shepherd/UPGRADING.md index c7ca4427..eddd205a 100644 --- a/deploy/helm/shepherd/UPGRADING.md +++ b/deploy/helm/shepherd/UPGRADING.md @@ -57,11 +57,16 @@ ORDER BY resource_id; To re-create a grant that is still wanted, an admin of the org that now owns the cluster assigns the group again from the collector's Access tab. -During the rolling update a v0.21 pod can still create assignments (the migration binds them to -the collector's current org), but its cluster claim and unclaim leave the previous org's +During the rolling update a v0.21 pod cannot create assignments (its insert names no org, which +`0053` requires), so an Access-tab change that reaches an old pod fails until the rollout +completes; retry it afterwards. A v0.21 pod's cluster claim and unclaim leave the previous org's assignments in place; they grant nothing on an upgraded pod and are removed by the cluster's next claim or unclaim. +`0053` judges an assignment by when it was first created. If a cluster moved and its new owner +then assigned a group the previous owner had also assigned, that assignment is removed too; +re-create it after the upgrade. + **Rolling back:** the down migration drops the `org_id` column. It does not restore the assignments `0053` removed; their audit entries remain. diff --git a/internal/migrations/sql/0053_group_assignment_org.down.sql b/internal/migrations/sql/0053_group_assignment_org.down.sql index ae2bcf3f..36b2994e 100644 --- a/internal/migrations/sql/0053_group_assignment_org.down.sql +++ b/internal/migrations/sql/0053_group_assignment_org.down.sql @@ -3,7 +3,5 @@ -- grants 0053 deleted are NOT restored (their audit_log entries, actor -- 'migration:0053', remain the record of them); re-create any that are still -- wanted from the collector's Access tab. -DROP TRIGGER IF EXISTS group_assignments_default_org ON group_assignments; -DROP FUNCTION IF EXISTS group_assignments_default_org(); DROP INDEX IF EXISTS idx_group_assignments_org_group; ALTER TABLE group_assignments DROP COLUMN org_id; diff --git a/internal/migrations/sql/0053_group_assignment_org.up.sql b/internal/migrations/sql/0053_group_assignment_org.up.sql index 09a54992..53e46903 100644 --- a/internal/migrations/sql/0053_group_assignment_org.up.sql +++ b/internal/migrations/sql/0053_group_assignment_org.up.sql @@ -22,7 +22,8 @@ -- the collector, detail.group_id), plus the reason and, when the log knows -- it, the org that granted the row (its latest assignment.create entry); the -- row is filed under that org so its admins see it. -ALTER TABLE group_assignments ADD COLUMN org_id uuid REFERENCES orgs(id); +-- A grant whose org is deleted grants nothing, so it goes with the org. +ALTER TABLE group_assignments ADD COLUMN org_id uuid REFERENCES orgs(id) ON DELETE CASCADE; UPDATE group_assignments ga SET org_id = cl.org_id @@ -77,25 +78,8 @@ ALTER TABLE group_assignments ALTER COLUMN org_id SET NOT NULL; -- (org_id, group_id). CREATE INDEX idx_group_assignments_org_group ON group_assignments (org_id, group_id); --- Rolling-update and rollback compatibility: a v0.21 binary's --- CreateGroupAssignment names no org_id, and NOT NULL would refuse it. That --- binary has just checked the collector belongs to the caller's org, so an --- insert without an org is bound to the collector's current one. A collector --- with no org still gets no row (NOT NULL refuses it). New binaries always --- name the org, so this never fires for them. -CREATE FUNCTION group_assignments_default_org() RETURNS trigger -LANGUAGE plpgsql AS $$ -BEGIN - IF NEW.org_id IS NULL THEN - SELECT cl.org_id INTO NEW.org_id - FROM collectors c - JOIN clusters cl ON cl.id = c.cluster_id - WHERE c.id = NEW.collector_id; - END IF; - RETURN NEW; -END; -$$; - -CREATE TRIGGER group_assignments_default_org - BEFORE INSERT ON group_assignments - FOR EACH ROW EXECUTE FUNCTION group_assignments_default_org(); +-- Rolling-update compatibility: a v0.21 binary's CreateGroupAssignment names +-- no org_id, so NOT NULL refuses it and that pod's Access-tab create fails +-- until the rollout completes. That is deliberate: filling the org in for it +-- (a trigger reading the collector's current org) could bind a grant to an org +-- the cluster moved to after the old pod's ownership check. diff --git a/internal/store/group_assignment_org_migration_test.go b/internal/store/group_assignment_org_migration_test.go index 44653e5a..6954916a 100644 --- a/internal/store/group_assignment_org_migration_test.go +++ b/internal/store/group_assignment_org_migration_test.go @@ -43,7 +43,7 @@ var _ = Describe("Migration: 0053_group_assignment_org", Label("integration"), f BeforeEach(func(ctx context.Context) { url = sharedPG.IsolatedDB(ctx, GinkgoTB()) - Expect(store.MigrateTo(ctx, url, 51)).To(Succeed()) + Expect(store.MigrateTo(ctx, url, 52)).To(Succeed()) reopen(ctx) orgA = scalar(ctx, `INSERT INTO orgs (name, display_name, admin_group_id) VALUES ('m53-a', 'A', 'a-admin') RETURNING id::text`) @@ -142,18 +142,16 @@ var _ = Describe("Migration: 0053_group_assignment_org", Label("integration"), f Expect(orgOf[collNo]).To(BeEmpty(), "no granting org is known, so the entry is platform-level") }) - It("binds a v0.21 binary's insert (no org_id) to the collector's org, and refuses one with no org", func(ctx context.Context) { + It("refuses a v0.21 binary's insert, which names no org", func(ctx context.Context) { + // Filling the org in for it could bind a grant to an org the cluster + // moved to after the old binary's ownership check, so it is refused. _, err := db.Exec(ctx, `INSERT INTO group_assignments (collector_id, group_id, group_display_name) VALUES ($1, 'g-old', 'g-old')`, collStill) - Expect(err).NotTo(HaveOccurred()) - Expect(scalar(ctx, `SELECT org_id::text FROM group_assignments WHERE group_id = 'g-old'`)).To(Equal(orgA)) - - _, err = db.Exec(ctx, `INSERT INTO group_assignments (collector_id, group_id, group_display_name) VALUES ($1, 'g-none', 'g-none')`, collNo) Expect(err).To(MatchError(ContainSubstring("not-null"))) }) It("drops the column on the down migration and keeps the bound rows", func(ctx context.Context) { db.Close() - Expect(store.MigrateTo(ctx, url, 51)).To(Succeed()) + Expect(store.MigrateTo(ctx, url, 52)).To(Succeed()) reopen(ctx) Expect(scalar(ctx, `SELECT count(*)::text FROM information_schema.columns WHERE table_name = 'group_assignments' AND column_name = 'org_id'`)).To(Equal("0")) diff --git a/internal/store/group_assignment_org_test.go b/internal/store/group_assignment_org_test.go index 6dfc100f..fd0b7ede 100644 --- a/internal/store/group_assignment_org_test.go +++ b/internal/store/group_assignment_org_test.go @@ -73,6 +73,14 @@ var _ = Describe("group assignments across cluster org changes", Label("integrat return len(rows) + len(rowsB) } + It("lets an org be deleted while it still holds a grant on a cluster another org owns", func() { + // A v0.21 pod's claim moves the cluster without removing org A's grant. + _, err := st.Pool().Exec(ctx, "UPDATE clusters SET org_id = $1 WHERE id = $2", orgB, cluster.ID) // RAW-SQL-OK: simulates an older binary's claim + Expect(err).NotTo(HaveOccurred()) + Expect(st.Queries.DeleteOrg(ctx, orgA)).To(Succeed()) + Expect(rowCount()).To(Equal(0)) + }) + It("grants within the org that made the grant", func() { Expect(grantsIn(orgA)).To(ConsistOf(collector.ID)) Expect(grantsIn(orgB)).To(BeEmpty()) diff --git a/internal/store/queries/group_assignments.sql b/internal/store/queries/group_assignments.sql index fc8a3a4a..b9d30f4b 100644 --- a/internal/store/queries/group_assignments.sql +++ b/internal/store/queries/group_assignments.sql @@ -3,12 +3,14 @@ -- while the collector's cluster is in that org, so a cluster that moved -- between the caller's ownership check and this statement gets no row (the -- caller sees pgx.ErrNoRows) rather than a grant bound to an org that no --- longer owns it. +-- longer owns it. FOR SHARE OF cl makes a claim that is already updating the +-- cluster finish first, so the org check sees its result. INSERT INTO group_assignments (collector_id, group_id, group_display_name, org_id) SELECT c.id, sqlc.arg('group_id')::text, sqlc.arg('group_display_name')::text, cl.org_id FROM collectors c JOIN clusters cl ON cl.id = c.cluster_id WHERE c.id = sqlc.arg('collector_id') AND cl.org_id = sqlc.arg('org_id') +FOR SHARE OF cl ON CONFLICT (collector_id, group_id) DO UPDATE SET group_display_name = EXCLUDED.group_display_name, org_id = EXCLUDED.org_id, diff --git a/internal/store/sqlc/group_assignments.sql.go b/internal/store/sqlc/group_assignments.sql.go index 018ed9d8..523f15b2 100644 --- a/internal/store/sqlc/group_assignments.sql.go +++ b/internal/store/sqlc/group_assignments.sql.go @@ -17,6 +17,7 @@ SELECT c.id, $1::text, $2::text, cl.org_id FROM collectors c JOIN clusters cl ON cl.id = c.cluster_id WHERE c.id = $3 AND cl.org_id = $4 +FOR SHARE OF cl ON CONFLICT (collector_id, group_id) DO UPDATE SET group_display_name = EXCLUDED.group_display_name, org_id = EXCLUDED.org_id, @@ -35,7 +36,8 @@ type CreateGroupAssignmentParams struct { // while the collector's cluster is in that org, so a cluster that moved // between the caller's ownership check and this statement gets no row (the // caller sees pgx.ErrNoRows) rather than a grant bound to an org that no -// longer owns it. +// longer owns it. FOR SHARE OF cl makes a claim that is already updating the +// cluster finish first, so the org check sees its result. func (q *Queries) CreateGroupAssignment(ctx context.Context, arg CreateGroupAssignmentParams) (GroupAssignment, error) { row := q.db.QueryRow(ctx, createGroupAssignment, arg.GroupID,