Skip to content

Commit

Permalink
Merge pull request TencentBlueKing#1039 from felixncheng/issue_1038
Browse files Browse the repository at this point in the history
bug: 修复cns相关错误 TencentBlueKing#1038 TencentBlueKing#1043
  • Loading branch information
owenlxu authored Aug 8, 2023
2 parents 863874a + 4a25d44 commit 33e26f8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class BootAssemblyHandlerMapping : RequestMappingHandlerMapping() {
@Suppress("SpreadOperator")
private fun updateMapping(mapping: RequestMappingInfo, method: Method): RequestMappingInfo? {
val serviceName = ServiceCommonUtils.getServiceName(method.declaringClass)
if (serviceName == "common") {
return mapping
}
val newPatterns = updatePatterns(mapping.patternsCondition?.patterns.orEmpty(), serviceName)
if (newPatterns.isEmpty()) {
return null
Expand Down
3 changes: 3 additions & 0 deletions src/backend/boot-assembly/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ spring:
main:
allow-bean-definition-overriding: true

cns:
enabled: true

logging:
config: classpath:logback-config.xml
path: /data/logs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import org.springframework.web.bind.annotation.RestController

@Principal(type = PrincipalType.ADMIN)
@RestController
@RequestMapping("cns")
@RequestMapping("/service/cns")
class CnsController(
private val cnsService: CnsService
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,20 @@
package com.tencent.bkrepo.common.artifact.cns.impl

import com.google.common.util.concurrent.ThreadFactoryBuilder
import com.tencent.bkrepo.common.api.constant.MS_AUTH_HEADER_UID
import com.tencent.bkrepo.common.api.constant.USER_KEY
import com.tencent.bkrepo.common.api.pojo.Response
import com.tencent.bkrepo.common.artifact.cns.CnsProperties
import com.tencent.bkrepo.common.artifact.cns.CnsService
import com.tencent.bkrepo.common.artifact.pojo.RepositoryType
import com.tencent.bkrepo.common.security.constant.MS_AUTH_HEADER_SECURITY_TOKEN
import com.tencent.bkrepo.common.security.service.ServiceAuthManager
import com.tencent.bkrepo.common.service.otel.util.AsyncUtils.trace
import com.tencent.bkrepo.common.service.util.HttpContextHolder
import com.tencent.bkrepo.common.storage.core.StorageProperties
import com.tencent.bkrepo.common.storage.core.StorageService
import com.tencent.bkrepo.repository.api.StorageCredentialsClient
import com.tencent.bkrepo.repository.constant.SYSTEM_USER
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.cloud.client.DefaultServiceInstance
Expand All @@ -59,20 +64,21 @@ class CnsServiceImpl(
private val storageService: StorageService,
private val storageCredentialsClient: StorageCredentialsClient,
private val storageProperties: StorageProperties,
private val cnsProperties: CnsProperties
private val cnsProperties: CnsProperties,
private val serviceAuthManager: ServiceAuthManager
) : CnsService {
@Value("\${service.prefix:}")
private val servicePrefix: String = ""

@Value("\${service.suffix:}")
private val serviceSuffix: String = ""

private val restTemplate = RestTemplate()

private var services = mutableMapOf<String, Set<ServiceInstance>>()

private var lastUpdatedTime = -1L

private val restTemplate = RestTemplate()

override fun exist(key: String?, sha256: String): Boolean {
val storageCredentials = storageCredentialsClient.findByKey(key).data
?: storageProperties.defaultStorageCredentials()
Expand All @@ -96,21 +102,22 @@ class CnsServiceImpl(
logger.info("Not found any match service.")
return false
}
val token = HttpContextHolder.getRequest().getHeader(HttpHeaders.AUTHORIZATION)
val uid = HttpContextHolder.getRequestOrNull()?.getAttribute(USER_KEY)?.toString() ?: SYSTEM_USER
val tasks = targetServices.map {
Callable { sendExistRequest(key, sha256, it, token) }.trace()
Callable { sendExistRequest(key, sha256, it, uid) }.trace()
}
val futures = threadPool.invokeAll(tasks)
return futures.firstOrNull { it.get() == false } == null
}

private fun sendExistRequest(key: String?, sha256: String, instance: ServiceInstance, token: String): Boolean {
private fun sendExistRequest(key: String?, sha256: String, instance: ServiceInstance, uid: String): Boolean {
with(instance) {
val target = instance.uri
val url = "$target/cns/exist?key=$key&sha256=$sha256"
val url = "$target/service/cns/exist?key=$key&sha256=$sha256"
try {
val headers = HttpHeaders()
headers.add(HttpHeaders.AUTHORIZATION, token)
headers.add(MS_AUTH_HEADER_SECURITY_TOKEN, serviceAuthManager.getSecurityToken())
headers.add(MS_AUTH_HEADER_UID, uid)
val httpEntity = HttpEntity<Any>(headers)
val response = restTemplate.exchange(url, HttpMethod.GET, httpEntity, Response::class.java).body
if (logger.isDebugEnabled) {
Expand Down Expand Up @@ -153,6 +160,7 @@ class CnsServiceImpl(
serviceIds.add(dockerServiceId)
serviceIds.add(ociServiceId)
}

else -> {
serviceIds.add(formatServiceId(type.name.toLowerCase()))
}
Expand All @@ -164,7 +172,7 @@ class CnsServiceImpl(
return "$servicePrefix$name$serviceSuffix"
}

companion object {
private companion object {
private val logger = LoggerFactory.getLogger(CnsServiceImpl::class.java)
private val threadFactory = ThreadFactoryBuilder().setNameFormat("cns-%d").build()
private val threadPool =
Expand Down

0 comments on commit 33e26f8

Please sign in to comment.