38 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-02-10 15:47:44 +01:00
import { encrypt } from '$lib/crypto';
import { generateSshKeyPair, prisma } from './common';
2022-02-10 15:47:44 +01:00
export async function updateDeployKey({ id, deployKeyId }) {
const application = await prisma.application.findUnique({
where: { id },
include: { gitSource: { include: { gitlabApp: true } } }
});
return await prisma.gitlabApp.update({
where: { id: application.gitSource.gitlabApp.id },
data: { deployKeyId }
});
}
export async function getSshKey({ id }) {
const application = await prisma.application.findUnique({
where: { id },
include: { gitSource: { include: { gitlabApp: true } } }
});
return { status: 200, body: { publicKey: application.gitSource.gitlabApp.publicSshKey } };
}
export async function generateSshKey({ id }) {
const application = await prisma.application.findUnique({
where: { id },
include: { gitSource: { include: { gitlabApp: true } } }
});
if (!application.gitSource?.gitlabApp?.privateSshKey) {
const keys = await generateSshKeyPair();
const encryptedPrivateKey = encrypt(keys.privateKey);
await prisma.gitlabApp.update({
where: { id: application.gitSource.gitlabApp.id },
data: { privateSshKey: encryptedPrivateKey, publicSshKey: keys.publicKey }
});
return { status: 201, body: { publicKey: keys.publicKey } };
} else {
return { status: 200 };
}
}