Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(infra): read the built-in mode from any spelling of the bundled host
Yudhi Armyndharis6cb800f94bcb
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.mdindex 7f765eed8..76fe35d09 100644--- a/docs/06-api-specification.md+++ b/docs/06-api-specification.md@@ -5671,7 +5671,7 @@ Read the effective infrastructure config used to hydrate the dashboard form. Eac}```-`builtInSet` says whether the saved configuration states the built-in mode of the database, Redis or storage. It is `false` when a host pinned by the environment (`DATABASE_HOST`, `REDIS_HOST`, `S3_ENDPOINT`) decides the mode, in which case `builtIn` reports the pin (`true` for the bundled host `postgres`, `redis` or `http://minio:9000`), when the saved file does not select the service (the first boot writes every flag as `false` next to a service that is not in use), when the file holds the bundled host with the flag not `true` (the next boot dials the container, so `builtIn` is `true`), and when nothing is saved. Read what runs from `GET /api/infra/status` then. Where no host is pinned, a flag pinned by the environment itself counts as stated.+`builtInSet` says whether the saved configuration states the built-in mode of the database, Redis or storage. It is `false` when a host pinned by the environment (`DATABASE_HOST`, `REDIS_HOST`, `S3_ENDPOINT`) decides the mode, in which case `builtIn` reports the pin (`true` for the bundled host `postgres`, `redis` or `http://minio:9000`, or another spelling that reaches the same container: its container name such as `openwa-postgres`, another letter case, a trailing dot), when the saved file does not select the service (the first boot writes every flag as `false` next to a service that is not in use), when the file holds the bundled host in any of those spellings with the flag not `true` (the next boot dials the container, so `builtIn` is `true`), and when nothing is saved. Read what runs from `GET /api/infra/status` then. Where no host is pinned, a flag pinned by the environment itself counts as stated.When nothing supplies a key — no pinned environment value and the file absent or silent about it — empty-string/default values are returned (`schema='public'`, `poolSize=10`, `sslRejectUnauthorized=true`, `engine.type='whatsapp-web.js'`, `headless=true`).diff --git a/src/modules/infra/infra-config.controller.spec.ts b/src/modules/infra/infra-config.controller.spec.tsindex f258b342a..160290a70 100644--- a/src/modules/infra/infra-config.controller.spec.ts+++ b/src/modules/infra/infra-config.controller.spec.ts@@ -1890,6 +1890,36 @@ describe('InfraConfigController.getConfig answers the saved built-in mode for ev}});+ // A host that reaches the bundled container in another spelling reads as the bundled host, pinned or in the file;+ // any other host does not.+ describe.each(SERVICE_NAMES)('%s under another spelling of the bundled host', service => {+ const spec = SERVICES[service];+ const inFile = (host: string) => {+ recordPinnedEnvKeys({});+ (fs.existsSync as jest.Mock).mockReturnValue(true);+ (fs.readFileSync as jest.Mock).mockReturnValue(+ `${spec.selection}=${spec.selected}\n${spec.flag}=false\n${spec.host}=${host}\n`,+ );+ return modeOf(newController().getConfig(), service);+ };+ const pinned = (host: string) => {+ process.env[spec.host] = host;+ pinnedKeys = [spec.host];+ recordPinnedEnvKeys({ [spec.host]: host });+ (fs.existsSync as jest.Mock).mockReturnValue(true);+ (fs.readFileSync as jest.Mock).mockReturnValue(`${spec.selection}=${spec.selected}\n${spec.flag}=false\n`);+ return modeOf(newController().getConfig(), service);+ };+ it.each(HOST_SPELLINGS[service].reach)('%j pinned or in the file reads built-in', host => {+ expect(inFile(host)).toEqual({ builtIn: true, builtInSet: false });+ expect(pinned(host)).toEqual({ builtIn: true, builtInSet: false });+ });+ it.each(HOST_SPELLINGS[service].elsewhere)('%j pinned or in the file does not', host => {+ expect(inFile(host)).toEqual({ builtIn: false, builtInSet: true });+ expect(pinned(host)).toEqual({ builtIn: false, builtInSet: false });+ });+ });+// The first boot writes every flag false next to a service that is not selected. With the compose .env pinning// the bundled hosts and the containers running, that file must not read as three saved external services.it('the first-boot file under the compose .env pins reads as bundled, never as a saved external choice', () => {diff --git a/src/modules/infra/infra-status.controller.spec.ts b/src/modules/infra/infra-status.controller.spec.tsindex b0e3090ae..552069f62 100644--- a/src/modules/infra/infra-status.controller.spec.ts+++ b/src/modules/infra/infra-status.controller.spec.ts@@ -22,6 +22,7 @@ import * as fs from 'fs';import {FALLBACK_BUILT_IN,FILE_STATES,+ HOST_SPELLINGS,PIN_STATES,SERVICES,SERVICE_NAMES,@@ -451,6 +452,40 @@ describe('InfraStatusController.getStatus running bundled containers', () => {expect([status.database.running, status.redis.running, status.storage.running]).toEqual([false, false, false]);});+ // A running bundled container backs the service when its host reaches the container in any spelling.+ describe.each([+ ['database', 'dataDatabase.host', 'database'],+ ['redis', 'redis.host', 'cache'],+ ['storage', 'storage.s3.endpoint', 'storage'],+ ] as const)('%s built-in under another spelling of its host', (service, configKey, container) => {+ const statusFor = async (host: string) => {+ const values: Record<string, unknown> = {+ 'dataDatabase.type': 'postgres',+ 'storage.type': 's3',+ 'storage.s3.endpoint': 'https://s3.ext.example',+ [configKey]: host,+ };+ const ds = { isInitialized: true, query: jest.fn().mockResolvedValue([{ '1': 1 }]) };+ const running = { database: false, cache: false, storage: false, [container]: 'compose' };+ return new InfraStatusController(+ { get: (k: string, def?: unknown) => values[k] ?? def } as never,+ ds as never,+ ds as never,+ { create: jest.fn() } as never,+ { isDockerAvailable: () => true, getRunningBuiltinServices: jest.fn().mockResolvedValue(running) } as never,+ { isAvailable: jest.fn().mockResolvedValue(false) } as never,+ { refreshS3Availability: jest.fn().mockResolvedValue(true) } as never,+ undefined as never,+ ).getStatus();+ };+ it.each([+ ...HOST_SPELLINGS[service].reach.map(host => [host, true] as const),+ ...HOST_SPELLINGS[service].elsewhere.filter(Boolean).map(host => [host, false] as const),+ ])('%j reads built-in: %s', async (host, builtIn) => {+ expect((await statusFor(host))[service].builtIn).toBe(builtIn);+ });+ });+// Nothing can be seen or stopped then, so a reader cannot take the absence of a container for a fact.it('leaves running out when Docker is unreachable', async () => {const status = await build(null).getStatus();diff --git a/src/modules/infra/infra-status.controller.ts b/src/modules/infra/infra-status.controller.tsindex e84c58bb1..4507bad25 100644--- a/src/modules/infra/infra-status.controller.ts+++ b/src/modules/infra/infra-status.controller.ts@@ -18,7 +18,7 @@ import { Public, RequireRole, RequireUnscopedKey } from '../auth/decorators/authimport { ApiKeyRole } from '../auth/entities/api-key.entity';import { EngineFactory } from '../../engine/engine.factory';import { getEffectiveWebVersionInfo, resolveCurrentWebVersion } from '../../engine/wa-web-version';-import { DockerService } from '../docker';+import { DockerService, namesBundledContainer } from '../docker';import { CacheService } from '../../common/cache/cache.service';import { StorageService } from '../../common/storage/storage.service';import { createLogger } from '../../common/services/logger.service';@@ -203,9 +203,11 @@ export class InfraStatusController {? await this.dockerService.getRunningBuiltinServices(): null;const savedBuiltin = this.readSavedBuiltinFlags();- const dbBuiltIn = running ? !!running.database && dbHost === 'postgres' : savedBuiltin.database;- const redisBuiltIn = running ? !!running.cache && redisHost === 'redis' : savedBuiltin.cache;- const storageBuiltIn = running ? !!running.storage && s3Endpoint === 'http://minio:9000' : savedBuiltin.storage;+ const dbBuiltIn = running ? !!running.database && namesBundledContainer('postgres', dbHost) : savedBuiltin.database;+ const redisBuiltIn = running ? !!running.cache && namesBundledContainer('redis', redisHost) : savedBuiltin.cache;+ const storageBuiltIn = running+ ? !!running.storage && namesBundledContainer('minio', s3Endpoint)+ : savedBuiltin.storage;// Re-probe (throttled) so a MinIO/S3 that came up after boot is reflected, not latched unreachable.const s3Available = storageType === 's3' ? await this.storageService.refreshS3Availability() : undefined;diff --git a/src/modules/infra/saved-builtin-mode.ts b/src/modules/infra/saved-builtin-mode.tsindex f93b302c2..785d34224 100644--- a/src/modules/infra/saved-builtin-mode.ts+++ b/src/modules/infra/saved-builtin-mode.ts@@ -1,31 +1,32 @@import { isEnvPinned } from '../../config/env-precedence';+import { namesBundledContainer } from '../docker';/*** The built-in (bundled container) mode the saved configuration states for each datastore, shared by* GET /infra/config and the Docker-unreachable fallback of GET /infra/status so the two cannot disagree.*- * `flag` is what the dashboard saves, `host` is what the next boot connects to, and `selection` is the- * file's own record that the service is in use at all.+ * `flag` is what the dashboard saves, `host` is what the next boot connects to, `profile` names the bundled container+ * that host may reach, and `selection` is the file's own record that the service is in use at all.*/const SERVICES = {database: {flag: 'POSTGRES_BUILTIN',host: 'DATABASE_HOST',- bundledHost: 'postgres',+ profile: 'postgres',selection: 'DATABASE_TYPE',selected: 'postgres',},redis: {flag: 'REDIS_BUILTIN',host: 'REDIS_HOST',- bundledHost: 'redis',+ profile: 'redis',selection: 'REDIS_ENABLED',selected: 'true',},storage: {flag: 'MINIO_BUILTIN',host: 'S3_ENDPOINT',- bundledHost: 'http://minio:9000',+ profile: 'minio',selection: 'STORAGE_TYPE',selected: 's3',},@@ -62,8 +63,8 @@ export function effectiveBuiltInFlag(service: BuiltInService, saved: Record<stri/*** Rules, in order:* 1. A host pinned by the environment (or the project .env) decides, as it does at boot and in- * requestRestart: the bundled host is built-in, any other is not, and no saved flag can be a pending- * switch because the pin outranks it.+ * requestRestart: the bundled host in any spelling that reaches the container is built-in, any other is+ * not, and no saved flag can be a pending switch because the pin outranks it.* 2. Otherwise the flag counts as saved when the environment pins it, or when the file stores it for a* service the file also selects. A flag that is not true beside the bundled host name in the file is no* choice of an external service, though: the next boot dials the container, so it reads as built-in and@@ -73,12 +74,12 @@ export function effectiveBuiltInFlag(service: BuiltInService, saved: Record<striexport function savedBuiltInMode(service: BuiltInService, saved: Record<string, string>): SavedBuiltInMode {const spec = SERVICES[service];if (isEnvPinned(spec.host)) {- return { builtIn: process.env[spec.host] === spec.bundledHost, builtInSet: false };+ return { builtIn: namesBundledContainer(spec.profile, process.env[spec.host]), builtInSet: false };}const flagPinned = isEnvPinned(spec.flag);const flag = flagValue(service, saved);const selectedInFile = saved[spec.selection] === spec.selected;- const dialsBundled = saved[spec.host] === spec.bundledHost;+ const dialsBundled = namesBundledContainer(spec.profile, saved[spec.host]);if (flag !== 'true' && dialsBundled) return { builtIn: true, builtInSet: false };return { builtIn: flag === 'true', builtInSet: flag !== undefined && (flagPinned || selectedInFile) };}