// Package identity owns persistent actor identity and lifecycle semantics. package identity import ( "context" "crypto/sha256" "errors" "fmt" "strings" "time" "github.com/google/uuid" ) 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} } // StableID derives a version 5 shaped UUID from a name. It keeps SHA-256 // rather than uuid.NewSHA1 so IDs minted before the shape fix map to the // current ones by a string rewrite (migration 0051). func StableID(legacyName string) IdentityID { sum := sha256.Sum256([]byte("archie.identity.v1\x00" + strings.ToLower(strings.TrimSpace(legacyName)))) var id uuid.UUID copy(id[:], sum[:16]) id[6] = id[6]&0x0f | 0x50 id[8] = id[8]&0x3f | 0x80 return IdentityID(id.String()) } // 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") } // AgentID is the bot identity the shipped workflows run as. The org upgrade // seeds it on every install, so shipped YAML can name its fixed ID. func AgentID() IdentityID { return StableID("agent") } 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 }