Skip to content

Commit

Permalink
feat: 构件传输记录指标增加userId #2448
Browse files Browse the repository at this point in the history
* feat : 修复权限校验bug #2375 

* feat : 修复权限校验bug #2375

* feat : 修复权限校验bug #2375

* feat : 修复权限校验bug #2375

* feat : 修复权限校验bug #2375 

* feat : 修复权限校验bug #2375

* feat : 修复权限校验bug #2375

* feat : 修复权限校验bug #2375

* feat : 修复权限校验bug #2375

* feat : 修复权限校验bug #2375

* feat: Commit-Edge模式兼容独立集群模式 #2298

* feat: 添加 node_download 类型 #2380  

* feat: 添加 node_download 类型 #2380

* feat: 添加 download 类型 #2380

* feat : 修复权限校验bug #2375

* feat: Commit-Edge模式兼容独立集群模式 #2298

* feat: 构件传输记录指标增加userId #2448

* feat: 构件传输记录指标增加userId #2448

* feat: 构件传输记录指标增加userId #2448

---------

Co-authored-by: owen <[email protected]>
Co-authored-by: zacYL <[email protected]>
  • Loading branch information
3 people authored Aug 1, 2024
1 parent d97bda8 commit 8c69b82
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@ package com.tencent.bkrepo.auth.pojo.enums
enum class ActionTypeMapping(val resType: String, val pAction: String) {
PROJECT_MANAGE(ResourceType.PROJECT.name, PermissionAction.MANAGE.name),
PROJECT_VIEW(ResourceType.PROJECT.name, PermissionAction.READ.name),
PROJECT_DOWNLOAD(ResourceType.PROJECT.name, PermissionAction.DOWNLOAD.name),
PROJECT_EDIT(ResourceType.PROJECT.name, PermissionAction.UPDATE.name),
REPO_CREATE(ResourceType.PROJECT.name, PermissionAction.WRITE.name),
REPO_MANAGE(ResourceType.REPO.name, PermissionAction.MANAGE.name),
REPO_VIEW(ResourceType.REPO.name, PermissionAction.READ.name),
REPO_DOWNLOAD(ResourceType.REPO.name, PermissionAction.DOWNLOAD.name),
REPO_EDIT(ResourceType.REPO.name, PermissionAction.UPDATE.name),
REPO_DELETE(ResourceType.REPO.name, PermissionAction.DELETE.name),
NODE_CREATE(ResourceType.REPO.name, PermissionAction.WRITE.name),
NODE_VIEW(ResourceType.NODE.name, PermissionAction.VIEW.name),
NODE_DOWNLOAD(ResourceType.NODE.name, PermissionAction.READ.name),
NODE_READ(ResourceType.NODE.name, PermissionAction.READ.name),
NODE_DOWNLOAD(ResourceType.NODE.name, PermissionAction.DOWNLOAD.name),
NODE_EDIT(ResourceType.NODE.name, PermissionAction.UPDATE.name),
NODE_WRITE(ResourceType.NODE.name, PermissionAction.WRITE.name),
NODE_DELETE(ResourceType.NODE.name, PermissionAction.DELETE.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import com.tencent.bkrepo.auth.model.TAccount
import com.tencent.bkrepo.auth.model.TPermission
import com.tencent.bkrepo.auth.pojo.enums.PermissionAction
import com.tencent.bkrepo.auth.pojo.enums.PermissionAction.READ
import com.tencent.bkrepo.auth.pojo.enums.PermissionAction.DOWNLOAD
import com.tencent.bkrepo.auth.pojo.enums.PermissionAction.MANAGE
import com.tencent.bkrepo.auth.pojo.oauth.AuthorizationGrantType
import com.tencent.bkrepo.auth.pojo.permission.CheckPermissionRequest
Expand Down Expand Up @@ -92,10 +93,10 @@ class PermissionHelper constructor(
if (queryRoles.isEmpty()) return false

val result = roleRepository.findByTypeAndProjectIdAndAdminAndRepoNameAndIdIn(
projectId = request.projectId!!,
type = RoleType.REPO,
repoName = request.repoName!!,
projectId = request.projectId!!,
admin = true,
repoName = request.repoName!!,
ids = queryRoles
)
if (result.isNotEmpty()) return true
Expand Down Expand Up @@ -185,7 +186,8 @@ class PermissionHelper constructor(
}

fun checkProjectReadAction(request: CheckPermissionRequest, isProjectUser: Boolean): Boolean {
return request.projectId != null && request.action == READ.name && isProjectUser
val readeOrdownload = request.action == READ.name || request.action == DOWNLOAD.name
return request.projectId != null && readeOrdownload && isProjectUser
}

fun getPermissionPathFromConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ import com.tencent.bkrepo.auth.constant.PIPELINE
import com.tencent.bkrepo.auth.constant.REPORT
import com.tencent.bkrepo.auth.dao.PersonalPathDao
import com.tencent.bkrepo.auth.dao.RepoAuthConfigDao
import com.tencent.bkrepo.auth.pojo.enums.PermissionAction.DOWNLOAD
import com.tencent.bkrepo.auth.pojo.enums.PermissionAction.VIEW
import com.tencent.bkrepo.auth.pojo.enums.PermissionAction.WRITE
import com.tencent.bkrepo.auth.pojo.enums.PermissionAction.MANAGE
import com.tencent.bkrepo.auth.pojo.enums.PermissionAction.READ
import com.tencent.bkrepo.auth.pojo.enums.ResourceType.NODE
Expand Down Expand Up @@ -194,19 +197,26 @@ class DevopsPermissionServiceImpl constructor(
return false
}
when (repoName) {
CUSTOM, LOG, REPORT -> {
CUSTOM, LOG -> {
return checkDevopsCustomPermission(request)
}
PIPELINE -> {
return checkDevopsPipelinePermission(request)
}
REPORT -> {
return checkDevopsReportPermission(request.action)
}
else -> {
return checkRepoNotInDevops(request)
}
}
}
}

private fun checkDevopsReportPermission(action: String): Boolean {
return action == READ.name || action == WRITE.name || action == VIEW.name || action == DOWNLOAD.name
}

private fun checkDevopsCustomPermission(request: CheckPermissionRequest): Boolean {
logger.debug("check devops custom permission request [$request]")
with(request) {
Expand All @@ -221,12 +231,11 @@ class DevopsPermissionServiceImpl constructor(
private fun checkRepoNotInDevops(request: CheckPermissionRequest): Boolean {
logger.debug("check repo not in devops request [$request]")
with(request) {
val isDevopsProjectMember = isDevopsProjectMember(uid, projectId!!, action) ||
isUserLocalProjectUser(uid, projectId!!)
val isDevopsProjectMember = isDevopsProjectMember(uid, projectId!!, action)
if (needCheckPathPermission(resourceType, projectId!!, repoName!!)) {
return checkNodeAction(request, null, isDevopsProjectMember)
}
return isDevopsProjectMember
return isDevopsProjectMember || super.checkPermission(request)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ open class PermissionServiceImpl constructor(
return permHelper.isUserLocalProjectAdmin(userId, projectId)
}

fun isUserLocalProjectUser(userId: String, projectId: String): Boolean {
private fun isUserLocalProjectUser(userId: String, projectId: String): Boolean {
return permHelper.isUserLocalProjectUser(userId, projectId)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import com.tencent.bkrepo.common.artifact.constant.DEFAULT_STORAGE_KEY
import com.tencent.bkrepo.common.artifact.event.ArtifactReceivedEvent
import com.tencent.bkrepo.common.artifact.event.ArtifactResponseEvent
import com.tencent.bkrepo.common.artifact.event.ChunkArtifactTransferEvent
import com.tencent.bkrepo.common.artifact.hash.md5
import com.tencent.bkrepo.common.artifact.metrics.ArtifactCacheMetrics
import com.tencent.bkrepo.common.artifact.metrics.ArtifactMetrics
import com.tencent.bkrepo.common.artifact.metrics.ArtifactMetricsProperties
Expand Down Expand Up @@ -106,7 +107,8 @@ class ArtifactTransferListener(
host = artifactMetricsProperties.host,
builderAgentList = artifactMetricsProperties.builderAgentList,
clientAgentList = artifactMetricsProperties.clientAgentList
).name
).name,
userId = SecurityUtils.getUserId().md5()
)
if (SecurityUtils.getUserId() != SYSTEM_USER) {
projectUsageStatisticsService.inc(projectId = projectId, receivedBytes = throughput.bytes)
Expand Down Expand Up @@ -151,7 +153,8 @@ class ArtifactTransferListener(
host = artifactMetricsProperties.host,
builderAgentList = artifactMetricsProperties.builderAgentList,
clientAgentList = artifactMetricsProperties.clientAgentList
).name
).name,
userId = SecurityUtils.getUserId().md5()
)
if (SecurityUtils.getUserId() != SYSTEM_USER) {
projectUsageStatisticsService.inc(projectId = projectId, responseBytes = throughput.bytes)
Expand Down Expand Up @@ -207,7 +210,8 @@ class ArtifactTransferListener(
host = artifactMetricsProperties.host,
builderAgentList = artifactMetricsProperties.builderAgentList,
clientAgentList = artifactMetricsProperties.clientAgentList
).name
).name,
userId = SecurityUtils.getUserId().md5()
)
if (artifactMetricsProperties.collectByLog) {
logger.info(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ data class ArtifactTransferRecord(
val fullPath: String,
@Column(name = "agent")
val agent: String,
@Column(name = "userId")
val userId: String,
) {
companion object {
const val RECEIVE = "RECEIVE"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class ArtifactTransferRecordLog(
val repoName: String = record.repoName
val agent: String = record.agent
val fullPath: String = record.fullPath
val userId: String = record.userId
val service: String? = commonTag["service"]
val instance: String? = commonTag["instance"]
val host: String? = commonTag["host"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class ArtifactMetricsExporter(
labels[ArtifactTransferRecord::elapsed.name] = record.elapsed.toString()
labels[ArtifactTransferRecord::type.name] = record.type
labels[ArtifactTransferRecord::agent.name] = record.agent
labels[ArtifactTransferRecord::userId.name] = record.userId
return labels
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ class OperateAutoConfiguration {
clusterProperties: ClusterProperties
): OperateLogService {
return if (clusterProperties.role == ClusterNodeType.EDGE &&
clusterProperties.architecture == ClusterArchitecture.COMMIT_EDGE
clusterProperties.architecture == ClusterArchitecture.COMMIT_EDGE &&
clusterProperties.commitEdge.oplog.enabled
) {
CommitEdgeOperateLogServiceImpl(operateProperties, operateLogDao, permissionManager, clusterProperties)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ package com.tencent.bkrepo.common.service.cluster.properties.commitedge
data class CommitEdgeProperties(
var repo: RepoProperties = RepoProperties(),
var `package`: PackageProperties = PackageProperties(),
var auth: AuthProperties = AuthProperties()
var auth: AuthProperties = AuthProperties(),
var oplog: OpLogProperties = OpLogProperties()
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2024 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.service.cluster.properties.commitedge

data class OpLogProperties(
var enabled: Boolean = false,
)
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class CommitEdgeCenterFsServiceImpl(
) {
override fun buildTNode(request: NodeCreateRequest): TNode {
val tNode = super.buildTNode(request)
tNode.clusterNames = setOf(SecurityUtils.getClusterName() ?: clusterProperties.self.name!!)
tNode.clusterNames = SecurityUtils.getClusterName()?.let { setOf(it) }
return tNode
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@

package com.tencent.bkrepo.repository.service.fs.impl.edge

import com.tencent.bkrepo.common.service.cluster.properties.ClusterProperties
import com.tencent.bkrepo.common.artifact.util.ClusterUtils.reportMetadataToCenter
import com.tencent.bkrepo.common.service.cluster.condition.CommitEdgeEdgeCondition
import com.tencent.bkrepo.common.service.cluster.properties.ClusterProperties
import com.tencent.bkrepo.common.service.feign.FeignClientFactory
import com.tencent.bkrepo.repository.api.cluster.ClusterFsNodeClient
import com.tencent.bkrepo.repository.dao.NodeDao
Expand All @@ -53,18 +54,28 @@ class EdgeFsServiceImpl(
by lazy { FeignClientFactory.create(clusterProperties.center, "repository", clusterProperties.self.name) }

override fun createNode(createRequest: NodeCreateRequest): NodeDetail {
centerNodeClient.createNode(createRequest)
return super.createNode(createRequest)
with(createRequest) {
if (reportMetadataToCenter(projectId, repoName)) {
centerNodeClient.createNode(this)
}
return super.createNode(this)
}
}

override fun setLength(setLengthRequest: NodeSetLengthRequest) {
centerNodeClient.setLength(setLengthRequest)
super.setLength(setLengthRequest)
with(setLengthRequest) {
if (reportMetadataToCenter(projectId, repoName)) {
centerNodeClient.setLength(this)
}
super.setLength(this)
}
}

override fun buildTNode(request: NodeCreateRequest): TNode {
val tNode = super.buildTNode(request)
tNode.clusterNames = setOf(clusterProperties.self.name!!)
if (reportMetadataToCenter(request.projectId, request.repoName)) {
tNode.clusterNames = setOf(clusterProperties.self.name!!)
}
return tNode
}
}

0 comments on commit 8c69b82

Please sign in to comment.