Phase 4: applications + SASL (sasldb2) + sender_login_maps

Adds application accounts bound to domains: a SASL login/password in
sasldb2, a per-application address mode (wildcard @domain or an explicit
list), and matching smtpd_sender_login_maps bindings — with create,
list, edit-mode, delete and password regeneration (spec 4.1, 5.1,
7.2.5-9). Generated passwords are shown exactly once and never stored in
plaintext (7.6.1).

- internal/store/applications.go: transactional CRUD; globally unique
  login; ListBindings (address->login) as the map source; logins-by-
  domain for pre-cascade SASL cleanup.
- internal/app: saslpasswd2 wrapper (password via stdin, login as a
  whitelisted argv element, no shell — 7.6.3); strong base64url password;
  address validation that enforces domain ownership before any config
  write (7.6.2); service orchestrating store + sasldb2 + map with full
  rollback on partial failure.
- internal/postfix: sender_login_maps regenerated as a pure function of
  the registry (many-to-one logins merged per address), atomic write,
  injection backstop (7.6.4).
- Postfix reload, corrected: `postfix start-fg` forks a separate master,
  so signalling the supervised process never reaches it. Reload now runs
  the canonical `postfix reload` via a one-shot supervisord program the
  unprivileged panel triggers over the group control socket. Verified in
  mail.log.
- domain.Service.Delete purges the domain's SASL accounts, then cascades,
  then rebuilds the sender map and reloads; manual reload now covers both
  OpenDKIM and Postfix.
- web: application management in the domain page, one-time credential
  shown inline; postfix joins the selfpost group and entrypoint normalises
  /data/sasl and /data/postfix (setgid, group-readable) with self-heal.

