feat(registry): refactor Docker login command execution to use execFileAsync for improved input handling

This commit is contained in:
Mauricio Siu 2025-04-05 23:03:57 -06:00
parent e83efa3379
commit cb20950dd9
2 changed files with 55 additions and 5 deletions

View File

@ -10,8 +10,8 @@ import {
import {
IS_CLOUD,
createRegistry,
execAsync,
execAsyncRemote,
execFileAsync,
findRegistryById,
removeRegistry,
updateRegistry,
@ -83,7 +83,13 @@ export const registryRouter = createTRPCRouter({
.input(apiTestRegistry)
.mutation(async ({ input }) => {
try {
const loginCommand = `echo ${input.password} | docker login ${input.registryUrl} --username ${input.username} --password-stdin`;
const args = [
"login",
input.registryUrl,
"--username",
input.username,
"--password-stdin",
];
if (IS_CLOUD && !input.serverId) {
throw new TRPCError({
@ -93,9 +99,14 @@ export const registryRouter = createTRPCRouter({
}
if (input.serverId && input.serverId !== "none") {
await execAsyncRemote(input.serverId, loginCommand);
await execAsyncRemote(
input.serverId,
`echo ${input.password} | docker ${args.join(" ")}`,
);
} else {
await execAsync(loginCommand);
await execFileAsync("docker", args, {
input: Buffer.from(input.password).toString(),
});
}
return true;

View File

@ -1,9 +1,48 @@
import { exec } from "node:child_process";
import { exec, execFile } from "node:child_process";
import util from "node:util";
import { findServerById } from "@dokploy/server/services/server";
import { Client } from "ssh2";
export const execAsync = util.promisify(exec);
export const execFileAsync = async (
command: string,
args: string[],
options: { input?: string } = {},
): Promise<{ stdout: string; stderr: string }> => {
const child = execFile(command, args);
if (options.input && child.stdin) {
child.stdin.write(options.input);
child.stdin.end();
}
return new Promise((resolve, reject) => {
let stdout = "";
let stderr = "";
child.stdout?.on("data", (data) => {
stdout += data.toString();
});
child.stderr?.on("data", (data) => {
stderr += data.toString();
});
child.on("close", (code) => {
if (code === 0) {
resolve({ stdout, stderr });
} else {
reject(
new Error(`Command failed with code ${code}. Stderr: ${stderr}`),
);
}
});
child.on("error", reject);
});
};
export const execAsyncRemote = async (
serverId: string | null,
command: string,