logtail: a refusal the panel did not get to write is written from mail.log (stage 2b)
test / test (push) Canceled after 0s
test / test (push) Canceled after 0s
The sender check answers 550 5.7.1 and writes the refused row through a
buffer a moment later; a panel stopped in between (a rollout, a kill) left
an application refused and a log that did not know it. A refusal at MAIL
FROM needs nothing: the journal milter writes its row before it answers.
- logtail.refusalLines remembers each queue id's login from smtpd's
"<id>: client=..., sasl_username=<login>" (4096 ids at most) and, on
cleanup's "<id>: milter-reject: END-OF-MESSAGE ...: 5.7.1 From: <line> -
<why>; from=<...> to=<...>", waits 30 s and hands it to
store.RecoverRefusal - an INSERT ... WHERE NOT EXISTS a refused row of that
queue id within two minutes: the row the panel wrote, or one recovered
before, means nothing is written. It is read from the saved offset on, so
a kill is covered as well as a clean stop - which a flush of the buffer at
shutdown would not.
- Migration 0009 (not deployed yet) gains send_log.recovered; the row is
written with the line's time, the login's domain id, reason "from", the
From: line as the check printed it (to the last " - " before ";").
- The message page: a fact "Written: from mail.log after a restart: the
panel stopped before it wrote the refusal".
Tests: store (not written when the panel wrote it; written once however
often); logtail (the login from client=, the From: line with a " - " in it,
nothing before the wait is over); view (the fact). e2e
refusal_recovered_from_the_log: a From: refusal, the panel stopped, its row
deleted and the tailer's saved place moved before the message's first line
(python3 in the container, as the panel's user - a kill cannot be timed to
that moment from outside); started again, the row is back, recovered, once.
Also fixed: handlers TestMessagePageOfARefusedMessage still expected the
old sentence; it failed on the server after e166a2e, whose run showed only
the schema failures - now it wants the new one.
schema-migrations row 9, CHANGELOG [Unreleased], audit plan line ticked,
roadmap 26/62.
Evidence on the test server for this tree but the handler-test fix: make e2e
exit 0 (the new case 37 s); go test 22/23 ok, the 23rd that test - fixed,
passes locally; the full run follows before the push.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,3 +7,9 @@
|
||||
-- headers are judged), and its lines are found by it like any other's.
|
||||
-- DDL only; a row written earlier has '' and says so on its page.
|
||||
ALTER TABLE send_log ADD COLUMN refusal_line TEXT NOT NULL DEFAULT '';
|
||||
|
||||
-- A refusal row the panel did not get to write - the sender check answers 550
|
||||
-- and writes the row a moment later, and a panel stopped in between wrote
|
||||
-- none - is written by the log tailer from Postfix's line about it, and marked
|
||||
-- so: recovered = 1.
|
||||
ALTER TABLE send_log ADD COLUMN recovered INTEGER NOT NULL DEFAULT 0;
|
||||
|
||||
@@ -77,3 +77,39 @@ func TestSendLogLinesOfARefusalAreItsOwn(t *testing.T) {
|
||||
t.Fatalf("lines = %+v, want its own alone", lines)
|
||||
}
|
||||
}
|
||||
|
||||
// A From: refusal's row is written from the log only when the panel wrote
|
||||
// none for that queue id about then; once, however often the line is read.
|
||||
func TestRecoverRefusal(t *testing.T) {
|
||||
st := openTestStore(t)
|
||||
d := addTestDomain(t, st, "sender.example")
|
||||
if _, err := st.AddApplication(d.ID, "app1", AddressModeWildcard, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
// The panel wrote this one itself.
|
||||
if err := st.InsertRejected(SendLogEntry{QueueID: "WRITTEN", AppLogin: "app1", From: "a@sender.example", Reason: ReasonFrom}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
e := SendLogEntry{QueueID: "WRITTEN", AppLogin: "app1", From: "a@sender.example", To: "r@x", Domain: "sender.example", Reason: ReasonFrom, HeaderFrom: "x@y"}
|
||||
if wrote, err := st.RecoverRefusal(e, now); err != nil || wrote {
|
||||
t.Fatalf("a refusal the panel wrote was written again: %v, %v", wrote, err)
|
||||
}
|
||||
e.QueueID = "LOST"
|
||||
for i := 0; i < 2; i++ {
|
||||
wrote, err := st.RecoverRefusal(e, now)
|
||||
if err != nil || wrote != (i == 0) {
|
||||
t.Fatalf("recovery %d: wrote %v, %v", i, wrote, err)
|
||||
}
|
||||
}
|
||||
rows, _ := st.QuerySendLog(SendLogFilter{DomainIDs: []int64{d.ID}}, 10, 0)
|
||||
var found SendLogRow
|
||||
for _, r := range rows {
|
||||
if r.QueueID == "LOST" {
|
||||
found, _ = st.GetSendLog(r.ID)
|
||||
}
|
||||
}
|
||||
if !found.Recovered || found.Status != StatusRejected || found.Reason != ReasonFrom || found.HeaderFrom != "x@y" || found.DomainID != d.ID {
|
||||
t.Fatalf("the recovered row: %+v", found)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ func TestBaselineSchema(t *testing.T) {
|
||||
"applications": "id domain_id login address_mode auth_ip_restrict auth_allowed_ips created_at",
|
||||
"application_addresses": "id application_id address",
|
||||
"rate_limits": "id scope ref_id max_messages window_seconds mode auto_multiplier auto_updated_at",
|
||||
"send_log": "id queue_id domain app_login from_addr to_addr subject status created_at updated_at reason header_from domain_id refusal_line",
|
||||
"send_log": "id queue_id domain app_login from_addr to_addr subject status created_at updated_at reason header_from domain_id refusal_line recovered",
|
||||
"logtail_state": "path fingerprint read_offset updated_at",
|
||||
"inbound_domains": "id name recipient_mode created_at",
|
||||
"inbound_transports": "inbound_domain_id host port tls_mode",
|
||||
|
||||
@@ -224,7 +224,10 @@ type SendLogRow struct {
|
||||
// RefusalLine is Postfix's line about a refusal that has no queue id
|
||||
// (migration 0009); "" for every other row, and for one refused before.
|
||||
RefusalLine string
|
||||
CreatedAt time.Time
|
||||
// Recovered is a refusal row the log tailer wrote from mail.log because
|
||||
// the panel stopped before it wrote the row itself.
|
||||
Recovered bool
|
||||
CreatedAt time.Time
|
||||
// UpdatedAt is when the status last changed — the log-tailer's report of
|
||||
// the delivery attempt. It equals CreatedAt while a row is still queued.
|
||||
UpdatedAt time.Time
|
||||
@@ -256,6 +259,31 @@ func (s *Store) AttachRefusalLine(login, from string, at time.Time, line string)
|
||||
return n > 0, nil
|
||||
}
|
||||
|
||||
// RecoverRefusal writes the row of a message the sender check refused for its
|
||||
// From: line, from Postfix's line about it, when the panel stopped before it
|
||||
// wrote the row itself (audit 2026-10-12, stage 2b): the check answers 550
|
||||
// and writes the row through a buffer a moment later. The message had a queue
|
||||
// id; a refused row with that id from around at means the row was written,
|
||||
// and nothing is. It reports whether it wrote one.
|
||||
func (s *Store) RecoverRefusal(e SendLogEntry, at time.Time) (bool, error) {
|
||||
stamp := at.UTC().Format(time.RFC3339)
|
||||
res, err := s.db.Exec(
|
||||
`INSERT INTO send_log
|
||||
(queue_id, domain, app_login, from_addr, to_addr, subject, status, reason, header_from, recovered, created_at, updated_at, domain_id)
|
||||
SELECT ?, ?, ?, ?, ?, '', ?, ?, ?, 1, ?, ?, `+loginDomainID+`
|
||||
WHERE NOT EXISTS (SELECT 1 FROM send_log
|
||||
WHERE queue_id = ? AND status = ? AND created_at BETWEEN ? AND ?)`,
|
||||
e.QueueID, e.Domain, e.AppLogin, e.From, e.To, StatusRejected, e.Reason, e.HeaderFrom, stamp, stamp, e.AppLogin,
|
||||
e.QueueID, StatusRejected,
|
||||
at.Add(-refusalLineWindow).UTC().Format(time.RFC3339), at.Add(refusalLineWindow).UTC().Format(time.RFC3339),
|
||||
)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("recover refusal %s: %w", e.QueueID, err)
|
||||
}
|
||||
n, _ := res.RowsAffected()
|
||||
return n > 0, nil
|
||||
}
|
||||
|
||||
// refusalLineWindow is how far apart a refusal's row and its log line may be
|
||||
// dated: the milter writes the row at MAIL FROM, Postfix the line a moment
|
||||
// later, and the log's stamps are whole seconds.
|
||||
@@ -274,10 +302,10 @@ func (s *Store) GetSendLog(id int64) (SendLogRow, error) {
|
||||
createdAt, updatedAt string
|
||||
)
|
||||
err := s.db.QueryRow(
|
||||
`SELECT id, queue_id, domain, COALESCE(domain_id, 0), app_login, from_addr, to_addr, subject, status, reason, header_from, refusal_line, created_at, updated_at
|
||||
`SELECT id, queue_id, domain, COALESCE(domain_id, 0), app_login, from_addr, to_addr, subject, status, reason, header_from, refusal_line, recovered, created_at, updated_at
|
||||
FROM send_log WHERE id = ?`, id,
|
||||
).Scan(&row.ID, &row.QueueID, &row.Domain, &row.DomainID, &row.AppLogin, &row.From, &row.To,
|
||||
&row.Subject, &row.Status, &row.Reason, &row.HeaderFrom, &row.RefusalLine, &createdAt, &updatedAt)
|
||||
&row.Subject, &row.Status, &row.Reason, &row.HeaderFrom, &row.RefusalLine, &row.Recovered, &createdAt, &updatedAt)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return SendLogRow{}, ErrSendLogNotFound
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user