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:
sam committed 2026-10-27 11:06:18 +11:00
1 parent c3d49cbc6c
commit 4bb473c0b0
12 files changed
+101 -21

No files matched your search

+3 -1
View File
@@ -19,6 +19,7 @@ import (
"github.com/samcharles93/archie-core/internal/container"
daemonpb "github.com/samcharles93/archie-core/internal/contracts/daemon/v1"
pb "github.com/samcharles93/archie-core/internal/contracts/harness/v1"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/presence"
"github.com/samcharles93/archie-core/internal/infrastructure/harnesssession"
@@ -207,7 +208,8 @@ func (b *boot) setupDaemonRPC(ctx context.Context) {
b.log.WarnContext(ctx, "harness sessions disabled: listen", "err", err)
return
}
callers := rpcidentity.Callers{Principals: b.d.Principals}
workspaces, _ := b.stateStore.(access.WorkspaceLister)
callers := rpcidentity.Callers{Principals: b.d.Principals, Workspaces: workspaces}
srv := grpc.NewServer(append(servicekit.ServerLimits(), creds, grpc.ChainUnaryInterceptor(callers.Unary()), grpc.ChainStreamInterceptor(callers.Stream()))...)
daemonpb.RegisterDaemonServiceServer(srv, &repositoryServer{boot: b})
// Terminal sessions still require certificates and a container launcher.
+2 -2
View File
@@ -87,12 +87,12 @@ const inboundMessageHeadroomBytes = 1 << 20
// request carries a channel attachment's bytes: the channel frontend
// downloads the file, and this process -- which runs the turn -- holds no
// platform credential to fetch it again.
func gatewayServerOpts(listen string, id *rpctls.Identity, principals access.PrincipalSource) ([]grpc.ServerOption, error) {
func gatewayServerOpts(listen string, id *rpctls.Identity, principals access.PrincipalSource, workspaces access.WorkspaceLister) ([]grpc.ServerOption, error) {
creds, err := id.ServerOption(listen, false)
if err != nil {
return nil, fmt.Errorf("gateway: %w", err)
}
callers := rpcidentity.Callers{Principals: principals}
callers := rpcidentity.Callers{Principals: principals, Workspaces: workspaces}
return []grpc.ServerOption{
creds,
grpc.MaxRecvMsgSize(messaging.MaxInboundAttachmentBytes + inboundMessageHeadroomBytes),
+1 -1
View File
@@ -128,7 +128,7 @@ func Run(ctx context.Context, options Options) error {
if err != nil {
return err
}
opts, err := gatewayServerOpts(listen, b.id, b.stateStore)
opts, err := gatewayServerOpts(listen, b.id, b.stateStore, b.stateStore)
if err != nil {
return err
}
+1 -1
View File
@@ -131,7 +131,7 @@ func (d *Daemon) mayRunWorkflow(ctx context.Context, task *workflow.Task, id ide
if d.Access == nil || d.Principals == nil {
return true, nil
}
principal, err := d.Principals.PrincipalFor(ctx, id)
principal, err := d.principalIn(ctx, id, task.Org)
if err != nil {
d.Log.WarnContext(ctx, "workflow identity: principal unavailable", "task", task.ID, "identity", id, "err", err)
return false, nil
+20 -4
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/taskstate"
@@ -24,15 +25,30 @@ func (d *Daemon) workflowContext(ctx context.Context, task *workflow.Task) (cont
}
return ctx, nil
}
principal, err := d.Principals.PrincipalFor(ctx, d.publisherIdentity(task.Identity))
principal, err := d.principalIn(ctx, d.publisherIdentity(task.Identity), owner)
if err != nil {
return nil, pinFailure{taskstate.ParkTransient, fmt.Errorf("workflow principal: %w", err)}
}
if principal.Org == "" {
principal.Org = org.DefaultOrgID
}
if principal.Org != owner {
return nil, pinFailure{taskstate.ParkNeedsDecision, fmt.Errorf("workflow identity belongs to org %s, task belongs to %s", principal.Org, owner)}
}
return org.WithOrg(access.WithPrincipal(ctx, principal), owner), nil
}
// principalIn is id's principal acting in owner: the org it serves, or one it
// is a member of, since a person's task belongs to the org they selected. An
// identity that is no member keeps its own org, which callers refuse.
func (d *Daemon) principalIn(ctx context.Context, id identity.IdentityID, owner org.OrgID) (access.Principal, error) {
principal, err := d.Principals.PrincipalFor(ctx, id)
if err != nil {
return access.Principal{}, err
}
if principal.Org == "" {
principal.Org = org.DefaultOrgID
}
if principal.Org == owner || !principal.Member(owner) {
return principal, nil
}
lister, _ := d.Store.(access.WorkspaceLister)
return principal.Select(org.WithOrg(access.WithPrincipal(ctx, principal), principal.Org), owner, lister)
}
+18 -2
View File
@@ -30,6 +30,20 @@ func (p workflowPrincipals) PrincipalFor(_ context.Context, id identity.Identity
return access.Principal{IdentityID: id, Org: owner}, nil
}
// memberOf is a person who serves one org and holds a membership in another.
type memberOf struct{ home, other org.OrgID }
func (m memberOf) PrincipalFor(_ context.Context, id identity.IdentityID) (access.Principal, error) {
return access.Principal{IdentityID: id, Org: m.home, Memberships: []org.Membership{{IdentityID: id, OrgID: m.home}, {IdentityID: id, OrgID: m.other}}}, nil
}
// listsWorkspaces lets a principal select an org, as the State Store client does.
type listsWorkspaces struct{ *identityStore }
func (listsWorkspaces) ListWorkspaces(context.Context, org.OrgID) ([]org.Workspace, error) {
return nil, nil
}
type orgBindingStore struct {
storecontract.TaskStore
storecontract.BindingStore
@@ -122,7 +136,7 @@ func TestPinWorkflowInTaskOrg(t *testing.T) {
for _, tc := range []struct {
name string
owner org.OrgID
principals workflowPrincipals
principals access.PrincipalSource
want string
refused bool
contextOrg org.OrgID
@@ -130,11 +144,13 @@ func TestPinWorkflowInTaskOrg(t *testing.T) {
{"legacy default org", "", nil, shared, false, ""},
{"non-default org definition", "acme", workflowPrincipals{"bot": "acme"}, custom, false, ""},
{"another org cannot supply the definition", "acme", workflowPrincipals{"bot": org.DefaultOrgID}, "", true, ""},
{"a member of the task's org may start it from another", "acme", memberOf{home: org.DefaultOrgID, other: "acme"}, custom, false, ""},
{"a non-member cannot start a task in another org", "acme", memberOf{home: org.DefaultOrgID, other: "beta"}, "", true, ""},
{"unresolved org is refused", "acme", nil, "", true, ""},
{"unresolved context cannot select another org", "", nil, "", true, "acme"},
} {
t.Run(tc.name, func(t *testing.T) {
d := &Daemon{Store: &identityStore{}, RootIdentityID: "bot", WorkflowDefinitions: orgDefinitions{org.DefaultOrgID: shared, "acme": custom}}
d := &Daemon{Store: listsWorkspaces{&identityStore{}}, RootIdentityID: "bot", WorkflowDefinitions: orgDefinitions{org.DefaultOrgID: shared, "acme": custom}}
if tc.principals != nil {
d.Principals = tc.principals
}
@@ -420,6 +420,8 @@ SELECT
$10::text
FROM number, (SELECT COALESCE(
(SELECT a.org_id FROM org_agents a WHERE a.identity_id = $7),
(SELECT m.org_id FROM memberships m WHERE m.identity_id = $7
AND m.org_id = $11::text LIMIT 1),
(SELECT m.org_id FROM memberships m WHERE m.identity_id = $7
ORDER BY m.created_at, m.org_id, m.workspace_id NULLS LAST LIMIT 1),
'org-sys'
@@ -88,6 +88,8 @@ SELECT
sqlc.arg(draft_digest)::text
FROM number, (SELECT COALESCE(
(SELECT a.org_id FROM org_agents a WHERE a.identity_id = sqlc.arg(identity)),
(SELECT m.org_id FROM memberships m WHERE m.identity_id = sqlc.arg(identity)
AND m.org_id = sqlc.narg(scope_org)::text LIMIT 1),
(SELECT m.org_id FROM memberships m WHERE m.identity_id = sqlc.arg(identity)
ORDER BY m.created_at, m.org_id, m.workspace_id NULLS LAST LIMIT 1),
'org-sys'
@@ -81,7 +81,7 @@ func (c Callers) resolve(ctx context.Context) (context.Context, error) {
}
// The forwarded org is a claim: the membership check, not the metadata,
// decides whether the call may act there.
p, err = p.Select(ctx, org.OrgID(first(md, OrgKey)), c.Workspaces)
p, err = p.Select(org.WithOrg(access.WithPrincipal(ctx, p), p.Org), org.OrgID(first(md, OrgKey)), c.Workspaces)
if err != nil {
if errors.Is(err, access.ErrNotMember) {
return nil, status.Error(codes.PermissionDenied, err.Error())
+11 -1
View File
@@ -109,6 +109,16 @@ func orgFor(ctx context.Context, requested string) org.OrgID {
return caller
}
// memberOrgFor is orgFor, also naming any org the caller is a member of: a
// member selecting an org needs that org's workspaces to act in it, and sees
// nothing it could not already see as a member.
func memberOrgFor(ctx context.Context, requested string) org.OrgID {
if p, ok := access.PrincipalFromContext(ctx); ok && requested != "" && p.Member(org.OrgID(requested)) {
return org.OrgID(requested)
}
return orgFor(ctx, requested)
}
func (s *orgServer) GetOrg(ctx context.Context, r *pb.GetOrgRequest) (*pb.GetOrgResponse, error) {
if s.deps.Orgs == nil {
return nil, errOrgsUnavailable
@@ -144,7 +154,7 @@ func (s *orgServer) ListWorkspaces(ctx context.Context, r *pb.ListWorkspacesRequ
if s.deps.Orgs == nil {
return nil, errOrgsUnavailable
}
values, err := s.deps.Orgs.ListWorkspaces(ctx, orgFor(ctx, r.GetOrgId()))
values, err := s.deps.Orgs.ListWorkspaces(ctx, memberOrgFor(ctx, r.GetOrgId()))
if err != nil {
return nil, s.orgErr("ListWorkspaces", err)
}
+36 -7
View File
@@ -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 }
+4 -1
View File
@@ -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