-
Notifications
You must be signed in to change notification settings - Fork 4
/
audit_service.go
357 lines (297 loc) · 9.31 KB
/
audit_service.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright (c) 2016 Couchbase, Inc.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
package audit
import (
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
"github.com/couchbase/cbauth"
"github.com/couchbase/go-couchbase"
mc "github.com/couchbase/gomemcached"
mcc "github.com/couchbase/gomemcached/client"
log "github.com/couchbase/clog"
)
// opcode for memcached audit command
var AuditPutCommandCode = mc.CommandCode(0x27)
// write timeout
var WriteTimeout = 1000 * time.Millisecond
// read timeout
var ReadTimeout = 1000 * time.Millisecond
var PoolClients = 5
type GenericFields struct {
Timestamp string `json:"timestamp"`
RealUserid RealUserId `json:"real_userid"`
}
type CommonAuditFields struct {
GenericFields
Local *addressFields `json:"local,omitempty"`
Remote *addressFields `json:"remote,omitempty"`
}
type addressFields struct {
Ip string `json:"ip"`
Port string `json:"port,omitempty"`
}
type RealUserId struct {
Domain string `json:"domain"`
Username string `json:"user"`
}
type AuditSvc struct {
uri string
u string
p string
localhost string
kvaddr string
client chan *mcc.Client
m sync.Mutex // Protects the fields that follow.
initialized bool
}
func NewAuditSvc(uri string) (*AuditSvc, error) {
parsedUri, err := url.Parse(uri)
if err != nil {
return nil, err
}
u, p, err := cbauth.GetHTTPServiceAuth(parsedUri.Host)
if err != nil {
return nil, err
}
localhost, _, _ := net.SplitHostPort(parsedUri.Host)
service := &AuditSvc{
uri: uri,
u: u,
p: p,
localhost: localhost,
initialized: false,
client: make(chan *mcc.Client, PoolClients),
}
// Attempt to initialize the service here. No need to check for
// the err returned as if the service initialization were to
// fail because of the server not being ready yet, we will do
// this lazily when the Write API is invoked.
service.init()
log.Printf("audit: created new audit service")
return service, nil
}
func (service *AuditSvc) Write(eventId uint32, event interface{}) error {
err := service.init()
if err != nil {
return err
}
client, err := service.getClient()
if err != nil {
return err
}
if !client.IsHealthy() {
log.Printf("audit: Client found unhealthy. Creating new client.")
err = client.Close()
if err != nil {
log.Printf("audit: unable to close unhealthy connection: %v", err)
}
newClient, err := GetNewConnection(service.kvaddr)
if err != nil {
return fmt.Errorf("audit: unable to create new client: %v", err)
}
client = newClient
}
defer service.releaseClient(client)
return service.writeOnClient(client, eventId, event)
}
// Get a new connection that is not part of the general pool.
// The caller is responsible for its management.
func (service *AuditSvc) GetNonPoolClient() (*mcc.Client, error) {
client, err := GetNewConnection(service.kvaddr)
return client, err
}
func (service *AuditSvc) WriteUsingNonPoolClient(client *mcc.Client, eventId uint32,
event interface{}) error {
return service.writeOnClient(client, eventId, event)
}
func (service *AuditSvc) writeOnClient(client *mcc.Client, eventId uint32,
event interface{}) error {
req, err := composeAuditRequest(eventId, event)
if err != nil {
return err
}
if err := client.TransmitWithDeadline(req, time.Now().Add(WriteTimeout)); err != nil {
return err
}
res, err := client.ReceiveWithDeadline(time.Now().Add(ReadTimeout))
if err != nil {
return err
} else if res.Opcode != AuditPutCommandCode {
return errors.New(fmt.Sprintf("unexpected #opcode %v", res.Opcode))
} else if req.Opaque != res.Opaque {
return errors.New(fmt.Sprintf("opaque mismatch, %v over %v",
req.Opaque, res.Opaque))
} else if res.Status != mc.SUCCESS {
return errors.New(fmt.Sprintf("unsuccessful status = %v", res.Status))
}
return nil
}
func composeAuditRequest(eventId uint32, event interface{}) (
*mc.MCRequest, error) {
eventBytes, err := json.Marshal(event)
if err != nil {
return nil, err
}
req := &mc.MCRequest{
Opcode: AuditPutCommandCode}
req.Extras = make([]byte, 4)
binary.BigEndian.PutUint32(req.Extras[:4], eventId)
req.Body = eventBytes
req.Opaque = eventId
return req, nil
}
func (service *AuditSvc) init() error {
service.m.Lock()
defer service.m.Unlock()
if !service.initialized {
client, err := couchbase.ConnectWithAuthCreds(service.uri,
service.u, service.p)
if err != nil {
return fmt.Errorf("audit: error in connecting to url %s: %v", service.uri, err)
}
pool, err := client.GetPool("default")
if err != nil {
return fmt.Errorf("audit: error in connecting to default pool: %v", err)
}
for _, p := range pool.Nodes {
if p.ThisNode {
port, ok := p.Ports["direct"]
if !ok {
return fmt.Errorf("Error in getting memcached port")
}
var h string
if service.localhost != "" {
h = service.localhost
} else {
h, _, err = net.SplitHostPort(p.Hostname)
if err != nil || len(h) < 1 {
return fmt.Errorf("Invalid host string")
}
}
service.kvaddr = net.JoinHostPort(h, strconv.Itoa(port))
break
}
}
if service.kvaddr == "" {
return fmt.Errorf("Error in getting port")
}
for i := len(service.client); i < PoolClients; i++ {
c, err := GetNewConnection(service.kvaddr)
if err != nil {
return fmt.Errorf("audit: Unable to get connection: %v", err)
}
service.client <- c
}
service.initialized = true
}
return nil
}
func (service *AuditSvc) getClient() (*mcc.Client, error) {
return <-service.client, nil
}
func (service *AuditSvc) releaseClient(client *mcc.Client) error {
service.client <- client
return nil
}
func GetAuditBasicFields(req *http.Request) GenericFields {
uid := getRealUserIdFromRequest(req)
t := time.Now().Format("2006-01-02T15:04:05.000Z07:00")
return GenericFields{t, *uid}
}
/*
The following function should be used by go services to log audit-log information common across all services.
Presently, timestamp, user, local/remote endpoint ip-address have been taken care of.
Logical fields to be added here in future would be:
additional node-level information, if any.
cluster-name
etc.
W.r.t the "local" address, i.e., the ip-address / port the service is listening on for connection requests:
The ticket https://issues.couchbase.com/browse/MB-40204 has sample code provided by James Lee / Daniel Owen on
how to extract the local address information for go services.
go services that do not setup a ConnContext function in httpServer will get a different behavior wherein the
"local" address information is extracted from the HttpRequest.Host header.
*/
func GetCommonAuditFields(req *http.Request) CommonAuditFields {
gf := GetAuditBasicFields(req)
var l string
var portValidator func(port string) bool
if req.Context().Value("conn") != nil {
conn := req.Context().Value("conn").(net.Conn)
l = conn.LocalAddr().String()
} else {
l = req.Host
portValidator = func(port string) bool {
_, err := strconv.Atoi(port)
if err != nil {
return true
}
return false
}
}
// remote address always comes out from the connection; no validation necessary
remote := parseAddress(req.RemoteAddr, nil)
// local can come either out of the connection context, or from HttpRequest.Host header (untrusted).
// Need to validate if we try extract the address from an untrusted source.
local := parseAddress(l, portValidator)
return CommonAuditFields{gf, local, remote}
}
func parseAddress(addr string, portValidator func(port string) bool) *addressFields {
if addr == "" {
return nil
}
lastColon := strings.LastIndex(addr, ":")
if lastColon == -1 {
// No host:port separator. Put everything in Ip field.
return &addressFields{Ip: addr}
}
host := addr[:lastColon]
port := addr[lastColon+1:] // Not including the colon itself.
if portValidator != nil {
if invalidPort := portValidator(port); invalidPort {
log.Printf("Auditing: unable to parse port %s of address %s", port, addr)
return &addressFields{Ip: addr}
}
}
return &addressFields{Ip: host, Port: port}
}
func getRealUserIdFromRequest(request *http.Request) *RealUserId {
creds, err := cbauth.AuthWebCreds(request)
if err != nil {
log.Printf("audit: unable to get real userid from request: %v", err)
// put unknown user in the audit log.
return &RealUserId{"internal", "unknown"}
}
return &RealUserId{creds.Domain(), creds.Name()}
}
func GetNewConnection(kvAddr string) (*mcc.Client, error) {
c, err := mcc.Connect("tcp", kvAddr)
if err != nil {
return nil, fmt.Errorf("audit: Error in connection to"+
" memcached: %v", err)
}
u, p, err := cbauth.GetMemcachedServiceAuth(kvAddr)
if err != nil {
return nil, fmt.Errorf("audit: Error in getting auth for"+
" memcached: %v", err)
}
_, err = c.Auth(u, p)
if err != nil {
return nil, fmt.Errorf("audit: Error in auth: %v", err)
}
return c, nil
}