10000 feat: Rbac more coderd endpoints, unit test to confirm by Emyrk · Pull Request #1437 · coder/coder · GitHub
[go: up one dir, main page]

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
Fix 401 -> 403
Fix comments
  • Loading branch information
Emyrk committed May 16, 2022
commit 186eb5f4ed440a18221642a272ec0af9409e1bc5
2 changes: 1 addition & 1 deletion coderd/authorize.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (api *api) Authorize(rw http.ResponseWriter, r *http.Request, action rbac.A
roles := httpmw.UserRoles(r)
err := api.Authorizer.ByRoleName(r.Context(), roles.ID.String(), roles.Roles, action, object)
if err != nil {
httpapi.Write(rw, http.StatusUnauthorized, httpapi.Response{
httpapi.Write(rw, http.StatusForbidden, httpapi.Response{
Message: err.Error(),
})

Expand Down
6 changes: 6 additions & 0 deletions coderd/httpapi/httpapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ type Error struct {
Detail string `json:"detail" validate:"required"`
}

func Forbidden(rw http.ResponseWriter) {
Write(rw, http.StatusForbidden, Response{
Message: "forbidden",
})
}

// Write outputs a standardized format to an HTTP response body.
func Write(rw http.ResponseWriter, status int, response interface{}) {
buf := &bytes.Buffer{}
Expand Down
6 changes: 3 additions & 3 deletions coderd/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ func (api *api) workspaceByOwnerAndName(rw http.ResponseWriter, r *http.Request)
}

if workspace.OrganizationID != organization.ID {
httpapi.Write(rw, http.StatusUnauthorized, httpapi.Response{
httpapi.Write(rw, http.StatusForbidden, httpapi.Response{
Message: fmt.Sprintf("workspace is not owned by organization %q", organization.Name),
})
return
Expand Down Expand Up @@ -493,7 +493,7 @@ func (api *api) postWorkspacesByOrganization(rw http.ResponseWriter, r *http.Req
}

if organization.ID != template.OrganizationID {
httpapi.Write(rw, http.StatusUnauthorized, httpapi.Response{
httpapi.Write(rw, http.StatusForbidden, httpapi.Response{
Message: fmt.Sprintf("template is not in organization %q", organization.Name),
})
return
Expand All @@ -503,7 +503,7 @@ func (api *api) postWorkspacesByOrganization(rw http.ResponseWriter, r *http.Req
UserID: apiKey.UserID,
})
if errors.Is(err, sql.ErrNoRows) {
httpapi.Write(rw, http.StatusUnauthorized, httpapi.Response{
httpapi.Write(rw, http.StatusForbidden, httpapi.Response{
Message: "you aren't allowed to access templates in that organization",
})
return
Expand Down
2 changes: 1 addition & 1 deletion coderd/rbac/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const (
// errUnauthorized is the error message that should be returned to
// clients when an action is forbidden. It is intentionally vague to prevent
// disclosing information that a client should not have access to.
errUnauthorized = "unauthorized"
errUnauthorized = "forbidden"
)

// UnauthorizedError is the error type for authorization errors
Expand Down
6 changes: 3 additions & 3 deletions coderd/rbac/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
Type: "file",
}

// ResourceOrganization CRUD. Always has an org owner.
// ResourceOrganization CRUD. Has an org owner on all but 'create'.
// create/delete = make or delete organizations
// read = view org information (Can add user owner for read)
// update = ??
Expand Down Expand Up @@ -56,8 +56,8 @@ var (
// ResourceUser is the user in the 'users' table.
// ResourceUser never has any owners or in an org, as it's site wide.
// create/delete = make or delete a new user.
// read = view all user's settings
// update = update all user field & settings
// read = view all 'user' table data
// update = update all 'user' table data
ResourceUser = Object{
Type: "user",
}
Expand Down
10 changes: 8 additions & 2 deletions coderd/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,16 @@ func (api *api) assignableOrgRoles(rw http.ResponseWriter, r *http.Request) {
}

func (api *api) checkPermissions(rw http.ResponseWriter, r *http.Request) {
roles := httpmw.UserRoles(r)
user := httpmw.UserParam(r)

if !api.Authorize(rw, r, rbac.ActionRead, rbac.ResourceUserData.WithOwner(user.ID.String())) {
if !api.Authorize(rw, r, rbac.ActionRead, rbac.ResourceUser.WithOwner(user.ID.String())) {
return
}

// use the roles of the user specified, not the person making the request.
roles, err := api.Database.GetAllUserRoles(r.Context(), user.ID)
if err != nil {
httpapi.Forbidden(rw)
return
}

Expand Down
8 changes: 2 additions & 6 deletions coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,15 +538,11 @@ func (api *api) organizationByUserAndName(rw http.ResponseWriter, r *http.Reques
if errors.Is(err, sql.ErrNoRows) {
// Return unauthorized rather than a 404 to not leak if the organization
// exists.
httpapi.Write(rw, http.StatusUnauthorized, httpapi.Response{
Message: "unauthorized",
})
httpapi.Forbidden(rw)
return
}
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: fmt.Sprintf("get organization by name: %s", err),
})
httpapi.Forbidden(rw)
return
}

Expand Down
0