* 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
171 lines
5.5 KiB
Go
171 lines
5.5 KiB
Go
// Package identity owns persistent actor identity and lifecycle semantics.
|
|
package identity
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type (
|
|
IdentityID string
|
|
Kind string
|
|
Lifecycle string
|
|
CommandType string
|
|
)
|
|
|
|
const (
|
|
SystemID IdentityID = "00000000-0000-5000-8000-000000000001"
|
|
KindSystem Kind = "system"
|
|
KindBot Kind = "bot"
|
|
KindServiceAccount Kind = "service_account"
|
|
KindUser Kind = "user"
|
|
LifecycleActive Lifecycle = "active"
|
|
LifecycleSuspended Lifecycle = "suspended"
|
|
LifecycleRetired Lifecycle = "retired"
|
|
CommandRename CommandType = "rename"
|
|
CommandSuspend CommandType = "suspend"
|
|
CommandReactivate CommandType = "reactivate"
|
|
CommandRetire CommandType = "retire"
|
|
)
|
|
|
|
var (
|
|
ErrInvalid = errors.New("invalid identity")
|
|
ErrNotFound = errors.New("identity not found")
|
|
ErrConflict = errors.New("identity version conflict")
|
|
ErrSystemImmutable = errors.New("system identity is immutable")
|
|
ErrIllegalTransition = errors.New("illegal identity lifecycle transition")
|
|
)
|
|
|
|
type Identity struct {
|
|
ID IdentityID `json:"id"`
|
|
Kind Kind `json:"kind"`
|
|
DisplayName string `json:"display_name"`
|
|
Lifecycle Lifecycle `json:"lifecycle"`
|
|
Version int64 `json:"version"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
type (
|
|
Command struct {
|
|
Type CommandType
|
|
DisplayName string
|
|
}
|
|
Event struct {
|
|
IdentityID IdentityID
|
|
Type CommandType
|
|
From Lifecycle
|
|
To Lifecycle
|
|
DisplayName string
|
|
At time.Time
|
|
}
|
|
Audit struct {
|
|
ActorID IdentityID
|
|
Source, RequestID string
|
|
At time.Time
|
|
}
|
|
)
|
|
|
|
type Repository interface {
|
|
List(context.Context) ([]Identity, error)
|
|
Get(context.Context, IdentityID) (Identity, error)
|
|
ResolveLegacyName(context.Context, string) (Identity, error)
|
|
Create(context.Context, Identity, Audit) (Identity, error)
|
|
Apply(context.Context, IdentityID, int64, Command, Audit) (Identity, error)
|
|
}
|
|
|
|
func System() Identity {
|
|
return Identity{ID: SystemID, Kind: KindSystem, DisplayName: "System", Lifecycle: LifecycleActive, Version: 1}
|
|
}
|
|
|
|
func StableID(legacyName string) IdentityID {
|
|
sum := sha256.Sum256([]byte("archie.identity.v1\x00" + strings.ToLower(strings.TrimSpace(legacyName))))
|
|
return IdentityID(fmt.Sprintf("%08x-%04x-5%03x-%04x-%012x", sum[:4], sum[4:6], sum[6:8], uint16(sum[8])<<8|uint16(sum[9])&0x3fff|0x8000, sum[10:16]))
|
|
}
|
|
|
|
// OperatorID is the person identity the dashboard acts as when no provider
|
|
// signs anyone in. Deriving it from a name lets the upgrade that creates it
|
|
// and the request path that resolves it agree without a lookup.
|
|
func OperatorID() IdentityID { return StableID("operator") }
|
|
|
|
func New(id IdentityID, kind Kind, displayName string) (Identity, error) {
|
|
value := Identity{ID: id, Kind: kind, DisplayName: strings.TrimSpace(displayName), Lifecycle: LifecycleActive, Version: 1}
|
|
if err := value.Validate(); err != nil {
|
|
return Identity{}, err
|
|
}
|
|
return value, nil
|
|
}
|
|
|
|
// FromLegacyName builds the bot identity for a configured name.
|
|
func FromLegacyName(legacyName string) (Identity, error) {
|
|
return New(StableID(legacyName), KindBot, legacyName)
|
|
}
|
|
|
|
// Agent reports whether the kind acts on its own rather than for a person: an
|
|
// agent serves exactly one org.
|
|
func (k Kind) Agent() bool { return k == KindBot || k == KindServiceAccount }
|
|
|
|
// ValidDisplayName reports whether name is non-empty after trimming.
|
|
func ValidDisplayName(name string) bool { return strings.TrimSpace(name) != "" }
|
|
|
|
func (i Identity) Validate() error {
|
|
if i.ID == "" || !ValidDisplayName(i.DisplayName) {
|
|
return fmt.Errorf("%w: ID and display name are required", ErrInvalid)
|
|
}
|
|
switch i.Kind {
|
|
case KindSystem, KindBot, KindServiceAccount, KindUser:
|
|
default:
|
|
return fmt.Errorf("%w: unknown kind %q", ErrInvalid, i.Kind)
|
|
}
|
|
switch i.Lifecycle {
|
|
case LifecycleActive, LifecycleSuspended, LifecycleRetired:
|
|
default:
|
|
return fmt.Errorf("%w: unknown lifecycle %q", ErrInvalid, i.Lifecycle)
|
|
}
|
|
if i.Kind == KindSystem && (i.ID != SystemID || i.Lifecycle != LifecycleActive) {
|
|
return ErrSystemImmutable
|
|
}
|
|
if i.ID == SystemID && i.Kind != KindSystem {
|
|
return ErrSystemImmutable
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func Apply(i Identity, command Command) (Identity, Event, error) {
|
|
if i.ID == SystemID || i.Kind == KindSystem {
|
|
return Identity{}, Event{}, ErrSystemImmutable
|
|
}
|
|
event := Event{IdentityID: i.ID, Type: command.Type, From: i.Lifecycle, To: i.Lifecycle, At: time.Now().UTC()}
|
|
switch command.Type {
|
|
case CommandRename:
|
|
name := strings.TrimSpace(command.DisplayName)
|
|
if name == "" {
|
|
return Identity{}, Event{}, fmt.Errorf("%w: display name is required", ErrInvalid)
|
|
}
|
|
i.DisplayName, event.DisplayName = name, name
|
|
case CommandSuspend:
|
|
if i.Lifecycle != LifecycleActive {
|
|
return Identity{}, Event{}, ErrIllegalTransition
|
|
}
|
|
i.Lifecycle, event.To = LifecycleSuspended, LifecycleSuspended
|
|
case CommandReactivate:
|
|
if i.Lifecycle != LifecycleSuspended {
|
|
return Identity{}, Event{}, ErrIllegalTransition
|
|
}
|
|
i.Lifecycle, event.To = LifecycleActive, LifecycleActive
|
|
case CommandRetire:
|
|
if i.Lifecycle != LifecycleActive && i.Lifecycle != LifecycleSuspended {
|
|
return Identity{}, Event{}, ErrIllegalTransition
|
|
}
|
|
i.Lifecycle, event.To = LifecycleRetired, LifecycleRetired
|
|
default:
|
|
return Identity{}, Event{}, fmt.Errorf("%w: unknown command %q", ErrInvalid, command.Type)
|
|
}
|
|
i.Version++
|
|
return i, event, nil
|
|
}
|