Skip to content

Commit 032e804

Browse files
committed
fix: remove empty routers/services traefik config instead of writing invalid yaml
Attaching Basic Auth or a Redirect to an application with no domains wrote an empty routers/services block to the app's dynamic config file. Traefik's file provider rejects that as invalid and aborts its watcher, blocking config updates for every other application. Fixes Dokploy#5189
1 parent ca3da4d commit 032e804

4 files changed

Lines changed: 134 additions & 13 deletions

File tree

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import fs from "node:fs";
2+
import os from "node:os";
3+
import path from "node:path";
4+
import { writeAppTraefikConfig } from "@dokploy/server/utils/traefik/application";
5+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
6+
7+
const mocks = vi.hoisted(() => ({
8+
execAsyncRemote: vi.fn(),
9+
}));
10+
11+
vi.mock("@dokploy/server/utils/process/execAsync", async (importOriginal) => {
12+
const actual =
13+
await importOriginal<typeof import("@dokploy/server/utils/process/execAsync")>();
14+
return {
15+
...actual,
16+
execAsyncRemote: mocks.execAsyncRemote,
17+
};
18+
});
19+
20+
describe("writeAppTraefikConfig", () => {
21+
let cwd: string;
22+
let dynamicPath: string;
23+
24+
beforeEach(() => {
25+
cwd = fs.mkdtempSync(path.join(os.tmpdir(), "dokploy-traefik-"));
26+
dynamicPath = path.join(cwd, ".docker", "traefik", "dynamic");
27+
fs.mkdirSync(dynamicPath, { recursive: true });
28+
vi.spyOn(process, "cwd").mockReturnValue(cwd);
29+
vi.clearAllMocks();
30+
});
31+
32+
afterEach(() => {
33+
vi.restoreAllMocks();
34+
fs.rmSync(cwd, { recursive: true, force: true });
35+
});
36+
37+
// Regression test for #5189: Traefik's file provider rejects a standalone
38+
// `routers: {}` / `services: {}` map and aborts its watcher for every
39+
// dynamic config once it hits one, so an app with no domains must never
40+
// get an on-disk config file at all.
41+
it("removes the file instead of writing empty routers/services", async () => {
42+
const appName = "no-domain-app";
43+
const configPath = path.join(dynamicPath, `${appName}.yml`);
44+
fs.writeFileSync(configPath, "stale content", "utf8");
45+
46+
await writeAppTraefikConfig(
47+
{ http: { routers: {}, services: {} } },
48+
appName,
49+
);
50+
51+
expect(fs.existsSync(configPath)).toBe(false);
52+
});
53+
54+
it("writes the file when routers/services are present", async () => {
55+
const appName = "with-domain-app";
56+
const configPath = path.join(dynamicPath, `${appName}.yml`);
57+
58+
await writeAppTraefikConfig(
59+
{
60+
http: {
61+
routers: { [`${appName}-router-1`]: { rule: "Host(`x`)" } },
62+
services: {},
63+
},
64+
},
65+
appName,
66+
);
67+
68+
expect(fs.existsSync(configPath)).toBe(true);
69+
});
70+
71+
it("removes the remote file instead of writing empty routers/services", async () => {
72+
mocks.execAsyncRemote.mockResolvedValue({ stdout: "", stderr: "" });
73+
74+
await writeAppTraefikConfig(
75+
{ http: { routers: {}, services: {} } },
76+
"no-domain-app",
77+
"server-id",
78+
);
79+
80+
expect(mocks.execAsyncRemote).toHaveBeenCalledOnce();
81+
const [, command] = mocks.execAsyncRemote.mock.calls[0];
82+
expect(command).toMatch(/^rm -f /);
83+
expect(command).toContain("no-domain-app.yml");
84+
});
85+
86+
it("writes the remote file when routers/services are present", async () => {
87+
mocks.execAsyncRemote.mockResolvedValue({ stdout: "", stderr: "" });
88+
89+
await writeAppTraefikConfig(
90+
{
91+
http: {
92+
routers: { "with-domain-app-router-1": { rule: "Host(`x`)" } },
93+
services: {},
94+
},
95+
},
96+
"with-domain-app",
97+
"server-id",
98+
);
99+
100+
expect(mocks.execAsyncRemote).toHaveBeenCalledOnce();
101+
const [, command] = mocks.execAsyncRemote.mock.calls[0];
102+
expect(command).toMatch(/^echo /);
103+
});
104+
});

