fix(webui): resolve a selected org's workspaces as the caller, wire selection into gateway and harness, and let a member start tasks in the org they selected
This commit is contained in:
1 parent
c3d49cbc6c
commit
4bb473c0b0
12 files changed
+101
-21
No files matched your search
@@ -6,6 +6,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -54,7 +55,17 @@ func (f *fakeOrgs) GetOrg(_ context.Context, id org.OrgID) (org.Org, error) {
|
||||
return org.Org{}, org.ErrOrgNotFound
|
||||
}
|
||||
|
||||
func (f *fakeOrgs) ListWorkspaces(_ context.Context, id org.OrgID) ([]org.Workspace, error) {
|
||||
// ListWorkspaces answers as the State Store does: the org asked for only to a
|
||||
// caller who is a member of it, otherwise the caller's own org, and the
|
||||
// system org when the call carries no principal.
|
||||
func (f *fakeOrgs) ListWorkspaces(ctx context.Context, id org.OrgID) ([]org.Workspace, error) {
|
||||
p, ok := access.PrincipalFromContext(ctx)
|
||||
switch {
|
||||
case !ok:
|
||||
id = org.DefaultOrgID
|
||||
case !p.Member(id):
|
||||
id = p.Org
|
||||
}
|
||||
return f.workspaces[id], nil
|
||||
}
|
||||
|
||||
@@ -450,19 +461,24 @@ func TestOrgSelection(t *testing.T) {
|
||||
wantStatus int
|
||||
wantCookie string // value of the org cookie the response sets, "-" for none
|
||||
wantBody string
|
||||
// wantWorkspace is a workspace the acting principal must carry,
|
||||
// the selected org's own and not another's.
|
||||
wantWorkspace org.WorkspaceID
|
||||
}{
|
||||
{"selects a member org", http.MethodPut, "/api/orgs/current", `{"id":"acme"}`, "", http.StatusNoContent, "acme", ""},
|
||||
{"refuses an org it is not a member of", http.MethodPut, "/api/orgs/current", `{"id":"beta"}`, "", http.StatusForbidden, "-", ""},
|
||||
{"acts in the selected org", http.MethodGet, "/api/orgs", "", "acme", http.StatusOK, "-", `"current":"acme"`},
|
||||
{"ignores and clears a cookie for a non-member org", http.MethodGet, "/api/orgs", "", "beta", http.StatusOK, "", `"current":"org-sys"`},
|
||||
{"lists only the orgs it may select", http.MethodGet, "/api/orgs", "", "", http.StatusOK, "-", `"member_orgs":["org-sys","acme"]`},
|
||||
{"selects a member org", http.MethodPut, "/api/orgs/current", `{"id":"acme"}`, "", http.StatusNoContent, "acme", "", ""},
|
||||
{"refuses an org it is not a member of", http.MethodPut, "/api/orgs/current", `{"id":"beta"}`, "", http.StatusForbidden, "-", "", ""},
|
||||
{"acts in the selected org", http.MethodGet, "/api/orgs", "", "acme", http.StatusOK, "-", `"current":"acme"`, "acme-ws"},
|
||||
{"ignores and clears a cookie for a non-member org", http.MethodGet, "/api/orgs", "", "beta", http.StatusOK, "", `"current":"org-sys"`, ""},
|
||||
{"lists only the orgs it may select", http.MethodGet, "/api/orgs", "", "", http.StatusOK, "-", `"member_orgs":["org-sys","acme"]`, ""},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
orgs := newFakeOrgs()
|
||||
orgs.orgs = append(orgs.orgs, org.Org{ID: "acme"}, org.Org{ID: "beta"})
|
||||
orgs.workspaces["acme"] = []org.Workspace{{ID: "acme-ws", OrgID: "acme"}}
|
||||
chain := &recordingChain{}
|
||||
s := &Server{
|
||||
Access: allowAll{}, Principals: memberPrincipals{}, Orgs: orgs,
|
||||
Access: chain, Principals: memberPrincipals{}, Orgs: orgs,
|
||||
Authenticate: func(_ context.Context, token string) (identity.Identity, error) {
|
||||
return identity.Identity{ID: identity.IdentityID(token), Kind: identity.KindUser}, nil
|
||||
},
|
||||
@@ -496,6 +512,19 @@ func TestOrgSelection(t *testing.T) {
|
||||
if !strings.Contains(rec.Body.String(), tt.wantBody) {
|
||||
t.Fatalf("body = %s, want it to contain %q", rec.Body.String(), tt.wantBody)
|
||||
}
|
||||
if tt.wantWorkspace != "" && !slices.ContainsFunc(chain.last.Workspaces, func(w org.Workspace) bool { return w.ID == tt.wantWorkspace }) {
|
||||
t.Fatalf("acting principal workspaces = %v, want %q", chain.last.Workspaces, tt.wantWorkspace)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// recordingChain allows everything and keeps the last principal it judged.
|
||||
type recordingChain struct{ last access.Principal }
|
||||
|
||||
func (c *recordingChain) Authorize(p access.Principal, _ access.Action, _ access.Resource, _ access.Context) access.Decision {
|
||||
c.last = p
|
||||
return access.Allowed()
|
||||
}
|
||||
|
||||
func (*recordingChain) Ready() bool { return true }
|
||||
@@ -21,7 +21,10 @@ func (s *Server) selectedOrg(w http.ResponseWriter, r *http.Request, principal a
|
||||
if chosen == "" {
|
||||
return principal, nil
|
||||
}
|
||||
selected, err := principal.Select(r.Context(), org.OrgID(chosen), s.workspaces())
|
||||
// The lookup runs as the caller, so the State Store knows whose
|
||||
// memberships entitle it to the chosen org's workspaces.
|
||||
ctx := org.WithOrg(access.WithPrincipal(r.Context(), principal), principal.Org)
|
||||
selected, err := principal.Select(ctx, org.OrgID(chosen), s.workspaces())
|
||||
if errors.Is(err, access.ErrNotMember) {
|
||||
s.setOrgCookie(w, r, "")
|
||||
return principal, nil
|
||||
|
||||
Reference in new issue
Block a user