Skip to content

feat: Rbac more coderd endpoints, unit test to confirm #1437

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 30 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
bed0f8f
feat: Enforce authorize call on all endpoints
Emyrk May 11, 2022
af6dc5f
Add more endpoints to the unit test
Emyrk May 12, 2022
01b2c94
Merge remote-tracking branch 'origin/main' into stevenmasley/rbac_end…
Emyrk May 12, 2022
be5b0b3
Rbac users endpoints
Emyrk May 12, 2022
970e345
Make test pass by skipping missed endpoints
Emyrk May 12, 2022
945e9fa
Fix broken tests
Emyrk May 12, 2022
fdfef88
Import order
Emyrk May 12, 2022
89a3678
PR comment fixes
Emyrk May 12, 2022
29da9aa
Merge remote-tracking branch 'origin/main' into stevenmasley/rbac_end…
Emyrk May 13, 2022
63727e0
omit another endpoint
Emyrk May 13, 2022
96a5727
Cleanup comments
Emyrk May 13, 2022
4b6c9b0
Do not leak if an organization name exists
Emyrk May 13, 2022
cd2fda7
Update comment
Emyrk May 13, 2022
62ec87e
feat: Implement authorize for each endpoint
Emyrk May 13, 2022
452c72d
Authorize per endpoint
Emyrk May 13, 2022
307f6c0
Merge remote-tracking branch 'origin/main' into stevenmasley/rbac_end…
Emyrk May 16, 2022
32af1e6
feat: Move all authorize calls into each handler
Emyrk May 16, 2022
28a099f
Delete unused code
Emyrk May 16, 2022
ff7bd81
feat: Add some perms to users
Emyrk May 16, 2022
d123b9f
Drop comment
Emyrk May 16, 2022
186eb5f
Fix 401 -> 403
Emyrk May 16, 2022
5d32d9d
Fix using User over UserData
Emyrk May 16, 2022
301d42a
Rename UserRole to RoleAssignment
Emyrk May 16, 2022
a989224
Refactor workspacesByUser
Emyrk May 16, 2022
ed9be78
Merge remote-tracking branch 'origin/main' into stevenmasley/rbac_end…
Emyrk May 16, 2022
1047391
Fix some routes
Emyrk May 16, 2022
7ad069e
Drop update User auth check from assign roles
Emyrk May 16, 2022
e68fdbf
Correct unit tests
Emyrk May 17, 2022
1a4e7e1
Unit tests use 403 vs 401
Emyrk May 17, 2022
f250fbe
401 -> 403 in unit test
Emyrk May 17, 2022
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
Prev Previous commit
Next Next commit
PR comment fixes
- Rename authObject
- Shorten 'AuthorizeByRoleName'
  • Loading branch information
Emyrk committed May 12, 2022
commit 89a367890c41e5f73403b9d672b02cefc3bf481e
2 changes: 1 addition & 1 deletion coderd/coderd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ type fakeAuthorizer struct {
AlwaysReturn error
}

