-
Notifications
You must be signed in to change notification settings - Fork 8
/
audit.go
68 lines (59 loc) · 1.69 KB
/
audit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package sabakan
import (
"context"
"time"
)
// AuditCategory is the type of audit categories.
type AuditCategory string
// Audit categories.
const (
AuditAssets = AuditCategory("assets")
AuditCrypts = AuditCategory("crypts")
AuditDHCP = AuditCategory("dhcp")
AuditIgnition = AuditCategory("ignition")
AuditImage = AuditCategory("image")
AuditIPAM = AuditCategory("ipam")
AuditIPXE = AuditCategory("ipxe")
AuditMachines = AuditCategory("machines")
)
// AuditLog represents an audit log entry.
type AuditLog struct {
Timestamp time.Time `json:"ts"`
Revision int64 `json:"rev,string"`
User string `json:"user"`
IP string `json:"ip"`
Host string `json:"host"`
Category AuditCategory `json:"category"`
Instance string `json:"instance"`
Action string `json:"action"`
Detail string `json:"detail"`
}
// AuditContextKey is the type of context keys for audit.
type AuditContextKey string
// Audit context keys. Values must be string.
const (
AuditKeyUser = AuditContextKey("user")
AuditKeyIP = AuditContextKey("ip")
AuditKeyHost = AuditContextKey("host")
)
// NewAuditLog creates an audit log entry and initializes it.
func NewAuditLog(ctx context.Context, ts time.Time, rev int64, cat AuditCategory,
instance, action, detail string) *AuditLog {
a := new(AuditLog)
a.Timestamp = ts.UTC()
a.Revision = rev
if v := ctx.Value(AuditKeyUser); v != nil {
a.User = v.(string)
}
if v := ctx.Value(AuditKeyIP); v != nil {
a.IP = v.(string)
}
if v := ctx.Value(AuditKeyHost); v != nil {
a.Host = v.(string)
}
a.Category = cat
a.Instance = instance
a.Action = action
a.Detail = detail
return a
}