mirror of
https://github.com/Dokploy/dokploy.git
synced 2026-09-12 19:51:00 +05:00
refactor: centralize destination validation
This commit is contained in:
parent
79050dd970
commit
4b753b2180
@ -1,15 +1,11 @@
|
|||||||
import {
|
import {
|
||||||
ADDITIONAL_FLAG_ERROR,
|
ADDITIONAL_FLAG_ERROR,
|
||||||
ADDITIONAL_FLAG_REGEX,
|
ADDITIONAL_FLAG_REGEX,
|
||||||
FTP_TLS_CONFLICT_ERROR,
|
getDestinationValidationIssues,
|
||||||
FTP_TLS_REQUIRED_ERROR,
|
|
||||||
getFtpTlsState,
|
getFtpTlsState,
|
||||||
hasSftpHostKeyVerification,
|
|
||||||
isNamedRcloneDestinationProvider,
|
isNamedRcloneDestinationProvider,
|
||||||
|
isRcloneDestinationProvider,
|
||||||
RCLONE_DESTINATION_PROVIDERS,
|
RCLONE_DESTINATION_PROVIDERS,
|
||||||
RCLONE_REMOTE_NAME_ERROR,
|
|
||||||
RCLONE_REMOTE_NAME_REGEX,
|
|
||||||
SFTP_HOST_KEY_REQUIRED_ERROR,
|
|
||||||
} from "@dokploy/server/db/validations/destination";
|
} from "@dokploy/server/db/validations/destination";
|
||||||
import { standardSchemaResolver as zodResolver } from "@hookform/resolvers/standard-schema";
|
import { standardSchemaResolver as zodResolver } from "@hookform/resolvers/standard-schema";
|
||||||
import { PenBoxIcon, PlusIcon, Trash2 } from "lucide-react";
|
import { PenBoxIcon, PlusIcon, Trash2 } from "lucide-react";
|
||||||
@ -72,73 +68,22 @@ const addDestination = z
|
|||||||
.optional(),
|
.optional(),
|
||||||
})
|
})
|
||||||
.superRefine((data, ctx) => {
|
.superRefine((data, ctx) => {
|
||||||
if (isNamedRcloneDestinationProvider(data.provider)) {
|
const additionalFlags = data.additionalFlags?.map((flag) => flag.value) ?? [];
|
||||||
if (!RCLONE_REMOTE_NAME_REGEX.test(data.endpoint.trim())) {
|
for (const issue of getDestinationValidationIssues({
|
||||||
ctx.addIssue({
|
provider: data.provider,
|
||||||
code: "custom",
|
accessKey: data.accessKeyId,
|
||||||
path: ["endpoint"],
|
region: data.region,
|
||||||
message: RCLONE_REMOTE_NAME_ERROR,
|
endpoint: data.endpoint,
|
||||||
});
|
additionalFlags,
|
||||||
}
|
})) {
|
||||||
return;
|
ctx.addIssue({
|
||||||
|
code: "custom",
|
||||||
|
path: [issue.field === "accessKey" ? "accessKeyId" : issue.field],
|
||||||
|
message: issue.message,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (isRcloneDestinationProvider(data.provider)) return;
|
||||||
data.provider === RCLONE_DESTINATION_PROVIDERS.FTP ||
|
|
||||||
data.provider === RCLONE_DESTINATION_PROVIDERS.SFTP
|
|
||||||
) {
|
|
||||||
if (!data.endpoint.trim()) {
|
|
||||||
ctx.addIssue({
|
|
||||||
code: "custom",
|
|
||||||
path: ["endpoint"],
|
|
||||||
message: "Host is required",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (!data.accessKeyId.trim()) {
|
|
||||||
ctx.addIssue({
|
|
||||||
code: "custom",
|
|
||||||
path: ["accessKeyId"],
|
|
||||||
message: "Username is required",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (data.region.trim()) {
|
|
||||||
const port = Number(data.region);
|
|
||||||
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
||||||
ctx.addIssue({
|
|
||||||
code: "custom",
|
|
||||||
path: ["region"],
|
|
||||||
message: "Port must be an integer between 1 and 65535",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const flags = data.additionalFlags?.map((flag) => flag.value) ?? [];
|
|
||||||
if (data.provider === RCLONE_DESTINATION_PROVIDERS.FTP) {
|
|
||||||
const { implicitTlsEnabled, explicitTlsEnabled } =
|
|
||||||
getFtpTlsState(flags);
|
|
||||||
if (!implicitTlsEnabled && !explicitTlsEnabled) {
|
|
||||||
ctx.addIssue({
|
|
||||||
code: "custom",
|
|
||||||
path: ["additionalFlags"],
|
|
||||||
message: FTP_TLS_REQUIRED_ERROR,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (implicitTlsEnabled && explicitTlsEnabled) {
|
|
||||||
ctx.addIssue({
|
|
||||||
code: "custom",
|
|
||||||
path: ["additionalFlags"],
|
|
||||||
message: FTP_TLS_CONFLICT_ERROR,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} else if (!hasSftpHostKeyVerification(flags)) {
|
|
||||||
ctx.addIssue({
|
|
||||||
code: "custom",
|
|
||||||
path: ["additionalFlags"],
|
|
||||||
message: SFTP_HOST_KEY_REQUIRED_ERROR,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const [field, label] of [
|
for (const [field, label] of [
|
||||||
["accessKeyId", "Access Key Id"],
|
["accessKeyId", "Access Key Id"],
|
||||||
@ -679,4 +624,4 @@ export const HandleDestinations = ({ destinationId }: Props) => {
|
|||||||
</DialogContent>
|
</DialogContent>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@ -6,17 +6,7 @@ import { z } from "zod";
|
|||||||
import {
|
import {
|
||||||
ADDITIONAL_FLAG_ERROR,
|
ADDITIONAL_FLAG_ERROR,
|
||||||
ADDITIONAL_FLAG_REGEX,
|
ADDITIONAL_FLAG_REGEX,
|
||||||
FTP_CERTIFICATE_VERIFICATION_REQUIRED_ERROR,
|
getDestinationValidationIssues,
|
||||||
FTP_TLS_CONFLICT_ERROR,
|
|
||||||
FTP_TLS_REQUIRED_ERROR,
|
|
||||||
getFtpTlsState,
|
|
||||||
hasDisabledFtpCertificateVerification,
|
|
||||||
hasSftpHostKeyVerification,
|
|
||||||
isNamedRcloneDestinationProvider,
|
|
||||||
RCLONE_DESTINATION_PROVIDERS,
|
|
||||||
RCLONE_REMOTE_NAME_ERROR,
|
|
||||||
RCLONE_REMOTE_NAME_REGEX,
|
|
||||||
SFTP_HOST_KEY_REQUIRED_ERROR,
|
|
||||||
} from "../validations/destination";
|
} from "../validations/destination";
|
||||||
import { organization } from "./account";
|
import { organization } from "./account";
|
||||||
import { backups } from "./backups";
|
import { backups } from "./backups";
|
||||||
@ -75,84 +65,11 @@ const validateDestination = (
|
|||||||
},
|
},
|
||||||
ctx: z.RefinementCtx,
|
ctx: z.RefinementCtx,
|
||||||
) => {
|
) => {
|
||||||
if (isNamedRcloneDestinationProvider(data.provider)) {
|
for (const issue of getDestinationValidationIssues(data)) {
|
||||||
const remoteName = data.endpoint?.trim() || "";
|
|
||||||
if (!RCLONE_REMOTE_NAME_REGEX.test(remoteName)) {
|
|
||||||
ctx.addIssue({
|
|
||||||
code: "custom",
|
|
||||||
path: ["endpoint"],
|
|
||||||
message: RCLONE_REMOTE_NAME_ERROR,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
data.provider === RCLONE_DESTINATION_PROVIDERS.FTP ||
|
|
||||||
data.provider === RCLONE_DESTINATION_PROVIDERS.SFTP
|
|
||||||
) {
|
|
||||||
if (!data.endpoint?.trim()) {
|
|
||||||
ctx.addIssue({
|
|
||||||
code: "custom",
|
|
||||||
path: ["endpoint"],
|
|
||||||
message: "Host is required",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (!data.accessKey?.trim()) {
|
|
||||||
ctx.addIssue({
|
|
||||||
code: "custom",
|
|
||||||
path: ["accessKey"],
|
|
||||||
message: "Username is required",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (data.region?.trim()) {
|
|
||||||
const port = Number(data.region);
|
|
||||||
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
||||||
ctx.addIssue({
|
|
||||||
code: "custom",
|
|
||||||
path: ["region"],
|
|
||||||
message: "Port must be an integer between 1 and 65535",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data.provider === RCLONE_DESTINATION_PROVIDERS.FTP) {
|
|
||||||
const { implicitTlsEnabled, explicitTlsEnabled } = getFtpTlsState(
|
|
||||||
data.additionalFlags,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!implicitTlsEnabled && !explicitTlsEnabled) {
|
|
||||||
ctx.addIssue({
|
|
||||||
code: "custom",
|
|
||||||
path: ["additionalFlags"],
|
|
||||||
message: FTP_TLS_REQUIRED_ERROR,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (implicitTlsEnabled && explicitTlsEnabled) {
|
|
||||||
ctx.addIssue({
|
|
||||||
code: "custom",
|
|
||||||
path: ["additionalFlags"],
|
|
||||||
message: FTP_TLS_CONFLICT_ERROR,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (hasDisabledFtpCertificateVerification(data.additionalFlags)) {
|
|
||||||
ctx.addIssue({
|
|
||||||
code: "custom",
|
|
||||||
path: ["additionalFlags"],
|
|
||||||
message: FTP_CERTIFICATE_VERIFICATION_REQUIRED_ERROR,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
data.provider === RCLONE_DESTINATION_PROVIDERS.SFTP &&
|
|
||||||
!hasSftpHostKeyVerification(data.additionalFlags)
|
|
||||||
) {
|
|
||||||
ctx.addIssue({
|
ctx.addIssue({
|
||||||
code: "custom",
|
code: "custom",
|
||||||
path: ["additionalFlags"],
|
path: [issue.field],
|
||||||
message: SFTP_HOST_KEY_REQUIRED_ERROR,
|
message: issue.message,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -104,3 +104,80 @@ export const hasSftpHostKeyVerification = (
|
|||||||
const value = knownHostsFlags[0]?.slice(prefix.length).trim() ?? "";
|
const value = knownHostsFlags[0]?.slice(prefix.length).trim() ?? "";
|
||||||
return value.length > 0 && value !== "none";
|
return value.length > 0 && value !== "none";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type DestinationValidationField =
|
||||||
|
| "endpoint"
|
||||||
|
| "accessKey"
|
||||||
|
| "region"
|
||||||
|
| "additionalFlags";
|
||||||
|
|
||||||
|
export interface DestinationValidationIssue {
|
||||||
|
field: DestinationValidationField;
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DestinationValidationInput {
|
||||||
|
provider?: string | null;
|
||||||
|
accessKey?: string;
|
||||||
|
region?: string;
|
||||||
|
endpoint?: string;
|
||||||
|
additionalFlags?: readonly string[] | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getDestinationValidationIssues = (
|
||||||
|
data: DestinationValidationInput,
|
||||||
|
): DestinationValidationIssue[] => {
|
||||||
|
const issues: DestinationValidationIssue[] = [];
|
||||||
|
const provider = data.provider;
|
||||||
|
const flags = data.additionalFlags ?? [];
|
||||||
|
|
||||||
|
if (isNamedRcloneDestinationProvider(provider)) {
|
||||||
|
if (!RCLONE_REMOTE_NAME_REGEX.test(data.endpoint?.trim() || "")) {
|
||||||
|
issues.push({ field: "endpoint", message: RCLONE_REMOTE_NAME_ERROR });
|
||||||
|
}
|
||||||
|
return issues;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
provider !== RCLONE_DESTINATION_PROVIDERS.FTP &&
|
||||||
|
provider !== RCLONE_DESTINATION_PROVIDERS.SFTP
|
||||||
|
) {
|
||||||
|
return issues;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!data.endpoint?.trim()) {
|
||||||
|
issues.push({ field: "endpoint", message: "Host is required" });
|
||||||
|
}
|
||||||
|
if (!data.accessKey?.trim()) {
|
||||||
|
issues.push({ field: "accessKey", message: "Username is required" });
|
||||||
|
}
|
||||||
|
if (data.region?.trim()) {
|
||||||
|
const port = Number(data.region);
|
||||||
|
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
||||||
|
issues.push({
|
||||||
|
field: "region",
|
||||||
|
message: "Port must be an integer between 1 and 65535",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (provider === RCLONE_DESTINATION_PROVIDERS.FTP) {
|
||||||
|
const { implicitTlsEnabled, explicitTlsEnabled } = getFtpTlsState(flags);
|
||||||
|
if (!implicitTlsEnabled && !explicitTlsEnabled) {
|
||||||
|
issues.push({ field: "additionalFlags", message: FTP_TLS_REQUIRED_ERROR });
|
||||||
|
}
|
||||||
|
if (implicitTlsEnabled && explicitTlsEnabled) {
|
||||||
|
issues.push({ field: "additionalFlags", message: FTP_TLS_CONFLICT_ERROR });
|
||||||
|
}
|
||||||
|
if (hasDisabledFtpCertificateVerification(flags)) {
|
||||||
|
issues.push({
|
||||||
|
field: "additionalFlags",
|
||||||
|
message: FTP_CERTIFICATE_VERIFICATION_REQUIRED_ERROR,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else if (!hasSftpHostKeyVerification(flags)) {
|
||||||
|
issues.push({ field: "additionalFlags", message: SFTP_HOST_KEY_REQUIRED_ERROR });
|
||||||
|
}
|
||||||
|
|
||||||
|
return issues;
|
||||||
|
};
|
||||||
|
|||||||
@ -1,16 +1,11 @@
|
|||||||
import {
|
import {
|
||||||
ADDITIONAL_FLAG_ERROR,
|
ADDITIONAL_FLAG_ERROR,
|
||||||
ADDITIONAL_FLAG_REGEX,
|
ADDITIONAL_FLAG_REGEX,
|
||||||
FTP_CERTIFICATE_VERIFICATION_REQUIRED_ERROR,
|
getDestinationValidationIssues,
|
||||||
FTP_TLS_CONFLICT_ERROR,
|
|
||||||
FTP_TLS_REQUIRED_ERROR,
|
|
||||||
getFtpTlsState,
|
getFtpTlsState,
|
||||||
hasDisabledFtpCertificateVerification,
|
|
||||||
hasSftpHostKeyVerification,
|
|
||||||
isNamedRcloneDestinationProvider,
|
isNamedRcloneDestinationProvider,
|
||||||
|
isRcloneDestinationProvider,
|
||||||
RCLONE_DESTINATION_PROVIDERS,
|
RCLONE_DESTINATION_PROVIDERS,
|
||||||
RCLONE_REMOTE_NAME_REGEX,
|
|
||||||
SFTP_HOST_KEY_REQUIRED_ERROR,
|
|
||||||
} from "@dokploy/server/db/validations/destination";
|
} from "@dokploy/server/db/validations/destination";
|
||||||
import { logger } from "@dokploy/server/lib/logger";
|
import { logger } from "@dokploy/server/lib/logger";
|
||||||
import type { BackupSchedule } from "@dokploy/server/services/backup";
|
import type { BackupSchedule } from "@dokploy/server/services/backup";
|
||||||
@ -147,15 +142,16 @@ export const getRclonePathAndFlags = async (
|
|||||||
const provider = destination.provider;
|
const provider = destination.provider;
|
||||||
const additionalFlags = getValidatedAdditionalFlags(destination);
|
const additionalFlags = getValidatedAdditionalFlags(destination);
|
||||||
|
|
||||||
|
if (isRcloneDestinationProvider(provider)) {
|
||||||
|
const [issue] = getDestinationValidationIssues(destination);
|
||||||
|
if (issue) throw new Error(issue.message);
|
||||||
|
}
|
||||||
|
|
||||||
if (isNamedRcloneDestinationProvider(provider)) {
|
if (isNamedRcloneDestinationProvider(provider)) {
|
||||||
const remoteName = destination.endpoint.trim();
|
|
||||||
if (!RCLONE_REMOTE_NAME_REGEX.test(remoteName)) {
|
|
||||||
throw new Error("Invalid rclone remote name");
|
|
||||||
}
|
|
||||||
const remotePath = joinRclonePath(destination.bucket, path);
|
const remotePath = joinRclonePath(destination.bucket, path);
|
||||||
return {
|
return {
|
||||||
flags: additionalFlags,
|
flags: additionalFlags,
|
||||||
path: `${remoteName}:${remotePath}`,
|
path: `${destination.endpoint.trim()}:${remotePath}`,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -165,25 +161,12 @@ export const getRclonePathAndFlags = async (
|
|||||||
) {
|
) {
|
||||||
const backend =
|
const backend =
|
||||||
provider === RCLONE_DESTINATION_PROVIDERS.FTP ? "ftp" : "sftp";
|
provider === RCLONE_DESTINATION_PROVIDERS.FTP ? "ftp" : "sftp";
|
||||||
|
const defaultPort =
|
||||||
let defaultPort = "22";
|
provider === RCLONE_DESTINATION_PROVIDERS.FTP
|
||||||
if (provider === RCLONE_DESTINATION_PROVIDERS.FTP) {
|
? getFtpTlsState(additionalFlags).implicitTlsEnabled
|
||||||
const { implicitTlsEnabled, explicitTlsEnabled } =
|
? "990"
|
||||||
getFtpTlsState(additionalFlags);
|
: "21"
|
||||||
if (!implicitTlsEnabled && !explicitTlsEnabled) {
|
: "22";
|
||||||
throw new Error(FTP_TLS_REQUIRED_ERROR);
|
|
||||||
}
|
|
||||||
if (implicitTlsEnabled && explicitTlsEnabled) {
|
|
||||||
throw new Error(FTP_TLS_CONFLICT_ERROR);
|
|
||||||
}
|
|
||||||
if (hasDisabledFtpCertificateVerification(additionalFlags)) {
|
|
||||||
throw new Error(FTP_CERTIFICATE_VERIFICATION_REQUIRED_ERROR);
|
|
||||||
}
|
|
||||||
defaultPort = implicitTlsEnabled ? "990" : "21";
|
|
||||||
} else if (!hasSftpHostKeyVerification(additionalFlags)) {
|
|
||||||
throw new Error(SFTP_HOST_KEY_REQUIRED_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
const port = destination.region.trim() || defaultPort;
|
const port = destination.region.trim() || defaultPort;
|
||||||
const flags = [
|
const flags = [
|
||||||
`--${backend}-host=${quote([destination.endpoint.trim()])}`,
|
`--${backend}-host=${quote([destination.endpoint.trim()])}`,
|
||||||
@ -429,4 +412,4 @@ export const getBackupCommand = (
|
|||||||
echo "[$(date)] ✅ Backup uploaded successfully" >> ${logPath};
|
echo "[$(date)] ✅ Backup uploaded successfully" >> ${logPath};
|
||||||
echo "Backup done ✅" >> ${logPath};
|
echo "Backup done ✅" >> ${logPath};
|
||||||
`;
|
`;
|
||||||
};
|
};
|
||||||
Loading…
Reference in New Issue
Block a user