‎packages/server/src/utils/traefik/application.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,26 @@ export const writeTraefikConfigRemote = async (
292292
}
293293
};
294294

295+
const isEmptyHttpRoutersAndServices = (traefikConfig: FileConfig) =>
296+
Object.keys(traefikConfig.http?.routers || {}).length === 0 &&
297+
Object.keys(traefikConfig.http?.services || {}).length === 0;
298+
299+
export const writeAppTraefikConfig = async (
300+
traefikConfig: FileConfig,
301+
appName: string,
302+
serverId?: string | null,
303+
) => {
304+
if (isEmptyHttpRoutersAndServices(traefikConfig)) {
305+
await removeTraefikConfig(appName, serverId);
306+
return;
307+
}
308+
if (serverId) {
309+
await writeTraefikConfigRemote(traefikConfig, appName, serverId);
310+
} else {
311+
writeTraefikConfig(traefikConfig, appName);
312+
}
313+
};
314+
295315
export const createServiceConfig = (
296316
appName: string,
297317
domain: Domain,

‎packages/server/src/utils/traefik/redirect.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { ApplicationNested } from "../builders";
33
import {
44
loadOrCreateConfig,
55
loadOrCreateConfigRemote,
6-
writeTraefikConfig,
6+
writeAppTraefikConfig,
77
writeTraefikConfigRemote,
88
} from "./application";
99
import type { FileConfig } from "./file-types";
@@ -89,11 +89,10 @@ export const createRedirectMiddleware = async (
8989

9090
if (serverId) {
9191
await writeTraefikConfigRemote(config, "middlewares", serverId);
92-
await writeTraefikConfigRemote(appConfig, appName, serverId);
9392
} else {
9493
writeMiddleware(config);
95-
writeTraefikConfig(appConfig, appName);
9694
}
95+
await writeAppTraefikConfig(appConfig, appName, serverId);
9796
};
9897

9998
export const removeRedirectMiddleware = async (
@@ -124,9 +123,8 @@ export const removeRedirectMiddleware = async (
124123

125124
if (serverId) {
126125
await writeTraefikConfigRemote(config, "middlewares", serverId);
127-
await writeTraefikConfigRemote(appConfig, appName, serverId);
128126
} else {
129-
writeTraefikConfig(appConfig, appName);
130127
writeMiddleware(config);
131128
}
129+
await writeAppTraefikConfig(appConfig, appName, serverId);
132130
};

‎packages/server/src/utils/traefik/security.ts‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { ApplicationNested } from "../builders";
44
import {
55
loadOrCreateConfig,
66
loadOrCreateConfigRemote,
7-
writeTraefikConfig,
7+
writeAppTraefikConfig,
88
writeTraefikConfigRemote,
99
} from "./application";
1010
import type {
@@ -62,11 +62,10 @@ export const createSecurityMiddleware = async (
6262
addMiddleware(appConfig, middlewareName);
6363
if (serverId) {
6464
await writeTraefikConfigRemote(config, "middlewares", serverId);
65-
await writeTraefikConfigRemote(appConfig, appName, serverId);
6665
} else {
67-
writeTraefikConfig(appConfig, appName);
6866
writeMiddleware(config);
6967
}
68+
await writeAppTraefikConfig(appConfig, appName, serverId);
7069
};
7170

7271
export const removeSecurityMiddleware = async (
@@ -89,6 +88,7 @@ export const removeSecurityMiddleware = async (
8988
appConfig = loadOrCreateConfig(appName);
9089
}
9190
const middlewareName = `auth-${appName}`;
91+
let removedLastUser = false;
9292

9393
if (config.http?.middlewares) {
9494
const currentMiddleware = config.http.middlewares[middlewareName];
@@ -106,11 +106,7 @@ export const removeSecurityMiddleware = async (
106106
delete config.http.middlewares[middlewareName];
107107
}
108108
deleteMiddleware(appConfig, middlewareName);
109-
if (serverId) {
110-
await writeTraefikConfigRemote(appConfig, appName, serverId);
111-
} else {
112-
writeTraefikConfig(appConfig, appName);
113-
}
109+
removedLastUser = true;
114110
}
115111
}
116112
}
@@ -120,6 +116,9 @@ export const removeSecurityMiddleware = async (
120116
} else {
121117
writeMiddleware(config);
122118
}
119+
if (removedLastUser) {
120+
await writeAppTraefikConfig(appConfig, appName, serverId);
121+
}
123122
};
124123

125124
const isBasicAuthMiddleware = (

0 commit comments

Comments
 (0)