Files
ssw-ontology/server/access_log.go
T
ssw f1b687c2d2 目录扁平化:子目录内容提升至仓库根,文档归入 docs/
- 将 shangshu-ontology/ 子目录内容移至仓库根一级
- 修复 run.sh 的 REPO_ROOT 计算(扁平化后不再需要 cd ..),ARCHIVE_DIR 正确指向仓库根/archive
- 文档(README/PLAN/USAGE/IMPORT/product-intro)移至 docs/,同步更新结构树与导入命令中的旧路径
- 新增根 README.md 索引指向 docs/
- 新增 .gitignore:忽略 bin/、web/node_modules/、web/dist/、server/webroot/、.env、.workbuddy/ 等噪音
- main.go 的 ARCHIVE_DIR fallback 基于可执行文件相邻上级,扁平化后仍正确,无需改动
2026-08-07 14:21:34 +08:00

267 lines
7.5 KiB
Go

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"
"sync"
"time"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5/pgxpool"
)
// ---------- 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 *pgxpool.Pool) {
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 *pgxpool.Pool) {
go func() {
for range flushSignal {
// 合并多次唤醒:休眠 1s 等批量累积
time.Sleep(1 * time.Second)
writeAccessLog(p)
}
}()
}
// flushNow 在停服前刷一次(可选)
func flushAccessLogNow(p *pgxpool.Pool) {
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)
typ := c.Param("type")
pk := c.Param("pk")
if typ == "" {
typ = c.Query("type")
}
if pk == "" {
pk = c.Query("pk")
}
// 决定(默认 allow;若 handler 已通过 c.Set("authz_decision", ...) 标记,则用它)
decision := c.GetString("authz_decision")
if decision == "" {
decision = "allow"
}
// 命中 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()),
})
}
}
func classifyAction(method, path string) string {
switch {
case contains(path, "/objects/") && method == "GET":
return "read"
case contains(path, "/objects") && (method == "POST" || method == "PUT"):
return "write"
case contains(path, "/objects/") && method == "DELETE":
return "delete"
case contains(path, "/connector/") && method == "GET":
return "read"
case contains(path, "/access/"):
return "manage"
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})
}
// strconv_Atoi 占位(已用标准库,保留符号避免引用残留)
var _ = strconv.Atoi