Trigger dispatch, run start, every run claim, every delivery attempt and webhook intake now ask the policy chain. Background work acts as the workload principal; a refusal is final and recorded on the run or the delivery, and an undecidable chain leaves the work queued for another attempt. Intake applies the per-source delivery policy after source verification.
108 lines
4.3 KiB
Go
108 lines
4.3 KiB
Go
package deliveries
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/samcharles93/archie-core/modules/connectors"
|
|
"github.com/samcharles93/archie-core/modules/deliveries/internal/store"
|
|
"github.com/samcharles93/archie-core/modules/governance"
|
|
)
|
|
|
|
// sequencedAccess answers each Authorize with the next decision, the last one
|
|
// repeating, and records what was asked.
|
|
type sequencedAccess struct {
|
|
decisions []governance.Decision
|
|
asked []string
|
|
}
|
|
|
|
func (s *sequencedAccess) Authorize(_ context.Context, p governance.Principal, a governance.Action, r governance.Resource, _ governance.AccessContext) governance.Decision {
|
|
s.asked = append(s.asked, string(p.IdentityID)+" "+string(a)+" "+string(r.Kind)+" "+r.ID)
|
|
d := s.decisions[0]
|
|
if len(s.decisions) > 1 {
|
|
s.decisions = s.decisions[1:]
|
|
}
|
|
return d
|
|
}
|
|
|
|
// ledger records each attempt's outcome by kind.
|
|
type ledger struct{ outcomes []string }
|
|
|
|
func (l *ledger) Sent(context.Context, int64, int, string, time.Duration) error {
|
|
l.outcomes = append(l.outcomes, "sent")
|
|
return nil
|
|
}
|
|
|
|
func (l *ledger) Failed(_ context.Context, _ int64, _ int, reason string, _ time.Duration) error {
|
|
l.outcomes = append(l.outcomes, "failed: "+reason)
|
|
return nil
|
|
}
|
|
|
|
func (l *ledger) GaveUp(_ context.Context, _ int64, _ int, reason string, _ time.Duration) error {
|
|
l.outcomes = append(l.outcomes, "gave up: "+reason)
|
|
return nil
|
|
}
|
|
|
|
// channelLookups counts the sends a worker tried: it has no connector running,
|
|
// so a send that was attempted fails and a send that was refused never looks.
|
|
type channelLookups struct{ looked int }
|
|
|
|
func (c *channelLookups) Delivering(string) []*connectors.Connector { return nil }
|
|
|
|
func (c *channelLookups) Get(string) (*connectors.Connector, bool) {
|
|
c.looked++
|
|
return nil, false
|
|
}
|
|
|
|
// TestDeliveryIsAuthorizedOnEveryAttempt guards that a queued message is sent
|
|
// only while the workload principal may still deliver on its channel. Each
|
|
// attempt, including a retry after a backoff or a restart, asks again: a
|
|
// revoked grant ends the message as failed, and a chain that can't decide
|
|
// leaves it queued for its next attempt instead of dropping it.
|
|
func TestDeliveryIsAuthorizedOnEveryAttempt(t *testing.T) {
|
|
allowed, revoked, unavailable := governance.Allowed(), governance.DeniedAt(governance.LevelOrg, []string{"org-workload"}), governance.Unavailable()
|
|
tests := []struct {
|
|
name string
|
|
decisions []governance.Decision
|
|
unwired bool
|
|
attempts int
|
|
wantSends int
|
|
wantOutcomes []string
|
|
}{
|
|
{name: "an allowed attempt is sent", decisions: []governance.Decision{allowed}, attempts: 1, wantSends: 1, wantOutcomes: []string{"failed: connector \"slack\" isn't running"}},
|
|
{name: "a revoked grant ends the message", decisions: []governance.Decision{revoked}, attempts: 1, wantOutcomes: []string{"gave up: " + connectors.ErrRejected.Error() + ": " + governance.ErrDenied.Error() + ": deliver delivery slack"}},
|
|
{name: "a retry after revocation is refused", decisions: []governance.Decision{allowed, revoked}, attempts: 2, wantSends: 1, wantOutcomes: []string{"failed: connector \"slack\" isn't running", "gave up: " + connectors.ErrRejected.Error() + ": " + governance.ErrDenied.Error() + ": deliver delivery slack"}},
|
|
{name: "an undecidable chain leaves the message queued", decisions: []governance.Decision{unavailable}, attempts: 3},
|
|
{name: "no policy wired leaves the message queued", unwired: true, attempts: 1},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
access, out, channels := &sequencedAccess{decisions: tt.decisions}, &ledger{}, &channelLookups{}
|
|
m := &Module{outcomes: out, channels: channels, log: slog.New(slog.DiscardHandler)}
|
|
if !tt.unwired {
|
|
m.UseAccess(access)
|
|
}
|
|
for range tt.attempts {
|
|
m.deliver(t.Context(), store.Message{Seq: 1, ID: uuid.New(), Channel: "slack", Attempts: 1})
|
|
}
|
|
if channels.looked != tt.wantSends {
|
|
t.Fatalf("sends tried = %d, want %d", channels.looked, tt.wantSends)
|
|
}
|
|
if !slices.Equal(out.outcomes, tt.wantOutcomes) {
|
|
t.Fatalf("outcomes = %q, want %q", out.outcomes, tt.wantOutcomes)
|
|
}
|
|
for _, call := range access.asked {
|
|
if !strings.HasPrefix(call, "workload deliver delivery ") {
|
|
t.Fatalf("asked %q, want the workload principal to deliver", call)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|