Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(infra): read only the flag for the status fallback with Docker down
Yudhi Armyndharis75bcb08da994
diff --git a/src/modules/infra/infra-status.controller.spec.ts b/src/modules/infra/infra-status.controller.spec.tsindex c9e069881..cd9a37da4 100644--- a/src/modules/infra/infra-status.controller.spec.ts+++ b/src/modules/infra/infra-status.controller.spec.ts@@ -20,7 +20,7 @@ import { recordPinnedEnvKeys } from '../../config/env-precedence';import { producerConnectWaitMs } from '../queue/redis-connection';import * as fs from 'fs';import {- EXPECTED_MODE,+ FALLBACK_BUILT_IN,FILE_STATES,PIN_STATES,SERVICES,@@ -294,9 +294,10 @@ describe('InfraStatusController.getStatus envPinned', () => {});});-// With Docker unreachable the status falls back to the saved built-in mode, and it must be the one GET /infra/config-// answers with, or the page claims a switch that no restart makes. Every file and pin state, each service.-describe('InfraStatusController.getStatus built-in fallback agrees with the saved mode when Docker is unreachable', () => {+// With Docker unreachable the status falls back to the saved flag, the environment's first. A host name proves+// nothing without a container to see (an external server may be called postgres), so a pinned bundled host must not+// read as built-in there: it would replace the stored external credentials with the bundled defaults on a save.+describe('InfraStatusController.getStatus built-in fallback reads the flag only when Docker is unreachable', () => {const build = () => {const config = { get: (k: string, def?: unknown) => (k === 'engine.type' ? 'baileys' : def) };const ds = { isInitialized: true, query: jest.fn().mockResolvedValue([{ '1': 1 }]) };@@ -343,9 +344,28 @@ describe('InfraStatusController.getStatus built-in fallback agrees with the saveconst status = await build().getStatus();- expect(status[service].builtIn).toBe(EXPECTED_MODE[state][pin].builtIn);+ expect(status[service].builtIn).toBe(FALLBACK_BUILT_IN[state]);});}}}++ it('a flag pinned by the environment wins over the file, and a pinned host still proves nothing', async () => {+ const pins = {+ POSTGRES_BUILTIN: 'true',+ REDIS_BUILTIN: 'false',+ DATABASE_HOST: 'postgres',+ REDIS_HOST: 'redis',+ S3_ENDPOINT: 'http://minio:9000',+ };+ recordPinnedEnvKeys(pins);+ Object.assign(process.env, pins);+ pinnedKeys = Object.keys(pins);+ (fs.existsSync as jest.Mock).mockReturnValue(true);+ (fs.readFileSync as jest.Mock).mockReturnValue('POSTGRES_BUILTIN=false\nREDIS_BUILTIN=true\nMINIO_BUILTIN=false\n');++ const status = await build().getStatus();++ expect([status.database.builtIn, status.redis.builtIn, status.storage.builtIn]).toEqual([true, false, false]);+ });});diff --git a/src/modules/infra/infra-status.controller.ts b/src/modules/infra/infra-status.controller.tsindex 90a7b7f07..fb3a6a976 100644--- a/src/modules/infra/infra-status.controller.ts+++ b/src/modules/infra/infra-status.controller.ts@@ -23,7 +23,7 @@ import { CacheService } from '../../common/cache/cache.service';import { StorageService } from '../../common/storage/storage.service';import { createLogger } from '../../common/services/logger.service';import { readGeneratedEnv } from './generated-env';-import { savedBuiltInMode } from './saved-builtin-mode';+import { effectiveBuiltInFlag } from './saved-builtin-mode';import { isEnvPinned } from '../../config/env-precedence';import { DEFAULT_PUPPETEER_ARGS } from '../../config/configuration';import { checkForUpdate, type UpdateCheck } from './update-check';@@ -31,7 +31,7 @@ import { checkForUpdate, type UpdateCheck } from './update-check';interface InfraStatus {// `builtIn` reflects whether OpenWA's own bundled container is actually running and backing this// service (detected live from the labeled container), not merely the saved intent. Falls back to the- // saved flag when Docker is unavailable. (#488)+ // saved flag, the environment's first, when Docker is unavailable. (#488)database: { connected: boolean; type: string; host: string; builtIn: boolean };redis: { enabled: boolean; connected: boolean; host: string; port: number; builtIn: boolean };queue: {@@ -182,8 +182,8 @@ export class InfraStatusController {// Built-in detection: prefer the actually-running bundled container as truth (so a stopped/missing// container, or a host-pinned external host, reads as NOT built-in), and require the app to be- // pointed at the bundled service. Fall back to the saved *_BUILTIN intent when Docker is- // unreachable (bare-npm / socket-less) so the toggles don't spuriously flip off. (#488)+ // pointed at the bundled service. Fall back to the saved *_BUILTIN flag (the environment's first) when+ // Docker is unreachable (bare-npm / socket-less) so the toggles don't spuriously flip off. (#488)const s3Endpoint = this.configService.get<string>('storage.s3.endpoint');const running = this.dockerService.isDockerAvailable()? await this.dockerService.getRunningBuiltinServices()@@ -244,16 +244,16 @@ export class InfraStatusController {}/**- * The saved built-in mode, the fallback when Docker isn't reachable. The same answer GET /infra/config gives,- * so a host the environment pins decides here too and the page never claims a switch no restart makes.+ * The saved built-in flags, the fallback when Docker isn't reachable: the environment's, else+ * data/.env.generated's. A host name proves nothing without a container to see, so none counts here.*/private readSavedBuiltinFlags(): { database: boolean; cache: boolean; storage: boolean } {try {const saved = readGeneratedEnv();return {- database: savedBuiltInMode('database', saved).builtIn,- cache: savedBuiltInMode('redis', saved).builtIn,- storage: savedBuiltInMode('storage', saved).builtIn,+ database: effectiveBuiltInFlag('database', saved),+ cache: effectiveBuiltInFlag('redis', saved),+ storage: effectiveBuiltInFlag('storage', saved),};} catch {return { database: false, cache: false, storage: false };diff --git a/src/modules/infra/saved-builtin-mode.ts b/src/modules/infra/saved-builtin-mode.tsindex b9f569d03..bb49903b8 100644--- a/src/modules/infra/saved-builtin-mode.ts+++ b/src/modules/infra/saved-builtin-mode.ts@@ -44,6 +44,21 @@ export interface SavedBuiltInMode {builtInSet: boolean;}+/** The flag value the next boot reads: the environment's when it pins the flag, else the file's. */+function flagValue(service: BuiltInService, saved: Record<string, string>): string | undefined {+ const flag = SERVICES[service].flag;+ return isEnvPinned(flag) ? process.env[flag] : saved[flag];+}++/**+ * The built-in flag as GET /infra/status falls back to it with Docker unreachable. No host rule here: with no+ * container to see, a host called postgres is as likely an external server (a Kubernetes Service, say) and must+ * not read as the bundled container.+ */+export function effectiveBuiltInFlag(service: BuiltInService, saved: Record<string, string>): boolean {+ return flagValue(service, saved) === 'true';+}+/*** Rules, in order:* 1. A host pinned by the environment (or the project .env) decides, as it does at boot and in@@ -59,7 +74,7 @@ export function savedBuiltInMode(service: BuiltInService, saved: Record<string,return { builtIn: process.env[spec.host] === spec.bundledHost, builtInSet: false };}const flagPinned = isEnvPinned(spec.flag);- const flag = (flagPinned ? process.env[spec.flag] : undefined) ?? saved[spec.flag];+ const flag = flagValue(service, saved);const selectedInFile = saved[spec.selection] === spec.selected;return { builtIn: flag === 'true', builtInSet: flag !== undefined && (flagPinned || selectedInFile) };}diff --git a/test/fixtures/builtin-mode-cases.ts b/test/fixtures/builtin-mode-cases.tsindex 320186642..ebbd03168 100644--- a/test/fixtures/builtin-mode-cases.ts+++ b/test/fixtures/builtin-mode-cases.ts@@ -104,6 +104,19 @@ export const EXPECTED_MODE: Record<FileState, Record<PinState, SavedMode>> = {},};+/**+ * What GET /infra/status answers with Docker unreachable, where there is no container to see. A host name proves+ * nothing there (an external server may be called postgres), so only the flag counts: the environment's, else the+ * file's, whatever the host pins say.+ */+export const FALLBACK_BUILT_IN: Record<FileState, boolean> = {+ absent: false,+ template: false,+ 'stored-false': false,+ 'stored-true': true,+ 'unselected-true': true,+};+/** The lines a saved file holds about one service in a given state. */export function fileLines(service: ServiceName, state: FileState): string[] {const spec = SERVICES[service];