Files
archie/internal/infrastructure/staterpc/identities.go
T
sam 924b02a1b0 feat(controlplane): runtime administration over the State Store's gRPC server
The Web UI and messaging channels now read and write Archie's runtime
settings through one versioned, audited control-plane API hosted on the
State Store's existing gRPC server: catalog, query, command, watch. Each
feature owns its resource, validation and commands; storage only writes
versions and audit records.

Workflows move with it. Definitions become portable YAML compiled through
a closed, typed step registry and stored as a control-plane resource, so
the yaegi interpretation paths (internal/gate, wfeval, wfextract,
gateeval, gateextract, skillbuild) are deleted rather than carried.

The UI process reads definitions as data: the JSON projection lives in
internal/domain/workflow/task so a dashboard decodes the stored
collection without linking the workflow engine, which cmd/archie-ui's
deletion gate forbids. Shipped definitions ride the catalog descriptor
rather than a bespoke REST route, keeping the API to its four operations.

archie-messaging gains -state-store-target/-state-store-token and reads
channel settings from the database, failing closed when the State Store
is unreachable: the database is authoritative for those settings.
2026-09-21 08:17:42 +10:00

196 lines
7.9 KiB
Go

package staterpc
import (
"context"
"errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
"github.com/samcharles93/archie-core/internal/domain/identity"
)
func (s *server) ListIdentities(ctx context.Context, _ *pb.ListIdentitiesRequest) (*pb.ListIdentitiesResponse, error) {
if s.deps.Identities == nil {
return nil, status.Error(codes.Unavailable, "identity store unavailable")
}
values, err := s.deps.Identities.List(ctx)
if err != nil {
return nil, identityStatus(err)
}
out := make([]*pb.Identity, len(values))
for i := range values {
out[i] = identityProto(values[i])
}
return &pb.ListIdentitiesResponse{Identities: out}, nil
}
func (s *server) GetIdentity(ctx context.Context, request *pb.GetIdentityRequest) (*pb.GetIdentityResponse, error) {
value, err := s.deps.Identities.Get(ctx, identity.IdentityID(request.Id))
if errors.Is(err, identity.ErrNotFound) {
value, err = s.deps.Identities.ResolveLegacyName(ctx, request.Id)
}
if err != nil {
return nil, identityStatus(err)
}
return &pb.GetIdentityResponse{Identity: identityProto(value)}, nil
}
func (s *server) CreateIdentity(ctx context.Context, request *pb.CreateIdentityRequest) (*pb.CreateIdentityResponse, error) {
value, err := identity.New(identity.IdentityID(request.Id), identity.Kind(request.Kind), request.DisplayName)
if err == nil {
value, err = s.deps.Identities.Create(ctx, value, identityAudit(request.Audit))
}
if err != nil {
return nil, identityStatus(err)
}
return &pb.CreateIdentityResponse{Identity: identityProto(value)}, nil
}
func (s *server) RenameIdentity(ctx context.Context, request *pb.RenameIdentityRequest) (*pb.RenameIdentityResponse, error) {
value, err := s.applyIdentityCommand(ctx, request.Id, request.ExpectedVersion, request.Audit, identity.Command{Type: identity.CommandRename, DisplayName: request.DisplayName})
return &pb.RenameIdentityResponse{Identity: identityProto(value)}, err
}
func (s *server) SuspendIdentity(ctx context.Context, request *pb.SuspendIdentityRequest) (*pb.SuspendIdentityResponse, error) {
value, err := s.applyIdentityCommand(ctx, request.Id, request.ExpectedVersion, request.Audit, identity.Command{Type: identity.CommandSuspend})
return &pb.SuspendIdentityResponse{Identity: identityProto(value)}, err
}
func (s *server) ReactivateIdentity(ctx context.Context, request *pb.ReactivateIdentityRequest) (*pb.ReactivateIdentityResponse, error) {
value, err := s.applyIdentityCommand(ctx, request.Id, request.ExpectedVersion, request.Audit, identity.Command{Type: identity.CommandReactivate})
return &pb.ReactivateIdentityResponse{Identity: identityProto(value)}, err
}
func (s *server) RetireIdentity(ctx context.Context, request *pb.RetireIdentityRequest) (*pb.RetireIdentityResponse, error) {
value, err := s.applyIdentityCommand(ctx, request.Id, request.ExpectedVersion, request.Audit, identity.Command{Type: identity.CommandRetire})
return &pb.RetireIdentityResponse{Identity: identityProto(value)}, err
}
func (s *server) applyIdentityCommand(ctx context.Context, id string, version int64, audit *pb.IdentityAudit, command identity.Command) (identity.Identity, error) {
value, err := s.deps.Identities.Apply(ctx, identity.IdentityID(id), version, command, identityAudit(audit))
if err != nil {
return identity.Identity{}, identityStatus(err)
}
return value, nil
}
func (c *Client) List(ctx context.Context) ([]identity.Identity, error) {
response, err := c.client.ListIdentities(ctx, &pb.ListIdentitiesRequest{})
if err != nil {
return nil, identityClientError(err)
}
out := make([]identity.Identity, len(response.Identities))
for i := range response.Identities {
out[i] = identityValue(response.Identities[i])
}
return out, nil
}
func (c *Client) Get(ctx context.Context, id identity.IdentityID) (identity.Identity, error) {
response, err := c.client.GetIdentity(ctx, &pb.GetIdentityRequest{Id: string(id)})
if err != nil {
return identity.Identity{}, identityClientError(err)
}
return identityValue(response.Identity), nil
}
func (c *Client) ResolveLegacyName(ctx context.Context, name string) (identity.Identity, error) {
return c.Get(ctx, identity.IdentityID(name))
}
func (c *Client) Create(ctx context.Context, value identity.Identity, audit identity.Audit) (identity.Identity, error) {
response, err := c.client.CreateIdentity(ctx, &pb.CreateIdentityRequest{Id: string(value.ID), Kind: string(value.Kind), DisplayName: value.DisplayName, Audit: auditProto(audit)})
if err != nil {
return identity.Identity{}, identityClientError(err)
}
return identityValue(response.Identity), nil
}
func (c *Client) Apply(ctx context.Context, id identity.IdentityID, version int64, command identity.Command, audit identity.Audit) (identity.Identity, error) {
var value *pb.Identity
var err error
switch command.Type {
case identity.CommandRename:
response, callErr := c.client.RenameIdentity(ctx, &pb.RenameIdentityRequest{Id: string(id), ExpectedVersion: version, DisplayName: command.DisplayName, Audit: auditProto(audit)})
err = callErr
if response != nil {
value = response.Identity
}
case identity.CommandSuspend:
response, callErr := c.client.SuspendIdentity(ctx, &pb.SuspendIdentityRequest{Id: string(id), ExpectedVersion: version, Audit: auditProto(audit)})
err = callErr
if response != nil {
value = response.Identity
}
case identity.CommandReactivate:
response, callErr := c.client.ReactivateIdentity(ctx, &pb.ReactivateIdentityRequest{Id: string(id), ExpectedVersion: version, Audit: auditProto(audit)})
err = callErr
if response != nil {
value = response.Identity
}
case identity.CommandRetire:
response, callErr := c.client.RetireIdentity(ctx, &pb.RetireIdentityRequest{Id: string(id), ExpectedVersion: version, Audit: auditProto(audit)})
err = callErr
if response != nil {
value = response.Identity
}
default:
return identity.Identity{}, identity.ErrInvalid
}
if err != nil {
return identity.Identity{}, identityClientError(err)
}
return identityValue(value), nil
}
func identityProto(value identity.Identity) *pb.Identity {
return &pb.Identity{Id: string(value.ID), Kind: string(value.Kind), DisplayName: value.DisplayName, Lifecycle: string(value.Lifecycle), Version: value.Version, CreatedAt: timestamppb.New(value.CreatedAt), UpdatedAt: timestamppb.New(value.UpdatedAt)}
}
func identityValue(value *pb.Identity) identity.Identity {
if value == nil {
return identity.Identity{}
}
return identity.Identity{ID: identity.IdentityID(value.Id), Kind: identity.Kind(value.Kind), DisplayName: value.DisplayName, Lifecycle: identity.Lifecycle(value.Lifecycle), Version: value.Version, CreatedAt: value.CreatedAt.AsTime(), UpdatedAt: value.UpdatedAt.AsTime()}
}
func identityAudit(value *pb.IdentityAudit) identity.Audit {
if value == nil {
return identity.Audit{}
}
return identity.Audit{ActorID: identity.IdentityID(value.ActorId), Source: value.Source, RequestID: value.RequestId}
}
func auditProto(value identity.Audit) *pb.IdentityAudit {
return &pb.IdentityAudit{ActorId: string(value.ActorID), Source: value.Source, RequestId: value.RequestID}
}
func identityStatus(err error) error {
switch {
case errors.Is(err, identity.ErrNotFound):
return status.Error(codes.NotFound, err.Error())
case errors.Is(err, identity.ErrConflict):
return status.Error(codes.Aborted, err.Error())
case errors.Is(err, identity.ErrInvalid), errors.Is(err, identity.ErrIllegalTransition), errors.Is(err, identity.ErrSystemImmutable):
return status.Error(codes.FailedPrecondition, err.Error())
default:
return status.Error(codes.Internal, "internal state store error")
}
}
func identityClientError(err error) error {
switch status.Code(err) {
case codes.NotFound:
return identity.ErrNotFound
case codes.Aborted:
return identity.ErrConflict
case codes.FailedPrecondition:
return errors.Join(identity.ErrInvalid, err)
default:
return err
}
}