-
Notifications
You must be signed in to change notification settings - Fork 881
feat: Rbac more coderd endpoints, unit test to confirm #1406
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
bed0f8f
feat: Enforce authorize call on all endpoints
Emyrk af6dc5f
Add more endpoints to the unit test
Emyrk 01b2c94
Merge remote-tracking branch 'origin/main' into stevenmasley/rbac_end…
Emyrk be5b0b3
Rbac users endpoints
Emyrk 970e345
Make test pass by skipping missed endpoints
Emyrk 945e9fa
Fix broken tests
Emyrk fdfef88
Import order
Emyrk 89a3678
PR comment fixes
Emyrk 29da9aa
Merge remote-tracking branch 'origin/main' into stevenmasley/rbac_end…
Emyrk 63727e0
omit another endpoint
Emyrk 96a5727
Cleanup comments
Emyrk 4b6c9b0
Do not leak if an organization name exists
Emyrk cd2fda7
Update comment
Emyrk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add more endpoints to the unit test
- Loading branch information
commit af6dc5fce6104a8ff9ffb82751fbbb29ea3c30a0
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,9 @@ package coderd_test | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/go-chi/chi/v5" | ||
|
@@ -32,39 +34,65 @@ func TestBuildInfo(t *testing.T) { | |
func TestAuthorizeAllEndpoints(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. praise: This is a really useful check to have. Nice one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 |
||
t.Parallel() | ||
|
||
// skipRoutes allows skipping routes from being checked. | ||
type routeCheck struct { | ||
NoAuthorize bool | ||
} | ||
assertRoute := map[string]routeCheck{ | ||
"GET:/api/v2": {NoAuthorize: true}, | ||
"GET:/api/v2/buildinfo": {NoAuthorize: true}, | ||
} | ||
|
||
authorizer := &fakeAuthorizer{} | ||
srv, client := coderdtest.NewMemoryCoderd(t, &coderdtest.Options{ | ||
Authorizer: authorizer, | ||
}) | ||
admin := coderdtest.CreateFirstUser(t, client) | ||
var _ = admin | ||
|
||
// skipRoutes allows skipping routes from being checked. | ||
type routeCheck struct { | ||
NoAuthorize bool | ||
AssertObject rbac.Object | ||
} | ||
assertRoute := map[string]routeCheck{ | ||
"GET:/api/v2": {NoAuthorize: true}, | ||
"GET:/api/v2/buildinfo": {NoAuthorize: true}, | ||
"GET:/api/v2/users/first": {NoAuthorize: true}, | ||
"POST:/api/v2/users/first": {NoAuthorize: true}, | ||
"POST:/api/v2/users/login": {NoAuthorize: true}, | ||
"POST:/api/v2/users/logout": {NoAuthorize: true}, | ||
"GET:/api/v2/users/authmethods": {NoAuthorize: true}, | ||
|
||
// TODO: @emyrk these need to be fixed by adding authorize calls | ||
"/api/v2/organizations/{organization}/provisionerdaemons": {NoAuthorize: true}, | ||
"GET:/api/v2/organizations/{organization}": {AssertObject: rbac.ResourceOrganization.InOrg(admin.OrganizationID)}, | ||
} | ||
|
||
c := srv.Config.Handler.(*chi.Mux) | ||
err := chi.Walk(c, func(method string, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error { | ||
name := method + ":" + route | ||
t.Run(name, func(t *testing.T) { | ||
authorizer.reset() | ||
routeAssertions, ok := assertRoute[name] | ||
routeAssertions, ok := assertRoute[strings.TrimRight(name, "/")] | ||
if !ok { | ||
// By default, all omitted routes check for just "authorize" called | ||
routeAssertions = routeCheck{} | ||
} | ||
|
||
// Replace all url params with expected | ||
route = strings.ReplaceAll(route, "{organization}", admin.OrganizationID.String()) | ||
|
||
resp, err := client.Request(context.Background(), method, route, nil) | ||
require.NoError(t, err, "do req") | ||
_ = resp.Body.Close() | ||
|
||
if !routeAssertions.NoAuthorize { | ||
assert.NotNil(t, authorizer.Called, "authorizer expected") | ||
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode, "expect unauthorized") | ||
if routeAssertions.AssertObject.Type != "" { | ||
assert.Equal(t, routeAssertions.AssertObject.Type, authorizer.Called.Object.Type, "resource type") | ||
} | ||
if routeAssertions.AssertObject.Owner != "" { | ||
assert.Equal(t, routeAssertions.AssertObject.Owner, authorizer.Called.Object.Owner, "resource owner") | ||
} | ||
if routeAssertions.AssertObject.OrgID != "" { | ||
assert.Equal(t, routeAssertions.AssertObject.OrgID, authorizer.Called.Object.OrgID, "resource org") | ||
} | ||
if routeAssertions.AssertObject.ResourceID != "" { | ||
assert.Equal(t, routeAssertions.AssertObject.ResourceID, authorizer.Called.Object.ResourceID, "resource ID") | ||
} | ||
} else { | ||
assert.Nil(t, authorizer.Called, "authorize not expected") | ||
} | ||
|
@@ -92,7 +120,7 @@ func (f *fakeAuthorizer) AuthorizeByRoleName(ctx context.Context, subjectID stri | |
Action: action, | ||
Object: object, | ||
} | ||
return nil | ||
return rbac.ForbiddenWithInternal(fmt.Errorf("fake implementation"), nil, nil) | ||
} | ||
|
||
func (f *fakeAuthorizer) reset() { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This indent makes this harder to read. Can we not push this middleware into the r.Use() block?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just for 1 endpoint. I can put it inside a group block by itself with use?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.