Skip to content
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

feat: add api for patching custom org roles #13357

Merged
merged 14 commits into from
May 29, 2024

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented May 23, 2024

What this does

Adds apis to create custom roles for a given organization.

Removes site role patching

Custom site role creation was moved to custom org role creating. It was decided to do org roles first. Fixed the unit tests to do org roles rather than site.

Comment on lines -66 to -94
func (api *API) updateOrganizationMemberRoles(ctx context.Context, args database.UpdateMemberRolesParams) (database.OrganizationMember, error) {
// Enforce only site wide roles
for _, r := range args.GrantedRoles {
// Must be an org role for the org in the args
orgID, ok := rbac.IsOrgRole(r)
if !ok {
return database.OrganizationMember{}, xerrors.Errorf("must only update organization roles")
}

roleOrg, err := uuid.Parse(orgID)
if err != nil {
return database.OrganizationMember{}, xerrors.Errorf("Role must have proper UUIDs for organization, %q does not", r)
}

if roleOrg != args.OrgID {
return database.OrganizationMember{}, xerrors.Errorf("Must only pass roles for org %q", args.OrgID.String())
}

if _, err := rbac.RoleByName(r); err != nil {
return database.OrganizationMember{}, xerrors.Errorf("%q is not a supported organization role", r)
}
}

updatedUser, err := api.Database.UpdateMemberRoles(ctx, args)
if err != nil {
return database.OrganizationMember{}, xerrors.Errorf("Update site roles: %w", err)
}
return updatedUser, nil
}
Copy link
Member Author

@Emyrk Emyrk May 23, 2024

Choose a reason for hiding this comment

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

This is all moved to dbauthz. The same is done for site wide roles in dbauthz already.

@Emyrk Emyrk changed the title feat: patching custom org roles feat: api for patching custom org roles May 24, 2024
@Emyrk Emyrk force-pushed the stevenmasley/patch_org_roles_rebased branch from ddad37a to cd3ca65 Compare May 24, 2024 18:56
Copy link
Member Author

Emyrk commented May 24, 2024

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @Emyrk and the rest of your teammates on Graphite Graphite

@Emyrk Emyrk marked this pull request as ready for review May 24, 2024 19:50
@Emyrk Emyrk requested a review from johnstcn May 24, 2024 20:05
@Emyrk Emyrk force-pushed the stevenmasley/patch_org_roles_rebased branch from 968cb76 to 44ddddd Compare May 24, 2024 20:42
coderd/database/dbauthz/dbauthz.go Show resolved Hide resolved
coderd/roles.go Outdated Show resolved Hide resolved
Comment on lines 46 to 60
if len(role.OrganizationPermissions) > 1 {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Invalid request, Only 1 organization can be assigned permissions",
Detail: "roles can only contain 1 organization",
})
return codersdk.Role{}, false
}
Copy link
Member

Choose a reason for hiding this comment

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

If this is an invalid state, why is it representable in a codersdk.Role?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea this is annoying. It is possible in the rbac library, but we never use this functionality. Essentially, making a role that has permissions in 2 orgs makes no sense imo. Because it is technically a possibility, if we hit this, and I strip it from the sdk, then if we ever hit it, I have to throw information out.


I'll make this impossible on the sdk. Let me see what happens on the BE when I hit the edge case that should never be hit.

Copy link
Member Author

Choose a reason for hiding this comment

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

It silently omits permissions. Which feels a bit off.

// This is not perfect. If there are organization permissions in another
// organization, they will be omitted. This should not be allowed, so
// should never happen.

Returning an error feels like it could have a single role "break" things. Wondering if I could include an extra field with like warnings 🤔

Copy link
Member

Choose a reason for hiding this comment

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

Returning an error doesn't have to specifically break things, we could export ErrNoMultiOrgRole and IsNoMultiOrgRoleError() in db2sdk and handle them appropriately by dropping an error log. This would at least allow us to detect this in tests.

However, it feels like the 'right' fix here is to just not allow multi-org roles at all in rbac. It's not a blocker to this PR, but it feels like something we should fix.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea, it might be worth refactoring the rbac to just prevent this altogether 🤔. I think it could be done without trickling down to the rego.

I think that is the better approach, as I can't see a reason for it in the future.

enterprise/coderd/roles.go Outdated Show resolved Hide resolved
enterprise/coderd/roles.go Outdated Show resolved Hide resolved
enterprise/coderd/roles_test.go Outdated Show resolved Hide resolved
@Emyrk Emyrk changed the title feat: api for patching custom org roles feat: add api for patching custom org roles May 28, 2024
Comment on lines -330 to -344
r.Route("/users/roles", func(r chi.Router) {
r.Use(
apiKeyMiddleware,
)
r.Group(func(r chi.Router) {
r.Use(
api.customRolesEnabledMW,
)
r.Patch("/", api.patchRole)
})
// Unfortunate, but this r.Route overrides the AGPL roles route.
// The AGPL does not have the entitlements to block the licensed
// routes, so we need to duplicate the AGPL here.
r.Get("/", api.AGPL.AssignableSiteRoles)
})
Copy link
Member Author

Choose a reason for hiding this comment

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

This is why that interface was created. This was moved to the /organizations route, and would require duplicating all the routes. So instead the code lives in AGPL and enterprise just patches the interface.

coderd/database/dbauthz/dbauthz.go Show resolved Hide resolved
coderd/roles.go Outdated Show resolved Hide resolved
Comment on lines 46 to 60
if len(role.OrganizationPermissions) > 1 {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Invalid request, Only 1 organization can be assigned permissions",
Detail: "roles can only contain 1 organization",
})
return codersdk.Role{}, false
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Yea this is annoying. It is possible in the rbac library, but we never use this functionality. Essentially, making a role that has permissions in 2 orgs makes no sense imo. Because it is technically a possibility, if we hit this, and I strip it from the sdk, then if we ever hit it, I have to throw information out.


I'll make this impossible on the sdk. Let me see what happens on the BE when I hit the edge case that should never be hit.

enterprise/coderd/roles_test.go Outdated Show resolved Hide resolved
@Emyrk Emyrk force-pushed the stevenmasley/patch_org_roles_rebased branch from 469f74f to 6eb1167 Compare May 28, 2024 17:25
@Emyrk Emyrk force-pushed the stevenmasley/patch_org_roles_rebased branch from 6eb1167 to 5ac97f8 Compare May 28, 2024 19:18
@Emyrk Emyrk requested a review from johnstcn May 28, 2024 19:42
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. I think we may need to do some follow-up changes so we don't need to worry about the multi-org role issue, but that's out of scope here.

@Emyrk Emyrk merged commit afd9d3b into main May 29, 2024
36 checks passed
@Emyrk Emyrk deleted the stevenmasley/patch_org_roles_rebased branch May 29, 2024 14:49
@github-actions github-actions bot locked and limited conversation to collaborators May 29, 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.

None yet

2 participants