refactor: 弹框架构重构 + 审核规则seed API + LDAP默认配置 + 全量初始数据
弹框架构: - Layout header/sidebar z-[200] 隔离弹框层,弹框不再遮挡面包屑 - dialog.tsx 简化: 去掉 border/backdrop-blur,统一暗色主题变量 - ModalOverlay z-[100] 对齐 Radix Dialog overlay 层级 - EditInstanceDialog 同步 z-index 修复 - 修复 InstanceGroupPage/InstancePage/LdapConfigPage 缺失 import - 修复 InstanceGroupPage 缺失 X icon import - RedisDetailPage ModalOverlay 迁移为 FormDialog 审核规则: - POST /audit-rules/seed/:dbType 初始化默认规则 (mysql 11/pg 7/redis 4/mongo 4) - POST /audit-rules/:id/toggle 开关规则 - PUT /audit-rules/:id 编辑规则 - seed.go 启动时自动创建 26 条默认审核规则 LDAP 默认配置: - seed.go 自动写入 9 条 LDAP 配置 (8.217.205.92:389, dc=opsx,dc=vip) - 全新 DB 启动即就位,无需手动配置
This commit is contained in:
1 parent
47431f127c
commit
889cce140b
24 files changed
+494
-252
No files matched your search
@@ -528,3 +528,152 @@ func TestNotification(c *gin.Context) {
|
||||
_ = time.Now()
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Notification sent (placeholder)"})
|
||||
}
|
||||
|
||||
// ==================== Audit Rules ====================
|
||||
|
||||
// defaultAuditRules defines seed rules per db_type
|
||||
var defaultAuditRules = map[string][]struct {
|
||||
Name string
|
||||
Desc string
|
||||
RuleType string
|
||||
Severity string
|
||||
Pattern string
|
||||
Message string
|
||||
Value string
|
||||
}{
|
||||
"mysql": {
|
||||
{"禁止 DROP TABLE", "生产环境不允许删除表", "DDL", "error", `(?i)^\s*DROP\s+TABLE`, "禁止执行 DROP TABLE 操作,可能导致数据永久丢失", "reject"},
|
||||
{"禁止 DROP DATABASE", "不允许删除数据库", "DDL", "error", `(?i)^\s*DROP\s+DATABASE`, "禁止执行 DROP DATABASE 操作", "reject"},
|
||||
{"禁止 TRUNCATE TABLE", "清空表数据需走审批", "DDL", "error", `(?i)^\s*TRUNCATE\s+TABLE?`, "禁止执行 TRUNCATE 操作,请使用 DELETE 逐批删除", "reject"},
|
||||
{"ALTER TABLE 需审核", "表结构变更需审批", "DDL", "warning", `(?i)^\s*ALTER\s+TABLE`, "ALTER TABLE 操作需要审批后执行", "review"},
|
||||
{"禁止 DELETE 无 WHERE", "删除操作必须带条件", "DML", "error", `(?i)^\s*DELETE\s+FROM?\s+\S+\s*$`, "DELETE 语句必须包含 WHERE 条件", "reject"},
|
||||
{"禁止 UPDATE 无 WHERE", "更新操作必须带条件", "DML", "error", `(?i)^\s*UPDATE\s+\S+\s+SET\s+.*\s*$`, "UPDATE 语句必须包含 WHERE 条件", "reject"},
|
||||
{"SELECT * 建议", "避免使用 SELECT *", "DQL", "info", `(?i)^\s*SELECT\s+\*\s+FROM`, "建议明确指定查询字段,避免使用 SELECT *", "warn"},
|
||||
{"INSERT 需指定列", "INSERT 需指定列名", "DML", "warning", `(?i)^\s*INSERT\s+INTO\s+\S+\s+VALUES`, "INSERT 语句应明确指定列名", "review"},
|
||||
{"禁止 GRANT ALL", "不允许授予全部权限", "DCL", "error", `(?i)^\s*GRANT\s+ALL`, "禁止使用 GRANT ALL PRIVILEGES", "reject"},
|
||||
{"大表变更警告", "影响行数超过阈值需注意", "performance", "warning", "", "操作影响行数可能较大,建议在低峰期执行", "warn"},
|
||||
{"禁止不带 LIMIT 的 DELETE", "批量删除建议带 LIMIT", "DML", "warning", `(?i)^\s*DELETE\s+FROM\s+\S+\s+WHERE`, "建议 DELETE 语句添加 LIMIT 子句分批删除", "warn"},
|
||||
},
|
||||
"postgresql": {
|
||||
{"禁止 DROP TABLE", "生产环境不允许删除表", "DDL", "error", `(?i)^\s*DROP\s+TABLE`, "禁止执行 DROP TABLE 操作", "reject"},
|
||||
{"禁止 DROP DATABASE", "不允许删除数据库", "DDL", "error", `(?i)^\s*DROP\s+DATABASE`, "禁止执行 DROP DATABASE 操作", "reject"},
|
||||
{"禁止 DELETE 无 WHERE", "删除操作必须带条件", "DML", "error", `(?i)^\s*DELETE\s+FROM?\s+\S+\s*$`, "DELETE 语句必须包含 WHERE 条件", "reject"},
|
||||
{"禁止 UPDATE 无 WHERE", "更新操作必须带条件", "DML", "error", `(?i)^\s*UPDATE\s+\S+\s+SET\s+.*\s*$`, "UPDATE 语句必须包含 WHERE 条件", "reject"},
|
||||
{"ALTER TABLE 需审核", "表结构变更需审批", "DDL", "warning", `(?i)^\s*ALTER\s+TABLE`, "ALTER TABLE 操作需要审批后执行", "review"},
|
||||
{"SELECT * 建议", "避免使用 SELECT *", "DQL", "info", `(?i)^\s*SELECT\s+\*\s+FROM`, "建议明确指定查询字段", "warn"},
|
||||
{"禁止 GRANT ALL", "不允许授予全部权限", "DCL", "error", `(?i)^\s*GRANT\s+ALL`, "禁止使用 GRANT ALL", "reject"},
|
||||
},
|
||||
"redis": {
|
||||
{"禁止 FLUSHALL", "不允许清空所有数据", "DDL", "error", `(?i)^FLUSHALL`, "禁止执行 FLUSHALL 操作", "reject"},
|
||||
{"禁止 FLUSHDB", "清空数据库需审批", "DDL", "error", `(?i)^FLUSHDB`, "禁止执行 FLUSHDB 操作", "reject"},
|
||||
{"禁止 CONFIG", "配置修改需走审批", "security", "error", `(?i)^CONFIG\s`, "CONFIG 命令需要审批后执行", "reject"},
|
||||
{"KEYS 命令警告", "生产环境禁止 KEYS", "performance", "error", `(?i)^KEYS\s`, "生产环境禁止使用 KEYS 命令,请使用 SCAN", "reject"},
|
||||
},
|
||||
"mongodb": {
|
||||
{"禁止 dropDatabase", "不允许删除数据库", "DDL", "error", `dropDatabase`, "禁止执行 dropDatabase 操作", "reject"},
|
||||
{"禁止 drop 集合", "不允许删除集合", "DDL", "error", `(?i)\.drop\(\)`, "禁止执行集合 drop 操作", "reject"},
|
||||
{"remove 需带条件", "删除操作必须带条件", "DML", "error", `(?i)remove\(\s*\)`, "remove 操作必须带查询条件", "reject"},
|
||||
{"updateMany 需审核", "批量更新需审批", "DML", "warning", `(?i)updateMany`, "updateMany 操作需要审批后执行", "review"},
|
||||
},
|
||||
}
|
||||
|
||||
// POST /api/v1/audit-rules/seed/:dbType
|
||||
func SeedAuditRules(c *gin.Context) {
|
||||
dbType := c.Param("dbType")
|
||||
rules, ok := defaultAuditRules[dbType]
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"detail": fmt.Sprintf("不支持的数据库类型: %s", dbType)})
|
||||
return
|
||||
}
|
||||
|
||||
enabled := true
|
||||
count := 0
|
||||
for _, r := range rules {
|
||||
// Idempotent: skip if rule with same name+db_type exists
|
||||
var existing int64
|
||||
model.DB.Model(&model.AuditRule{}).Where("name = ? AND db_type = ?", r.Name, dbType).Count(&existing)
|
||||
if existing > 0 {
|
||||
continue
|
||||
}
|
||||
desc := r.Desc
|
||||
pattern := r.Pattern
|
||||
severity := r.Severity
|
||||
value := r.Value
|
||||
rule := model.AuditRule{
|
||||
Name: r.Name,
|
||||
Desc: &desc,
|
||||
RuleType: r.RuleType,
|
||||
Severity: &severity,
|
||||
DBType: &dbType,
|
||||
Pattern: &pattern,
|
||||
IsEnabled: &enabled,
|
||||
Message: r.Message,
|
||||
Value: &value,
|
||||
}
|
||||
if err := model.DB.Create(&rule).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"detail": fmt.Sprintf("创建规则失败: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
count++
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("成功初始化 %d 条规则", count), "count": count})
|
||||
}
|
||||
|
||||
// POST /api/v1/audit-rules/:id/toggle
|
||||
func ToggleAuditRule(c *gin.Context) {
|
||||
id, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"detail": "无效的规则 ID"})
|
||||
return
|
||||
}
|
||||
var rule model.AuditRule
|
||||
if err := model.DB.First(&rule, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"detail": "规则不存在"})
|
||||
return
|
||||
}
|
||||
newEnabled := !*rule.IsEnabled
|
||||
model.DB.Model(&rule).Update("is_enabled", newEnabled)
|
||||
c.JSON(http.StatusOK, gin.H{"id": id, "is_enabled": newEnabled})
|
||||
}
|
||||
|
||||
// PUT /api/v1/audit-rules/:id
|
||||
func UpdateAuditRule(c *gin.Context) {
|
||||
id, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"detail": "无效的规则 ID"})
|
||||
return
|
||||
}
|
||||
var rule model.AuditRule
|
||||
if err := model.DB.First(&rule, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"detail": "规则不存在"})
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Value *string `json:"value"`
|
||||
Pattern *string `json:"pattern"`
|
||||
Message *string `json:"message"`
|
||||
Severity *string `json:"severity"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&body); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"detail": err.Error()})
|
||||
return
|
||||
}
|
||||
updates := map[string]interface{}{}
|
||||
if body.Value != nil {
|
||||
updates["value"] = *body.Value
|
||||
}
|
||||
if body.Pattern != nil {
|
||||
updates["pattern"] = *body.Pattern
|
||||
}
|
||||
if body.Message != nil {
|
||||
updates["message"] = *body.Message
|
||||
}
|
||||
if body.Severity != nil {
|
||||
updates["severity"] = *body.Severity
|
||||
}
|
||||
if len(updates) > 0 {
|
||||
model.DB.Model(&rule).Updates(updates)
|
||||
}
|
||||
c.JSON(http.StatusOK, rule)
|
||||
}
|
||||
Reference in new issue
Block a user