Files
archie/internal/domain/org/org.go
T
sam 03b43fc07b Feat/org epic (#1634)
* fix(postgres): scope OpenPRs to the caller's org

A principal-carrying caller read every org's pr_open tasks. The query now
honours the request scope; the daemon's unscoped review scan still sees all
orgs.

Issue: archie-core-kgxw.23

* fix(statestore): serve task logs only to the task's org

ReadTaskLog and StreamTaskLogContent took a task ID and read the file with
no org check. The State Store's task-log reader now confirms the task is
visible under the caller's scope first; unscoped services read as before.

Issue: archie-core-kgxw.24

* fix(postgres): scope ListPolicies to the caller's org

Every org's policy text reached any principal-carrying caller. A scoped read
now sees its own org's policies, plus instance policies when scoped to the
system org. The engines and the lockout guard decide for every org, so they
read the full chain unscoped: the dashboard engine loads without the
request's principal and the guard lifts the caller's scope.

Issue: archie-core-kgxw.25

* fix(postgres): scope identity reads to the caller's org

ListIdentities and GetIdentity (and its alias fallback) returned every
identity to any caller. A caller scoped to an org now sees only that org's
members and assigned agents; the system org and unscoped services still see
the whole instance.

Issue: archie-core-kgxw.26

* fix(postgres): hide instance status from other orgs

ListPresence, ListChannelStatus, ListApplyStatus and GetConfigSnapshot
answered any org principal with instance-wide state. They now return nothing
unless the request is unscoped or scoped to the system org. The dashboard
reads the published config as itself, since its handlers project it for
callers of every org.

Issue: archie-core-kgxw.27

* feat(postgres): assign new agents an org and role at creation

A bot or service account created through the API served no org until the
next State Store boot granted it one. Create now assigns it to the org the
request acts in with the developer role in the same transaction, so it is
authorized immediately.

Issue: archie-core-kgxw.14

* feat(edastore): create event records in a chosen workspace

Sources are created in a workspace of the caller's org, the default one when
none is named; event types and bindings take their source's workspace and
mappings their event type's, the same rule the org backfill applied. The
binding, mapping and event type contracts carry the workspace, the four list
endpoints accept ?workspace=, and the Events page has a workspace selector
that filters every step and picks where a new source goes.

Issue: archie-core-kgxw.11

* feat(webui): authorize a record in its workspace

The API built every access request with no workspace, so workspace
policies and per-workspace roles never applied. A request naming a source,
binding, mapping, event type or task now reads the record's workspace under
the caller's scope and authorizes in it; tasks carry their workspace on the
State Store contract for this.

Issue: archie-core-kgxw.9

* feat(access): expose workspace environment to policies

Policies can test resource.workspace.environment. The workspace attribute
is the workspace entity, which carries its environment; the principal
brings its org's workspaces from the State Store, so the dashboard and the
daemon's run checks describe a resource's workspace without another lookup,
and the daemon now authorizes runs in the task's or binding's workspace.

Issue: archie-core-kgxw.10

* fix(webui): ask read_secret when a source secret is read

Rotating a source's secret returns its value, but the route asked update,
which every developer holds, so no policy could narrow who reads a secret.
It now asks read_secret on the secret, which the shipped roles give admins
and an object policy can narrow further.

Issue: archie-core-kgxw.17

* refactor(org): delete the unused upgrade-incomplete sentinel

Nothing returns org.ErrUpgradeIncomplete: the State Store finishes the org
upgrade before it serves.

Issue: archie-core-kgxw.18
2026-10-12 03:24:04 +11:00

135 lines
4.0 KiB
Go

// Package org owns the tenant boundary: orgs, the workspaces that divide them,
// and the memberships that give an identity a role in either
package org
import (
"errors"
"fmt"
"strings"
"github.com/samcharles93/archie-core/internal/domain/identity"
)
// OrgID and WorkspaceID identify an org and a workspace.
type (
OrgID string
WorkspaceID string
)
// The org and workspace every record of a single-operator install, and every
// record written before orgs existed, belongs to.
const (
DefaultOrgID OrgID = "org-sys"
DefaultWorkspaceID WorkspaceID = "default"
)
var (
ErrInvalidRole = errors.New("org: invalid role")
ErrInvalidOrg = errors.New("org: invalid org")
ErrInvalidMembership = errors.New("org: invalid membership")
ErrInvalidWorkspace = errors.New("org: invalid workspace")
ErrInvalidAssignment = errors.New("org: invalid agent assignment")
ErrOrgNotFound = errors.New("org: org not found")
ErrOrgExists = errors.New("org: org already exists")
ErrWorkspaceExists = errors.New("org: workspace already exists")
ErrMembershipExists = errors.New("org: membership already exists")
)
// Role is a member's role in an org or one of its workspaces.
type Role string
const (
RoleOwner Role = "owner"
RoleAdmin Role = "admin"
RoleDeveloper Role = "developer"
RoleViewer Role = "viewer"
)
// Validate reports whether r is one of the shipped roles.
func (r Role) Validate() error {
switch r {
case RoleOwner, RoleAdmin, RoleDeveloper, RoleViewer:
return nil
}
return fmt.Errorf("%w: %q", ErrInvalidRole, r)
}
// Org is a tenant.
type Org struct {
ID OrgID `json:"id"`
Name string `json:"name"`
}
// Validate requires an org to have an ID and a name.
func (o Org) Validate() error {
if o.ID == "" || strings.TrimSpace(o.Name) == "" {
return fmt.Errorf("%w: ID and name are required", ErrInvalidOrg)
}
return nil
}
// Workspace divides an org. Environment is a free-form attribute policies can
// test; it is not required.
type Workspace struct {
ID WorkspaceID `json:"id"`
OrgID OrgID `json:"org_id"`
Name string `json:"name"`
Environment string `json:"environment,omitempty"`
}
// Validate requires a workspace to belong to an org and to have a name.
func (w Workspace) Validate() error {
if w.OrgID == "" {
return fmt.Errorf("%w: no org", ErrInvalidWorkspace)
}
if strings.TrimSpace(w.Name) == "" {
return fmt.Errorf("%w: no name", ErrInvalidWorkspace)
}
return nil
}
// Membership gives an identity a role in an org, or in one workspace of it
// when WorkspaceID is set.
type Membership struct {
IdentityID identity.IdentityID `json:"identity_id"`
OrgID OrgID `json:"org_id"`
WorkspaceID WorkspaceID `json:"workspace_id,omitempty"`
Role Role `json:"role"`
}
// Member is one identity's membership in an org or workspace, with the
// identity's display name and kind for the dashboard.
type Member struct {
IdentityID identity.IdentityID `json:"identity_id"`
Kind identity.Kind `json:"kind"`
DisplayName string `json:"display_name"`
WorkspaceID WorkspaceID `json:"workspace_id,omitempty"`
Role Role `json:"role"`
}
// Validate requires an identity, an org and a shipped role.
func (m Membership) Validate() error {
if m.IdentityID == "" {
return fmt.Errorf("%w: no identity", ErrInvalidMembership)
}
if m.OrgID == "" {
return fmt.Errorf("%w: no org", ErrInvalidMembership)
}
return m.Role.Validate()
}
// AgentAssignment records the one org an agent or service identity serves.
// People join orgs through memberships; agents are assigned to exactly one.
type AgentAssignment struct {
IdentityID identity.IdentityID `json:"identity_id"`
OrgID OrgID `json:"org_id"`
}
// Validate requires an identity and an org.
func (a AgentAssignment) Validate() error {
if a.IdentityID == "" || a.OrgID == "" {
return fmt.Errorf("%w: identity and org are required", ErrInvalidAssignment)
}
return nil
}