feat(audit,approvals,deliveries): wire audit producers, ITSM change requests, and fix Dockerfile.archie (#2153)
This commit is contained in:
26 files changed
+671
-35
No files matched your search
@@ -2,6 +2,7 @@ package events
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
@@ -50,8 +51,84 @@ func (noEcho) SentBy(context.Context, string, ...string) (uuid.UUID, bool, error
|
||||
return uuid.Nil, false, nil
|
||||
}
|
||||
|
||||
func (noEcho) RequestByExternalID(context.Context, string, string) (uuid.UUID, bool, error) {
|
||||
return uuid.Nil, false, nil
|
||||
}
|
||||
|
||||
func (noEcho) Send(context.Context, string, string, string, string) error { return nil }
|
||||
|
||||
type fakeChangeDeliveries struct {
|
||||
noEcho
|
||||
byNumber map[string]uuid.UUID
|
||||
}
|
||||
|
||||
func (f fakeChangeDeliveries) RequestByExternalID(_ context.Context, channel, externalID string) (uuid.UUID, bool, error) {
|
||||
id, ok := f.byNumber[channel+":"+externalID]
|
||||
return id, ok, nil
|
||||
}
|
||||
|
||||
type fakeChangeAnswers struct {
|
||||
settled []struct {
|
||||
id uuid.UUID
|
||||
via string
|
||||
number string
|
||||
approved bool
|
||||
}
|
||||
}
|
||||
|
||||
func (f *fakeChangeAnswers) Settle(context.Context, uuid.UUID, governance.Subject, string, bool) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (f *fakeChangeAnswers) SettleChangeRequest(_ context.Context, id uuid.UUID, connector, changeNumber string, approved bool) (bool, error) {
|
||||
f.settled = append(f.settled, struct {
|
||||
id uuid.UUID
|
||||
via string
|
||||
number string
|
||||
approved bool
|
||||
}{id: id, via: connector, number: changeNumber, approved: approved})
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func TestDispatchSettlesOwnChangeRequestDecisionEvents(t *testing.T) {
|
||||
reqID := uuid.New()
|
||||
answers := &fakeChangeAnswers{}
|
||||
m := &Module{
|
||||
reader: oneSource{connector: "servicenow"},
|
||||
deliveries: fakeChangeDeliveries{byNumber: map[string]uuid.UUID{"servicenow:CHG0012345": reqID}},
|
||||
answers: answers,
|
||||
log: slog.New(slog.DiscardHandler),
|
||||
}
|
||||
evApproved := store.Event{
|
||||
ID: uuid.New(),
|
||||
OrgID: "default",
|
||||
SourceID: hookSource,
|
||||
DeclaredEventType: changeApprovedType,
|
||||
LinkKey: sql.NullString{String: "CHG0012345", Valid: true},
|
||||
Own: true,
|
||||
}
|
||||
if _, err := m.dispatch(nil, evApproved); err != nil {
|
||||
t.Fatalf("dispatch approved change event: %v", err)
|
||||
}
|
||||
evRejected := evApproved
|
||||
evRejected.ID = uuid.New()
|
||||
evRejected.DeclaredEventType = changeRejectedType
|
||||
evRejected.LinkKey = sql.NullString{}
|
||||
evRejected.Fields = []byte(`{"number":"CHG0012345"}`)
|
||||
if _, err := m.dispatch(nil, evRejected); err != nil {
|
||||
t.Fatalf("dispatch rejected change event: %v", err)
|
||||
}
|
||||
if len(answers.settled) != 2 {
|
||||
t.Fatalf("settled count = %d, want 2", len(answers.settled))
|
||||
}
|
||||
if answers.settled[0].id != reqID || answers.settled[0].via != "servicenow" || answers.settled[0].number != "CHG0012345" || !answers.settled[0].approved {
|
||||
t.Fatalf("unexpected first settlement: %+v", answers.settled[0])
|
||||
}
|
||||
if answers.settled[1].id != reqID || answers.settled[1].approved {
|
||||
t.Fatalf("unexpected second settlement: %+v", answers.settled[1])
|
||||
}
|
||||
}
|
||||
|
||||
// TestIntakeAppliesTheDeliveryPolicyBeforeStoring guards the webhook entry
|
||||
// point: a delivery that passes source verification is still refused when the
|
||||
// per-source, per-address delivery policy denies it or can't be read, and
|
||||
|
||||
+77
-14
@@ -60,6 +60,9 @@ type Runs interface {
|
||||
// belonged to no run.
|
||||
type Deliveries interface {
|
||||
SentBy(ctx context.Context, channel string, ids ...string) (run uuid.UUID, found bool, err error)
|
||||
// RequestByExternalID resolves a confirmed change_request's external ID
|
||||
// back to its approval request ID.
|
||||
RequestByExternalID(ctx context.Context, channel, externalID string) (uuid.UUID, bool, error)
|
||||
// Send writes text to one chat on a channel.
|
||||
Send(ctx context.Context, channel, chatID, text, key string) error
|
||||
}
|
||||
@@ -67,6 +70,7 @@ type Deliveries interface {
|
||||
// Answers settles the approval requests answer events name.
|
||||
type Answers interface {
|
||||
Settle(ctx context.Context, id uuid.UUID, who governance.Subject, via string, approved bool) (bool, error)
|
||||
SettleChangeRequest(ctx context.Context, id uuid.UUID, connector, changeNumber string, approved bool) (bool, error)
|
||||
}
|
||||
|
||||
// Links binds the channel accounts that account.link events name to the
|
||||
@@ -82,8 +86,10 @@ type Links interface {
|
||||
// Both name the sender as "<connector>:<channel user id>" in by, and may name
|
||||
// the chat to reply to in chat_id.
|
||||
const (
|
||||
answerType = "approval.answer"
|
||||
linkType = "account.link"
|
||||
answerType = "approval.answer"
|
||||
linkType = "account.link"
|
||||
changeApprovedType = "change.approved"
|
||||
changeRejectedType = "change.rejected"
|
||||
)
|
||||
|
||||
// listenRetry is how long a listener waits after its stream breaks.
|
||||
@@ -551,6 +557,50 @@ func (m *Module) answer(ctx context.Context, e store.Event) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// settleChange resolves a confirmed change number from change.approved or
|
||||
// change.rejected back to the pending change-request approval and settles it.
|
||||
func (m *Module) settleChange(ctx context.Context, e store.Event, approved bool) error {
|
||||
if !e.SourceID.Valid || m.reader == nil || m.deliveries == nil || m.answers == nil {
|
||||
return nil
|
||||
}
|
||||
src, err := m.reader.Source(ctx, e.OrgID, e.SourceID.UUID)
|
||||
if err != nil {
|
||||
m.log.WarnContext(ctx, "change event from an unknown source", "event", e.ID, "source", e.SourceID.UUID, "error", err)
|
||||
return nil
|
||||
}
|
||||
var changeNumber string
|
||||
if e.LinkKey.Valid {
|
||||
changeNumber = strings.TrimSpace(e.LinkKey.String)
|
||||
}
|
||||
if changeNumber == "" {
|
||||
var f struct {
|
||||
Number string `json:"number"`
|
||||
}
|
||||
if err := json.Unmarshal(e.Fields, &f); err == nil {
|
||||
changeNumber = strings.TrimSpace(f.Number)
|
||||
}
|
||||
}
|
||||
if changeNumber == "" {
|
||||
m.log.WarnContext(ctx, "change event missing link key or change number", "event", e.ID)
|
||||
return nil
|
||||
}
|
||||
reqID, found, err := m.deliveries.RequestByExternalID(ctx, src.Connector, changeNumber)
|
||||
if err != nil {
|
||||
return fmt.Errorf("lookup change request %s: %w", changeNumber, err)
|
||||
}
|
||||
if !found {
|
||||
return nil
|
||||
}
|
||||
settled, err := m.answers.SettleChangeRequest(ctx, reqID, src.Connector, changeNumber, approved)
|
||||
if err != nil {
|
||||
return fmt.Errorf("settle change request %s: %w", reqID, err)
|
||||
}
|
||||
if !settled {
|
||||
m.log.InfoContext(ctx, "change request decision changed nothing", "event", e.ID, "request", reqID, "change", changeNumber)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// link binds the channel account that sent a link code to the account that
|
||||
// issued the code, and tells the sender how it went. A code that doesn't work
|
||||
// is not retried.
|
||||
@@ -631,18 +681,8 @@ func (m *Module) reply(ctx context.Context, e store.Event, src store.Source, cha
|
||||
// dispatch never starts a second run.
|
||||
func (m *Module) dispatch(tx *sqlx.Tx, e store.Event) (bool, error) {
|
||||
ctx := context.Background()
|
||||
if e.Own {
|
||||
// Archie's own message coming back starts nothing and proposes nothing.
|
||||
return false, nil
|
||||
}
|
||||
switch e.DeclaredEventType {
|
||||
case answerType:
|
||||
return false, m.answer(ctx, e)
|
||||
case linkType:
|
||||
if err := m.link(ctx, e); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return false, m.redactHandledLink(ctx, tx, e)
|
||||
if handled, err := m.dispatchBuiltin(ctx, tx, e); handled || err != nil {
|
||||
return false, err
|
||||
}
|
||||
linked, err := store.Link(ctx, tx, e)
|
||||
if err != nil || linked {
|
||||
@@ -671,6 +711,29 @@ func (m *Module) dispatch(tx *sqlx.Tx, e store.Event) (bool, error) {
|
||||
return matched, nil
|
||||
}
|
||||
|
||||
func (m *Module) dispatchBuiltin(ctx context.Context, tx *sqlx.Tx, e store.Event) (bool, error) {
|
||||
switch e.DeclaredEventType {
|
||||
case changeApprovedType:
|
||||
return true, m.settleChange(ctx, e, true)
|
||||
case changeRejectedType:
|
||||
return true, m.settleChange(ctx, e, false)
|
||||
}
|
||||
if e.Own {
|
||||
// Archie's own message coming back starts nothing and proposes nothing.
|
||||
return true, nil
|
||||
}
|
||||
switch e.DeclaredEventType {
|
||||
case answerType:
|
||||
return true, m.answer(ctx, e)
|
||||
case linkType:
|
||||
if err := m.link(ctx, e); err != nil {
|
||||
return true, err
|
||||
}
|
||||
return true, m.redactHandledLink(ctx, tx, e)
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// outcome is the result of evaluating a trigger against an event: notMatched
|
||||
// (its filter didn't match), noRun (it matched but started no run, and the
|
||||
// dispatch records why) or fired (a run started).
|
||||
|
||||
Reference in new issue
Block a user