One ihasmail in front of several Stalwarts, from #238. STALWART_URL stays required and stays the default, so an installation that sets nothing behaves exactly as it always has -- the mapping only adds domains that go elsewhere. An unlisted domain goes to the default. So does a bare username, which Stalwart accepts and which has no domain to map at all. A listed domain never falls back. If its server is unreachable that sign-in fails rather than retrying against the default, because falling back would authenticate somebody against a server their domain was deliberately routed away from -- and if the same account name existed there, they would land in another tenant's mailbox. The fallback is a decision about unmapped domains, taken before any network call, not a recovery path. Smaller than it sounds because only four places read config.stalwartUrl, all in upstream.ts. The upstream session now records which server issued it, since the relative URLs inside it only mean anything against that server, and every route already holding a session gets the right upstream without a second lookup. The client is untouched: it talks to one proxy and never learns there is more than one server behind it, which is exactly why this is small and several-servers-at-once is not. The upstream is derived from the username rather than stored on the session, so a mapping change takes effect on restart instead of being frozen into sessions that outlive it. Validated at boot the way the settings policy is: malformed JSON, a duplicate domain once normalised, a missing file or a value that is not an http(s) URL all stop the server. Domains are lower-cased and stripped of a trailing dot, because that is how one arrives off a username and comparing them any other way means a mapping that silently never matches. The servers themselves are not contacted -- a mapping is a routing table, not a health check, and one customer's outage must not stop ihasmail starting for the other four. Eight tests on the routing, two on the shipped example, and the four refusals checked by hand against a real config load.
68 lines
2.5 KiB
TypeScript
68 lines
2.5 KiB
TypeScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
process.env.STALWART_URL = "https://default.example";
|
|
|
|
const { upstreamFor } = await import("./upstream.js");
|
|
const { config } = await import("./config.js");
|
|
|
|
/**
|
|
* Which Stalwart a username goes to (#238).
|
|
*
|
|
* `STALWART_URL` is required and is the default. The mapping only adds domains
|
|
* that go elsewhere, so an installation with no mapping behaves exactly as it
|
|
* always has -- which is what these first cases pin.
|
|
*/
|
|
|
|
test("with no mapping at all, everything goes to the default", () => {
|
|
assert.deepEqual(config.stalwartServers, {});
|
|
assert.equal(upstreamFor("[email protected]"), "https://default.example");
|
|
assert.equal(upstreamFor("[email protected]"), "https://default.example");
|
|
});
|
|
|
|
test("a bare username has no domain to map, so it goes to the default", () => {
|
|
// Stalwart accepts a login with no domain at all.
|
|
assert.equal(upstreamFor("demo"), "https://default.example");
|
|
assert.equal(upstreamFor(""), "https://default.example");
|
|
});
|
|
|
|
test("a mapped domain goes to its own server", () => {
|
|
config.stalwartServers["mapped.test"] = "https://mail.mapped.test";
|
|
try {
|
|
assert.equal(upstreamFor("[email protected]"), "https://mail.mapped.test");
|
|
} finally {
|
|
delete config.stalwartServers["mapped.test"];
|
|
}
|
|
});
|
|
|
|
test("an unmapped domain still goes to the default while others are mapped", () => {
|
|
config.stalwartServers["mapped.test"] = "https://mail.mapped.test";
|
|
try {
|
|
assert.equal(upstreamFor("[email protected]"), "https://default.example");
|
|
} finally {
|
|
delete config.stalwartServers["mapped.test"];
|
|
}
|
|
});
|
|
|
|
test("the domain is matched however it was typed", () => {
|
|
// Keys are normalised on load; the username has to be normalised the same
|
|
// way or a mapping silently never matches.
|
|
config.stalwartServers["mapped.test"] = "https://mail.mapped.test";
|
|
try {
|
|
assert.equal(upstreamFor("[email protected]"), "https://mail.mapped.test");
|
|
assert.equal(upstreamFor("[email protected]."), "https://mail.mapped.test", "root dot");
|
|
assert.equal(upstreamFor("someone@ mapped.test "), "https://mail.mapped.test", "stray spaces");
|
|
} finally {
|
|
delete config.stalwartServers["mapped.test"];
|
|
}
|
|
});
|
|
|
|
test("an address with an @ in the local part maps on the last one", () => {
|
|
config.stalwartServers["mapped.test"] = "https://mail.mapped.test";
|
|
try {
|
|
assert.equal(upstreamFor('"odd@name"@mapped.test'), "https://mail.mapped.test");
|
|
} finally {
|
|
delete config.stalwartServers["mapped.test"];
|
|
}
|
|
});
|