mirror of
https://github.com/andwati/dokploy.git
synced 2026-11-23 20:28:47 +00:00
feat(registry): enhance registry handling with optional password and new test functionality
- Updated the AddRegistrySchema to make the password field optional when editing an existing registry. - Introduced a new mutation, testRegistryById, to validate registry credentials using existing data. - Improved form handling to conditionally require the password based on the editing state. - Enhanced user feedback for registry testing with clearer error messages and instructions.
This commit is contained in:
@@ -42,9 +42,7 @@ const AddRegistrySchema = z.object({
|
|||||||
username: z.string().min(1, {
|
username: z.string().min(1, {
|
||||||
message: "Username is required",
|
message: "Username is required",
|
||||||
}),
|
}),
|
||||||
password: z.string().min(1, {
|
password: z.string(),
|
||||||
message: "Password is required",
|
|
||||||
}),
|
|
||||||
registryUrl: z
|
registryUrl: z
|
||||||
.string()
|
.string()
|
||||||
.optional()
|
.optional()
|
||||||
@@ -75,6 +73,7 @@ const AddRegistrySchema = z.object({
|
|||||||
),
|
),
|
||||||
imagePrefix: z.string(),
|
imagePrefix: z.string(),
|
||||||
serverId: z.string().optional(),
|
serverId: z.string().optional(),
|
||||||
|
isEditing: z.boolean().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
type AddRegistry = z.infer<typeof AddRegistrySchema>;
|
type AddRegistry = z.infer<typeof AddRegistrySchema>;
|
||||||
@@ -108,6 +107,12 @@ export const HandleRegistry = ({ registryId }: Props) => {
|
|||||||
error: testRegistryError,
|
error: testRegistryError,
|
||||||
isError: testRegistryIsError,
|
isError: testRegistryIsError,
|
||||||
} = api.registry.testRegistry.useMutation();
|
} = api.registry.testRegistry.useMutation();
|
||||||
|
const {
|
||||||
|
mutateAsync: testRegistryById,
|
||||||
|
isLoading: isLoadingById,
|
||||||
|
error: testRegistryByIdError,
|
||||||
|
isError: testRegistryByIdIsError,
|
||||||
|
} = api.registry.testRegistryById.useMutation();
|
||||||
const form = useForm<AddRegistry>({
|
const form = useForm<AddRegistry>({
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
username: "",
|
username: "",
|
||||||
@@ -116,8 +121,23 @@ export const HandleRegistry = ({ registryId }: Props) => {
|
|||||||
imagePrefix: "",
|
imagePrefix: "",
|
||||||
registryName: "",
|
registryName: "",
|
||||||
serverId: "",
|
serverId: "",
|
||||||
|
isEditing: !!registryId,
|
||||||
},
|
},
|
||||||
resolver: zodResolver(AddRegistrySchema),
|
resolver: zodResolver(
|
||||||
|
AddRegistrySchema.refine(
|
||||||
|
(data) => {
|
||||||
|
// When creating a new registry, password is required
|
||||||
|
if (!data.isEditing && (!data.password || data.password.length === 0)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "Password is required",
|
||||||
|
path: ["password"],
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
});
|
});
|
||||||
|
|
||||||
const password = form.watch("password");
|
const password = form.watch("password");
|
||||||
@@ -138,6 +158,7 @@ export const HandleRegistry = ({ registryId }: Props) => {
|
|||||||
registryUrl: registry.registryUrl,
|
registryUrl: registry.registryUrl,
|
||||||
imagePrefix: registry.imagePrefix || "",
|
imagePrefix: registry.imagePrefix || "",
|
||||||
registryName: registry.registryName,
|
registryName: registry.registryName,
|
||||||
|
isEditing: true,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
form.reset({
|
form.reset({
|
||||||
@@ -146,13 +167,13 @@ export const HandleRegistry = ({ registryId }: Props) => {
|
|||||||
registryUrl: "",
|
registryUrl: "",
|
||||||
imagePrefix: "",
|
imagePrefix: "",
|
||||||
serverId: "",
|
serverId: "",
|
||||||
|
isEditing: false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, [form, form.reset, form.formState.isSubmitSuccessful, registry]);
|
}, [form, form.reset, form.formState.isSubmitSuccessful, registry]);
|
||||||
|
|
||||||
const onSubmit = async (data: AddRegistry) => {
|
const onSubmit = async (data: AddRegistry) => {
|
||||||
await mutateAsync({
|
const payload: any = {
|
||||||
password: data.password,
|
|
||||||
registryName: data.registryName,
|
registryName: data.registryName,
|
||||||
username: data.username,
|
username: data.username,
|
||||||
registryUrl: data.registryUrl || "",
|
registryUrl: data.registryUrl || "",
|
||||||
@@ -160,7 +181,15 @@ export const HandleRegistry = ({ registryId }: Props) => {
|
|||||||
imagePrefix: data.imagePrefix,
|
imagePrefix: data.imagePrefix,
|
||||||
serverId: data.serverId,
|
serverId: data.serverId,
|
||||||
registryId: registryId || "",
|
registryId: registryId || "",
|
||||||
})
|
};
|
||||||
|
|
||||||
|
// Only include password if it's been provided (not empty)
|
||||||
|
// When editing, empty password means "keep the existing password"
|
||||||
|
if (data.password && data.password.length > 0) {
|
||||||
|
payload.password = data.password;
|
||||||
|
}
|
||||||
|
|
||||||
|
await mutateAsync(payload)
|
||||||
.then(async (_data) => {
|
.then(async (_data) => {
|
||||||
await utils.registry.all.invalidate();
|
await utils.registry.all.invalidate();
|
||||||
toast.success(registryId ? "Registry updated" : "Registry added");
|
toast.success(registryId ? "Registry updated" : "Registry added");
|
||||||
@@ -198,11 +227,14 @@ export const HandleRegistry = ({ registryId }: Props) => {
|
|||||||
Fill the next fields to add a external registry.
|
Fill the next fields to add a external registry.
|
||||||
</DialogDescription>
|
</DialogDescription>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
{(isError || testRegistryIsError) && (
|
{(isError || testRegistryIsError || testRegistryByIdIsError) && (
|
||||||
<div className="flex flex-row gap-4 rounded-lg bg-red-50 p-2 dark:bg-red-950">
|
<div className="flex flex-row gap-4 rounded-lg bg-red-50 p-2 dark:bg-red-950">
|
||||||
<AlertTriangle className="text-red-600 dark:text-red-400" />
|
<AlertTriangle className="text-red-600 dark:text-red-400" />
|
||||||
<span className="text-sm text-red-600 dark:text-red-400">
|
<span className="text-sm text-red-600 dark:text-red-400">
|
||||||
{testRegistryError?.message || error?.message || ""}
|
{testRegistryError?.message ||
|
||||||
|
testRegistryByIdError?.message ||
|
||||||
|
error?.message ||
|
||||||
|
""}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
@@ -253,10 +285,21 @@ export const HandleRegistry = ({ registryId }: Props) => {
|
|||||||
name="password"
|
name="password"
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem>
|
<FormItem>
|
||||||
<FormLabel>Password</FormLabel>
|
<FormLabel>
|
||||||
|
Password{registryId && " (Optional)"}
|
||||||
|
</FormLabel>
|
||||||
|
{registryId && (
|
||||||
|
<FormDescription>
|
||||||
|
Leave blank to keep existing password. Enter new password to test or update it.
|
||||||
|
</FormDescription>
|
||||||
|
)}
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Input
|
<Input
|
||||||
placeholder="Password"
|
placeholder={
|
||||||
|
registryId
|
||||||
|
? "Leave blank to keep existing"
|
||||||
|
: "Password"
|
||||||
|
}
|
||||||
autoComplete="one-time-code"
|
autoComplete="one-time-code"
|
||||||
{...field}
|
{...field}
|
||||||
type="password"
|
type="password"
|
||||||
@@ -387,8 +430,37 @@ export const HandleRegistry = ({ registryId }: Props) => {
|
|||||||
<Button
|
<Button
|
||||||
type="button"
|
type="button"
|
||||||
variant={"secondary"}
|
variant={"secondary"}
|
||||||
isLoading={isLoading}
|
isLoading={isLoading || isLoadingById}
|
||||||
onClick={async () => {
|
onClick={async () => {
|
||||||
|
// When editing with empty password, use the existing password from DB
|
||||||
|
if (registryId && (!password || password.length === 0)) {
|
||||||
|
await testRegistryById({
|
||||||
|
registryId: registryId || "",
|
||||||
|
...(serverId && { serverId }),
|
||||||
|
})
|
||||||
|
.then((data) => {
|
||||||
|
if (data) {
|
||||||
|
toast.success("Registry Tested Successfully");
|
||||||
|
} else {
|
||||||
|
toast.error("Registry Test Failed");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
toast.error("Error testing the registry");
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// When creating, password is required
|
||||||
|
if (!registryId && (!password || password.length === 0)) {
|
||||||
|
form.setError("password", {
|
||||||
|
type: "manual",
|
||||||
|
message: "Password is required",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// When creating or editing with new password, validate and test with provided credentials
|
||||||
const validationResult = AddRegistrySchema.safeParse({
|
const validationResult = AddRegistrySchema.safeParse({
|
||||||
username,
|
username,
|
||||||
password,
|
password,
|
||||||
@@ -396,6 +468,7 @@ export const HandleRegistry = ({ registryId }: Props) => {
|
|||||||
registryName: "Dokploy Registry",
|
registryName: "Dokploy Registry",
|
||||||
imagePrefix,
|
imagePrefix,
|
||||||
serverId,
|
serverId,
|
||||||
|
isEditing: !!registryId,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!validationResult.success) {
|
if (!validationResult.success) {
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import {
|
|||||||
apiFindOneRegistry,
|
apiFindOneRegistry,
|
||||||
apiRemoveRegistry,
|
apiRemoveRegistry,
|
||||||
apiTestRegistry,
|
apiTestRegistry,
|
||||||
|
apiTestRegistryById,
|
||||||
apiUpdateRegistry,
|
apiUpdateRegistry,
|
||||||
registry,
|
registry,
|
||||||
} from "@/server/db/schema";
|
} from "@/server/db/schema";
|
||||||
@@ -109,6 +110,67 @@ export const registryRouter = createTRPCRouter({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: "BAD_REQUEST",
|
||||||
|
message:
|
||||||
|
error instanceof Error
|
||||||
|
? error.message
|
||||||
|
: "Error testing the registry",
|
||||||
|
cause: error,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
testRegistryById: protectedProcedure
|
||||||
|
.input(apiTestRegistryById)
|
||||||
|
.mutation(async ({ input, ctx }) => {
|
||||||
|
try {
|
||||||
|
// Get the full registry with password from database
|
||||||
|
const registryData = await db.query.registry.findFirst({
|
||||||
|
where: eq(registry.registryId, input.registryId ?? ""),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!registryData) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: "NOT_FOUND",
|
||||||
|
message: "Registry not found",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (registryData.organizationId !== ctx.session.activeOrganizationId) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: "UNAUTHORIZED",
|
||||||
|
message: "You are not allowed to test this registry",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const args = [
|
||||||
|
"login",
|
||||||
|
registryData.registryUrl,
|
||||||
|
"--username",
|
||||||
|
registryData.username,
|
||||||
|
"--password-stdin",
|
||||||
|
];
|
||||||
|
|
||||||
|
if (IS_CLOUD && !input.serverId) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: "NOT_FOUND",
|
||||||
|
message: "Select a server to test the registry",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input.serverId && input.serverId !== "none") {
|
||||||
|
await execAsyncRemote(
|
||||||
|
input.serverId,
|
||||||
|
`echo ${registryData.password} | docker ${args.join(" ")}`,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
await execFileAsync("docker", args, {
|
||||||
|
input: Buffer.from(registryData.password).toString(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
|
|||||||
@@ -80,6 +80,14 @@ export const apiTestRegistry = createSchema.pick({}).extend({
|
|||||||
serverId: z.string().optional(),
|
serverId: z.string().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const apiTestRegistryById = createSchema
|
||||||
|
.pick({
|
||||||
|
registryId: true,
|
||||||
|
})
|
||||||
|
.extend({
|
||||||
|
serverId: z.string().optional(),
|
||||||
|
});
|
||||||
|
|
||||||
export const apiRemoveRegistry = createSchema
|
export const apiRemoveRegistry = createSchema
|
||||||
.pick({
|
.pick({
|
||||||
registryId: true,
|
registryId: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user