From 23918502185aed7f5c3a3e82f2aa41e41d6377ca Mon Sep 17 00:00:00 2001 From: Titouan Mathis Date: Wed, 21 Dec 2022 14:55:13 +0100 Subject: [PATCH] Add support for custom SSH user for GitLab self-hosted --- .../migration.sql | 27 +++++++++++++++++++ apps/api/prisma/schema.prisma | 1 + apps/api/src/jobs/deployApplication.ts | 1 + apps/api/src/lib/importers/gitlab.ts | 6 +++-- .../api/src/routes/api/v1/sources/handlers.ts | 14 +++++----- apps/api/src/routes/api/v1/sources/types.ts | 1 + apps/server/prisma/schema.prisma | 1 + .../ui/src/routes/sources/[id]/_Gitlab.svelte | 22 +++++++++++++-- 8 files changed, 63 insertions(+), 10 deletions(-) create mode 100644 apps/api/prisma/migrations/20221221140911_git_source_custom_user/migration.sql diff --git a/apps/api/prisma/migrations/20221221140911_git_source_custom_user/migration.sql b/apps/api/prisma/migrations/20221221140911_git_source_custom_user/migration.sql new file mode 100644 index 000000000..d9d5a7204 --- /dev/null +++ b/apps/api/prisma/migrations/20221221140911_git_source_custom_user/migration.sql @@ -0,0 +1,27 @@ +-- RedefineTables +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_GitSource" ( + "id" TEXT NOT NULL PRIMARY KEY, + "name" TEXT NOT NULL, + "forPublic" BOOLEAN NOT NULL DEFAULT false, + "type" TEXT, + "apiUrl" TEXT, + "htmlUrl" TEXT, + "customPort" INTEGER NOT NULL DEFAULT 22, + "customUser" TEXT NOT NULL DEFAULT 'git', + "organization" TEXT, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL, + "githubAppId" TEXT, + "gitlabAppId" TEXT, + "isSystemWide" BOOLEAN NOT NULL DEFAULT false, + CONSTRAINT "GitSource_gitlabAppId_fkey" FOREIGN KEY ("gitlabAppId") REFERENCES "GitlabApp" ("id") ON DELETE SET NULL ON UPDATE CASCADE, + CONSTRAINT "GitSource_githubAppId_fkey" FOREIGN KEY ("githubAppId") REFERENCES "GithubApp" ("id") ON DELETE SET NULL ON UPDATE CASCADE +); +INSERT INTO "new_GitSource" ("apiUrl", "createdAt", "customPort", "forPublic", "githubAppId", "gitlabAppId", "htmlUrl", "id", "isSystemWide", "name", "organization", "type", "updatedAt") SELECT "apiUrl", "createdAt", "customPort", "forPublic", "githubAppId", "gitlabAppId", "htmlUrl", "id", "isSystemWide", "name", "organization", "type", "updatedAt" FROM "GitSource"; +DROP TABLE "GitSource"; +ALTER TABLE "new_GitSource" RENAME TO "GitSource"; +CREATE UNIQUE INDEX "GitSource_githubAppId_key" ON "GitSource"("githubAppId"); +CREATE UNIQUE INDEX "GitSource_gitlabAppId_key" ON "GitSource"("gitlabAppId"); +PRAGMA foreign_key_check; +PRAGMA foreign_keys=ON; diff --git a/apps/api/prisma/schema.prisma b/apps/api/prisma/schema.prisma index 54eefc504..1e9b75aa5 100644 --- a/apps/api/prisma/schema.prisma +++ b/apps/api/prisma/schema.prisma @@ -325,6 +325,7 @@ model GitSource { apiUrl String? htmlUrl String? customPort Int @default(22) + customUser String @default("git") organization String? createdAt DateTime @default(now()) updatedAt DateTime @updatedAt diff --git a/apps/api/src/jobs/deployApplication.ts b/apps/api/src/jobs/deployApplication.ts index 6f79b94e8..cc3740af4 100644 --- a/apps/api/src/jobs/deployApplication.ts +++ b/apps/api/src/jobs/deployApplication.ts @@ -419,6 +419,7 @@ import * as buildpacks from '../lib/buildPacks'; githubAppId: gitSource.githubApp?.id, gitlabAppId: gitSource.gitlabApp?.id, customPort: gitSource.customPort, + customUser: gitSource.customUser, gitCommitHash, configuration, repository, diff --git a/apps/api/src/lib/importers/gitlab.ts b/apps/api/src/lib/importers/gitlab.ts index eef9b62ef..d75c3bbef 100644 --- a/apps/api/src/lib/importers/gitlab.ts +++ b/apps/api/src/lib/importers/gitlab.ts @@ -12,7 +12,8 @@ export default async function ({ buildId, privateSshKey, customPort, - forPublic + forPublic, + customUser, }: { applicationId: string; workdir: string; @@ -25,6 +26,7 @@ export default async function ({ privateSshKey: string; customPort: number; forPublic: boolean; + customUser: string; }): Promise { const url = htmlUrl.replace('https://', '').replace('http://', '').replace(/\/$/, ''); if (!forPublic) { @@ -53,7 +55,7 @@ export default async function ({ } else { await executeCommand({ command: - `git clone -q -b ${branch} git@${url}:${repository}.git --config core.sshCommand="ssh -p ${customPort} -q -i ${repodir}id.rsa -o StrictHostKeyChecking=no" ${workdir}/ && cd ${workdir}/ && git checkout ${gitCommitHash || ""} && git submodule update --init --recursive && git lfs pull && cd .. `, shell: true + `git clone -q -b ${branch} ${customUser}@${url}:${repository}.git --config core.sshCommand="ssh -p ${customPort} -q -i ${repodir}id.rsa -o StrictHostKeyChecking=no" ${workdir}/ && cd ${workdir}/ && git checkout ${gitCommitHash || ""} && git submodule update --init --recursive && git lfs pull && cd .. `, shell: true } ); } diff --git a/apps/api/src/routes/api/v1/sources/handlers.ts b/apps/api/src/routes/api/v1/sources/handlers.ts index e33652571..b4bd81aa4 100644 --- a/apps/api/src/routes/api/v1/sources/handlers.ts +++ b/apps/api/src/routes/api/v1/sources/handlers.ts @@ -22,11 +22,11 @@ export async function listSources(request: FastifyRequest) { export async function saveSource(request, reply) { try { const { id } = request.params - let { name, htmlUrl, apiUrl, customPort, isSystemWide } = request.body + let { name, htmlUrl, apiUrl, customPort, customUser, isSystemWide } = request.body if (customPort) customPort = Number(customPort) await prisma.gitSource.update({ where: { id }, - data: { name, htmlUrl, apiUrl, customPort, isSystemWide } + data: { name, htmlUrl, apiUrl, customPort, customUser, isSystemWide } }); return reply.code(201).send() } catch ({ status, message }) { @@ -48,6 +48,7 @@ export async function getSource(request: FastifyRequest) { apiUrl: null, organization: null, customPort: 22, + customUser: 'git', }, settings } @@ -102,7 +103,7 @@ export async function saveGitHubSource(request: FastifyRequest const { teamId } = request.user const { id } = request.params - let { name, htmlUrl, apiUrl, organization, customPort, isSystemWide } = request.body + let { name, htmlUrl, apiUrl, organization, customPort, customUser, isSystemWide } = request.body if (customPort) customPort = Number(customPort) if (id === 'new') { @@ -115,6 +116,7 @@ export async function saveGitHubSource(request: FastifyRequest apiUrl, organization, customPort, + customUser, isSystemWide, type: 'github', teams: { connect: { id: teamId } } @@ -133,7 +135,7 @@ export async function saveGitLabSource(request: FastifyRequest try { const { id } = request.params const { teamId } = request.user - let { type, name, htmlUrl, apiUrl, oauthId, appId, appSecret, groupName, customPort } = + let { type, name, htmlUrl, apiUrl, oauthId, appId, appSecret, groupName, customPort, customUser } = request.body if (oauthId) oauthId = Number(oauthId); @@ -142,7 +144,7 @@ export async function saveGitLabSource(request: FastifyRequest if (id === 'new') { const newId = cuid() - await prisma.gitSource.create({ data: { id: newId, type, apiUrl, htmlUrl, name, customPort, teams: { connect: { id: teamId } } } }); + await prisma.gitSource.create({ data: { id: newId, type, apiUrl, htmlUrl, name, customPort, customUser, teams: { connect: { id: teamId } } } }); await prisma.gitlabApp.create({ data: { teams: { connect: { id: teamId } }, @@ -158,7 +160,7 @@ export async function saveGitLabSource(request: FastifyRequest id: newId } } else { - await prisma.gitSource.update({ where: { id }, data: { type, apiUrl, htmlUrl, name, customPort } }); + await prisma.gitSource.update({ where: { id }, data: { type, apiUrl, htmlUrl, name, customPort, customUser } }); await prisma.gitlabApp.update({ where: { id }, data: { diff --git a/apps/api/src/routes/api/v1/sources/types.ts b/apps/api/src/routes/api/v1/sources/types.ts index bb65c7d85..bbccd2e7b 100644 --- a/apps/api/src/routes/api/v1/sources/types.ts +++ b/apps/api/src/routes/api/v1/sources/types.ts @@ -21,6 +21,7 @@ export interface SaveGitLabSource extends OnlyId { appSecret: string, groupName: string, customPort: number, + customUser: string, } } export interface CheckGitLabOAuthId extends OnlyId { diff --git a/apps/server/prisma/schema.prisma b/apps/server/prisma/schema.prisma index 54eefc504..1e9b75aa5 100644 --- a/apps/server/prisma/schema.prisma +++ b/apps/server/prisma/schema.prisma @@ -325,6 +325,7 @@ model GitSource { apiUrl String? htmlUrl String? customPort Int @default(22) + customUser String @default("git") organization String? createdAt DateTime @default(now()) updatedAt DateTime @updatedAt diff --git a/apps/ui/src/routes/sources/[id]/_Gitlab.svelte b/apps/ui/src/routes/sources/[id]/_Gitlab.svelte index 6f724ead2..a7c850824 100644 --- a/apps/ui/src/routes/sources/[id]/_Gitlab.svelte +++ b/apps/ui/src/routes/sources/[id]/_Gitlab.svelte @@ -51,7 +51,8 @@ appId: source.gitlabApp.appId, appSecret: source.gitlabApp.appSecret, groupName: source.gitlabApp.groupName, - customPort: source.customPort + customPort: source.customPort, + customUser: source.customUser, }); const from = $page.url.searchParams.get('from'); if (from) { @@ -70,7 +71,8 @@ name: source.name, htmlUrl: source.htmlUrl.replace(/\/$/, ''), apiUrl: source.apiUrl.replace(/\/$/, ''), - customPort: source.customPort + customPort: source.customPort, + customuser: source.customuser }); return addToast({ message: 'Configuration saved.', @@ -244,6 +246,22 @@ /> {#if selfHosted} +
+ + +