Verified on the dev server: gofmt/vet/test green, image builds, and a
container e2e covers the full application lifecycle, domain-delete
cascade, restart persistence, and a real postfix reload.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-12 21:18:10 +03:00
parent a7a5ad3f91
commit c6eeb30258
24 changed files with 2156 additions and 51 deletions
+189
View File
@@ -0,0 +1,189 @@
package store
import (
"errors"
"testing"
)
func addTestDomain(t *testing.T, st *Store, name string) Domain {
t.Helper()
d, err := st.AddDomain(name, "selfpost")
if err != nil {
t.Fatalf("AddDomain(%q): %v", name, err)
}
return d
}
func TestAddApplicationWildcard(t *testing.T) {
st := openTestStore(t)
d := addTestDomain(t, st, "example.com")
a, err := st.AddApplication(d.ID, "alerts", AddressModeWildcard, nil)
if err != nil {
t.Fatalf("AddApplication: %v", err)
}
if a.ID == 0 || a.Login != "alerts" || a.AddressMode != AddressModeWildcard {
t.Fatalf("unexpected application: %+v", a)
}
if len(a.Addresses) != 0 {
t.Errorf("wildcard app should have no addresses, got %v", a.Addresses)
}
got, err := st.GetApplication(a.ID)
if err != nil {
t.Fatalf("GetApplication: %v", err)
}
if got.Login != "alerts" || len(got.Addresses) != 0 {
t.Fatalf("round-trip mismatch: %+v", got)
}
}
func TestAddApplicationListStoresAddresses(t *testing.T) {
st := openTestStore(t)
d := addTestDomain(t, st, "example.com")
addrs := []string{"noreply@example.com", "alerts@example.com"}
a, err := st.AddApplication(d.ID, "app1", AddressModeList, addrs)
if err != nil {
t.Fatalf("AddApplication: %v", err)
}
got, err := st.GetApplication(a.ID)
if err != nil {
t.Fatal(err)
}
// Addresses come back sorted.
if len(got.Addresses) != 2 || got.Addresses[0] != "alerts@example.com" || got.Addresses[1] != "noreply@example.com" {
t.Fatalf("addresses = %v", got.Addresses)
}
}
func TestAddApplicationDuplicateLogin(t *testing.T) {
st := openTestStore(t)
d := addTestDomain(t, st, "example.com")
d2 := addTestDomain(t, st, "other.com")
if _, err := st.AddApplication(d.ID, "shared", AddressModeWildcard, nil); err != nil {
t.Fatal(err)
}
// Same login under a different domain must still collide (global uniqueness).
_, err := st.AddApplication(d2.ID, "shared", AddressModeWildcard, nil)
if !errors.Is(err, ErrLoginExists) {
t.Fatalf("duplicate login error = %v, want ErrLoginExists", err)
}
}
func TestUpdateApplicationMode(t *testing.T) {
st := openTestStore(t)
d := addTestDomain(t, st, "example.com")
a, err := st.AddApplication(d.ID, "app1", AddressModeList, []string{"a@example.com"})
if err != nil {
t.Fatal(err)
}
// list -> wildcard drops the addresses.
if err := st.UpdateApplicationMode(a.ID, AddressModeWildcard, nil); err != nil {
t.Fatalf("UpdateApplicationMode: %v", err)
}
got, _ := st.GetApplication(a.ID)
if got.AddressMode != AddressModeWildcard || len(got.Addresses) != 0 {
t.Fatalf("after wildcard switch: %+v", got)
}
// wildcard -> list adds a fresh set.
if err := st.UpdateApplicationMode(a.ID, AddressModeList, []string{"b@example.com", "c@example.com"}); err != nil {
t.Fatal(err)
}
got, _ = st.GetApplication(a.ID)
if got.AddressMode != AddressModeList || len(got.Addresses) != 2 {
t.Fatalf("after list switch: %+v", got)
}
}
func TestUpdateApplicationModeNotFound(t *testing.T) {
st := openTestStore(t)
if err := st.UpdateApplicationMode(999, AddressModeWildcard, nil); !errors.Is(err, ErrApplicationNotFound) {
t.Fatalf("UpdateApplicationMode(missing) = %v, want ErrApplicationNotFound", err)
}
}
func TestDeleteApplication(t *testing.T) {
st := openTestStore(t)
d := addTestDomain(t, st, "example.com")
a, err := st.AddApplication(d.ID, "app1", AddressModeList, []string{"a@example.com"})
if err != nil {
t.Fatal(err)
}
deleted, err := st.DeleteApplication(a.ID)
if err != nil {
t.Fatalf("DeleteApplication: %v", err)
}
if deleted.Login != "app1" {
t.Errorf("deleted login = %q, want app1", deleted.Login)
}
assertCount(t, st, "applications", 0)
assertCount(t, st, "application_addresses", 0)
if _, err := st.DeleteApplication(a.ID); !errors.Is(err, ErrApplicationNotFound) {
t.Fatalf("second delete = %v, want ErrApplicationNotFound", err)
}
}
func TestListBindingsMixedModes(t *testing.T) {
st := openTestStore(t)
d1 := addTestDomain(t, st, "example.com")
d2 := addTestDomain(t, st, "other.com")
if _, err := st.AddApplication(d1.ID, "wild", AddressModeWildcard, nil); err != nil {
t.Fatal(err)
}
if _, err := st.AddApplication(d1.ID, "listed", AddressModeList,
[]string{"alerts@example.com", "noreply@example.com"}); err != nil {
t.Fatal(err)
}
if _, err := st.AddApplication(d2.ID, "wild2", AddressModeWildcard, nil); err != nil {
t.Fatal(err)
}
bindings, err := st.ListBindings()
if err != nil {
t.Fatalf("ListBindings: %v", err)
}
want := []Binding{
{"@example.com", "wild"},
{"@other.com", "wild2"},
{"alerts@example.com", "listed"},
{"noreply@example.com", "listed"},
}
if len(bindings) != len(want) {
t.Fatalf("bindings = %+v, want %+v", bindings, want)
}
for i := range want {
if bindings[i] != want[i] {
t.Errorf("binding[%d] = %+v, want %+v", i, bindings[i], want[i])
}
}
}
func TestListLoginsByDomain(t *testing.T) {
st := openTestStore(t)
d := addTestDomain(t, st, "example.com")
other := addTestDomain(t, st, "other.com")
if _, err := st.AddApplication(d.ID, "a", AddressModeWildcard, nil); err != nil {
t.Fatal(err)
}
if _, err := st.AddApplication(d.ID, "b", AddressModeWildcard, nil); err != nil {
t.Fatal(err)
}
if _, err := st.AddApplication(other.ID, "c", AddressModeWildcard, nil); err != nil {
t.Fatal(err)
}
logins, err := st.ListLoginsByDomain(d.ID)
if err != nil {
t.Fatal(err)
}
if len(logins) != 2 || logins[0] != "a" || logins[1] != "b" {
t.Fatalf("logins = %v, want [a b]", logins)
}
}