feat(identity): add server sessions and audited mail recovery
This commit is contained in:
1 parent
20c03288d6
commit
4ee53d9a74
58 files changed
+5359
-567
No files matched your search
@@ -640,6 +640,47 @@ func (delivery Delivery) Rebind(at time.Time, providerAccount MailProviderAccoun
|
||||
return next, nil
|
||||
}
|
||||
|
||||
// StopAutomaticRetry ends the automatic retry schedule without claiming that
|
||||
// an uncertain delivery was not sent. The delivery remains unknown, with no
|
||||
// due time, so only an audited operator decision can start another send cycle.
|
||||
// It also accepts pending or waiting_quota when a later confirmed refusal
|
||||
// leaves an earlier unknown outcome unresolved after the automatic cutoff.
|
||||
func (delivery Delivery) StopAutomaticRetry(at time.Time) (Delivery, error) {
|
||||
const location = "Delivery.StopAutomaticRetry"
|
||||
if err := delivery.checkChangeTime(location, at); err != nil {
|
||||
return Delivery{}, err
|
||||
}
|
||||
if (delivery.status != DeliveryStatusUnknown && delivery.status != DeliveryStatusPending && delivery.status != DeliveryStatusWaitingQuota) || delivery.nextAttemptAt.IsZero() {
|
||||
return Delivery{}, fmt.Errorf("%s - delivery is %s without a scheduled retry: %w", location, delivery.status, ErrDeliveryTransition)
|
||||
}
|
||||
next := delivery
|
||||
next.status = DeliveryStatusUnknown
|
||||
next.nextAttemptAt = time.Time{}
|
||||
next.version = delivery.version + 1
|
||||
next.updatedAt = at
|
||||
return next, nil
|
||||
}
|
||||
|
||||
// ScheduleUnknownRetry keeps an unknown delivery in the due queue until a
|
||||
// provider quota reset. It does not change the outcome or append an attempt.
|
||||
func (delivery Delivery) ScheduleUnknownRetry(at, nextAttemptAt time.Time) (Delivery, error) {
|
||||
const location = "Delivery.ScheduleUnknownRetry"
|
||||
if err := delivery.checkChangeTime(location, at); err != nil {
|
||||
return Delivery{}, err
|
||||
}
|
||||
if delivery.status != DeliveryStatusUnknown || delivery.nextAttemptAt.IsZero() {
|
||||
return Delivery{}, fmt.Errorf("%s - delivery is %s without an automatic retry: %w", location, delivery.status, ErrDeliveryTransition)
|
||||
}
|
||||
if nextAttemptAt.IsZero() || nextAttemptAt.Before(at) {
|
||||
return Delivery{}, invalidDelivery(location, "next retry time must be set and not precede the decision")
|
||||
}
|
||||
next := delivery
|
||||
next.nextAttemptAt = nextAttemptAt
|
||||
next.version = delivery.version + 1
|
||||
next.updatedAt = at
|
||||
return next, nil
|
||||
}
|
||||
|
||||
// checkChangeTime rejects a zero delivery and a change time before the last
|
||||
// recorded change.
|
||||
func (delivery Delivery) checkChangeTime(location string, at time.Time) error {
|
||||
|
||||
@@ -317,6 +317,49 @@ func TestRecordAttemptRejectsInconsistentResults(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStopAutomaticRetryKeepsAnUncertainDeliveryUnknown(t *testing.T) {
|
||||
start := newTestDelivery(t)
|
||||
scheduled := mustRecord(t, start, DeliveryAttemptInput{
|
||||
At: after(1), Outcome: DeliveryOutcomeUnknown, NextAttemptAt: after(2),
|
||||
})
|
||||
stopped, err := scheduled.StopAutomaticRetry(after(3))
|
||||
if err != nil {
|
||||
t.Fatalf("StopAutomaticRetry() error = %v", err)
|
||||
}
|
||||
if stopped.Status() != DeliveryStatusUnknown || !stopped.NextAttemptAt().IsZero() {
|
||||
t.Fatalf("stopped delivery is %s due %s, want unknown without a due time", stopped.Status(), stopped.NextAttemptAt())
|
||||
}
|
||||
if stopped.AttemptCount() != scheduled.AttemptCount() || stopped.Version() != scheduled.Version()+1 {
|
||||
t.Fatalf("stopped delivery has %d attempts and version %d, want %d and %d", stopped.AttemptCount(), stopped.Version(), scheduled.AttemptCount(), scheduled.Version()+1)
|
||||
}
|
||||
if stopped.IdempotencyKey() != scheduled.IdempotencyKey() || stopped.Content().Hash() != scheduled.Content().Hash() {
|
||||
t.Fatal("stopping retries changed the idempotency key or message content")
|
||||
}
|
||||
if _, err := stopped.StopAutomaticRetry(after(4)); !errors.Is(err, ErrDeliveryTransition) {
|
||||
t.Fatalf("StopAutomaticRetry() without a scheduled retry error = %v, want ErrDeliveryTransition", err)
|
||||
}
|
||||
|
||||
quotaWait := mustRecord(t, newTestDelivery(t), DeliveryAttemptInput{
|
||||
At: after(1), Outcome: DeliveryOutcomeRejected, Rejection: DeliveryRejectionQuotaExhausted, NextAttemptAt: after(2),
|
||||
})
|
||||
quotaStopped, err := quotaWait.StopAutomaticRetry(after(3))
|
||||
if err != nil || quotaStopped.Status() != DeliveryStatusUnknown || !quotaStopped.NextAttemptAt().IsZero() {
|
||||
t.Fatalf("StopAutomaticRetry() after a manual-cycle quota refusal = %s due %s, %v; want unknown without a due time",
|
||||
quotaStopped.Status(), quotaStopped.NextAttemptAt(), err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMailRetryActor(t *testing.T) {
|
||||
if got, err := ParseMailRetryActor("operator:mail-ops"); err != nil || got != "operator:mail-ops" {
|
||||
t.Fatalf("ParseMailRetryActor() = %q, %v", got, err)
|
||||
}
|
||||
for _, value := range []string{"", "operator:Upper", "operator:has space", "person:mail-ops", "operator:" + strings.Repeat("a", 122)} {
|
||||
if _, err := ParseMailRetryActor(value); !errors.Is(err, ErrInvalidDelivery) {
|
||||
t.Fatalf("ParseMailRetryActor(%q) error = %v, want ErrInvalidDelivery", value, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func deliverySnapshot(delivery Delivery) DeliverySnapshot {
|
||||
return DeliverySnapshot{
|
||||
ID: delivery.ID(),
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package domain
|
||||
|
||||
import "regexp"
|
||||
|
||||
var mailRetryActorPattern = regexp.MustCompile(`^operator:[a-z0-9][a-z0-9._-]*$`)
|
||||
|
||||
// MailRetryActor identifies an operator who authorized a retry of an unknown
|
||||
// delivery. It is a handle, not an account ID, email address, or credential.
|
||||
type MailRetryActor string
|
||||
|
||||
// ParseMailRetryActor accepts an operator handle in the form
|
||||
// "operator:<handle>".
|
||||
func ParseMailRetryActor(value string) (MailRetryActor, error) {
|
||||
if len(value) > 128 || !mailRetryActorPattern.MatchString(value) {
|
||||
return "", invalidDelivery("domain.ParseMailRetryActor", "actor must be operator:<handle> of at most 128 bytes in lowercase letters, digits, '.', '_', or '-'")
|
||||
}
|
||||
return MailRetryActor(value), nil
|
||||
}
|
||||
Reference in new issue
Block a user