fix(webui): member changes need the admin grant and owner changes an owner
This commit is contained in:
3 files changed
+114
-6
No files matched your search
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
)
|
||||
@@ -157,6 +158,9 @@ func (s *Server) handleOrgMemberSet(w http.ResponseWriter, r *http.Request) {
|
||||
WorkspaceID: org.WorkspaceID(request.Workspace),
|
||||
Role: org.Role(request.Role),
|
||||
}
|
||||
if !s.mayChangeMembership(w, r, membership, membership.Role) {
|
||||
return
|
||||
}
|
||||
if err := s.Orgs.EnsureMembership(r.Context(), membership); err != nil {
|
||||
writeOrgError(w, err)
|
||||
return
|
||||
@@ -177,6 +181,9 @@ func (s *Server) handleOrgMemberRemove(w http.ResponseWriter, r *http.Request) {
|
||||
OrgID: id,
|
||||
WorkspaceID: org.WorkspaceID(r.URL.Query().Get("workspace")),
|
||||
}
|
||||
if !s.mayChangeMembership(w, r, membership, "") {
|
||||
return
|
||||
}
|
||||
if err := s.Orgs.RemoveMembership(r.Context(), membership); err != nil {
|
||||
writeOrgError(w, err)
|
||||
return
|
||||
@@ -205,6 +212,39 @@ func (s *Server) handleOrgAgentAssign(w http.ResponseWriter, r *http.Request) {
|
||||
// writeOrgError maps the org domain's errors, and their gRPC statuses, onto
|
||||
// HTTP. The remote-client path keeps the status code through unmapError's
|
||||
// wrapping, so writeControlPlaneError names the same code the store sent.
|
||||
// mayChangeMembership refuses, having written the error, a change that grants
|
||||
// the owner role or alters an owner's membership unless the caller is an owner
|
||||
// of the org: member management is an admin's grant, and without this an
|
||||
// admin could make themselves owner -- and an owner of org-sys owns the
|
||||
// instance.
|
||||
func (s *Server) mayChangeMembership(w http.ResponseWriter, r *http.Request, target org.Membership, grant org.Role) bool {
|
||||
principal, ok := access.PrincipalFromContext(r.Context())
|
||||
if !ok {
|
||||
principal = access.SharedTokenOwner()
|
||||
}
|
||||
if principal.Role("") == org.RoleOwner {
|
||||
return true
|
||||
}
|
||||
touchesOwner := grant == org.RoleOwner
|
||||
if !touchesOwner {
|
||||
members, err := s.Orgs.ListMembers(r.Context(), target.OrgID)
|
||||
if err != nil {
|
||||
writeOrgError(w, err)
|
||||
return false
|
||||
}
|
||||
for _, m := range members {
|
||||
if m.IdentityID == target.IdentityID && m.WorkspaceID == target.WorkspaceID && m.Role == org.RoleOwner {
|
||||
touchesOwner = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if touchesOwner {
|
||||
http.Error(w, "only an owner may grant or change the owner role", http.StatusForbidden)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func writeOrgError(w http.ResponseWriter, err error) {
|
||||
switch {
|
||||
case errors.Is(err, org.ErrInvalidRole), errors.Is(err, org.ErrInvalidOrg),
|
||||
|
||||
@@ -8,8 +8,10 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
infraaccess "github.com/samcharles93/archie-core/internal/infrastructure/access"
|
||||
)
|
||||
|
||||
// fakeOrgs is an in-memory org.API that validates memberships the way the
|
||||
@@ -153,3 +155,66 @@ func TestOrgRoutes(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// rolePrincipals gives each identity id the org-sys role of the same name.
|
||||
type rolePrincipals struct{}
|
||||
|
||||
func (rolePrincipals) PrincipalFor(_ context.Context, id identity.IdentityID) (access.Principal, error) {
|
||||
return access.Principal{IdentityID: id, Org: org.DefaultOrgID, Memberships: []org.Membership{
|
||||
{IdentityID: id, OrgID: org.DefaultOrgID, Role: org.Role(id)},
|
||||
}}, nil
|
||||
}
|
||||
|
||||
// TestOrgMembershipNeedsItsRole holds membership changes to the shipped role
|
||||
// policies: a developer manages no members, an admin manages members but can
|
||||
// neither grant the owner role nor change an owner's, and an owner can.
|
||||
func TestOrgMembershipNeedsItsRole(t *testing.T) {
|
||||
engine, err := infraaccess.New(access.ShippedOrgPolicies(org.DefaultOrgID))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
members := "/api/orgs/" + string(org.DefaultOrgID) + "/members/"
|
||||
tests := []struct {
|
||||
name string
|
||||
caller string
|
||||
method string
|
||||
path string
|
||||
body string
|
||||
wantStatus int
|
||||
}{
|
||||
{"a developer cannot add a member", "developer", http.MethodPut, members + "sam", `{"role":"viewer"}`, http.StatusForbidden},
|
||||
{"a developer cannot make itself owner", "developer", http.MethodPut, members + "developer", `{"role":"owner"}`, http.StatusForbidden},
|
||||
{"an admin adds a member", "admin", http.MethodPut, members + "sam", `{"role":"developer"}`, http.StatusOK},
|
||||
{"an admin cannot make itself owner", "admin", http.MethodPut, members + "admin", `{"role":"owner"}`, http.StatusForbidden},
|
||||
{"an admin cannot demote an owner", "admin", http.MethodPut, members + "boss", `{"role":"viewer"}`, http.StatusForbidden},
|
||||
{"an admin cannot remove an owner", "admin", http.MethodDelete, members + "boss", "", http.StatusForbidden},
|
||||
{"an owner grants owner", "owner", http.MethodPut, members + "sam", `{"role":"owner"}`, http.StatusOK},
|
||||
{"a developer cannot assign an agent", "developer", http.MethodPut, "/api/orgs/" + string(org.DefaultOrgID) + "/agents/bot", "", http.StatusForbidden},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
orgs := newFakeOrgs()
|
||||
orgs.members[org.DefaultOrgID] = append(orgs.members[org.DefaultOrgID], org.Member{IdentityID: "boss", Role: org.RoleOwner})
|
||||
s := &Server{
|
||||
Access: engine, Principals: rolePrincipals{}, Orgs: orgs,
|
||||
Authenticate: func(_ context.Context, token string) (identity.Identity, error) {
|
||||
return identity.Identity{ID: identity.IdentityID(token), Kind: identity.KindUser}, nil
|
||||
},
|
||||
}
|
||||
var body io.Reader
|
||||
if tt.body != "" {
|
||||
body = strings.NewReader(tt.body)
|
||||
}
|
||||
req := httptest.NewRequestWithContext(t.Context(), tt.method, tt.path, body)
|
||||
req.Header.Set("Authorization", "Bearer "+tt.caller)
|
||||
if tt.body != "" {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
orgRoutes(s).ServeHTTP(rec, req)
|
||||
if rec.Code != tt.wantStatus {
|
||||
t.Fatalf("status = %d, want %d, body %s", rec.Code, tt.wantStatus, rec.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -137,7 +137,7 @@ func routeOverride(action access.Action, path string) (access.Action, access.Res
|
||||
case matchPrefix(path, "/api/identities"):
|
||||
return actionManage(action), access.KindIdentity, segmentValue(path, 2), true
|
||||
case matchPrefix(path, "/api/orgs"):
|
||||
return orgRouteAction(action), access.KindOrg, segmentValue(path, 2), true
|
||||
return orgRouteAction(action, path), access.KindOrg, segmentValue(path, 2), true
|
||||
case matchPrefix(path, "/api/extensions"):
|
||||
// Installing and enabling an extension runs a process on the host, so it
|
||||
// is the same administration as editing a control-plane resource.
|
||||
@@ -178,12 +178,15 @@ func bindingCommand(action access.Action, path string) (access.Action, bool) {
|
||||
return action, false
|
||||
}
|
||||
|
||||
// orgRouteAction maps an org request: reads are org reads, and every mutation
|
||||
// of the org's workspaces, members or agents is an update of the org itself,
|
||||
// so the read/edit role policies decide it.
|
||||
func orgRouteAction(action access.Action) access.Action {
|
||||
if action == access.ActionRead {
|
||||
// orgRouteAction maps an org request: reads are org reads, changing who
|
||||
// belongs to the org or which agents serve it is member management (an
|
||||
// admin's grant), and any other mutation updates the org.
|
||||
func orgRouteAction(action access.Action, path string) access.Action {
|
||||
switch {
|
||||
case action == access.ActionRead:
|
||||
return access.ActionRead
|
||||
case segmentValue(path, 3) == "members", segmentValue(path, 3) == "agents":
|
||||
return access.ActionManageMembers
|
||||
}
|
||||
return access.ActionUpdate
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user