Skip to content

Commit

Permalink
feat: 处理代码扫描的问题 TencentBlueKing#1086
Browse files Browse the repository at this point in the history
  • Loading branch information
felixncheng committed Sep 11, 2023
1 parent fd6a088 commit 840429b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ import org.springframework.data.redis.connection.RedisStringCommands.SetOption.U
import org.springframework.data.redis.core.RedisTemplate
import org.springframework.data.redis.core.types.Expiration
import org.springframework.stereotype.Service
import java.util.*
import java.util.Base64
import java.util.concurrent.TimeUnit
import kotlin.collections.HashMap
import kotlin.collections.LinkedHashMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import io.kubernetes.client.openapi.ApiException
import io.kubernetes.client.openapi.apis.CoreV1Api
import io.kubernetes.client.openapi.models.V1Secret
import org.slf4j.LoggerFactory
import java.util.*
import java.util.Base64

class K8SHelper(k8sProp: KubernetesExecutionClusterProperties) {

Expand All @@ -48,7 +48,7 @@ class K8SHelper(k8sProp: KubernetesExecutionClusterProperties) {
secretName: String,
dockerServer: String,
dockerUserName: String,
dockerPassword: String
dockerPassword: String,
): V1Secret {
val dockerAuthBytes = "$dockerUserName:$dockerPassword".toByteArray()
val dockerAuth = Base64.getEncoder().encodeToString(dockerAuthBytes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package com.tencent.bkrepo.common.analysis.pojo.scanner.utils

import com.github.dockerjava.api.DockerClient
import com.github.dockerjava.api.command.PullImageCmd
import com.github.dockerjava.api.command.PullImageResultCallback
import com.github.dockerjava.api.command.WaitContainerResultCallback
import com.github.dockerjava.api.model.AuthConfig
Expand Down Expand Up @@ -73,11 +74,7 @@ object DockerUtils {
logger.info("pulling image: $tag")
val elapsedTime = measureTimeMillis {
val result = pullImageCmd(tag)
.withAuthConfig(
AuthConfig()
.withUsername(userName)
.withPassword(password)
)
.withAuthConfigIfNeed(userName, password)
.exec(PullImageResultCallback())
.awaitCompletion(DEFAULT_PULL_IMAGE_DURATION, TimeUnit.MILLISECONDS)
if (!result) {
Expand All @@ -92,7 +89,7 @@ object DockerUtils {
userName: String?,
password: String?,
hostConfig: HostConfig? = null,
cmd: List<String>? = null
cmd: List<String>? = null,
): String {
// 拉取镜像
pullImage(image, userName, password)
Expand Down Expand Up @@ -122,7 +119,7 @@ object DockerUtils {
binds: Binds,
maxSize: Long,
mem: Long,
withPrivileged: Boolean = false
withPrivileged: Boolean = false,
): HostConfig {
return HostConfig().apply {
withBinds(binds)
Expand All @@ -149,4 +146,15 @@ object DockerUtils {
}
}
}

private fun PullImageCmd.withAuthConfigIfNeed(userName: String?, password: String?): PullImageCmd {
if (userName != null && password != null) {
withAuthConfig(
AuthConfig()
.withUsername(userName)
.withPassword(password),
)
}
return this
}
}

0 comments on commit 840429b

Please sign in to comment.