Skip to content
Merged
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 50 additions & 4 deletions deploy/helm/shepherd/UPGRADING.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,14 +18,58 @@ 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 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.

## 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,
Expand Down
7 changes: 6 additions & 1 deletion docs/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion internal/auth/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/auth/authz_roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
83 changes: 83 additions & 0 deletions internal/mgmtapi/cross_org_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: 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",
})
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",
Expand Down
37 changes: 29 additions & 8 deletions internal/mgmtapi/rpc_fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand All @@ -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
}

Expand All @@ -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
}

Expand Down
7 changes: 7 additions & 0 deletions internal/migrations/sql/0053_group_assignment_org.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- 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 INDEX IF EXISTS idx_group_assignments_org_group;
ALTER TABLE group_assignments DROP COLUMN org_id;
Loading
Loading