Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): inherit partial modes from stored hosts

Yudhi Armyndharisc79dade58ab5
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.md
index 902b2d10c..37ad5f72a 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` stays in the stored mode. A stored `POSTGRES_BUILTIN`, `REDIS_BUILTIN` or `MINIO_BUILTIN` of `true` writes the bundled connection values again, and the fields sent apply only to an external service; send `builtIn: false` with them to switch it, as the dashboard does once a card beside a host the environment pins elsewhere 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 flag was already `true` or the next boot's host already 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; 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. 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 an omitted `builtIn` preserves a stored bundled host or 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`.
```json
{
diff --git a/src/modules/infra/bundled-host-decisions.spec.ts b/src/modules/infra/bundled-host-decisions.spec.ts
index 11c80e0c6..6ef17b014 100644
--- a/src/modules/infra/bundled-host-decisions.spec.ts
+++ b/src/modules/infra/bundled-host-decisions.spec.ts
@@ -194,7 +194,7 @@ describe.each(cases.map(c => [label(c), c] as const))('%s', (_label, c) => {
? { database: { type: 'postgres', builtIn: true } }
: { storage: { type: 's3', builtIn: true } },
);
- const keep = c.flag === 'true' || reached;
+ const keep = c.position === 'pinned' || reached;
if (c.service === 'database') expect(env.DATABASE_PASSWORD).toBe(keep ? 'StoredPw789!' : 'openwa');
else
expect([env.S3_ACCESS_KEY_ID, env.S3_SECRET_ACCESS_KEY]).toEqual(
@@ -230,6 +230,7 @@ describe.each(cases.map(c => [label(c), c] as const))('%s', (_label, c) => {
const env = saved(section);
expect(env[spec.flag]).toBe(c.flag === 'absent' ? undefined : c.flag);
const sentHost = c.service === 'storage' ? c.host || undefined : c.host.trim() || 'localhost';
- expect(env[spec.host]).toBe(c.flag === 'true' ? spec.bundledHost : sentHost);
+ const storedBundled = c.position === 'pinned' ? !c.reach : c.reach;
+ expect(env[spec.host]).toBe(storedBundled ? spec.bundledHost : sentHost);
});
});
diff --git a/src/modules/infra/config-sections.ts b/src/modules/infra/config-sections.ts
index df96c1447..9241e00df 100644
--- a/src/modules/infra/config-sections.ts
+++ b/src/modules/infra/config-sections.ts
@@ -75,10 +75,9 @@ export function applyDatabaseSection(
if (database.builtIn !== undefined) {
updates.POSTGRES_BUILTIN = database.builtIn ? 'true' : 'false';
}
- // The effective mode: an explicit builtIn wins; when it is absent the saved mode
- // is inherited so a partial payload stays in the current mode instead of silently
- // flipping to external.
- const dbBuiltIn = database.builtIn ?? existing.POSTGRES_BUILTIN === 'true';
+ // 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.
+ const dbBuiltIn = database.builtIn ?? namesBundledContainer('postgres', existing.DATABASE_HOST);
if (type === 'postgres') {
if (dbBuiltIn) {
// Built-in PostgreSQL - use container name as host
@@ -90,24 +89,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 =
- nextBootValue('DATABASE_TYPE', existing) === 'postgres' &&
- namesBundledContainer('postgres', nextBootValue('DATABASE_HOST', existing));
+ (database.builtIn === undefined &&
+ existing.DATABASE_TYPE === 'postgres' &&
+ namesBundledContainer('postgres', existing.DATABASE_HOST)) ||
+ (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';
- // The bundled credential is only the DEFAULT. Secrets are never echoed back to the form,
- // so an absent password field means "unchanged", not "reset to 'openwa'" — and the
- // dashboard ALWAYS sends builtIn, so keying the reset on "explicit builtIn:true" reset a
- // re-keyed container on every save from the Infrastructure page.
- // What decides it is whether the stored password belongs to this same bundled container:
- // it does when the previous mode was already built-in, or when the next boot already dials the
- // bundled host with it (the environment's host, else the file's, a file that names it without the
- // flag included, in any spelling that reaches the container). Coming from external, the stored value is the
- // external DB's credential and must not be carried into the container; the one the running process dials the
- // bundled container with is that container's own.
+ // Secrets are not echoed to the form. A blank password keeps one belonging to the bundled host (stored, pinned,
+ // or still serving the running process after a saved switch away); external credentials do not follow it.
const storedPassword =
- existing.POSTGRES_BUILTIN === 'true' ||
+ namesBundledContainer('postgres', existing.DATABASE_HOST) ||
namesBundledContainer('postgres', nextBootValue('DATABASE_HOST', existing))
? existing.DATABASE_PASSWORD
: servesRunning
@@ -115,9 +109,7 @@ export function applyDatabaseSection(
: undefined;
updates.DATABASE_PASSWORD = database.password || storedPassword || 'openwa';
updates.DATABASE_NAME = kept.DATABASE_NAME || 'openwa';
- // Built-in Postgres is initialized with the default 'public' schema (see
- // scripts/postgres-init-schema.sh). Pin it so a later switch from a custom-schema
- // external DB to built-in doesn't carry a stale POSTGRES_SCHEMA forward.
+ // A newly initialized bundled database starts in public; an existing one keeps its schema.
updates.POSTGRES_SCHEMA = kept.POSTGRES_SCHEMA || 'public';
profiles.push('postgres');
} else {
@@ -194,9 +186,8 @@ export function applyRedisSection(
const { updates, staleKeys, profiles } = ctx;
if (redis.enabled !== undefined) updates.REDIS_ENABLED = redis.enabled ? 'true' : 'false';
if (redis.builtIn !== undefined) updates.REDIS_BUILTIN = redis.builtIn ? 'true' : 'false';
- // An absent builtIn keeps the stored mode, as for the database: a stored built-in Redis keeps its bundled host
- // rather than taking the fields sent (the page leaves the flag out beside a pinned host, and sends that host).
- const redisBuiltIn = redis.builtIn ?? existing.REDIS_BUILTIN === 'true';
+ // As for the database, an omitted choice follows the stored host, independently of the flag that starts a container.
+ const redisBuiltIn = redis.builtIn ?? namesBundledContainer('redis', existing.REDIS_HOST);
if (redisBuiltIn) {
// Built-in Redis - use container name as host. The bundled container runs without
// auth, so a password saved by an earlier external setup is stale: leaving it
@@ -254,14 +245,14 @@ export function applyStorageSection(
}
} else {
staleKeys.add('STORAGE_LOCAL_PATH');
- // An absent builtIn keeps the stored mode, as for the database and Redis.
- if (storage.builtIn ?? existing.MINIO_BUILTIN === 'true') {
+ // An absent builtIn follows the stored endpoint, as for the database and Redis.
+ if (storage.builtIn ?? namesBundledContainer('minio', existing.S3_ENDPOINT)) {
// Built-in MinIO - use container name as endpoint. The bundled keys are only the DEFAULT, as for the database
- // password: keys the next boot already dials the bundled endpoint with (the flag already set, or the bundled
- // endpoint pinned or stored, in any spelling) are the ones that container was created with, and are kept; else
+ // password: keys at the bundled endpoint (pinned or stored, in any spelling) are that container's, and are kept; else
// the ones the running process dials it with, as for the database.
const keepKeys =
- existing.MINIO_BUILTIN === 'true' || namesBundledContainer('minio', nextBootValue('S3_ENDPOINT', existing));
+ 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 : {};
@@ -271,8 +262,11 @@ 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 =
- nextBootValue('STORAGE_TYPE', existing) === 's3' &&
- namesBundledContainer('minio', nextBootValue('S3_ENDPOINT', existing));
+ (storage.builtIn === undefined &&
+ existing.STORAGE_TYPE === 's3' &&
+ namesBundledContainer('minio', existing.S3_ENDPOINT)) ||
+ (nextBootValue('STORAGE_TYPE', existing) === 's3' &&
+ namesBundledContainer('minio', nextBootValue('S3_ENDPOINT', existing)));
const kept: Partial<Record<string, string>> = servesStored ? existing : servesRunning ? process.env : {};
updates.S3_BUCKET = kept.S3_BUCKET || 'openwa';
updates.S3_REGION = kept.S3_REGION || 'us-east-1';
diff --git a/src/modules/infra/infra-config.controller.spec.ts b/src/modules/infra/infra-config.controller.spec.ts
index 0bcc284c3..ffb4b2918 100644
--- a/src/modules/infra/infra-config.controller.spec.ts
+++ b/src/modules/infra/infra-config.controller.spec.ts
@@ -868,6 +868,40 @@ describe('InfraConfigController.saveConfig per-key merge (partial payloads)', ()
expect(env).toContain('DATABASE_PASSWORD=keepme');
});
+ it('an omitted builtIn follows the stored host beside stale true flags', () => {
+ const existing =
+ 'DATABASE_TYPE=postgres\nPOSTGRES_BUILTIN=true\nDATABASE_HOST=db.ext.example\nDATABASE_USERNAME=app\n' +
+ 'DATABASE_PASSWORD=ExtPw12345!\nDATABASE_NAME=appdb\nPOSTGRES_SCHEMA=tenant\n' +
+ 'REDIS_ENABLED=true\nREDIS_BUILTIN=true\nREDIS_HOST=cache.ext.example\nREDIS_PORT=6380\nREDIS_PASSWORD=RedisPw123456\n' +
+ 'STORAGE_TYPE=s3\nMINIO_BUILTIN=true\nS3_ENDPOINT=https://s3.ext.example\nS3_BUCKET=media\nS3_REGION=eu-west-1\n' +
+ 'S3_ACCESS_KEY_ID=ExternalKey12345\nS3_SECRET_ACCESS_KEY=ExternalSecret1234567890\n';
+ const dotenv = jest.requireActual<typeof import('dotenv')>('dotenv');
+
+ expect(
+ dotenv.parse(
+ written({ database: { poolSize: 25 }, redis: { port: '6381' }, storage: { s3Region: 'eu-west-2' } }, existing),
+ ),
+ ).toEqual({
+ ...dotenv.parse(existing),
+ DATABASE_POOL_SIZE: '25',
+ REDIS_PORT: '6381',
+ S3_REGION: 'eu-west-2',
+ });
+ });
+
+ it('a true flag beside an external host does not give its credentials to a newly chosen bundled container', () => {
+ const env = written(
+ { database: { type: 'postgres', builtIn: true }, storage: { type: 's3', builtIn: true } },
+ 'DATABASE_TYPE=postgres\nPOSTGRES_BUILTIN=true\nDATABASE_HOST=db.ext.example\nDATABASE_PASSWORD=ExtPw12345!\n' +
+ 'STORAGE_TYPE=s3\nMINIO_BUILTIN=true\nS3_ENDPOINT=https://s3.ext.example\nS3_ACCESS_KEY_ID=ExternalKey12345\n' +
+ 'S3_SECRET_ACCESS_KEY=ExternalSecret1234567890\n',
+ );
+
+ expect(env).toContain('DATABASE_PASSWORD=openwa');
+ expect(env).toContain('S3_ACCESS_KEY_ID=minioadmin');
+ expect(env).toContain('S3_SECRET_ACCESS_KEY=minioadmin');
+ });
+
it('a null pool size reads as absent and keeps the stored one', () => {
const existing =
'DATABASE_TYPE=postgres\nPOSTGRES_BUILTIN=false\nDATABASE_HOST=old.example.com\nDATABASE_PASSWORD=keepme\nDATABASE_POOL_SIZE=25\n';
@@ -1059,6 +1093,25 @@ describe('InfraConfigController.saveConfig per-key merge (partial payloads)', ()
}
});
+ it('keeps the details of a stored bundled host without a true flag while another host is pinned', () => {
+ pin({ DATABASE_HOST: 'db.ext.example', S3_ENDPOINT: 'https://s3.ext.example' });
+ const stored = STORED_EXTERNAL.replace('db.example.com', 'openwa-postgres').replace(
+ 'https://s3.example.com',
+ 'http://openwa-minio:9000',
+ );
+ const dotenv = jest.requireActual<typeof import('dotenv')>('dotenv');
+
+ expect(dotenv.parse(written({ database: { poolSize: 25 }, storage: {} }, stored))).toEqual({
+ ...dotenv.parse(stored),
+ DATABASE_HOST: 'postgres',
+ DATABASE_PORT: '5432',
+ DATABASE_POOL_SIZE: '25',
+ POSTGRES_SCHEMA: 'public',
+ S3_ENDPOINT: 'http://minio:9000',
+ S3_REGION: 'us-east-1',
+ });
+ });
+
// The page leaves out an untouched flag beside a pinned host. The stored external flag is inherited then, so the
// credentials the pinned host may need stay as stored, where a flag of true would write the bundled ones.
it('keeps stored external credentials when the flag is left out beside a pinned bundled host', () => {
@@ -1516,8 +1569,8 @@ describe('InfraConfigController.saveConfig built-in/external mode flips and the
expect(env).toContain('DATABASE_PASSWORD=CustomPw123!');
});
- // The pinned password keeps that boot past the default-secret guard; the file's is what the rule decides.
- it('still resets it when the environment pins another host over the bundled host in the file', () => {
+ // Removing the host pin restores the stored bundled host, so its own password must remain usable then.
+ it('keeps it when the environment pins another host over the bundled host in the file', () => {
process.env.DATABASE_HOST = 'db.example.com';
process.env.DATABASE_PASSWORD = 'PinnedPw456!';
recordPinnedEnvKeys({ DATABASE_HOST: 'db.example.com', DATABASE_PASSWORD: 'PinnedPw456!' });
@@ -1525,8 +1578,7 @@ describe('InfraConfigController.saveConfig built-in/external mode flips and the
{ database: { type: 'postgres', builtIn: true } },
'DATABASE_TYPE=postgres\nPOSTGRES_BUILTIN=false\nDATABASE_HOST=postgres\nDATABASE_PASSWORD=ExternalPw!\n',
);
- expect(env).toContain('DATABASE_PASSWORD=openwa');
- expect(env).not.toContain('ExternalPw!');
+ expect(env).toContain('DATABASE_PASSWORD=ExternalPw!');
});
// Any spelling that reaches the bundled container is the bundled host, as for the restart's stop and start rules.