Skip to content

Commit 577d671

Browse files
committed
Enforce container-name proxy targets
1 parent 56f1fc8 commit 577d671

4 files changed

Lines changed: 33 additions & 1 deletion

File tree

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ jobs:
2323
- run: npm ci
2424
- run: npm run check
2525
- run: npm run build
26+
- name: Validate Docker proxy target
27+
run: npm run proxy:target-check
2628
- name: Validate npmctl desired state
2729
run: npmctl validate desired-state
2830
- run: npm run docker:build

.github/workflows/deploy.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ jobs:
3131
- run: npm ci
3232
- run: npm run check
3333
- run: npm run build
34+
- name: Validate Docker proxy target
35+
run: npm run proxy:target-check
3436
- name: Validate npmctl proxy desired state
3537
run: npmctl validate desired-state/proxy.yaml
3638
- name: Check Nginx Proxy Manager API schema

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"dns:doctor": "npmctl dns doctor --provider namecheap",
1919
"proxy:validate": "npmctl validate desired-state/proxy.yaml",
2020
"proxy:plan": "npmctl plan desired-state/proxy.yaml --owner npmctl-com",
21-
"proxy:apply": "npmctl apply desired-state/proxy.yaml --owner npmctl-com"
21+
"proxy:apply": "npmctl apply desired-state/proxy.yaml --owner npmctl-com",
22+
"proxy:target-check": "node scripts/validate-proxy-target.mjs"
2223
},
2324
"dependencies": {
2425
"@mdwrk/lander-core": "^0.1.2",

scripts/validate-proxy-target.mjs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import fs from "node:fs";
2+
3+
const compose = fs.readFileSync("docker-compose.yaml", "utf8");
4+
const proxy = fs.readFileSync("desired-state/proxy.yaml", "utf8");
5+
6+
const containerMatch = compose.match(/^[ \t]*container_name:[ \t]*([^\r\n#]+)/m);
7+
if (!containerMatch) {
8+
throw new Error("docker-compose.yaml must define an explicit container_name for the site service");
9+
}
10+
11+
const containerName = containerMatch[1].trim();
12+
const forwardMatches = [...proxy.matchAll(/^[ \t]*forward_host:[ \t]*([^\r\n#]+)/gm)].map((match) => match[1].trim());
13+
if (forwardMatches.length === 0) {
14+
throw new Error("desired-state/proxy.yaml must define at least one forward_host");
15+
}
16+
17+
const ipLiteral = /^(?:\d{1,3}\.){3}\d{1,3}$/;
18+
for (const forwardHost of forwardMatches) {
19+
if (ipLiteral.test(forwardHost)) {
20+
throw new Error(`forward_host must be the Docker container name, not an IP literal: ${forwardHost}`);
21+
}
22+
if (forwardHost !== containerName) {
23+
throw new Error(`forward_host "${forwardHost}" must match docker-compose container_name "${containerName}"`);
24+
}
25+
}
26+
27+
console.log(`proxy target ok: ${containerName}`);

0 commit comments

Comments
 (0)