feat(org): tenant boundary domain package — orgs, workspaces, memberships, agent assignments (archie-core-e5fn)
This commit is contained in:
3 files changed
+268
No files matched your search
@@ -0,0 +1,128 @@
|
||||
// Package org owns the tenant boundary: orgs, the workspaces that divide them,
|
||||
// and the memberships that give an identity a role in either
|
||||
// (docs/prds/orgs-and-access.md).
|
||||
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 = "default"
|
||||
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")
|
||||
ErrMembershipExists = errors.New("org: membership already exists")
|
||||
// ErrUpgradeIncomplete reports that the resumable default org/workspace
|
||||
// upgrade has not finished (docs/prds/orgs-and-access.md, "Upgrading
|
||||
// existing installs"): the store refuses scoped calls rather than serving
|
||||
// records with no org.
|
||||
ErrUpgradeIncomplete = errors.New("org: default org upgrade incomplete")
|
||||
)
|
||||
|
||||
// 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"`
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package org
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRoleValidate(t *testing.T) {
|
||||
for _, r := range []Role{RoleOwner, RoleAdmin, RoleDeveloper, RoleViewer} {
|
||||
if err := r.Validate(); err != nil {
|
||||
t.Errorf("%q.Validate() = %v, want nil", r, err)
|
||||
}
|
||||
}
|
||||
for _, r := range []Role{"", "approver", "Owner"} {
|
||||
if err := r.Validate(); !errors.Is(err, ErrInvalidRole) {
|
||||
t.Errorf("%q.Validate() = %v, want ErrInvalidRole", r, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMembershipValidate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
m Membership
|
||||
want error
|
||||
}{
|
||||
{name: "org-wide", m: Membership{IdentityID: "id-1", OrgID: DefaultOrgID, Role: RoleOwner}},
|
||||
{name: "one workspace", m: Membership{IdentityID: "id-1", OrgID: DefaultOrgID, WorkspaceID: DefaultWorkspaceID, Role: RoleViewer}},
|
||||
{name: "no identity", m: Membership{OrgID: DefaultOrgID, Role: RoleOwner}, want: ErrInvalidMembership},
|
||||
{name: "no org", m: Membership{IdentityID: "id-1", Role: RoleOwner}, want: ErrInvalidMembership},
|
||||
{name: "bad role", m: Membership{IdentityID: "id-1", OrgID: DefaultOrgID, Role: "root"}, want: ErrInvalidRole},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := tt.m.Validate(); !errors.Is(err, tt.want) {
|
||||
t.Fatalf("Validate() = %v, want %v", err, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrgValidate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
o Org
|
||||
want error
|
||||
}{
|
||||
{name: "named", o: Org{ID: DefaultOrgID, Name: "Default"}},
|
||||
{name: "no id", o: Org{Name: "Default"}, want: ErrInvalidOrg},
|
||||
{name: "blank name", o: Org{ID: DefaultOrgID, Name: " "}, want: ErrInvalidOrg},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := tt.o.Validate(); !errors.Is(err, tt.want) {
|
||||
t.Fatalf("Validate() = %v, want %v", err, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgentAssignmentValidate(t *testing.T) {
|
||||
valid := AgentAssignment{IdentityID: "id-1", OrgID: DefaultOrgID}
|
||||
if err := valid.Validate(); err != nil {
|
||||
t.Fatalf("Validate() = %v, want nil", err)
|
||||
}
|
||||
for _, a := range []AgentAssignment{
|
||||
{},
|
||||
{OrgID: DefaultOrgID},
|
||||
{IdentityID: "id-1"},
|
||||
} {
|
||||
if err := a.Validate(); !errors.Is(err, ErrInvalidAssignment) {
|
||||
t.Errorf("%+v.Validate() = %v, want ErrInvalidAssignment", a, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkspaceValidate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
w Workspace
|
||||
want error
|
||||
}{
|
||||
{name: "named with environment", w: Workspace{ID: "w1", OrgID: DefaultOrgID, Name: "networking-prd", Environment: "production"}},
|
||||
{name: "environment optional", w: Workspace{ID: "w1", OrgID: DefaultOrgID, Name: "service-desk-dev"}},
|
||||
{name: "blank name", w: Workspace{ID: "w1", OrgID: DefaultOrgID, Name: " "}, want: ErrInvalidWorkspace},
|
||||
{name: "no org", w: Workspace{ID: "w1", Name: "x"}, want: ErrInvalidWorkspace},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := tt.w.Validate(); !errors.Is(err, tt.want) {
|
||||
t.Fatalf("Validate() = %v, want %v", err, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
// The persistence contract for orgs, workspaces, memberships and agent
|
||||
// assignments. Postgres implements it (internal/infrastructure/postgres);
|
||||
// composition wires it like identity.Repository.
|
||||
package org
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
)
|
||||
|
||||
// Repository persists the tenant boundary. A store that implements it owns
|
||||
// ID conflict rules (org and workspace IDs are caller-provided and unique).
|
||||
type Repository interface {
|
||||
// CreateOrg inserts a new org; re-creating an existing ID is an error.
|
||||
CreateOrg(context.Context, Org) (Org, error)
|
||||
// GetOrg returns one org by ID, or ErrOrgNotFound.
|
||||
GetOrg(context.Context, OrgID) (Org, error)
|
||||
// ListOrgs returns every org, oldest first.
|
||||
ListOrgs(context.Context) ([]Org, error)
|
||||
// CreateWorkspace inserts a workspace into an existing org.
|
||||
CreateWorkspace(context.Context, Workspace) (Workspace, error)
|
||||
// ListWorkspaces returns an org's workspaces, oldest first.
|
||||
ListWorkspaces(context.Context, OrgID) ([]Workspace, error)
|
||||
// EnsureMembership grants an identity a role in an org, or in one
|
||||
// workspace of it; an identical existing membership is not an error.
|
||||
EnsureMembership(context.Context, Membership) error
|
||||
// AssignAgent records the one org an agent or service identity serves,
|
||||
// replacing any prior assignment for that identity.
|
||||
AssignAgent(context.Context, AgentAssignment) error
|
||||
// OrgForIdentity returns the org an identity serves: the assigned org
|
||||
// for an agent, or the org of one of its memberships for a person.
|
||||
// An identity in no org resolves to DefaultOrgID.
|
||||
OrgForIdentity(context.Context, identity.IdentityID) (OrgID, error)
|
||||
}
|
||||
|
||||
// Upgrader performs the resumable default org/workspace upgrade
|
||||
// (docs/prds/orgs-and-access.md, "Upgrading existing installs"). It runs in
|
||||
// the State Store first, then in the event store, each phase resumable from
|
||||
// its recorded ledger row, and it must run before scoped calls are served.
|
||||
type Upgrader interface {
|
||||
// UpgradeDefaultOrg is idempotent: a completed phase is never re-run,
|
||||
// an interrupted one resumes from where the ledger says it stopped.
|
||||
UpgradeDefaultOrg(context.Context) error
|
||||
}
|
||||
Reference in new issue
Block a user