Skip to content

chore: create type for unique role names #13506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jun 11, 2024
Prev Previous commit
Next Next commit
Work on switching the string to a struct
  • Loading branch information
Emyrk committed Jun 10, 2024
commit 7be77554169f99313c5975c51a11be2c56749585
14 changes: 7 additions & 7 deletions coderd/database/dbauthz/dbauthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,12 +584,12 @@ func (q *querier) authorizeUpdateFileTemplate(ctx context.Context, file database

// uniqueOrganizationRoles converts a set of scoped role names to their unique
// scoped names.
func (q *querier) uniqueOrganizationRoles(organizationID uuid.UUID, names []string) ([]rbac.UniqueRoleName, error) {
uniques := make([]rbac.UniqueRoleName, 0, len(names))
func (q *querier) uniqueOrganizationRoles(organizationID uuid.UUID, names []string) ([]rbac.RoleName, error) {
uniques := make([]rbac.RoleName, 0, len(names))
for _, name := range names {
// This check is a developer safety check. Old code might try to invoke this code path with
// organization id suffixes. Catch this and return a nice error so it can be fixed.
_, foundOrg, _ := rbac.RoleSplit(rbac.UniqueRoleName(name))
_, foundOrg, _ := rbac.RoleSplit(rbac.RoleName(name))
if foundOrg != "" {
return nil, xerrors.Errorf("attempt to assign a role %q, remove the ':<organization_id> suffix", name)
}
Expand All @@ -601,7 +601,7 @@ func (q *querier) uniqueOrganizationRoles(organizationID uuid.UUID, names []stri
}

// canAssignRoles handles assigning built in and custom roles.
func (q *querier) canAssignRoles(ctx context.Context, orgID *uuid.UUID, added, removed []rbac.UniqueRoleName) error {
func (q *querier) canAssignRoles(ctx context.Context, orgID *uuid.UUID, added, removed []rbac.RoleName) error {
actor, ok := ActorFromContext(ctx)
if !ok {
return NoActorError
Expand All @@ -615,7 +615,7 @@ func (q *querier) canAssignRoles(ctx context.Context, orgID *uuid.UUID, added, r
}

grantedRoles := append(added, removed...)
customRoles := make([]rbac.UniqueRoleName, 0)
customRoles := make([]rbac.RoleName, 0)
// Validate that the roles being assigned are valid.
for _, r := range grantedRoles {
roleOrgIDStr, isOrgRole := rbac.IsOrgRole(r)
Expand Down Expand Up @@ -647,7 +647,7 @@ func (q *querier) canAssignRoles(ctx context.Context, orgID *uuid.UUID, added, r
}
}

customRolesMap := make(map[rbac.UniqueRoleName]struct{}, len(customRoles))
customRolesMap := make(map[rbac.RoleName]struct{}, len(customRoles))
for _, r := range customRoles {
customRolesMap[r] = struct{}{}
}
Expand Down Expand Up @@ -2867,7 +2867,7 @@ func (q *querier) UpdateMemberRoles(ctx context.Context, arg database.UpdateMemb

// The 'rbac' package expects role names to be scoped.
// Convert the argument roles for validation.
scopedGranted := make([]rbac.UniqueRoleName, 0, len(arg.GrantedRoles))
scopedGranted := make([]rbac.RoleName, 0, len(arg.GrantedRoles))
for _, grantedRole := range arg.GrantedRoles {
// This check is a developer safety check. Old code might try to invoke this code path with
// organization id suffixes. Catch this and return a nice error so it can be fixed.
Expand Down
2 changes: 1 addition & 1 deletion coderd/database/modelmethods.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func (p ProvisionerJob) FinishedAt() time.Time {
return time.Time{}
}

func (r CustomRole) UniqueName() rbac.UniqueRoleName {
func (r CustomRole) UniqueName() rbac.RoleName {
if r.OrganizationID.UUID == uuid.Nil {
return rbac.RoleName(r.Name, "")
}
Expand Down
4 changes: 2 additions & 2 deletions coderd/rbac/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ func (s Subject) SafeScopeName() string {
}

// SafeRoleNames prevent nil pointer dereference.
func (s Subject) SafeRoleNames() []UniqueRoleName {
func (s Subject) SafeRoleNames() []RoleName {
if s.Roles == nil {
return []UniqueRoleName{}
return []RoleName{}
}
return s.Roles.Names()
}
Expand Down
Loading