Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(docker): create built-in MinIO with the credentials the next boot uses
Yudhi Armyndharis6066c7319e75
diff --git a/src/modules/docker/compose-parity.spec.ts b/src/modules/docker/compose-parity.spec.tsindex 9e52c78f9..431b5ce2e 100644--- a/src/modules/docker/compose-parity.spec.ts+++ b/src/modules/docker/compose-parity.spec.ts@@ -1,4 +1,5 @@-import { readFileSync, readdirSync } from 'fs';+import { mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'fs';+import { tmpdir } from 'os';import { join } from 'path';import { DockerService, MANAGED_DOCKER_PROFILES } from './docker.service';@@ -80,8 +81,8 @@ const labelsToMap = (list: string[]): Record<string, string> =>* Runs createService against a fake daemon and returns the exact ContainerCreateOptions the* profile's spec produces — parity is asserted on what would actually be sent to the daemon.*/-async function capture(profile: string): Promise<CapturedConfig> {- const service = new DockerService();+async function capture(profile: string, Service: typeof DockerService = DockerService): Promise<CapturedConfig> {+ const service = new Service();jest.spyOn(service, 'getContainerByService').mockResolvedValue(null);let captured: CapturedConfig | undefined;const fakeDocker = {@@ -411,6 +412,38 @@ describe('DockerService managed specs ↔ docker-compose.yml parity', () => {expect(cfg.Env).toEqual(['MINIO_ROOT_USER=canonical-user', 'MINIO_ROOT_PASSWORD=canonical-secret']);});+ it('minio: provisions the credentials the next boot reads, not the ones this process booted with', async () => {+ // External keys saved from the dashboard reached process.env from data/.env.generated at boot; a+ // switch to built-in storage has since rewritten that file with minioadmin, 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-minio-'));+ mkdirSync(join(dir, 'data'));+ writeFileSync(+ join(dir, 'data', '.env.generated'),+ 'S3_ACCESS_KEY_ID=minioadmin\nS3_SECRET_ACCESS_KEY=minioadmin\n',+ );+ const cwd = jest.spyOn(process, 'cwd').mockReturnValue(dir);+ try {+ let FreshDockerService: typeof DockerService = DockerService;+ jest.isolateModules(() => {+ // require() (not dynamic import()): the relative specifier trips TS2835 under nodenext.+ // 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.S3_ACCESS_KEY_ID = 'AKIAEXTERNAL';+ process.env.S3_SECRET_ACCESS_KEY = 'external-secret';+ const cfg = await capture('minio', FreshDockerService);+ expect(cfg.Env).toEqual(['MINIO_ROOT_USER=minioadmin', 'MINIO_ROOT_PASSWORD=minioadmin']);+ } finally {+ cwd.mockRestore();+ rmSync(dir, { recursive: true, force: true });+ }+ });+it('minio: publishes the same localhost-only ports as compose', async () => {const cfg = await capture('minio');expect(compose.services.minio.ports).toEqual(['127.0.0.1:9000:9000', '127.0.0.1:9001:9001']);diff --git a/src/modules/docker/docker.service.ts b/src/modules/docker/docker.service.tsindex d65dc0e6e..923ce93b8 100644--- a/src/modules/docker/docker.service.ts+++ b/src/modules/docker/docker.service.ts@@ -1,5 +1,7 @@import { Injectable, Logger, OnModuleInit } from '@nestjs/common';import Docker from 'dockerode';+import { isEnvPinned, isOsProvidedEnv } from '../../config/env-precedence';+import { readGeneratedEnv } from '../infra/generated-env';/*** The only Docker profiles OpenWA manages (and may start/stop). Used to bound teardown so a@@ -244,6 +246,13 @@ export class DockerService implements OnModuleInit {ports?: { container: number; host: number }[];securityOpt: string[];} | null {+ // A container outlives the process that creates it, and after a dashboard save the restart that+ // creates it runs in the OLD process, whose env still holds the values the save just replaced.+ // Resolve what the next boot reads instead: a host or project .env value pins, the saved file+ // supplies the rest. With no boot snapshot (unit tests) process.env is the only source there is.+ let saved: Record<string, string> | undefined;+ const nextBoot = (key: string): string | undefined =>+ isEnvPinned(key) || isOsProvidedEnv(key) ? process.env[key] : (saved ??= readGeneratedEnv())[key];const specs: Record<string, ReturnType<typeof this.getContainerSpec>> = {redis: {image: 'redis:7-alpine',@@ -295,8 +304,8 @@ export class DockerService implements OnModuleInit {env: [// Prefer the canonical names the app/dashboard use; fall back to the legacy ones, then the// built-in default, so the bundled MinIO and the app share credentials.- `MINIO_ROOT_USER=${process.env.S3_ACCESS_KEY_ID || process.env.S3_ACCESS_KEY || 'minioadmin'}`,- `MINIO_ROOT_PASSWORD=${process.env.S3_SECRET_ACCESS_KEY || process.env.S3_SECRET_KEY || 'minioadmin'}`,+ `MINIO_ROOT_USER=${nextBoot('S3_ACCESS_KEY_ID') || nextBoot('S3_ACCESS_KEY') || 'minioadmin'}`,+ `MINIO_ROOT_PASSWORD=${nextBoot('S3_SECRET_ACCESS_KEY') || nextBoot('S3_SECRET_KEY') || 'minioadmin'}`,],volumes: [{ name: 'openwa_minio-data', path: '/data' }],ports: [