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
Merged

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Jun 7, 2024

Before RoleName was a string that was formatted <role_name>[:<org_id>]. This was super loose and hard to tell when a string had the org_id and when it did not.

This change makes the string a struct. So now all string names are just the name, without the org_id scope.

Why do this?

The database and the sdk store roles as a struct, so it makes sense the rbac package should also. It makes the organizationID an obvious field vs something implied from an arbitrary string format.

It also disambiguates the format at any given point in the code. The old string was almost arbitrary when it was formatted with the org id, and when not

Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vibe check 👍

@@ -192,7 +192,7 @@ func (r *RootCmd) newCreateAdminUserCommand() *serpent.Command {
HashedPassword: []byte(hashedPassword),
CreatedAt: dbtime.Now(),
UpdatedAt: dbtime.Now(),
RBACRoles: []string{rbac.RoleOwner()},
RBACRoles: []string{rbac.RoleOwner().String()},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review: string does not implement fmt.Stringer :(

@Emyrk Emyrk force-pushed the stevenmasley/typed_role_name branch from 3977989 to fa2f704 Compare June 10, 2024 19:39
@Emyrk Emyrk marked this pull request as ready for review June 10, 2024 20:00
@Emyrk Emyrk requested a review from johnstcn June 10, 2024 21:03
Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -199,7 +199,8 @@ func (api *API) convertAuditLog(ctx context.Context, dblog database.GetAuditLogs
Roles: []codersdk.SlimRole{},
}

for _, roleName := range dblog.UserRoles {
for _, input := range dblog.UserRoles {
roleName, _ := rbac.RoleNameFromString(input)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we at least drop an error log if we were unable to covert any of the roles rom the db?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is in the convert audit log, I think that could get quite noisy. I agree this is not ideal

Comment on lines +173 to +178
// TODO: Currently the api only returns site wide roles.
// Should it return organization roles?
rbacRole, err := rbac.RoleByName(rbac.RoleIdentifier{
Name: roleName,
OrganizationID: uuid.Nil,
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

follow-up for TODO?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not going to do this until we get farther in the organization work.

Right now this api is used for the /users page on the UI. If I were to add it, then organization roles would pop up there, which is probably incorrect behavior.

So this is a genuine question if we should expand the roles returned, and on the UI filter out the organization ones. But at present, showing org roles at all is incorrect as orgs do not really exist.

Comment on lines +1540 to +1542
// TODO: This only supports mapping deployment wide roles. Organization scoped roles
// are unsupported.
if _, err := rbac.RoleByName(rbac.RoleIdentifier{Name: role}); err == nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

follow-up for TODO

also, should we catch an attempt to lookup an org-scoped role here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Followup is already an issue: #12147

And we do check that an org scoped role is not assigned here:

if len(role.Org) > 0 && name.OrganizationID == uuid.Nil {

If you pass a nil uuid, and some org permissions exist, it will fail.

@Emyrk Emyrk merged commit 5ccf508 into main Jun 11, 2024
27 checks passed
@Emyrk Emyrk deleted the stevenmasley/typed_role_name branch June 11, 2024 13:55
@github-actions github-actions bot locked and limited conversation to collaborators Jun 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants