Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: Implement list roles & enforce authorize examples#1273

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Emyrk merged 16 commits intomainfromstevenmasley/list_roles
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
16 commits
Select commitHold shift + click to select a range
30e2031
feat: First draft of adding authorize to an http endpoint
EmyrkMay 3, 2022
2161f84
WIP: Using middleware to change auth object params
EmyrkMay 3, 2022
54bc054
feat: Implement basic authorize and unit test
EmyrkMay 3, 2022
d083a7c
Some cleanup
EmyrkMay 3, 2022
95b9a14
Merge remote-tracking branch 'origin/main' into stevenmasley/list_roles
EmyrkMay 3, 2022
1498dcd
Expand 'orgs' to 'organizations' in func namings
EmyrkMay 3, 2022
f36ae37
Renamings
EmyrkMay 3, 2022
b831260
Use rbac.object directly
EmyrkMay 3, 2022
db04d67
Fix broken tests
EmyrkMay 3, 2022
b76f373
Add some comments
EmyrkMay 3, 2022
117f838
Linting
EmyrkMay 3, 2022
42b42ab
Handle out of order lists
EmyrkMay 3, 2022
0efe72c
Add unit test
EmyrkMay 3, 2022
dba617d
Add unit test for mw
EmyrkMay 3, 2022
190940f
parallel unit test
EmyrkMay 3, 2022
c86c67c
style order
EmyrkMay 3, 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
PrevPrevious commit
NextNext commit
Use rbac.object directly
  • Loading branch information
@Emyrk
Emyrk committedMay 3, 2022
commitb831260332af2956d66fa56f73e5828a3a00ff9d
19 changes: 4 additions & 15 deletionscoderd/httpmw/authorize.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,23 +13,15 @@ import (
"github.com/coder/coder/coderd/rbac"
)

// AuthObject wraps the rbac object type for middleware to customize this value
// before being passed to Authorize().
type AuthObject struct {
// Object is that base static object the above functions can modify.
Object rbac.Object
}

// Authorize will enforce if the user roles can complete the action on the AuthObject.
// The organization and owner are found using the ExtractOrganization and
// ExtractUser middleware if present.
func Authorize(logger slog.Logger, auth *rbac.RegoAuthorizer, action rbac.Action) func(http.Handler) http.Handler {
Copy link
Member

Choose a reason for hiding this comment

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

Authorize feels like action, not something that would return a handler.

What do you think about renaming this toEnforceRBAC?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I thinkEnforceRBAC is also weak though. I was thinking the packagehttpmw provides enough context, and it does do the actionAuthorize().

Authorize is the correct word for what is happening, as it's not authentication. I feelEnforceRBAC doesn't indicate theobject andaction are included.

Another word that comes to mind is "Access". Idk,EnforceAccess,EnforcePermissions. MaybeEnforceRBAC isn't that bad, just felt odd to me at first.

Copy link
Member

Choose a reason for hiding this comment

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

Fair enough. I'm primarily trying to display that theRBAC package is being leveraged when calling this handle.Enforce is a bit sketchy too.

While it isauthorizing, I'm nervous that this will get conflated with authentication really easily.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

yea this is classic authorization vs authentication. If you aren't familiar with it, it's easy to mix up.

kylecarbs reacted with thumbs up emoji
Copy link
Member

Choose a reason for hiding this comment

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

Agreed agreed

return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
roles := UserRoles(r)
args :=GetAuthObject(r)
object :=authObject(r)

object := args.Object
if object.Type == "" {
panic("developer error: auth object has no type")
}
Expand DownExpand Up@@ -80,8 +72,8 @@ func Authorize(logger slog.Logger, auth *rbac.RegoAuthorizer, action rbac.Action
type authObjectKey struct{}

// APIKey returns the API key from the ExtractAPIKey handler.
funcGetAuthObject(r *http.Request)AuthObject {
obj, ok := r.Context().Value(authObjectKey{}).(AuthObject)
funcauthObject(r *http.Request)rbac.Object {
obj, ok := r.Context().Value(authObjectKey{}).(rbac.Object)
if !ok {
panic("developer error: auth object middleware not provided")
}
Expand All@@ -93,10 +85,7 @@ func GetAuthObject(r *http.Request) AuthObject {
func WithRBACObject(object rbac.Object) func(http.Handler) http.Handler {
Copy link
Member

Choose a reason for hiding this comment

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

It might be confusing that this is calledWithRBACObject, but the values arerbac.ResourceX. It could be helpful to make these the same names!

return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
ao := GetAuthObject(r)
ao.Object = object

ctx := context.WithValue(r.Context(), authObjectKey{}, ao)
ctx := context.WithValue(r.Context(), authObjectKey{}, object)
next.ServeHTTP(rw, r.WithContext(ctx))
})
}
Expand Down
2 changes: 1 addition & 1 deletioncoderd/roles_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -39,7 +39,7 @@ func TestListRoles(t *testing.T) {
Roles: []string{rbac.RoleOrgMember(admin.OrganizationID), rbac.RoleOrgAdmin(admin.OrganizationID)},
},
)
require.NoError(t, err)
require.NoError(t, err, "update org member roles")

testCases := []struct {
Name string
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp