fix: validate duplicate container names and ports before deploy
Add preflight validation for container deploys and App Installer installs. Show validation failures through the existing toast alert flow.
This commit is contained in:
1 parent
dbc819583b
commit
be15285f34
6 files changed
+425
-2
No files matched your search
+158
-1
@@ -1,6 +1,6 @@
|
||||
import { FastifyInstance } from "fastify";
|
||||
import { z } from "zod";
|
||||
import { $Enums } from "@prisma/client";
|
||||
import { $Enums, type Server } from "@prisma/client";
|
||||
import prisma from "../lib/prisma";
|
||||
import { authenticate, requireApiKeyPermission } from "../middleware/auth";
|
||||
import { auditLog } from "../services/audit.service";
|
||||
@@ -147,6 +147,43 @@ function parseCsvBindings(value: string): string[] {
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
function normalizeContainerNameForConflict(value: string) {
|
||||
return value.trim().replace(/^\//, "").toLowerCase();
|
||||
}
|
||||
|
||||
function extractHostPortFromPortMapping(value: string) {
|
||||
const trimmed = value.trim().replace(/^['"]|['"]$/g, "");
|
||||
if (!trimmed) return null;
|
||||
|
||||
const dockerPsMatch = trimmed.match(/(?:^|:)(\d+)->/);
|
||||
if (dockerPsMatch?.[1]) return dockerPsMatch[1];
|
||||
|
||||
const mapping = trimmed.split("/")[0]?.trim() ?? trimmed;
|
||||
const parts = mapping.split(":").filter(Boolean);
|
||||
if (parts.length < 2) return null;
|
||||
|
||||
const hostPort = parts[parts.length - 2] ?? "";
|
||||
return /^\d+$/.test(hostPort) ? hostPort : null;
|
||||
}
|
||||
|
||||
function extractHostPortsFromPortMappings(value?: string | null) {
|
||||
return parseCsvBindings(value ?? "")
|
||||
.map(extractHostPortFromPortMapping)
|
||||
.filter((port): port is string => Boolean(port));
|
||||
}
|
||||
|
||||
function extractHostPortsFromStoredPorts(value: unknown) {
|
||||
const ports = Array.isArray(value)
|
||||
? value.filter((entry): entry is string => typeof entry === "string")
|
||||
: typeof value === "string"
|
||||
? parseCsvBindings(value)
|
||||
: [];
|
||||
|
||||
return ports
|
||||
.map(extractHostPortFromPortMapping)
|
||||
.filter((port): port is string => Boolean(port));
|
||||
}
|
||||
|
||||
function parseEnvironmentVariables(env: string): string[] {
|
||||
return env
|
||||
.split("\n")
|
||||
@@ -249,6 +286,110 @@ async function upsertAppInstallerContainer(input: {
|
||||
});
|
||||
}
|
||||
|
||||
async function assertAppInstallTargetAvailable(input: {
|
||||
server: Server;
|
||||
containerName: string;
|
||||
ports: string;
|
||||
}) {
|
||||
const requestedName = normalizeContainerNameForConflict(input.containerName);
|
||||
const requestedHostPorts = extractHostPortsFromPortMappings(input.ports);
|
||||
|
||||
const seenHostPorts = new Set<string>();
|
||||
const duplicateRequestedPort = requestedHostPorts.find((port) => {
|
||||
if (seenHostPorts.has(port)) return true;
|
||||
seenHostPorts.add(port);
|
||||
return false;
|
||||
});
|
||||
|
||||
if (duplicateRequestedPort) {
|
||||
throw new Error(
|
||||
`Host port ${duplicateRequestedPort} is mapped more than once in this install.`,
|
||||
);
|
||||
}
|
||||
|
||||
const runtimeContainers = await ssh.listDockerContainers(input.server);
|
||||
for (const container of runtimeContainers) {
|
||||
if (
|
||||
normalizeContainerNameForConflict(container.name) === requestedName
|
||||
) {
|
||||
throw new Error(
|
||||
`Container name "${input.containerName}" is already used on the selected server.`,
|
||||
);
|
||||
}
|
||||
|
||||
const conflictingPort = requestedHostPorts.find((port) =>
|
||||
extractHostPortsFromPortMappings(container.ports).includes(port),
|
||||
);
|
||||
if (conflictingPort) {
|
||||
throw new Error(
|
||||
`Host port ${conflictingPort} is already used by container "${container.name}" on the selected server.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const containers = await prisma.container.findMany({
|
||||
where: {
|
||||
serverId: input.server.id,
|
||||
status: { not: "ERROR" },
|
||||
},
|
||||
select: {
|
||||
name: true,
|
||||
ports: true,
|
||||
},
|
||||
});
|
||||
|
||||
for (const container of containers) {
|
||||
if (
|
||||
normalizeContainerNameForConflict(container.name) === requestedName
|
||||
) {
|
||||
throw new Error(
|
||||
`Container name "${input.containerName}" is already used on the selected server.`,
|
||||
);
|
||||
}
|
||||
|
||||
const conflictingPort = requestedHostPorts.find((port) =>
|
||||
extractHostPortsFromStoredPorts(container.ports).includes(port),
|
||||
);
|
||||
if (conflictingPort) {
|
||||
throw new Error(
|
||||
`Host port ${conflictingPort} is already used by container "${container.name}" on the selected server.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const activeInstalls = await prisma.appInstall.findMany({
|
||||
where: {
|
||||
serverId: input.server.id,
|
||||
status: { notIn: ["FAILED", "REMOVED"] },
|
||||
},
|
||||
select: {
|
||||
appName: true,
|
||||
containerName: true,
|
||||
port: true,
|
||||
},
|
||||
});
|
||||
|
||||
for (const install of activeInstalls) {
|
||||
if (
|
||||
install.containerName &&
|
||||
normalizeContainerNameForConflict(install.containerName) === requestedName
|
||||
) {
|
||||
throw new Error(
|
||||
`Container name "${input.containerName}" is already used by app install "${install.appName}" on the selected server.`,
|
||||
);
|
||||
}
|
||||
|
||||
const conflictingPort = requestedHostPorts.find((port) =>
|
||||
extractHostPortsFromPortMappings(install.port ?? "").includes(port),
|
||||
);
|
||||
if (conflictingPort) {
|
||||
throw new Error(
|
||||
`Host port ${conflictingPort} is already used by app install "${install.appName}" on the selected server.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function classifyRuntimeError(
|
||||
message: string,
|
||||
): "CONTAINER_NOT_FOUND" | "INSPECT_FAILED" | "RUNTIME_UNAVAILABLE" {
|
||||
@@ -888,6 +1029,22 @@ export async function appsRoutes(app: FastifyInstance) {
|
||||
finalVolumes;
|
||||
}
|
||||
|
||||
try {
|
||||
await assertAppInstallTargetAvailable({
|
||||
server,
|
||||
containerName,
|
||||
ports: finalPorts,
|
||||
});
|
||||
} catch (error) {
|
||||
return reply.status(400).send({
|
||||
success: false,
|
||||
error:
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: "Failed to validate app install target",
|
||||
});
|
||||
}
|
||||
|
||||
const install = await prisma.appInstall.create({
|
||||
data: {
|
||||
appId: finalAppId,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { FastifyInstance, FastifyReply } from "fastify";
|
||||
import yaml from "js-yaml";
|
||||
import { posix as pathPosix } from "path";
|
||||
import { z } from "zod";
|
||||
import prisma from "../lib/prisma";
|
||||
@@ -430,6 +431,202 @@ function parseDockerPorts(ports: string): string[] {
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
function extractHostPortFromPortMapping(
|
||||
value: string,
|
||||
options: { allowSinglePortAsHost?: boolean } = {},
|
||||
) {
|
||||
const trimmed = value.trim().replace(/^['"]|['"]$/g, "");
|
||||
if (!trimmed) return null;
|
||||
|
||||
const dockerPsMatch = trimmed.match(/(?:^|:)(\d+)->/);
|
||||
if (dockerPsMatch?.[1]) return dockerPsMatch[1];
|
||||
|
||||
const mapping = trimmed.split("/")[0]?.trim() ?? trimmed;
|
||||
const parts = mapping.split(":").filter(Boolean);
|
||||
|
||||
if (parts.length === 1) {
|
||||
return options.allowSinglePortAsHost && /^\d+$/.test(parts[0])
|
||||
? parts[0]
|
||||
: null;
|
||||
}
|
||||
|
||||
const hostPort = parts[parts.length - 2] ?? "";
|
||||
return /^\d+$/.test(hostPort) ? hostPort : null;
|
||||
}
|
||||
|
||||
function extractHostPortsFromPortMappings(
|
||||
ports: string,
|
||||
options: { allowSinglePortAsHost?: boolean } = {},
|
||||
) {
|
||||
return ports
|
||||
.split(",")
|
||||
.map((entry) => extractHostPortFromPortMapping(entry, options))
|
||||
.filter((port): port is string => Boolean(port));
|
||||
}
|
||||
|
||||
function extractHostPortsFromStoredPorts(value: unknown) {
|
||||
const ports = Array.isArray(value)
|
||||
? value.filter((entry): entry is string => typeof entry === "string")
|
||||
: typeof value === "string"
|
||||
? parseDockerPorts(value)
|
||||
: [];
|
||||
|
||||
return ports
|
||||
.map((entry) => extractHostPortFromPortMapping(entry))
|
||||
.filter((port): port is string => Boolean(port));
|
||||
}
|
||||
|
||||
function extractComposePublishedHostPorts(composeContent?: string | null) {
|
||||
const content = toOptionalValue(composeContent);
|
||||
if (!content) return [];
|
||||
|
||||
let parsed: unknown;
|
||||
try {
|
||||
parsed = yaml.load(content);
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (typeof parsed !== "object" || parsed === null || !("services" in parsed)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const services = (parsed as { services?: unknown }).services;
|
||||
if (typeof services !== "object" || services === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const hostPorts: string[] = [];
|
||||
for (const service of Object.values(services)) {
|
||||
if (typeof service !== "object" || service === null) continue;
|
||||
|
||||
const ports = (service as { ports?: unknown }).ports;
|
||||
if (!Array.isArray(ports)) continue;
|
||||
|
||||
for (const entry of ports) {
|
||||
if (typeof entry === "string") {
|
||||
const hostPort = extractHostPortFromPortMapping(entry);
|
||||
if (hostPort) hostPorts.push(hostPort);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (typeof entry !== "object" || entry === null) continue;
|
||||
|
||||
const published = (entry as { published?: unknown }).published;
|
||||
const hostPort =
|
||||
typeof published === "number" || typeof published === "string"
|
||||
? String(published).trim()
|
||||
: "";
|
||||
if (/^\d+$/.test(hostPort)) hostPorts.push(hostPort);
|
||||
}
|
||||
}
|
||||
|
||||
return hostPorts;
|
||||
}
|
||||
|
||||
function collectRequestedHostPorts(input: z.infer<typeof DeploySchema>) {
|
||||
const hostPorts = [
|
||||
...extractHostPortsFromPortMappings(input.ports),
|
||||
...extractHostPortsFromPortMappings(input.portOverride ?? "", {
|
||||
allowSinglePortAsHost: true,
|
||||
}),
|
||||
];
|
||||
|
||||
if (input.sourceType === "MANUAL" && input.deployMode === "COMPOSE") {
|
||||
hostPorts.push(...extractComposePublishedHostPorts(input.composeContent));
|
||||
}
|
||||
|
||||
return hostPorts;
|
||||
}
|
||||
|
||||
async function assertRequestedHostPortsAvailable(input: {
|
||||
deployInput: z.infer<typeof DeploySchema>;
|
||||
serverId: string;
|
||||
}) {
|
||||
const requestedHostPorts = collectRequestedHostPorts(input.deployInput);
|
||||
if (requestedHostPorts.length === 0) return;
|
||||
|
||||
const seen = new Set<string>();
|
||||
const duplicateRequestedPort = requestedHostPorts.find((port) => {
|
||||
if (seen.has(port)) return true;
|
||||
seen.add(port);
|
||||
return false;
|
||||
});
|
||||
|
||||
if (duplicateRequestedPort) {
|
||||
throw new Error(
|
||||
`Host port ${duplicateRequestedPort} is mapped more than once in this deployment.`,
|
||||
);
|
||||
}
|
||||
|
||||
const containers = await prisma.container.findMany({
|
||||
where: {
|
||||
serverId: input.serverId,
|
||||
status: { not: "ERROR" },
|
||||
},
|
||||
select: {
|
||||
name: true,
|
||||
ports: true,
|
||||
},
|
||||
});
|
||||
|
||||
for (const container of containers) {
|
||||
const usedHostPorts = extractHostPortsFromStoredPorts(container.ports);
|
||||
const conflictingPort = requestedHostPorts.find((port) =>
|
||||
usedHostPorts.includes(port),
|
||||
);
|
||||
|
||||
if (conflictingPort) {
|
||||
throw new Error(
|
||||
`Host port ${conflictingPort} is already used by container "${container.name}" on the selected server.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function shouldValidateRequestedContainerName(
|
||||
input: z.infer<typeof DeploySchema>,
|
||||
) {
|
||||
const gitBuildType = resolveGitBuildType(input);
|
||||
return input.deployMode !== "COMPOSE" && gitBuildType !== "COMPOSE";
|
||||
}
|
||||
|
||||
function normalizeContainerNameForConflict(value: string) {
|
||||
return value.trim().replace(/^\//, "").toLowerCase();
|
||||
}
|
||||
|
||||
async function assertRequestedContainerNameAvailable(input: {
|
||||
deployInput: z.infer<typeof DeploySchema>;
|
||||
serverId: string;
|
||||
}) {
|
||||
if (!shouldValidateRequestedContainerName(input.deployInput)) return;
|
||||
|
||||
const requestedName = normalizeContainerNameForConflict(
|
||||
input.deployInput.name,
|
||||
);
|
||||
if (!requestedName) return;
|
||||
|
||||
const containers = await prisma.container.findMany({
|
||||
where: {
|
||||
serverId: input.serverId,
|
||||
status: { not: "ERROR" },
|
||||
},
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
});
|
||||
const conflict = containers.find(
|
||||
(container) =>
|
||||
normalizeContainerNameForConflict(container.name) === requestedName,
|
||||
);
|
||||
|
||||
if (conflict) {
|
||||
throw new Error(
|
||||
`Container name "${input.deployInput.name}" is already used on the selected server.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function parseEnvironmentVariables(env: string): string[] {
|
||||
return env
|
||||
.split("\n")
|
||||
@@ -2654,6 +2851,45 @@ export async function containerRoutes(app: FastifyInstance) {
|
||||
}
|
||||
}
|
||||
|
||||
const shouldValidateHostPorts =
|
||||
collectRequestedHostPorts(body.data).length > 0;
|
||||
const shouldValidateContainerName =
|
||||
shouldValidateRequestedContainerName(body.data);
|
||||
|
||||
if (shouldValidateHostPorts || shouldValidateContainerName) {
|
||||
try {
|
||||
await syncContainersForServers([server], req.userId, (event) =>
|
||||
recordDockerCommandTiming(app, event),
|
||||
);
|
||||
|
||||
if (shouldValidateContainerName) {
|
||||
await assertRequestedContainerNameAvailable({
|
||||
deployInput: body.data,
|
||||
serverId,
|
||||
});
|
||||
}
|
||||
|
||||
if (shouldValidateHostPorts) {
|
||||
await assertRequestedHostPortsAvailable({
|
||||
deployInput: body.data,
|
||||
serverId,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
const errorMessage = redactSecret(
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: "Failed to validate requested deployment target",
|
||||
accessToken,
|
||||
);
|
||||
|
||||
return reply.status(400).send({
|
||||
success: false,
|
||||
error: errorMessage,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (sourceType === "MANUAL" && deployMode === "IMAGE") {
|
||||
const image = toOptionalValue(rest.image)!;
|
||||
|
||||
|
||||
Reference in new issue
Block a user