Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(infra): keep a bundled service the environment pins the app to
Yudhi Armyndharis8169ea22bc24
diff --git a/src/modules/infra/infra-config.controller.spec.ts b/src/modules/infra/infra-config.controller.spec.tsindex d7024570e..7101dcf1c 100644--- a/src/modules/infra/infra-config.controller.spec.ts+++ b/src/modules/infra/infra-config.controller.spec.ts@@ -1228,6 +1228,62 @@ describe('InfraConfigController.requestRestart constrains teardown to managed prexpect(JSON.stringify(result.removal)).not.toContain('removed');});+ describe('a service the environment pins the app to', () => {+ const KEYS = ['DATABASE_HOST', 'REDIS_HOST', 'S3_ENDPOINT'];+ let savedEnv: Array<[string, string | undefined]>;++ beforeEach(() => {+ savedEnv = KEYS.map(k => [k, process.env[k]]);+ });+ afterEach(() => {+ for (const [k, v] of savedEnv) {+ if (v === undefined) delete process.env[k];+ else process.env[k] = v;+ }+ // Back to the no-snapshot default the rest of the file assumes.+ recordPinnedEnvKeys(process.env);+ });++ it('is never stopped, since the restarted app would still point at it', async () => {+ // The documented manual built-in Postgres setup: DATABASE_HOST=postgres in .env or on the host.+ process.env.DATABASE_HOST = 'postgres';+ process.env.REDIS_HOST = 'redis';+ process.env.S3_ENDPOINT = 'http://minio:9000';+ recordPinnedEnvKeys(process.env);+ const stopManagedService = jest.fn().mockResolvedValue(true);+ const controller = buildController({+ isDockerAvailable: () => true,+ stopManagedService,+ orchestrateProfiles: jest.fn().mockResolvedValue({}),+ });++ const result = await controller.requestRestart({ profilesToRemove: ['postgres', 'redis', 'minio'] });++ expect(stopManagedService).not.toHaveBeenCalled();+ expect(result.profilesToRemove).toEqual([]);+ });++ it('is still stopped when the pin names a different host', async () => {+ process.env.DATABASE_HOST = 'db.example.internal';+ delete process.env.REDIS_HOST;+ delete process.env.S3_ENDPOINT;+ recordPinnedEnvKeys(process.env);+ const stopManagedService = jest.fn().mockResolvedValue(true);+ const controller = buildController({+ isDockerAvailable: () => true,+ stopManagedService,+ orchestrateProfiles: jest.fn().mockResolvedValue({}),+ });++ await controller.requestRestart({ profilesToRemove: ['postgres', 'redis'] });++ expect(stopManagedService.mock.calls.map(call => String((call as unknown[])[0])).sort()).toEqual([+ 'postgres',+ 'redis',+ ]);+ });+ });+it('starts only allowlisted profiles, never an unknown entry (symmetry with teardown)', async () => {const orchestrateProfiles = jest.fn().mockResolvedValue({});const controller = buildController({diff --git a/src/modules/infra/infra-config.controller.ts b/src/modules/infra/infra-config.controller.tsindex 1a8f88079..9f4f3be64 100644--- a/src/modules/infra/infra-config.controller.ts+++ b/src/modules/infra/infra-config.controller.ts@@ -56,6 +56,13 @@ class RestartDto {profilesToRemove?: string[];}+// The env key and value that point the app at each bundled service, as /infra/status detects them.+const BUNDLED_SERVICE_ENV: Record<string, [string, string]> = {+ postgres: ['DATABASE_HOST', 'postgres'],+ redis: ['REDIS_HOST', 'redis'],+ minio: ['S3_ENDPOINT', 'http://minio:9000'],+};+// Saved infrastructure config returned to the dashboard form for hydration. Secret// values are never echoed back — a `*Set` boolean indicates whether one is stored.interface SavedConfigResponse {@@ -404,7 +411,17 @@ export class InfraConfigController {removal?: object;}> {const profiles = body?.profiles || [];- const profilesToRemove = body?.profilesToRemove || [];+ // Never stop a bundled service the environment pins the app to (e.g. the documented manual+ // built-in Postgres with DATABASE_HOST=postgres in .env): that pin outranks the saved config, so+ // the restarted app would still point at the container and fail to boot.+ const pinnedToBundled = Object.entries(BUNDLED_SERVICE_ENV)+ .filter(([, [key, value]]) => isEnvPinned(key) && process.env[key] === value)+ .map(([profile]) => profile);+ const requestedRemoval = body?.profilesToRemove || [];+ const profilesToRemove = requestedRemoval.filter(p => !pinnedToBundled.includes(p));+ if (profilesToRemove.length < requestedRemoval.length) {+ this.logger.warn('Keeping profiles the environment pins the app to', { pinnedToBundled });+ }let orchestrationResult: object | undefined;// Teardown is stop-only (see DockerService.stopManagedService): containers are stopped and// retained for re-enable, never deleted — the result below reports exactly that.@@ -419,8 +436,9 @@ export class InfraConfigController {// (re)start. We deliberately do NOT infer teardown from the saved *_BUILTIN flag: the default// data/.env.generated carries POSTGRES_BUILTIN=false, so a bare compose-profile restart would// otherwise tear down the very backend the app is running on. The dashboard fills this list from the- // live /infra/status builtIn flags at save time (running minus the new profiles), so a container is- // left running only when that status read had failed and the dashboard had nothing to go on.+ // live /infra/status builtIn flags at save time (running minus the new profiles). A container is+ // left running when that status read had failed, or when an environment pin keeps the app on it+ // (dropped above).// Only ever tear down OpenWA-managed services. An arbitrary profile name (or the empty string)// would otherwise reach stopManagedService and, via container-name matching, could stop an unrelated// container — so constrain teardown to the managed allowlist and drop anything else.