audit.go 940 Bytes
Newer Older
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
package accountserver

import (
	"context"
	"encoding/json"
	"log"
)

type auditLogger interface {
	Log(context.Context, ResourceID, string)
}

type auditLogEntry struct {
	User         string `json:"user,omitempty"`
	By           string `json:"by"`
	Message      string `json:"message"`
	Comment      string `json:"comment,omitempty"`
	ResourceName string `json:"resource_name,omitempty"`
	ResourceType string `json:"resource_type,omitempty"`
}

type syslogAuditLogger struct{}

func (l *syslogAuditLogger) Log(ctx context.Context, resourceID ResourceID, what string) {
	e := auditLogEntry{
		User:    userFromContext(ctx),
		By:      authUserFromContext(ctx),
		Message: what,
		Comment: commentFromContext(ctx),
	}

	if !resourceID.Empty() {
		e.ResourceName = resourceID.Name()
		e.ResourceType = resourceID.Type()
35
36
37
		if u := resourceID.User(); u != "" {
			e.User = u
		}
38
39
40
41
42
	}

	data, _ := json.Marshal(&e)
	log.Printf("@cee:%s", data)
}