340 lines
10 KiB
Go
340 lines
10 KiB
Go
package governance
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/samcharles93/archie-core/modules/governance/internal/store"
|
|
)
|
|
|
|
var (
|
|
_ OrgRepository = (*Module)(nil)
|
|
_ WorkspaceLister = (*Module)(nil)
|
|
_ PrincipalSource = (*Module)(nil)
|
|
)
|
|
|
|
// CreateOrg creates an organisation with its default workspace and owner membership.
|
|
func (m *Module) CreateOrg(ctx context.Context, value Org, owner IdentityID) (Org, error) {
|
|
if err := value.Validate(); err != nil {
|
|
return Org{}, err
|
|
}
|
|
if err := (Membership{IdentityID: owner, OrgID: value.ID, Role: RoleOwner}).Validate(); err != nil {
|
|
return Org{}, err
|
|
}
|
|
err := m.store.CreateOrg(ctx, string(value.ID), value.Name, string(DefaultWorkspaceID), string(owner))
|
|
if errors.Is(err, store.ErrAlreadyExists) {
|
|
return Org{}, fmt.Errorf("%w: %s", ErrOrgExists, value.ID)
|
|
}
|
|
if err != nil {
|
|
return Org{}, err
|
|
}
|
|
value.Version = 1
|
|
return value, nil
|
|
}
|
|
|
|
// GetOrg returns one org by ID or ErrOrgNotFound.
|
|
func (m *Module) GetOrg(ctx context.Context, id OrgID) (Org, error) {
|
|
row, ok, err := m.store.GetOrg(ctx, string(id))
|
|
if err != nil {
|
|
return Org{}, err
|
|
}
|
|
if !ok {
|
|
return Org{}, ErrOrgNotFound
|
|
}
|
|
return Org{ID: OrgID(row.ID), Name: row.Name, Version: row.Version}, nil
|
|
}
|
|
|
|
// ListOrgs returns every organisation, oldest first.
|
|
func (m *Module) ListOrgs(ctx context.Context) ([]Org, error) {
|
|
rows, err := m.store.ListOrgs(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]Org, 0, len(rows))
|
|
for _, r := range rows {
|
|
out = append(out, Org{ID: OrgID(r.ID), Name: r.Name, Version: r.Version})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// RenameOrg changes an organisation's display name and appends an audit event
|
|
// naming the person who made the change. Only an owner of the organisation may.
|
|
func (m *Module) RenameOrg(ctx context.Context, id OrgID, name string, expectedVersion int64, actor IdentityID) (Org, error) {
|
|
p, ok := PrincipalFromContext(ctx)
|
|
if !ok || !p.mayRename(id) {
|
|
return Org{}, ErrNotOwner
|
|
}
|
|
name, err := ValidateName(name)
|
|
if err != nil {
|
|
return Org{}, fmt.Errorf("%w: %w", ErrInvalidOrg, err)
|
|
}
|
|
actorName, err := m.actorName(ctx, actor)
|
|
if err != nil {
|
|
return Org{}, err
|
|
}
|
|
row, err := m.store.RenameOrg(ctx, string(id), name, expectedVersion, store.OrgEventRow{
|
|
Type: "org.renamed", ActorID: string(actor), ActorName: actorName, RequestID: newRequestID("org-rename"),
|
|
})
|
|
switch {
|
|
case errors.Is(err, store.ErrNotFound):
|
|
return Org{}, ErrOrgNotFound
|
|
case errors.Is(err, store.ErrVersionConflict):
|
|
return Org{}, ErrNameChanged
|
|
case err != nil:
|
|
return Org{}, err
|
|
}
|
|
return Org{ID: OrgID(row.ID), Name: row.Name, Version: row.Version}, nil
|
|
}
|
|
|
|
// CreateWorkspace adds a workspace to an existing organisation.
|
|
func (m *Module) CreateWorkspace(ctx context.Context, value Workspace) (Workspace, error) {
|
|
if err := value.Validate(); err != nil {
|
|
return Workspace{}, err
|
|
}
|
|
err := m.store.CreateWorkspace(ctx, store.WorkspaceRow{
|
|
ID: string(value.ID),
|
|
OrgID: string(value.OrgID),
|
|
Name: value.Name,
|
|
Environment: value.Environment,
|
|
})
|
|
switch {
|
|
case errors.Is(err, store.ErrNotFound):
|
|
return Workspace{}, ErrOrgNotFound
|
|
case errors.Is(err, store.ErrAlreadyExists):
|
|
return Workspace{}, fmt.Errorf("%w: %s/%s", ErrWorkspaceExists, value.OrgID, value.ID)
|
|
case err != nil:
|
|
return Workspace{}, err
|
|
}
|
|
value.Version = 1
|
|
return value, nil
|
|
}
|
|
|
|
// ListWorkspaces returns an organisation's workspaces, oldest first.
|
|
func (m *Module) ListWorkspaces(ctx context.Context, id OrgID) ([]Workspace, error) {
|
|
rows, err := m.store.ListWorkspaces(ctx, string(id))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]Workspace, 0, len(rows))
|
|
for _, r := range rows {
|
|
out = append(out, Workspace{
|
|
ID: WorkspaceID(r.ID),
|
|
OrgID: OrgID(r.OrgID),
|
|
Name: r.Name,
|
|
Environment: r.Environment,
|
|
Version: r.Version,
|
|
})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// RenameWorkspace changes a workspace's display name and appends an audit event
|
|
// naming the person who made the change. Only an owner of its organisation may.
|
|
func (m *Module) RenameWorkspace(ctx context.Context, orgID OrgID, id WorkspaceID, name string, expectedVersion int64, actor IdentityID) (Workspace, error) {
|
|
p, ok := PrincipalFromContext(ctx)
|
|
if !ok || !p.mayRename(orgID) {
|
|
return Workspace{}, ErrNotOwner
|
|
}
|
|
name, err := ValidateName(name)
|
|
if err != nil {
|
|
return Workspace{}, fmt.Errorf("%w: %w", ErrInvalidWorkspace, err)
|
|
}
|
|
actorName, err := m.actorName(ctx, actor)
|
|
if err != nil {
|
|
return Workspace{}, err
|
|
}
|
|
row, err := m.store.RenameWorkspace(ctx, string(orgID), string(id), name, expectedVersion, store.OrgEventRow{
|
|
Type: "workspace.renamed", ActorID: string(actor), ActorName: actorName, RequestID: newRequestID("workspace-rename"),
|
|
})
|
|
switch {
|
|
case errors.Is(err, store.ErrNotFound):
|
|
return Workspace{}, ErrWorkspaceNotFound
|
|
case errors.Is(err, store.ErrVersionConflict):
|
|
return Workspace{}, ErrNameChanged
|
|
case err != nil:
|
|
return Workspace{}, err
|
|
}
|
|
return Workspace{
|
|
ID: WorkspaceID(row.ID), OrgID: OrgID(row.OrgID), Name: row.Name,
|
|
Environment: row.Environment, Version: row.Version,
|
|
}, nil
|
|
}
|
|
|
|
// ListOrgEvents returns the organisation's name changes, newest first.
|
|
func (m *Module) ListOrgEvents(ctx context.Context, id OrgID, limit int) ([]OrgEvent, error) {
|
|
rows, err := m.store.ListOrgEvents(ctx, string(id), limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]OrgEvent, 0, len(rows))
|
|
for _, r := range rows {
|
|
kind := "org"
|
|
if r.WorkspaceID != "" {
|
|
kind = "workspace"
|
|
}
|
|
out = append(out, OrgEvent{
|
|
ID: r.ID, Kind: kind, WorkspaceID: WorkspaceID(r.WorkspaceID),
|
|
ActorID: IdentityID(r.ActorID), ActorName: r.ActorName,
|
|
OldName: r.OldName, NewName: r.NewName, At: r.At,
|
|
})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// actorName is the display name recorded beside an actor ID so an audit row
|
|
// still reads when that identity is later renamed or retired.
|
|
func (m *Module) actorName(ctx context.Context, id IdentityID) (string, error) {
|
|
row, err := m.store.GetIdentity(ctx, string(id))
|
|
if errors.Is(err, store.ErrNotFound) {
|
|
return string(id), nil
|
|
}
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return row.DisplayName, nil
|
|
}
|
|
|
|
func newRequestID(prefix string) string {
|
|
return prefix + ":" + rand.Text()
|
|
}
|
|
|
|
// EnsureMembership grants or updates a membership role in an organisation or workspace.
|
|
func (m *Module) EnsureMembership(ctx context.Context, value Membership) error {
|
|
if err := value.Validate(); err != nil {
|
|
return err
|
|
}
|
|
var ws sql.NullString
|
|
if value.WorkspaceID != "" {
|
|
ws = sql.NullString{String: string(value.WorkspaceID), Valid: true}
|
|
}
|
|
err := m.store.EnsureMembership(ctx, store.MembershipRow{
|
|
IdentityID: string(value.IdentityID),
|
|
OrgID: string(value.OrgID),
|
|
WorkspaceID: ws,
|
|
Role: string(value.Role),
|
|
})
|
|
switch {
|
|
case errors.Is(err, store.ErrNotFound):
|
|
return ErrOrgNotFound
|
|
case errors.Is(err, store.ErrLastOwner):
|
|
return ErrLastOwner
|
|
default:
|
|
return err
|
|
}
|
|
}
|
|
|
|
// ListMembers returns an organisation's memberships with identity metadata.
|
|
func (m *Module) ListMembers(ctx context.Context, id OrgID) ([]Member, error) {
|
|
rows, err := m.store.ListMembers(ctx, string(id))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]Member, 0, len(rows))
|
|
for _, r := range rows {
|
|
var ws WorkspaceID
|
|
if r.WorkspaceID.Valid {
|
|
ws = WorkspaceID(r.WorkspaceID.String)
|
|
}
|
|
member := Member{
|
|
IdentityID: IdentityID(r.IdentityID),
|
|
Kind: Kind(r.Kind),
|
|
DisplayName: r.DisplayName,
|
|
WorkspaceID: ws,
|
|
Role: Role(r.Role),
|
|
Status: Lifecycle(r.Lifecycle),
|
|
}
|
|
if r.LastSignIn.Valid {
|
|
member.LastSignIn = &r.LastSignIn.Time
|
|
}
|
|
out = append(out, member)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// RemoveMembership revokes a membership while keeping at least one active owner.
|
|
func (m *Module) RemoveMembership(ctx context.Context, value Membership) error {
|
|
if value.IdentityID == "" || value.OrgID == "" {
|
|
return fmt.Errorf("%w: identity and org are required", ErrInvalidMembership)
|
|
}
|
|
err := m.store.RemoveMembership(ctx, string(value.IdentityID), string(value.OrgID), string(value.WorkspaceID))
|
|
if errors.Is(err, store.ErrLastOwner) {
|
|
return ErrLastOwner
|
|
}
|
|
return err
|
|
}
|
|
|
|
// ListOrgAgents returns every agent assignment in org id.
|
|
func (m *Module) ListOrgAgents(ctx context.Context, id OrgID) ([]AgentAssignment, error) {
|
|
rows, err := m.store.ListOrgAgents(ctx, string(id))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]AgentAssignment, 0, len(rows))
|
|
for _, r := range rows {
|
|
out = append(out, AgentAssignment{IdentityID: IdentityID(r.IdentityID), OrgID: OrgID(r.OrgID)})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// AssignAgent assigns an agent identity to an organisation.
|
|
func (m *Module) AssignAgent(ctx context.Context, value AgentAssignment) error {
|
|
if err := value.Validate(); err != nil {
|
|
return err
|
|
}
|
|
err := m.store.AssignAgent(ctx, string(value.IdentityID), string(value.OrgID))
|
|
if errors.Is(err, store.ErrNotFound) {
|
|
return ErrOrgNotFound
|
|
}
|
|
return err
|
|
}
|
|
|
|
// OrgForIdentity resolves the organisation an identity serves.
|
|
func (m *Module) OrgForIdentity(ctx context.Context, id IdentityID) (OrgID, error) {
|
|
resolved, err := m.store.ResolveIdentityOrg(ctx, string(id), string(DefaultOrgID))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return OrgID(resolved), nil
|
|
}
|
|
|
|
// PrincipalFor assembles the Cedar Principal for id.
|
|
func (m *Module) PrincipalFor(ctx context.Context, id IdentityID) (Principal, error) {
|
|
resolved, err := m.OrgForIdentity(ctx, id)
|
|
if err != nil {
|
|
return Principal{}, err
|
|
}
|
|
rows, err := m.store.ListMembershipsByIdentity(ctx, string(id))
|
|
if err != nil {
|
|
return Principal{}, err
|
|
}
|
|
memberships := make([]Membership, 0, len(rows))
|
|
for _, r := range rows {
|
|
var ws WorkspaceID
|
|
if r.WorkspaceID.Valid {
|
|
ws = WorkspaceID(r.WorkspaceID.String)
|
|
}
|
|
memberships = append(memberships, Membership{
|
|
IdentityID: id,
|
|
OrgID: OrgID(r.OrgID),
|
|
WorkspaceID: ws,
|
|
Role: Role(r.Role),
|
|
})
|
|
}
|
|
workspaces, err := m.ListWorkspaces(ctx, resolved)
|
|
if err != nil {
|
|
return Principal{}, err
|
|
}
|
|
if id == SystemID {
|
|
memberships = SharedTokenOwner().Memberships
|
|
}
|
|
return Principal{
|
|
IdentityID: id,
|
|
Org: resolved,
|
|
Memberships: memberships,
|
|
Workspaces: workspaces,
|
|
}, nil
|
|
}
|