logauditlogic.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Code scaffolded by goctl. Safe to edit.
  2. // goctl 1.10.2
  3. package systemlog
  4. import (
  5. "context"
  6. "udeServer/internal/apierr"
  7. "udeServer/internal/audit"
  8. "udeServer/internal/log"
  9. "udeServer/internal/svc"
  10. "udeServer/internal/types"
  11. "github.com/zeromicro/go-zero/core/logx"
  12. "github.com/zeromicro/go-zero/core/stores/sqlx"
  13. )
  14. type LogAuditLogic struct {
  15. logx.Logger
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. }
  19. // 日志审计(校验Hmac防篡改)
  20. func NewLogAuditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LogAuditLogic {
  21. return &LogAuditLogic{
  22. Logger: logx.WithContext(ctx),
  23. ctx: ctx,
  24. svcCtx: svcCtx,
  25. }
  26. }
  27. // LogAudit recomputes the HMAC of the selected entries and marks each one
  28. // as audited-passed or audited-anomaly.
  29. func (l *LogAuditLogic) LogAudit(req *types.SystemLogAuditReq) (*types.SystemLogAuditResp, error) {
  30. if len(req.Idens) == 0 {
  31. return nil, apierr.BadRequest("idens 不能为空")
  32. }
  33. list, err := l.svcCtx.SystemLogModel.FindByIdens(l.ctx, req.Idens)
  34. if err != nil {
  35. log.RecordError(l.ctx, err)
  36. return nil, apierr.New(500, "查询日志失败")
  37. }
  38. // HMAC 密钥与写入时一致(来自 t_config.log_hmac_secret)
  39. _, logConfig := audit.GetGlobalConfig(l.svcCtx.ConfigModel)
  40. var invalidCount, validCount int64
  41. auditList := make([]types.AuditResult, 0, len(list))
  42. for i := range list {
  43. v := &list[i]
  44. auditResult := types.AuditResult{LogIden: v.Iden}
  45. macBuffer := make([]byte, 0, 32)
  46. mac, hErr := audit.CalculateHMAC(v, logConfig.HmacSecret, macBuffer)
  47. switch {
  48. case hErr != nil:
  49. log.RecordError(l.ctx, hErr)
  50. auditResult.Valid = false
  51. v.AuditStatus = audit.AuditedAnomaly
  52. auditResult.Reason = "HMAC计算失败"
  53. case v.HMac == mac:
  54. auditResult.Valid = true
  55. v.AuditStatus = audit.AuditedPassed
  56. validCount++
  57. auditList = append(auditList, auditResult)
  58. continue
  59. default:
  60. auditResult.Valid = false
  61. v.AuditStatus = audit.AuditedAnomaly
  62. auditResult.Reason = "HMAC不匹配,日志被篡改!"
  63. invalidCount++
  64. auditList = append(auditList, auditResult)
  65. continue
  66. }
  67. invalidCount++
  68. auditList = append(auditList, auditResult)
  69. }
  70. // 事务内逐条更新审计状态
  71. err = l.svcCtx.DB.TransactCtx(l.ctx, func(ctx context.Context, session sqlx.Session) error {
  72. for i := range list {
  73. if _, err := session.ExecCtx(ctx,
  74. "update t_system_log set audit_status = ? where id = ?",
  75. list[i].AuditStatus, list[i].Id); err != nil {
  76. return err
  77. }
  78. }
  79. return nil
  80. })
  81. if err != nil {
  82. log.RecordError(l.ctx, err)
  83. return nil, apierr.New(500, "更新审计状态失败")
  84. }
  85. resp := &types.SystemLogAuditResp{
  86. AuditResults: auditList,
  87. TotalCount: int64(len(auditList)),
  88. ValidCount: validCount,
  89. InValidCount: invalidCount,
  90. }
  91. log.RecordDebug(l.ctx, "日志审计完成!")
  92. return resp, nil
  93. }