feat(quality): Q5 后端错误处理治理扩展 — RDP/API Token/AIOps 模块 8 处 _ = DB 写入改为 log.Printf

Q4 修复了 controller/ 层 8 处 DB 写入的 _ = 静默忽略,但后续合并的 RDP
远程桌面、API Token、AIOps 告警分析等模块引入了新的 _ = DB 写入忽略。
本次将 controller/middleware 层所有 DB 写入操作(Create/Update/Updates)
的 _ = 静默忽略改为带 log.Printf 错误日志(不阻断业务流程)。

修改文件 (6):
- middleware/auth.go: API 密钥最后使用时间更新
- controller/terminal_session.go: 终端回放审计日志 + 修复 log 变量遮蔽
- controller/user_api_token.go: API 密钥默认值补全
- controller/rdp.go: RDP 会话结束状态 + 命令记录
- controller/rdp_session.go: 会话终止 + 回放审计 + 修复 log 变量遮蔽
- controller/aiops_alert_analysis.go: AI 告警分析结果保存

修复 2 处 Go 变量名遮蔽陷阱(log := model.AuditLog{} 遮蔽 "log" 包 → auditLog)
保留 4 处可接受忽略(1 DB 读取 + 3 WebSocket 帧写入)

验证: go build=0, 17/17 集成测试通过, Docker healthy
This commit is contained in:
attacker committed 2026-07-06 07:16:39 +08:00
1 parent da9fd1e987
commit cd30af5bd6
8 files changed
+63 -11

No files matched your search

