- Notifications
You must be signed in to change notification settings - Fork928
feat: Add rbac to templateversion+orgmember endpoints#1713
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
a96a5ba
99d4073
f86484e
65ccc98
3f4888b
d7f29a6
eb7ffd4
24430ff
5646b7b
4ee5eca
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -28,12 +28,12 @@ func OrganizationParam(r *http.Request) database.Organization { | ||
funcOrganizationMemberParam(r*http.Request) database.OrganizationMember { | ||
organizationMember,ok:=r.Context().Value(organizationMemberParamContextKey{}).(database.OrganizationMember) | ||
if!ok { | ||
panic("developer error: organizationmemberparam middleware not provided") | ||
} | ||
returnorganizationMember | ||
} | ||
// ExtractOrganizationParam grabs an organization from the "organization" URL parameter. | ||
// This middleware requires the API key middleware higher in the call stack for authentication. | ||
funcExtractOrganizationParam(db database.Store)func(http.Handler) http.Handler { | ||
returnfunc(next http.Handler) http.Handler { | ||
@@ -56,11 +56,23 @@ func ExtractOrganizationParam(db database.Store) func(http.Handler) http.Handler | ||
}) | ||
return | ||
} | ||
ctx:=context.WithValue(r.Context(),organizationParamContextKey{},organization) | ||
next.ServeHTTP(rw,r.WithContext(ctx)) | ||
}) | ||
} | ||
} | ||
// ExtractOrganizationMemberParam grabs a user membership from the "organization" and "user" URL parameter. | ||
// This middleware requires the ExtractUser and ExtractOrganization middleware higher in the stack | ||
funcExtractOrganizationMemberParam(db database.Store)func(http.Handler) http.Handler { | ||
Comment on lines +65 to +67 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. Before So I changed this to use the user from | ||
returnfunc(next http.Handler) http.Handler { | ||
returnhttp.HandlerFunc(func(rw http.ResponseWriter,r*http.Request) { | ||
organization:=OrganizationParam(r) | ||
user:=UserParam(r) | ||
organizationMember,err:=db.GetOrganizationMemberByUserID(r.Context(), database.GetOrganizationMemberByUserIDParams{ | ||
OrganizationID:organization.ID, | ||
UserID:user.ID, | ||
}) | ||
iferrors.Is(err,sql.ErrNoRows) { | ||
httpapi.Write(rw,http.StatusForbidden, httpapi.Response{ | ||
@@ -74,9 +86,8 @@ func ExtractOrganizationParam(db database.Store) func(http.Handler) http.Handler | ||
}) | ||
return | ||
} | ||
ctx:=context.WithValue(r.Context(),organizationMemberParamContextKey{},organizationMember) | ||
next.ServeHTTP(rw,r.WithContext(ctx)) | ||
}) | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -3,6 +3,7 @@ package rbac | ||
import ( | ||
"context" | ||
_ "embed" | ||
"golang.org/x/xerrors" | ||
"github.com/open-policy-agent/opa/rego" | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -135,6 +135,12 @@ var ( | ||
Action: ActionRead, | ||
ResourceID: "*", | ||
}, | ||
{ | ||
// Can read available roles. | ||
ResourceType: ResourceOrgRoleAssignment.Type, | ||
ResourceID: "*", | ||
Action: ActionRead, | ||
}, | ||
}, | ||
}, | ||
} | ||
@@ -217,6 +223,37 @@ func SiteRoles() []Role { | ||
return roles | ||
} | ||
// ChangeRoleSet is a helper function that finds the difference of 2 sets of | ||
// roles. When setting a user's new roles, it is equivalent to adding and | ||
// removing roles. This set determines the changes, so that the appropriate | ||
// RBAC checks can be applied using "ActionCreate" and "ActionDelete" for | ||
// "added" and "removed" roles respectively. | ||
func ChangeRoleSet(from []string, to []string) (added []string, removed []string) { | ||
Emyrk marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
has := make(map[string]struct{}) | ||
for _, exists := range from { | ||
has[exists] = struct{}{} | ||
} | ||
for _, roleName := range to { | ||
// If the user already has the role assigned, we don't need to check the permission | ||
// to reassign it. Only run permission checks on the difference in the set of | ||
// roles. | ||
if _, ok := has[roleName]; ok { | ||
delete(has, roleName) | ||
continue | ||
} | ||
added = append(added, roleName) | ||
} | ||
// Remaining roles are the ones removed/deleted. | ||
for roleName := range has { | ||
removed = append(removed, roleName) | ||
} | ||
return added, removed | ||
} | ||
// roleName is a quick helper function to return | ||
// role_name:scopeID | ||
// If no scopeID is required, only 'role_name' is returned | ||
Uh oh!
There was an error while loading.Please reload this page.