Skip to content

Commit

Permalink
feat: 支持上报制品分析许可与敏感信息结果 #2970
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlkl committed Feb 25, 2025
1 parent 2376894 commit 33b02a4
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2025 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.analyst.component

import com.tencent.bkrepo.analyst.NODE_SHA256
import com.tencent.bkrepo.analyst.configuration.ReportExportProperties
import com.tencent.bkrepo.analyst.pojo.report.Report
import com.tencent.bkrepo.analyst.utils.buildScanExecutorResult
import com.tencent.bkrepo.analyst.utils.buildSubScanTask
import com.tencent.bkrepo.common.stream.event.supplier.MessageSupplier
import io.mockk.every
import io.mockk.mockk
import io.mockk.slot
import io.mockk.verify
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance

@DisplayName("制品报告上报测试")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ReportExporterTest {

private lateinit var reportExporter: ReportExporter

private lateinit var messageSupplier: MessageSupplier

@BeforeAll
fun beforeAll() {
val properties = ReportExportProperties()
properties.enabled = true
properties.topic = "test-topic"
messageSupplier = mockk()
reportExporter = ReportExporter(properties, messageSupplier)
}

@Test
fun testExport() {
// 准备数据
val reportSlot = slot<Report>()
every { messageSupplier.delegateToSupplier(capture(reportSlot), any(), any(), any(), any()) }.returns(Unit)
val result = buildScanExecutorResult()
val securityResult = result.output!!.result!!.securityResults!![0]
val newSecurityResults = (0..2000).map { securityResult.copy(pkgName = securityResult.pkgName + "$it") }
val newResult = result.copy(
output = result.output!!.copy(
result = result.output!!.result!!.copy(
securityResults = newSecurityResults
)
)
)

// 执行测试
reportExporter.export(buildSubScanTask("taskId", NODE_SHA256), newResult)


// 验证
verify(exactly = 5) {
// license与sensitive各上报1次,security上报3次
messageSupplier.delegateToSupplier<Report>(any(), any(), any(), any(), any())
}
with(reportSlot.captured) {
// 验证基本信息
assertEquals("subTaskId", taskId)
assertEquals(NODE_SHA256, sha256)

// 验证组件数量不超过分块大小
assertTrue(components.size <= 1000)

// 验证组件内容
components.forEachIndexed { index, component ->
assertEquals(securityResult.pkgName + index, component.name)
assertEquals(1, component.vulnerabilities.size)
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,20 @@ import com.tencent.bkrepo.analyst.model.TFileScanResult
import com.tencent.bkrepo.analyst.model.TScanPlan
import com.tencent.bkrepo.analyst.model.TScanResult
import com.tencent.bkrepo.analyst.model.TScanTask
import com.tencent.bkrepo.analyst.model.TSubScanTask
import com.tencent.bkrepo.analyst.pojo.ScanTaskStatus
import com.tencent.bkrepo.analyst.pojo.ScanTriggerType
import com.tencent.bkrepo.common.analysis.pojo.scanner.CveOverviewKey
import com.tencent.bkrepo.common.analysis.pojo.scanner.Level
import com.tencent.bkrepo.common.analysis.pojo.scanner.SubScanTaskStatus
import com.tencent.bkrepo.common.analysis.pojo.scanner.standard.LicenseResult
import com.tencent.bkrepo.common.analysis.pojo.scanner.standard.Result
import com.tencent.bkrepo.common.analysis.pojo.scanner.standard.SecurityResult
import com.tencent.bkrepo.common.analysis.pojo.scanner.standard.SensitiveResult
import com.tencent.bkrepo.common.analysis.pojo.scanner.standard.StandardScanExecutorResult
import com.tencent.bkrepo.common.analysis.pojo.scanner.standard.StandardScanner
import com.tencent.bkrepo.common.analysis.pojo.scanner.standard.ToolOutput
import com.tencent.bkrepo.common.analysis.pojo.scanner.standard.VersionPaths
import com.tencent.bkrepo.common.artifact.pojo.RepositoryType
import com.tencent.bkrepo.common.storage.core.locator.HashFileLocator
import java.time.LocalDateTime
Expand Down Expand Up @@ -129,6 +138,86 @@ fun buildArchiveSubScanTask(
),
)

fun buildSubScanTask(taskId: String, sha256: String, now: LocalDateTime = LocalDateTime.now()) = TSubScanTask(
id = "subTaskId",
createdBy = UT_USER,
createdDate = now,
lastModifiedBy = UT_USER,
lastModifiedDate = now,
startDateTime = now,
triggerType = ScanTriggerType.MANUAL.name,
parentScanTaskId = taskId,
planId = UT_PLAN_ID,
projectId = PROJECT_ID,
repoName = REPO,
repoType = RepositoryType.GENERIC.name,
fullPath = fileLocator.locate(sha256),
artifactName = "demo-1.0.0.jar",
status = SubScanTaskStatus.SUCCESS.name,
executedTimes = 1,
scanner = UT_SCANNER,
scannerType = StandardScanner.TYPE,
sha256 = sha256,
size = 1024L,
packageSize = 1024L,
credentialsKey = UT_CREDENTIALS_KEY,
)

fun buildScanExecutorResult(): StandardScanExecutorResult {
val securityResults = listOf(
SecurityResult(
vulId = "CVE-123",
versionsPaths = mutableSetOf(
VersionPaths("1.1", mutableSetOf("/a/b/c.x", "/b/c/d.x")),
VersionPaths("1.2", mutableSetOf("/a/b/c.x", "/b/c/d.x"))
),
cvss = 1.1,
pkgName = "test-pkg",
pkgVersions = mutableSetOf("1.1", "1.2"),
severity = Level.CRITICAL.name,
),
SecurityResult(
vulId = "CVE-123",
versionsPaths = mutableSetOf(
VersionPaths("1.1", mutableSetOf("/a/b/c.x", "/b/c/d.x")),
VersionPaths("1.2", mutableSetOf("/a/b/c.x", "/b/c/d.x"))
),
cvss = 1.1,
pkgName = "test-pkg2",
pkgVersions = mutableSetOf("1.1", "1.2"),
severity = Level.CRITICAL.name,
),
)

val licenseResults = listOf(
LicenseResult(
pkgName = "test-pkg",
pkgVersions = mutableSetOf("1.1", "1.2"),
licenseName = "MIT",
versionsPaths = mutableSetOf(
VersionPaths("1.1", mutableSetOf("/a/b/c.x", "/b/c/d.x")),
VersionPaths("1.2", mutableSetOf("/a/b/c.x", "/b/c/d.x"))
),
)
)

val sensitiveResults = listOf(
SensitiveResult("/a/b/c", "AWS", "xxx****xxxx"),
SensitiveResult("/a/b/e", "EMAIL", "xxx****xxxx")
)

return StandardScanExecutorResult(
ToolOutput(
status = SubScanTaskStatus.SUCCESS.name,
result = Result(
securityResults = securityResults,
licenseResults = licenseResults,
sensitiveResults = sensitiveResults,
)
)
)
}

fun buildFileResult(now: LocalDateTime, sha256: String, taskId: String) = TFileScanResult(
id = null,
lastModifiedDate = now,
Expand Down

0 comments on commit 33b02a4

Please sign in to comment.