feat(archied): dispatch every organisation's schedules

This commit is contained in:
sam committed 2026-10-17 10:19:32 +11:00
1 parent fd833aefec
commit eeeb88d565
3 files changed
+159 -27

No files matched your search

+19 -5
View File
@@ -71,8 +71,9 @@ func (s *server) ListOrgs(ctx context.Context, _ *pb.ListOrgsRequest) (*pb.ListO
if err != nil {
return nil, s.orgErr("ListOrgs", err)
}
// Only an instance admin sees every org; anyone else sees their own.
if p, ok := access.PrincipalFromContext(ctx); !ok || !p.InstanceAdmin() {
// Only an instance admin or an internal service sees every org; anyone
// else sees their own.
if !readsEveryOrg(ctx) {
caller := org.OrgFromContext(ctx)
values = slices.DeleteFunc(values, func(o org.Org) bool { return o.ID != caller })
}
@@ -83,8 +84,17 @@ func (s *server) ListOrgs(ctx context.Context, _ *pb.ListOrgsRequest) (*pb.ListO
return &pb.ListOrgsResponse{Orgs: out}, nil
}
// orgInForce is the org a request is served for: the one the caller's
// principal carries. A request field cannot choose another tenant.
// readsEveryOrg reports whether ctx may read another org's directory: an
// instance admin, or an internal service acting for no one and confined to no
// org, such as the daemon ticking every org's schedules.
func readsEveryOrg(ctx context.Context) bool {
if p, ok := access.PrincipalFromContext(ctx); ok {
return p.InstanceAdmin()
}
_, scoped := org.Scope(ctx)
return !scoped
}
// orgFor is the org a request acts in: the caller's own, or the one it names
// when the caller is an instance admin. Anyone else naming another org still
// acts in their own, so the contract cannot reach another tenant.
@@ -221,7 +231,11 @@ func (s *server) ListOrgAgents(ctx context.Context, r *pb.ListOrgAgentsRequest)
if s.deps.Orgs == nil {
return nil, errOrgsUnavailable
}
values, err := s.deps.Orgs.ListOrgAgents(ctx, orgFor(ctx, r.GetOrgId()))
owner := orgFor(ctx, r.GetOrgId())
if r.GetOrgId() != "" && readsEveryOrg(ctx) {
owner = org.OrgID(r.GetOrgId())
}
values, err := s.deps.Orgs.ListOrgAgents(ctx, owner)
if err != nil {
return nil, s.orgErr("ListOrgAgents", err)
}
@@ -78,7 +78,7 @@ func (*fakeOrgs) OrgForIdentity(context.Context, identity.IdentityID) (org.OrgID
func TestOrgRPCsActInTheCallersOrg(t *testing.T) {
repo := &fakeOrgs{}
s := &server{deps: Deps{Orgs: repo, Log: slog.New(slog.DiscardHandler)}}
ctx := org.WithOrg(context.Background(), "acme")
ctx := org.WithScope(org.WithOrg(context.Background(), "acme"), "acme")
got, err := s.GetOrg(ctx, &pb.GetOrgRequest{OrgId: "other"})
if err != nil || got.GetOrg().GetId() != "acme" {
@@ -97,6 +97,12 @@ func TestOrgRPCsActInTheCallersOrg(t *testing.T) {
if err != nil || len(agents.GetAgents()) != 1 || agents.Agents[0].OrgId != "acme" {
t.Fatalf("assignment read escaped org: %v %v", agents, err)
}
// An unscoped internal service, the daemon ticking every org's
// schedules, reads the org it names.
agents, err = s.ListOrgAgents(context.Background(), &pb.ListOrgAgentsRequest{OrgId: "acme"})
if err != nil || len(agents.GetAgents()) != 1 {
t.Fatalf("internal service could not read a named org: %v %v", agents, err)
}
if _, err := s.CreateWorkspace(ctx, &pb.CreateWorkspaceRequest{OrgId: "other", Id: "ws", Name: "Workspace"}); err != nil {
t.Fatal(err)
}