audit

package
v0.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package audit 记录管理后台的操作审计日志:谁、何时、从哪个 IP、做了什么。

日志以 JSON Lines 追加写入配置目录下的 audit.log,超过大小上限后轮转。 审计只记录方法、路径、结果与操作者,不记录请求体,因此不会泄露密钥。

Index

Constants

View Source
const (
	CategoryLogin  = "login"  // 登录、登出
	CategoryChange = "change" // 配置或状态变更
	CategoryAccess = "access" // 读取敏感内容(备份、诊断包、终端连接等)
)

操作类别

Variables

This section is empty.

Functions

func Via

func Via(r *http.Request) string

Via 返回转发该请求的代理地址:客户端地址取自代理请求头时为 TCP 对端,直连时为空; 经由已标记的代理(如飞牛统一网关)时为代理名称。

Types

type Entry

type Entry struct {
	Time time.Time `json:"time"`
	// Actor 为操作者:用户名,或 api-token:<令牌名>
	Actor string `json:"actor"`
	IP    string `json:"ip"`
	// Via 为转发该请求的本机代理地址;直连时为空
	Via string `json:"via,omitempty"`
	// Category 见 Category* 常量
	Category string `json:"category"`
	// Module 为所属模块 ID,系统级操作为空
	Module string `json:"module,omitempty"`
	Method string `json:"method,omitempty"`
	Path   string `json:"path,omitempty"`
	// Status 为 HTTP 状态码,非请求类记录为 0
	Status int `json:"status,omitempty"`
	// Detail 为补充说明(例如登录失败原因)
	Detail string `json:"detail,omitempty"`
}

Entry 是一条审计记录。

type Log

type Log struct {
	// contains filtered or unexported fields
}

Log 是审计日志文件的句柄。

func New

func New(dir string) *Log

New 返回写入 dir/audit.log 的审计日志。

func (*Log) Middleware

func (l *Log) Middleware(next http.Handler) http.Handler

Middleware 记录经过认证后的变更类请求与敏感读取。必须挂在认证中间件之内,才能取到操作者。

func (*Log) Record

func (l *Log) Record(e Entry)

Record 追加一条记录。写入失败只丢弃该条,不影响业务请求。

func (*Log) Search

func (l *Log) Search(q Query) Result

Search 按时间倒序返回匹配记录。

func (*Log) SetHook

func (l *Log) SetHook(h func(Entry))

SetHook 设置记录写入后的回调(在写入协程内同步调用,应当快速返回)。

type Query

type Query struct {
	Category string
	Module   string
	Actor    string
	// Text 在路径、操作者、IP、详情中做不区分大小写的子串匹配
	Text string
	// Limit 默认 100,上限 1000
	Limit int
	// Offset 跳过最新的若干条匹配记录,用于翻页
	Offset int
}

Query 是查询条件,空字段不参与过滤。

type Result

type Result struct {
	Entries []Entry `json:"entries"`
	// Total 为匹配的总条数(仅统计保留的日志文件)
	Total int `json:"total"`
}

Result 是一页查询结果。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL