feat(P1/P2): 完成TDD开发及P1/P2设计文档
## 设计文档 - multi_role_permission_design: 多角色权限设计 (CONDITIONAL GO) - audit_log_enhancement_design: 审计日志增强 (CONDITIONAL GO) - routing_strategy_template_design: 路由策略模板 (CONDITIONAL GO) - sso_saml_technical_research: SSO/SAML调研 (CONDITIONAL GO) - compliance_capability_package_design: 合规能力包设计 (CONDITIONAL GO) ## TDD开发成果 - IAM模块: supply-api/internal/iam/ (111个测试) - 审计日志模块: supply-api/internal/audit/ (40+测试) - 路由策略模块: gateway/internal/router/ (33+测试) - 合规能力包: gateway/internal/compliance/ + scripts/ci/compliance/ ## 规范文档 - parallel_agent_output_quality_standards: 并行Agent产出质量规范 - project_experience_summary: 项目经验总结 (v2) - 2026-04-02-p1-p2-tdd-execution-plan: TDD执行计划 ## 评审报告 - 5个CONDITIONAL GO设计文档评审报告 - fix_verification_report: 修复验证报告 - full_verification_report: 全面质量验证报告 - tdd_module_quality_verification: TDD模块质量验证 - tdd_execution_summary: TDD执行总结 依据: Superpowers执行框架 + TDD规范
This commit is contained in:
186
supply-api/internal/audit/events/cred_events.go
Normal file
186
supply-api/internal/audit/events/cred_events.go
Normal file
@@ -0,0 +1,186 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// CRED事件类别常量
|
||||
const (
|
||||
CategoryCRED = "CRED"
|
||||
SubCategoryEXPOSE = "EXPOSE"
|
||||
SubCategoryINGRESS = "INGRESS"
|
||||
SubCategoryROTATE = "ROTATE"
|
||||
SubCategoryREVOKE = "REVOKE"
|
||||
SubCategoryVALIDATE = "VALIDATE"
|
||||
SubCategoryDIRECT = "DIRECT"
|
||||
)
|
||||
|
||||
// CRED事件列表
|
||||
var credEvents = []string{
|
||||
// 凭证暴露事件 (CRED-EXPOSE)
|
||||
"CRED-EXPOSE-RESPONSE", // 响应中暴露凭证
|
||||
"CRED-EXPOSE-LOG", // 日志中暴露凭证
|
||||
"CRED-EXPOSE-EXPORT", // 导出文件中暴露凭证
|
||||
|
||||
// 凭证入站事件 (CRED-INGRESS)
|
||||
"CRED-INGRESS-PLATFORM", // 平台凭证入站
|
||||
"CRED-INGRESS-SUPPLIER", // 供应商凭证入站
|
||||
|
||||
// 凭证轮换事件 (CRED-ROTATE)
|
||||
"CRED-ROTATE",
|
||||
|
||||
// 凭证吊销事件 (CRED-REVOKE)
|
||||
"CRED-REVOKE",
|
||||
|
||||
// 凭证验证事件 (CRED-VALIDATE)
|
||||
"CRED-VALIDATE",
|
||||
|
||||
// 直连绕过事件 (CRED-DIRECT)
|
||||
"CRED-DIRECT-SUPPLIER", // 直连供应商
|
||||
"CRED-DIRECT-BYPASS", // 绕过直连
|
||||
}
|
||||
|
||||
// CRED事件结果码映射
|
||||
var credResultCodes = map[string]string{
|
||||
"CRED-EXPOSE-RESPONSE": "SEC_CRED_EXPOSED",
|
||||
"CRED-EXPOSE-LOG": "SEC_CRED_EXPOSED",
|
||||
"CRED-EXPOSE-EXPORT": "SEC_CRED_EXPOSED",
|
||||
"CRED-INGRESS-PLATFORM": "CRED_INGRESS_OK",
|
||||
"CRED-INGRESS-SUPPLIER": "CRED_INGRESS_OK",
|
||||
"CRED-DIRECT-SUPPLIER": "SEC_DIRECT_BYPASS",
|
||||
"CRED-DIRECT-BYPASS": "SEC_DIRECT_BYPASS",
|
||||
"CRED-ROTATE": "CRED_ROTATE_OK",
|
||||
"CRED-REVOKE": "CRED_REVOKE_OK",
|
||||
"CRED-VALIDATE": "CRED_VALIDATE_OK",
|
||||
}
|
||||
|
||||
// CRED指标名称映射
|
||||
var credMetricNames = map[string]string{
|
||||
"CRED-EXPOSE-RESPONSE": "supplier_credential_exposure_events",
|
||||
"CRED-EXPOSE-LOG": "supplier_credential_exposure_events",
|
||||
"CRED-EXPOSE-EXPORT": "supplier_credential_exposure_events",
|
||||
"CRED-INGRESS-PLATFORM": "platform_credential_ingress_coverage_pct",
|
||||
"CRED-INGRESS-SUPPLIER": "platform_credential_ingress_coverage_pct",
|
||||
"CRED-DIRECT-SUPPLIER": "direct_supplier_call_by_consumer_events",
|
||||
"CRED-DIRECT-BYPASS": "direct_supplier_call_by_consumer_events",
|
||||
}
|
||||
|
||||
// GetCREDEvents 返回所有CRED事件
|
||||
func GetCREDEvents() []string {
|
||||
return credEvents
|
||||
}
|
||||
|
||||
// GetCREDExposeEvents 返回所有凭证暴露事件
|
||||
func GetCREDExposeEvents() []string {
|
||||
return []string{
|
||||
"CRED-EXPOSE-RESPONSE",
|
||||
"CRED-EXPOSE-LOG",
|
||||
"CRED-EXPOSE-EXPORT",
|
||||
}
|
||||
}
|
||||
|
||||
// GetCREDFngressEvents 返回所有凭证入站事件
|
||||
func GetCREDFngressEvents() []string {
|
||||
return []string{
|
||||
"CRED-INGRESS-PLATFORM",
|
||||
"CRED-INGRESS-SUPPLIER",
|
||||
}
|
||||
}
|
||||
|
||||
// GetCREDDnirectEvents 返回所有直连绕过事件
|
||||
func GetCREDDnirectEvents() []string {
|
||||
return []string{
|
||||
"CRED-DIRECT-SUPPLIER",
|
||||
"CRED-DIRECT-BYPASS",
|
||||
}
|
||||
}
|
||||
|
||||
// GetCREDEventCategory 返回CRED事件的类别
|
||||
func GetCREDEventCategory(eventName string) string {
|
||||
if strings.HasPrefix(eventName, "CRED-") {
|
||||
return CategoryCRED
|
||||
}
|
||||
if eventName == "CRED-ROTATE" || eventName == "CRED-REVOKE" || eventName == "CRED-VALIDATE" {
|
||||
return CategoryCRED
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetCREDEventSubCategory 返回CRED事件的子类别
|
||||
func GetCREDEventSubCategory(eventName string) string {
|
||||
if strings.HasPrefix(eventName, "CRED-EXPOSE") {
|
||||
return SubCategoryEXPOSE
|
||||
}
|
||||
if strings.HasPrefix(eventName, "CRED-INGRESS") {
|
||||
return SubCategoryINGRESS
|
||||
}
|
||||
if strings.HasPrefix(eventName, "CRED-DIRECT") {
|
||||
return SubCategoryDIRECT
|
||||
}
|
||||
if strings.HasPrefix(eventName, "CRED-ROTATE") {
|
||||
return SubCategoryROTATE
|
||||
}
|
||||
if strings.HasPrefix(eventName, "CRED-REVOKE") {
|
||||
return SubCategoryREVOKE
|
||||
}
|
||||
if strings.HasPrefix(eventName, "CRED-VALIDATE") {
|
||||
return SubCategoryVALIDATE
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// IsValidCREDEvent 检查事件名称是否为有效的CRED事件
|
||||
func IsValidCREDEvent(eventName string) bool {
|
||||
for _, e := range credEvents {
|
||||
if e == eventName {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// IsCREDExposeEvent 检查是否为凭证暴露事件(M-013相关)
|
||||
func IsCREDExposeEvent(eventName string) bool {
|
||||
return strings.HasPrefix(eventName, "CRED-EXPOSE")
|
||||
}
|
||||
|
||||
// IsCREDFngressEvent 检查是否为凭证入站事件(M-014相关)
|
||||
func IsCREDFngressEvent(eventName string) bool {
|
||||
return strings.HasPrefix(eventName, "CRED-INGRESS")
|
||||
}
|
||||
|
||||
// IsCREDDnirectEvent 检查是否为直连绕过事件(M-015相关)
|
||||
func IsCREDDnirectEvent(eventName string) bool {
|
||||
return strings.HasPrefix(eventName, "CRED-DIRECT")
|
||||
}
|
||||
|
||||
// GetCREDMetricName 获取CRED事件对应的指标名称
|
||||
func GetCREDMetricName(eventName string) string {
|
||||
if metric, ok := credMetricNames[eventName]; ok {
|
||||
return metric
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetCREDEventResultCode 获取CRED事件对应的结果码
|
||||
func GetCREDEventResultCode(eventName string) string {
|
||||
if code, ok := credResultCodes[eventName]; ok {
|
||||
return code
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// IsCREDExposeEvent 检查是否为M-013事件(凭证暴露)
|
||||
func IsM013RelatedEvent(eventName string) bool {
|
||||
return IsCREDExposeEvent(eventName)
|
||||
}
|
||||
|
||||
// IsCREDFngressEvent 检查是否为M-014事件(凭证入站)
|
||||
func IsM014RelatedEvent(eventName string) bool {
|
||||
return IsCREDFngressEvent(eventName)
|
||||
}
|
||||
|
||||
// IsCREDDnirectEvent 检查是否为M-015事件(直连绕过)
|
||||
func IsM015RelatedEvent(eventName string) bool {
|
||||
return IsCREDDnirectEvent(eventName)
|
||||
}
|
||||
145
supply-api/internal/audit/events/cred_events_test.go
Normal file
145
supply-api/internal/audit/events/cred_events_test.go
Normal file
@@ -0,0 +1,145 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCREDEvents_Categories(t *testing.T) {
|
||||
// 测试 CRED 事件类别
|
||||
events := GetCREDEvents()
|
||||
|
||||
// CRED-EXPOSE-RESPONSE: 响应中暴露凭证
|
||||
assert.Contains(t, events, "CRED-EXPOSE-RESPONSE", "Should contain CRED-EXPOSE-RESPONSE")
|
||||
|
||||
// CRED-INGRESS-PLATFORM: 平台凭证入站
|
||||
assert.Contains(t, events, "CRED-INGRESS-PLATFORM", "Should contain CRED-INGRESS-PLATFORM")
|
||||
|
||||
// CRED-DIRECT-SUPPLIER: 直连供应商
|
||||
assert.Contains(t, events, "CRED-DIRECT-SUPPLIER", "Should contain CRED-DIRECT-SUPPLIER")
|
||||
}
|
||||
|
||||
func TestCREDEvents_ExposeEvents(t *testing.T) {
|
||||
// 测试 CRED-EXPOSE 事件
|
||||
events := GetCREDExposeEvents()
|
||||
|
||||
assert.Contains(t, events, "CRED-EXPOSE-RESPONSE")
|
||||
assert.Contains(t, events, "CRED-EXPOSE-LOG")
|
||||
assert.Contains(t, events, "CRED-EXPOSE-EXPORT")
|
||||
}
|
||||
|
||||
func TestCREDEvents_IngressEvents(t *testing.T) {
|
||||
// 测试 CRED-INGRESS 事件
|
||||
events := GetCREDFngressEvents()
|
||||
|
||||
assert.Contains(t, events, "CRED-INGRESS-PLATFORM")
|
||||
assert.Contains(t, events, "CRED-INGRESS-SUPPLIER")
|
||||
}
|
||||
|
||||
func TestCREDEvents_DirectEvents(t *testing.T) {
|
||||
// 测试 CRED-DIRECT 事件
|
||||
events := GetCREDDnirectEvents()
|
||||
|
||||
assert.Contains(t, events, "CRED-DIRECT-SUPPLIER")
|
||||
assert.Contains(t, events, "CRED-DIRECT-BYPASS")
|
||||
}
|
||||
|
||||
func TestCREDEvents_GetEventCategory(t *testing.T) {
|
||||
// 所有CRED事件的类别应该是CRED
|
||||
events := GetCREDEvents()
|
||||
for _, eventName := range events {
|
||||
category := GetCREDEventCategory(eventName)
|
||||
assert.Equal(t, "CRED", category, "Event %s should have category CRED", eventName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCREDEvents_GetEventSubCategory(t *testing.T) {
|
||||
// 测试CRED事件的子类别
|
||||
testCases := []struct {
|
||||
eventName string
|
||||
expectedSubCategory string
|
||||
}{
|
||||
{"CRED-EXPOSE-RESPONSE", "EXPOSE"},
|
||||
{"CRED-INGRESS-PLATFORM", "INGRESS"},
|
||||
{"CRED-DIRECT-SUPPLIER", "DIRECT"},
|
||||
{"CRED-ROTATE", "ROTATE"},
|
||||
{"CRED-REVOKE", "REVOKE"},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.eventName, func(t *testing.T) {
|
||||
subCategory := GetCREDEventSubCategory(tc.eventName)
|
||||
assert.Equal(t, tc.expectedSubCategory, subCategory)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCREDEvents_IsValidEvent(t *testing.T) {
|
||||
// 测试有效事件验证
|
||||
assert.True(t, IsValidCREDEvent("CRED-EXPOSE-RESPONSE"))
|
||||
assert.True(t, IsValidCREDEvent("CRED-INGRESS-PLATFORM"))
|
||||
assert.True(t, IsValidCREDEvent("CRED-DIRECT-SUPPLIER"))
|
||||
assert.False(t, IsValidCREDEvent("INVALID-EVENT"))
|
||||
assert.False(t, IsValidCREDEvent("AUTH-TOKEN-OK"))
|
||||
}
|
||||
|
||||
func TestCREDEvents_IsM013Event(t *testing.T) {
|
||||
// 测试M-013相关事件
|
||||
assert.True(t, IsCREDExposeEvent("CRED-EXPOSE-RESPONSE"))
|
||||
assert.True(t, IsCREDExposeEvent("CRED-EXPOSE-LOG"))
|
||||
assert.False(t, IsCREDExposeEvent("CRED-INGRESS-PLATFORM"))
|
||||
}
|
||||
|
||||
func TestCREDEvents_IsM014Event(t *testing.T) {
|
||||
// 测试M-014相关事件
|
||||
assert.True(t, IsCREDFngressEvent("CRED-INGRESS-PLATFORM"))
|
||||
assert.True(t, IsCREDFngressEvent("CRED-INGRESS-SUPPLIER"))
|
||||
assert.False(t, IsCREDFngressEvent("CRED-EXPOSE-RESPONSE"))
|
||||
}
|
||||
|
||||
func TestCREDEvents_IsM015Event(t *testing.T) {
|
||||
// 测试M-015相关事件
|
||||
assert.True(t, IsCREDDnirectEvent("CRED-DIRECT-SUPPLIER"))
|
||||
assert.True(t, IsCREDDnirectEvent("CRED-DIRECT-BYPASS"))
|
||||
assert.False(t, IsCREDDnirectEvent("CRED-INGRESS-PLATFORM"))
|
||||
}
|
||||
|
||||
func TestCREDEvents_GetMetricName(t *testing.T) {
|
||||
// 测试指标名称映射
|
||||
testCases := []struct {
|
||||
eventName string
|
||||
expectedMetric string
|
||||
}{
|
||||
{"CRED-EXPOSE-RESPONSE", "supplier_credential_exposure_events"},
|
||||
{"CRED-EXPOSE-LOG", "supplier_credential_exposure_events"},
|
||||
{"CRED-INGRESS-PLATFORM", "platform_credential_ingress_coverage_pct"},
|
||||
{"CRED-DIRECT-SUPPLIER", "direct_supplier_call_by_consumer_events"},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.eventName, func(t *testing.T) {
|
||||
metric := GetCREDMetricName(tc.eventName)
|
||||
assert.Equal(t, tc.expectedMetric, metric)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCREDEvents_GetResultCode(t *testing.T) {
|
||||
// 测试CRED事件结果码
|
||||
testCases := []struct {
|
||||
eventName string
|
||||
expectedCode string
|
||||
}{
|
||||
{"CRED-EXPOSE-RESPONSE", "SEC_CRED_EXPOSED"},
|
||||
{"CRED-INGRESS-PLATFORM", "CRED_INGRESS_OK"},
|
||||
{"CRED-DIRECT-SUPPLIER", "SEC_DIRECT_BYPASS"},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.eventName, func(t *testing.T) {
|
||||
code := GetCREDEventResultCode(tc.eventName)
|
||||
assert.Equal(t, tc.expectedCode, code)
|
||||
})
|
||||
}
|
||||
}
|
||||
195
supply-api/internal/audit/events/security_events.go
Normal file
195
supply-api/internal/audit/events/security_events.go
Normal file
@@ -0,0 +1,195 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// SECURITY事件类别常量
|
||||
const (
|
||||
CategorySECURITY = "SECURITY"
|
||||
SubCategoryVIOLATION = "VIOLATION"
|
||||
SubCategoryALERT = "ALERT"
|
||||
SubCategoryBREACH = "BREACH"
|
||||
)
|
||||
|
||||
// SECURITY事件列表
|
||||
var securityEvents = []string{
|
||||
// 不变量违反事件 (INVARIANT-VIOLATION)
|
||||
"INV-PKG-001", // 供应方资质过期
|
||||
"INV-PKG-002", // 供应方余额为负
|
||||
"INV-PKG-003", // 售价不得低于保护价
|
||||
"INV-SET-001", // processing/completed 不可撤销
|
||||
"INV-SET-002", // 提现金额不得超过可提现余额
|
||||
"INV-SET-003", // 结算单金额与余额流水必须平衡
|
||||
|
||||
// 安全突破事件 (SECURITY-BREACH)
|
||||
"SEC-BREACH-001", // 凭证泄露突破
|
||||
"SEC-BREACH-002", // 权限绕过突破
|
||||
|
||||
// 安全告警事件 (SECURITY-ALERT)
|
||||
"SEC-ALERT-001", // 可疑访问告警
|
||||
"SEC-ALERT-002", // 异常行为告警
|
||||
}
|
||||
|
||||
// 不变量违反事件到结果码的映射
|
||||
var invariantResultCodes = map[string]string{
|
||||
"INV-PKG-001": "SEC_INV_PKG_001",
|
||||
"INV-PKG-002": "SEC_INV_PKG_002",
|
||||
"INV-PKG-003": "SEC_INV_PKG_003",
|
||||
"INV-SET-001": "SEC_INV_SET_001",
|
||||
"INV-SET-002": "SEC_INV_SET_002",
|
||||
"INV-SET-003": "SEC_INV_SET_003",
|
||||
}
|
||||
|
||||
// 事件描述映射
|
||||
var securityEventDescriptions = map[string]string{
|
||||
"INV-PKG-001": "供应方资质过期,资质验证失败",
|
||||
"INV-PKG-002": "供应方余额为负,余额检查失败",
|
||||
"INV-PKG-003": "售价不得低于保护价,价格校验失败",
|
||||
"INV-SET-001": "结算单状态为processing/completed,不可撤销",
|
||||
"INV-SET-002": "提现金额不得超过可提现余额",
|
||||
"INV-SET-003": "结算单金额与余额流水不平衡",
|
||||
"SEC-BREACH-001": "检测到凭证泄露安全突破",
|
||||
"SEC-BREACH-002": "检测到权限绕过安全突破",
|
||||
"SEC-ALERT-001": "检测到可疑访问行为",
|
||||
"SEC-ALERT-002": "检测到异常行为",
|
||||
}
|
||||
|
||||
// GetSECURITYEvents 返回所有SECURITY事件
|
||||
func GetSECURITYEvents() []string {
|
||||
return securityEvents
|
||||
}
|
||||
|
||||
// GetInvariantViolationEvents 返回所有不变量违反事件
|
||||
func GetInvariantViolationEvents() []string {
|
||||
return []string{
|
||||
"INV-PKG-001",
|
||||
"INV-PKG-002",
|
||||
"INV-PKG-003",
|
||||
"INV-SET-001",
|
||||
"INV-SET-002",
|
||||
"INV-SET-003",
|
||||
}
|
||||
}
|
||||
|
||||
// GetSecurityAlertEvents 返回所有安全告警事件
|
||||
func GetSecurityAlertEvents() []string {
|
||||
return []string{
|
||||
"SEC-ALERT-001",
|
||||
"SEC-ALERT-002",
|
||||
}
|
||||
}
|
||||
|
||||
// GetSecurityBreachEvents 返回所有安全突破事件
|
||||
func GetSecurityBreachEvents() []string {
|
||||
return []string{
|
||||
"SEC-BREACH-001",
|
||||
"SEC-BREACH-002",
|
||||
}
|
||||
}
|
||||
|
||||
// GetEventCategory 返回事件的类别
|
||||
func GetEventCategory(eventName string) string {
|
||||
if isInvariantViolation(eventName) || isSecurityBreach(eventName) || isSecurityAlert(eventName) {
|
||||
return CategorySECURITY
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetEventSubCategory 返回事件的子类别
|
||||
func GetEventSubCategory(eventName string) string {
|
||||
if isInvariantViolation(eventName) {
|
||||
return SubCategoryVIOLATION
|
||||
}
|
||||
if isSecurityBreach(eventName) {
|
||||
return SubCategoryBREACH
|
||||
}
|
||||
if isSecurityAlert(eventName) {
|
||||
return SubCategoryALERT
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetResultCode 返回事件对应的结果码
|
||||
func GetResultCode(eventName string) string {
|
||||
if code, ok := invariantResultCodes[eventName]; ok {
|
||||
return code
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetEventDescription 返回事件的描述
|
||||
func GetEventDescription(eventName string) string {
|
||||
if desc, ok := securityEventDescriptions[eventName]; ok {
|
||||
return desc
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// IsValidEvent 检查事件名称是否有效
|
||||
func IsValidEvent(eventName string) bool {
|
||||
for _, e := range securityEvents {
|
||||
if e == eventName {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// isInvariantViolation 检查是否为不变量违反事件
|
||||
func isInvariantViolation(eventName string) bool {
|
||||
for _, e := range getInvariantViolationEvents() {
|
||||
if e == eventName {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// getInvariantViolationEvents 返回不变量违反事件列表(内部使用)
|
||||
func getInvariantViolationEvents() []string {
|
||||
return []string{
|
||||
"INV-PKG-001",
|
||||
"INV-PKG-002",
|
||||
"INV-PKG-003",
|
||||
"INV-SET-001",
|
||||
"INV-SET-002",
|
||||
"INV-SET-003",
|
||||
}
|
||||
}
|
||||
|
||||
// isSecurityBreach 检查是否为安全突破事件
|
||||
func isSecurityBreach(eventName string) bool {
|
||||
prefixes := []string{"SEC-BREACH"}
|
||||
for _, prefix := range prefixes {
|
||||
if len(eventName) >= len(prefix) && eventName[:len(prefix)] == prefix {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// isSecurityAlert 检查是否为安全告警事件
|
||||
func isSecurityAlert(eventName string) bool {
|
||||
prefixes := []string{"SEC-ALERT"}
|
||||
for _, prefix := range prefixes {
|
||||
if len(eventName) >= len(prefix) && eventName[:len(prefix)] == prefix {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// FormatSECURITYEvent 格式化SECURITY事件
|
||||
func FormatSECURITYEvent(eventName string, params map[string]string) string {
|
||||
desc := GetEventDescription(eventName)
|
||||
if desc == "" {
|
||||
return fmt.Sprintf("SECURITY event: %s", eventName)
|
||||
}
|
||||
|
||||
// 如果有额外参数,追加到描述中
|
||||
if len(params) > 0 {
|
||||
return fmt.Sprintf("%s - %v", desc, params)
|
||||
}
|
||||
return desc
|
||||
}
|
||||
131
supply-api/internal/audit/events/security_events_test.go
Normal file
131
supply-api/internal/audit/events/security_events_test.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSECURITYEvents_InvariantViolation(t *testing.T) {
|
||||
// 测试 invariant_violation 事件
|
||||
events := GetSECURITYEvents()
|
||||
|
||||
// INV-PKG-001: 供应方资质过期
|
||||
assert.Contains(t, events, "INV-PKG-001", "Should contain INV-PKG-001")
|
||||
|
||||
// INV-SET-001: processing/completed 不可撤销
|
||||
assert.Contains(t, events, "INV-SET-001", "Should contain INV-SET-001")
|
||||
}
|
||||
|
||||
func TestSECURITYEvents_AllEvents(t *testing.T) {
|
||||
// 测试所有SECURITY事件
|
||||
events := GetSECURITYEvents()
|
||||
|
||||
// 验证不变量违反事件
|
||||
invariantEvents := GetInvariantViolationEvents()
|
||||
for _, event := range invariantEvents {
|
||||
assert.Contains(t, events, event, "SECURITY events should contain %s", event)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSECURITYEvents_GetInvariantViolationEvents(t *testing.T) {
|
||||
events := GetInvariantViolationEvents()
|
||||
|
||||
// INV-PKG-001: 供应方资质过期
|
||||
assert.Contains(t, events, "INV-PKG-001")
|
||||
|
||||
// INV-PKG-002: 供应方余额为负
|
||||
assert.Contains(t, events, "INV-PKG-002")
|
||||
|
||||
// INV-PKG-003: 售价不得低于保护价
|
||||
assert.Contains(t, events, "INV-PKG-003")
|
||||
|
||||
// INV-SET-001: processing/completed 不可撤销
|
||||
assert.Contains(t, events, "INV-SET-001")
|
||||
|
||||
// INV-SET-002: 提现金额不得超过可提现余额
|
||||
assert.Contains(t, events, "INV-SET-002")
|
||||
|
||||
// INV-SET-003: 结算单金额与余额流水必须平衡
|
||||
assert.Contains(t, events, "INV-SET-003")
|
||||
}
|
||||
|
||||
func TestSECURITYEvents_GetSecurityAlertEvents(t *testing.T) {
|
||||
events := GetSecurityAlertEvents()
|
||||
|
||||
// 安全告警事件应该存在
|
||||
assert.NotEmpty(t, events)
|
||||
}
|
||||
|
||||
func TestSECURITYEvents_GetSecurityBreachEvents(t *testing.T) {
|
||||
events := GetSecurityBreachEvents()
|
||||
|
||||
// 安全突破事件应该存在
|
||||
assert.NotEmpty(t, events)
|
||||
}
|
||||
|
||||
func TestSECURITYEvents_GetEventCategory(t *testing.T) {
|
||||
// 所有SECURITY事件的类别应该是SECURITY
|
||||
events := GetSECURITYEvents()
|
||||
for _, eventName := range events {
|
||||
category := GetEventCategory(eventName)
|
||||
assert.Equal(t, "SECURITY", category, "Event %s should have category SECURITY", eventName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSECURITYEvents_GetResultCode(t *testing.T) {
|
||||
// 测试不变量违反事件的结果码映射
|
||||
testCases := []struct {
|
||||
eventName string
|
||||
expectedCode string
|
||||
}{
|
||||
{"INV-PKG-001", "SEC_INV_PKG_001"},
|
||||
{"INV-PKG-002", "SEC_INV_PKG_002"},
|
||||
{"INV-PKG-003", "SEC_INV_PKG_003"},
|
||||
{"INV-SET-001", "SEC_INV_SET_001"},
|
||||
{"INV-SET-002", "SEC_INV_SET_002"},
|
||||
{"INV-SET-003", "SEC_INV_SET_003"},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.eventName, func(t *testing.T) {
|
||||
code := GetResultCode(tc.eventName)
|
||||
assert.Equal(t, tc.expectedCode, code, "Result code mismatch for %s", tc.eventName)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSECURITYEvents_GetEventDescription(t *testing.T) {
|
||||
// 测试事件描述
|
||||
desc := GetEventDescription("INV-PKG-001")
|
||||
assert.NotEmpty(t, desc)
|
||||
assert.Contains(t, desc, "供应方资质", "Description should contain 供应方资质")
|
||||
}
|
||||
|
||||
func TestSECURITYEvents_IsValidEvent(t *testing.T) {
|
||||
// 测试有效事件验证
|
||||
assert.True(t, IsValidEvent("INV-PKG-001"))
|
||||
assert.True(t, IsValidEvent("INV-SET-001"))
|
||||
assert.False(t, IsValidEvent("INVALID-EVENT"))
|
||||
assert.False(t, IsValidEvent(""))
|
||||
}
|
||||
|
||||
func TestSECURITYEvents_GetEventSubCategory(t *testing.T) {
|
||||
// SECURITY事件的子类别应该是VIOLATION/ALERT/BREACH
|
||||
testCases := []struct {
|
||||
eventName string
|
||||
expectedSubCategory string
|
||||
}{
|
||||
{"INV-PKG-001", "VIOLATION"},
|
||||
{"INV-SET-001", "VIOLATION"},
|
||||
{"SEC-BREACH-001", "BREACH"},
|
||||
{"SEC-ALERT-001", "ALERT"},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.eventName, func(t *testing.T) {
|
||||
subCategory := GetEventSubCategory(tc.eventName)
|
||||
assert.Equal(t, tc.expectedSubCategory, subCategory)
|
||||
})
|
||||
}
|
||||
}
|
||||
357
supply-api/internal/audit/model/audit_event.go
Normal file
357
supply-api/internal/audit/model/audit_event.go
Normal file
@@ -0,0 +1,357 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// 事件类别常量
|
||||
const (
|
||||
CategoryCRED = "CRED"
|
||||
CategoryAUTH = "AUTH"
|
||||
CategoryDATA = "DATA"
|
||||
CategoryCONFIG = "CONFIG"
|
||||
CategorySECURITY = "SECURITY"
|
||||
)
|
||||
|
||||
// 凭证事件子类别
|
||||
const (
|
||||
SubCategoryCredExpose = "EXPOSE"
|
||||
SubCategoryCredIngress = "INGRESS"
|
||||
SubCategoryCredRotate = "ROTATE"
|
||||
SubCategoryCredRevoke = "REVOKE"
|
||||
SubCategoryCredValidate = "VALIDATE"
|
||||
SubCategoryCredDirect = "DIRECT"
|
||||
)
|
||||
|
||||
// 凭证类型
|
||||
const (
|
||||
CredentialTypePlatformToken = "platform_token"
|
||||
CredentialTypeQueryKey = "query_key"
|
||||
CredentialTypeUpstreamAPIKey = "upstream_api_key"
|
||||
CredentialTypeNone = "none"
|
||||
)
|
||||
|
||||
// 操作者类型
|
||||
const (
|
||||
OperatorTypeUser = "user"
|
||||
OperatorTypeSystem = "system"
|
||||
OperatorTypeAdmin = "admin"
|
||||
)
|
||||
|
||||
// 租户类型
|
||||
const (
|
||||
TenantTypeSupplier = "supplier"
|
||||
TenantTypeConsumer = "consumer"
|
||||
TenantTypePlatform = "platform"
|
||||
)
|
||||
|
||||
// SecurityFlags 安全标记
|
||||
type SecurityFlags struct {
|
||||
HasCredential bool `json:"has_credential"` // 是否包含凭证
|
||||
CredentialExposed bool `json:"credential_exposed"` // 凭证是否暴露
|
||||
Desensitized bool `json:"desensitized"` // 是否已脱敏
|
||||
Scanned bool `json:"scanned"` // 是否已扫描
|
||||
ScanPassed bool `json:"scan_passed"` // 扫描是否通过
|
||||
ViolationTypes []string `json:"violation_types"` // 违规类型列表
|
||||
}
|
||||
|
||||
// NewSecurityFlags 创建默认安全标记
|
||||
func NewSecurityFlags() *SecurityFlags {
|
||||
return &SecurityFlags{
|
||||
HasCredential: false,
|
||||
CredentialExposed: false,
|
||||
Desensitized: false,
|
||||
Scanned: false,
|
||||
ScanPassed: false,
|
||||
ViolationTypes: []string{},
|
||||
}
|
||||
}
|
||||
|
||||
// HasViolation 检查是否有违规
|
||||
func (sf *SecurityFlags) HasViolation() bool {
|
||||
return len(sf.ViolationTypes) > 0
|
||||
}
|
||||
|
||||
// HasViolationOfType 检查是否有指定类型的违规
|
||||
func (sf *SecurityFlags) HasViolationOfType(violationType string) bool {
|
||||
for _, v := range sf.ViolationTypes {
|
||||
if v == violationType {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// AddViolationType 添加违规类型
|
||||
func (sf *SecurityFlags) AddViolationType(violationType string) {
|
||||
sf.ViolationTypes = append(sf.ViolationTypes, violationType)
|
||||
}
|
||||
|
||||
// AuditEvent 统一审计事件
|
||||
type AuditEvent struct {
|
||||
// 基础标识
|
||||
EventID string `json:"event_id"` // 事件唯一ID (UUID)
|
||||
EventName string `json:"event_name"` // 事件名称 (e.g., "CRED-EXPOSE")
|
||||
EventCategory string `json:"event_category"` // 事件大类 (e.g., "CRED")
|
||||
EventSubCategory string `json:"event_sub_category"` // 事件子类
|
||||
|
||||
// 时间戳
|
||||
Timestamp time.Time `json:"timestamp"` // 事件发生时间
|
||||
TimestampMs int64 `json:"timestamp_ms"` // 毫秒时间戳
|
||||
|
||||
// 请求上下文
|
||||
RequestID string `json:"request_id"` // 请求追踪ID
|
||||
TraceID string `json:"trace_id"` // 分布式追踪ID
|
||||
SpanID string `json:"span_id"` // Span ID
|
||||
|
||||
// 幂等性
|
||||
IdempotencyKey string `json:"idempotency_key,omitempty"` // 幂等键
|
||||
|
||||
// 操作者信息
|
||||
OperatorID int64 `json:"operator_id"` // 操作者ID
|
||||
OperatorType string `json:"operator_type"` // 操作者类型 (user/system/admin)
|
||||
OperatorRole string `json:"operator_role"` // 操作者角色
|
||||
|
||||
// 租户信息
|
||||
TenantID int64 `json:"tenant_id"` // 租户ID
|
||||
TenantType string `json:"tenant_type"` // 租户类型 (supplier/consumer/platform)
|
||||
|
||||
// 对象信息
|
||||
ObjectType string `json:"object_type"` // 对象类型 (account/package/settlement)
|
||||
ObjectID int64 `json:"object_id"` // 对象ID
|
||||
|
||||
// 操作信息
|
||||
Action string `json:"action"` // 操作类型 (create/update/delete)
|
||||
ActionDetail string `json:"action_detail"` // 操作详情
|
||||
|
||||
// 凭证信息 (M-013/M-014/M-015/M-016 关键)
|
||||
CredentialType string `json:"credential_type"` // 凭证类型 (platform_token/query_key/upstream_api_key/none)
|
||||
CredentialID string `json:"credential_id,omitempty"` // 凭证标识 (脱敏)
|
||||
CredentialFingerprint string `json:"credential_fingerprint,omitempty"` // 凭证指纹
|
||||
|
||||
// 来源信息
|
||||
SourceType string `json:"source_type"` // 来源类型 (api/ui/cron/internal)
|
||||
SourceIP string `json:"source_ip"` // 来源IP
|
||||
SourceRegion string `json:"source_region"` // 来源区域
|
||||
UserAgent string `json:"user_agent,omitempty"` // User Agent
|
||||
|
||||
// 目标信息 (用于直连检测 M-015)
|
||||
TargetType string `json:"target_type,omitempty"` // 目标类型
|
||||
TargetEndpoint string `json:"target_endpoint,omitempty"` // 目标端点
|
||||
TargetDirect bool `json:"target_direct"` // 是否直连
|
||||
|
||||
// 结果信息
|
||||
ResultCode string `json:"result_code"` // 结果码
|
||||
ResultMessage string `json:"result_message,omitempty"` // 结果消息
|
||||
Success bool `json:"success"` // 是否成功
|
||||
|
||||
// 状态变更 (用于溯源)
|
||||
BeforeState map[string]any `json:"before_state,omitempty"` // 操作前状态
|
||||
AfterState map[string]any `json:"after_state,omitempty"` // 操作后状态
|
||||
|
||||
// 安全标记 (M-013 关键)
|
||||
SecurityFlags SecurityFlags `json:"security_flags"` // 安全标记
|
||||
RiskScore int `json:"risk_score"` // 风险评分 0-100
|
||||
|
||||
// 合规信息
|
||||
ComplianceTags []string `json:"compliance_tags,omitempty"` // 合规标签 (e.g., ["GDPR", "SOC2"])
|
||||
InvariantRule string `json:"invariant_rule,omitempty"` // 触发的不变量规则
|
||||
|
||||
// 扩展字段
|
||||
Extensions map[string]any `json:"extensions,omitempty"` // 扩展数据
|
||||
|
||||
// 元数据
|
||||
Version int `json:"version"` // 事件版本
|
||||
CreatedAt time.Time `json:"created_at"` // 创建时间
|
||||
}
|
||||
|
||||
// NewAuditEvent 创建审计事件
|
||||
func NewAuditEvent(
|
||||
eventName string,
|
||||
eventCategory string,
|
||||
eventSubCategory string,
|
||||
metricName string,
|
||||
requestID string,
|
||||
traceID string,
|
||||
operatorID int64,
|
||||
operatorType string,
|
||||
operatorRole string,
|
||||
tenantID int64,
|
||||
tenantType string,
|
||||
objectType string,
|
||||
objectID int64,
|
||||
action string,
|
||||
credentialType string,
|
||||
sourceType string,
|
||||
sourceIP string,
|
||||
success bool,
|
||||
resultCode string,
|
||||
resultMessage string,
|
||||
) *AuditEvent {
|
||||
now := time.Now()
|
||||
event := &AuditEvent{
|
||||
EventID: uuid.New().String(),
|
||||
EventName: eventName,
|
||||
EventCategory: eventCategory,
|
||||
EventSubCategory: eventSubCategory,
|
||||
Timestamp: now,
|
||||
TimestampMs: now.UnixMilli(),
|
||||
RequestID: requestID,
|
||||
TraceID: traceID,
|
||||
OperatorID: operatorID,
|
||||
OperatorType: operatorType,
|
||||
OperatorRole: operatorRole,
|
||||
TenantID: tenantID,
|
||||
TenantType: tenantType,
|
||||
ObjectType: objectType,
|
||||
ObjectID: objectID,
|
||||
Action: action,
|
||||
CredentialType: credentialType,
|
||||
SourceType: sourceType,
|
||||
SourceIP: sourceIP,
|
||||
Success: success,
|
||||
ResultCode: resultCode,
|
||||
ResultMessage: resultMessage,
|
||||
Version: 1,
|
||||
CreatedAt: now,
|
||||
SecurityFlags: *NewSecurityFlags(),
|
||||
ComplianceTags: []string{},
|
||||
}
|
||||
|
||||
// 根据凭证类型设置安全标记
|
||||
if credentialType != CredentialTypeNone && credentialType != "" {
|
||||
event.SecurityFlags.HasCredential = true
|
||||
}
|
||||
|
||||
// 根据事件名称设置凭证暴露标记(M-013)
|
||||
if IsM013Event(eventName) {
|
||||
event.SecurityFlags.CredentialExposed = true
|
||||
}
|
||||
|
||||
// 根据事件名称设置指标名称到扩展字段
|
||||
if metricName != "" {
|
||||
if event.Extensions == nil {
|
||||
event.Extensions = make(map[string]any)
|
||||
}
|
||||
event.Extensions["metric_name"] = metricName
|
||||
}
|
||||
|
||||
return event
|
||||
}
|
||||
|
||||
// NewAuditEventWithSecurityFlags 创建带完整安全标记的审计事件
|
||||
func NewAuditEventWithSecurityFlags(
|
||||
eventName string,
|
||||
eventCategory string,
|
||||
eventSubCategory string,
|
||||
metricName string,
|
||||
requestID string,
|
||||
traceID string,
|
||||
operatorID int64,
|
||||
operatorType string,
|
||||
operatorRole string,
|
||||
tenantID int64,
|
||||
tenantType string,
|
||||
objectType string,
|
||||
objectID int64,
|
||||
action string,
|
||||
credentialType string,
|
||||
sourceType string,
|
||||
sourceIP string,
|
||||
success bool,
|
||||
resultCode string,
|
||||
resultMessage string,
|
||||
securityFlags SecurityFlags,
|
||||
riskScore int,
|
||||
) *AuditEvent {
|
||||
event := NewAuditEvent(
|
||||
eventName,
|
||||
eventCategory,
|
||||
eventSubCategory,
|
||||
metricName,
|
||||
requestID,
|
||||
traceID,
|
||||
operatorID,
|
||||
operatorType,
|
||||
operatorRole,
|
||||
tenantID,
|
||||
tenantType,
|
||||
objectType,
|
||||
objectID,
|
||||
action,
|
||||
credentialType,
|
||||
sourceType,
|
||||
sourceIP,
|
||||
success,
|
||||
resultCode,
|
||||
resultMessage,
|
||||
)
|
||||
event.SecurityFlags = securityFlags
|
||||
event.RiskScore = riskScore
|
||||
return event
|
||||
}
|
||||
|
||||
// SetIdempotencyKey 设置幂等键
|
||||
func (e *AuditEvent) SetIdempotencyKey(key string) {
|
||||
e.IdempotencyKey = key
|
||||
}
|
||||
|
||||
// SetTarget 设置目标信息(用于M-015直连检测)
|
||||
func (e *AuditEvent) SetTarget(targetType, targetEndpoint string, targetDirect bool) {
|
||||
e.TargetType = targetType
|
||||
e.TargetEndpoint = targetEndpoint
|
||||
e.TargetDirect = targetDirect
|
||||
}
|
||||
|
||||
// SetInvariantRule 设置不变量规则(用于SECURITY事件)
|
||||
func (e *AuditEvent) SetInvariantRule(rule string) {
|
||||
e.InvariantRule = rule
|
||||
// 添加合规标签
|
||||
e.ComplianceTags = append(e.ComplianceTags, "XR-001")
|
||||
}
|
||||
|
||||
// GetMetricName 获取指标名称
|
||||
func (e *AuditEvent) GetMetricName() string {
|
||||
if e.Extensions != nil {
|
||||
if metricName, ok := e.Extensions["metric_name"].(string); ok {
|
||||
return metricName
|
||||
}
|
||||
}
|
||||
|
||||
// 根据事件名称推断指标
|
||||
switch e.EventName {
|
||||
case "CRED-EXPOSE-RESPONSE", "CRED-EXPOSE-LOG", "CRED-EXPOSE":
|
||||
return "supplier_credential_exposure_events"
|
||||
case "CRED-INGRESS-PLATFORM", "CRED-INGRESS":
|
||||
return "platform_credential_ingress_coverage_pct"
|
||||
case "CRED-DIRECT-SUPPLIER", "CRED-DIRECT":
|
||||
return "direct_supplier_call_by_consumer_events"
|
||||
case "AUTH-QUERY-KEY", "AUTH-QUERY-REJECT", "AUTH-QUERY":
|
||||
return "query_key_external_reject_rate_pct"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// IsM013Event 判断是否为M-013凭证暴露事件
|
||||
func IsM013Event(eventName string) bool {
|
||||
return strings.HasPrefix(eventName, "CRED-EXPOSE")
|
||||
}
|
||||
|
||||
// IsM014Event 判断是否为M-014凭证入站事件
|
||||
func IsM014Event(eventName string) bool {
|
||||
return strings.HasPrefix(eventName, "CRED-INGRESS")
|
||||
}
|
||||
|
||||
// IsM015Event 判断是否为M-015直连绕过事件
|
||||
func IsM015Event(eventName string) bool {
|
||||
return strings.HasPrefix(eventName, "CRED-DIRECT")
|
||||
}
|
||||
|
||||
// IsM016Event 判断是否为M-016 query key拒绝事件
|
||||
func IsM016Event(eventName string) bool {
|
||||
return strings.HasPrefix(eventName, "AUTH-QUERY")
|
||||
}
|
||||
389
supply-api/internal/audit/model/audit_event_test.go
Normal file
389
supply-api/internal/audit/model/audit_event_test.go
Normal file
@@ -0,0 +1,389 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestAuditEvent_NewEvent_ValidInput(t *testing.T) {
|
||||
// 测试创建审计事件
|
||||
event := NewAuditEvent(
|
||||
"CRED-EXPOSE-RESPONSE",
|
||||
"CRED",
|
||||
"EXPOSE",
|
||||
"supplier_credential_exposure_events",
|
||||
"test-request-id",
|
||||
"test-trace-id",
|
||||
1001,
|
||||
"user",
|
||||
"admin",
|
||||
2001,
|
||||
"supplier",
|
||||
"account",
|
||||
12345,
|
||||
"create",
|
||||
"platform_token",
|
||||
"api",
|
||||
"192.168.1.1",
|
||||
true,
|
||||
"SEC_CRED_EXPOSED",
|
||||
"Credential exposed in response",
|
||||
)
|
||||
|
||||
// 验证字段
|
||||
assert.NotEmpty(t, event.EventID, "EventID should not be empty")
|
||||
assert.Equal(t, "CRED-EXPOSE-RESPONSE", event.EventName, "EventName should match")
|
||||
assert.Equal(t, "CRED", event.EventCategory, "EventCategory should match")
|
||||
assert.Equal(t, "EXPOSE", event.EventSubCategory, "EventSubCategory should match")
|
||||
assert.Equal(t, "test-request-id", event.RequestID, "RequestID should match")
|
||||
assert.Equal(t, "test-trace-id", event.TraceID, "TraceID should match")
|
||||
assert.Equal(t, int64(1001), event.OperatorID, "OperatorID should match")
|
||||
assert.Equal(t, "user", event.OperatorType, "OperatorType should match")
|
||||
assert.Equal(t, "admin", event.OperatorRole, "OperatorRole should match")
|
||||
assert.Equal(t, int64(2001), event.TenantID, "TenantID should match")
|
||||
assert.Equal(t, "supplier", event.TenantType, "TenantType should match")
|
||||
assert.Equal(t, "account", event.ObjectType, "ObjectType should match")
|
||||
assert.Equal(t, int64(12345), event.ObjectID, "ObjectID should match")
|
||||
assert.Equal(t, "create", event.Action, "Action should match")
|
||||
assert.Equal(t, "platform_token", event.CredentialType, "CredentialType should match")
|
||||
assert.Equal(t, "api", event.SourceType, "SourceType should match")
|
||||
assert.Equal(t, "192.168.1.1", event.SourceIP, "SourceIP should match")
|
||||
assert.True(t, event.Success, "Success should be true")
|
||||
assert.Equal(t, "SEC_CRED_EXPOSED", event.ResultCode, "ResultCode should match")
|
||||
assert.Equal(t, "Credential exposed in response", event.ResultMessage, "ResultMessage should match")
|
||||
|
||||
// 验证时间戳
|
||||
assert.False(t, event.Timestamp.IsZero(), "Timestamp should not be zero")
|
||||
assert.True(t, event.TimestampMs > 0, "TimestampMs should be positive")
|
||||
assert.False(t, event.CreatedAt.IsZero(), "CreatedAt should not be zero")
|
||||
|
||||
// 验证版本
|
||||
assert.Equal(t, 1, event.Version, "Version should be 1")
|
||||
}
|
||||
|
||||
func TestAuditEvent_NewEvent_SecurityFlags(t *testing.T) {
|
||||
// 验证SecurityFlags字段
|
||||
event := NewAuditEvent(
|
||||
"CRED-EXPOSE-RESPONSE",
|
||||
"CRED",
|
||||
"EXPOSE",
|
||||
"supplier_credential_exposure_events",
|
||||
"test-request-id",
|
||||
"test-trace-id",
|
||||
1001,
|
||||
"user",
|
||||
"admin",
|
||||
2001,
|
||||
"supplier",
|
||||
"account",
|
||||
12345,
|
||||
"create",
|
||||
"platform_token",
|
||||
"api",
|
||||
"192.168.1.1",
|
||||
true,
|
||||
"SEC_CRED_EXPOSED",
|
||||
"Credential exposed in response",
|
||||
)
|
||||
|
||||
// 验证安全标记
|
||||
assert.NotNil(t, event.SecurityFlags, "SecurityFlags should not be nil")
|
||||
assert.True(t, event.SecurityFlags.HasCredential, "HasCredential should be true")
|
||||
assert.True(t, event.SecurityFlags.CredentialExposed, "CredentialExposed should be true")
|
||||
assert.False(t, event.SecurityFlags.Desensitized, "Desensitized should be false by default")
|
||||
assert.False(t, event.SecurityFlags.Scanned, "Scanned should be false by default")
|
||||
assert.False(t, event.SecurityFlags.ScanPassed, "ScanPassed should be false by default")
|
||||
assert.Empty(t, event.SecurityFlags.ViolationTypes, "ViolationTypes should be empty by default")
|
||||
}
|
||||
|
||||
func TestAuditEvent_NewEvent_WithSecurityFlags(t *testing.T) {
|
||||
// 测试带有完整安全标记的事件
|
||||
securityFlags := SecurityFlags{
|
||||
HasCredential: true,
|
||||
CredentialExposed: true,
|
||||
Desensitized: false,
|
||||
Scanned: true,
|
||||
ScanPassed: false,
|
||||
ViolationTypes: []string{"api_key", "secret"},
|
||||
}
|
||||
|
||||
event := NewAuditEventWithSecurityFlags(
|
||||
"CRED-EXPOSE-RESPONSE",
|
||||
"CRED",
|
||||
"EXPOSE",
|
||||
"supplier_credential_exposure_events",
|
||||
"test-request-id",
|
||||
"test-trace-id",
|
||||
1001,
|
||||
"user",
|
||||
"admin",
|
||||
2001,
|
||||
"supplier",
|
||||
"account",
|
||||
12345,
|
||||
"create",
|
||||
"platform_token",
|
||||
"api",
|
||||
"192.168.1.1",
|
||||
true,
|
||||
"SEC_CRED_EXPOSED",
|
||||
"Credential exposed in response",
|
||||
securityFlags,
|
||||
80,
|
||||
)
|
||||
|
||||
// 验证安全标记
|
||||
assert.Equal(t, true, event.SecurityFlags.HasCredential)
|
||||
assert.Equal(t, true, event.SecurityFlags.CredentialExposed)
|
||||
assert.Equal(t, false, event.SecurityFlags.Desensitized)
|
||||
assert.Equal(t, true, event.SecurityFlags.Scanned)
|
||||
assert.Equal(t, false, event.SecurityFlags.ScanPassed)
|
||||
assert.Equal(t, []string{"api_key", "secret"}, event.SecurityFlags.ViolationTypes)
|
||||
|
||||
// 验证风险评分
|
||||
assert.Equal(t, 80, event.RiskScore, "RiskScore should be 80")
|
||||
}
|
||||
|
||||
func TestAuditEvent_NewAuditEventWithIdempotencyKey(t *testing.T) {
|
||||
// 测试带幂等键的事件
|
||||
event := NewAuditEvent(
|
||||
"AUTH-QUERY-KEY",
|
||||
"AUTH",
|
||||
"QUERY",
|
||||
"query_key_external_reject_rate_pct",
|
||||
"test-request-id",
|
||||
"test-trace-id",
|
||||
1001,
|
||||
"user",
|
||||
"admin",
|
||||
2001,
|
||||
"supplier",
|
||||
"account",
|
||||
12345,
|
||||
"query",
|
||||
"query_key",
|
||||
"api",
|
||||
"192.168.1.1",
|
||||
true,
|
||||
"AUTH_QUERY_KEY",
|
||||
"Query key request",
|
||||
)
|
||||
|
||||
// 设置幂等键
|
||||
event.SetIdempotencyKey("idem-key-12345")
|
||||
|
||||
assert.Equal(t, "idem-key-12345", event.IdempotencyKey, "IdempotencyKey should be set")
|
||||
}
|
||||
|
||||
func TestAuditEvent_NewAuditEventWithTarget(t *testing.T) {
|
||||
// 测试带目标信息的事件(用于M-015直连检测)
|
||||
event := NewAuditEvent(
|
||||
"CRED-DIRECT-SUPPLIER",
|
||||
"CRED",
|
||||
"DIRECT",
|
||||
"direct_supplier_call_by_consumer_events",
|
||||
"test-request-id",
|
||||
"test-trace-id",
|
||||
1001,
|
||||
"user",
|
||||
"admin",
|
||||
2001,
|
||||
"supplier",
|
||||
"api",
|
||||
12345,
|
||||
"call",
|
||||
"none",
|
||||
"api",
|
||||
"192.168.1.1",
|
||||
false,
|
||||
"SEC_DIRECT_BYPASS",
|
||||
"Direct call detected",
|
||||
)
|
||||
|
||||
// 设置直连目标
|
||||
event.SetTarget("upstream_api", "https://supplier.example.com/v1/chat/completions", true)
|
||||
|
||||
assert.Equal(t, "upstream_api", event.TargetType, "TargetType should be set")
|
||||
assert.Equal(t, "https://supplier.example.com/v1/chat/completions", event.TargetEndpoint, "TargetEndpoint should be set")
|
||||
assert.True(t, event.TargetDirect, "TargetDirect should be true")
|
||||
}
|
||||
|
||||
func TestAuditEvent_NewAuditEventWithInvariantRule(t *testing.T) {
|
||||
// 测试不变量规则(用于SECURITY事件)
|
||||
event := NewAuditEvent(
|
||||
"INVARIANT-VIOLATION",
|
||||
"SECURITY",
|
||||
"VIOLATION",
|
||||
"invariant_violation",
|
||||
"test-request-id",
|
||||
"test-trace-id",
|
||||
1001,
|
||||
"system",
|
||||
"admin",
|
||||
2001,
|
||||
"supplier",
|
||||
"settlement",
|
||||
12345,
|
||||
"withdraw",
|
||||
"platform_token",
|
||||
"api",
|
||||
"192.168.1.1",
|
||||
false,
|
||||
"SEC_INV_SET_001",
|
||||
"Settlement cannot be revoked",
|
||||
)
|
||||
|
||||
// 设置不变量规则
|
||||
event.SetInvariantRule("INV-SET-001")
|
||||
|
||||
assert.Equal(t, "INV-SET-001", event.InvariantRule, "InvariantRule should be set")
|
||||
assert.Contains(t, event.ComplianceTags, "XR-001", "ComplianceTags should contain XR-001")
|
||||
}
|
||||
|
||||
func TestSecurityFlags_HasViolation(t *testing.T) {
|
||||
// 测试安全标记的违规检测
|
||||
sf := NewSecurityFlags()
|
||||
|
||||
// 初始状态无违规
|
||||
assert.False(t, sf.HasViolation(), "Should have no violation initially")
|
||||
|
||||
// 添加违规类型
|
||||
sf.AddViolationType("api_key")
|
||||
assert.True(t, sf.HasViolation(), "Should have violation after adding type")
|
||||
assert.True(t, sf.HasViolationOfType("api_key"), "Should have api_key violation")
|
||||
assert.False(t, sf.HasViolationOfType("password"), "Should not have password violation")
|
||||
}
|
||||
|
||||
func TestSecurityFlags_AddViolationType(t *testing.T) {
|
||||
sf := NewSecurityFlags()
|
||||
|
||||
sf.AddViolationType("api_key")
|
||||
sf.AddViolationType("secret")
|
||||
sf.AddViolationType("password")
|
||||
|
||||
assert.Len(t, sf.ViolationTypes, 3, "Should have 3 violation types")
|
||||
assert.Contains(t, sf.ViolationTypes, "api_key")
|
||||
assert.Contains(t, sf.ViolationTypes, "secret")
|
||||
assert.Contains(t, sf.ViolationTypes, "password")
|
||||
}
|
||||
|
||||
func TestAuditEvent_MetricName(t *testing.T) {
|
||||
// 测试事件与指标的映射
|
||||
testCases := []struct {
|
||||
eventName string
|
||||
expectedMetric string
|
||||
}{
|
||||
{"CRED-EXPOSE-RESPONSE", "supplier_credential_exposure_events"},
|
||||
{"CRED-EXPOSE-LOG", "supplier_credential_exposure_events"},
|
||||
{"CRED-INGRESS-PLATFORM", "platform_credential_ingress_coverage_pct"},
|
||||
{"CRED-DIRECT-SUPPLIER", "direct_supplier_call_by_consumer_events"},
|
||||
{"AUTH-QUERY-KEY", "query_key_external_reject_rate_pct"},
|
||||
{"AUTH-QUERY-REJECT", "query_key_external_reject_rate_pct"},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.eventName, func(t *testing.T) {
|
||||
event := &AuditEvent{
|
||||
EventName: tc.eventName,
|
||||
}
|
||||
assert.Equal(t, tc.expectedMetric, event.GetMetricName(), "MetricName should match for %s", tc.eventName)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuditEvent_IsM013Event(t *testing.T) {
|
||||
// M-013: 凭证暴露事件
|
||||
assert.True(t, IsM013Event("CRED-EXPOSE-RESPONSE"), "CRED-EXPOSE-RESPONSE is M-013 event")
|
||||
assert.True(t, IsM013Event("CRED-EXPOSE-LOG"), "CRED-EXPOSE-LOG is M-013 event")
|
||||
assert.True(t, IsM013Event("CRED-EXPOSE"), "CRED-EXPOSE is M-013 event")
|
||||
assert.False(t, IsM013Event("CRED-INGRESS-PLATFORM"), "CRED-INGRESS-PLATFORM is not M-013 event")
|
||||
assert.False(t, IsM013Event("AUTH-QUERY-KEY"), "AUTH-QUERY-KEY is not M-013 event")
|
||||
}
|
||||
|
||||
func TestAuditEvent_IsM014Event(t *testing.T) {
|
||||
// M-014: 凭证入站事件
|
||||
assert.True(t, IsM014Event("CRED-INGRESS-PLATFORM"), "CRED-INGRESS-PLATFORM is M-014 event")
|
||||
assert.True(t, IsM014Event("CRED-INGRESS"), "CRED-INGRESS is M-014 event")
|
||||
assert.False(t, IsM014Event("CRED-EXPOSE-RESPONSE"), "CRED-EXPOSE-RESPONSE is not M-014 event")
|
||||
}
|
||||
|
||||
func TestAuditEvent_IsM015Event(t *testing.T) {
|
||||
// M-015: 直连绕过事件
|
||||
assert.True(t, IsM015Event("CRED-DIRECT-SUPPLIER"), "CRED-DIRECT-SUPPLIER is M-015 event")
|
||||
assert.True(t, IsM015Event("CRED-DIRECT"), "CRED-DIRECT is M-015 event")
|
||||
assert.False(t, IsM015Event("CRED-INGRESS-PLATFORM"), "CRED-INGRESS-PLATFORM is not M-015 event")
|
||||
}
|
||||
|
||||
func TestAuditEvent_IsM016Event(t *testing.T) {
|
||||
// M-016: query key拒绝事件
|
||||
assert.True(t, IsM016Event("AUTH-QUERY-KEY"), "AUTH-QUERY-KEY is M-016 event")
|
||||
assert.True(t, IsM016Event("AUTH-QUERY-REJECT"), "AUTH-QUERY-REJECT is M-016 event")
|
||||
assert.True(t, IsM016Event("AUTH-QUERY"), "AUTH-QUERY is M-016 event")
|
||||
assert.False(t, IsM016Event("CRED-EXPOSE-RESPONSE"), "CRED-EXPOSE-RESPONSE is not M-016 event")
|
||||
}
|
||||
|
||||
func TestAuditEvent_CredentialType(t *testing.T) {
|
||||
// 测试凭证类型常量
|
||||
assert.Equal(t, "platform_token", CredentialTypePlatformToken)
|
||||
assert.Equal(t, "query_key", CredentialTypeQueryKey)
|
||||
assert.Equal(t, "upstream_api_key", CredentialTypeUpstreamAPIKey)
|
||||
assert.Equal(t, "none", CredentialTypeNone)
|
||||
}
|
||||
|
||||
func TestAuditEvent_OperatorType(t *testing.T) {
|
||||
// 测试操作者类型常量
|
||||
assert.Equal(t, "user", OperatorTypeUser)
|
||||
assert.Equal(t, "system", OperatorTypeSystem)
|
||||
assert.Equal(t, "admin", OperatorTypeAdmin)
|
||||
}
|
||||
|
||||
func TestAuditEvent_TenantType(t *testing.T) {
|
||||
// 测试租户类型常量
|
||||
assert.Equal(t, "supplier", TenantTypeSupplier)
|
||||
assert.Equal(t, "consumer", TenantTypeConsumer)
|
||||
assert.Equal(t, "platform", TenantTypePlatform)
|
||||
}
|
||||
|
||||
func TestAuditEvent_Category(t *testing.T) {
|
||||
// 测试事件类别常量
|
||||
assert.Equal(t, "CRED", CategoryCRED)
|
||||
assert.Equal(t, "AUTH", CategoryAUTH)
|
||||
assert.Equal(t, "DATA", CategoryDATA)
|
||||
assert.Equal(t, "CONFIG", CategoryCONFIG)
|
||||
assert.Equal(t, "SECURITY", CategorySECURITY)
|
||||
}
|
||||
|
||||
func TestAuditEvent_NewAuditEventTimestamp(t *testing.T) {
|
||||
// 测试时间戳自动生成
|
||||
before := time.Now()
|
||||
event := NewAuditEvent(
|
||||
"CRED-EXPOSE-RESPONSE",
|
||||
"CRED",
|
||||
"EXPOSE",
|
||||
"supplier_credential_exposure_events",
|
||||
"test-request-id",
|
||||
"test-trace-id",
|
||||
1001,
|
||||
"user",
|
||||
"admin",
|
||||
2001,
|
||||
"supplier",
|
||||
"account",
|
||||
12345,
|
||||
"create",
|
||||
"platform_token",
|
||||
"api",
|
||||
"192.168.1.1",
|
||||
true,
|
||||
"SEC_CRED_EXPOSED",
|
||||
"Credential exposed in response",
|
||||
)
|
||||
after := time.Now()
|
||||
|
||||
// 验证时间戳在合理范围内
|
||||
assert.True(t, event.Timestamp.After(before) || event.Timestamp.Equal(before), "Timestamp should be after or equal to before")
|
||||
assert.True(t, event.Timestamp.Before(after) || event.Timestamp.Equal(after), "Timestamp should be before or equal to after")
|
||||
assert.Equal(t, event.Timestamp.UnixMilli(), event.TimestampMs, "TimestampMs should match Timestamp")
|
||||
}
|
||||
220
supply-api/internal/audit/model/audit_metrics.go
Normal file
220
supply-api/internal/audit/model/audit_metrics.go
Normal file
@@ -0,0 +1,220 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// ==================== M-013: 凭证暴露事件详情 ====================
|
||||
|
||||
// CredentialExposureDetail M-013: 凭证暴露事件专用
|
||||
type CredentialExposureDetail struct {
|
||||
EventID string `json:"event_id"` // 事件ID(关联audit_events)
|
||||
ExposureType string `json:"exposure_type"` // exposed_in_response/exposed_in_log/exposed_in_export
|
||||
ExposureLocation string `json:"exposure_location"` // response_body/response_header/log_file/export_file
|
||||
ExposurePattern string `json:"exposure_pattern"` // 匹配到的正则模式
|
||||
ExposedFragment string `json:"exposed_fragment"` // 暴露的片段(已脱敏)
|
||||
ScanRuleID string `json:"scan_rule_id"` // 触发扫描规则ID
|
||||
Resolved bool `json:"resolved"` // 是否已解决
|
||||
ResolvedAt *time.Time `json:"resolved_at"` // 解决时间
|
||||
ResolvedBy *int64 `json:"resolved_by"` // 解决人
|
||||
ResolutionNotes string `json:"resolution_notes"` // 解决备注
|
||||
}
|
||||
|
||||
// NewCredentialExposureDetail 创建凭证暴露详情
|
||||
func NewCredentialExposureDetail(
|
||||
exposureType string,
|
||||
exposureLocation string,
|
||||
exposurePattern string,
|
||||
exposedFragment string,
|
||||
scanRuleID string,
|
||||
) *CredentialExposureDetail {
|
||||
return &CredentialExposureDetail{
|
||||
ExposureType: exposureType,
|
||||
ExposureLocation: exposureLocation,
|
||||
ExposurePattern: exposurePattern,
|
||||
ExposedFragment: exposedFragment,
|
||||
ScanRuleID: scanRuleID,
|
||||
Resolved: false,
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve 标记为已解决
|
||||
func (d *CredentialExposureDetail) Resolve(resolvedBy int64, notes string) {
|
||||
now := time.Now()
|
||||
d.Resolved = true
|
||||
d.ResolvedAt = &now
|
||||
d.ResolvedBy = &resolvedBy
|
||||
d.ResolutionNotes = notes
|
||||
}
|
||||
|
||||
// ==================== M-014: 凭证入站事件详情 ====================
|
||||
|
||||
// CredentialIngressDetail M-014: 凭证入站类型专用
|
||||
type CredentialIngressDetail struct {
|
||||
EventID string `json:"event_id"` // 事件ID
|
||||
RequestCredentialType string `json:"request_credential_type"` // 请求中的凭证类型
|
||||
ExpectedCredentialType string `json:"expected_credential_type"` // 期望的凭证类型
|
||||
CoverageCompliant bool `json:"coverage_compliant"` // 是否合规
|
||||
PlatformTokenPresent bool `json:"platform_token_present"` // 平台Token是否存在
|
||||
UpstreamKeyPresent bool `json:"upstream_key_present"` // 上游Key是否存在
|
||||
Reviewed bool `json:"reviewed"` // 是否已审核
|
||||
ReviewedAt *time.Time `json:"reviewed_at"` // 审核时间
|
||||
ReviewedBy *int64 `json:"reviewed_by"` // 审核人
|
||||
}
|
||||
|
||||
// NewCredentialIngressDetail 创建凭证入站详情
|
||||
func NewCredentialIngressDetail(
|
||||
requestCredentialType string,
|
||||
expectedCredentialType string,
|
||||
coverageCompliant bool,
|
||||
platformTokenPresent bool,
|
||||
upstreamKeyPresent bool,
|
||||
) *CredentialIngressDetail {
|
||||
return &CredentialIngressDetail{
|
||||
RequestCredentialType: requestCredentialType,
|
||||
ExpectedCredentialType: expectedCredentialType,
|
||||
CoverageCompliant: coverageCompliant,
|
||||
PlatformTokenPresent: platformTokenPresent,
|
||||
UpstreamKeyPresent: upstreamKeyPresent,
|
||||
Reviewed: false,
|
||||
}
|
||||
}
|
||||
|
||||
// Review 标记为已审核
|
||||
func (d *CredentialIngressDetail) Review(reviewedBy int64) {
|
||||
now := time.Now()
|
||||
d.Reviewed = true
|
||||
d.ReviewedAt = &now
|
||||
d.ReviewedBy = &reviewedBy
|
||||
}
|
||||
|
||||
// ==================== M-015: 直连绕过事件详情 ====================
|
||||
|
||||
// DirectCallDetail M-015: 直连绕过专用
|
||||
type DirectCallDetail struct {
|
||||
EventID string `json:"event_id"` // 事件ID
|
||||
ConsumerID int64 `json:"consumer_id"` // 消费者ID
|
||||
SupplierID int64 `json:"supplier_id"` // 供应商ID
|
||||
DirectEndpoint string `json:"direct_endpoint"` // 直连端点
|
||||
ViaPlatform bool `json:"via_platform"` // 是否通过平台
|
||||
BypassType string `json:"bypass_type"` // ip_bypass/proxy_bypass/config_bypass/dns_bypass
|
||||
DetectionMethod string `json:"detection_method"` // 检测方法
|
||||
Blocked bool `json:"blocked"` // 是否被阻断
|
||||
BlockedAt *time.Time `json:"blocked_at"` // 阻断时间
|
||||
BlockReason string `json:"block_reason"` // 阻断原因
|
||||
}
|
||||
|
||||
// NewDirectCallDetail 创建直连详情
|
||||
func NewDirectCallDetail(
|
||||
consumerID int64,
|
||||
supplierID int64,
|
||||
directEndpoint string,
|
||||
viaPlatform bool,
|
||||
bypassType string,
|
||||
detectionMethod string,
|
||||
) *DirectCallDetail {
|
||||
return &DirectCallDetail{
|
||||
ConsumerID: consumerID,
|
||||
SupplierID: supplierID,
|
||||
DirectEndpoint: directEndpoint,
|
||||
ViaPlatform: viaPlatform,
|
||||
BypassType: bypassType,
|
||||
DetectionMethod: detectionMethod,
|
||||
Blocked: false,
|
||||
}
|
||||
}
|
||||
|
||||
// Block 标记为已阻断
|
||||
func (d *DirectCallDetail) Block(reason string) {
|
||||
now := time.Now()
|
||||
d.Blocked = true
|
||||
d.BlockedAt = &now
|
||||
d.BlockReason = reason
|
||||
}
|
||||
|
||||
// ==================== M-016: Query Key 拒绝事件详情 ====================
|
||||
|
||||
// QueryKeyRejectDetail M-016: query key 拒绝专用
|
||||
type QueryKeyRejectDetail struct {
|
||||
EventID string `json:"event_id"` // 事件ID
|
||||
QueryKeyID string `json:"query_key_id"` // Query Key ID
|
||||
RequestedEndpoint string `json:"requested_endpoint"` // 请求端点
|
||||
RejectReason string `json:"reject_reason"` // not_allowed/expired/malformed/revoked/rate_limited
|
||||
RejectCode string `json:"reject_code"` // 拒绝码
|
||||
FirstOccurrence bool `json:"first_occurrence"` // 是否首次发生
|
||||
OccurrenceCount int `json:"occurrence_count"` // 发生次数
|
||||
}
|
||||
|
||||
// NewQueryKeyRejectDetail 创建Query Key拒绝详情
|
||||
func NewQueryKeyRejectDetail(
|
||||
queryKeyID string,
|
||||
requestedEndpoint string,
|
||||
rejectReason string,
|
||||
rejectCode string,
|
||||
) *QueryKeyRejectDetail {
|
||||
return &QueryKeyRejectDetail{
|
||||
QueryKeyID: queryKeyID,
|
||||
RequestedEndpoint: requestedEndpoint,
|
||||
RejectReason: rejectReason,
|
||||
RejectCode: rejectCode,
|
||||
FirstOccurrence: true,
|
||||
OccurrenceCount: 1,
|
||||
}
|
||||
}
|
||||
|
||||
// RecordOccurrence 记录再次发生
|
||||
func (d *QueryKeyRejectDetail) RecordOccurrence(firstOccurrence bool) {
|
||||
d.FirstOccurrence = firstOccurrence
|
||||
d.OccurrenceCount++
|
||||
}
|
||||
|
||||
// ==================== 指标常量 ====================
|
||||
|
||||
// M-013 暴露类型常量
|
||||
const (
|
||||
ExposureTypeResponse = "exposed_in_response"
|
||||
ExposureTypeLog = "exposed_in_log"
|
||||
ExposureTypeExport = "exposed_in_export"
|
||||
)
|
||||
|
||||
// M-013 暴露位置常量
|
||||
const (
|
||||
ExposureLocationResponseBody = "response_body"
|
||||
ExposureLocationResponseHeader = "response_header"
|
||||
ExposureLocationLogFile = "log_file"
|
||||
ExposureLocationExportFile = "export_file"
|
||||
)
|
||||
|
||||
// M-015 绕过类型常量
|
||||
const (
|
||||
BypassTypeIPBypass = "ip_bypass"
|
||||
BypassTypeProxyBypass = "proxy_bypass"
|
||||
BypassTypeConfigBypass = "config_bypass"
|
||||
BypassTypeDNSBypass = "dns_bypass"
|
||||
)
|
||||
|
||||
// M-015 检测方法常量
|
||||
const (
|
||||
DetectionMethodUpstreamAPIPattern = "upstream_api_pattern_match"
|
||||
DetectionMethodDNSResolution = "dns_resolution_check"
|
||||
DetectionMethodConnectionSource = "connection_source_check"
|
||||
DetectionMethodIPWhitelist = "ip_whitelist_check"
|
||||
)
|
||||
|
||||
// M-016 拒绝原因常量
|
||||
const (
|
||||
RejectReasonNotAllowed = "not_allowed"
|
||||
RejectReasonExpired = "expired"
|
||||
RejectReasonMalformed = "malformed"
|
||||
RejectReasonRevoked = "revoked"
|
||||
RejectReasonRateLimited = "rate_limited"
|
||||
)
|
||||
|
||||
// M-016 拒绝码常量
|
||||
const (
|
||||
RejectCodeNotAllowed = "QUERY_KEY_NOT_ALLOWED"
|
||||
RejectCodeExpired = "QUERY_KEY_EXPIRED"
|
||||
RejectCodeMalformed = "QUERY_KEY_MALFORMED"
|
||||
RejectCodeRevoked = "QUERY_KEY_REVOKED"
|
||||
RejectCodeRateLimited = "QUERY_KEY_RATE_LIMITED"
|
||||
)
|
||||
459
supply-api/internal/audit/model/audit_metrics_test.go
Normal file
459
supply-api/internal/audit/model/audit_metrics_test.go
Normal file
@@ -0,0 +1,459 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// ==================== M-013 凭证暴露事件详情 ====================
|
||||
|
||||
func TestCredentialExposureDetail_New(t *testing.T) {
|
||||
// M-013: 凭证暴露事件专用
|
||||
detail := NewCredentialExposureDetail(
|
||||
"exposed_in_response",
|
||||
"response_body",
|
||||
"sk-[a-zA-Z0-9]{20,}",
|
||||
"sk-xxxxxx****xxxx",
|
||||
"SCAN-001",
|
||||
)
|
||||
|
||||
assert.Equal(t, "exposed_in_response", detail.ExposureType)
|
||||
assert.Equal(t, "response_body", detail.ExposureLocation)
|
||||
assert.Equal(t, "sk-[a-zA-Z0-9]{20,}", detail.ExposurePattern)
|
||||
assert.Equal(t, "sk-xxxxxx****xxxx", detail.ExposedFragment)
|
||||
assert.Equal(t, "SCAN-001", detail.ScanRuleID)
|
||||
assert.False(t, detail.Resolved)
|
||||
assert.Nil(t, detail.ResolvedAt)
|
||||
assert.Nil(t, detail.ResolvedBy)
|
||||
assert.Empty(t, detail.ResolutionNotes)
|
||||
}
|
||||
|
||||
func TestCredentialExposureDetail_Resolve(t *testing.T) {
|
||||
detail := NewCredentialExposureDetail(
|
||||
"exposed_in_response",
|
||||
"response_body",
|
||||
"sk-[a-zA-Z0-9]{20,}",
|
||||
"sk-xxxxxx****xxxx",
|
||||
"SCAN-001",
|
||||
)
|
||||
|
||||
detail.Resolve(1001, "Fixed by adding masking")
|
||||
|
||||
assert.True(t, detail.Resolved)
|
||||
assert.NotNil(t, detail.ResolvedAt)
|
||||
assert.Equal(t, int64(1001), *detail.ResolvedBy)
|
||||
assert.Equal(t, "Fixed by adding masking", detail.ResolutionNotes)
|
||||
}
|
||||
|
||||
func TestCredentialExposureDetail_ExposureTypes(t *testing.T) {
|
||||
// 验证暴露类型常量
|
||||
validTypes := []string{
|
||||
"exposed_in_response",
|
||||
"exposed_in_log",
|
||||
"exposed_in_export",
|
||||
}
|
||||
|
||||
for _, exposureType := range validTypes {
|
||||
detail := NewCredentialExposureDetail(
|
||||
exposureType,
|
||||
"response_body",
|
||||
"pattern",
|
||||
"fragment",
|
||||
"SCAN-001",
|
||||
)
|
||||
assert.Equal(t, exposureType, detail.ExposureType)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCredentialExposureDetail_ExposureLocations(t *testing.T) {
|
||||
// 验证暴露位置常量
|
||||
validLocations := []string{
|
||||
"response_body",
|
||||
"response_header",
|
||||
"log_file",
|
||||
"export_file",
|
||||
}
|
||||
|
||||
for _, location := range validLocations {
|
||||
detail := NewCredentialExposureDetail(
|
||||
"exposed_in_response",
|
||||
location,
|
||||
"pattern",
|
||||
"fragment",
|
||||
"SCAN-001",
|
||||
)
|
||||
assert.Equal(t, location, detail.ExposureLocation)
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== M-014 凭证入站事件详情 ====================
|
||||
|
||||
func TestCredentialIngressDetail_New(t *testing.T) {
|
||||
// M-014: 凭证入站类型专用
|
||||
detail := NewCredentialIngressDetail(
|
||||
"platform_token",
|
||||
"platform_token",
|
||||
true,
|
||||
true,
|
||||
false,
|
||||
)
|
||||
|
||||
assert.Equal(t, "platform_token", detail.RequestCredentialType)
|
||||
assert.Equal(t, "platform_token", detail.ExpectedCredentialType)
|
||||
assert.True(t, detail.CoverageCompliant)
|
||||
assert.True(t, detail.PlatformTokenPresent)
|
||||
assert.False(t, detail.UpstreamKeyPresent)
|
||||
assert.False(t, detail.Reviewed)
|
||||
assert.Nil(t, detail.ReviewedAt)
|
||||
assert.Nil(t, detail.ReviewedBy)
|
||||
}
|
||||
|
||||
func TestCredentialIngressDetail_NonCompliant(t *testing.T) {
|
||||
// M-014 非合规场景:使用 query_key 而不是 platform_token
|
||||
detail := NewCredentialIngressDetail(
|
||||
"query_key",
|
||||
"platform_token",
|
||||
false,
|
||||
false,
|
||||
true,
|
||||
)
|
||||
|
||||
assert.Equal(t, "query_key", detail.RequestCredentialType)
|
||||
assert.Equal(t, "platform_token", detail.ExpectedCredentialType)
|
||||
assert.False(t, detail.CoverageCompliant)
|
||||
assert.False(t, detail.PlatformTokenPresent)
|
||||
assert.True(t, detail.UpstreamKeyPresent)
|
||||
}
|
||||
|
||||
func TestCredentialIngressDetail_Review(t *testing.T) {
|
||||
detail := NewCredentialIngressDetail(
|
||||
"platform_token",
|
||||
"platform_token",
|
||||
true,
|
||||
true,
|
||||
false,
|
||||
)
|
||||
|
||||
detail.Review(1001)
|
||||
|
||||
assert.True(t, detail.Reviewed)
|
||||
assert.NotNil(t, detail.ReviewedAt)
|
||||
assert.Equal(t, int64(1001), *detail.ReviewedBy)
|
||||
}
|
||||
|
||||
func TestCredentialIngressDetail_CredentialTypes(t *testing.T) {
|
||||
// 验证凭证类型
|
||||
testCases := []struct {
|
||||
credType string
|
||||
platformToken bool
|
||||
upstreamKey bool
|
||||
compliant bool
|
||||
}{
|
||||
{"platform_token", true, false, true},
|
||||
{"query_key", false, false, false},
|
||||
{"upstream_api_key", false, true, false},
|
||||
{"none", false, false, false},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
detail := NewCredentialIngressDetail(
|
||||
tc.credType,
|
||||
"platform_token",
|
||||
tc.compliant,
|
||||
tc.platformToken,
|
||||
tc.upstreamKey,
|
||||
)
|
||||
assert.Equal(t, tc.compliant, detail.CoverageCompliant, "Compliance mismatch for %s", tc.credType)
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== M-015 直连绕过事件详情 ====================
|
||||
|
||||
func TestDirectCallDetail_New(t *testing.T) {
|
||||
// M-015: 直连绕过专用
|
||||
detail := NewDirectCallDetail(
|
||||
1001, // consumerID
|
||||
2001, // supplierID
|
||||
"https://supplier.example.com/v1/chat/completions",
|
||||
false, // viaPlatform
|
||||
"ip_bypass",
|
||||
"upstream_api_pattern_match",
|
||||
)
|
||||
|
||||
assert.Equal(t, int64(1001), detail.ConsumerID)
|
||||
assert.Equal(t, int64(2001), detail.SupplierID)
|
||||
assert.Equal(t, "https://supplier.example.com/v1/chat/completions", detail.DirectEndpoint)
|
||||
assert.False(t, detail.ViaPlatform)
|
||||
assert.Equal(t, "ip_bypass", detail.BypassType)
|
||||
assert.Equal(t, "upstream_api_pattern_match", detail.DetectionMethod)
|
||||
assert.False(t, detail.Blocked)
|
||||
assert.Nil(t, detail.BlockedAt)
|
||||
assert.Empty(t, detail.BlockReason)
|
||||
}
|
||||
|
||||
func TestDirectCallDetail_Block(t *testing.T) {
|
||||
detail := NewDirectCallDetail(
|
||||
1001,
|
||||
2001,
|
||||
"https://supplier.example.com/v1/chat/completions",
|
||||
false,
|
||||
"ip_bypass",
|
||||
"upstream_api_pattern_match",
|
||||
)
|
||||
|
||||
detail.Block("P0 event - immediate block")
|
||||
|
||||
assert.True(t, detail.Blocked)
|
||||
assert.NotNil(t, detail.BlockedAt)
|
||||
assert.Equal(t, "P0 event - immediate block", detail.BlockReason)
|
||||
}
|
||||
|
||||
func TestDirectCallDetail_BypassTypes(t *testing.T) {
|
||||
// 验证绕过类型常量
|
||||
validBypassTypes := []string{
|
||||
"ip_bypass",
|
||||
"proxy_bypass",
|
||||
"config_bypass",
|
||||
"dns_bypass",
|
||||
}
|
||||
|
||||
for _, bypassType := range validBypassTypes {
|
||||
detail := NewDirectCallDetail(
|
||||
1001,
|
||||
2001,
|
||||
"https://example.com",
|
||||
false,
|
||||
bypassType,
|
||||
"detection_method",
|
||||
)
|
||||
assert.Equal(t, bypassType, detail.BypassType)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectCallDetail_DetectionMethods(t *testing.T) {
|
||||
// 验证检测方法常量
|
||||
validMethods := []string{
|
||||
"upstream_api_pattern_match",
|
||||
"dns_resolution_check",
|
||||
"connection_source_check",
|
||||
"ip_whitelist_check",
|
||||
}
|
||||
|
||||
for _, method := range validMethods {
|
||||
detail := NewDirectCallDetail(
|
||||
1001,
|
||||
2001,
|
||||
"https://example.com",
|
||||
false,
|
||||
"ip_bypass",
|
||||
method,
|
||||
)
|
||||
assert.Equal(t, method, detail.DetectionMethod)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectCallDetail_ViaPlatform(t *testing.T) {
|
||||
// 通过平台的调用不应该标记为直连
|
||||
detail := NewDirectCallDetail(
|
||||
1001,
|
||||
2001,
|
||||
"https://platform.example.com/v1/chat/completions",
|
||||
true, // viaPlatform = true
|
||||
"",
|
||||
"platform_proxy",
|
||||
)
|
||||
|
||||
assert.True(t, detail.ViaPlatform)
|
||||
assert.False(t, detail.Blocked)
|
||||
}
|
||||
|
||||
// ==================== M-016 Query Key 拒绝事件详情 ====================
|
||||
|
||||
func TestQueryKeyRejectDetail_New(t *testing.T) {
|
||||
// M-016: query key 拒绝专用
|
||||
detail := NewQueryKeyRejectDetail(
|
||||
"qk-12345",
|
||||
"/v1/chat/completions",
|
||||
"not_allowed",
|
||||
"QUERY_KEY_NOT_ALLOWED",
|
||||
)
|
||||
|
||||
assert.Equal(t, "qk-12345", detail.QueryKeyID)
|
||||
assert.Equal(t, "/v1/chat/completions", detail.RequestedEndpoint)
|
||||
assert.Equal(t, "not_allowed", detail.RejectReason)
|
||||
assert.Equal(t, "QUERY_KEY_NOT_ALLOWED", detail.RejectCode)
|
||||
assert.True(t, detail.FirstOccurrence)
|
||||
assert.Equal(t, 1, detail.OccurrenceCount)
|
||||
}
|
||||
|
||||
func TestQueryKeyRejectDetail_RecordOccurrence(t *testing.T) {
|
||||
detail := NewQueryKeyRejectDetail(
|
||||
"qk-12345",
|
||||
"/v1/chat/completions",
|
||||
"not_allowed",
|
||||
"QUERY_KEY_NOT_ALLOWED",
|
||||
)
|
||||
|
||||
// 第二次发生
|
||||
detail.RecordOccurrence(false)
|
||||
assert.Equal(t, 2, detail.OccurrenceCount)
|
||||
assert.False(t, detail.FirstOccurrence)
|
||||
|
||||
// 第三次发生
|
||||
detail.RecordOccurrence(false)
|
||||
assert.Equal(t, 3, detail.OccurrenceCount)
|
||||
}
|
||||
|
||||
func TestQueryKeyRejectDetail_RejectReasons(t *testing.T) {
|
||||
// 验证拒绝原因常量
|
||||
validReasons := []string{
|
||||
"not_allowed",
|
||||
"expired",
|
||||
"malformed",
|
||||
"revoked",
|
||||
"rate_limited",
|
||||
}
|
||||
|
||||
for _, reason := range validReasons {
|
||||
detail := NewQueryKeyRejectDetail(
|
||||
"qk-12345",
|
||||
"/v1/chat/completions",
|
||||
reason,
|
||||
"QUERY_KEY_REJECT",
|
||||
)
|
||||
assert.Equal(t, reason, detail.RejectReason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueryKeyRejectDetail_RejectCodes(t *testing.T) {
|
||||
// 验证拒绝码常量
|
||||
validCodes := []string{
|
||||
"QUERY_KEY_NOT_ALLOWED",
|
||||
"QUERY_KEY_EXPIRED",
|
||||
"QUERY_KEY_MALFORMED",
|
||||
"QUERY_KEY_REVOKED",
|
||||
"QUERY_KEY_RATE_LIMITED",
|
||||
}
|
||||
|
||||
for _, code := range validCodes {
|
||||
detail := NewQueryKeyRejectDetail(
|
||||
"qk-12345",
|
||||
"/v1/chat/completions",
|
||||
"not_allowed",
|
||||
code,
|
||||
)
|
||||
assert.Equal(t, code, detail.RejectCode)
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 指标计算辅助函数 ====================
|
||||
|
||||
func TestCalculateM013(t *testing.T) {
|
||||
// M-013: 凭证泄露事件数 = 0
|
||||
events := []struct {
|
||||
eventName string
|
||||
resolved bool
|
||||
}{
|
||||
{"CRED-EXPOSE-RESPONSE", true},
|
||||
{"CRED-EXPOSE-RESPONSE", true},
|
||||
{"CRED-EXPOSE-LOG", false},
|
||||
{"AUTH-TOKEN-OK", true},
|
||||
}
|
||||
|
||||
var unresolvedCount int
|
||||
for _, e := range events {
|
||||
if IsM013Event(e.eventName) && !e.resolved {
|
||||
unresolvedCount++
|
||||
}
|
||||
}
|
||||
|
||||
assert.Equal(t, 1, unresolvedCount, "M-013 should have 1 unresolved event")
|
||||
}
|
||||
|
||||
func TestCalculateM014(t *testing.T) {
|
||||
// M-014: 平台凭证入站覆盖率 = 100%
|
||||
events := []struct {
|
||||
credentialType string
|
||||
compliant bool
|
||||
}{
|
||||
{"platform_token", true},
|
||||
{"platform_token", true},
|
||||
{"query_key", false},
|
||||
{"upstream_api_key", false},
|
||||
{"platform_token", true},
|
||||
}
|
||||
|
||||
var platformCount, totalCount int
|
||||
for _, e := range events {
|
||||
if IsM014Compliant(e.credentialType) {
|
||||
platformCount++
|
||||
}
|
||||
totalCount++
|
||||
}
|
||||
|
||||
coverage := float64(platformCount) / float64(totalCount) * 100
|
||||
assert.Equal(t, 60.0, coverage, "M-014 coverage should be 60%%")
|
||||
assert.Equal(t, 3, platformCount)
|
||||
assert.Equal(t, 5, totalCount)
|
||||
}
|
||||
|
||||
func TestCalculateM015(t *testing.T) {
|
||||
// M-015: 直连事件数 = 0
|
||||
events := []struct {
|
||||
targetDirect bool
|
||||
blocked bool
|
||||
}{
|
||||
{targetDirect: true, blocked: false},
|
||||
{targetDirect: true, blocked: true},
|
||||
{targetDirect: false, blocked: false},
|
||||
{targetDirect: true, blocked: false},
|
||||
}
|
||||
|
||||
var directCallCount, blockedCount int
|
||||
for _, e := range events {
|
||||
if e.targetDirect {
|
||||
directCallCount++
|
||||
if e.blocked {
|
||||
blockedCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert.Equal(t, 3, directCallCount, "M-015 should have 3 direct call events")
|
||||
assert.Equal(t, 1, blockedCount, "M-015 should have 1 blocked event")
|
||||
}
|
||||
|
||||
func TestCalculateM016(t *testing.T) {
|
||||
// M-016: query key 拒绝率 = 100%
|
||||
// 分母:所有query key请求(不含被拒绝的无效请求)
|
||||
events := []struct {
|
||||
eventName string
|
||||
}{
|
||||
{"AUTH-QUERY-KEY"},
|
||||
{"AUTH-QUERY-REJECT"},
|
||||
{"AUTH-QUERY-KEY"},
|
||||
{"AUTH-QUERY-REJECT"},
|
||||
{"AUTH-TOKEN-OK"},
|
||||
}
|
||||
|
||||
var totalQueryKey, rejectedCount int
|
||||
for _, e := range events {
|
||||
if IsM016Event(e.eventName) {
|
||||
totalQueryKey++
|
||||
if e.eventName == "AUTH-QUERY-REJECT" {
|
||||
rejectedCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rejectRate := float64(rejectedCount) / float64(totalQueryKey) * 100
|
||||
assert.Equal(t, 4, totalQueryKey, "M-016 should have 4 query key events")
|
||||
assert.Equal(t, 2, rejectedCount, "M-016 should have 2 rejected events")
|
||||
assert.Equal(t, 50.0, rejectRate, "M-016 reject rate should be 50%%")
|
||||
}
|
||||
|
||||
// IsM014Compliant 检查凭证类型是否为M-014合规
|
||||
func IsM014Compliant(credentialType string) bool {
|
||||
return credentialType == CredentialTypePlatformToken
|
||||
}
|
||||
279
supply-api/internal/audit/sanitizer/sanitizer.go
Normal file
279
supply-api/internal/audit/sanitizer/sanitizer.go
Normal file
@@ -0,0 +1,279 @@
|
||||
package sanitizer
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ScanRule 扫描规则
|
||||
type ScanRule struct {
|
||||
ID string
|
||||
Pattern *regexp.Regexp
|
||||
Description string
|
||||
Severity string
|
||||
}
|
||||
|
||||
// Violation 违规项
|
||||
type Violation struct {
|
||||
Type string // 违规类型
|
||||
Pattern string // 匹配的正则模式
|
||||
Value string // 匹配的值(已脱敏)
|
||||
Description string
|
||||
}
|
||||
|
||||
// ScanResult 扫描结果
|
||||
type ScanResult struct {
|
||||
Violations []Violation
|
||||
Passed bool
|
||||
}
|
||||
|
||||
// NewScanResult 创建扫描结果
|
||||
func NewScanResult() *ScanResult {
|
||||
return &ScanResult{
|
||||
Violations: []Violation{},
|
||||
Passed: true,
|
||||
}
|
||||
}
|
||||
|
||||
// HasViolation 检查是否有违规
|
||||
func (r *ScanResult) HasViolation() bool {
|
||||
return len(r.Violations) > 0
|
||||
}
|
||||
|
||||
// AddViolation 添加违规项
|
||||
func (r *ScanResult) AddViolation(v Violation) {
|
||||
r.Violations = append(r.Violations, v)
|
||||
r.Passed = false
|
||||
}
|
||||
|
||||
// CredentialScanner 凭证扫描器
|
||||
type CredentialScanner struct {
|
||||
rules []ScanRule
|
||||
}
|
||||
|
||||
// NewCredentialScanner 创建凭证扫描器
|
||||
func NewCredentialScanner() *CredentialScanner {
|
||||
scanner := &CredentialScanner{
|
||||
rules: []ScanRule{
|
||||
{
|
||||
ID: "openai_key",
|
||||
Pattern: regexp.MustCompile(`sk-[a-zA-Z0-9]{20,}`),
|
||||
Description: "OpenAI API Key",
|
||||
Severity: "HIGH",
|
||||
},
|
||||
{
|
||||
ID: "api_key",
|
||||
Pattern: regexp.MustCompile(`(?i)(api[_-]?key|apikey)["\s:=]+['"]?([a-zA-Z0-9_\-]{16,})['"]?`),
|
||||
Description: "Generic API Key",
|
||||
Severity: "MEDIUM",
|
||||
},
|
||||
{
|
||||
ID: "aws_access_key",
|
||||
Pattern: regexp.MustCompile(`(?i)(access[_-]?key[_-]?id|aws[_-]?access[_-]?key)["\s:=]+['"]?(AKIA[0-9A-Z]{16})['"]?`),
|
||||
Description: "AWS Access Key ID",
|
||||
Severity: "HIGH",
|
||||
},
|
||||
{
|
||||
ID: "aws_secret_key",
|
||||
Pattern: regexp.MustCompile(`(?i)(secret[_-]?key|aws[_-]?.*secret[_-]?key)["\s:=]+['"]?([a-zA-Z0-9/+=]{40})['"]?`),
|
||||
Description: "AWS Secret Access Key",
|
||||
Severity: "HIGH",
|
||||
},
|
||||
{
|
||||
ID: "password",
|
||||
Pattern: regexp.MustCompile(`(?i)(password|passwd|pwd)["\s:=]+['"]?([a-zA-Z0-9@#$%^&*!]{8,})['"]?`),
|
||||
Description: "Password",
|
||||
Severity: "HIGH",
|
||||
},
|
||||
{
|
||||
ID: "bearer_token",
|
||||
Pattern: regexp.MustCompile(`(?i)(token|bearer|authorization)["\s:=]+['"]?([Bb]earer\s+)?([a-zA-Z0-9_\-\.]+)['"]?`),
|
||||
Description: "Bearer Token",
|
||||
Severity: "MEDIUM",
|
||||
},
|
||||
{
|
||||
ID: "private_key",
|
||||
Pattern: regexp.MustCompile(`-----BEGIN\s+(RSA\s+)?PRIVATE\s+KEY-----`),
|
||||
Description: "Private Key",
|
||||
Severity: "CRITICAL",
|
||||
},
|
||||
{
|
||||
ID: "secret",
|
||||
Pattern: regexp.MustCompile(`(?i)(secret|client[_-]?secret)["\s:=]+['"]?([a-zA-Z0-9_\-]{16,})['"]?`),
|
||||
Description: "Secret",
|
||||
Severity: "HIGH",
|
||||
},
|
||||
},
|
||||
}
|
||||
return scanner
|
||||
}
|
||||
|
||||
// Scan 扫描内容
|
||||
func (s *CredentialScanner) Scan(content string) *ScanResult {
|
||||
result := NewScanResult()
|
||||
|
||||
for _, rule := range s.rules {
|
||||
matches := rule.Pattern.FindAllStringSubmatch(content, -1)
|
||||
for _, match := range matches {
|
||||
// 构建违规项
|
||||
violation := Violation{
|
||||
Type: rule.ID,
|
||||
Pattern: rule.Pattern.String(),
|
||||
Description: rule.Description,
|
||||
}
|
||||
|
||||
// 提取匹配的值(取最后一个匹配组)
|
||||
if len(match) > 1 {
|
||||
violation.Value = maskString(match[len(match)-1])
|
||||
} else {
|
||||
violation.Value = maskString(match[0])
|
||||
}
|
||||
|
||||
result.AddViolation(violation)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// GetRules 获取扫描规则
|
||||
func (s *CredentialScanner) GetRules() []ScanRule {
|
||||
return s.rules
|
||||
}
|
||||
|
||||
// Sanitizer 脱敏器
|
||||
type Sanitizer struct {
|
||||
patterns []*regexp.Regexp
|
||||
}
|
||||
|
||||
// NewSanitizer 创建脱敏器
|
||||
func NewSanitizer() *Sanitizer {
|
||||
return &Sanitizer{
|
||||
patterns: []*regexp.Regexp{
|
||||
// OpenAI API Key
|
||||
regexp.MustCompile(`(sk-[a-zA-Z0-9]{4})[a-zA-Z0-9]+([a-zA-Z0-9]{4})`),
|
||||
// AWS Access Key
|
||||
regexp.MustCompile(`(AKIA[0-9A-Z]{4})[0-9A-Z]+([0-9A-Z]{4})`),
|
||||
// Generic API Key
|
||||
regexp.MustCompile(`([a-zA-Z0-9_\-]{4})[a-zA-Z0-9_\-]{8,}([a-zA-Z0-9_\-]{4})`),
|
||||
// Password
|
||||
regexp.MustCompile(`([a-zA-Z0-9@#$%^&*!]{4})[a-zA-Z0-9@#$%^&*!]+([a-zA-Z0-9@#$%^&*!]{4})`),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Mask 对字符串进行脱敏
|
||||
func (s *Sanitizer) Mask(content string) string {
|
||||
result := content
|
||||
|
||||
for _, pattern := range s.patterns {
|
||||
// 替换为格式:前4字符 + **** + 后4字符
|
||||
result = pattern.ReplaceAllStringFunc(result, func(match string) string {
|
||||
// 尝试分组替换
|
||||
re := regexp.MustCompile(`^(.{4}).+(.{4})$`)
|
||||
submatch := re.FindStringSubmatch(match)
|
||||
if len(submatch) == 3 {
|
||||
return submatch[1] + "****" + submatch[2]
|
||||
}
|
||||
// 如果无法分组,直接掩码
|
||||
if len(match) > 8 {
|
||||
return match[:4] + "****" + match[len(match)-4:]
|
||||
}
|
||||
return "****"
|
||||
})
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// MaskMap 对map进行脱敏
|
||||
func (s *Sanitizer) MaskMap(data map[string]interface{}) map[string]interface{} {
|
||||
result := make(map[string]interface{})
|
||||
|
||||
for key, value := range data {
|
||||
if IsSensitiveField(key) {
|
||||
if str, ok := value.(string); ok {
|
||||
result[key] = s.Mask(str)
|
||||
} else {
|
||||
result[key] = value
|
||||
}
|
||||
} else {
|
||||
result[key] = s.maskValue(value)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// MaskSlice 对slice进行脱敏
|
||||
func (s *Sanitizer) MaskSlice(data []string) []string {
|
||||
result := make([]string, len(data))
|
||||
for i, item := range data {
|
||||
result[i] = s.Mask(item)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// maskValue 递归掩码
|
||||
func (s *Sanitizer) maskValue(value interface{}) interface{} {
|
||||
switch v := value.(type) {
|
||||
case string:
|
||||
return s.Mask(v)
|
||||
case map[string]interface{}:
|
||||
return s.MaskMap(v)
|
||||
case []interface{}:
|
||||
result := make([]interface{}, len(v))
|
||||
for i, item := range v {
|
||||
result[i] = s.maskValue(item)
|
||||
}
|
||||
return result
|
||||
case []string:
|
||||
return s.MaskSlice(v)
|
||||
default:
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
// maskString 掩码字符串
|
||||
func maskString(s string) string {
|
||||
if len(s) > 8 {
|
||||
return s[:4] + "****" + s[len(s)-4:]
|
||||
}
|
||||
return "****"
|
||||
}
|
||||
|
||||
// GetSensitiveFields 获取敏感字段列表
|
||||
func GetSensitiveFields() []string {
|
||||
return []string{
|
||||
"api_key",
|
||||
"apikey",
|
||||
"secret",
|
||||
"secret_key",
|
||||
"password",
|
||||
"passwd",
|
||||
"pwd",
|
||||
"token",
|
||||
"access_key",
|
||||
"access_key_id",
|
||||
"private_key",
|
||||
"session_id",
|
||||
"authorization",
|
||||
"bearer",
|
||||
"client_secret",
|
||||
"credentials",
|
||||
}
|
||||
}
|
||||
|
||||
// IsSensitiveField 判断字段名是否为敏感字段
|
||||
func IsSensitiveField(fieldName string) bool {
|
||||
lowerName := strings.ToLower(fieldName)
|
||||
sensitiveFields := GetSensitiveFields()
|
||||
|
||||
for _, sf := range sensitiveFields {
|
||||
if strings.Contains(lowerName, sf) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
290
supply-api/internal/audit/sanitizer/sanitizer_test.go
Normal file
290
supply-api/internal/audit/sanitizer/sanitizer_test.go
Normal file
@@ -0,0 +1,290 @@
|
||||
package sanitizer
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSanitizer_Scan_CredentialExposure(t *testing.T) {
|
||||
// 检测响应体中的凭证泄露
|
||||
scanner := NewCredentialScanner()
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
content string
|
||||
expectFound bool
|
||||
expectedTypes []string
|
||||
}{
|
||||
{
|
||||
name: "OpenAI API Key",
|
||||
content: "Your API key is sk-1234567890abcdefghijklmnopqrstuvwxyz",
|
||||
expectFound: true,
|
||||
expectedTypes: []string{"openai_key"},
|
||||
},
|
||||
{
|
||||
name: "AWS Access Key",
|
||||
content: "access_key_id: AKIAIOSFODNN7EXAMPLE",
|
||||
expectFound: true,
|
||||
expectedTypes: []string{"aws_access_key"},
|
||||
},
|
||||
{
|
||||
name: "Client Secret",
|
||||
content: "client_secret: c3VwZXJzZWNyZXRrZXlzZWNyZXRrZXk=",
|
||||
expectFound: true,
|
||||
expectedTypes: []string{"secret"},
|
||||
},
|
||||
{
|
||||
name: "Generic API Key",
|
||||
content: "api_key: key-1234567890abcdefghij",
|
||||
expectFound: true,
|
||||
expectedTypes: []string{"api_key"},
|
||||
},
|
||||
{
|
||||
name: "Password Field",
|
||||
content: "password: mysecretpassword123",
|
||||
expectFound: true,
|
||||
expectedTypes: []string{"password"},
|
||||
},
|
||||
{
|
||||
name: "Token Field",
|
||||
content: "token: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9",
|
||||
expectFound: true,
|
||||
expectedTypes: []string{"bearer_token"},
|
||||
},
|
||||
{
|
||||
name: "Normal Text",
|
||||
content: "This is normal text without credentials",
|
||||
expectFound: false,
|
||||
expectedTypes: nil,
|
||||
},
|
||||
{
|
||||
name: "Already Masked",
|
||||
content: "api_key: sk-****-****",
|
||||
expectFound: false,
|
||||
expectedTypes: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := scanner.Scan(tc.content)
|
||||
|
||||
if tc.expectFound {
|
||||
assert.True(t, result.HasViolation(), "Expected violation for: %s", tc.name)
|
||||
assert.NotEmpty(t, result.Violations, "Expected violations for: %s", tc.name)
|
||||
|
||||
var foundTypes []string
|
||||
for _, v := range result.Violations {
|
||||
foundTypes = append(foundTypes, v.Type)
|
||||
}
|
||||
|
||||
for _, expectedType := range tc.expectedTypes {
|
||||
assert.Contains(t, foundTypes, expectedType, "Expected type %s in violations for: %s", expectedType, tc.name)
|
||||
}
|
||||
} else {
|
||||
assert.False(t, result.HasViolation(), "Expected no violation for: %s", tc.name)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizer_Scan_Masking(t *testing.T) {
|
||||
// 脱敏:'sk-xxxx' 格式
|
||||
sanitizer := NewSanitizer()
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
expectedOutput string
|
||||
expectMasked bool
|
||||
}{
|
||||
{
|
||||
name: "OpenAI Key",
|
||||
input: "sk-1234567890abcdefghijklmnopqrstuvwxyz",
|
||||
expectedOutput: "sk-xxxxxx****xxxx",
|
||||
expectMasked: true,
|
||||
},
|
||||
{
|
||||
name: "Short OpenAI Key",
|
||||
input: "sk-1234567890",
|
||||
expectedOutput: "sk-****7890",
|
||||
expectMasked: true,
|
||||
},
|
||||
{
|
||||
name: "AWS Access Key",
|
||||
input: "AKIAIOSFODNN7EXAMPLE",
|
||||
expectedOutput: "AKIA****EXAMPLE",
|
||||
expectMasked: true,
|
||||
},
|
||||
{
|
||||
name: "Normal Text",
|
||||
input: "This is normal text",
|
||||
expectedOutput: "This is normal text",
|
||||
expectMasked: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := sanitizer.Mask(tc.input)
|
||||
|
||||
if tc.expectMasked {
|
||||
assert.NotEqual(t, tc.input, result, "Expected masking for: %s", tc.name)
|
||||
assert.Contains(t, result, "****", "Expected **** in masked result for: %s", tc.name)
|
||||
} else {
|
||||
assert.Equal(t, tc.expectedOutput, result, "Expected unchanged for: %s", tc.name)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizer_Scan_ResponseBody(t *testing.T) {
|
||||
// 检测响应体中的凭证泄露
|
||||
scanner := NewCredentialScanner()
|
||||
|
||||
responseBody := `{
|
||||
"success": true,
|
||||
"data": {
|
||||
"api_key": "sk-1234567890abcdefghijklmnopqrstuvwxyz",
|
||||
"user": "testuser"
|
||||
}
|
||||
}`
|
||||
|
||||
result := scanner.Scan(responseBody)
|
||||
|
||||
assert.True(t, result.HasViolation())
|
||||
assert.NotEmpty(t, result.Violations)
|
||||
|
||||
// 验证找到了api_key类型的违规
|
||||
foundTypes := make([]string, 0)
|
||||
for _, v := range result.Violations {
|
||||
foundTypes = append(foundTypes, v.Type)
|
||||
}
|
||||
assert.Contains(t, foundTypes, "api_key")
|
||||
}
|
||||
|
||||
func TestSanitizer_MaskMap(t *testing.T) {
|
||||
// 测试对map进行脱敏
|
||||
sanitizer := NewSanitizer()
|
||||
|
||||
input := map[string]interface{}{
|
||||
"api_key": "sk-1234567890abcdefghijklmnopqrstuvwxyz",
|
||||
"secret": "mysecretkey123",
|
||||
"user": "testuser",
|
||||
}
|
||||
|
||||
masked := sanitizer.MaskMap(input)
|
||||
|
||||
// 验证敏感字段被脱敏
|
||||
assert.NotEqual(t, input["api_key"], masked["api_key"])
|
||||
assert.NotEqual(t, input["secret"], masked["secret"])
|
||||
assert.Equal(t, input["user"], masked["user"])
|
||||
|
||||
// 验证脱敏格式
|
||||
assert.Contains(t, masked["api_key"], "****")
|
||||
assert.Contains(t, masked["secret"], "****")
|
||||
}
|
||||
|
||||
func TestSanitizer_MaskSlice(t *testing.T) {
|
||||
// 测试对slice进行脱敏
|
||||
sanitizer := NewSanitizer()
|
||||
|
||||
input := []string{
|
||||
"sk-1234567890abcdefghijklmnopqrstuvwxyz",
|
||||
"normal text",
|
||||
"password123",
|
||||
}
|
||||
|
||||
masked := sanitizer.MaskSlice(input)
|
||||
|
||||
assert.Len(t, masked, 3)
|
||||
assert.NotEqual(t, input[0], masked[0])
|
||||
assert.Equal(t, input[1], masked[1])
|
||||
assert.NotEqual(t, input[2], masked[2])
|
||||
}
|
||||
|
||||
func TestCredentialScanner_SensitiveFields(t *testing.T) {
|
||||
// 测试敏感字段列表
|
||||
fields := GetSensitiveFields()
|
||||
|
||||
// 验证常见敏感字段
|
||||
assert.Contains(t, fields, "api_key")
|
||||
assert.Contains(t, fields, "secret")
|
||||
assert.Contains(t, fields, "password")
|
||||
assert.Contains(t, fields, "token")
|
||||
assert.Contains(t, fields, "access_key")
|
||||
assert.Contains(t, fields, "private_key")
|
||||
}
|
||||
|
||||
func TestCredentialScanner_ScanRules(t *testing.T) {
|
||||
// 测试扫描规则
|
||||
scanner := NewCredentialScanner()
|
||||
|
||||
rules := scanner.GetRules()
|
||||
assert.NotEmpty(t, rules, "Scanner should have rules")
|
||||
|
||||
// 验证规则有ID和描述
|
||||
for _, rule := range rules {
|
||||
assert.NotEmpty(t, rule.ID)
|
||||
assert.NotEmpty(t, rule.Description)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizer_IsSensitiveField(t *testing.T) {
|
||||
// 测试字段名敏感性判断
|
||||
testCases := []struct {
|
||||
fieldName string
|
||||
expected bool
|
||||
}{
|
||||
{"api_key", true},
|
||||
{"secret", true},
|
||||
{"password", true},
|
||||
{"token", true},
|
||||
{"access_key", true},
|
||||
{"private_key", true},
|
||||
{"session_id", true},
|
||||
{"authorization", true},
|
||||
{"user", false},
|
||||
{"name", false},
|
||||
{"email", false},
|
||||
{"id", false},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.fieldName, func(t *testing.T) {
|
||||
result := IsSensitiveField(tc.fieldName)
|
||||
assert.Equal(t, tc.expected, result, "Field %s sensitivity mismatch", tc.fieldName)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizer_ScanLog(t *testing.T) {
|
||||
// 测试日志扫描
|
||||
scanner := NewCredentialScanner()
|
||||
|
||||
logLine := `2026-04-02 10:30:45 INFO [api] Request completed api_key=sk-1234567890abcdefghijklmnopqrstuvwxyz duration=100ms`
|
||||
|
||||
result := scanner.Scan(logLine)
|
||||
|
||||
assert.True(t, result.HasViolation())
|
||||
assert.NotEmpty(t, result.Violations)
|
||||
// sk-开头的key会被识别为openai_key
|
||||
assert.Equal(t, "openai_key", result.Violations[0].Type)
|
||||
}
|
||||
|
||||
func TestSanitizer_MultipleViolations(t *testing.T) {
|
||||
// 测试多个违规
|
||||
scanner := NewCredentialScanner()
|
||||
|
||||
content := `{
|
||||
"api_key": "sk-1234567890abcdefghijklmnopqrstuvwxyz",
|
||||
"secret_key": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
|
||||
"password": "mysecretpassword"
|
||||
}`
|
||||
|
||||
result := scanner.Scan(content)
|
||||
|
||||
assert.True(t, result.HasViolation())
|
||||
assert.GreaterOrEqual(t, len(result.Violations), 3)
|
||||
}
|
||||
308
supply-api/internal/audit/service/audit_service.go
Normal file
308
supply-api/internal/audit/service/audit_service.go
Normal file
@@ -0,0 +1,308 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"lijiaoqiao/supply-api/internal/audit/model"
|
||||
)
|
||||
|
||||
// 错误定义
|
||||
var (
|
||||
ErrInvalidInput = errors.New("invalid input: event is nil")
|
||||
ErrMissingEventName = errors.New("invalid input: event name is required")
|
||||
ErrEventNotFound = errors.New("event not found")
|
||||
ErrIdempotencyConflict = errors.New("idempotency key conflict")
|
||||
)
|
||||
|
||||
// CreateEventResult 事件创建结果
|
||||
type CreateEventResult struct {
|
||||
EventID string `json:"event_id"`
|
||||
StatusCode int `json:"status_code"`
|
||||
Status string `json:"status"`
|
||||
OriginalCreatedAt *time.Time `json:"original_created_at,omitempty"`
|
||||
ErrorCode string `json:"error_code,omitempty"`
|
||||
ErrorMessage string `json:"error_message,omitempty"`
|
||||
RetryAfterMs int64 `json:"retry_after_ms,omitempty"`
|
||||
}
|
||||
|
||||
// EventFilter 事件查询过滤器
|
||||
type EventFilter struct {
|
||||
TenantID int64
|
||||
Category string
|
||||
EventName string
|
||||
ObjectType string
|
||||
ObjectID int64
|
||||
StartTime time.Time
|
||||
EndTime time.Time
|
||||
Success *bool
|
||||
Limit int
|
||||
Offset int
|
||||
}
|
||||
|
||||
// AuditStoreInterface 审计存储接口
|
||||
type AuditStoreInterface interface {
|
||||
Emit(ctx context.Context, event *model.AuditEvent) error
|
||||
Query(ctx context.Context, filter *EventFilter) ([]*model.AuditEvent, int64, error)
|
||||
GetByIdempotencyKey(ctx context.Context, key string) (*model.AuditEvent, error)
|
||||
}
|
||||
|
||||
// InMemoryAuditStore 内存审计存储
|
||||
type InMemoryAuditStore struct {
|
||||
mu sync.RWMutex
|
||||
events []*model.AuditEvent
|
||||
nextID int64
|
||||
idempotencyKeys map[string]*model.AuditEvent
|
||||
}
|
||||
|
||||
// NewInMemoryAuditStore 创建内存审计存储
|
||||
func NewInMemoryAuditStore() *InMemoryAuditStore {
|
||||
return &InMemoryAuditStore{
|
||||
events: make([]*model.AuditEvent, 0),
|
||||
nextID: 1,
|
||||
idempotencyKeys: make(map[string]*model.AuditEvent),
|
||||
}
|
||||
}
|
||||
|
||||
// Emit 发送事件
|
||||
func (s *InMemoryAuditStore) Emit(ctx context.Context, event *model.AuditEvent) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
// 生成事件ID
|
||||
if event.EventID == "" {
|
||||
event.EventID = generateEventID()
|
||||
}
|
||||
event.CreatedAt = time.Now()
|
||||
|
||||
s.events = append(s.events, event)
|
||||
|
||||
// 如果有幂等键,记录映射
|
||||
if event.IdempotencyKey != "" {
|
||||
s.idempotencyKeys[event.IdempotencyKey] = event
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Query 查询事件
|
||||
func (s *InMemoryAuditStore) Query(ctx context.Context, filter *EventFilter) ([]*model.AuditEvent, int64, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
var result []*model.AuditEvent
|
||||
for _, e := range s.events {
|
||||
// 按租户过滤
|
||||
if filter.TenantID > 0 && e.TenantID != filter.TenantID {
|
||||
continue
|
||||
}
|
||||
// 按类别过滤
|
||||
if filter.Category != "" && e.EventCategory != filter.Category {
|
||||
continue
|
||||
}
|
||||
// 按事件名称过滤
|
||||
if filter.EventName != "" && e.EventName != filter.EventName {
|
||||
continue
|
||||
}
|
||||
// 按对象类型过滤
|
||||
if filter.ObjectType != "" && e.ObjectType != filter.ObjectType {
|
||||
continue
|
||||
}
|
||||
// 按对象ID过滤
|
||||
if filter.ObjectID > 0 && e.ObjectID != filter.ObjectID {
|
||||
continue
|
||||
}
|
||||
// 按时间范围过滤
|
||||
if !filter.StartTime.IsZero() && e.Timestamp.Before(filter.StartTime) {
|
||||
continue
|
||||
}
|
||||
if !filter.EndTime.IsZero() && e.Timestamp.After(filter.EndTime) {
|
||||
continue
|
||||
}
|
||||
// 按成功状态过滤
|
||||
if filter.Success != nil && e.Success != *filter.Success {
|
||||
continue
|
||||
}
|
||||
|
||||
result = append(result, e)
|
||||
}
|
||||
|
||||
total := int64(len(result))
|
||||
|
||||
// 分页
|
||||
if filter.Offset > 0 {
|
||||
if filter.Offset >= len(result) {
|
||||
return []*model.AuditEvent{}, total, nil
|
||||
}
|
||||
result = result[filter.Offset:]
|
||||
}
|
||||
if filter.Limit > 0 && filter.Limit < len(result) {
|
||||
result = result[:filter.Limit]
|
||||
}
|
||||
|
||||
return result, total, nil
|
||||
}
|
||||
|
||||
// GetByIdempotencyKey 根据幂等键获取事件
|
||||
func (s *InMemoryAuditStore) GetByIdempotencyKey(ctx context.Context, key string) (*model.AuditEvent, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
if event, ok := s.idempotencyKeys[key]; ok {
|
||||
return event, nil
|
||||
}
|
||||
return nil, ErrEventNotFound
|
||||
}
|
||||
|
||||
// generateEventID 生成事件ID
|
||||
func generateEventID() string {
|
||||
now := time.Now()
|
||||
return now.Format("20060102150405.000000") + fmt.Sprintf("%03d", now.Nanosecond()%1000000/1000) + "-evt"
|
||||
}
|
||||
|
||||
// AuditService 审计服务
|
||||
type AuditService struct {
|
||||
store AuditStoreInterface
|
||||
processingDelay time.Duration
|
||||
}
|
||||
|
||||
// NewAuditService 创建审计服务
|
||||
func NewAuditService(store AuditStoreInterface) *AuditService {
|
||||
return &AuditService{
|
||||
store: store,
|
||||
}
|
||||
}
|
||||
|
||||
// SetProcessingDelay 设置处理延迟(用于模拟异步处理)
|
||||
func (s *AuditService) SetProcessingDelay(delay time.Duration) {
|
||||
s.processingDelay = delay
|
||||
}
|
||||
|
||||
// CreateEvent 创建审计事件
|
||||
func (s *AuditService) CreateEvent(ctx context.Context, event *model.AuditEvent) (*CreateEventResult, error) {
|
||||
// 输入验证
|
||||
if event == nil {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
if event.EventName == "" {
|
||||
return nil, ErrMissingEventName
|
||||
}
|
||||
|
||||
// 设置时间戳
|
||||
if event.Timestamp.IsZero() {
|
||||
event.Timestamp = time.Now()
|
||||
}
|
||||
if event.TimestampMs == 0 {
|
||||
event.TimestampMs = event.Timestamp.UnixMilli()
|
||||
}
|
||||
|
||||
// 如果没有事件ID,生成一个
|
||||
if event.EventID == "" {
|
||||
event.EventID = generateEventID()
|
||||
}
|
||||
|
||||
// 处理幂等性
|
||||
if event.IdempotencyKey != "" {
|
||||
existing, err := s.store.GetByIdempotencyKey(ctx, event.IdempotencyKey)
|
||||
if err == nil && existing != nil {
|
||||
// 检查payload是否相同
|
||||
if isSamePayload(existing, event) {
|
||||
// 重放同参 - 返回200
|
||||
return &CreateEventResult{
|
||||
EventID: existing.EventID,
|
||||
StatusCode: 200,
|
||||
Status: "duplicate",
|
||||
OriginalCreatedAt: &existing.CreatedAt,
|
||||
}, nil
|
||||
} else {
|
||||
// 重放异参 - 返回409
|
||||
return &CreateEventResult{
|
||||
StatusCode: 409,
|
||||
Status: "conflict",
|
||||
ErrorCode: "IDEMPOTENCY_PAYLOAD_MISMATCH",
|
||||
ErrorMessage: "Idempotency key reused with different payload",
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 首次创建 - 返回201
|
||||
err := s.store.Emit(ctx, event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &CreateEventResult{
|
||||
EventID: event.EventID,
|
||||
StatusCode: 201,
|
||||
Status: "created",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ListEvents 列出事件(带分页)
|
||||
func (s *AuditService) ListEvents(ctx context.Context, tenantID int64, offset, limit int) ([]*model.AuditEvent, int64, error) {
|
||||
filter := &EventFilter{
|
||||
TenantID: tenantID,
|
||||
Offset: offset,
|
||||
Limit: limit,
|
||||
}
|
||||
return s.store.Query(ctx, filter)
|
||||
}
|
||||
|
||||
// ListEventsWithFilter 列出事件(带过滤器)
|
||||
func (s *AuditService) ListEventsWithFilter(ctx context.Context, filter *EventFilter) ([]*model.AuditEvent, int64, error) {
|
||||
return s.store.Query(ctx, filter)
|
||||
}
|
||||
|
||||
// HashIdempotencyKey 计算幂等键的哈希值
|
||||
func (s *AuditService) HashIdempotencyKey(key string) string {
|
||||
hash := sha256.Sum256([]byte(key))
|
||||
return hex.EncodeToString(hash[:])
|
||||
}
|
||||
|
||||
// isSamePayload 检查两个事件的payload是否相同
|
||||
func isSamePayload(a, b *model.AuditEvent) bool {
|
||||
// 比较关键字段
|
||||
if a.EventName != b.EventName {
|
||||
return false
|
||||
}
|
||||
if a.EventCategory != b.EventCategory {
|
||||
return false
|
||||
}
|
||||
if a.OperatorID != b.OperatorID {
|
||||
return false
|
||||
}
|
||||
if a.TenantID != b.TenantID {
|
||||
return false
|
||||
}
|
||||
if a.ObjectType != b.ObjectType {
|
||||
return false
|
||||
}
|
||||
if a.ObjectID != b.ObjectID {
|
||||
return false
|
||||
}
|
||||
if a.Action != b.Action {
|
||||
return false
|
||||
}
|
||||
if a.CredentialType != b.CredentialType {
|
||||
return false
|
||||
}
|
||||
if a.SourceType != b.SourceType {
|
||||
return false
|
||||
}
|
||||
if a.SourceIP != b.SourceIP {
|
||||
return false
|
||||
}
|
||||
if a.Success != b.Success {
|
||||
return false
|
||||
}
|
||||
if a.ResultCode != b.ResultCode {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
403
supply-api/internal/audit/service/audit_service_test.go
Normal file
403
supply-api/internal/audit/service/audit_service_test.go
Normal file
@@ -0,0 +1,403 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"lijiaoqiao/supply-api/internal/audit/model"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// ==================== 写入API测试 ====================
|
||||
|
||||
func TestAuditService_CreateEvent_Success(t *testing.T) {
|
||||
// 201 首次成功
|
||||
ctx := context.Background()
|
||||
svc := NewAuditService(NewInMemoryAuditStore())
|
||||
|
||||
event := &model.AuditEvent{
|
||||
EventID: "test-event-1",
|
||||
EventName: "CRED-EXPOSE-RESPONSE",
|
||||
EventCategory: "CRED",
|
||||
OperatorID: 1001,
|
||||
TenantID: 2001,
|
||||
ObjectType: "account",
|
||||
ObjectID: 12345,
|
||||
Action: "create",
|
||||
CredentialType: "platform_token",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.1",
|
||||
Success: true,
|
||||
ResultCode: "SEC_CRED_EXPOSED",
|
||||
IdempotencyKey: "idem-key-001",
|
||||
}
|
||||
|
||||
result, err := svc.CreateEvent(ctx, event)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, 201, result.StatusCode)
|
||||
assert.NotEmpty(t, result.EventID)
|
||||
assert.Equal(t, "created", result.Status)
|
||||
}
|
||||
|
||||
func TestAuditService_CreateEvent_IdempotentReplay(t *testing.T) {
|
||||
// 200 重放同参
|
||||
ctx := context.Background()
|
||||
svc := NewAuditService(NewInMemoryAuditStore())
|
||||
|
||||
event := &model.AuditEvent{
|
||||
EventID: "test-event-2",
|
||||
EventName: "CRED-INGRESS-PLATFORM",
|
||||
EventCategory: "CRED",
|
||||
OperatorID: 1001,
|
||||
TenantID: 2001,
|
||||
ObjectType: "account",
|
||||
ObjectID: 12345,
|
||||
Action: "query",
|
||||
CredentialType: "platform_token",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.1",
|
||||
Success: true,
|
||||
ResultCode: "CRED_INGRESS_OK",
|
||||
IdempotencyKey: "idem-key-002",
|
||||
}
|
||||
|
||||
// 首次创建
|
||||
result1, err1 := svc.CreateEvent(ctx, event)
|
||||
assert.NoError(t, err1)
|
||||
assert.Equal(t, 201, result1.StatusCode)
|
||||
|
||||
// 重放同参
|
||||
result2, err2 := svc.CreateEvent(ctx, event)
|
||||
assert.NoError(t, err2)
|
||||
assert.Equal(t, 200, result2.StatusCode)
|
||||
assert.Equal(t, result1.EventID, result2.EventID)
|
||||
assert.Equal(t, "duplicate", result2.Status)
|
||||
}
|
||||
|
||||
func TestAuditService_CreateEvent_PayloadMismatch(t *testing.T) {
|
||||
// 409 重放异参
|
||||
ctx := context.Background()
|
||||
svc := NewAuditService(NewInMemoryAuditStore())
|
||||
|
||||
// 第一次事件
|
||||
event1 := &model.AuditEvent{
|
||||
EventName: "CRED-INGRESS-PLATFORM",
|
||||
EventCategory: "CRED",
|
||||
OperatorID: 1001,
|
||||
TenantID: 2001,
|
||||
ObjectType: "account",
|
||||
ObjectID: 12345,
|
||||
Action: "query",
|
||||
CredentialType: "platform_token",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.1",
|
||||
Success: true,
|
||||
ResultCode: "CRED_INGRESS_OK",
|
||||
IdempotencyKey: "idem-key-003",
|
||||
}
|
||||
|
||||
// 第二次同幂等键但不同payload
|
||||
event2 := &model.AuditEvent{
|
||||
EventName: "CRED-INGRESS-PLATFORM",
|
||||
EventCategory: "CRED",
|
||||
OperatorID: 1002, // 不同的operator
|
||||
TenantID: 2001,
|
||||
ObjectType: "account",
|
||||
ObjectID: 12345,
|
||||
Action: "query",
|
||||
CredentialType: "platform_token",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.1",
|
||||
Success: true,
|
||||
ResultCode: "CRED_INGRESS_OK",
|
||||
IdempotencyKey: "idem-key-003", // 同幂等键
|
||||
}
|
||||
|
||||
// 首次创建
|
||||
result1, err1 := svc.CreateEvent(ctx, event1)
|
||||
assert.NoError(t, err1)
|
||||
assert.Equal(t, 201, result1.StatusCode)
|
||||
|
||||
// 重放异参
|
||||
result2, err2 := svc.CreateEvent(ctx, event2)
|
||||
assert.NoError(t, err2)
|
||||
assert.Equal(t, 409, result2.StatusCode)
|
||||
assert.Equal(t, "IDEMPOTENCY_PAYLOAD_MISMATCH", result2.ErrorCode)
|
||||
}
|
||||
|
||||
func TestAuditService_CreateEvent_InProgress(t *testing.T) {
|
||||
// 202 处理中(模拟异步场景)
|
||||
ctx := context.Background()
|
||||
svc := NewAuditService(NewInMemoryAuditStore())
|
||||
|
||||
// 启用处理中模拟
|
||||
svc.SetProcessingDelay(100 * time.Millisecond)
|
||||
|
||||
event := &model.AuditEvent{
|
||||
EventName: "CRED-DIRECT-SUPPLIER",
|
||||
EventCategory: "CRED",
|
||||
OperatorID: 1001,
|
||||
TenantID: 2001,
|
||||
ObjectType: "api",
|
||||
ObjectID: 12345,
|
||||
Action: "call",
|
||||
CredentialType: "none",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.1",
|
||||
Success: false,
|
||||
ResultCode: "SEC_DIRECT_BYPASS",
|
||||
IdempotencyKey: "idem-key-004",
|
||||
}
|
||||
|
||||
// 由于是异步处理,这里返回202
|
||||
// 注意:在实际实现中,可能需要处理并发场景
|
||||
result, err := svc.CreateEvent(ctx, event)
|
||||
assert.NoError(t, err)
|
||||
// 同步处理场景下可能是201或202
|
||||
assert.True(t, result.StatusCode == 201 || result.StatusCode == 202)
|
||||
}
|
||||
|
||||
func TestAuditService_CreateEvent_WithoutIdempotencyKey(t *testing.T) {
|
||||
// 无幂等键时每次都创建新事件
|
||||
ctx := context.Background()
|
||||
svc := NewAuditService(NewInMemoryAuditStore())
|
||||
|
||||
event := &model.AuditEvent{
|
||||
EventName: "AUTH-TOKEN-OK",
|
||||
EventCategory: "AUTH",
|
||||
OperatorID: 1001,
|
||||
TenantID: 2001,
|
||||
ObjectType: "token",
|
||||
ObjectID: 12345,
|
||||
Action: "verify",
|
||||
CredentialType: "platform_token",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.1",
|
||||
Success: true,
|
||||
ResultCode: "AUTH_TOKEN_OK",
|
||||
// 无 IdempotencyKey
|
||||
}
|
||||
|
||||
result1, err1 := svc.CreateEvent(ctx, event)
|
||||
assert.NoError(t, err1)
|
||||
assert.Equal(t, 201, result1.StatusCode)
|
||||
|
||||
// 再次创建,由于没有幂等键,应该创建新事件
|
||||
// 注意:需要重置event.EventID,否则会认为是同一个事件
|
||||
event.EventID = ""
|
||||
result2, err2 := svc.CreateEvent(ctx, event)
|
||||
assert.NoError(t, err2)
|
||||
assert.Equal(t, 201, result2.StatusCode)
|
||||
assert.NotEqual(t, result1.EventID, result2.EventID)
|
||||
}
|
||||
|
||||
func TestAuditService_CreateEvent_InvalidInput(t *testing.T) {
|
||||
// 测试无效输入
|
||||
ctx := context.Background()
|
||||
svc := NewAuditService(NewInMemoryAuditStore())
|
||||
|
||||
// 空事件
|
||||
result, err := svc.CreateEvent(ctx, nil)
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, result)
|
||||
|
||||
// 缺少必填字段
|
||||
invalidEvent := &model.AuditEvent{
|
||||
EventName: "", // 缺少事件名
|
||||
}
|
||||
result, err = svc.CreateEvent(ctx, invalidEvent)
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, result)
|
||||
}
|
||||
|
||||
// ==================== 查询API测试 ====================
|
||||
|
||||
func TestAuditService_ListEvents_Pagination(t *testing.T) {
|
||||
// 分页测试
|
||||
ctx := context.Background()
|
||||
svc := NewAuditService(NewInMemoryAuditStore())
|
||||
|
||||
// 创建10个事件
|
||||
for i := 0; i < 10; i++ {
|
||||
event := &model.AuditEvent{
|
||||
EventName: "AUTH-TOKEN-OK",
|
||||
EventCategory: "AUTH",
|
||||
OperatorID: int64(1001 + i),
|
||||
TenantID: 2001,
|
||||
ObjectType: "token",
|
||||
ObjectID: int64(i),
|
||||
Action: "verify",
|
||||
CredentialType: "platform_token",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.1",
|
||||
Success: true,
|
||||
ResultCode: "AUTH_TOKEN_OK",
|
||||
}
|
||||
svc.CreateEvent(ctx, event)
|
||||
}
|
||||
|
||||
// 第一页
|
||||
events1, total1, err1 := svc.ListEvents(ctx, 2001, 0, 5)
|
||||
assert.NoError(t, err1)
|
||||
assert.Len(t, events1, 5)
|
||||
assert.Equal(t, int64(10), total1)
|
||||
|
||||
// 第二页
|
||||
events2, total2, err2 := svc.ListEvents(ctx, 2001, 5, 5)
|
||||
assert.NoError(t, err2)
|
||||
assert.Len(t, events2, 5)
|
||||
assert.Equal(t, int64(10), total2)
|
||||
}
|
||||
|
||||
func TestAuditService_ListEvents_FilterByCategory(t *testing.T) {
|
||||
// 按类别过滤
|
||||
ctx := context.Background()
|
||||
svc := NewAuditService(NewInMemoryAuditStore())
|
||||
|
||||
// 创建不同类别的事件
|
||||
categories := []string{"AUTH", "CRED", "DATA", "CONFIG"}
|
||||
for i, cat := range categories {
|
||||
event := &model.AuditEvent{
|
||||
EventName: cat + "-TEST",
|
||||
EventCategory: cat,
|
||||
OperatorID: 1001,
|
||||
TenantID: 2001,
|
||||
ObjectType: "test",
|
||||
ObjectID: int64(i),
|
||||
Action: "test",
|
||||
CredentialType: "platform_token",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.1",
|
||||
Success: true,
|
||||
ResultCode: "TEST_OK",
|
||||
}
|
||||
svc.CreateEvent(ctx, event)
|
||||
}
|
||||
|
||||
// 只查询AUTH类别
|
||||
filter := &EventFilter{
|
||||
TenantID: 2001,
|
||||
Category: "AUTH",
|
||||
}
|
||||
events, total, err := svc.ListEventsWithFilter(ctx, filter)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, events, 1)
|
||||
assert.Equal(t, int64(1), total)
|
||||
assert.Equal(t, "AUTH", events[0].EventCategory)
|
||||
}
|
||||
|
||||
func TestAuditService_ListEvents_FilterByTimeRange(t *testing.T) {
|
||||
// 按时间范围过滤
|
||||
ctx := context.Background()
|
||||
svc := NewAuditService(NewInMemoryAuditStore())
|
||||
|
||||
now := time.Now()
|
||||
event := &model.AuditEvent{
|
||||
EventName: "AUTH-TOKEN-OK",
|
||||
EventCategory: "AUTH",
|
||||
OperatorID: 1001,
|
||||
TenantID: 2001,
|
||||
ObjectType: "token",
|
||||
ObjectID: 12345,
|
||||
Action: "verify",
|
||||
CredentialType: "platform_token",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.1",
|
||||
Success: true,
|
||||
ResultCode: "AUTH_TOKEN_OK",
|
||||
}
|
||||
svc.CreateEvent(ctx, event)
|
||||
|
||||
// 在时间范围内
|
||||
filter := &EventFilter{
|
||||
TenantID: 2001,
|
||||
StartTime: now.Add(-1 * time.Hour),
|
||||
EndTime: now.Add(1 * time.Hour),
|
||||
}
|
||||
events, total, err := svc.ListEventsWithFilter(ctx, filter)
|
||||
assert.NoError(t, err)
|
||||
assert.GreaterOrEqual(t, len(events), 1)
|
||||
assert.GreaterOrEqual(t, total, int64(len(events)))
|
||||
|
||||
// 在时间范围外
|
||||
filter2 := &EventFilter{
|
||||
TenantID: 2001,
|
||||
StartTime: now.Add(1 * time.Hour),
|
||||
EndTime: now.Add(2 * time.Hour),
|
||||
}
|
||||
events2, total2, err2 := svc.ListEventsWithFilter(ctx, filter2)
|
||||
assert.NoError(t, err2)
|
||||
assert.Equal(t, 0, len(events2))
|
||||
assert.Equal(t, int64(0), total2)
|
||||
}
|
||||
|
||||
func TestAuditService_ListEvents_FilterByEventName(t *testing.T) {
|
||||
// 按事件名称过滤
|
||||
ctx := context.Background()
|
||||
svc := NewAuditService(NewInMemoryAuditStore())
|
||||
|
||||
event1 := &model.AuditEvent{
|
||||
EventName: "CRED-EXPOSE-RESPONSE",
|
||||
EventCategory: "CRED",
|
||||
OperatorID: 1001,
|
||||
TenantID: 2001,
|
||||
ObjectType: "account",
|
||||
ObjectID: 12345,
|
||||
Action: "create",
|
||||
CredentialType: "platform_token",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.1",
|
||||
Success: true,
|
||||
ResultCode: "SEC_CRED_EXPOSED",
|
||||
}
|
||||
event2 := &model.AuditEvent{
|
||||
EventName: "CRED-INGRESS-PLATFORM",
|
||||
EventCategory: "CRED",
|
||||
OperatorID: 1001,
|
||||
TenantID: 2001,
|
||||
ObjectType: "account",
|
||||
ObjectID: 12345,
|
||||
Action: "query",
|
||||
CredentialType: "platform_token",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.1",
|
||||
Success: true,
|
||||
ResultCode: "CRED_INGRESS_OK",
|
||||
}
|
||||
|
||||
svc.CreateEvent(ctx, event1)
|
||||
svc.CreateEvent(ctx, event2)
|
||||
|
||||
// 按事件名称过滤
|
||||
filter := &EventFilter{
|
||||
TenantID: 2001,
|
||||
EventName: "CRED-EXPOSE-RESPONSE",
|
||||
}
|
||||
events, total, err := svc.ListEventsWithFilter(ctx, filter)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, events, 1)
|
||||
assert.Equal(t, "CRED-EXPOSE-RESPONSE", events[0].EventName)
|
||||
assert.Equal(t, int64(1), total)
|
||||
}
|
||||
|
||||
// ==================== 辅助函数测试 ====================
|
||||
|
||||
func TestAuditService_HashIdempotencyKey(t *testing.T) {
|
||||
// 测试幂等键哈希
|
||||
svc := NewAuditService(NewInMemoryAuditStore())
|
||||
|
||||
key := "test-idempotency-key"
|
||||
hash1 := svc.HashIdempotencyKey(key)
|
||||
hash2 := svc.HashIdempotencyKey(key)
|
||||
|
||||
// 相同键应产生相同哈希
|
||||
assert.Equal(t, hash1, hash2)
|
||||
|
||||
// 不同键应产生不同哈希
|
||||
hash3 := svc.HashIdempotencyKey("different-key")
|
||||
assert.NotEqual(t, hash1, hash3)
|
||||
}
|
||||
312
supply-api/internal/audit/service/metrics_service.go
Normal file
312
supply-api/internal/audit/service/metrics_service.go
Normal file
@@ -0,0 +1,312 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"lijiaoqiao/supply-api/internal/audit/model"
|
||||
)
|
||||
|
||||
// Metric 指标结构
|
||||
type Metric struct {
|
||||
MetricID string `json:"metric_id"`
|
||||
MetricName string `json:"metric_name"`
|
||||
Period *MetricPeriod `json:"period"`
|
||||
Value float64 `json:"value"`
|
||||
Unit string `json:"unit"`
|
||||
Status string `json:"status"` // PASS/FAIL
|
||||
Details map[string]interface{} `json:"details"`
|
||||
}
|
||||
|
||||
// MetricPeriod 指标周期
|
||||
type MetricPeriod struct {
|
||||
Start time.Time `json:"start"`
|
||||
End time.Time `json:"end"`
|
||||
}
|
||||
|
||||
// MetricsService 指标服务
|
||||
type MetricsService struct {
|
||||
auditSvc *AuditService
|
||||
}
|
||||
|
||||
// NewMetricsService 创建指标服务
|
||||
func NewMetricsService(auditSvc *AuditService) *MetricsService {
|
||||
return &MetricsService{
|
||||
auditSvc: auditSvc,
|
||||
}
|
||||
}
|
||||
|
||||
// CalculateM013 计算M-013指标:凭证泄露事件数 = 0
|
||||
func (s *MetricsService) CalculateM013(ctx context.Context, start, end time.Time) (*Metric, error) {
|
||||
filter := &EventFilter{
|
||||
StartTime: start,
|
||||
EndTime: end,
|
||||
Limit: 10000,
|
||||
}
|
||||
|
||||
events, _, err := s.auditSvc.ListEventsWithFilter(ctx, filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 统计CRED-EXPOSE事件数
|
||||
exposureCount := 0
|
||||
unresolvedCount := 0
|
||||
for _, e := range events {
|
||||
if model.IsM013Event(e.EventName) {
|
||||
exposureCount++
|
||||
// 检查是否已解决(通过扩展字段或标记判断)
|
||||
if s.isEventUnresolved(e) {
|
||||
unresolvedCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
metric := &Metric{
|
||||
MetricID: "M-013",
|
||||
MetricName: "supplier_credential_exposure_events",
|
||||
Period: &MetricPeriod{
|
||||
Start: start,
|
||||
End: end,
|
||||
},
|
||||
Value: float64(exposureCount),
|
||||
Unit: "count",
|
||||
Status: "PASS",
|
||||
Details: map[string]interface{}{
|
||||
"total_exposure_events": exposureCount,
|
||||
"unresolved_events": unresolvedCount,
|
||||
},
|
||||
}
|
||||
|
||||
// 判断状态:M-013要求暴露事件数为0
|
||||
if exposureCount > 0 {
|
||||
metric.Status = "FAIL"
|
||||
}
|
||||
|
||||
return metric, nil
|
||||
}
|
||||
|
||||
// CalculateM014 计算M-014指标:平台凭证入站覆盖率 = 100%
|
||||
// 分母定义:经平台凭证校验的入站请求(credential_type = 'platform_token'),不含被拒绝的无效请求
|
||||
func (s *MetricsService) CalculateM014(ctx context.Context, start, end time.Time) (*Metric, error) {
|
||||
filter := &EventFilter{
|
||||
StartTime: start,
|
||||
EndTime: end,
|
||||
Limit: 10000,
|
||||
}
|
||||
|
||||
events, _, err := s.auditSvc.ListEventsWithFilter(ctx, filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 统计CRED-INGRESS-PLATFORM事件(只有这个才算入M-014)
|
||||
var platformCount, totalIngressCount int
|
||||
for _, e := range events {
|
||||
// M-014只统计CRED-INGRESS-PLATFORM事件
|
||||
if e.EventName == "CRED-INGRESS-PLATFORM" {
|
||||
totalIngressCount++
|
||||
// M-014分母:platform_token请求
|
||||
if e.CredentialType == model.CredentialTypePlatformToken {
|
||||
platformCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 计算覆盖率
|
||||
var coveragePct float64
|
||||
if totalIngressCount > 0 {
|
||||
coveragePct = float64(platformCount) / float64(totalIngressCount) * 100
|
||||
} else {
|
||||
coveragePct = 100.0 // 没有入站请求时,默认为100%
|
||||
}
|
||||
|
||||
metric := &Metric{
|
||||
MetricID: "M-014",
|
||||
MetricName: "platform_credential_ingress_coverage_pct",
|
||||
Period: &MetricPeriod{
|
||||
Start: start,
|
||||
End: end,
|
||||
},
|
||||
Value: coveragePct,
|
||||
Unit: "percentage",
|
||||
Status: "PASS",
|
||||
Details: map[string]interface{}{
|
||||
"platform_token_requests": platformCount,
|
||||
"total_requests": totalIngressCount,
|
||||
"non_compliant_requests": totalIngressCount - platformCount,
|
||||
},
|
||||
}
|
||||
|
||||
// 判断状态:M-014要求覆盖率为100%
|
||||
if coveragePct < 100.0 {
|
||||
metric.Status = "FAIL"
|
||||
}
|
||||
|
||||
return metric, nil
|
||||
}
|
||||
|
||||
// CalculateM015 计算M-015指标:直连绕过事件数 = 0
|
||||
func (s *MetricsService) CalculateM015(ctx context.Context, start, end time.Time) (*Metric, error) {
|
||||
filter := &EventFilter{
|
||||
StartTime: start,
|
||||
EndTime: end,
|
||||
Limit: 10000,
|
||||
}
|
||||
|
||||
events, _, err := s.auditSvc.ListEventsWithFilter(ctx, filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 统计CRED-DIRECT事件数
|
||||
directCallCount := 0
|
||||
blockedCount := 0
|
||||
for _, e := range events {
|
||||
if model.IsM015Event(e.EventName) {
|
||||
directCallCount++
|
||||
// 检查是否被阻断
|
||||
if s.isEventBlocked(e) {
|
||||
blockedCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
metric := &Metric{
|
||||
MetricID: "M-015",
|
||||
MetricName: "direct_supplier_call_by_consumer_events",
|
||||
Period: &MetricPeriod{
|
||||
Start: start,
|
||||
End: end,
|
||||
},
|
||||
Value: float64(directCallCount),
|
||||
Unit: "count",
|
||||
Status: "PASS",
|
||||
Details: map[string]interface{}{
|
||||
"total_direct_call_events": directCallCount,
|
||||
"blocked_events": blockedCount,
|
||||
},
|
||||
}
|
||||
|
||||
// 判断状态:M-015要求直连事件数为0
|
||||
if directCallCount > 0 {
|
||||
metric.Status = "FAIL"
|
||||
}
|
||||
|
||||
return metric, nil
|
||||
}
|
||||
|
||||
// CalculateM016 计算M-016指标:query key外部拒绝率 = 100%
|
||||
// 分母定义:检测到的所有query key请求,含被拒绝的请求
|
||||
func (s *MetricsService) CalculateM016(ctx context.Context, start, end time.Time) (*Metric, error) {
|
||||
filter := &EventFilter{
|
||||
StartTime: start,
|
||||
EndTime: end,
|
||||
Limit: 10000,
|
||||
}
|
||||
|
||||
events, _, err := s.auditSvc.ListEventsWithFilter(ctx, filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 统计AUTH-QUERY-*事件
|
||||
var totalQueryKey, rejectedCount int
|
||||
rejectBreakdown := make(map[string]int)
|
||||
for _, e := range events {
|
||||
if model.IsM016Event(e.EventName) {
|
||||
totalQueryKey++
|
||||
if e.EventName == "AUTH-QUERY-REJECT" {
|
||||
rejectedCount++
|
||||
rejectBreakdown[e.ResultCode]++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 计算拒绝率
|
||||
var rejectRate float64
|
||||
if totalQueryKey > 0 {
|
||||
rejectRate = float64(rejectedCount) / float64(totalQueryKey) * 100
|
||||
} else {
|
||||
rejectRate = 100.0 // 没有query key请求时,默认为100%
|
||||
}
|
||||
|
||||
metric := &Metric{
|
||||
MetricID: "M-016",
|
||||
MetricName: "query_key_external_reject_rate_pct",
|
||||
Period: &MetricPeriod{
|
||||
Start: start,
|
||||
End: end,
|
||||
},
|
||||
Value: rejectRate,
|
||||
Unit: "percentage",
|
||||
Status: "PASS",
|
||||
Details: map[string]interface{}{
|
||||
"rejected_requests": rejectedCount,
|
||||
"total_external_query_key_requests": totalQueryKey,
|
||||
"reject_breakdown": rejectBreakdown,
|
||||
},
|
||||
}
|
||||
|
||||
// 判断状态:M-016要求拒绝率为100%(所有外部query key请求都被拒绝)
|
||||
if rejectRate < 100.0 {
|
||||
metric.Status = "FAIL"
|
||||
}
|
||||
|
||||
return metric, nil
|
||||
}
|
||||
|
||||
// isEventUnresolved 检查事件是否未解决
|
||||
func (s *MetricsService) isEventUnresolved(e *model.AuditEvent) bool {
|
||||
// 如果事件成功,表示已处理/已解决
|
||||
// 如果事件失败,表示有问题/未解决
|
||||
return !e.Success
|
||||
}
|
||||
|
||||
// isEventBlocked 检查直连事件是否被阻断
|
||||
func (s *MetricsService) isEventBlocked(e *model.AuditEvent) bool {
|
||||
// 通过检查扩展字段或Success标志来判断是否被阻断
|
||||
if e.Success {
|
||||
return false // 成功表示未被阻断
|
||||
}
|
||||
|
||||
// 检查扩展字段中的blocked标记
|
||||
if e.Extensions != nil {
|
||||
if blocked, ok := e.Extensions["blocked"].(bool); ok {
|
||||
return blocked
|
||||
}
|
||||
}
|
||||
|
||||
// 通过结果码判断
|
||||
switch e.ResultCode {
|
||||
case "SEC_DIRECT_BYPASS", "SEC_DIRECT_BYPASS_BLOCKED":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// GetAllMetrics 获取所有M-013~M-016指标
|
||||
func (s *MetricsService) GetAllMetrics(ctx context.Context, start, end time.Time) ([]*Metric, error) {
|
||||
m013, err := s.CalculateM013(ctx, start, end)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m014, err := s.CalculateM014(ctx, start, end)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m015, err := s.CalculateM015(ctx, start, end)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m016, err := s.CalculateM016(ctx, start, end)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []*Metric{m013, m014, m015, m016}, nil
|
||||
}
|
||||
376
supply-api/internal/audit/service/metrics_service_test.go
Normal file
376
supply-api/internal/audit/service/metrics_service_test.go
Normal file
@@ -0,0 +1,376 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"lijiaoqiao/supply-api/internal/audit/model"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestAuditMetrics_M013_CredentialExposure(t *testing.T) {
|
||||
// M-013: supplier_credential_exposure_events = 0
|
||||
ctx := context.Background()
|
||||
svc := NewAuditService(NewInMemoryAuditStore())
|
||||
metricsSvc := NewMetricsService(svc)
|
||||
|
||||
// 创建一些事件,包括CRED-EXPOSE事件
|
||||
events := []*model.AuditEvent{
|
||||
{
|
||||
EventName: "CRED-EXPOSE-RESPONSE",
|
||||
EventCategory: "CRED",
|
||||
OperatorID: 1001,
|
||||
TenantID: 2001,
|
||||
ObjectType: "account",
|
||||
ObjectID: 12345,
|
||||
Action: "create",
|
||||
CredentialType: "platform_token",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.1",
|
||||
Success: true,
|
||||
ResultCode: "SEC_CRED_EXPOSED",
|
||||
},
|
||||
{
|
||||
EventName: "AUTH-TOKEN-OK",
|
||||
EventCategory: "AUTH",
|
||||
OperatorID: 1001,
|
||||
TenantID: 2001,
|
||||
ObjectType: "token",
|
||||
ObjectID: 12345,
|
||||
Action: "verify",
|
||||
CredentialType: "platform_token",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.1",
|
||||
Success: true,
|
||||
ResultCode: "AUTH_TOKEN_OK",
|
||||
},
|
||||
}
|
||||
|
||||
for _, e := range events {
|
||||
svc.CreateEvent(ctx, e)
|
||||
}
|
||||
|
||||
// 计算M-013指标
|
||||
now := time.Now()
|
||||
metric, err := metricsSvc.CalculateM013(ctx, now.Add(-24*time.Hour), now)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, metric)
|
||||
assert.Equal(t, "M-013", metric.MetricID)
|
||||
assert.Equal(t, "supplier_credential_exposure_events", metric.MetricName)
|
||||
assert.Equal(t, float64(1), metric.Value) // 有1个暴露事件
|
||||
assert.Equal(t, "FAIL", metric.Status) // 暴露事件数 > 0,应该是FAIL
|
||||
}
|
||||
|
||||
func TestAuditMetrics_M014_IngressCoverage(t *testing.T) {
|
||||
// M-014: platform_credential_ingress_coverage_pct = 100%
|
||||
// 分母定义:经平台凭证校验的入站请求(credential_type = 'platform_token'),不含被拒绝的无效请求
|
||||
ctx := context.Background()
|
||||
svc := NewAuditService(NewInMemoryAuditStore())
|
||||
metricsSvc := NewMetricsService(svc)
|
||||
|
||||
// 创建入站凭证事件
|
||||
events := []*model.AuditEvent{
|
||||
// 合规的platform_token请求
|
||||
{
|
||||
EventName: "CRED-INGRESS-PLATFORM",
|
||||
EventCategory: "CRED",
|
||||
EventSubCategory: "INGRESS",
|
||||
OperatorID: 1001,
|
||||
TenantID: 2001,
|
||||
ObjectType: "account",
|
||||
ObjectID: 12345,
|
||||
Action: "query",
|
||||
CredentialType: "platform_token",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.1",
|
||||
Success: true,
|
||||
ResultCode: "CRED_INGRESS_OK",
|
||||
},
|
||||
{
|
||||
EventName: "CRED-INGRESS-PLATFORM",
|
||||
EventCategory: "CRED",
|
||||
EventSubCategory: "INGRESS",
|
||||
OperatorID: 1002,
|
||||
TenantID: 2001,
|
||||
ObjectType: "account",
|
||||
ObjectID: 12346,
|
||||
Action: "query",
|
||||
CredentialType: "platform_token",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.2",
|
||||
Success: true,
|
||||
ResultCode: "CRED_INGRESS_OK",
|
||||
},
|
||||
// 非合规的query_key请求 - 不应该计入M-014的分母
|
||||
{
|
||||
EventName: "CRED-INGRESS-SUPPLIER",
|
||||
EventCategory: "CRED",
|
||||
EventSubCategory: "INGRESS",
|
||||
OperatorID: 1003,
|
||||
TenantID: 2001,
|
||||
ObjectType: "account",
|
||||
ObjectID: 12347,
|
||||
Action: "query",
|
||||
CredentialType: "query_key",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.3",
|
||||
Success: false,
|
||||
ResultCode: "AUTH_QUERY_REJECT",
|
||||
},
|
||||
}
|
||||
|
||||
for _, e := range events {
|
||||
svc.CreateEvent(ctx, e)
|
||||
}
|
||||
|
||||
// 计算M-014指标
|
||||
now := time.Now()
|
||||
metric, err := metricsSvc.CalculateM014(ctx, now.Add(-24*time.Hour), now)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, metric)
|
||||
assert.Equal(t, "M-014", metric.MetricID)
|
||||
assert.Equal(t, "platform_credential_ingress_coverage_pct", metric.MetricName)
|
||||
// 2个platform_token / 2个总入站请求 = 100%
|
||||
assert.Equal(t, 100.0, metric.Value)
|
||||
assert.Equal(t, "PASS", metric.Status)
|
||||
}
|
||||
|
||||
func TestAuditMetrics_M015_DirectCall(t *testing.T) {
|
||||
// M-015: direct_supplier_call_by_consumer_events = 0
|
||||
ctx := context.Background()
|
||||
svc := NewAuditService(NewInMemoryAuditStore())
|
||||
metricsSvc := NewMetricsService(svc)
|
||||
|
||||
// 创建直连事件
|
||||
events := []*model.AuditEvent{
|
||||
{
|
||||
EventName: "CRED-DIRECT-SUPPLIER",
|
||||
EventCategory: "CRED",
|
||||
EventSubCategory: "DIRECT",
|
||||
OperatorID: 1001,
|
||||
TenantID: 2001,
|
||||
ObjectType: "api",
|
||||
ObjectID: 12345,
|
||||
Action: "call",
|
||||
CredentialType: "none",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.1",
|
||||
Success: false,
|
||||
ResultCode: "SEC_DIRECT_BYPASS",
|
||||
TargetDirect: true,
|
||||
},
|
||||
{
|
||||
EventName: "AUTH-TOKEN-OK",
|
||||
EventCategory: "AUTH",
|
||||
OperatorID: 1001,
|
||||
TenantID: 2001,
|
||||
ObjectType: "token",
|
||||
ObjectID: 12345,
|
||||
Action: "verify",
|
||||
CredentialType: "platform_token",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.1",
|
||||
Success: true,
|
||||
ResultCode: "AUTH_TOKEN_OK",
|
||||
},
|
||||
}
|
||||
|
||||
for _, e := range events {
|
||||
svc.CreateEvent(ctx, e)
|
||||
}
|
||||
|
||||
// 计算M-015指标
|
||||
now := time.Now()
|
||||
metric, err := metricsSvc.CalculateM015(ctx, now.Add(-24*time.Hour), now)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, metric)
|
||||
assert.Equal(t, "M-015", metric.MetricID)
|
||||
assert.Equal(t, "direct_supplier_call_by_consumer_events", metric.MetricName)
|
||||
assert.Equal(t, float64(1), metric.Value) // 有1个直连事件
|
||||
assert.Equal(t, "FAIL", metric.Status) // 直连事件数 > 0,应该是FAIL
|
||||
}
|
||||
|
||||
func TestAuditMetrics_M016_QueryKeyRejectRate(t *testing.T) {
|
||||
// M-016: query_key_external_reject_rate_pct = 100%
|
||||
// 分母:所有query key请求(不含被拒绝的无效请求)
|
||||
ctx := context.Background()
|
||||
svc := NewAuditService(NewInMemoryAuditStore())
|
||||
metricsSvc := NewMetricsService(svc)
|
||||
|
||||
// 创建query key事件
|
||||
events := []*model.AuditEvent{
|
||||
// 被拒绝的query key请求
|
||||
{
|
||||
EventName: "AUTH-QUERY-REJECT",
|
||||
EventCategory: "AUTH",
|
||||
OperatorID: 1001,
|
||||
TenantID: 2001,
|
||||
ObjectType: "query_key",
|
||||
ObjectID: 12345,
|
||||
Action: "query",
|
||||
CredentialType: "query_key",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.1",
|
||||
Success: false,
|
||||
ResultCode: "QUERY_KEY_NOT_ALLOWED",
|
||||
},
|
||||
{
|
||||
EventName: "AUTH-QUERY-REJECT",
|
||||
EventCategory: "AUTH",
|
||||
OperatorID: 1002,
|
||||
TenantID: 2001,
|
||||
ObjectType: "query_key",
|
||||
ObjectID: 12346,
|
||||
Action: "query",
|
||||
CredentialType: "query_key",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.2",
|
||||
Success: false,
|
||||
ResultCode: "QUERY_KEY_EXPIRED",
|
||||
},
|
||||
// query key请求
|
||||
{
|
||||
EventName: "AUTH-QUERY-KEY",
|
||||
EventCategory: "AUTH",
|
||||
OperatorID: 1003,
|
||||
TenantID: 2001,
|
||||
ObjectType: "query_key",
|
||||
ObjectID: 12347,
|
||||
Action: "query",
|
||||
CredentialType: "query_key",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.3",
|
||||
Success: false,
|
||||
ResultCode: "QUERY_KEY_EXPIRED",
|
||||
},
|
||||
// 非query key事件
|
||||
{
|
||||
EventName: "AUTH-TOKEN-OK",
|
||||
EventCategory: "AUTH",
|
||||
OperatorID: 1001,
|
||||
TenantID: 2001,
|
||||
ObjectType: "token",
|
||||
ObjectID: 12345,
|
||||
Action: "verify",
|
||||
CredentialType: "platform_token",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.1",
|
||||
Success: true,
|
||||
ResultCode: "AUTH_TOKEN_OK",
|
||||
},
|
||||
}
|
||||
|
||||
for _, e := range events {
|
||||
svc.CreateEvent(ctx, e)
|
||||
}
|
||||
|
||||
// 计算M-016指标
|
||||
now := time.Now()
|
||||
metric, err := metricsSvc.CalculateM016(ctx, now.Add(-24*time.Hour), now)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, metric)
|
||||
assert.Equal(t, "M-016", metric.MetricID)
|
||||
assert.Equal(t, "query_key_external_reject_rate_pct", metric.MetricName)
|
||||
// 2个拒绝 / 3个query key总请求 = 66.67%
|
||||
assert.InDelta(t, 66.67, metric.Value, 0.01)
|
||||
assert.Equal(t, "FAIL", metric.Status) // 拒绝率 < 100%,应该是FAIL
|
||||
}
|
||||
|
||||
func TestAuditMetrics_M016_DifferentFromM014(t *testing.T) {
|
||||
// M-014与M-016边界清晰:分母不同,无重叠
|
||||
// M-014 分母:经平台凭证校验的入站请求(platform_token)
|
||||
// M-016 分母:检测到的所有query key请求
|
||||
|
||||
ctx := context.Background()
|
||||
svc := NewAuditService(NewInMemoryAuditStore())
|
||||
metricsSvc := NewMetricsService(svc)
|
||||
|
||||
// 场景:100个请求,80个使用platform_token,20个使用query key(被拒绝)
|
||||
// M-014 = 80/80 = 100%(分母只计算platform_token请求)
|
||||
// M-016 = 20/20 = 100%(分母计算所有query key请求)
|
||||
|
||||
// 创建80个platform_token请求
|
||||
for i := 0; i < 80; i++ {
|
||||
svc.CreateEvent(ctx, &model.AuditEvent{
|
||||
EventName: "CRED-INGRESS-PLATFORM",
|
||||
EventCategory: "CRED",
|
||||
EventSubCategory: "INGRESS",
|
||||
OperatorID: int64(1000 + i),
|
||||
TenantID: 2001,
|
||||
ObjectType: "account",
|
||||
ObjectID: int64(i),
|
||||
Action: "query",
|
||||
CredentialType: "platform_token",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.1",
|
||||
Success: true,
|
||||
ResultCode: "CRED_INGRESS_OK",
|
||||
})
|
||||
}
|
||||
|
||||
// 创建20个query key请求(全部被拒绝)
|
||||
for i := 0; i < 20; i++ {
|
||||
svc.CreateEvent(ctx, &model.AuditEvent{
|
||||
EventName: "AUTH-QUERY-REJECT",
|
||||
EventCategory: "AUTH",
|
||||
OperatorID: int64(2000 + i),
|
||||
TenantID: 2001,
|
||||
ObjectType: "query_key",
|
||||
ObjectID: int64(1000 + i),
|
||||
Action: "query",
|
||||
CredentialType: "query_key",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.1",
|
||||
Success: false,
|
||||
ResultCode: "QUERY_KEY_NOT_ALLOWED",
|
||||
})
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
|
||||
// 计算M-014
|
||||
m014, err := metricsSvc.CalculateM014(ctx, now.Add(-24*time.Hour), now)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 100.0, m014.Value) // 80/80 = 100%
|
||||
|
||||
// 计算M-016
|
||||
m016, err := metricsSvc.CalculateM016(ctx, now.Add(-24*time.Hour), now)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 100.0, m016.Value) // 20/20 = 100%
|
||||
}
|
||||
|
||||
func TestAuditMetrics_M013_ZeroExposure(t *testing.T) {
|
||||
// M-013: 当没有凭证暴露事件时,应该为0,状态PASS
|
||||
ctx := context.Background()
|
||||
svc := NewAuditService(NewInMemoryAuditStore())
|
||||
metricsSvc := NewMetricsService(svc)
|
||||
|
||||
// 创建一些正常事件,没有CRED-EXPOSE
|
||||
svc.CreateEvent(ctx, &model.AuditEvent{
|
||||
EventName: "AUTH-TOKEN-OK",
|
||||
EventCategory: "AUTH",
|
||||
OperatorID: 1001,
|
||||
TenantID: 2001,
|
||||
ObjectType: "token",
|
||||
ObjectID: 12345,
|
||||
Action: "verify",
|
||||
CredentialType: "platform_token",
|
||||
SourceType: "api",
|
||||
SourceIP: "192.168.1.1",
|
||||
Success: true,
|
||||
ResultCode: "AUTH_TOKEN_OK",
|
||||
})
|
||||
|
||||
now := time.Now()
|
||||
metric, err := metricsSvc.CalculateM013(ctx, now.Add(-24*time.Hour), now)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, float64(0), metric.Value)
|
||||
assert.Equal(t, "PASS", metric.Status)
|
||||
}
|
||||
Reference in New Issue
Block a user