feat(webui): act in a selected organisation the caller is a member of, enforced at the dashboard and the State Store

This commit is contained in:
sam committed 2026-10-27 11:06:18 +11:00
1 parent 2cf9d435cc
commit 2fb0084d2c
17 files changed
+356 -69

No files matched your search

@@ -3,6 +3,7 @@ package rpcidentity
import (
"context"
"errors"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
@@ -23,14 +24,22 @@ const CallerKey = "state-store-caller"
// for one.
const PrincipalKey = "state-store-principal"
// OrgKey carries the org the principal acts in, which differs from the one it
// serves when the caller selected another org it is a member of.
const OrgKey = "state-store-org"
// Outgoing forwards the context's principal, replacing any stale attribution.
func Outgoing(ctx context.Context, caller string) context.Context {
md, _ := metadata.FromOutgoingContext(ctx)
md = md.Copy()
md.Set(CallerKey, caller)
md.Delete(PrincipalKey)
md.Delete(OrgKey)
if p, ok := access.PrincipalFromContext(ctx); ok {
md.Set(PrincipalKey, string(p.IdentityID))
if p.Org != "" {
md.Set(OrgKey, string(p.Org))
}
}
return metadata.NewOutgoingContext(ctx, md)
}
@@ -42,6 +51,9 @@ func Outgoing(ctx context.Context, caller string) context.Context {
// org; any other acts in the default org.
type Callers struct {
Principals access.PrincipalSource
// Workspaces lets a call act in an org it selected; without it any
// selection other than the principal's own org is refused.
Workspaces access.WorkspaceLister
}
func (c Callers) resolve(ctx context.Context) (context.Context, error) {
@@ -67,6 +79,15 @@ func (c Callers) resolve(ctx context.Context) (context.Context, error) {
if err != nil {
return nil, status.Errorf(codes.Unavailable, "resolve principal: %v", err)
}
// 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)
if err != nil {
if errors.Is(err, access.ErrNotMember) {
return nil, status.Error(codes.PermissionDenied, err.Error())
}
return nil, status.Errorf(codes.Unavailable, "select org: %v", err)
}
ctx = org.WithScope(org.WithOrg(access.WithActor(access.WithPrincipal(ctx, p), id), p.Org), p.Org)
return ctx, nil
}
@@ -44,7 +44,7 @@ func (credentials) DeleteExpiredRunCredentials(context.Context, time.Time) error
type principalsByID map[identity.IdentityID]org.OrgID
func (p principalsByID) PrincipalFor(_ context.Context, id identity.IdentityID) (access.Principal, error) {
return access.Principal{IdentityID: id, Org: p[id]}, nil
return access.Principal{IdentityID: id, Org: p[id], Memberships: []org.Membership{{IdentityID: id, OrgID: p[id], Role: org.RoleDeveloper}}}, nil
}
// TestCallerAttribution pins who a call acts as: a trusted service's
@@ -73,6 +73,8 @@ func TestCallerAttribution(t *testing.T) {
}{
{"service alone acts in the default org", plaintext, []string{rpcidentity.CallerKey, "archied"}, "archied", org.DefaultOrgID, false},
{"forwarded principal sets actor and org", plaintext, []string{rpcidentity.CallerKey, "archie-ui", rpcidentity.PrincipalKey, "alice"}, "alice", "acme", false},
{"forwarded org the principal serves is accepted", plaintext, []string{rpcidentity.CallerKey, "archie-ui", rpcidentity.PrincipalKey, "alice", rpcidentity.OrgKey, "acme"}, "alice", "acme", false},
{"forwarded org the principal is not a member of is refused", plaintext, []string{rpcidentity.CallerKey, "archie-ui", rpcidentity.PrincipalKey, "alice", rpcidentity.OrgKey, "other"}, "", "", true},
{"task grant cannot claim a principal", noCert, []string{tokenMetadataKey, taskToken, rpcidentity.CallerKey, "archie-ui", rpcidentity.PrincipalKey, "alice"}, "task/7", org.DefaultOrgID, false},
{"no certificate and no grant is refused", noCert, []string{rpcidentity.CallerKey, "archied"}, "", "", true},
}