Skip to content

Commit

Permalink
Merge pull request TencentBlueKing#1245 from owenlxu/issue_1244
Browse files Browse the repository at this point in the history
bug: 处理调用鉴权服务异常返回状态 TencentBlueKing#1244
  • Loading branch information
owenlxu authored Oct 12, 2023
2 parents bfb5c53 + 505c357 commit fb86d1e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class CIAuthService @Autowired constructor(private val devopsAuthConfig: DevopsA
return try {
val request = Request.Builder().url(url).header(DEVOPS_BK_TOKEN, devopsAuthConfig.getBkciAuthToken())
.header(DEVOPS_PROJECT_ID, projectCode).get().build()
val apiResponse = HttpUtils.doRequest(okHttpClient, request, 2, setOf(HttpStatus.FORBIDDEN.value))
val apiResponse = HttpUtils.doRequest(okHttpClient, request, 2, allowHttpStatusSet)
logger.debug("validateProjectUsers url[$url],result[${apiResponse.code},${apiResponse.content}]")
if (apiResponse.code == HttpStatus.OK.value) {
val responseObject = objectMapper.readValue<BkciAuthCheckResponse>(apiResponse.content)
Expand Down Expand Up @@ -102,7 +102,7 @@ class CIAuthService @Autowired constructor(private val devopsAuthConfig: DevopsA
return try {
val request = Request.Builder().url(url).header(DEVOPS_BK_TOKEN, devopsAuthConfig.getBkciAuthToken())
.header(DEVOPS_PROJECT_ID, projectCode).get().build()
val apiResponse = HttpUtils.doRequest(okHttpClient, request, 2)
val apiResponse = HttpUtils.doRequest(okHttpClient, request, 2, allowHttpStatusSet)
val responseObject = objectMapper.readValue<BkciAuthCheckResponse>(apiResponse.content)
logger.debug("validateProjectManager url[$url], result[${apiResponse.content}]")
resourcePermissionCache.put(cacheKey, responseObject.data)
Expand Down Expand Up @@ -138,7 +138,7 @@ class CIAuthService @Autowired constructor(private val devopsAuthConfig: DevopsA
return try {
val request = Request.Builder().url(url).header(DEVOPS_UID, user).header(DEVOPS_PROJECT_ID, projectCode)
.header(DEVOPS_BK_TOKEN, devopsAuthConfig.getBkciAuthToken()).get().build()
val apiResponse = HttpUtils.doRequest(okHttpClient, request, 2, setOf(HttpStatus.FORBIDDEN.value))
val apiResponse = HttpUtils.doRequest(okHttpClient, request, 2, allowHttpStatusSet)
logger.debug(
"validateProjectSuperAdmin , requestUrl: [$url]," + " result : [${
apiResponse.content.replace("\n", "")
Expand Down Expand Up @@ -182,7 +182,7 @@ class CIAuthService @Autowired constructor(private val devopsAuthConfig: DevopsA
return try {
val request = Request.Builder().url(url).header(DEVOPS_BK_TOKEN, devopsAuthConfig.getBkciAuthToken())
.header(DEVOPS_UID, user).header(DEVOPS_PROJECT_ID, projectCode).get().build()
val apiResponse = HttpUtils.doRequest(okHttpClient, request, 2, setOf(HttpStatus.FORBIDDEN.value))
val apiResponse = HttpUtils.doRequest(okHttpClient, request, 2, allowHttpStatusSet)
val responseObject = objectMapper.readValue<BkciAuthCheckResponse>(apiResponse.content)
logger.debug("validateUserResourcePermission,requestUrl: [$url], result : [${apiResponse.content}]")
resourcePermissionCache.put(cacheKey, responseObject.data)
Expand All @@ -202,7 +202,7 @@ class CIAuthService @Autowired constructor(private val devopsAuthConfig: DevopsA
"action=${action.value}&resourceType=${resourceType.value}"
val request = Request.Builder().url(url).header(DEVOPS_BK_TOKEN, devopsAuthConfig.getBkciAuthToken())
.header(DEVOPS_UID, user).header(DEVOPS_PROJECT_ID, projectCode).get().build()
val apiResponse = HttpUtils.doRequest(okHttpClient, request, 2, setOf(HttpStatus.FORBIDDEN.value))
val apiResponse = HttpUtils.doRequest(okHttpClient, request, 2, allowHttpStatusSet)
val responseObject = objectMapper.readValue<BkciAuthListResponse>(apiResponse.content)
logger.debug("getUserResourceByPermission, requestUrl: [$url], result : [${apiResponse.content}]")
return responseObject.data
Expand All @@ -217,7 +217,7 @@ class CIAuthService @Autowired constructor(private val devopsAuthConfig: DevopsA
val url = "${devopsAuthConfig.getBkciAuthServer()}/auth/api/open/service/auth/projects/users/$user"
val request = Request.Builder().url(url).header(DEVOPS_BK_TOKEN, devopsAuthConfig.getBkciAuthToken())
.header(DEVOPS_GATEWAY_TAG, DEVOPS_PROD_V3).header(DEVOPS_UID, user).get().build()
val apiResponse = HttpUtils.doRequest(okHttpClient, request, 2, setOf(HttpStatus.FORBIDDEN.value))
val apiResponse = HttpUtils.doRequest(okHttpClient, request, 2, allowHttpStatusSet)
val responseObject = objectMapper.readValue<BkciAuthListResponse>(apiResponse.content)
logger.debug("getProjectListByUser, requestUrl: [$url], result : [${apiResponse.content}]")
return responseObject.data
Expand All @@ -229,6 +229,8 @@ class CIAuthService @Autowired constructor(private val devopsAuthConfig: DevopsA

companion object {
private val logger = LoggerFactory.getLogger(CIAuthService::class.java)
private val allowHttpStatusSet =
setOf(HttpStatus.FORBIDDEN.value, HttpStatus.BAD_REQUEST.value, HttpStatus.NOT_FOUND.value)
const val DEVOPS_BK_TOKEN = "X-DEVOPS-BK-TOKEN"
const val DEVOPS_UID = "X-DEVOPS-UID"
const val DEVOPS_PROJECT_ID = "X-DEVOPS-PROJECT-ID"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ object HttpUtils {
return ApiResponse(responseCode, responseContent)
}
throw RuntimeException(
"http request url ${request.url} failed, " +
"code: $responseCode, responseContent: $responseContent"
"http request url ${request.url} failed, code: $responseCode, responseContent: $responseContent"
)
} catch (e: Exception) {
if (retry > 0) {
Expand Down

0 comments on commit fb86d1e

Please sign in to comment.