From 8ed4966cdc0872d27dc7a467d640fc3750bdbcf0 Mon Sep 17 00:00:00 2001 From: Jan-Niclas Struewer <j.n.struewer@gmail.com> Date: Tue, 31 Oct 2023 11:28:21 +0100 Subject: [PATCH] Added functionality to handle private gits --- kubernetes/deployment.yaml | 5 +++++ .../configuration/OpenCodeGitlabApiProperties.kt | 9 ++++++++- .../dataprovider/tools/occmd/service/OccmdService.kt | 12 ++++++++++-- src/main/resources/application-local.properties | 6 ++++-- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/kubernetes/deployment.yaml b/kubernetes/deployment.yaml index d28a92d9..7db87de1 100644 --- a/kubernetes/deployment.yaml +++ b/kubernetes/deployment.yaml @@ -55,6 +55,11 @@ spec: secretKeyRef: name: backendapisecrets key: oc_api_key + - name: OC_USER + valueFrom: + secretKeyRef: + name: backendapisecrets + key: oc_user - name: API_KEY valueFrom: secretKeyRef: diff --git a/src/main/kotlin/de/fraunhofer/iem/dataprovider/configuration/OpenCodeGitlabApiProperties.kt b/src/main/kotlin/de/fraunhofer/iem/dataprovider/configuration/OpenCodeGitlabApiProperties.kt index c8d89fb8..99f6d921 100644 --- a/src/main/kotlin/de/fraunhofer/iem/dataprovider/configuration/OpenCodeGitlabApiProperties.kt +++ b/src/main/kotlin/de/fraunhofer/iem/dataprovider/configuration/OpenCodeGitlabApiProperties.kt @@ -12,12 +12,19 @@ data class OpenCodeGitlabApiProperties( @field:NotBlank val host: String, @field:NotBlank - val accessToken: String + val accessToken: String, + + val analyzePrivateRepos: Boolean = false, + val userName: String? ) { @PostConstruct fun postConstruct() { // There is no try catch block around the operations on purpose! // We want to throw here if this operations fail. URL(host).toURI() + + if (analyzePrivateRepos && userName.isNullOrEmpty()) { + throw Exception("To analyze private repositories a username must be set") + } } } diff --git a/src/main/kotlin/de/fraunhofer/iem/dataprovider/tools/occmd/service/OccmdService.kt b/src/main/kotlin/de/fraunhofer/iem/dataprovider/tools/occmd/service/OccmdService.kt index 61322382..254efa18 100644 --- a/src/main/kotlin/de/fraunhofer/iem/dataprovider/tools/occmd/service/OccmdService.kt +++ b/src/main/kotlin/de/fraunhofer/iem/dataprovider/tools/occmd/service/OccmdService.kt @@ -15,6 +15,7 @@ import kotlinx.coroutines.future.await import kotlinx.coroutines.withContext import kotlinx.serialization.json.Json import org.eclipse.jgit.api.Git +import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider import org.springframework.stereotype.Service import java.io.File import java.nio.file.Files.isExecutable @@ -128,10 +129,17 @@ class OccmdService( } private suspend fun cloneGit(repoUrl: String, outDir: File) { - val git: Git = Git.cloneRepository() + val gitRequest = Git.cloneRepository() + .setCloneSubmodules(true) .setURI(repoUrl) .setDirectory(outDir) - .call() + if (gitlabApiProperties.analyzePrivateRepos) { + gitRequest.setCredentialsProvider( + UsernamePasswordCredentialsProvider(gitlabApiProperties.userName, gitlabApiProperties.accessToken) + ) + } + + val git = gitRequest.call() git.close() } } diff --git a/src/main/resources/application-local.properties b/src/main/resources/application-local.properties index a5b16512..c8fc466d 100644 --- a/src/main/resources/application-local.properties +++ b/src/main/resources/application-local.properties @@ -1,8 +1,10 @@ spring.config.import=optional:classpath:.env[.properties] # Config for the OpencoDE platform # Token can be an empty string to access public repositories only -opencode.host=https://gitlab.opencode.de/ +opencode.host=https://gitlab.dev.o4oe.de/ opencode.access-token=${OC_GL_APIKEY:} +opencode.analyze-private-repos=true +opencode.user-name=${OC_USER:} # Tool APIs opencode.api.base-path=https://sl.dev.o4oe.de/api/v1/project/ opencode.api.ort=/cve-result @@ -41,5 +43,5 @@ spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.CockroachDialect spring.datasource.url=${DB_URL:} spring.datasource.username=${DB_USER:} spring.datasource.password=${DB_PW:} -projects.project-ids=1108 +projects.project-ids=159 #, 888, 438, 1189, 820, 788, 400, 1052 -- GitLab