Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): keep details behind pinned hosts

Yudhi Armyndharisf467b43e1501
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.md
index 37ad5f72a..04cc76cba 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; 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`.
+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`.
```json
{
diff --git a/src/modules/infra/config-sections.ts b/src/modules/infra/config-sections.ts
index 9241e00df..b026e80ec 100644
--- a/src/modules/infra/config-sections.ts
+++ b/src/modules/infra/config-sections.ts
@@ -76,10 +76,11 @@ export function applyDatabaseSection(
updates.POSTGRES_BUILTIN = database.builtIn ? 'true' : 'false';
}
// 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.
+ // 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.
const dbBuiltIn = database.builtIn ?? namesBundledContainer('postgres', existing.DATABASE_HOST);
if (type === 'postgres') {
- if (dbBuiltIn) {
+ if (dbBuiltIn && (database.builtIn !== undefined || !hostPinnedElsewhere('postgres'))) {
// Built-in PostgreSQL - use container name as host
updates.DATABASE_HOST = 'postgres';
updates.DATABASE_PORT = '5432';
@@ -89,11 +90,8 @@ 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 =
- (database.builtIn === undefined &&
- existing.DATABASE_TYPE === 'postgres' &&
- namesBundledContainer('postgres', existing.DATABASE_HOST)) ||
- (nextBootValue('DATABASE_TYPE', existing) === 'postgres' &&
- namesBundledContainer('postgres', nextBootValue('DATABASE_HOST', existing)));
+ 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 : {};
@@ -111,8 +109,7 @@ export function applyDatabaseSection(
updates.DATABASE_NAME = kept.DATABASE_NAME || 'openwa';
// 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 {
+ } else if (!dbBuiltIn) {
// External PostgreSQL. Flipping built-in -> external must not carry the bundled
// 'openwa' password into the external config: the production boot guard rejects
// it, so the next boot would crash-loop. A password in the same payload wins.
@@ -135,6 +132,7 @@ export function applyDatabaseSection(
updates.POSTGRES_SCHEMA = database.schema || 'public';
}
}
+ if (dbBuiltIn) profiles.push('postgres');
// null is what JSON makes of an emptied number input, and reads as absent like the other fields.
if (database.poolSize !== undefined && database.poolSize !== null) {
if (!Number.isInteger(database.poolSize) || database.poolSize < 0) {
@@ -188,14 +186,14 @@ export function applyRedisSection(
if (redis.builtIn !== undefined) updates.REDIS_BUILTIN = redis.builtIn ? 'true' : 'false';
// 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) {
+ if (redisBuiltIn && (redis.builtIn !== undefined || !hostPinnedElsewhere('redis'))) {
// 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
// would make the client AUTH against a passwordless server on the next boot.
updates.REDIS_HOST = 'redis';
updates.REDIS_PORT = '6379';
if (!redis.password) staleKeys.add('REDIS_PASSWORD');
- } else {
+ } else if (!redisBuiltIn) {
// External Redis (explicit, or inherited when builtIn is absent)
if (redis.host !== undefined) updates.REDIS_HOST = redis.host.trim() || 'localhost';
if (redis.port !== undefined) {
@@ -246,7 +244,8 @@ export function applyStorageSection(
} else {
staleKeys.add('STORAGE_LOCAL_PATH');
// An absent builtIn follows the stored endpoint, as for the database and Redis.
- if (storage.builtIn ?? namesBundledContainer('minio', existing.S3_ENDPOINT)) {
+ const storageBuiltIn = storage.builtIn ?? namesBundledContainer('minio', existing.S3_ENDPOINT);
+ if (storageBuiltIn && (storage.builtIn !== undefined || !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
// the ones the running process dials it with, as for the database.
@@ -262,16 +261,12 @@ 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 =
- (storage.builtIn === undefined &&
- existing.STORAGE_TYPE === 's3' &&
- namesBundledContainer('minio', existing.S3_ENDPOINT)) ||
- (nextBootValue('STORAGE_TYPE', existing) === 's3' &&
- namesBundledContainer('minio', nextBootValue('S3_ENDPOINT', existing)));
+ 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';
- profiles.push('minio');
- } else {
+ } else if (!storageBuiltIn) {
// External S3/MinIO. Flipping built-in -> external must not carry the bundled
// 'minioadmin' credentials or the internal endpoint into the external config:
// the production boot guard rejects those credentials (crash-loop), and a stale
@@ -298,6 +293,7 @@ export function applyStorageSection(
}
}
}
+ if (storageBuiltIn) profiles.push('minio');
}
}
diff --git a/src/modules/infra/infra-config.controller.spec.ts b/src/modules/infra/infra-config.controller.spec.ts
index 4896e35e3..3cfb27f5d 100644
--- a/src/modules/infra/infra-config.controller.spec.ts
+++ b/src/modules/infra/infra-config.controller.spec.ts
@@ -1103,12 +1103,7 @@ describe('InfraConfigController.saveConfig per-key merge (partial payloads)', ()
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',
});
});
@@ -1134,6 +1129,30 @@ describe('InfraConfigController.saveConfig per-key merge (partial payloads)', ()
expect(env).not.toContain('openwa.sqlite');
});
+ it.each(['true', 'false'])(
+ 'an omitted choice keeps connection details under external host pins beside flags %s',
+ flag => {
+ pin({
+ DATABASE_HOST: 'db.ext.example',
+ REDIS_HOST: 'cache.ext.example',
+ S3_ENDPOINT: 'https://s3.ext.example',
+ });
+ const stored =
+ `DATABASE_TYPE=postgres\nPOSTGRES_BUILTIN=${flag}\nDATABASE_HOST=postgres\nDATABASE_PORT=6432\n` +
+ 'DATABASE_USERNAME=app\nDATABASE_PASSWORD=StrongPw12345!\nDATABASE_NAME=appdb\nPOSTGRES_SCHEMA=tenant\nDATABASE_SSL=true\nDATABASE_SSL_REJECT_UNAUTHORIZED=false\n' +
+ `REDIS_ENABLED=true\nREDIS_BUILTIN=${flag}\nREDIS_HOST=redis\nREDIS_PORT=6380\nREDIS_PASSWORD=CachePw123456\n` +
+ `STORAGE_TYPE=s3\nMINIO_BUILTIN=${flag}\nS3_ENDPOINT=http://minio:9000\nS3_BUCKET=media\n` +
+ 'S3_ACCESS_KEY=StoredKey123456\nS3_SECRET_KEY=StoredSecret1234567890\n';
+ const dotenv = jest.requireActual<typeof import('dotenv')>('dotenv');
+
+ expect(dotenv.parse(written({ database: {}, redis: {}, storage: {} }, stored))).toEqual(dotenv.parse(stored));
+ expect(dotenv.parse(written({ database: { poolSize: 25 }, redis: {}, storage: {} }, stored))).toEqual({
+ ...dotenv.parse(stored),
+ DATABASE_POOL_SIZE: '25',
+ });
+ },
+ );
+
// 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', () => {