diff --git a/internal/domain/messaging/settings.go b/internal/domain/messaging/settings.go index 18b443c78..6f4337ce0 100644 --- a/internal/domain/messaging/settings.go +++ b/internal/domain/messaging/settings.go @@ -100,9 +100,27 @@ func (c *SettingsCommand) Execute(ctx context.Context, actor, input string) stri } } -func (c *SettingsCommand) replace(ctx context.Context, actor, input string) string { +// resolveActor maps a channel sender to the Archie identity the write is +// recorded against. A sender that does not resolve cannot run a command: the +// audit record names an identity or the command does not happen. +func (c *SettingsCommand) resolveActor(ctx context.Context, subject, actor string) (identity.IdentityID, string) { if strings.TrimSpace(actor) == "" { - return "Settings changes require an authenticated Archie identity; this channel cannot resolve one." + return "", subject + " require an authenticated Archie identity; this channel cannot resolve one." + } + if c.identities == nil { + return "", subject + " require an authenticated Archie identity; identity administration is unavailable." + } + resolved, err := c.identities.ResolveLegacyName(ctx, actor) + if err != nil { + return "", subject + " require an authenticated Archie identity; this sender did not resolve." + } + return resolved.ID, "" +} + +func (c *SettingsCommand) replace(ctx context.Context, actor, input string) string { + actorID, refusal := c.resolveActor(ctx, "Settings changes", actor) + if refusal != "" { + return refusal } kind, raw, ok := strings.Cut(strings.TrimSpace(input), " ") if !ok || kind == "" || strings.TrimSpace(raw) == "" { @@ -119,7 +137,7 @@ func (c *SettingsCommand) replace(ctx context.Context, actor, input string) stri if err := json.Unmarshal([]byte(raw), &value); err != nil { return "Settings validation failed: invalid JSON." } - updated, err := c.client.Command(ctx, SettingCommand{Kind: kind, RawValue: value, ExpectedVersion: current.Version, Actor: actor, Source: "messaging", RequestID: newSettingsRequestID()}) + updated, err := c.client.Command(ctx, SettingCommand{Kind: kind, RawValue: value, ExpectedVersion: current.Version, Actor: string(actorID), Source: "messaging", RequestID: newSettingsRequestID()}) if err != nil { return settingsError(err) } @@ -128,15 +146,9 @@ func (c *SettingsCommand) replace(ctx context.Context, actor, input string) stri func (c *SettingsCommand) identity(ctx context.Context, actor string, args []string) string { //nolint:gocyclo,cyclop,funlen // command arity checks remain adjacent to spoof-sensitive dispatch const usage = "Usage: /settings identity |rename |suspend |reactivate |retire >" - if c.identities == nil { - return "Identity administration is unavailable." - } - if strings.TrimSpace(actor) == "" { - return "Identity changes require an authenticated Archie identity; this channel cannot resolve one." - } - actorIdentity, err := c.identities.ResolveLegacyName(ctx, actor) - if err != nil { - return "Identity changes require an authenticated Archie identity; this sender did not resolve." + actorID, refusal := c.resolveActor(ctx, "Identity changes", actor) + if refusal != "" { + return refusal } if len(args) == 1 && args[0] == "list" { values, listErr := c.identities.List(ctx) @@ -149,7 +161,7 @@ func (c *SettingsCommand) identity(ctx context.Context, actor string, args []str } return strings.Join(lines, "\n") } - audit := identity.Audit{ActorID: actorIdentity.ID, Source: "messaging", RequestID: newSettingsRequestID()} + audit := identity.Audit{ActorID: actorID, Source: "messaging", RequestID: newSettingsRequestID()} if len(args) == 3 && args[0] == "create" { id := identity.StableID(audit.RequestID) value, createErr := identity.New(id, identity.Kind(args[1]), args[2]) @@ -235,8 +247,9 @@ func (c *SettingsCommand) set(ctx context.Context, actor string, args []string) if len(args) < 3 { return settingsUsage } - if strings.TrimSpace(actor) == "" { - return "Settings changes require an authenticated Archie identity; this channel cannot resolve one." + actorID, refusal := c.resolveActor(ctx, "Settings changes", actor) + if refusal != "" { + return refusal } descriptor, err := c.descriptor(ctx, args[1]) if err != nil { @@ -259,7 +272,7 @@ func (c *SettingsCommand) set(ctx context.Context, actor string, args []string) } value[name] = parsed } - updated, err := c.client.Command(ctx, SettingCommand{Kind: current.Kind, Value: value, ExpectedVersion: current.Version, Actor: actor, Source: "messaging", RequestID: newSettingsRequestID()}) + updated, err := c.client.Command(ctx, SettingCommand{Kind: current.Kind, Value: value, ExpectedVersion: current.Version, Actor: string(actorID), Source: "messaging", RequestID: newSettingsRequestID()}) if err != nil { return settingsError(err) } diff --git a/internal/domain/messaging/settings_test.go b/internal/domain/messaging/settings_test.go index df12cd607..93067f3fc 100644 --- a/internal/domain/messaging/settings_test.go +++ b/internal/domain/messaging/settings_test.go @@ -101,6 +101,15 @@ func (s *settingsClientStub) Command(_ context.Context, command SettingCommand) return SettingResource{Kind: command.Kind, Version: command.ExpectedVersion + 1, Value: command.Value}, s.commandErr } +// operatorRepository resolves the one sender the settings tests speak as. +func operatorRepository(name string) (*identityRepositoryStub, identity.Identity) { + operator, _ := identity.New(identity.StableID(name), identity.KindUser, "operator") + return &identityRepositoryStub{ + values: map[identity.IdentityID]identity.Identity{operator.ID: operator}, + aliases: map[string]identity.IdentityID{name: operator.ID}, + }, operator +} + func TestSettingsSetUsesTrustedActorAndCurrentVersion(t *testing.T) { client := &settingsClientStub{ descriptors: []SettingDescriptor{{Kind: "limits", Fields: map[string]SettingField{ @@ -108,15 +117,16 @@ func TestSettingsSetUsesTrustedActorAndCurrentVersion(t *testing.T) { }}}, resource: SettingResource{Kind: "limits", Version: 7, Value: map[string]any{"enabled": false, "retries": float64(1), "timeout": "1m", "label": "old"}}, } - command := NewSettingsCommand(client) + repository, operator := operatorRepository("archie:operator-7") + command := NewSettingsCommand(client).WithIdentities(repository) reply := command.Execute(t.Context(), "archie:operator-7", `set limits enabled=true retries=3 timeout="2m30s" label="night run"`) if !strings.Contains(reply, "version 8") { t.Fatalf("reply = %q, want version confirmation", reply) } - if client.command.Actor != "archie:operator-7" { - t.Fatalf("actor = %q, want trusted actor", client.command.Actor) + if client.command.Actor != string(operator.ID) { + t.Fatalf("actor = %q, want the resolved identity ID %q", client.command.Actor, operator.ID) } if client.command.ExpectedVersion != 7 { t.Fatalf("expected version = %d, want 7", client.command.ExpectedVersion) @@ -136,6 +146,10 @@ func TestSettingsSetUsesTrustedActorAndCurrentVersion(t *testing.T) { if !strings.Contains(unauthenticatedReply, "authenticated Archie identity") || client.command.RequestID != previousID { t.Fatalf("unauthenticated reply = %q, command unexpectedly dispatched", unauthenticatedReply) } + unresolvedReply := command.Execute(t.Context(), "mallory", "set limits retries=4") + if !strings.Contains(unresolvedReply, "did not resolve") || client.command.RequestID != previousID { + t.Fatalf("unresolved sender reply = %q, command unexpectedly dispatched", unresolvedReply) + } } func TestSettingsSetConflictDoesNotRetryAgainstANewerVersion(t *testing.T) { @@ -144,7 +158,8 @@ func TestSettingsSetConflictDoesNotRetryAgainstANewerVersion(t *testing.T) { resource: SettingResource{Kind: "limits", Version: 4, Value: map[string]any{"retries": float64(1)}}, commandErr: ErrSettingsConflict, } - command := NewSettingsCommand(client) + repository, _ := operatorRepository("archie:operator-7") + command := NewSettingsCommand(client).WithIdentities(repository) reply := command.Execute(t.Context(), "archie:operator-7", "set limits retries=2") @@ -161,7 +176,8 @@ func TestSettingsReplaceAcceptsCollectionJSON(t *testing.T) { descriptors: []SettingDescriptor{{Kind: "schedules"}}, resource: SettingResource{Kind: "schedules", Version: 3, Raw: []any{}}, } - reply := NewSettingsCommand(client).Execute(t.Context(), "archie:operator-7", `replace schedules [{"id":"daily"}]`) + repository, _ := operatorRepository("archie:operator-7") + reply := NewSettingsCommand(client).WithIdentities(repository).Execute(t.Context(), "archie:operator-7", `replace schedules [{"id":"daily"}]`) if !strings.Contains(reply, "version 4") { t.Fatalf("reply = %q", reply) } diff --git a/internal/webui/api_control_plane.go b/internal/webui/api_control_plane.go index dfac7c50e..5f78ceb9c 100644 --- a/internal/webui/api_control_plane.go +++ b/internal/webui/api_control_plane.go @@ -16,11 +16,6 @@ import ( controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" ) -const ( - controlPlaneActor = "operator:web" - controlPlaneSource = "archie-ui" -) - type controlPlaneCommandRequest struct { Value json.RawMessage `json:"value"` ExpectedVersion int64 `json:"expected_version"` @@ -73,15 +68,15 @@ func (s *Server) handleControlPlaneCommand(w http.ResponseWriter, r *http.Reques http.Error(w, "invalid request body", http.StatusBadRequest) return } - requestID, err := newControlPlaneRequestID() + audit, err := webAudit() if err != nil { http.Error(w, "cannot create request ID", http.StatusInternalServerError) return } response, err := s.ControlPlane.Command(r.Context(), &controlpb.CommandRequest{ Kind: r.PathValue("kind"), Command: r.PathValue("command"), ValueJson: request.Value, - ExpectedVersion: request.ExpectedVersion, Actor: controlPlaneActor, - Source: controlPlaneSource, RequestId: requestID, + ExpectedVersion: request.ExpectedVersion, Actor: string(audit.ActorID), + Source: audit.Source, RequestId: audit.RequestID, }) if err != nil { writeControlPlaneError(w, err) diff --git a/internal/webui/api_control_plane_test.go b/internal/webui/api_control_plane_test.go index ae270d189..6889d9364 100644 --- a/internal/webui/api_control_plane_test.go +++ b/internal/webui/api_control_plane_test.go @@ -14,6 +14,7 @@ import ( "google.golang.org/grpc/status" controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" + "github.com/samcharles93/archie-core/internal/domain/identity" ) type controlPlaneClientStub struct { @@ -56,8 +57,8 @@ func TestControlPlaneCommandSuppliesTrustedAttribution(t *testing.T) { if response.Code != http.StatusOK { t.Fatalf("status = %d, body = %q", response.Code, response.Body.String()) } - if client.command.Actor != controlPlaneActor || client.command.Source != controlPlaneSource { - t.Fatalf("attribution = %q/%q", client.command.Actor, client.command.Source) + if client.command.Actor != string(identity.SystemID) || client.command.Source != "archie-ui" { + t.Fatalf("attribution = %q/%q, want the System identity from archie-ui", client.command.Actor, client.command.Source) } if !strings.HasPrefix(client.command.RequestId, "ui-") || client.command.ExpectedVersion != 1 { t.Fatalf("request = %+v", client.command) diff --git a/internal/webui/api_identities.go b/internal/webui/api_identities.go index d4743506b..7a12434c4 100644 --- a/internal/webui/api_identities.go +++ b/internal/webui/api_identities.go @@ -87,9 +87,14 @@ func (s *Server) handleIdentityCreate(w http.ResponseWriter, r *http.Request) { http.Error(w, "cannot create identity ID", http.StatusInternalServerError) return } + audit, err := webAudit() + if err != nil { + http.Error(w, "cannot create request ID", http.StatusInternalServerError) + return + } value, err := identity.New(id, request.Kind, request.DisplayName) if err == nil { - value, err = s.Identities.Create(r.Context(), value, webIdentityAudit()) + value, err = s.Identities.Create(r.Context(), value, audit) } if err != nil { writeIdentityError(w, err) @@ -105,8 +110,13 @@ func (s *Server) handleIdentityCommand(w http.ResponseWriter, r *http.Request) { http.Error(w, "invalid request body", http.StatusBadRequest) return } + audit, err := webAudit() + if err != nil { + http.Error(w, "cannot create request ID", http.StatusInternalServerError) + return + } command := identity.Command{Type: identity.CommandType(r.PathValue("command")), DisplayName: request.DisplayName} - value, err := s.Identities.Apply(r.Context(), identity.IdentityID(r.PathValue("id")), request.ExpectedVersion, command, webIdentityAudit()) + value, err := s.Identities.Apply(r.Context(), identity.IdentityID(r.PathValue("id")), request.ExpectedVersion, command, audit) if err != nil { writeIdentityError(w, err) return @@ -114,9 +124,16 @@ func (s *Server) handleIdentityCommand(w http.ResponseWriter, r *http.Request) { writeJSON(w, value) } -func webIdentityAudit() identity.Audit { - id, _ := newControlPlaneRequestID() - return identity.Audit{ActorID: identity.SystemID, Source: "archie-ui", RequestID: id} +// webAudit attributes a dashboard write. The dashboard authenticates with one +// shared token and carries no per-user session, so the actor is the System +// identity: a real identity the repository resolves, never a name the request +// could claim for itself. +func webAudit() (identity.Audit, error) { + id, err := newControlPlaneRequestID() + if err != nil { + return identity.Audit{}, err + } + return identity.Audit{ActorID: identity.SystemID, Source: "archie-ui", RequestID: id}, nil } func randomIdentityID() (identity.IdentityID, error) {