Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 支持SVN代理 #1196 #1200

Merged
merged 7 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ enum class RepositoryType {
CONAN,
LFS,
DDC,
SVN,
;

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.tencent.bkrepo.common.security.interceptor.devx

/**
* 云研发配置
* */
data class DevxProperties(
/**
* 是否开启云研发相关配置
* */
var enabled: Boolean = false,
/**
* apigw app code
* */
var appCode: String = "",
/**
* apigw app secret
* */
var appSecret: String = "",
/**
* 查询云研发工作空间的URL
* */
var workspaceUrl: String = "",
)
Original file line number Diff line number Diff line change
@@ -1,48 +1,74 @@
package com.tencent.bkrepo.git.interceptor.devx
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.tencent.bkrepo.common.security.interceptor.devx

import com.google.common.cache.CacheBuilder
import com.google.common.cache.CacheLoader
import com.google.common.cache.LoadingCache
import com.tencent.bkrepo.common.api.util.readJsonString
import com.tencent.bkrepo.common.api.util.toJsonString
import com.tencent.bkrepo.common.artifact.repository.context.ArtifactContextHolder
import com.tencent.bkrepo.common.artifact.constant.PROJECT_ID
import com.tencent.bkrepo.common.security.exception.PermissionException
import com.tencent.bkrepo.common.service.util.HttpContextHolder
import com.tencent.bkrepo.git.config.GitProperties
import java.util.concurrent.TimeUnit
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import okhttp3.OkHttpClient
import okhttp3.Request
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.servlet.HandlerInterceptor
import org.springframework.web.servlet.HandlerMapping
import java.util.concurrent.TimeUnit
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
* 云研发源ip拦截器,只允许项目的云桌面ip通过
* */
class DevxSrcIpInterceptor : HandlerInterceptor {
@Autowired
lateinit var properties: GitProperties
class DevxSrcIpInterceptor(private val devxProperties: DevxProperties) : HandlerInterceptor {
private val httpClient = OkHttpClient.Builder().build()
private val projectIpsCache: LoadingCache<String, Set<String>> = CacheBuilder.newBuilder()
.maximumSize(MAX_CACHE_PROJECT_SIZE)
.expireAfterWrite(CACHE_EXPIRE_TIME, TimeUnit.SECONDS)
.build(CacheLoader.from { key -> listIpFromProject(key) })

override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean {
if (!properties.devx.enabled) {
if (!devxProperties.enabled) {
return true
}
request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE) ?: return false
val repo = ArtifactContextHolder.getRepoDetail()!!


val uriAttribute = request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE) ?: return false
require(uriAttribute is Map<*, *>)
val projectId = uriAttribute[PROJECT_ID].toString()
val srcIp = HttpContextHolder.getClientAddress()
if (!inWhiteList(srcIp, repo.projectId)) {
logger.info("Illegal src ip[$srcIp] in project[${repo.projectId}].")
if (!inWhiteList(srcIp, projectId)) {
logger.info("Illegal src ip[$srcIp] in project[${projectId}].")
throw PermissionException()
}
logger.info("Allow ip[$srcIp] to access ${repo.projectId}.")
logger.info("Allow ip[$srcIp] to access ${projectId}.")
return true
}

Expand All @@ -51,7 +77,6 @@ class DevxSrcIpInterceptor : HandlerInterceptor {
}

private fun listIpFromProject(projectId: String): Set<String> {
val devxProperties = properties.devx
val apiAuth = ApiAuth(devxProperties.appCode, devxProperties.appSecret)
val token = apiAuth.toJsonString().replace(System.lineSeparator(), "")
val workspaceUrl = devxProperties.workspaceUrl
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package com.tencent.bkrepo.common.service.util.proxy

import okhttp3.Request
import okhttp3.Response
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import okhttp3.Response

open class DefaultProxyCallHandler : ProxyCallHandler {
override fun before(
proxyRequest: HttpServletRequest,
proxyResponse: HttpServletResponse,
request: Request
): Request {
return request
}

override fun after(proxyRequest: HttpServletRequest, proxyResponse: HttpServletResponse, response: Response) {
// 转发状态码
proxyResponse.status = response.code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@ object HttpProxyUtil {
prefix: String? = null,
proxyCallHandler: ProxyCallHandler = defaultProxyCallHandler,
) {
val newUrl = "$targetUrl${proxyRequest.requestURI.removePrefix(prefix.orEmpty())}?${proxyRequest.queryString}"
val newUrl = if (proxyRequest.queryString.isNullOrEmpty()) {
"$targetUrl${proxyRequest.requestURI.removePrefix(prefix.orEmpty())}"
} else {
"$targetUrl${proxyRequest.requestURI.removePrefix(prefix.orEmpty())}?${proxyRequest.queryString}"
}
val newRequest = Request.Builder()
.url(newUrl)
.apply {
proxyRequest.headers().forEach { (key, value) -> this.header(key, value) }
}
.method(proxyRequest.method, proxyRequest.body())
.build()
.let { proxyCallHandler.before(proxyRequest, proxyResponse, it) }
val newResponse = client
.newCall(newRequest)
.execute()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.tencent.bkrepo.common.service.util.proxy

import okhttp3.Request
import okhttp3.Response
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import okhttp3.Response

interface ProxyCallHandler {
fun before(proxyRequest: HttpServletRequest, proxyResponse: HttpServletResponse, request: Request): Request

fun after(proxyRequest: HttpServletRequest, proxyResponse: HttpServletResponse, response: Response)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.tencent.bkrepo.git.config

import com.tencent.bkrepo.git.constant.HubType
import com.tencent.bkrepo.git.interceptor.devx.DevxProperties
import com.tencent.bkrepo.common.security.interceptor.devx.DevxProperties
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.NestedConfigurationProperty

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.tencent.bkrepo.git.config

import com.tencent.bkrepo.git.artifact.GitRepoInterceptor
import com.tencent.bkrepo.git.interceptor.ContextSettingInterceptor
import com.tencent.bkrepo.git.interceptor.devx.DevxSrcIpInterceptor
import com.tencent.bkrepo.common.security.interceptor.devx.DevxSrcIpInterceptor
import com.tencent.bkrepo.git.interceptor.ProxyInterceptor
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean
Expand All @@ -13,7 +13,9 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer

@Configuration
@EnableConfigurationProperties(GitProperties::class)
class GitWebConfig : WebMvcConfigurer {
class GitWebConfig(
private val properties: GitProperties
) : WebMvcConfigurer {

override fun addInterceptors(registry: InterceptorRegistry) {
registry.addInterceptor(repoInterceptor())
Expand All @@ -25,7 +27,7 @@ class GitWebConfig : WebMvcConfigurer {
registry.addInterceptor(ProxyInterceptor())
.addPathPatterns("/**")
.order(Ordered.HIGHEST_PRECEDENCE + 1)
registry.addInterceptor(devxSrcIpInterceptor())
registry.addInterceptor(devxSrcIpInterceptor(properties))
.addPathPatterns("/**")
.order(Ordered.HIGHEST_PRECEDENCE)
super.addInterceptors(registry)
Expand All @@ -35,5 +37,5 @@ class GitWebConfig : WebMvcConfigurer {
fun repoInterceptor() = GitRepoInterceptor()

@Bean
fun devxSrcIpInterceptor() = DevxSrcIpInterceptor()
fun devxSrcIpInterceptor(properties: GitProperties) = DevxSrcIpInterceptor(properties.devx)
}

This file was deleted.

1 change: 1 addition & 0 deletions src/backend/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@ includeAll(":fs")
includeAll(":config")
includeAll(":lfs")
includeAll(":ddc")
includeAll(":svn")
27 changes: 27 additions & 0 deletions src/backend/svn/api-svn/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

34 changes: 34 additions & 0 deletions src/backend/svn/biz-svn/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

dependencies {
api(project(":svn:api-svn"))
api(project(":common:common-service"))
implementation(project(":common:common-api"))
implementation(project(":common:common-artifact:artifact-service"))
implementation(project(":common:common-security"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.tencent.bkrepo.svn

import com.tencent.bkrepo.common.api.exception.MethodNotAllowedException
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/{projectId}/{repoName}")
class UserProxyController {

@RequestMapping("/**")
fun proxy(
@PathVariable projectId: String,
@PathVariable repoName: String,
) {
throw MethodNotAllowedException()
}
}
Loading
Loading