func (f *fakeAuthorizer) AuthorizeByRoleName(_ context.Context, subjectID string, roleNames []string, action rbac.Action, object rbac.Object) error {
func (f *fakeAuthorizer) ByRoleName(_ context.Context, subjectID string, roleNames []string, action rbac.Action, object rbac.Object) error {
f.Called = &authCall{
SubjectID: subjectID,
Roles: roleNames,
Expand Down
18 changes: 9 additions & 9 deletions coderd/httpmw/authorize.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/coder/coder/coderd/rbac"
)

// Authorize will enforce if the user roles can complete the action on the AuthObject.
// Authorize will enforce if the user roles can complete the action on the RBACObject.
// The organization and owner are found using the ExtractOrganization and
// ExtractUser middleware if present.
func Authorize(logger slog.Logger, auth rbac.Authorizer, actions ...rbac.Action) func(http.Handler) http.Handler {
Expand Down Expand Up @@ -51,7 +51,7 @@ func Authorize(logger slog.Logger, auth rbac.Authorizer, actions ...rbac.Action)
}

for _, action := range actions {
err := auth.AuthorizeByRoleName(r.Context(), roles.ID.String(), roles.Roles, action, object)
err := auth.ByRoleName(r.Context(), roles.ID.String(), roles.Roles, action, object)
if err != nil {
internalError := new(rbac.UnauthorizedError)
if xerrors.As(err, internalError) {
Expand Down Expand Up @@ -80,15 +80,15 @@ func Authorize(logger slog.Logger, auth rbac.Authorizer, actions ...rbac.Action)

type authObjectKey struct{}

type AuthObject struct {
type RBACObject struct {
Object rbac.Object

WithOwner func(r *http.Request) uuid.UUID
}

// APIKey returns the API key from the ExtractAPIKey handler.
func rbacObject(r *http.Request) AuthObject {
obj, ok := r.Context().Value(authObjectKey{}).(AuthObject)
func rbacObject(r *http.Request) RBACObject {
obj, ok := r.Context().Value(authObjectKey{}).(RBACObject)
if !ok {
panic("developer error: auth object middleware not provided")
}
Expand All @@ -107,11 +107,11 @@ func WithAPIKeyAsOwner() func(http.Handler) http.Handler {
func WithOwner(withOwner func(r *http.Request) uuid.UUID) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
obj, ok := r.Context().Value(authObjectKey{}).(AuthObject)
obj, ok := r.Context().Value(authObjectKey{}).(RBACObject)
if ok {
obj.WithOwner = withOwner
} else {
obj = AuthObject{WithOwner: withOwner}
obj = RBACObject{WithOwner: withOwner}
}

ctx := context.WithValue(r.Context(), authObjectKey{}, obj)
Expand All @@ -125,11 +125,11 @@ func WithOwner(withOwner func(r *http.Request) uuid.UUID) func(http.Handler) htt
func WithRBACObject(object rbac.Object) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
obj, ok := r.Context().Value(authObjectKey{}).(AuthObject)
obj, ok := r.Context().Value(authObjectKey{}).(RBACObject)
if ok {
obj.Object = object
} else {
obj = AuthObject{Object: object}
obj = RBACObject{Object: object}
}

ctx := context.WithValue(r.Context(), authObjectKey{}, obj)
Expand Down
6 changes: 3 additions & 3 deletions coderd/rbac/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type Authorizer interface {
AuthorizeByRoleName(ctx context.Context, subjectID string, roleNames []string, action Action, object Object) error
ByRoleName(ctx context.Context, subjectID string, roleNames []string, action Action, object Object) error
}

// RegoAuthorizer will use a prepared rego query for performing authorize()
Expand Down Expand Up @@ -42,10 +42,10 @@ type authSubject struct {
Roles []Role `json:"roles"`
}

// AuthorizeByRoleName will expand all roleNames into roles before calling Authorize().
// ByRoleName will expand all roleNames into roles before calling Authorize().
// This is the function intended to be used outside this package.
// The role is fetched from the builtin map located in memory.
func (a RegoAuthorizer) AuthorizeByRoleName(ctx context.Context, subjectID string, roleNames []string, action Action, object Object) error {
func (a RegoAuthorizer) ByRoleName(ctx context.Context, subjectID string, roleNames []string, action Action, object Object) error {
roles := make([]Role, 0, len(roleNames))
for _, n := range roleNames {
r, err := RoleByName(n)
Expand Down
10 changes: 5 additions & 5 deletions coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ func (api *api) putUserRoles(rw http.ResponseWriter, r *http.Request) {
// Assigning a role requires the create permission. The middleware checks if
// we can update this user, so the combination of the 2 permissions enables
// assigning new roles.
err := api.Authorizer.AuthorizeByRoleName(r.Context(), roles.ID.String(), roles.Roles,
err := api.Authorizer.ByRoleName(r.Context(), roles.ID.String(), roles.Roles,
rbac.ActionCreate, rbac.ResourceUserRole.WithID(roleName))
if err != nil {
httpapi.Write(rw, http.StatusUnauthorized, httpapi.Response{
Expand All @@ -420,7 +420,7 @@ func (api *api) putUserRoles(rw http.ResponseWriter, r *http.Request) {

// Any roles that were removed also need to be checked.
for roleName := range has {
err := api.Authorizer.AuthorizeByRoleName(r.Context(), roles.ID.String(), roles.Roles,
err := api.Authorizer.ByRoleName(r.Context(), roles.ID.String(), roles.Roles,
rbac.ActionDelete, rbac.ResourceUserRole.WithID(roleName))
if err != nil {
httpapi.Write(rw, http.StatusUnauthorized, httpapi.Response{
Expand Down Expand Up @@ -490,7 +490,7 @@ func (api *api) organizationsByUser(rw http.ResponseWriter, r *http.Request) {

publicOrganizations := make([]codersdk.Organization, 0, len(organizations))
for _, organization := range organizations {
err := api.Authorizer.AuthorizeByRoleName(r.Context(), roles.ID.String(), roles.Roles, rbac.ActionRead,
err := api.Authorizer.ByRoleName(r.Context(), roles.ID.String(), roles.Roles, rbac.ActionRead,
rbac.ResourceOrganization.
WithID(organization.ID.String()).
InOrg(organization.ID),
Expand Down Expand Up @@ -522,7 +522,7 @@ func (api *api) organizationByUserAndName(rw http.ResponseWriter, r *http.Reques
return
}

err = api.Authorizer.AuthorizeByRoleName(r.Context(), roles.ID.String(), roles.Roles, rbac.ActionRead,
err = api.Authorizer.ByRoleName(r.Context(), roles.ID.String(), roles.Roles, rbac.ActionRead,
rbac.ResourceOrganization.
InOrg(organization.ID).
WithID(organization.ID.String()),
Expand Down Expand Up @@ -825,7 +825,7 @@ func (api *api) workspacesByUser(rw http.ResponseWriter, r *http.Request) {
}
organizationIDs := make([]uuid.UUID, 0)
for _, organization := range organizations {
err = api.Authorizer.AuthorizeByRoleName(r.Context(), user.ID.String(), roles.Roles, rbac.ActionRead, rbac.ResourceWorkspace.All().InOrg(organization.ID))
err = api.Authorizer.ByRoleName(r.Context(), user.ID.String(), roles.Roles, rbac.ActionRead, rbac.ResourceWorkspace.All().InOrg(organization.ID))
var apiErr *rbac.UnauthorizedError
if xerrors.As(err, &apiErr) {
continue
Expand Down