feat(audit,approvals,deliveries): wire audit producers, ITSM change requests, and fix Dockerfile.archie (#2153)

This commit is contained in:
sam committed 2026-11-28 00:42:37 +11:00
1 parent fdfcbab4bd
commit d0d79cbf91
26 files changed
+671 -35

No files matched your search

+82 -3
View File
@@ -5,6 +5,7 @@
package approvals
import (
"cmp"
"context"
"database/sql"
"encoding/json/jsontext"
@@ -22,6 +23,7 @@ import (
"github.com/samcharles93/archie-core/internal/platform/hub"
"github.com/samcharles93/archie-core/modules/approvals/internal/store"
"github.com/samcharles93/archie-core/modules/audit"
"github.com/samcharles93/archie-core/modules/connectors"
"github.com/samcharles93/archie-core/modules/deliveries"
"github.com/samcharles93/archie-core/modules/governance"
@@ -273,6 +275,7 @@ type Module struct {
authz governance.Authorizer
confirm Confirmer
changes ChangeRequests
audit audit.Recorder
log *slog.Logger
now func() time.Time
}
@@ -285,6 +288,10 @@ func NewModule(db *sqlx.DB, log *slog.Logger, orgID string, tell Delivery, wake
}
}
// UseAudit configures the audit recorder notified on approval requests, decisions,
// refusals, and expirations.
func (m *Module) UseAudit(r audit.Recorder) { m.audit = r }
// UseLive publishes a note on the hub whenever a request moves, so a mounted
// view re-reads instead of polling.
func (m *Module) UseLive(h *hub.Hub) {
@@ -340,15 +347,27 @@ func (m *Module) Ask(ctx context.Context, runID uuid.UUID, key string, ds []judg
if err := m.changes.Open(ctx, r); err != nil {
return Request{}, fmt.Errorf("open change request: %w", err)
}
return r, nil
}
if err := m.tell.Ask(ctx, deliveries.ApprovalRequest{
} else if err := m.tell.Ask(ctx, deliveries.ApprovalRequest{
RunID: r.RunID, RequestID: r.ID, Key: r.Key + ":ask",
Title: approvalTitle(ds),
Body: r.Summary, ExpiresAt: r.StepDeadline,
}); err != nil {
return Request{}, fmt.Errorf("send approval request: %w", err)
}
audit.Emit(ctx, m.audit, m.log, audit.Event{
Category: "approval",
Action: "approval.request",
ActorID: cmp.Or(r.InitiatorID, string(governance.WorkloadID)),
ResourceKind: "approval",
ResourceID: r.ID.String(),
Outcome: StatePending,
Detail: map[string]any{
"connector": r.Connector,
"action": r.Action,
"change_request": r.ChangeRequest,
"require_sod": r.RequireSoD,
},
})
return r, nil
}
@@ -408,6 +427,19 @@ func (m *Module) answerAs(ctx context.Context, id uuid.UUID, who Answerer, via s
if row.State != state {
return Request{}, fmt.Errorf("%w: %s", ErrAnswered, row.State)
}
action := "approval.deny"
if approved {
action = "approval.approve"
}
audit.Emit(ctx, m.audit, m.log, audit.Event{
Category: "approval",
Action: action,
ActorID: cmp.Or(who.ID, who.Name, "unknown"),
ActorKind: via,
ResourceKind: "approval",
ResourceID: row.ID.String(),
Outcome: state,
})
answer := Answer{RequestID: row.ID, Approved: approved, By: who.Name, ByID: who.ID, Via: via, At: now}
if row.Kind == KindAutonomy {
if err := m.answerProposal(ctx, row, answer); err != nil {
@@ -525,6 +557,16 @@ func (m *Module) Settle(ctx context.Context, id uuid.UUID, who governance.Subjec
if refusal(err) {
identity := who.Issuer + ":" + who.Subject
m.log.WarnContext(ctx, "approval answer refused", "request", id, "identity", identity, "reason", err.Error())
audit.Emit(ctx, m.audit, m.log, audit.Event{
Category: "approval",
Action: "approval.refuse",
ActorID: identity,
ActorKind: via,
ResourceKind: "approval",
ResourceID: id.String(),
Outcome: "denied",
Detail: map[string]any{"reason": err.Error()},
})
if rerr := m.store.Refuse(ctx, store.Refusal{
ID: uuid.New(), OrgID: m.orgID, RequestID: id, Identity: identity, Via: via, Reason: err.Error(), At: m.now().UTC(),
}); rerr != nil {
@@ -548,6 +590,34 @@ func (m *Module) Settle(ctx context.Context, id uuid.UUID, who governance.Subjec
return false, err
}
// SettleChangeRequest settles a pending change-request approval from an
// external ITSM decision event. Missing, already-settled, expired, or
// non-change-request items return (false, nil).
func (m *Module) SettleChangeRequest(ctx context.Context, id uuid.UUID, connector, changeNumber string, approved bool) (bool, error) {
row, err := m.store.ByID(ctx, m.orgID, id)
if errors.Is(err, ErrNotFound) {
return false, nil
}
if err != nil {
return false, err
}
if !row.ChangeRequest || row.State != StatePending {
return false, nil
}
who := Answerer{
ID: connector + ":" + changeNumber,
Name: strings.TrimSpace(connector + " " + changeNumber),
}
_, err = m.answerAs(ctx, id, who, connector, approved)
switch {
case err == nil:
return true, nil
case errors.Is(err, ErrAnswered), errors.Is(err, ErrExpired), errors.Is(err, ErrNotFound):
return false, nil
}
return false, err
}
// channelAnswerer returns the account that holds the channel credential when
// that account may answer row.
func (m *Module) channelAnswerer(ctx context.Context, who governance.Subject, row store.Request) (Answerer, error) {
@@ -660,6 +730,15 @@ func (m *Module) Expire(ctx context.Context, id uuid.UUID) error {
return nil
}
m.log.InfoContext(ctx, "approval request expired", "request", row.ID, "run", row.RunID)
audit.Emit(ctx, m.audit, m.log, audit.Event{
Category: "approval",
Action: "approval.expire",
ActorID: "clock",
ActorKind: "system",
ResourceKind: "approval",
ResourceID: row.ID.String(),
Outcome: StateExpired,
})
withdrawn := ""
if row.ChangeRequest {
if err := m.changes.Withdraw(ctx, requestOf(row, judgement.Decision{})); err != nil {
+22
View File
@@ -3,12 +3,14 @@ package approvals
import (
"context"
"errors"
"log/slog"
"testing"
"time"
"github.com/google/uuid"
"github.com/samcharles93/archie-core/modules/approvals/internal/store"
"github.com/samcharles93/archie-core/modules/audit"
"github.com/samcharles93/archie-core/modules/governance"
"github.com/samcharles93/archie-core/modules/judgement"
)
@@ -296,3 +298,23 @@ func TestSoDRefusesSelfApproval(t *testing.T) {
t.Fatal("requireSoDOf override true returned false")
}
}
type recordedAudit []audit.Event
func (r *recordedAudit) Record(_ context.Context, e audit.Event) error {
*r = append(*r, e)
return nil
}
func TestUseAuditRecordsRefusals(t *testing.T) {
var rec recordedAudit
m := &Module{log: slog.New(slog.DiscardHandler)}
m.UseAudit(&rec)
audit.Emit(t.Context(), m.audit, m.log, audit.Event{
Category: "approval", Action: "approval.refuse", ActorID: "alice",
ResourceKind: "approval", ResourceID: uuid.NewString(), Outcome: "denied",
})
if len(rec) != 1 || rec[0].Action != "approval.refuse" || rec[0].Outcome != "denied" {
t.Fatalf("recorded audit events = %+v", rec)
}
}
+11
View File
@@ -12,6 +12,7 @@ import (
"github.com/samcharles93/archie-core/internal/platform/httpapi"
"github.com/samcharles93/archie-core/modules/approvals/internal/store"
"github.com/samcharles93/archie-core/modules/audit"
"github.com/samcharles93/archie-core/modules/connectors"
"github.com/samcharles93/archie-core/modules/governance"
"github.com/samcharles93/archie-core/modules/judgement"
@@ -196,6 +197,16 @@ func (m *Module) refuse(ctx context.Context, id uuid.UUID, identity string, reas
identity = "shared-token"
}
m.log.WarnContext(ctx, "approval answer refused", "request", id, "identity", identity, "reason", reason.Error())
audit.Emit(ctx, m.audit, m.log, audit.Event{
Category: "approval",
Action: "approval.refuse",
ActorID: identity,
ActorKind: viaDashboard,
ResourceKind: "approval",
ResourceID: id.String(),
Outcome: "denied",
Detail: map[string]any{"reason": reason.Error()},
})
if err := m.store.Refuse(ctx, store.Refusal{
ID: uuid.New(), OrgID: m.orgID, RequestID: id, Identity: identity, Via: viaDashboard, Reason: reason.Error(), At: m.now().UTC(),
}); err != nil {