package main // Dynamic Security 审计日志(Kinetic 层 ⑥Dynamic Security 的运行时轨迹) // // 每次访问请求的关键决策点都写到 access_log: // - principal / method / path / target / action // - decision (allow/deny/redact/hide) // - matched_grants (命中的 grant 列表) // - matched_markings (对象上的 marking 集) // - duration_ms (请求耗时) // // 中间件 accessLogMiddleware 自动记录到 mgmt + conn + mcp 路径。 // 前端 AccessControlView 提供"执行日志"子 tab 实时查看。 import ( "context" "encoding/json" "log" "strconv" "strings" "sync" "time" "github.com/gin-gonic/gin" ) // ---------- access_log 写入 ---------- var accessLogBuf []AccessLogRow var accessLogMu sync.Mutex type AccessLogRow struct { CreatedAt time.Time Principal string Method string Path string TargetType string TargetPK string Action string Decision string MatchedGrants []int64 MatchedMarkings []string DurationMs int } // writeAccessLog 异步批量写入(避免每次 INSERT 影响性能) func writeAccessLog(p dbExec) { accessLogMu.Lock() rows := accessLogBuf accessLogBuf = nil accessLogMu.Unlock() if len(rows) == 0 { return } for _, r := range rows { grants, _ := json.Marshal(r.MatchedGrants) marks, _ := json.Marshal(r.MatchedMarkings) _, err := p.Exec(context.Background(), `INSERT INTO access_log(created_at,principal,method,path,target_type,target_pk,action,decision,matched_grants,matched_markings,duration_ms) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9::jsonb,$10::jsonb,$11)`, r.CreatedAt, r.Principal, r.Method, r.Path, r.TargetType, r.TargetPK, r.Action, r.Decision, string(grants), string(marks), r.DurationMs) if err != nil { log.Printf("[access_log] 写入失败: %v", err) } } } // queueAccessLog 异步入队(带 1s 节流批量刷) func (a *app) queueAccessLog(row AccessLogRow) { accessLogMu.Lock() accessLogBuf = append(accessLogBuf, row) accessLogMu.Unlock() select { case flushSignal <- struct{}{}: default: } } var flushSignal = make(chan struct{}, 1) func startAccessLogFlusher(p dbExec) { go func() { for range flushSignal { // 合并多次唤醒:休眠 1s 等批量累积 time.Sleep(1 * time.Second) writeAccessLog(p) } }() } // flushNow 在停服前刷一次(可选) func flushAccessLogNow(p dbExec) { writeAccessLog(p) } // ---------- Gin 中间件:自动记录 ---------- // accessLogMiddleware 把 access_log 与请求生命周期挂钩 // - 解析 principal // - 提取 path 上的 :type / :pk(若存在) // - 请求结束后 flush 一条记录 // - decision 默认 "allow";下游可标记 "deny" / "redact" func (a *app) accessLogMiddleware() gin.HandlerFunc { return func(c *gin.Context) { start := time.Now() c.Next() // 不记录静态资源 / health if c.Request.URL.Path == "/health" || c.Request.URL.Path == "/" { return } // 解析 principal p := a.principalFromRequest(c) // 提取目标 (type/pk)。三层来源: // 1) handler 通过 c.Set("audit_target_pk", ...) 精确指定(create 类目标名在 body 里) // 2) 按路由模板映射(targetFromRoute,修复 :name/:id 参数名导致 target 为空) // 3) query 参数兜底(老行为) typ, pk := targetFromRoute(c) if v := c.GetString("audit_target_pk"); v != "" { pk = v } // 决定(默认 allow;若 handler 已通过 c.Set("authz_decision", ...) 标记,则用它) decision := c.GetString("authz_decision") if decision == "" { decision = "allow" } // 修复(2026-09-11):鉴权/业务层直接 401/403/404 的请求此前被记成 allow, // 导致"拒绝"统计失真 —— 按最终响应状态码兜底标记。 if decision == "allow" && c.Writer.Status() >= 400 { decision = "deny" } // 命中 grant(可选:handler 通过 c.Set("authz_grants", [...]) 写入) var matchedGrants []int64 if v, ok := c.Get("authz_grants"); ok { if ids, ok := v.([]int64); ok { matchedGrants = ids } } var matchedMarkings []string if v, ok := c.Get("authz_markings"); ok { if ms, ok := v.([]string); ok { matchedMarkings = ms } } a.queueAccessLog(AccessLogRow{ CreatedAt: time.Now(), Principal: p, Method: c.Request.Method, Path: c.Request.URL.Path, TargetType: typ, TargetPK: pk, Action: classifyAction(c.Request.Method, c.Request.URL.Path), Decision: decision, MatchedGrants: matchedGrants, MatchedMarkings: matchedMarkings, DurationMs: int(time.Since(start).Milliseconds()), }) } } // targetFromRoute 按路由模板(c.FullPath())把请求映射到 (target_type, target_pk)。 // 此前只认 :type/:pk 两个参数名,而 /object-types/:name、/properties/:id 等路由的 // 参数名是 name/id,导致本体类型层操作的 target 长期为空,分项统计无从聚合。 // 约定:实例类路由(/objects/:type/:pk、/connector/object[s]/…)的 target_type // 就是对象类型 api_name;元原语路由用固定类型名(object_type/link_type/...)。 func targetFromRoute(c *gin.Context) (string, string) { // 实例路由::type(+:pk)参数天然就是目标 if t := c.Param("type"); t != "" { return t, c.Param("pk") } // query 兜底(老行为保留) if t := c.Query("type"); t != "" { return t, c.Query("pk") } fp := c.FullPath() if fp == "" { return "", "" } name := c.Param("name") id := c.Param("id") switch { case strings.Contains(fp, "/object-types/:name/properties"): return "object_property", name // 具体属性名在 body,schemaAudit 会记全 case strings.Contains(fp, "/object-types/:name"): return "object_type", name case strings.Contains(fp, "/object-types"): return "object_type", "" // create:名字在 body case strings.Contains(fp, "/properties/:id"): return "object_property", id case strings.Contains(fp, "/link-types/:name/properties"): return "link_property", name case strings.Contains(fp, "/link-types/:name"): return "link_type", name case strings.Contains(fp, "/link-types"): return "link_type", "" case strings.Contains(fp, "/link-properties/:id"): return "link_property", id case strings.Contains(fp, "/action-types"): return "action_type", "" case strings.Contains(fp, "/function-types/:name"): return "function_type", name case strings.Contains(fp, "/function-types"): return "function_type", "" case strings.Contains(fp, "/interface-types/:name"): return "interface_type", name case strings.Contains(fp, "/interface-types"): return "interface_type", "" case strings.Contains(fp, "/materializations/:name"): return "materialization", name case strings.Contains(fp, "/backing-sources/:name"): return "backing_source", name case strings.Contains(fp, "/principals/:name"): return "principal", name case strings.Contains(fp, "/grants/:id"): return "grant", id case strings.Contains(fp, "/field-policies/:id"): return "field_policy", id } return "", "" } func classifyAction(method, path string) string { switch { case contains(path, "/access/"): return "manage" case method == "GET": return "read" case method == "DELETE": return "delete" case method == "POST" || method == "PUT": return "write" default: return method + " " + path } } func contains(s, sub string) bool { for i := 0; i+len(sub) <= len(s); i++ { if s[i:i+len(sub)] == sub { return true } } return false } // ---------- 查询 API ---------- func (a *app) listAccessLog(c *gin.Context) { principal := c.Query("principal") decision := c.Query("decision") limit, _ := time.ParseDuration("0s") _ = limit l := 200 if v, err := strconv.Atoi(c.Query("limit")); err == nil && v > 0 && v <= 1000 { l = v } args := []interface{}{} sql := `SELECT id, created_at, principal, method, path, target_type, target_pk, action, decision, matched_grants, matched_markings, duration_ms FROM access_log WHERE 1=1` if principal != "" { args = append(args, principal) sql += ` AND principal=$` + itoa(len(args)) } if decision != "" { args = append(args, decision) sql += ` AND decision=$` + itoa(len(args)) } args = append(args, l) sql += ` ORDER BY id DESC LIMIT $` + itoa(len(args)) rows, err := a.pool.Query(c, sql, args...) if err != nil { c.JSON(500, gin.H{"error": err.Error()}) return } defer rows.Close() type logRow struct { ID int64 `json:"id"` CreatedAt time.Time `json:"created_at"` Principal string `json:"principal"` Method string `json:"method"` Path string `json:"path"` TargetType string `json:"target_type"` TargetPK string `json:"target_pk"` Action string `json:"action"` Decision string `json:"decision"` MatchedGrants []int64 `json:"matched_grants"` MatchedMarkings []string `json:"matched_markings"` DurationMs int `json:"duration_ms"` } var out []logRow for rows.Next() { var r logRow var mgBytes, mmBytes []byte if err := rows.Scan(&r.ID, &r.CreatedAt, &r.Principal, &r.Method, &r.Path, &r.TargetType, &r.TargetPK, &r.Action, &r.Decision, &mgBytes, &mmBytes, &r.DurationMs); err != nil { continue } _ = json.Unmarshal(mgBytes, &r.MatchedGrants) _ = json.Unmarshal(mmBytes, &r.MatchedMarkings) out = append(out, r) } c.JSON(200, gin.H{"logs": out, "count": len(out)}) } // accessLogStats 简单统计 func (a *app) accessLogStats(c *gin.Context) { rows, err := a.pool.Query(c, `SELECT principal, decision, count(*) FROM access_log WHERE created_at > now() - interval '24 hours' GROUP BY principal, decision ORDER BY principal`) if err != nil { c.JSON(500, gin.H{"error": err.Error()}) return } defer rows.Close() type sRow struct { Principal string `json:"principal"` Decision string `json:"decision"` Count int64 `json:"count"` } var out []sRow for rows.Next() { var s sRow if rows.Scan(&s.Principal, &s.Decision, &s.Count) == nil { out = append(out, s) } } c.JSON(200, gin.H{"stats": out}) } // ---------- 保留策略 ---------- // startAccessLogRetention 定期清理过期 access_log,防止审计表无限膨胀。 // 默认保留 90 天(ACCESS_LOG_RETENTION_DAYS 覆盖,0 = 关闭清理)。 func startAccessLogRetention(p dbExec, days int) { if days <= 0 { log.Printf("[access_log] 保留策略已关闭(ACCESS_LOG_RETENTION_DAYS=0)") return } go func() { clean := func() { tag, err := p.Exec(context.Background(), `DELETE FROM access_log WHERE created_at < now() - make_interval(days => $1)`, days) if err != nil { log.Printf("[access_log] 过期清理失败: %v", err) } else if tag.RowsAffected() > 0 { log.Printf("[access_log] 清理过期审计日志 %d 行(保留 %d 天)", tag.RowsAffected(), days) } } clean() // 启动时清一次 t := time.NewTicker(24 * time.Hour) defer t.Stop() for range t.C { clean() } }() } // strconv_Atoi 占位(已用标准库,保留符号避免引用残留) var _ = strconv.Atoi