Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): start a bundled container its pin names in any spelling

Yudhi Armyndharis61b3c28a9ba2
diff --git a/src/modules/docker/docker.service.spec.ts b/src/modules/docker/docker.service.spec.ts
index 85b81d214..51cc858b1 100644
--- a/src/modules/docker/docker.service.spec.ts
+++ b/src/modules/docker/docker.service.spec.ts
@@ -1,6 +1,7 @@
import Docker from 'dockerode';
import { DockerService, prestartBuiltinDatabase } from './docker.service';
import { recordPinnedEnvKeys } from '../../config/env-precedence';
+import { HOST_SPELLINGS } from '../../../test/fixtures/builtin-mode-cases';
// Prevent actual Docker connections on module init during tests
jest.mock('dockerode');
@@ -315,16 +316,17 @@ describe('DockerService.onModuleInit', () => {
});
// The host the environment pins outranks the saved file at boot, so a bundled container started for a host pinned
- // elsewhere would run with nothing dialling it. A pinned bundled host still starts it.
+ // elsewhere would run with nothing dialling it. A pinned host that reaches the container in any spelling still starts
+ // it: the next boot dials it.
describe.each([
- ['openwa-postgres', 'POSTGRES_BUILTIN', { DATABASE_TYPE: 'postgres' }, 'DATABASE_HOST', 'postgres'],
+ ['openwa-postgres', 'POSTGRES_BUILTIN', { DATABASE_TYPE: 'postgres' }, 'DATABASE_HOST', 'database'],
['openwa-redis', 'REDIS_BUILTIN', { REDIS_ENABLED: 'true' }, 'REDIS_HOST', 'redis'],
- ['openwa-minio', 'MINIO_BUILTIN', { STORAGE_TYPE: 's3' }, 'S3_ENDPOINT', 'http://minio:9000'],
- ] as const)('%s with its host pinned', (container, flag, use, hostKey, bundled) => {
+ ['openwa-minio', 'MINIO_BUILTIN', { STORAGE_TYPE: 's3' }, 'S3_ENDPOINT', 'storage'],
+ ] as const)('%s with its host pinned', (container, flag, use, hostKey, service) => {
it.each([
- [bundled, true],
- ['external.example', false],
- ])('to %s is started: %s', async (host, expected) => {
+ ...HOST_SPELLINGS[service].reach.map(host => [host, true] as const),
+ ...HOST_SPELLINGS[service].elsewhere.map(host => [host, false] as const),
+ ])('to %j is started: %s', async (host, expected) => {
Object.assign(process.env, { [flag]: 'true', [hostKey]: host }, use);
recordPinnedEnvKeys({ [hostKey]: host });
const docker = happyDocker();
@@ -375,9 +377,9 @@ describe('prestartBuiltinDatabase', () => {
});
it.each([
- ['postgres', 1],
- ['db.external.example', 0],
- ])('with DATABASE_HOST pinned to %s starts the container %i times', async (host, calls) => {
+ ...HOST_SPELLINGS.database.reach.map(host => [host, 1] as const),
+ ...HOST_SPELLINGS.database.elsewhere.map(host => [host, 0] as const),
+ ])('with DATABASE_HOST pinned to %j starts the container %i times', async (host, calls) => {
recordPinnedEnvKeys({ DATABASE_HOST: host });
const service = { startBuiltinDatabase: jest.fn().mockResolvedValue(undefined) };
try {
diff --git a/src/modules/docker/docker.service.ts b/src/modules/docker/docker.service.ts
index 7e9c719ca..fe2a57b02 100644
--- a/src/modules/docker/docker.service.ts
+++ b/src/modules/docker/docker.service.ts
@@ -47,12 +47,13 @@ export function namesBundledContainer(profile: string, value: string | undefined
}
/**
- * Whether the environment (or the project .env) pins the profile's host to anything but its bundled container. The
- * pin outranks the saved file at every boot, so a container started for that profile would run with nothing using it.
+ * Whether the environment (or the project .env) pins the profile's host to anything but its bundled container, in
+ * any spelling namesBundledContainer accepts. The pin outranks the saved file at every boot, so a container started
+ * for that profile would run with nothing using it.
*/
export function hostPinnedElsewhere(profile: string, env: NodeJS.ProcessEnv = process.env): boolean {
- const [key, value] = BUNDLED_SERVICE_ENV[profile] ?? [];
- return key !== undefined && isEnvPinned(key) && env[key] !== value;
+ const [key] = BUNDLED_SERVICE_ENV[profile] ?? [];
+ return key !== undefined && isEnvPinned(key) && !namesBundledContainer(profile, env[key]);
}
interface ContainerInfo {
diff --git a/src/modules/infra/infra-config.controller.spec.ts b/src/modules/infra/infra-config.controller.spec.ts
index 9a1955e3a..68a08eb5e 100644
--- a/src/modules/infra/infra-config.controller.spec.ts
+++ b/src/modules/infra/infra-config.controller.spec.ts
@@ -38,6 +38,7 @@ import { validateEnv } from '../../config/env.validation';
import {
EXPECTED_MODE,
FILE_STATES,
+ HOST_SPELLINGS,
PIN_STATES,
SERVICES,
SERVICE_NAMES,
@@ -2055,25 +2056,14 @@ describe('InfraConfigController.requestRestart constrains teardown to managed pr
// The bundled container answers to its network alias and its container name, in any letter case and as an
// absolute DNS name with a trailing dot, so a host spelled any of these ways still reaches it.
- const SPELLINGS: Record<ServiceName, { reach: string[]; elsewhere: string[] }> = {
- database: {
- reach: ['Postgres', 'postgres.', 'openwa-postgres', ' OPENWA-POSTGRES. '],
- elsewhere: ['localhost', 'postgres-replica', 'postgres.example.com', 'openwa-postgres-2'],
- },
- redis: { reach: ['REDIS', 'redis.', 'openwa-redis'], elsewhere: ['localhost', 'redis-master', 'myredis'] },
- storage: {
- reach: ['http://MINIO:9000', 'http://minio:9000/', 'http://openwa-minio:9000', 'https://minio.:9000'],
- elsewhere: ['http://localhost:9000', 'http://minio.example.com:9000', 'minio:9000', 'not a url'],
- },
- };
describe.each(SERVICE_NAMES)('%s under another spelling of the bundled host', service => {
- for (const host of SPELLINGS[service].reach) {
+ for (const host of HOST_SPELLINGS[service].reach) {
it(`keeps it under ${JSON.stringify(host)}, in the file or pinned`, async () => {
expect((await stoppedFor(service, host, undefined)).stopped).toBe(false);
expect((await stoppedFor(service, SERVICES[service].externalHost, host)).stopped).toBe(false);
});
}
- for (const host of SPELLINGS[service].elsewhere) {
+ for (const host of HOST_SPELLINGS[service].elsewhere) {
it(`still stops it under ${JSON.stringify(host)}`, async () => {
expect((await stoppedFor(service, host, undefined)).stopped).toBe(true);
});
@@ -2158,6 +2148,28 @@ describe('InfraConfigController.requestRestart constrains teardown to managed pr
expect(orchestrateProfiles).toHaveBeenCalledTimes(starts ? 1 : 0);
expect(result.profiles).toEqual(starts ? [PROFILE_OF[service]] : []);
});
+
+ // The start rule reads a host the way the stop rule does: a spelling that reaches the bundled container is the
+ // container the next boot dials, so the restart starts it.
+ it.each([
+ ...HOST_SPELLINGS[service].reach.map(host => [host, true] as const),
+ ...HOST_SPELLINGS[service].elsewhere.map(host => [host, false] as const),
+ ])('host pinned to %j is started: %s', async (host, starts) => {
+ const key = SERVICES[service].host;
+ process.env[key] = host;
+ pinnedKeys.push(key);
+ recordPinnedEnvKeys({ [key]: host });
+ const orchestrateProfiles = jest.fn().mockResolvedValue({});
+ const controller = buildController({
+ isDockerAvailable: () => true,
+ stopManagedService: jest.fn().mockResolvedValue(true),
+ orchestrateProfiles,
+ });
+
+ const result = await controller.requestRestart({ profiles: [PROFILE_OF[service]] });
+
+ expect(result.profiles).toEqual(starts ? [PROFILE_OF[service]] : []);
+ });
});
it('leaves it out of the no-Docker signal file too', async () => {
diff --git a/test/fixtures/builtin-mode-cases.ts b/test/fixtures/builtin-mode-cases.ts
index 0d836d4a0..94c05f479 100644
--- a/test/fixtures/builtin-mode-cases.ts
+++ b/test/fixtures/builtin-mode-cases.ts
@@ -162,3 +162,31 @@ export function pinnedHost(service: ServiceName, pin: PinState): string | undefi
const spec = SERVICES[service];
return pin === 'none' ? undefined : pin === 'bundled' ? spec.bundledHost : spec.externalHost;
}
+
+/**
+ * Host spellings for each service. `reach` names the bundled container all the same: Docker's DNS answers to its
+ * network alias and its container name, in any letter case and as an absolute name with a trailing dot (for storage,
+ * the host of the endpoint URL). `elsewhere` names another server. Every decision that asks whether a host is the
+ * bundled container (start, stop, stored credentials, the built-in mode, the page's notes and restart list) must
+ * answer the same for each.
+ */
+export const HOST_SPELLINGS: Record<ServiceName, { reach: string[]; elsewhere: string[] }> = {
+ database: {
+ reach: ['postgres', 'Postgres', 'postgres.', 'openwa-postgres', ' OPENWA-POSTGRES. '],
+ elsewhere: ['localhost', 'postgres-replica', 'postgres.example.com', 'openwa-postgres-2', ''],
+ },
+ redis: {
+ reach: ['redis', 'REDIS', 'redis.', 'openwa-redis'],
+ elsewhere: ['localhost', 'redis-master', 'myredis', ''],
+ },
+ storage: {
+ reach: [
+ 'http://minio:9000',
+ 'http://MINIO:9000',
+ 'http://minio:9000/',
+ 'http://openwa-minio:9000',
+ 'https://minio.:9000',
+ ],
+ elsewhere: ['http://localhost:9000', 'http://minio.example.com:9000', 'minio:9000', 'not a url', ''],
+ },
+};