-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathaudit_ee.go
179 lines (160 loc) · 4.3 KB
/
audit_ee.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// +build !oss
/*
* Copyright 2021 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Dgraph Community License (the "License"); you
* may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* https://github.com/dgraph-io/dgraph/blob/master/licenses/DCL.txt
*/
package audit
import (
"fmt"
"io/ioutil"
"path/filepath"
"sync/atomic"
"time"
"github.com/dgraph-io/dgraph/x"
"github.com/golang/glog"
)
const (
defaultAuditConf = "dir=;compress=false;encrypt-file="
defaultAuditFilename = "dgraph_audit.log"
)
var auditEnabled uint32
type AuditConf struct {
Compress bool
Dir string
EncryptBytes []byte
}
type AuditEvent struct {
User string
ServerHost string
ClientHost string
Endpoint string
ReqType string
Req string
Status string
QueryParams map[string][]string
}
const (
UnauthorisedUser = "UnauthorisedUser"
UnknownUser = "UnknownUser"
PoorManAuth = "PoorManAuth"
Grpc = "Grpc"
Http = "Http"
)
var auditor *auditLogger = &auditLogger{}
type auditLogger struct {
log *x.Logger
tick *time.Ticker
}
func GetAuditConf(conf string) (*AuditConf, error) {
if conf == "" {
return nil, nil
}
auditFlag := x.NewSuperFlag(conf).MergeAndCheckDefault(defaultAuditConf)
dir := auditFlag.GetString("dir")
if dir == "" {
return nil, fmt.Errorf("dir flag is not provided for the audit logs")
}
encBytes, err := readAuditEncKey(auditFlag)
if err != nil {
return nil, err
}
return &AuditConf{
Compress: auditFlag.GetBool("compress"),
Dir: dir,
EncryptBytes: encBytes,
}, nil
}
func readAuditEncKey(conf *x.SuperFlag) ([]byte, error) {
encFile := conf.GetString("encrypt-file")
if encFile == "" {
return nil, nil
}
path, err := filepath.Abs(encFile)
if err != nil {
return nil, err
}
encKey, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return encKey, nil
}
// InitAuditorIfNecessary accepts conf and enterprise edition check function.
// This method keep tracks whether cluster is part of enterprise edition or not.
// It pools eeEnabled function every five minutes to check if the license is still valid or not.
func InitAuditorIfNecessary(conf *AuditConf, eeEnabled func() bool) error {
if conf == nil {
return nil
}
if eeEnabled() {
if err := InitAuditor(conf); err != nil {
return err
}
}
auditor.tick = time.NewTicker(time.Minute * 5)
go trackIfEEValid(conf, eeEnabled)
return nil
}
// InitAuditor initializes the auditor.
// This method doesnt keep track of whether cluster is part of enterprise edition or not.
// Client has to keep track of that.
func InitAuditor(conf *AuditConf) error {
var err error
if auditor.log, err = x.InitLogger(conf.Dir, defaultAuditFilename, conf.EncryptBytes,
conf.Compress); err != nil {
return err
}
atomic.StoreUint32(&auditEnabled, 1)
glog.Infoln("audit logs are enabled")
return nil
}
// trackIfEEValid tracks enterprise license of the cluster.
// Right now alpha doesn't know about the enterprise/licence.
// That's why we needed to track if the current node is part of enterprise edition cluster
func trackIfEEValid(conf *AuditConf, eeEnabledFunc func() bool) {
var err error
for {
select {
case <-auditor.tick.C:
if !eeEnabledFunc() && atomic.CompareAndSwapUint32(&auditEnabled, 1, 0) {
glog.Infof("audit logs are disabled")
auditor.log.Sync()
auditor.log = nil
continue
}
if atomic.LoadUint32(&auditEnabled) != 1 {
if auditor.log, err = x.InitLogger(conf.Dir, defaultAuditFilename,
conf.EncryptBytes, conf.Compress); err != nil {
continue
}
atomic.StoreUint32(&auditEnabled, 1)
glog.Infof("audit logs are enabled")
}
}
}
}
// Close stops the ticker and sync the pending logs in buffer.
// It also sets the log to nil, because its being called by zero when license expires.
// If license added, InitLogger will take care of the file.
func Close() {
if auditor.tick != nil {
auditor.tick.Stop()
}
auditor.log.Sync()
auditor.log = nil
}
func (a *auditLogger) Audit(event *AuditEvent) {
a.log.AuditI(event.Endpoint,
"user", event.User,
"server", event.ServerHost,
"client", event.ClientHost,
"req_type", event.ReqType,
"req_body", event.Req,
"query_param", event.QueryParams,
"status", event.Status)
}