Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(infra): keep stored credentials under any bundled host spelling
Yudhi Armyndharisc96e731aac78
diff --git a/src/modules/infra/config-sections.ts b/src/modules/infra/config-sections.tsindex 90ed6260d..51a6984c6 100644--- a/src/modules/infra/config-sections.ts+++ b/src/modules/infra/config-sections.ts@@ -3,7 +3,7 @@ import { DEFAULT_PUPPETEER_ARGS } from '../../config/configuration';import { isEnvPinned } from '../../config/env-precedence';import { postgresSchemaError } from '../../config/env.validation';import type { EngineFactory } from '../../engine/engine.factory';-import { datastoreInUse } from '../docker';+import { datastoreInUse, namesBundledContainer } from '../docker';import { DatabaseConfigDto, EngineConfigDto, RedisConfigDto, StorageConfigDto } from './dto/save-config.dto';// Mutable accumulator threaded through the pipeline stages and per-section appliers extracted@@ -91,10 +91,11 @@ export function applyDatabaseSection(// What decides it is whether the stored password belongs to this same bundled container:// it does when the previous mode was already built-in, or when the next boot already dials the// bundled host with it (the environment's host, else the file's, a file that names it without the- // flag included). Coming from external, the stored value is the external DB's credential and must- // not be carried into the container.+ // flag included, in any spelling that reaches the container). Coming from external, the stored value is the+ // external DB's credential and must not be carried into the container.const storedPassword =- existing.POSTGRES_BUILTIN === 'true' || nextBootHost('DATABASE_HOST', existing) === 'postgres'+ existing.POSTGRES_BUILTIN === 'true' ||+ namesBundledContainer('postgres', nextBootHost('DATABASE_HOST', existing))? existing.DATABASE_PASSWORD: undefined;updates.DATABASE_PASSWORD = database.password || storedPassword || 'openwa';@@ -198,7 +199,7 @@ export function applyRedisSection(// bundled container may be started for it at boot or kept by the restart, which keeps a container while the// saved host still names it. A Redis only a pin switches off keeps the stored choice.updates.REDIS_BUILTIN = 'false';- if (saved('REDIS_HOST') === 'redis') staleKeys.add('REDIS_HOST');+ if (namesBundledContainer('redis', saved('REDIS_HOST'))) staleKeys.add('REDIS_HOST');}}@@ -233,9 +234,9 @@ export function applyStorageSection(if (storage.builtIn === true) {// Built-in MinIO - use container name as endpoint. The bundled keys are only the DEFAULT, as for the database// password: keys the next boot already dials the bundled endpoint with (the flag already set, or the bundled- // endpoint pinned or stored) are the ones that container was created with, and are kept.+ // endpoint pinned or stored, in any spelling) are the ones that container was created with, and are kept.const keepKeys =- existing.MINIO_BUILTIN === 'true' || nextBootHost('S3_ENDPOINT', existing) === 'http://minio:9000';+ existing.MINIO_BUILTIN === 'true' || namesBundledContainer('minio', nextBootHost('S3_ENDPOINT', existing));updates.S3_ENDPOINT = 'http://minio:9000';updates.S3_ACCESS_KEY_ID = (keepKeys && (existing.S3_ACCESS_KEY_ID || existing.S3_ACCESS_KEY)) || 'minioadmin';updates.S3_SECRET_ACCESS_KEY =diff --git a/src/modules/infra/infra-config.controller.spec.ts b/src/modules/infra/infra-config.controller.spec.tsindex 68a08eb5e..f258b342a 100644--- a/src/modules/infra/infra-config.controller.spec.ts+++ b/src/modules/infra/infra-config.controller.spec.ts@@ -1214,6 +1214,16 @@ describe('InfraConfigController.saveConfig built-in/external mode flips and theexpect(env).toContain('REDIS_HOST=cache.example.com');});+ // The restart keeps a container while the saved host reaches it in any spelling, so each one is dropped here.+ it.each(HOST_SPELLINGS.redis.reach)('drops a host %j that still reaches the bundled container', host => {+ const env = written(+ { redis: { enabled: false, builtIn: false, host }, queue: { enabled: false } },+ `REDIS_ENABLED=true\nREDIS_BUILTIN=false\nREDIS_HOST=${host}\n`,+ );++ expect(env).not.toMatch(/^REDIS_HOST=/m);+ });+it('keeps the bundled Redis while the queue still uses it', () => {const env = written({ redis: { enabled: false, builtIn: true }, queue: { enabled: true } }, BUILTIN_REDIS_ENV);@@ -1377,6 +1387,24 @@ describe('InfraConfigController.saveConfig built-in/external mode flips and theexpect(env).toContain('DATABASE_PASSWORD=openwa');expect(env).not.toContain('ExternalPw!');});++ // Any spelling that reaches the bundled container is the bundled host, as for the restart's stop and start rules.+ const STORED = 'DATABASE_TYPE=postgres\nPOSTGRES_BUILTIN=false\nDATABASE_PASSWORD=CustomPw123!\n';+ it.each(HOST_SPELLINGS.database.reach)('keeps it under %j in the file or pinned', host => {+ expect(written({ database: { type: 'postgres', builtIn: true } }, `${STORED}DATABASE_HOST=${host}\n`)).toContain(+ 'DATABASE_PASSWORD=CustomPw123!',+ );+ process.env.DATABASE_HOST = host;+ recordPinnedEnvKeys({ DATABASE_HOST: host });+ expect(+ written({ database: { type: 'postgres', builtIn: true } }, `${STORED}DATABASE_HOST=db.old.example\n`),+ ).toContain('DATABASE_PASSWORD=CustomPw123!');+ });+ it.each(HOST_SPELLINGS.database.elsewhere)('still resets it under %j in the file', host => {+ expect(written({ database: { type: 'postgres', builtIn: true } }, `${STORED}DATABASE_HOST=${host}\n`)).toContain(+ 'DATABASE_PASSWORD=openwa',+ );+ });});// The bundled MinIO is created with the keys the next boot reads, like the database password above.@@ -1446,6 +1474,21 @@ describe('InfraConfigController.saveConfig built-in/external mode flips and theexpect(env).toContain('S3_SECRET_ACCESS_KEY=minioadmin');expect(env).not.toContain('StoredKey12345');});++ const STORED = 'STORAGE_TYPE=s3\nMINIO_BUILTIN=false\nS3_BUCKET=openwa\n' + KEYS;+ it.each(HOST_SPELLINGS.storage.reach)('keeps them under %j in the file or pinned', host => {+ expect(written({ storage: { type: 's3', builtIn: true } }, `${STORED}S3_ENDPOINT=${host}\n`)).toContain(+ 'S3_SECRET_ACCESS_KEY=StoredSecret1234567890',+ );+ process.env.S3_ENDPOINT = host;+ recordPinnedEnvKeys({ S3_ENDPOINT: host });+ expect(written({ storage: { type: 's3', builtIn: true } }, STORED)).toContain('S3_ACCESS_KEY_ID=StoredKey12345');+ });+ it.each(HOST_SPELLINGS.storage.elsewhere)('still resets them under %j in the file', host => {+ expect(written({ storage: { type: 's3', builtIn: true } }, `${STORED}S3_ENDPOINT=${host}\n`)).toContain(+ 'S3_ACCESS_KEY_ID=minioadmin',+ );+ });});// The guard must evaluate what the next boot would SEE: load-env.ts loads with dotenv