An action judgement holds at propose and wait is no longer a dead end. The new approvals module owns the request: it binds to the decision and the action's digest, so an answer covers the action the person was shown and nothing else, and it settles once. The answer wakes the run, which judges the action again and runs it only as the approval allows: a refusal still refuses, an altered input runs nothing, and a judgement that came back worse runs nothing on that approval. The composer carries the request as a message with the request id and its deadline, and its answer is a message too, so the trail shows what was asked and what was answered. A request nobody answers inside its deadline is settled as expired and nothing runs. How long a request waits is an organisation setting, edited in the dashboard and stored in milliseconds, because a Postgres interval scans as text. A run pins the deadline it started with, like its model. An agent that proposes the same action twice is asked about it once, and the notification says when the investigation proposed nothing at all.
269 lines
8.4 KiB
Go
269 lines
8.4 KiB
Go
package judgement
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json/jsontext"
|
|
"errors"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/samcharles93/archie-core/modules/connectors"
|
|
)
|
|
|
|
type fixedGuard struct {
|
|
name string
|
|
ceiling Level
|
|
err error
|
|
advisory bool
|
|
panics bool
|
|
calls *atomic.Int32
|
|
}
|
|
|
|
func (g fixedGuard) Name() string { return g.name }
|
|
func (g fixedGuard) Advisory() bool { return g.advisory }
|
|
|
|
func (g fixedGuard) Judge(context.Context, Action) (Verdict, error) {
|
|
if g.calls != nil {
|
|
g.calls.Add(1)
|
|
}
|
|
if g.panics {
|
|
panic("guard bug")
|
|
}
|
|
return Verdict{Ceiling: g.ceiling}, g.err
|
|
}
|
|
|
|
type refuseAll struct{}
|
|
|
|
func (refuseAll) Name() string { return "no deletes in production" }
|
|
func (refuseAll) Refuses(Action) (string, bool) { return "production is off limits", true }
|
|
|
|
// TestDecisionRunsAtTheLowestCeiling guards the weakest-link decision (spec
|
|
// section 5, decision tree 8.3): hard rules refuse before any guard runs; the
|
|
// action runs at the lowest ceiling; a guard that fails, panics or gives no
|
|
// ceiling holds the action at propose and wait; an advisory verdict is
|
|
// recorded but sets no ceiling.
|
|
func TestDecisionRunsAtTheLowestCeiling(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
rules []HardRule
|
|
guards []Guard
|
|
want Level
|
|
wantCalls int32
|
|
}{
|
|
{
|
|
name: "the lower of two ceilings wins",
|
|
guards: []Guard{fixedGuard{name: "a", ceiling: ActAndTell}, fixedGuard{name: "b", ceiling: ProposeAndWait}},
|
|
want: ProposeAndWait,
|
|
},
|
|
{
|
|
name: "guards with no concern act quietly",
|
|
guards: []Guard{fixedGuard{name: "a", ceiling: ActQuietly}, fixedGuard{name: "b", ceiling: ActQuietly}},
|
|
want: ActQuietly,
|
|
},
|
|
{
|
|
name: "a failing guard holds the action",
|
|
guards: []Guard{fixedGuard{name: "a", ceiling: ActQuietly}, fixedGuard{name: "b", err: errors.New("no evidence")}},
|
|
want: ProposeAndWait,
|
|
},
|
|
{
|
|
name: "a panicking guard holds the action",
|
|
guards: []Guard{fixedGuard{name: "a", ceiling: ActQuietly}, fixedGuard{name: "b", panics: true}},
|
|
want: ProposeAndWait,
|
|
},
|
|
{
|
|
name: "a guard without a ceiling holds the action",
|
|
guards: []Guard{fixedGuard{name: "a", ceiling: ActQuietly}, fixedGuard{name: "b"}},
|
|
want: ProposeAndWait,
|
|
},
|
|
{
|
|
name: "an advisory verdict sets no ceiling",
|
|
guards: []Guard{fixedGuard{name: "a", ceiling: ActQuietly}, fixedGuard{name: "judge", ceiling: Never, advisory: true}},
|
|
want: ActQuietly,
|
|
},
|
|
{
|
|
name: "a failing advisory guard sets no ceiling",
|
|
guards: []Guard{fixedGuard{name: "a", ceiling: ActAndTell}, fixedGuard{name: "judge", err: errors.New("timeout"), advisory: true}},
|
|
want: ActAndTell,
|
|
},
|
|
{
|
|
name: "no guards at all holds the action",
|
|
want: ProposeAndWait,
|
|
},
|
|
{
|
|
name: "a hard rule refuses before any guard runs",
|
|
rules: []HardRule{refuseAll{}},
|
|
guards: []Guard{fixedGuard{name: "a", ceiling: ActQuietly}},
|
|
want: Never,
|
|
wantCalls: 0,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
var calls atomic.Int32
|
|
for i, g := range tt.guards {
|
|
if f, ok := g.(fixedGuard); ok {
|
|
f.calls = &calls
|
|
tt.guards[i] = f
|
|
}
|
|
}
|
|
m := &Module{rules: tt.rules, guards: tt.guards}
|
|
d := m.decide(t.Context(), Action{Connector: "c", Name: "delete"})
|
|
if d.Level != tt.want {
|
|
t.Fatalf("level = %s, want %s (verdicts %+v)", d.Level, tt.want, d.Verdicts)
|
|
}
|
|
if len(d.Verdicts) != len(tt.guards) && tt.want != Never {
|
|
t.Fatalf("verdicts = %d, want one per guard (%d)", len(d.Verdicts), len(tt.guards))
|
|
}
|
|
if tt.rules != nil && calls.Load() != tt.wantCalls {
|
|
t.Fatalf("guards ran %d times, want %d", calls.Load(), tt.wantCalls)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
type countingTool struct{ invoked *atomic.Int32 }
|
|
|
|
func (t countingTool) Lookup(context.Context, string, jsontext.Value) (string, bool, error) {
|
|
return "", false, nil
|
|
}
|
|
|
|
func (t countingTool) Invoke(context.Context, connectors.Invocation) (string, error) {
|
|
t.invoked.Add(1)
|
|
return "done", nil
|
|
}
|
|
|
|
func (t countingTool) Resolve(context.Context, string, jsontext.Value) (connectors.Resolution, error) {
|
|
return connectors.Resolution{}, nil
|
|
}
|
|
|
|
type oneTool struct{ tool connectors.Tool }
|
|
|
|
func (o oneTool) Tool(string) (connectors.Tool, bool) { return o.tool, true }
|
|
|
|
// TestExecuteRunsOnlyTheJudgedAction guards spec section 5 rule 6 and
|
|
// decision tree 8.5: an action runs only at a level that runs without
|
|
// approval, and only with exactly the input that was judged. A refused, held
|
|
// or altered action never reaches the connector.
|
|
func TestExecuteRunsOnlyTheJudgedAction(t *testing.T) {
|
|
judged := jsontext.Value(`{"path":"/tmp/runner"}`)
|
|
decided := func(l Level) Decision {
|
|
return Decision{Level: l, Action: Action{Connector: "c", Name: "delete", Input: judged, Digest: connectors.Digest("delete", judged)}}
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
d Decision
|
|
input jsontext.Value
|
|
wantErr error
|
|
}{
|
|
{name: "act quietly runs", d: decided(ActQuietly), input: judged},
|
|
{name: "act and tell runs", d: decided(ActAndTell), input: judged},
|
|
{name: "propose and wait is held", d: decided(ProposeAndWait), input: judged, wantErr: ErrHeld},
|
|
{name: "escalate is held", d: decided(Escalate), input: judged, wantErr: ErrHeld},
|
|
{name: "never is refused", d: decided(Never), input: judged, wantErr: ErrRefused},
|
|
{name: "altered input is refused", d: decided(ActQuietly), input: jsontext.Value(`{"path":"/"}`), wantErr: ErrAltered},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
var invoked atomic.Int32
|
|
m := &Module{tools: oneTool{countingTool{&invoked}}}
|
|
_, err := m.Execute(t.Context(), tt.d, tt.input, "run:1")
|
|
if !errors.Is(err, tt.wantErr) {
|
|
t.Fatalf("err = %v, want %v", err, tt.wantErr)
|
|
}
|
|
want := int32(1)
|
|
if tt.wantErr != nil {
|
|
want = 0
|
|
}
|
|
if invoked.Load() != want {
|
|
t.Fatalf("connector invoked %d times, want %d", invoked.Load(), want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestExecuteApprovedRunsOnlyWhatThePersonSaw guards the approval boundary: an
|
|
// approval covers the decision and the exact action a person was shown, so a
|
|
// replayed or replayed-onto-another-action approval runs nothing, and a
|
|
// refusal still refuses with one.
|
|
func TestExecuteApprovedRunsOnlyWhatThePersonSaw(t *testing.T) {
|
|
judged := jsontext.Value(`{"path":"/tmp/runner"}`)
|
|
const key = "run:1:3"
|
|
decided := func() Decision {
|
|
return Decision{
|
|
ID: uuid.MustParse("11111111-1111-1111-1111-111111111111"),
|
|
Level: ProposeAndWait,
|
|
Action: Action{
|
|
Connector: "c", Name: "delete", Input: judged,
|
|
Digest: connectors.Digest("delete", judged),
|
|
},
|
|
}
|
|
}
|
|
approval := func(d Decision) Approval {
|
|
return Approval{DecisionID: d.ID, Digest: d.Action.Digest, By: "admin", At: time.Now().UTC()}
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
d func() Decision
|
|
a func(Decision) Approval
|
|
input jsontext.Value
|
|
wantErr error
|
|
}{
|
|
{name: "an approval for this action runs it", d: decided, a: approval, input: judged},
|
|
{
|
|
name: "an approval naming another decision runs nothing",
|
|
d: decided, input: judged,
|
|
a: func(d Decision) Approval {
|
|
other := approval(d)
|
|
other.DecisionID = uuid.MustParse("22222222-2222-2222-2222-222222222222")
|
|
return other
|
|
},
|
|
wantErr: ErrUnapproved,
|
|
},
|
|
{
|
|
name: "an approval for another action runs nothing",
|
|
d: decided, input: judged,
|
|
a: func(d Decision) Approval {
|
|
other := approval(d)
|
|
other.Digest = connectors.Digest("delete", jsontext.Value(`{"path":"/"}`))
|
|
return other
|
|
},
|
|
wantErr: ErrUnapproved,
|
|
},
|
|
{
|
|
name: "input that differs from the judged action is refused",
|
|
d: decided, a: approval, input: jsontext.Value(`{"path":"/"}`),
|
|
wantErr: ErrAltered,
|
|
},
|
|
{
|
|
name: "a refusal still refuses with one",
|
|
d: func() Decision {
|
|
d := decided()
|
|
d.Level, d.RefusedBy, d.Reason = Never, "blackout", "a release is in flight"
|
|
return d
|
|
},
|
|
a: approval, input: judged, wantErr: ErrRefused,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
var invoked atomic.Int32
|
|
m := &Module{tools: oneTool{countingTool{&invoked}}}
|
|
d := tt.d()
|
|
_, err := m.ExecuteApproved(t.Context(), d, tt.a(d), tt.input, key)
|
|
if !errors.Is(err, tt.wantErr) {
|
|
t.Fatalf("err = %v, want %v", err, tt.wantErr)
|
|
}
|
|
want := int32(1)
|
|
if tt.wantErr != nil {
|
|
want = 0
|
|
}
|
|
if got := invoked.Load(); got != want {
|
|
t.Fatalf("connector invoked %d times, want %d", got, want)
|
|
}
|
|
})
|
|
}
|
|
}
|