Files
archie/modules/governance/org.go
T
sam 6a3bf5a71a
deploy / plan (push) Successful in 3s
deploy / gate (push) Failing after 8m2s
deploy / archied-image (push) Skipped
deploy / agent-image (push) Skipped
deploy / dist-zip (push) Skipped
refactor(governance): open role identifiers and guard policy lockout with Cedar (#2017)
Carried policy lockout guard from internal/app/statestore/policy_guard.go.
2026-11-18 02:29:06 +11:00

217 lines
7.0 KiB
Go

package governance
import (
"context"
"errors"
"fmt"
"strings"
"github.com/samcharles93/archie-core/internal/platform/stableid"
)
// OrgID and WorkspaceID identify an organisation and a workspace.
type (
OrgID string
WorkspaceID string
)
// DefaultOrgID and DefaultWorkspaceID identify the default operating
// organisation and workspace.
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")
ErrLastOwner = errors.New("org: this is the organisation's last active owner; make another member owner first")
)
// Role is a member's role in an organisation or one of its workspaces.
type Role string
// Default role names used by the starter org policies seeded at org creation.
const (
RoleOwner Role = "owner"
RoleAdmin Role = "admin"
RoleDeveloper Role = "developer"
RoleViewer Role = "viewer"
)
// Validate reports whether r is a valid role identifier.
func (r Role) Validate() error {
if !stableid.Valid(string(r)) {
return fmt.Errorf("%w: %q (must match %s)", ErrInvalidRole, r, stableid.Pattern)
}
return nil
}
// 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 without slashes.
func (o Org) Validate() error {
if o.ID == "" || strings.TrimSpace(o.Name) == "" {
return fmt.Errorf("%w: ID and name are required", ErrInvalidOrg)
}
if strings.Contains(string(o.ID), "/") {
return fmt.Errorf("%w: ID must not contain a slash", ErrInvalidOrg)
}
return nil
}
// Workspace divides an organisation. Environment is a free-form attribute
// policies can test.
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 have a name without slashes.
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)
}
if strings.Contains(string(w.ID), "/") || strings.Contains(string(w.OrgID), "/") {
return fmt.Errorf("%w: IDs must not contain a slash", 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 IdentityID `json:"identity_id"`
OrgID OrgID `json:"org_id"`
WorkspaceID WorkspaceID `json:"workspace_id,omitempty"`
Role Role `json:"role"`
}
// Validate requires an identity, an org and a valid 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()
}
// Member is one identity's membership in an org or workspace.
type Member struct {
IdentityID IdentityID `json:"identity_id"`
Kind Kind `json:"kind"`
DisplayName string `json:"display_name"`
WorkspaceID WorkspaceID `json:"workspace_id,omitempty"`
Role Role `json:"role"`
}
// AgentAssignment records the one org an agent or service identity serves.
type AgentAssignment struct {
IdentityID 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
}
type (
orgContextKey struct{}
scopeContextKey struct{}
)
// WithOrg attaches the organisation a request acts in.
func WithOrg(ctx context.Context, id OrgID) context.Context {
return context.WithValue(ctx, orgContextKey{}, id)
}
// OrgFromContext returns the organisation a request acts in, or DefaultOrgID
// when the context carries none.
func OrgFromContext(ctx context.Context) OrgID {
if id, ok := ctx.Value(orgContextKey{}).(OrgID); ok && id != "" {
return id
}
return DefaultOrgID
}
// WithScope confines a request's reads to one org.
func WithScope(ctx context.Context, id OrgID) context.Context {
return context.WithValue(ctx, scopeContextKey{}, id)
}
// WithoutScope lifts a request's org confinement for instance-wide reads.
func WithoutScope(ctx context.Context) context.Context {
return context.WithValue(ctx, scopeContextKey{}, OrgID(""))
}
// InstanceScope reports whether ctx may read instance-wide records.
func InstanceScope(ctx context.Context) bool {
id, ok := Scope(ctx)
return !ok || id == DefaultOrgID
}
// Scope returns the org WithScope confined ctx to.
func Scope(ctx context.Context) (OrgID, bool) {
id, ok := ctx.Value(scopeContextKey{}).(OrgID)
return id, ok && id != ""
}
// OrgRepository persists organisations, workspaces, memberships and agent assignments.
type OrgRepository interface { //nolint:interfacebloat // one tenant-boundary store contract: orgs, workspaces, memberships and agent assignments
CreateOrg(context.Context, Org, IdentityID) (Org, error)
GetOrg(context.Context, OrgID) (Org, error)
ListOrgs(context.Context) ([]Org, error)
CreateWorkspace(context.Context, Workspace) (Workspace, error)
ListWorkspaces(context.Context, OrgID) ([]Workspace, error)
EnsureMembership(context.Context, Membership) error
ListMembers(context.Context, OrgID) ([]Member, error)
RemoveMembership(context.Context, Membership) error
ListOrgAgents(context.Context, OrgID) ([]AgentAssignment, error)
AssignAgent(context.Context, AgentAssignment) error
OrgForIdentity(context.Context, IdentityID) (OrgID, error)
}
// OrgAPI is the read and write org surface exposed to callers.
type OrgAPI interface {
ListOrgs(context.Context) ([]Org, error)
GetOrg(context.Context, OrgID) (Org, error)
ListWorkspaces(context.Context, OrgID) ([]Workspace, error)
CreateWorkspace(context.Context, Workspace) (Workspace, error)
ListMembers(context.Context, OrgID) ([]Member, error)
EnsureMembership(context.Context, Membership) error
RemoveMembership(context.Context, Membership) error
AssignAgent(context.Context, AgentAssignment) error
}
// OrgCreator creates an org with its default workspace and first owner.
type OrgCreator interface {
CreateOrg(context.Context, Org, IdentityID) (Org, error)
}
// AgentAssignments reads authoritative agent ownership.
type AgentAssignments interface {
ListOrgAgents(context.Context, OrgID) ([]AgentAssignment, error)
}