Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): scope stored details to their service

Yudhi Armyndharisafccf3b9044d
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.md
index 04cc76cba..6c872e3e6 100644
--- a/docs/06-api-specification.md
+++ b/docs/06-api-specification.md
@@ -5712,7 +5712,7 @@ Merge-save infrastructure config to `data/.env.generated` (a `0600` secret file)
| `engine.sessionDataPath` | string | No | — | Default `./data/sessions` |
| `engine.browserArgs` | string | No | — | Saved as `PUPPETEER_ARGS`. Empty saves the default `--no-sandbox --disable-setuid-sandbox --disable-dev-shm-usage --disable-gpu`; a non-empty value replaces that list in full |
-Built-in flags: a section sent without `builtIn` follows its stored host or endpoint, independently of `POSTGRES_BUILTIN`, `REDIS_BUILTIN` or `MINIO_BUILTIN`. A host that reaches the bundled container applies its connection values again, except when the environment pins that host elsewhere: an omitted choice preserves the stored connection details, including ports, passwords, TLS settings and legacy S3 keys. Other hosts keep their external settings, even beside a flag of `true`. A host pin does not replace the stored choice when `builtIn` is omitted; an explicit `builtIn: true` applies the bundled preset. Send `builtIn: false` with external fields to switch away from a stored bundled host, as the dashboard does once that card is edited. A built-in Redis asks for the `redis` profile only while something the next boot runs uses Redis (`REDIS_ENABLED`, `QUEUE_ENABLED` or `CACHE_ENABLED`, pinned, sent or stored); a save where nothing uses it, in the file either, writes `REDIS_BUILTIN=false` whatever `builtIn` says and drops a `REDIS_HOST` that names the bundled container. A built-in save keeps the stored database password and S3 keys (`S3_ACCESS_KEY_ID`/`S3_SECRET_ACCESS_KEY`, else the legacy `S3_ACCESS_KEY`/`S3_SECRET_KEY`) when the stored host or the next boot's pinned host reaches the bundled container, else the ones the running process dials the bundled container with (a switch away saved without a restart, then back), and writes `openwa` or `minioadmin` otherwise. It keeps the stored user, database name and schema (`DATABASE_USERNAME`, `DATABASE_NAME`, `POSTGRES_SCHEMA`), or the stored `S3_BUCKET` and `S3_REGION`, when the next boot already runs PostgreSQL or S3 storage on a host that reaches the bundled container (a file that names that container beside a database or bucket of its own), else the ones the bundled container serves the running process, and writes `openwa`, `openwa` and `public`, or `openwa` and `us-east-1`, otherwise. The bundled container counts in any spelling: its alias (`postgres`, `redis`, `minio`) or its container name (`openwa-postgres`, `openwa-redis`, `openwa-minio`), in any letter case, with or without a trailing dot, and for storage as the host of the endpoint URL. A built-in database writes `DATABASE_SSL=false` and drops `DATABASE_SSL_REJECT_UNAUTHORIZED`, since the bundled container has no TLS, except beside a `DATABASE_HOST` the environment pins elsewhere: the next boot dials that server, and `sslEnabled` is written as sent. A boot with `POSTGRES_BUILTIN=true`, `DATABASE_SSL=true` and a `DATABASE_HOST` that reaches the bundled container is refused with a message naming `DATABASE_SSL`.
+Built-in flags: a section sent without `builtIn` follows its stored host or endpoint, independently of `POSTGRES_BUILTIN`, `REDIS_BUILTIN` or `MINIO_BUILTIN`. A host that reaches the bundled container applies its connection values again, except when the environment pins that host elsewhere: an omitted choice preserves the stored connection details, including ports, passwords, TLS settings and legacy S3 keys, while the stored selection is still PostgreSQL or S3 (or the section is Redis). Changing from SQLite or local storage applies the new service preset instead of reusing stale connection details. Other hosts keep their external settings, even beside a flag of `true`. A host pin does not replace the stored choice when `builtIn` is omitted; an explicit `builtIn: true` applies the bundled preset. Send `builtIn: false` with external fields to switch away from a stored bundled host, as the dashboard does once that card is edited. A built-in Redis asks for the `redis` profile only while something the next boot runs uses Redis (`REDIS_ENABLED`, `QUEUE_ENABLED` or `CACHE_ENABLED`, pinned, sent or stored); a save where nothing uses it, in the file either, writes `REDIS_BUILTIN=false` whatever `builtIn` says and drops a `REDIS_HOST` that names the bundled container. A built-in save keeps the stored database password and S3 keys (`S3_ACCESS_KEY_ID`/`S3_SECRET_ACCESS_KEY`, else the legacy `S3_ACCESS_KEY`/`S3_SECRET_KEY`) when the stored selection matches the service and either the stored host or the next boot's pinned host reaches the bundled container, else the ones the running process dials the bundled container with (a switch away saved without a restart, then back), and writes `openwa` or `minioadmin` otherwise. It keeps the stored user, database name and schema (`DATABASE_USERNAME`, `DATABASE_NAME`, `POSTGRES_SCHEMA`), or the stored `S3_BUCKET` and `S3_REGION`, when the stored selection and the next boot are both PostgreSQL or S3 storage on a host that reaches the bundled container (a file that names that container beside a database or bucket of its own), else the ones the bundled container serves the running process, and writes `openwa`, `openwa` and `public`, or `openwa` and `us-east-1`, otherwise. The bundled container counts in any spelling: its alias (`postgres`, `redis`, `minio`) or its container name (`openwa-postgres`, `openwa-redis`, `openwa-minio`), in any letter case, with or without a trailing dot, and for storage as the host of the endpoint URL. A built-in database writes `DATABASE_SSL=false` and drops `DATABASE_SSL_REJECT_UNAUTHORIZED`, since the bundled container has no TLS, except beside a `DATABASE_HOST` the environment pins elsewhere: the next boot dials that server, and `sslEnabled` is written as sent. A boot with `POSTGRES_BUILTIN=true`, `DATABASE_SSL=true` and a `DATABASE_HOST` that reaches the bundled container is refused with a message naming `DATABASE_SSL`.
```json
{
diff --git a/src/modules/infra/config-sections.spec.ts b/src/modules/infra/config-sections.spec.ts
new file mode 100644
index 000000000..5f14a00e3
--- /dev/null
+++ b/src/modules/infra/config-sections.spec.ts
@@ -0,0 +1,206 @@
+import { recordOsEnvKeys, recordPinnedEnvKeys } from '../../config/env-precedence';
+import {
+ applyDatabaseSection,
+ applyRedisSection,
+ applyStorageSection,
+ type ConfigSectionContext,
+} from './config-sections';
+
+const cases: Array<readonly [string, string, boolean | undefined, string, boolean]> = [
+ 'database',
+ 'redis',
+ 'storage',
+].flatMap(service =>
+ ['omitted', 'same', 'changed'].flatMap(selection =>
+ [undefined, true, false].flatMap(builtIn =>
+ ['none', 'bundled', 'external'].map(pin => [service, selection, builtIn, pin, false] as const),
+ ),
+ ),
+);
+cases.push(
+ ...['database', 'storage'].flatMap(service =>
+ [undefined, true].flatMap(builtIn =>
+ ['none', 'bundled', 'external'].map(pin => [service, 'changed', builtIn, pin, true] as const),
+ ),
+ ),
+);
+const envKeys = /^(DATABASE_|POSTGRES_|REDIS_|STORAGE_|S3_|MINIO_|QUEUE_|CACHE_)/;
+let previous: Record<string, string | undefined>;
+beforeEach(() => {
+ previous = Object.fromEntries(Object.entries(process.env).filter(([key]) => envKeys.test(key)));
+ for (const key of Object.keys(previous)) delete process.env[key];
+ recordOsEnvKeys({});
+ recordPinnedEnvKeys({});
+});
+afterEach(() => {
+ for (const key of Object.keys(process.env)) if (envKeys.test(key)) delete process.env[key];
+ Object.assign(process.env, previous);
+ recordOsEnvKeys({});
+ recordPinnedEnvKeys({});
+});
+
+it.each(cases)(
+ '%s selection %s with builtIn %s and %s host pin, selection pinned %s',
+ (service, selection, builtIn, pin, pinSelection) => {
+ const changed = selection === 'changed';
+ const ctx: ConfigSectionContext = { updates: {}, staleKeys: new Set(), profiles: [] };
+ const existing: Record<string, string> =
+ service === 'database'
+ ? {
+ DATABASE_TYPE: changed ? 'sqlite' : 'postgres',
+ POSTGRES_BUILTIN: 'true',
+ DATABASE_HOST: 'openwa-postgres',
+ DATABASE_PORT: '6432',
+ DATABASE_USERNAME: 'app',
+ DATABASE_PASSWORD: 'StoredPassword123456',
+ DATABASE_NAME: changed ? './data/openwa.sqlite' : 'appdb',
+ POSTGRES_SCHEMA: 'tenant',
+ DATABASE_SSL: 'false',
+ }
+ : service === 'redis'
+ ? {
+ REDIS_ENABLED: changed ? 'false' : 'true',
+ REDIS_BUILTIN: 'true',
+ REDIS_HOST: 'openwa-redis',
+ REDIS_PORT: '6380',
+ REDIS_PASSWORD: 'StoredRedis123456',
+ }
+ : {
+ STORAGE_TYPE: changed ? 'local' : 's3',
+ MINIO_BUILTIN: 'true',
+ S3_ENDPOINT: 'http://openwa-minio:9000',
+ S3_ACCESS_KEY: 'StoredKey123456',
+ S3_SECRET_KEY: 'StoredSecret1234567890',
+ S3_BUCKET: 'media',
+ S3_REGION: 'eu-west-1',
+ };
+ const hostKey = service === 'database' ? 'DATABASE_HOST' : service === 'redis' ? 'REDIS_HOST' : 'S3_ENDPOINT';
+ if (pin !== 'none') {
+ const value =
+ pin === 'bundled'
+ ? existing[hostKey]
+ : service === 'storage'
+ ? 'https://storage.external.example'
+ : 'external.example';
+ process.env[hostKey] = value;
+ recordPinnedEnvKeys({ [hostKey]: value });
+ }
+ if (pinSelection) {
+ const key = service === 'database' ? 'DATABASE_TYPE' : 'STORAGE_TYPE';
+ process.env[key] = service === 'database' ? 'postgres' : 's3';
+ recordPinnedEnvKeys({
+ ...Object.fromEntries(Object.entries(process.env).filter(([name]) => envKeys.test(name))),
+ [key]: process.env[key],
+ });
+ }
+ const expected = { ...existing };
+ const flagKey =
+ service === 'database' ? 'POSTGRES_BUILTIN' : service === 'redis' ? 'REDIS_BUILTIN' : 'MINIO_BUILTIN';
+ if (builtIn !== undefined) expected[flagKey] = String(builtIn);
+ if (selection !== 'omitted')
+ expected[service === 'database' ? 'DATABASE_TYPE' : service === 'redis' ? 'REDIS_ENABLED' : 'STORAGE_TYPE'] =
+ service === 'database' ? 'postgres' : service === 'redis' ? 'true' : 's3';
+
+ if (service === 'database') {
+ const fields =
+ builtIn === false
+ ? {
+ host: 'new.external.example',
+ port: '6433',
+ username: 'newuser',
+ password: 'NewPassword123456',
+ database: 'newdb',
+ schema: 'new_schema',
+ }
+ : {};
+ applyDatabaseSection(
+ {
+ ...(selection === 'omitted' ? {} : { type: 'postgres' }),
+ ...(builtIn === undefined ? {} : { builtIn }),
+ ...fields,
+ },
+ existing,
+ ctx,
+ );
+ if (builtIn === false)
+ Object.assign(expected, {
+ DATABASE_HOST: 'new.external.example',
+ DATABASE_PORT: '6433',
+ DATABASE_USERNAME: 'newuser',
+ DATABASE_PASSWORD: 'NewPassword123456',
+ DATABASE_NAME: 'newdb',
+ POSTGRES_SCHEMA: 'new_schema',
+ });
+ else if (!(builtIn === undefined && pin === 'external' && !changed))
+ Object.assign(expected, {
+ DATABASE_HOST: 'postgres',
+ DATABASE_PORT: '5432',
+ DATABASE_USERNAME: !changed && pin !== 'external' ? 'app' : 'openwa',
+ DATABASE_PASSWORD: changed ? 'openwa' : existing.DATABASE_PASSWORD,
+ DATABASE_NAME: !changed && pin !== 'external' ? 'appdb' : 'openwa',
+ POSTGRES_SCHEMA: !changed && pin !== 'external' ? 'tenant' : 'public',
+ });
+ } else if (service === 'redis') {
+ const fields =
+ builtIn === false ? { host: 'new.external.example', port: '6381', password: 'NewRedis123456' } : {};
+ applyRedisSection(
+ {
+ ...(selection === 'omitted' ? {} : { enabled: true }),
+ ...(builtIn === undefined ? {} : { builtIn }),
+ ...fields,
+ },
+ existing,
+ ctx,
+ );
+ if (builtIn === false)
+ Object.assign(expected, {
+ REDIS_HOST: 'new.external.example',
+ REDIS_PORT: '6381',
+ REDIS_PASSWORD: 'NewRedis123456',
+ });
+ else if (!(builtIn === undefined && pin === 'external')) {
+ Object.assign(expected, { REDIS_HOST: 'redis', REDIS_PORT: '6379' });
+ delete expected.REDIS_PASSWORD;
+ }
+ } else {
+ const fields =
+ builtIn === false
+ ? {
+ s3Endpoint: 'https://new.external.example',
+ s3Bucket: 'new-bucket',
+ s3Region: 'new-region',
+ s3AccessKey: 'NewKey123456',
+ s3SecretKey: 'NewSecret1234567890',
+ }
+ : {};
+ applyStorageSection(
+ {
+ ...(selection === 'omitted' ? {} : { type: 's3' }),
+ ...(builtIn === undefined ? {} : { builtIn }),
+ ...fields,
+ },
+ existing,
+ ctx,
+ );
+ if (builtIn === false)
+ Object.assign(expected, {
+ S3_ENDPOINT: 'https://new.external.example',
+ S3_BUCKET: 'new-bucket',
+ S3_REGION: 'new-region',
+ S3_ACCESS_KEY_ID: 'NewKey123456',
+ S3_SECRET_ACCESS_KEY: 'NewSecret1234567890',
+ });
+ else if (!(builtIn === undefined && pin === 'external' && !changed))
+ Object.assign(expected, {
+ S3_ENDPOINT: 'http://minio:9000',
+ S3_BUCKET: !changed && pin !== 'external' ? 'media' : 'openwa',
+ S3_REGION: !changed && pin !== 'external' ? 'eu-west-1' : 'us-east-1',
+ S3_ACCESS_KEY_ID: changed ? 'minioadmin' : existing.S3_ACCESS_KEY,
+ S3_SECRET_ACCESS_KEY: changed ? 'minioadmin' : existing.S3_SECRET_KEY,
+ });
+ }
+ const merged = { ...existing, ...ctx.updates };
+ for (const key of ctx.staleKeys) delete merged[key];
+ expect(merged).toEqual(expected);
+ },
+);
diff --git a/src/modules/infra/config-sections.ts b/src/modules/infra/config-sections.ts
index b026e80ec..d03a39143 100644
--- a/src/modules/infra/config-sections.ts
+++ b/src/modules/infra/config-sections.ts
@@ -77,10 +77,13 @@ export function applyDatabaseSection(
}
// An explicit choice applies the bundled preset; otherwise the stored host decides the mode. A pinned host must
// not replace that stored choice, which takes effect again when the pin is removed. An omitted choice beside an
- // external host pin preserves its connection details instead of applying the bundled preset again.
+ // external host pin preserves the same stored service's connection details instead of applying its preset again.
const dbBuiltIn = database.builtIn ?? namesBundledContainer('postgres', existing.DATABASE_HOST);
if (type === 'postgres') {
- if (dbBuiltIn && (database.builtIn !== undefined || !hostPinnedElsewhere('postgres'))) {
+ if (
+ dbBuiltIn &&
+ (database.builtIn !== undefined || existing.DATABASE_TYPE !== 'postgres' || !hostPinnedElsewhere('postgres'))
+ ) {
// Built-in PostgreSQL - use container name as host
updates.DATABASE_HOST = 'postgres';
updates.DATABASE_PORT = '5432';
@@ -90,17 +93,19 @@ export function applyDatabaseSection(
// running process, whose environment the boot loaded and a save leaves as it is: switched away and back before a
// restart, the file holds the other server's values. From anywhere else they belong to another server.
const servesStored =
+ existing.DATABASE_TYPE === 'postgres' &&
nextBootValue('DATABASE_TYPE', existing) === 'postgres' &&
namesBundledContainer('postgres', nextBootValue('DATABASE_HOST', existing));
const servesRunning =
process.env.DATABASE_TYPE === 'postgres' && namesBundledContainer('postgres', process.env.DATABASE_HOST);
const kept: Partial<Record<string, string>> = servesStored ? existing : servesRunning ? process.env : {};
updates.DATABASE_USERNAME = kept.DATABASE_USERNAME || 'openwa';
- // Secrets are not echoed to the form. A blank password keeps one belonging to the bundled host (stored, pinned,
+ // Secrets are not echoed to the form. A blank password keeps one stored for PostgreSQL at the bundled host (stored, pinned,
// or still serving the running process after a saved switch away); external credentials do not follow it.
const storedPassword =
- namesBundledContainer('postgres', existing.DATABASE_HOST) ||
- namesBundledContainer('postgres', nextBootValue('DATABASE_HOST', existing))
+ existing.DATABASE_TYPE === 'postgres' &&
+ (namesBundledContainer('postgres', existing.DATABASE_HOST) ||
+ namesBundledContainer('postgres', nextBootValue('DATABASE_HOST', existing)))
? existing.DATABASE_PASSWORD
: servesRunning
? process.env.DATABASE_PASSWORD
@@ -245,13 +250,17 @@ export function applyStorageSection(
staleKeys.add('STORAGE_LOCAL_PATH');
// An absent builtIn follows the stored endpoint, as for the database and Redis.
const storageBuiltIn = storage.builtIn ?? namesBundledContainer('minio', existing.S3_ENDPOINT);
- if (storageBuiltIn && (storage.builtIn !== undefined || !hostPinnedElsewhere('minio'))) {
+ if (
+ storageBuiltIn &&
+ (storage.builtIn !== undefined || existing.STORAGE_TYPE !== 's3' || !hostPinnedElsewhere('minio'))
+ ) {
// Built-in MinIO - use container name as endpoint. The bundled keys are only the DEFAULT, as for the database
- // password: keys at the bundled endpoint (pinned or stored, in any spelling) are that container's, and are kept; else
+ // password: keys stored for S3 at the bundled endpoint (pinned or stored, in any spelling) are kept; else
// the ones the running process dials it with, as for the database.
const keepKeys =
- namesBundledContainer('minio', existing.S3_ENDPOINT) ||
- namesBundledContainer('minio', nextBootValue('S3_ENDPOINT', existing));
+ existing.STORAGE_TYPE === 's3' &&
+ (namesBundledContainer('minio', existing.S3_ENDPOINT) ||
+ namesBundledContainer('minio', nextBootValue('S3_ENDPOINT', existing)));
const servesRunning =
process.env.STORAGE_TYPE === 's3' && namesBundledContainer('minio', process.env.S3_ENDPOINT);
const keys: Partial<Record<string, string>> = keepKeys ? existing : servesRunning ? process.env : {};
@@ -261,6 +270,7 @@ export function applyStorageSection(
// As for the database: a bucket the next boot already reaches on the bundled MinIO, else the one it serves the
// running process, is kept with its region.
const servesStored =
+ existing.STORAGE_TYPE === 's3' &&
nextBootValue('STORAGE_TYPE', existing) === 's3' &&
namesBundledContainer('minio', nextBootValue('S3_ENDPOINT', existing));
const kept: Partial<Record<string, string>> = servesStored ? existing : servesRunning ? process.env : {};