Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(infra): report every pinned key the config save writes
Yudhi Armyndharis3f26573283ff
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.mdindex 06e04dc76..d9f51b7d4 100644--- a/docs/06-api-specification.md+++ b/docs/06-api-specification.md@@ -5543,7 +5543,7 @@ The `queue.webhooks` counters are live BullMQ job counts (`pending` = waiting +`builtIn` (on `database`/`redis`/`storage`) reports whether OpenWA's own bundled container is actually running _and_ backing this service, detected live from the labelled container (on a daemon another OpenWA instance shares, its container counts too while this instance's host is spelled like the bundled one, see [13 - Horizontal Scaling](./13-horizontal-scaling.md)); when Docker is unreachable it falls back to the saved `*_BUILTIN` flag (one pinned by the environment first, else the one in `data/.env.generated`), and a host name alone counts for nothing there, since an external server may be called `postgres`. `running` (on the same three) reports whether a bundled container OpenWA itself created is up, whether or not the service uses it: one Docker Compose created (it carries Compose's project label) reads `false`, so does one beside a configuration whose `POSTGRES_BUILTIN`, `REDIS_BUILTIN` or `MINIO_BUILTIN` (the environment's first, else the saved one) is not `true` (the container names are fixed per Docker daemon, so another OpenWA instance on the same daemon sees the same containers), and the key is absent while Docker is unreachable, since nothing can be seen or stopped then. In S3 mode `storage` additionally carries `bucket` and `endpoint` (each when one is configured; a bundled MinIO that is stopped still reports `http://minio:9000` here, with `builtIn` false) and `s3Available` (re-probed, throttled, while false; once true it stays true until a restart, so a later outage does not clear it); in local mode neither key is present. `engine.webVersion`/`engine.webVersionSource` (`pinned` / `auto` / `native`) appear only on `whatsapp-web.js`; `webVersion` is `null` until the auto-resolve first succeeds. `engine.browserArgs` is the effective launch argument list: `PUPPETEER_ARGS` (or the four defaults shown) with `--lang=en-US` appended unless a `--lang` flag is already present.-`envPinned` lists which of `DATABASE_TYPE`, `DATABASE_HOST`, `REDIS_ENABLED`, `REDIS_HOST`, `STORAGE_TYPE`, `S3_ENDPOINT`, `STORAGE_LOCAL_PATH`, `ENGINE_TYPE`, `PUPPETEER_HEADLESS`, `SESSION_DATA_PATH` and `PUPPETEER_ARGS` the host environment or the project `.env` sets. Those layers win over `data/.env.generated`, so a value saved for one of these keys through `PUT /api/infra/config` cannot take effect until that layer is changed. A pinned host also decides whether the bundled container backs its service, whatever `builtIn` is saved.+`envPinned` lists which of the keys `PUT /api/infra/config` writes the host environment or the project `.env` sets, by name only: `DATABASE_TYPE`, `POSTGRES_BUILTIN`, `DATABASE_HOST`, `DATABASE_PORT`, `DATABASE_USERNAME`, `DATABASE_PASSWORD`, `DATABASE_NAME`, `DATABASE_POOL_SIZE`, `POSTGRES_SCHEMA`, `DATABASE_SSL`, `DATABASE_SSL_REJECT_UNAUTHORIZED`, `REDIS_ENABLED`, `REDIS_BUILTIN`, `REDIS_HOST`, `REDIS_PORT`, `REDIS_PASSWORD`, `QUEUE_ENABLED`, `STORAGE_TYPE`, `MINIO_BUILTIN`, `S3_ENDPOINT`, `S3_BUCKET`, `S3_REGION`, `S3_ACCESS_KEY_ID`, `S3_SECRET_ACCESS_KEY`, `STORAGE_LOCAL_PATH`, `ENGINE_TYPE`, `PUPPETEER_HEADLESS`, `SESSION_DATA_PATH` and `PUPPETEER_ARGS`. Those layers win over `data/.env.generated`, so a value saved for one of these keys through `PUT /api/infra/config` cannot take effect until that layer is changed. A pinned host also decides whether the bundled container backs its service, whatever `builtIn` is saved.**Errors:** `401` missing/invalid key · `403` key role < ADMIN, or key is session-scopeddiff --git a/src/modules/infra/infra-status.controller.spec.ts b/src/modules/infra/infra-status.controller.spec.tsindex c4a295f45..237fb26f6 100644--- a/src/modules/infra/infra-status.controller.spec.ts+++ b/src/modules/infra/infra-status.controller.spec.ts@@ -16,7 +16,10 @@ jest.mock('fs', () => {});import { InfraStatusController } from './infra-status.controller';-import { recordPinnedEnvKeys } from '../../config/env-precedence';+import { InfraConfigController } from './infra-config.controller';+import type { ConfigSectionContext } from './config-sections';+import type { SaveConfigDto } from './dto/save-config.dto';+import { BLANK_SHADOWED_ENV_KEYS, recordPinnedEnvKeys } from '../../config/env-precedence';import { producerConnectWaitMs } from '../queue/redis-connection';import * as fs from 'fs';import {@@ -318,6 +321,63 @@ describe('InfraStatusController.getStatus envPinned', () => {expect((await buildController().getStatus()).envPinned).toEqual([]);});++ // A pinned key the page writes outranks the saved value at every boot, so the page must hear of each one: an+ // unreported pin under an edited field reads as a change the next boot makes (a bucket or database switch).+ it('reports every key a save from the page writes, and no other', async () => {+ recordPinnedEnvKeys({});+ const engines = { getAvailableEngines: () => [{ id: 'whatsapp-web.js' }, { id: 'baileys' }] };+ const saver = new InfraConfigController(engines as never, {} as never, {} as never) as unknown as {+ applyConfigSections: (config: SaveConfigDto, existing: Record<string, string>, ctx: ConfigSectionContext) => void;+ };+ const payloads = [+ {+ database: {+ type: 'postgres',+ builtIn: false,+ host: 'db.example',+ port: '5433',+ username: 'app',+ password: 'secret',+ database: 'appdb',+ schema: 'tenant',+ poolSize: 20,+ sslEnabled: true,+ sslRejectUnauthorized: false,+ },+ redis: { enabled: true, builtIn: false, host: 'cache.example', port: '6380', password: 'secret' },+ queue: { enabled: true },+ storage: {+ type: 's3',+ builtIn: false,+ s3Bucket: 'media',+ s3Region: 'eu-west-1',+ s3AccessKey: 'key',+ s3SecretKey: 'secret',+ s3Endpoint: 'https://s3.example',+ },+ engine: { type: 'baileys', headless: false, sessionDataPath: './data/s', browserArgs: '--no-sandbox' },+ },+ {+ database: { type: 'postgres', builtIn: true },+ redis: { enabled: true, builtIn: true },+ storage: { type: 's3', builtIn: true },+ },+ { database: { type: 'sqlite' }, redis: { enabled: false }, storage: { type: 'local', localPath: './data/m' } },+ ];+ const written = new Set<string>();+ for (const payload of payloads) {+ const ctx: ConfigSectionContext = { updates: {}, staleKeys: new Set(), profiles: [] };+ saver.applyConfigSections(payload as SaveConfigDto, {}, ctx);+ [...Object.keys(ctx.updates), ...ctx.staleKeys].forEach(key => written.add(key));+ }+ // Every compose forward is pinned beside them, so a reported key no save writes would show up too.+ recordPinnedEnvKeys(Object.fromEntries([...BLANK_SHADOWED_ENV_KEYS, ...written].map(key => [key, 'x'])));++ const reported = (await buildController().getStatus()).envPinned;++ expect([...reported].sort()).toEqual([...written].sort());+ });});// With Docker unreachable the status falls back to the saved flag, the environment's first. A host name provesdiff --git a/src/modules/infra/infra-status.controller.ts b/src/modules/infra/infra-status.controller.tsindex df0c9497a..a11f7ab62 100644--- a/src/modules/infra/infra-status.controller.ts+++ b/src/modules/infra/infra-status.controller.ts@@ -67,19 +67,37 @@ interface InfraStatus {}/**- * The keys behind the Infrastructure page's editable controls that a shipped compose file or an- * operator variable is known to pin. docker-compose.dev.yml pins the local storage path and the three- * whatsapp-web.js launch options with non-empty defaults. Only page controls are reported: naming- * variables the page cannot edit would be noise. A pinned host decides the Use Built-in mode of its- * service, so the three host keys are reported too.+ * Every key a save from the Infrastructure page writes (PUT /infra/config), so the page can name each one a layer+ * above the file pins: an unreported pin under an edited field reads as a change the next boot makes.+ * docker-compose.dev.yml pins the local storage path and the three whatsapp-web.js launch options with non-empty+ * defaults; the compose .env route pins the datastore settings. Only page controls are reported: naming variables+ * the page cannot edit would be noise. A pinned host decides the Use Built-in mode of its service.*/const DASHBOARD_PINNABLE_ENV_KEYS = ['DATABASE_TYPE',+ 'POSTGRES_BUILTIN','DATABASE_HOST',+ 'DATABASE_PORT',+ 'DATABASE_USERNAME',+ 'DATABASE_PASSWORD',+ 'DATABASE_NAME',+ 'DATABASE_POOL_SIZE',+ 'POSTGRES_SCHEMA',+ 'DATABASE_SSL',+ 'DATABASE_SSL_REJECT_UNAUTHORIZED','REDIS_ENABLED',+ 'REDIS_BUILTIN','REDIS_HOST',+ 'REDIS_PORT',+ 'REDIS_PASSWORD',+ 'QUEUE_ENABLED','STORAGE_TYPE',+ 'MINIO_BUILTIN','S3_ENDPOINT',+ 'S3_BUCKET',+ 'S3_REGION',+ 'S3_ACCESS_KEY_ID',+ 'S3_SECRET_ACCESS_KEY','STORAGE_LOCAL_PATH','ENGINE_TYPE','PUPPETEER_HEADLESS',