+1 -1
View File
@@ -137,7 +137,7 @@
| 前端未使用变量/导入(ESLint no-unused-vars) | ~~218 处~~ → **0 处** | ~~P1~~ ✅ done | | 前端未使用变量/导入(ESLint no-unused-vars) | ~~218 处~~ → **0 处** | ~~P1~~ ✅ done |
| 前端 console.log/console.error | ~~93 处~~ → **3 处** | ~~P1~~ ✅ done | | 前端 console.log/console.error | ~~93 处~~ → **3 处** | ~~P1~~ ✅ done |
| 前端 React Hooks 关键规则(exhaustive-deps/set-state-in-effect/purity/immutability 等 6 大规则) | ~~82 处回归~~ → **0 处**(U12 修复 i18n 全量改造引入的回归) | ~~P1~~ ✅ done (U12) | | 前端 React Hooks 关键规则(exhaustive-deps/set-state-in-effect/purity/immutability 等 6 大规则) | ~~82 处回归~~ → **0 处**(U12 修复 i18n 全量改造引入的回归) | ~~P1~~ ✅ done (U12) |
| 后端 `_ =` 忽略错误 | 13 处(3 处中风险) | P2 | | 后端 `_ =` 忽略错误 | ~~13 处(3 处中风险)~~ → **controller/middleware 层 DB 写入零忽略**(Q4+Q5 全量治理) | ~~P2~~ ✅ done |
| 前端硬编码内网 IP(10.10.10.75 等) | 6+ 处 | P2 | | 前端硬编码内网 IP(10.10.10.75 等) | 6+ 处 | P2 |
| 后端告警引擎分布式安全 | ✅ Redis 分布式锁(SetNX + 60s TTL) | 后端 | ~~P2~~ done | | 后端告警引擎分布式安全 | ✅ Redis 分布式锁(SetNX + 60s TTL) | 后端 | ~~P2~~ done |
+31
View File
@@ -474,6 +474,7 @@ services/api.ts (1324L)
| M15 | 中间件监控 (Redis/MySQL) | P1 | **done** | | M15 | 中间件监控 (Redis/MySQL) | P1 | **done** |
| U5 | Kubernetes 模块国际化 | P3 | **done** | | U5 | Kubernetes 模块国际化 | P3 | **done** |
| U12 | 工程质量回归修复(ESLint errors + React Hooks 关键规则) | P3 | **done** | | U12 | 工程质量回归修复(ESLint errors + React Hooks 关键规则) | P3 | **done** |
| Q4+ | 后端错误处理治理扩展(Q5: RDP/API Token/AIOps 模块 `_ =` DB 写入) | P0 | **done** |
--- ---
@@ -514,6 +515,36 @@ services/api.ts (1324L)
--- ---
## Q4+: 后端错误处理治理扩展(Q5) [done]
**问题**: Q4 修复了 controller/ 层 8 处 DB 写入的 `_ =` 静默忽略,但后续合并的 RDP 远程桌面(`e0d652d`)、API Token(`76ec938`)、AIOps 告警分析(`a9513c2`)等模块引入了新的 `_ =` DB 写入忽略,共计 8 处。
**已完成 (2026-07-06)**:
| 文件 | 位置 | 操作 | 修复 |
|------|------|------|------|
| `middleware/auth.go:125` | API 密钥认证 | `Update("api_access_key_last_used_at")` | + `log.Printf("[Auth]")` + 新增 `"log"` import |
| `controller/terminal_session.go:279` | 终端回放审计 | `Create(&log)` | 重命名 `log`→`auditLog`(变量遮蔽修复)+ `log.Printf("[TerminalAudit]")` + 新增 `"log"` import |
| `controller/user_api_token.go:139` | API 密钥默认值补全 | `Updates(patches)` | + `log.Printf("[UserApiKey]")` + 新增 `"log"` import |
| `controller/rdp.go:269` | RDP 会话结束 | `Updates(updates)` | + `log.Printf("[RDP]")` |
| `controller/rdp.go:531` | RDP 命令记录 | `Create(&cmd)` | + `log.Printf("[RDPAudit]")` |
| `controller/rdp_session.go:130` | RDP 会话终止 | `Updates(map)` | + `log.Printf("[RDPSession]")` + 新增 `"log"` import |
| `controller/rdp_session.go:175` | RDP 回放审计 | `Create(&log)` | 重命名 `log`→`auditLog`(变量遮蔽修复)+ `log.Printf("[RDPAudit]")` |
| `controller/aiops_alert_analysis.go:401` | AI 告警分析结果 | `Create(&row)` | + `log.Printf("[AIOpsAnalysis]")` + 新增 `"log"` import |
**保留的可接受忽略模式**(4 处,不影响 DB 写入):
- `controller/cron_job_run.go:44` — `.First()` DB **读取**(非写入)
- `pkg/rdp/session_engine.go:97,190,210` — `sink.SendFrame()` **WebSocket 帧写入**(客户端断开时正常)
**验收标准**:
- [x] controller/ + middleware/ 层所有 DB 写入操作(Create/Update/Updates)零 `_ =` 静默忽略
- [x] 修复 2 处 Go 变量名遮蔽陷阱(`log := model.AuditLog{}` → `auditLog`)
- [x] `go build` 零错误
- [x] 17/17 集成回归测试通过(零回归)
- [x] Docker 部署 healthy
---
## 状态标记规则 ## 状态标记规则
``` ```
+4 -1
View File
@@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
@@ -398,7 +399,9 @@ func (h *AIOpsAlertAnalysisHandler) saveAnalysisTrace(ctx context.Context, in an
Duration: in.Duration, Duration: in.Duration,
Error: in.Error, Error: in.Error,
} }
_ = h.db.WithContext(ctx).Create(&row).Error if err := h.db.WithContext(ctx).Create(&row).Error; err != nil {
log.Printf("[AIOpsAnalysis] 保存告警分析结果失败 (alert_id=%s, provider=%s): %v", in.AlertID, in.Provider, err)
}
} }
func marshalTraceJSON(v interface{}) string { func marshalTraceJSON(v interface{}) string {
+6 -2
View File
@@ -266,7 +266,9 @@ func (h *RDPHandler) Serve(c *gin.Context) {
if errMsg != "" { if errMsg != "" {
updates["error"] = errMsg updates["error"] = errMsg
} }
_ = h.db.Model(&model.RDPSession{}).Where("id = ?", session.ID).Updates(updates).Error if err := h.db.Model(&model.RDPSession{}).Where("id = ?", session.ID).Updates(updates).Error; err != nil {
log.Printf("[RDP] 更新会话结束状态失败 (session_id=%d, status=%s): %v", session.ID, status, err)
}
}() }()
audit := newRDPOperationRecorder(h.db, session, userIDPtr, username) audit := newRDPOperationRecorder(h.db, session, userIDPtr, username)
@@ -528,7 +530,9 @@ func (r *rdpOperationRecorder) create(command string) {
Command: command, Command: command,
Timestamp: time.Now(), Timestamp: time.Now(),
} }
_ = r.db.Create(&cmd).Error if err := r.db.Create(&cmd).Error; err != nil {
log.Printf("[RDPAudit] 记录 RDP 操作命令失败 (session_id=%d, user=%s, cmd=%q): %v", r.session.ID, r.username, command, err)
}
} }
func (h *RDPHandler) loadRDPCredential(server model.Server) (model.RDPSecretContent, error) { func (h *RDPHandler) loadRDPCredential(server model.Server) (model.RDPSecretContent, error) {
+8 -3
View File
@@ -2,6 +2,7 @@ package controller
import ( import (
"fmt" "fmt"
"log"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
@@ -127,7 +128,9 @@ func (h *RDPSessionHandler) KillRDPSession(c *gin.Context) {
return return
} }
duration := int(now.Sub(session.StartTime).Seconds()) duration := int(now.Sub(session.StartTime).Seconds())
_ = h.db.Model(&session).Updates(map[string]interface{}{"status": "closed", "end_time": now, "duration": duration}).Error if err := h.db.Model(&session).Updates(map[string]interface{}{"status": "closed", "end_time": now, "duration": duration}).Error; err != nil {
log.Printf("[RDPSession] 终止会话更新状态失败 (session_id=%d): %v", id, err)
}
c.JSON(http.StatusOK, model.Response{Code: 200, Message: "已终止"}) c.JSON(http.StatusOK, model.Response{Code: 200, Message: "已终止"})
} }
@@ -160,7 +163,7 @@ func (h *RDPSessionHandler) LogRDPReplayView(c *gin.Context) {
return return
} }
uid := middleware.GetUserID(c) uid := middleware.GetUserID(c)
log := model.AuditLog{ auditLog := model.AuditLog{
UserID: &uid, UserID: &uid,
Username: middleware.GetUsername(c), Username: middleware.GetUsername(c),
IP: c.ClientIP(), IP: c.ClientIP(),
@@ -172,6 +175,8 @@ func (h *RDPSessionHandler) LogRDPReplayView(c *gin.Context) {
Request: fmt.Sprintf("回放 Windows RDP 会话 #%d(%s → %s/%s)", id, session.Username, session.ServerName, session.ServerIP), Request: fmt.Sprintf("回放 Windows RDP 会话 #%d(%s → %s/%s)", id, session.Username, session.ServerName, session.ServerIP),
Status: 200, Status: 200,
} }
_ = h.db.Create(&log).Error if err := h.db.Create(&auditLog).Error; err != nil {
log.Printf("[RDPAudit] 记录回放审计日志失败 (session_id=%d, user=%s): %v", id, session.Username, err)
}
c.JSON(http.StatusOK, model.Response{Code: 200, Message: "ok"}) c.JSON(http.StatusOK, model.Response{Code: 200, Message: "ok"})
} }
+5 -2
View File
@@ -2,6 +2,7 @@ package controller
import ( import (
"fmt" "fmt"
"log"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
@@ -264,7 +265,7 @@ func (h *TerminalSessionHandler) LogReplayView(c *gin.Context) {
return return
} }
uid := middleware.GetUserID(c) uid := middleware.GetUserID(c)
log := model.AuditLog{ auditLog := model.AuditLog{
UserID: &uid, UserID: &uid,
Username: middleware.GetUsername(c), Username: middleware.GetUsername(c),
IP: c.ClientIP(), IP: c.ClientIP(),
@@ -276,7 +277,9 @@ func (h *TerminalSessionHandler) LogReplayView(c *gin.Context) {
Request: fmt.Sprintf("回放终端会话 #%d(%s → %s/%s)", id, session.Username, session.ServerName, session.ServerIP), Request: fmt.Sprintf("回放终端会话 #%d(%s → %s/%s)", id, session.Username, session.ServerName, session.ServerIP),
Status: 200, Status: 200,
} }
_ = h.db.Create(&log).Error // 落库失败不阻断回放 if err := h.db.Create(&auditLog).Error; err != nil {
log.Printf("[TerminalAudit] 记录回放审计日志失败 (session_id=%d, user=%s): %v", id, session.Username, err)
}
c.JSON(http.StatusOK, model.Response{Code: 200, Message: "ok"}) c.JSON(http.StatusOK, model.Response{Code: 200, Message: "ok"})
} }
+4 -1
View File
@@ -3,6 +3,7 @@ package controller
import ( import (
"crypto/rand" "crypto/rand"
"fmt" "fmt"
"log"
"math/big" "math/big"
"net/http" "net/http"
"time" "time"
@@ -136,7 +137,9 @@ func (h *UserHandler) ensureAPIKeyMeta(user *model.User) {
user.ApiAccessKeyDurationMonths = defaultAPIKeyMonths user.ApiAccessKeyDurationMonths = defaultAPIKeyMonths
} }
if len(patches) > 0 { if len(patches) > 0 {
_ = h.db.Model(&model.User{}).Where("id = ?", user.ID).Updates(patches).Error if err := h.db.Model(&model.User{}).Where("id = ?", user.ID).Updates(patches).Error; err != nil {
log.Printf("[UserApiKey] 补全 API 密钥默认值失败 (user_id=%d): %v", user.ID, err)
}
} }
} }
+4 -1
View File
@@ -1,6 +1,7 @@
package middleware package middleware
import ( import (
"log"
"net/http" "net/http"
"strings" "strings"
"time" "time"
@@ -122,7 +123,9 @@ func tryAPIKeyAuth(c *gin.Context, db *gorm.DB, tokenString string) bool {
return false return false
} }
now := time.Now() now := time.Now()
_ = db.Model(&model.User{}).Where("id = ?", user.ID).Update("api_access_key_last_used_at", now).Error if err := db.Model(&model.User{}).Where("id = ?", user.ID).Update("api_access_key_last_used_at", now).Error; err != nil {
log.Printf("[Auth] 更新 API 密钥最后使用时间失败 (user_id=%d): %v", user.ID, err)
}
c.Set("userID", user.ID) c.Set("userID", user.ID)
c.Set("username", user.Username) c.Set("username", user.Username)
c.Set("isAdmin", user.IsAdmin) c.Set("isAdmin", user.IsAdmin)