feat: ssh keys filesystel

This commit is contained in:
Lorenzo Migliorero
2024-07-25 20:16:49 +02:00
parent d243470029
commit 1f81ebd4fe
6 changed files with 56 additions and 26 deletions

View File

@@ -3,14 +3,35 @@ import * as path from "node:path";
import { SSH_PATH } from "@/server/constants";
import { spawnAsync } from "../process/spawnAsync";
export const generateSSHKey = async (appName: string) => {
export const saveSSHKey = async (
id: string,
publicKey: string,
privateKey: string,
) => {
const applicationDirectory = SSH_PATH;
const privateKeyPath = path.join(applicationDirectory, `${id}_rsa`);
const publicKeyPath = path.join(applicationDirectory, `${id}_rsa.pub`);
const privateKeyStream = fs.createWriteStream(privateKeyPath, {
mode: 0o400,
});
privateKeyStream.write(privateKey);
privateKeyStream.end();
const publicKeyStream = fs.createWriteStream(publicKeyPath, { mode: 0o400 });
publicKeyStream.write(publicKey);
publicKeyStream.end();
};
export const generateSSHKey = async (id: string) => {
const applicationDirectory = SSH_PATH;
if (!fs.existsSync(applicationDirectory)) {
fs.mkdirSync(applicationDirectory, { recursive: true });
}
const keyPath = path.join(applicationDirectory, `${appName}_rsa`);
const keyPath = path.join(applicationDirectory, `${id}_rsa`);
if (fs.existsSync(`${keyPath}`)) {
fs.unlinkSync(`${keyPath}`);
@@ -37,12 +58,12 @@ export const generateSSHKey = async (appName: string) => {
throw error;
}
};
export const readRSAFile = async (appName: string) => {
export const readSSHPublicKey = async (id: string) => {
try {
if (!fs.existsSync(SSH_PATH)) {
fs.mkdirSync(SSH_PATH, { recursive: true });
}
const keyPath = path.join(SSH_PATH, `${appName}_rsa.pub`);
const keyPath = path.join(SSH_PATH, `${id}_rsa.pub`);
const data = fs.readFileSync(keyPath, { encoding: "utf-8" });
return data;
} catch (error) {
@@ -50,10 +71,10 @@ export const readRSAFile = async (appName: string) => {
}
};
export const removeRSAFiles = async (appName: string) => {
export const removeSSHKey = async (id: string) => {
try {
const publicKeyPath = path.join(SSH_PATH, `${appName}_rsa.pub`);
const privateKeyPath = path.join(SSH_PATH, `${appName}_rsa`);
const publicKeyPath = path.join(SSH_PATH, `${id}_rsa.pub`);
const privateKeyPath = path.join(SSH_PATH, `${id}_rsa`);
await fs.promises.unlink(publicKeyPath);
await fs.promises.unlink(privateKeyPath);
} catch (error) {

View File

@@ -11,12 +11,12 @@ export const cloneGitRepository = async (
appName: string;
customGitUrl?: string | null;
customGitBranch?: string | null;
customGitSSHKey?: string | null;
customGitSSHKeyId?: string | null;
},
logPath: string,
isCompose = false,
) => {
const { appName, customGitUrl, customGitBranch, customGitSSHKey } = entity;
const { appName, customGitUrl, customGitBranch, customGitSSHKeyId } = entity;
if (!customGitUrl || !customGitBranch) {
throw new TRPCError({
@@ -26,7 +26,7 @@ export const cloneGitRepository = async (
}
const writeStream = createWriteStream(logPath, { flags: "a" });
const keyPath = path.join(SSH_PATH, `${appName}_rsa`);
const keyPath = path.join(SSH_PATH, `${customGitSSHKeyId}_rsa`);
const basePath = isCompose ? COMPOSE_PATH : APPLICATIONS_PATH;
const outputPath = join(basePath, appName, "code");
const knownHostsPath = path.join(SSH_PATH, "known_hosts");
@@ -39,7 +39,7 @@ export const cloneGitRepository = async (
writeStream.write(
`\nCloning Repo Custom ${customGitUrl} to ${outputPath}: ✅\n`,
);
console.log(customGitSSHKeyId);
await spawnAsync(
"git",
[
@@ -60,7 +60,7 @@ export const cloneGitRepository = async (
{
env: {
...process.env,
...(customGitSSHKey && {
...(customGitSSHKeyId && {
GIT_SSH_COMMAND: `ssh -i ${keyPath} -o UserKnownHostsFile=${knownHostsPath}`,
}),
},