Skip to content

Commit

Permalink
Merge remote-tracking branch 'github/master' into rbac
Browse files Browse the repository at this point in the history
  • Loading branch information
mingshewhe committed Dec 26, 2023
2 parents dadb30c + e4a01f9 commit 9eef2ed
Show file tree
Hide file tree
Showing 33 changed files with 885 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import com.tencent.devops.auth.dao.AuthResourceGroupDao
import com.tencent.devops.auth.service.AuthMonitorSpaceService
import com.tencent.devops.auth.service.AuthResourceCodeConverter
import com.tencent.devops.auth.service.AuthResourceService
import com.tencent.devops.auth.service.AuthProjectUserMetricsService
import com.tencent.devops.auth.service.AuthVerifyRecordService
import com.tencent.devops.auth.service.DeptService
import com.tencent.devops.auth.service.ItsmService
Expand Down Expand Up @@ -223,7 +224,8 @@ class RbacAuthConfiguration {
authResourceCodeConverter: AuthResourceCodeConverter,
permissionSuperManagerService: PermissionSuperManagerService,
rbacCacheService: RbacCacheService,
client: Client
client: Client,
authProjectUserMetricsService: AuthProjectUserMetricsService
) = RbacPermissionService(
authHelper = authHelper,
authResourceService = authResourceService,
Expand All @@ -232,7 +234,8 @@ class RbacAuthConfiguration {
authResourceCodeConverter = authResourceCodeConverter,
permissionSuperManagerService = permissionSuperManagerService,
rbacCacheService = rbacCacheService,
client = client
client = client,
authProjectUserMetricsService = authProjectUserMetricsService
)

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import com.tencent.devops.auth.service.AuthAuthorizationScopesService
import com.tencent.devops.auth.service.AuthResourceCodeConverter
import com.tencent.devops.auth.service.AuthResourceNameConverter
import com.tencent.devops.auth.service.AuthResourceService
import com.tencent.devops.auth.service.AuthProjectUserMetricsService
import com.tencent.devops.auth.service.ItsmService
import com.tencent.devops.auth.service.PermissionGradeManagerService
import com.tencent.devops.auth.service.PermissionGroupPoliciesService
Expand All @@ -66,14 +67,16 @@ class RbacServiceConfiguration {
authActionDao: AuthActionDao,
iamV2PolicyService: PolicyService,
iamConfiguration: IamConfiguration,
authResourceGroupConfigDao: AuthResourceGroupConfigDao
authResourceGroupConfigDao: AuthResourceGroupConfigDao,
authProjectUserMetricsService: AuthProjectUserMetricsService
) = RbacCacheService(
dslContext = dslContext,
authResourceTypeDao = authResourceTypeDao,
authActionDao = authActionDao,
policyService = iamV2PolicyService,
iamConfiguration = iamConfiguration,
authResourceGroupConfigDao = authResourceGroupConfigDao
authResourceGroupConfigDao = authResourceGroupConfigDao,
authUserDailyService = authProjectUserMetricsService
)

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class RbacCacheService constructor(
private val authActionDao: AuthActionDao,
private val policyService: PolicyService,
private val iamConfiguration: IamConfiguration,
private val authResourceGroupConfigDao: AuthResourceGroupConfigDao
private val authResourceGroupConfigDao: AuthResourceGroupConfigDao,
private val authUserDailyService: AuthProjectUserMetricsService
) {

companion object {
Expand Down Expand Up @@ -193,7 +194,11 @@ class RbacCacheService constructor(
.resources(listOf(resourceNode))
.build()

return policyService.verifyPermissions(queryPolicyDTO)
val result = policyService.verifyPermissions(queryPolicyDTO)
if (result) {
authUserDailyService.save(projectId = projectCode, userId = userId)
}
return result
} finally {
logger.info(
"It take(${System.currentTimeMillis() - startEpoch})ms to validate user project permission"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class RbacPermissionService constructor(
private val authResourceCodeConverter: AuthResourceCodeConverter,
private val permissionSuperManagerService: PermissionSuperManagerService,
private val rbacCacheService: RbacCacheService,
private val client: Client
private val client: Client,
private val authProjectUserMetricsService: AuthProjectUserMetricsService
) : PermissionService {
companion object {
private val logger = LoggerFactory.getLogger(RbacPermissionService::class.java)
Expand Down Expand Up @@ -209,7 +210,11 @@ class RbacPermissionService constructor(
.resources(listOf(resourceNode))
.build()

return policyService.verifyPermissions(queryPolicyDTO)
val result = policyService.verifyPermissions(queryPolicyDTO)
if (result) {
authProjectUserMetricsService.save(projectId = projectCode, userId = userId)
}
return result
} finally {
watcher.stop()
LogUtils.printCostTimeWE(watcher)
Expand Down Expand Up @@ -288,11 +293,15 @@ class RbacPermissionService constructor(
.attribute(attribute)
.system(iamConfiguration.systemId)
.build()
return policyService.batchVerifyPermissions(
val result = policyService.batchVerifyPermissions(
userId,
actionList,
listOf(resourceDTO)
)
if (result.values.any { it }) {
authProjectUserMetricsService.save(projectId = projectCode, userId = userId)
}
return result
} finally {
logger.info(
"It take(${System.currentTimeMillis() - startEpoch})ms to batch validate user resource permission|" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ import com.tencent.devops.auth.refresh.listener.AuthRefreshEventListener
import com.tencent.devops.auth.service.AuthUserBlackListService
import com.tencent.devops.auth.utils.HostUtils
import com.tencent.devops.common.client.ClientTokenService
import com.tencent.devops.common.event.dispatcher.pipeline.mq.MeasureEventDispatcher
import com.tencent.devops.common.event.dispatcher.pipeline.mq.MQ
import com.tencent.devops.common.web.mq.EXTEND_RABBIT_TEMPLATE_NAME
import org.springframework.amqp.core.Binding
import org.springframework.amqp.core.BindingBuilder
import org.springframework.amqp.core.DirectExchange
Expand All @@ -47,6 +49,7 @@ import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
Expand All @@ -61,6 +64,10 @@ class AuthCoreConfiguration {
@Bean
fun refreshDispatch(rabbitTemplate: RabbitTemplate) = AuthRefreshDispatch(rabbitTemplate)

@Bean
fun measureEventDispatcher(@Qualifier(EXTEND_RABBIT_TEMPLATE_NAME) extendRabbitTemplate: RabbitTemplate) =
MeasureEventDispatcher(extendRabbitTemplate)

@Bean
fun rabbitAdmin(connectionFactory: ConnectionFactory): RabbitAdmin {
return RabbitAdmin(connectionFactory)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2019 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.devops.auth.service

import com.google.common.cache.CacheBuilder
import com.google.common.hash.BloomFilter
import com.google.common.hash.Funnels
import com.tencent.devops.common.event.dispatcher.pipeline.mq.MeasureEventDispatcher
import com.tencent.devops.common.event.pojo.measure.ProjectUserDailyEvent
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.nio.charset.Charset
import java.time.LocalDate
import java.util.concurrent.TimeUnit

@Service
@Suppress("UnstableApiUsage")
class AuthProjectUserMetricsService @Autowired constructor(
private val measureEventDispatcher: MeasureEventDispatcher
) {

companion object {
private val logger = LoggerFactory.getLogger(AuthProjectUserMetricsService::class.java)
// 期待的用户数10w
private const val EXPECTED_USER_COUNT = 100000
// 错误率0.1%
private const val EXPECTED_FPP = 0.001
private val bloomFilterMap = CacheBuilder.newBuilder()
.maximumSize(2)
.expireAfterWrite(1, TimeUnit.DAYS)
.build<LocalDate, BloomFilter<String>>()
}

fun save(
projectId: String,
userId: String
) {
val theDate = LocalDate.now()
try {
val bloomKey = "${projectId}_$userId"
val bloomFilter = getBloomFilter(theDate)
if (!bloomFilter.mightContain(bloomKey)) {
measureEventDispatcher.dispatch(
ProjectUserDailyEvent(
projectId = projectId,
userId = userId,
theDate = theDate
)
)
bloomFilter.put(bloomKey)
}
} catch (ignored: Throwable) {
logger.error("save auth user error", ignored)
}
}

private fun getBloomFilter(theDate: LocalDate): BloomFilter<String> {
var bloomFilter = bloomFilterMap.getIfPresent(theDate)
if (bloomFilter == null) {
bloomFilter = BloomFilter.create(
Funnels.stringFunnel(Charset.defaultCharset()),
EXPECTED_USER_COUNT,
EXPECTED_FPP
)
bloomFilterMap.put(theDate, bloomFilter)
}
return bloomFilter!!
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ object MQ {
const val ROUTE_AUTH_RESOURCE_GROUP_MODIFY = "r.auth.resource.group.modify"
const val QUEUE_AUTH_RESOURCE_GROUP_MODIFY = "q.auth.resource.group.modify"

const val EXCHANGE_PROJECT_USER_DAILY_FANOUT = "e.metrics.project.user.daily.exchange.fanout"
const val QUEUE_PROJECT_USER_DAILY_METRICS = "q.metrics.project.user.daily.queue"

// 数据库分片
const val EXCHANGE_SHARDING_ROUTING_RULE_FANOUT = "e.sharding.routing.rule.exchange.fanout"
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ data class BuildEndPipelineMetricsData(
val buildId: String,
@ApiModelProperty("构建序号", required = true)
val buildNum: Int,
@ApiModelProperty("触发类型", required = true)
val trigger: String? = null,
@ApiModelProperty("代码库地址", required = false)
val repoUrl: String? = null,
@ApiModelProperty("代码库分支", required = false)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2019 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.devops.common.event.pojo.measure

import com.tencent.devops.common.event.annotation.Event
import com.tencent.devops.common.event.dispatcher.pipeline.mq.MQ
import io.swagger.annotations.ApiModelProperty
import java.time.LocalDate

@Event(exchange = MQ.EXCHANGE_PROJECT_USER_DAILY_FANOUT)
data class ProjectUserDailyEvent(
@ApiModelProperty("项目ID")
override val projectId: String,
@ApiModelProperty("用户ID")
val userId: String,
@ApiModelProperty("统计日期")
val theDate: LocalDate
) : IMeasureEvent(projectId = projectId, pipelineId = "", buildId = "")
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ package com.tencent.devops.metrics.config

import com.tencent.devops.common.event.dispatcher.pipeline.mq.MQ
import com.tencent.devops.common.event.dispatcher.pipeline.mq.MQ.QUEUE_DISPATCH_JOB_METRICS
import com.tencent.devops.common.event.dispatcher.pipeline.mq.MQ.QUEUE_PROJECT_USER_DAILY_METRICS
import com.tencent.devops.common.event.dispatcher.pipeline.mq.Tools
import com.tencent.devops.common.web.mq.EXTEND_CONNECTION_FACTORY_NAME
import com.tencent.devops.common.web.mq.EXTEND_RABBIT_ADMIN_NAME
import com.tencent.devops.metrics.listener.BuildEndMetricsDataReportListener
import com.tencent.devops.metrics.listener.DispatchJobMetricsListener
import com.tencent.devops.metrics.listener.LabelChangeMetricsDataSyncListener
import com.tencent.devops.metrics.listener.ProjectUserDailyMetricsListener
import org.springframework.amqp.core.Binding
import org.springframework.amqp.core.BindingBuilder
import org.springframework.amqp.core.FanoutExchange
Expand Down Expand Up @@ -94,6 +96,49 @@ class MetricsListenerConfiguration {
)
}

@Bean
fun projectUserDailyMetricsQueue() = Queue(QUEUE_PROJECT_USER_DAILY_METRICS)

/**
* 插件监控数据上报广播交换机
*/
@Bean
fun projectUserDailyMetricsFanoutExchange(): FanoutExchange {
val fanoutExchange = FanoutExchange(MQ.EXCHANGE_PROJECT_USER_DAILY_FANOUT, true, false)
fanoutExchange.isDelayed = true
return fanoutExchange
}

@Bean
fun projectUserDailyMetricsQueueBind(
@Autowired projectUserDailyMetricsQueue: Queue,
@Autowired projectUserDailyMetricsFanoutExchange: FanoutExchange
): Binding {
return BindingBuilder.bind(projectUserDailyMetricsQueue)
.to(projectUserDailyMetricsFanoutExchange)
}

@Bean
fun projectUserDailyMetricsListenerContainer(
@Qualifier(EXTEND_CONNECTION_FACTORY_NAME) @Autowired connectionFactory: ConnectionFactory,
@Autowired projectUserDailyMetricsQueue: Queue,
@Qualifier(value = EXTEND_RABBIT_ADMIN_NAME) @Autowired rabbitAdmin: RabbitAdmin,
@Autowired listener: ProjectUserDailyMetricsListener,
@Autowired messageConverter: Jackson2JsonMessageConverter
): SimpleMessageListenerContainer {
return Tools.createSimpleMessageListenerContainer(
connectionFactory = connectionFactory,
queue = projectUserDailyMetricsQueue,
rabbitAdmin = rabbitAdmin,
buildListener = listener,
messageConverter = messageConverter,
startConsumerMinInterval = 1000,
consecutiveActiveTrigger = 5,
concurrency = 5,
maxConcurrency = 50
)
}

@Bean
fun pipelineLabelChangeMetricsDataSyncQueue() = Queue(QUEUE_PIPELINE_LABEL_CHANGE_METRICS_DATA_SYNC)

Expand Down
Loading

0 comments on commit 9eef2ed

Please sign in to comment.