266 lines
8.5 KiB
JavaScript
266 lines
8.5 KiB
JavaScript
#!/usr/bin/env node
|
|
import { createHash } from "node:crypto";
|
|
import { spawnSync } from "node:child_process";
|
|
import {
|
|
cp,
|
|
lstat,
|
|
mkdir,
|
|
mkdtemp,
|
|
readFile,
|
|
readdir,
|
|
rm,
|
|
writeFile,
|
|
} from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { dirname, join, relative, resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const scriptDir = dirname(fileURLToPath(import.meta.url));
|
|
const platformRoot = resolve(scriptDir, "../..");
|
|
const sourceRoot = resolve(platformRoot, "device-plane");
|
|
const artifactDir = resolve(
|
|
process.env.NODEDC_DEPLOY_ARTIFACT_DIR
|
|
|| resolve(scriptDir, "../deploy-artifacts"),
|
|
);
|
|
const [
|
|
patchId = "device-edge-admission-gate-20260804-002",
|
|
...extra
|
|
] = process.argv.slice(2);
|
|
|
|
if (extra.length || !/^[A-Za-z0-9._-]{1,96}$/.test(patchId)) {
|
|
throw new Error(
|
|
"usage: build-device-edge-ingress-artifact.mjs [patch-id]",
|
|
);
|
|
}
|
|
|
|
const files = [
|
|
"docker-compose.device-edge.yml",
|
|
"docker-compose.device-edge.ingress.yml",
|
|
"services/device-edge-relay/Dockerfile",
|
|
"services/device-edge-relay/src",
|
|
"deployment/device-edge-admission-gate-v1.json",
|
|
];
|
|
const ignoredBasenames = new Set([".DS_Store", ".git", "node_modules"]);
|
|
const descriptor = await assertBoundary();
|
|
if (descriptor.ingressIpv4Approval !== "approved-outside-dhcp-pool") {
|
|
throw new Error("device_edge_ingress_ipv4_approval_pending");
|
|
}
|
|
|
|
const stage = await mkdtemp(join(tmpdir(), "nodedc-device-edge-ingress-"));
|
|
const payload = join(stage, "payload");
|
|
const target = join(artifactDir, `nodedc-device-edge-${patchId}.tgz`);
|
|
|
|
try {
|
|
await mkdir(payload, { recursive: true });
|
|
for (const sourceRelative of files) {
|
|
await copySafe(
|
|
resolve(sourceRoot, sourceRelative),
|
|
join(payload, sourceRelative),
|
|
);
|
|
}
|
|
await writeFile(
|
|
join(stage, "manifest.env"),
|
|
`id=${patchId}\ncomponent=device-edge\ntype=app-overlay\n`,
|
|
"utf8",
|
|
);
|
|
await writeFile(join(stage, "files.txt"), `${files.join("\n")}\n`, "utf8");
|
|
await mkdir(artifactDir, { recursive: true });
|
|
|
|
const tar = spawnSync(
|
|
"python3",
|
|
["-c", canonicalTarScript(), target, stage],
|
|
{ encoding: "utf8", maxBuffer: 128 * 1024 * 1024 },
|
|
);
|
|
if (tar.status !== 0) {
|
|
throw new Error(`tar_failed:${tar.stderr || tar.stdout}`);
|
|
}
|
|
|
|
const digest = createHash("sha256")
|
|
.update(await readFile(target))
|
|
.digest("hex");
|
|
console.log(JSON.stringify({
|
|
ok: true,
|
|
patchId,
|
|
artifact: target,
|
|
sha256: digest,
|
|
component: "device-edge",
|
|
transition: "reviewed-ipvlan-b2-relay-admission-gate",
|
|
entries: files,
|
|
services: ["device-edge-relay"],
|
|
preservedRuntime: ["device-edge-backhaul", "tailnet", "Gelios"],
|
|
ingress: {
|
|
parent: descriptor.parentInterface,
|
|
subnet: descriptor.lanSubnet,
|
|
gateway: descriptor.lanGateway,
|
|
ipv4: descriptor.ingressIpv4,
|
|
ipv4Approval: descriptor.ingressIpv4Approval,
|
|
tcp: 9921,
|
|
hostPortPublication: "disabled",
|
|
sourceAdmission: descriptor.sourceAdmission,
|
|
maxTrackedSourceAddresses: descriptor.maxTrackedSourceAddresses,
|
|
maxBytesPerDirection: descriptor.maxBytesPerDirection,
|
|
lifecycle: "quarantine",
|
|
commandTransport: "disabled",
|
|
},
|
|
rollback: descriptor.rollback,
|
|
}, null, 2));
|
|
} finally {
|
|
await rm(stage, { recursive: true, force: true });
|
|
}
|
|
|
|
async function assertBoundary() {
|
|
const baseline = await readFile(
|
|
resolve(sourceRoot, "docker-compose.device-edge.yml"),
|
|
"utf8",
|
|
);
|
|
const ingress = await readFile(
|
|
resolve(sourceRoot, "docker-compose.device-edge.ingress.yml"),
|
|
"utf8",
|
|
);
|
|
const descriptor = JSON.parse(await readFile(
|
|
resolve(
|
|
sourceRoot,
|
|
"deployment/device-edge-admission-gate-v1.json",
|
|
),
|
|
"utf8",
|
|
));
|
|
|
|
for (const fragment of [
|
|
"DEVICE_EDGE_RELAY_HEALTH_HOST: 127.0.0.1",
|
|
'DEVICE_EDGE_RELAY_INGRESS_ENABLED: "false"',
|
|
"read_only: true",
|
|
'user: "1000:1000"',
|
|
"no-new-privileges:true",
|
|
"cap_drop:",
|
|
"- ALL",
|
|
]) {
|
|
if (!baseline.includes(fragment)) {
|
|
throw new Error(`device_edge_baseline_boundary_missing:${fragment}`);
|
|
}
|
|
}
|
|
for (const forbidden of ["ports:", "device-edge-control"]){
|
|
if (baseline.includes(forbidden)) {
|
|
throw new Error(`device_edge_baseline_boundary_violation:${forbidden}`);
|
|
}
|
|
}
|
|
|
|
for (const fragment of [
|
|
'DEVICE_EDGE_RELAY_INGRESS_ENABLED: "true"',
|
|
"DEVICE_EDGE_RELAY_UPSTREAM_HOST: device-edge-backhaul",
|
|
'DEVICE_EDGE_RELAY_UPSTREAM_PORT: "19921"',
|
|
"DEVICE_EDGE_RELAY_SOURCE_POLICY: public-ipv4-only",
|
|
'DEVICE_EDGE_RELAY_MAX_TRACKED_SOURCE_ADDRESSES: "2048"',
|
|
'DEVICE_EDGE_RELAY_MAX_BYTES_PER_DIRECTION: "262144"',
|
|
"name: nodedc-device-edge-ingress",
|
|
"driver: ipvlan",
|
|
"parent: enp1s0f0",
|
|
"ipvlan_mode: l2",
|
|
"ipv4_address: 192.168.71.253",
|
|
"gw_priority: 100",
|
|
"subnet: 192.168.68.0/22",
|
|
"gateway: 192.168.68.1",
|
|
]) {
|
|
if (!ingress.includes(fragment)) {
|
|
throw new Error(`device_edge_ingress_boundary_missing:${fragment}`);
|
|
}
|
|
}
|
|
for (const forbidden of [
|
|
"ports:",
|
|
"network_mode: host",
|
|
"privileged: true",
|
|
"DEVICE_EDGE_RELAY_COMMAND",
|
|
"0.0.0.0:9921:9921",
|
|
]) {
|
|
if (ingress.includes(forbidden)) {
|
|
throw new Error(`device_edge_ingress_boundary_violation:${forbidden}`);
|
|
}
|
|
}
|
|
|
|
const expected = {
|
|
schemaVersion: "nodedc.device-edge.admission-gate.v1",
|
|
mode: "single-nic-ipvlan-b2-relay-only",
|
|
runtimeHost: "ndcmini12",
|
|
component: "device-edge",
|
|
selectedServices: ["device-edge-relay"],
|
|
preservedServices: ["device-edge-backhaul", "tailnet"],
|
|
composeProject: "nodedc-device-edge",
|
|
composeFiles: [
|
|
"docker-compose.device-edge.yml",
|
|
"docker-compose.device-edge.ingress.yml",
|
|
],
|
|
parentInterface: "enp1s0f0",
|
|
lanSubnet: "192.168.68.0/22",
|
|
lanGateway: "192.168.68.1",
|
|
ingressIpv4: "192.168.71.253",
|
|
ingressIpv4Approval: "approved-outside-dhcp-pool",
|
|
ingressNetwork: "nodedc-device-edge-ingress",
|
|
deviceTcpListen: "192.168.71.253:9921",
|
|
hostPortPublication: "disabled",
|
|
healthPublication: "disabled",
|
|
privateUpstream: "device-edge-backhaul:19921",
|
|
sourceAdmission: "public-ipv4-only",
|
|
maxTrackedSourceAddresses: 2048,
|
|
maxBytesPerDirection: 262144,
|
|
protocolInspection: "gateway-owned",
|
|
identityTrust: "claimed-not-ownership-proof",
|
|
discoveryLifecycle: "quarantine",
|
|
commandTransport: "disabled",
|
|
gelios: "untouched",
|
|
amneziaHostFullTunnel: "preserved",
|
|
routerNatFirewall: "separate-manual-gate",
|
|
rollback: "restore-reviewed-ipvlan-predecessor-without-network-or-router-mutation",
|
|
};
|
|
if (JSON.stringify(descriptor) !== JSON.stringify(expected)) {
|
|
throw new Error("device_edge_ingress_descriptor_mismatch");
|
|
}
|
|
return descriptor;
|
|
}
|
|
|
|
function canonicalTarScript() {
|
|
return [
|
|
"import gzip,io,pathlib,sys,tarfile",
|
|
"root=pathlib.Path(sys.argv[2])",
|
|
"with open(sys.argv[1],'wb') as out:",
|
|
" with gzip.GzipFile(filename='',mode='wb',fileobj=out,compresslevel=9,mtime=0) as gz:",
|
|
" with tarfile.open(fileobj=gz,mode='w',format=tarfile.PAX_FORMAT) as tar:",
|
|
" for top in ('manifest.env','files.txt','payload'):",
|
|
" p=root/top; paths=[p]+(sorted(p.rglob('*')) if p.is_dir() else [])",
|
|
" for x in paths:",
|
|
" info=tar.gettarinfo(str(x),arcname=x.relative_to(root).as_posix())",
|
|
" info.uid=info.gid=0; info.uname=info.gname='root'; info.mtime=0; info.mode=0o755 if info.isdir() else 0o644",
|
|
" with (open(x,'rb') if info.isfile() else io.BytesIO()) as src: tar.addfile(info,src if info.isfile() else None)",
|
|
].join("\n");
|
|
}
|
|
|
|
async function copySafe(source, destination) {
|
|
const sourceStat = await lstat(source);
|
|
if (sourceStat.isSymbolicLink()) {
|
|
throw new Error(`source_symlink_rejected:${relative(sourceRoot, source)}`);
|
|
}
|
|
if (sourceStat.isFile()) {
|
|
await mkdir(dirname(destination), { recursive: true });
|
|
await cp(source, destination, { force: true, verbatimSymlinks: true });
|
|
return;
|
|
}
|
|
if (!sourceStat.isDirectory()) {
|
|
throw new Error(`source_type_rejected:${source}`);
|
|
}
|
|
await mkdir(destination, { recursive: true });
|
|
for (const entry of await readdir(source, { withFileTypes: true })) {
|
|
if (
|
|
ignoredBasenames.has(entry.name)
|
|
|| entry.name.startsWith(".env")
|
|
) {
|
|
continue;
|
|
}
|
|
const childSource = join(source, entry.name);
|
|
const childDestination = join(destination, entry.name);
|
|
if (entry.isSymbolicLink()) {
|
|
throw new Error(
|
|
`source_symlink_rejected:${relative(sourceRoot, childSource)}`,
|
|
);
|
|
}
|
|
await copySafe(childSource, childDestination);
|
|
}
|
|
}
|