Skip to content

Commit 4347bf4

Browse files
committed
test: include per-org default roles in rbac user subjects
Aligns coderdtest.AuthzUserSubjectWithDB with GetAuthorizationUserRoles so tests built via this helper see the per-org default_org_member_roles union, not just organization-member. Also moves the single-use MemberSubject helper into its sole consumer and unexports it. Refs #25936.
1 parent 926baff commit 4347bf4

4 files changed

Lines changed: 40 additions & 33 deletions

File tree

coderd/coderdtest/coderdtest.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,16 @@ func AuthzUserSubjectWithDB(ctx context.Context, t testing.TB, db database.Store
908908
require.NoError(t, err)
909909
for _, org := range orgs {
910910
roles = append(roles, rbac.ScopedRoleOrgMember(org.ID))
911+
// The implicit role set (organization-member plus the org's
912+
// default_org_member_roles) is unioned at request time by
913+
// GetAuthorizationUserRoles. Subjects built directly here bypass
914+
// that SQL union, so mirror it explicitly.
915+
for _, name := range org.DefaultOrgMemberRoles {
916+
roles = append(roles, rbac.RoleIdentifier{
917+
Name: name,
918+
OrganizationID: org.ID,
919+
})
920+
}
911921
}
912922

913923
//nolint:gocritic // We need to expand DB-backed/system roles. The caller

coderd/coderdtest/subjects.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

coderd/workspaceconnwatcher/watcher_test.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"github.com/coder/coder/v2/coderd/database/dbmock"
2020
"github.com/coder/coder/v2/coderd/database/pubsub"
2121
"github.com/coder/coder/v2/coderd/httpmw"
22+
"github.com/coder/coder/v2/coderd/rbac"
23+
"github.com/coder/coder/v2/coderd/rbac/rolestore"
2224
"github.com/coder/coder/v2/coderd/workspaceconnwatcher"
2325
"github.com/coder/coder/v2/coderd/wspubsub"
2426
"github.com/coder/coder/v2/codersdk"
@@ -72,7 +74,7 @@ func (h *harness) Dial(ctx context.Context, url string) (*wsjson.Decoder[workspa
7274
Handler: http.HandlerFunc(h.watcher.WorkspaceAgentConnectionWatch),
7375
CtxMutator: func(ctx context.Context) context.Context {
7476
ctx = httpmw.WithWorkspaceParam(ctx, h.workspace)
75-
ctx = dbauthz.As(ctx, coderdtest.MemberSubject(userID, orgID))
77+
ctx = dbauthz.As(ctx, memberSubject(userID, orgID))
7678
return ctx
7779
},
7880
Logger: h.logger.Named("roundtripper"),
@@ -470,3 +472,29 @@ func TestWatcher_ClosedAfterDial(t *testing.T) {
470472
}
471473
testutil.TryReceive(ctx, t, closed)
472474
}
475+
476+
// memberSubject builds an RBAC subject scoped as a basic org member, used to
477+
// drive the watcher handler through dbauthz checks. Kept local to this test
478+
// because no other package needs it.
479+
func memberSubject(userID, orgID uuid.UUID) rbac.Subject {
480+
memberRole, err := rbac.RoleByName(rbac.RoleMember())
481+
if err != nil {
482+
panic(err)
483+
}
484+
orgMember, err := rolestore.TestingGetSystemRole(
485+
rbac.RoleOrgMember(),
486+
orgID,
487+
rbac.OrgSettings{ShareableWorkspaceOwners: rbac.ShareableWorkspaceOwnersNone},
488+
)
489+
if err != nil {
490+
panic(err)
491+
}
492+
return rbac.Subject{
493+
FriendlyName: "coderdtest-member",
494+
Email: "member@coderd.test",
495+
Type: rbac.SubjectTypeUser,
496+
ID: userID.String(),
497+
Roles: rbac.Roles{memberRole, orgMember},
498+
Scope: rbac.ScopeAll,
499+
}.WithCachedASTValue()
500+
}

testutil/websocket.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
// Handler: MyHandler,
2020
// CtxMutator: func(ctx context.Context) context.Context {
2121
// ctx = httpmw.WithWorkspaceParam(ctx, ws)
22-
// ctx = dbauthz.As(ctx, coderdtest.MemberSubject(userID, orgID))
22+
// ctx = dbauthz.As(ctx, mySubject(userID, orgID))
2323
// return ctx
2424
// },
2525
// Logger: logger.Named("roundtripper"),

0 commit comments

Comments
 (0)