Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(docker): create the built-in PostgreSQL with the saved password
Yudhi Armyndharisd2022adac787
diff --git a/src/modules/docker/compose-parity.spec.ts b/src/modules/docker/compose-parity.spec.tsindex 745a5f6dc..4586fe83f 100644--- a/src/modules/docker/compose-parity.spec.ts+++ b/src/modules/docker/compose-parity.spec.ts@@ -111,9 +111,9 @@ async function capture(profile: string, Service: typeof DockerService = DockerSedescribe('DockerService managed specs ↔ docker-compose.yml parity', () => {const compose = yaml.load(readFileSync(join(__dirname, '../../../docker-compose.yml'), 'utf8')) as ComposeFile;- // getContainerSpec reads the S3 credential env vars at call time; scrub them so the+ // getContainerSpec reads the credential env vars at call time; scrub them so the// default-fallback assertions don't depend on the developer's shell.- const S3_VARS = ['S3_ACCESS_KEY_ID', 'S3_SECRET_ACCESS_KEY', 'S3_ACCESS_KEY', 'S3_SECRET_KEY'];+ const S3_VARS = ['S3_ACCESS_KEY_ID', 'S3_SECRET_ACCESS_KEY', 'S3_ACCESS_KEY', 'S3_SECRET_KEY', 'DATABASE_PASSWORD'];let savedEnv: Record<string, string | undefined> = {};beforeEach(() => {savedEnv = {};@@ -211,7 +211,7 @@ describe('DockerService managed specs ↔ docker-compose.yml parity', () => {expect(cfg.HostConfig.PidsLimit).toBeUndefined();});- it('postgres: provisions the fixed built-in credentials; compose defaults agree on user/db only', async () => {+ it('postgres: provisions the default built-in credentials; compose defaults agree on user/db only', async () => {const cfg = await capture('postgres');expect(cfg.Env).toEqual(['POSTGRES_USER=openwa', 'POSTGRES_PASSWORD=openwa', 'POSTGRES_DB=openwa']);const env = compose.services.postgres.environment!;@@ -224,6 +224,39 @@ describe('DockerService managed specs ↔ docker-compose.yml parity', () => {expect(env.POSTGRES_PASSWORD).toBe('${DATABASE_PASSWORD:-}');});+ it('postgres: initializes the role with the password the app will dial with', async () => {+ process.env.DATABASE_PASSWORD = 'S3cure-pw-9';+ const cfg = await capture('postgres');+ expect(cfg.Env).toEqual(['POSTGRES_USER=openwa', 'POSTGRES_PASSWORD=S3cure-pw-9', 'POSTGRES_DB=openwa']);+ });++ it('postgres: provisions the password the next boot reads, not the one this process booted with', async () => {+ // A password saved from the dashboard reached process.env from data/.env.generated at boot; a later+ // save has rewritten the file, and the restart that creates the container runs in this old process.+ // Fresh modules: the precedence snapshot is global.+ const dir = mkdtempSync(join(tmpdir(), 'openwa-pg-'));+ mkdirSync(join(dir, 'data'));+ writeFileSync(join(dir, 'data', '.env.generated'), 'DATABASE_PASSWORD=S3cure-pw-9\n');+ const cwd = jest.spyOn(process, 'cwd').mockReturnValue(dir);+ try {+ let FreshDockerService: typeof DockerService = DockerService;+ jest.isolateModules(() => {+ // eslint-disable-next-line @typescript-eslint/no-require-imports+ const precedence = require('../../config/env-precedence') as typeof import('../../config/env-precedence');+ precedence.recordOsEnvKeys({});+ precedence.recordPinnedEnvKeys({});+ // eslint-disable-next-line @typescript-eslint/no-require-imports+ FreshDockerService = (require('./docker.service') as typeof import('./docker.service')).DockerService;+ });+ process.env.DATABASE_PASSWORD = 'old-boot-password';+ const cfg = await capture('postgres', FreshDockerService);+ expect(cfg.Env).toEqual(['POSTGRES_USER=openwa', 'POSTGRES_PASSWORD=S3cure-pw-9', 'POSTGRES_DB=openwa']);+ } finally {+ cwd.mockRestore();+ rmSync(dir, { recursive: true, force: true });+ }+ });+it('postgres: healthcheck resolves to the same pg_isready command as compose', async () => {const cfg = await capture('postgres');const composeTest = compose.services.postgres.healthcheck!.test;diff --git a/src/modules/docker/docker.service.ts b/src/modules/docker/docker.service.tsindex 6eb63e1a0..8e3f9b840 100644--- a/src/modules/docker/docker.service.ts+++ b/src/modules/docker/docker.service.ts@@ -275,8 +275,8 @@ export class DockerService implements OnModuleInit {* - Credentials: the compose services are the MANUAL operator path and deliberately ship no* default secret (an empty POSTGRES_PASSWORD fails when the volume is first initialized; a* MINIO_ROOT_PASSWORD left on its sub-minimum placeholder fails at every start). The specs below- * are the dashboard built-in path: they provision the fixed built-in credentials- * (openwa/openwa, minioadmin/minioadmin) that infra-config.controller writes to data/.env.generated+ * are the dashboard built-in path: they provision the built-in credentials, by default+ * (openwa/openwa, minioadmin/minioadmin), that infra-config.controller writes to data/.env.generated* and that the production boot guard (bootstrap-security.ts) exempts only while the* *_BUILTIN flag is set AND the datastore host resolves to the internal-only container.* - Postgres init script: compose bind-mounts scripts/postgres-init-schema.sh from the host@@ -333,10 +333,17 @@ export class DockerService implements OnModuleInit {image: 'postgres:16-alpine',name: 'openwa-postgres',alias: 'postgres',- // Fixed built-in credentials — the dashboard saves these same values to- // data/.env.generated (infra-config.controller) and the production boot guard exempts them only- // for the built-in, internal-host deployment (see the getContainerSpec docblock).- env: ['POSTGRES_USER=openwa', 'POSTGRES_PASSWORD=openwa', 'POSTGRES_DB=openwa'],+ // The built-in credentials — the dashboard saves them to data/.env.generated+ // (infra-config.controller) and the production boot guard exempts the default pair only for the+ // built-in, internal-host deployment (see the getContainerSpec docblock). The password is the+ // one the next boot dials with, like the MinIO keys below: a custom one saved with the built-in+ // database would otherwise never match the role this container initializes, and the app would+ // exit on its first connection.+ env: [+ 'POSTGRES_USER=openwa',+ `POSTGRES_PASSWORD=${nextBoot('DATABASE_PASSWORD') || 'openwa'}`,+ 'POSTGRES_DB=openwa',+ ],volumes: [{ name: 'openwa_postgres-data', path: '/var/lib/postgresql/data' }],healthcheck: {test: ['CMD-SHELL', 'pg_isready -U openwa'],