forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry_formatter.go
616 lines (526 loc) · 18.3 KB
/
entry_formatter.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package audit
import (
"context"
"errors"
"fmt"
"reflect"
"runtime/debug"
"strings"
"time"
"github.com/go-jose/go-jose/v3/jwt"
"github.com/hashicorp/eventlogger"
"github.com/hashicorp/go-hclog"
nshelper "github.com/hashicorp/vault/helper/namespace"
"github.com/hashicorp/vault/sdk/helper/jsonutil"
"github.com/hashicorp/vault/sdk/helper/salt"
"github.com/hashicorp/vault/sdk/logical"
"github.com/jefferai/jsonx"
"github.com/mitchellh/copystructure"
)
var _ eventlogger.Node = (*entryFormatter)(nil)
// timeProvider offers a way to supply a pre-configured time.
type timeProvider interface {
// formatTime provides the pre-configured time in a particular format.
formattedTime() string
}
// nonPersistentSalt is used for obtaining a salt that is not persisted.
type nonPersistentSalt struct{}
// entryFormatter should be used to format audit requests and responses.
// NOTE: Use newEntryFormatter to initialize the entryFormatter struct.
type entryFormatter struct {
config formatterConfig
salter Salter
logger hclog.Logger
name string
}
// newEntryFormatter should be used to create an entryFormatter.
func newEntryFormatter(name string, config formatterConfig, salter Salter, logger hclog.Logger) (*entryFormatter, error) {
name = strings.TrimSpace(name)
if name == "" {
return nil, fmt.Errorf("name is required: %w", ErrInvalidParameter)
}
if salter == nil {
return nil, fmt.Errorf("cannot create a new audit formatter with nil salter: %w", ErrInvalidParameter)
}
if logger == nil || reflect.ValueOf(logger).IsNil() {
return nil, fmt.Errorf("cannot create a new audit formatter with nil logger: %w", ErrInvalidParameter)
}
return &entryFormatter{
config: config,
salter: salter,
logger: logger,
name: name,
}, nil
}
// Reopen is a no-op for the formatter node.
func (*entryFormatter) Reopen() error {
return nil
}
// Type describes the type of this node (formatter).
func (*entryFormatter) Type() eventlogger.NodeType {
return eventlogger.NodeTypeFormatter
}
// Process will attempt to parse the incoming event data into a corresponding
// audit request/response which is serialized to JSON/JSONx and stored within the event.
func (f *entryFormatter) Process(ctx context.Context, e *eventlogger.Event) (_ *eventlogger.Event, retErr error) {
// Return early if the context was cancelled, eventlogger will not carry on
// asking nodes to process, so any sink node in the pipeline won't be called.
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
// Perform validation on the event, then retrieve the underlying AuditEvent
// and LogInput (from the AuditEvent Data).
if e == nil {
return nil, fmt.Errorf("event is nil: %w", ErrInvalidParameter)
}
a, ok := e.Payload.(*Event)
if !ok {
return nil, fmt.Errorf("cannot parse event payload: %w", ErrInvalidParameter)
}
if a.Data == nil {
return nil, fmt.Errorf("cannot audit a '%s' event with no data: %w", a.Subtype, ErrInvalidParameter)
}
// Handle panics
defer func() {
r := recover()
if r == nil {
return
}
path := "unknown"
if a.Data.Request != nil {
path = a.Data.Request.Path
}
f.logger.Error("panic during logging",
"request_path", path,
"audit_device_path", f.name,
"error", r,
"stacktrace", string(debug.Stack()))
// Ensure that we add this error onto any pre-existing error that was being returned.
retErr = errors.Join(retErr, fmt.Errorf("panic generating audit log: %q", f.name))
}()
// Using 'any' to make exclusion easier, the JSON encoder doesn't care about types.
var entry any
var err error
entry, err = f.createEntry(ctx, a)
if err != nil {
return nil, err
}
// If this pipeline has been configured with (Enterprise-only) exclusions then
// attempt to exclude the fields from the audit entry.
if f.shouldExclude() {
m, err := f.excludeFields(entry)
if err != nil {
return nil, fmt.Errorf("unable to exclude %s audit data from %q: %w", a.Subtype, f.name, err)
}
entry = m
}
result, err := jsonutil.EncodeJSON(entry)
if err != nil {
return nil, fmt.Errorf("unable to format %s: %w", a.Subtype, err)
}
if f.config.requiredFormat == jsonxFormat {
var err error
result, err = jsonx.EncodeJSONBytes(result)
if err != nil {
return nil, fmt.Errorf("unable to encode JSONx using JSON data: %w", err)
}
if result == nil {
return nil, fmt.Errorf("encoded JSONx was nil: %w", err)
}
}
// This makes a bit of a mess of the 'format' since both JSON and XML (JSONx)
// don't support a prefix just sitting there.
// However, this would be a breaking change to how Vault currently works to
// include the prefix as part of the JSON object or XML document.
if f.config.prefix != "" {
result = append([]byte(f.config.prefix), result...)
}
// Create a new event, so we can store our formatted data without conflict.
e2 := &eventlogger.Event{
Type: e.Type,
CreatedAt: e.CreatedAt,
Formatted: make(map[string][]byte), // we are about to set this ourselves.
Payload: a,
}
e2.FormattedAs(f.config.requiredFormat.String(), result)
return e2, nil
}
// remoteAddr safely gets the remote address avoiding a nil pointer.
func remoteAddr(req *logical.Request) string {
if req != nil && req.Connection != nil {
return req.Connection.RemoteAddr
}
return ""
}
// remotePort safely gets the remote port avoiding a nil pointer.
func remotePort(req *logical.Request) int {
if req != nil && req.Connection != nil {
return req.Connection.RemotePort
}
return 0
}
// clientCertSerialNumber attempts the retrieve the serial number of the peer
// certificate from the specified tls.ConnectionState.
func clientCertSerialNumber(req *logical.Request) string {
if req == nil || req.Connection == nil {
return ""
}
connState := req.Connection.ConnState
if connState == nil || len(connState.VerifiedChains) == 0 || len(connState.VerifiedChains[0]) == 0 {
return ""
}
return connState.VerifiedChains[0][0].SerialNumber.String()
}
// parseVaultTokenFromJWT returns a string iff the token was a JWT, and we could
// extract the original token ID from inside
func parseVaultTokenFromJWT(token string) *string {
if strings.Count(token, ".") != 2 {
return nil
}
parsedJWT, err := jwt.ParseSigned(token)
if err != nil {
return nil
}
var claims jwt.Claims
if err = parsedJWT.UnsafeClaimsWithoutVerification(&claims); err != nil {
return nil
}
return &claims.ID
}
// newTemporaryEntryFormatter creates a cloned entryFormatter instance with a non-persistent Salter.
func newTemporaryEntryFormatter(n *entryFormatter) *entryFormatter {
return &entryFormatter{
salter: &nonPersistentSalt{},
config: n.config,
}
}
// Salt returns a new salt with default configuration and no storage usage, and no error.
func (s *nonPersistentSalt) Salt(_ context.Context) (*salt.Salt, error) {
return salt.NewNonpersistentSalt(), nil
}
// clone can be used to deep clone the specified type.
func clone[V any](s V) (V, error) {
s2, err := copystructure.Copy(s)
return s2.(V), err
}
// newAuth takes a logical.Auth and the number of remaining client token uses
// (which should be supplied from the logical.Request's client token), and creates
// an audit auth.
// tokenRemainingUses should be the client token remaining uses to include in auth.
// This usually can be found in logical.Request.ClientTokenRemainingUses.
// NOTE: supplying a nil value for auth will result in a nil return value and
// (nil) error. The caller should check the return value before attempting to use it.
// ignore-nil-nil-function-check.
func newAuth(input *logical.Auth, tokenRemainingUses int) (*auth, error) {
if input == nil {
return nil, nil
}
extNSPolicies, err := clone(input.ExternalNamespacePolicies)
if err != nil {
return nil, fmt.Errorf("unable to clone logical auth: external namespace policies: %w", err)
}
identityPolicies, err := clone(input.IdentityPolicies)
if err != nil {
return nil, fmt.Errorf("unable to clone logical auth: identity policies: %w", err)
}
metadata, err := clone(input.Metadata)
if err != nil {
return nil, fmt.Errorf("unable to clone logical auth: metadata: %w", err)
}
policies, err := clone(input.Policies)
if err != nil {
return nil, fmt.Errorf("unable to clone logical auth: policies: %w", err)
}
var polRes *policyResults
if input.PolicyResults != nil {
polRes = &policyResults{
Allowed: input.PolicyResults.Allowed,
GrantingPolicies: make([]policyInfo, len(input.PolicyResults.GrantingPolicies)),
}
for _, p := range input.PolicyResults.GrantingPolicies {
polRes.GrantingPolicies = append(polRes.GrantingPolicies, policyInfo{
Name: p.Name,
NamespaceId: p.NamespaceId,
NamespacePath: p.NamespacePath,
Type: p.Type,
})
}
}
tokenPolicies, err := clone(input.TokenPolicies)
if err != nil {
return nil, fmt.Errorf("unable to clone logical auth: token policies: %w", err)
}
var tokenIssueTime string
if !input.IssueTime.IsZero() {
tokenIssueTime = input.IssueTime.Format(time.RFC3339)
}
return &auth{
Accessor: input.Accessor,
ClientToken: input.ClientToken,
DisplayName: input.DisplayName,
EntityCreated: input.EntityCreated,
EntityID: input.EntityID,
ExternalNamespacePolicies: extNSPolicies,
IdentityPolicies: identityPolicies,
Metadata: metadata,
NoDefaultPolicy: input.NoDefaultPolicy,
NumUses: input.NumUses,
Policies: policies,
PolicyResults: polRes,
RemainingUses: tokenRemainingUses,
TokenPolicies: tokenPolicies,
TokenIssueTime: tokenIssueTime,
TokenTTL: int64(input.TTL.Seconds()),
TokenType: input.TokenType.String(),
}, nil
}
// newRequest takes a logical.Request and namespace.Namespace, transforms and
// aggregates them into an audit request.
func newRequest(req *logical.Request, ns *nshelper.Namespace) (*request, error) {
if req == nil {
return nil, fmt.Errorf("request cannot be nil")
}
remoteAddr := remoteAddr(req)
remotePort := remotePort(req)
clientCertSerial := clientCertSerialNumber(req)
data, err := clone(req.Data)
if err != nil {
return nil, fmt.Errorf("unable to clone logical request: data: %w", err)
}
headers, err := clone(req.Headers)
if err != nil {
return nil, fmt.Errorf("unable to clone logical request: headers: %w", err)
}
var reqURI string
if req.HTTPRequest != nil && req.HTTPRequest.RequestURI != req.Path {
reqURI = req.HTTPRequest.RequestURI
}
var wrapTTL int
if req.WrapInfo != nil {
wrapTTL = int(req.WrapInfo.TTL / time.Second)
}
return &request{
ClientCertificateSerialNumber: clientCertSerial,
ClientID: req.ClientID,
ClientToken: req.ClientToken,
ClientTokenAccessor: req.ClientTokenAccessor,
Data: data,
Headers: headers,
ID: req.ID,
MountAccessor: req.MountAccessor,
MountClass: req.MountClass(),
MountIsExternalPlugin: req.MountIsExternalPlugin(),
MountPoint: req.MountPoint,
MountRunningSha256: req.MountRunningSha256(),
MountRunningVersion: req.MountRunningVersion(),
MountType: req.MountType,
Namespace: &namespace{
ID: ns.ID,
Path: ns.Path,
},
Operation: req.Operation,
Path: req.Path,
PolicyOverride: req.PolicyOverride,
RemoteAddr: remoteAddr,
RemotePort: remotePort,
ReplicationCluster: req.ReplicationCluster,
RequestURI: reqURI,
WrapTTL: wrapTTL,
}, nil
}
// newResponse takes a logical.Response and logical.Request, transforms and
// aggregates them into an audit response.
// isElisionRequired is used to indicate that response 'Data' should be elided.
// NOTE: supplying a nil value for response will result in a nil return value and
// (nil) error. The caller should check the return value before attempting to use it.
// ignore-nil-nil-function-check.
func newResponse(resp *logical.Response, req *logical.Request, isElisionRequired bool) (*response, error) {
if resp == nil {
return nil, nil
}
if req == nil {
// Request should never be nil, even for a response.
return nil, fmt.Errorf("request cannot be nil")
}
auth, err := newAuth(resp.Auth, req.ClientTokenRemainingUses)
if err != nil {
return nil, fmt.Errorf("unable to convert logical auth response: %w", err)
}
var data map[string]any
if resp.Data != nil {
data = make(map[string]any, len(resp.Data))
if isElisionRequired {
// Performs the actual elision (ideally for list operations) of response data,
// once surrounding code has determined it should apply to a particular request.
// If the value for a key should not be elided, then it will be cloned.
for k, v := range resp.Data {
isCloneRequired := true
switch k {
case "keys":
if vSlice, ok := v.([]string); ok {
data[k] = len(vSlice)
isCloneRequired = false
}
case "key_info":
if vMap, ok := v.(map[string]any); ok {
data[k] = len(vMap)
isCloneRequired = false
}
}
// Clone values if they weren't legitimate keys or key_info.
if isCloneRequired {
v2, err := clone(v)
if err != nil {
return nil, fmt.Errorf("unable to clone response data while eliding: %w", err)
}
data[k] = v2
}
}
} else {
// Deep clone all values, no shortcuts here.
data, err = clone(resp.Data)
if err != nil {
return nil, fmt.Errorf("unable to clone response data: %w", err)
}
}
}
headers, err := clone(resp.Headers)
if err != nil {
return nil, fmt.Errorf("unable to clone logical response: headers: %w", err)
}
var s *secret
if resp.Secret != nil {
s = &secret{LeaseID: resp.Secret.LeaseID}
}
var wrapInfo *responseWrapInfo
if resp.WrapInfo != nil {
token := resp.WrapInfo.Token
if jwtToken := parseVaultTokenFromJWT(token); jwtToken != nil {
token = *jwtToken
}
ttl := int(resp.WrapInfo.TTL / time.Second)
wrapInfo = &responseWrapInfo{
TTL: ttl,
Token: token,
Accessor: resp.WrapInfo.Accessor,
CreationTime: resp.WrapInfo.CreationTime.UTC().Format(time.RFC3339Nano),
CreationPath: resp.WrapInfo.CreationPath,
WrappedAccessor: resp.WrapInfo.WrappedAccessor,
}
}
warnings, err := clone(resp.Warnings)
if err != nil {
return nil, fmt.Errorf("unable to clone logical response: warnings: %w", err)
}
return &response{
Auth: auth,
Data: data,
Headers: headers,
MountAccessor: req.MountAccessor,
MountClass: req.MountClass(),
MountIsExternalPlugin: req.MountIsExternalPlugin(),
MountPoint: req.MountPoint,
MountRunningSha256: req.MountRunningSha256(),
MountRunningVersion: req.MountRunningVersion(),
MountType: req.MountType,
Redirect: resp.Redirect,
Secret: s,
WrapInfo: wrapInfo,
Warnings: warnings,
}, nil
}
// createEntry takes the AuditEvent and builds an audit entry.
// The entry will be HMAC'd and elided where required.
func (f *entryFormatter) createEntry(ctx context.Context, a *Event) (*entry, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
data := a.Data
if data.Request == nil {
// Request should never be nil, even for a response.
return nil, fmt.Errorf("unable to parse request from '%s' audit event: request cannot be nil", a.Subtype)
}
ns, err := nshelper.FromContext(ctx)
if err != nil {
return nil, fmt.Errorf("unable to retrieve namespace from context: %w", err)
}
auth, err := newAuth(data.Auth, data.Request.ClientTokenRemainingUses)
if err != nil {
return nil, fmt.Errorf("cannot convert auth: %w", err)
}
req, err := newRequest(data.Request, ns)
if err != nil {
return nil, fmt.Errorf("cannot convert request: %w", err)
}
var resp *response
if a.Subtype == ResponseType {
shouldElide := f.config.elideListResponses && req.Operation == logical.ListOperation
resp, err = newResponse(data.Response, data.Request, shouldElide)
if err != nil {
return nil, fmt.Errorf("cannot convert response: %w", err)
}
}
var outerErr string
if data.OuterErr != nil {
outerErr = data.OuterErr.Error()
}
entryType := data.Type
if entryType == "" {
entryType = a.Subtype.String()
}
entry := &entry{
Auth: auth,
Error: outerErr,
Forwarded: false,
ForwardedFrom: data.Request.ForwardedFrom,
Request: req,
Response: resp,
Type: entryType,
}
if !f.config.omitTime {
// Use the time provider to supply the time for this entry.
entry.Time = a.timeProvider().formattedTime()
}
// If the request is present in the input data, apply header configuration
// regardless. We shouldn't be in a situation where the header formatter isn't
// present as it's required.
if entry.Request != nil {
// Ensure that any headers in the request, are formatted as required, and are
// only present if they have been configured to appear in the audit log.
// e.g. via: /sys/config/auditing/request-headers/:name
entry.Request.Headers, err = f.config.headerFormatter.ApplyConfig(ctx, entry.Request.Headers, f.salter)
if err != nil {
return nil, fmt.Errorf("unable to transform headers for auditing: %w", err)
}
}
// If the request contains a Server-Side Consistency Token (SSCT), and we
// have an auth response, overwrite the existing client token with the SSCT,
// so that the SSCT appears in the audit log for this entry.
if data.Request != nil && data.Request.InboundSSCToken != "" && entry.Auth != nil {
entry.Auth.ClientToken = data.Request.InboundSSCToken
}
// Hash the entry if we aren't expecting raw output.
if !f.config.raw {
// Requests and responses have auth and request.
err = hashAuth(ctx, f.salter, entry.Auth, f.config.hmacAccessor)
if err != nil {
return nil, err
}
err = hashRequest(ctx, f.salter, entry.Request, f.config.hmacAccessor, data.NonHMACReqDataKeys)
if err != nil {
return nil, err
}
if a.Subtype == ResponseType {
if err = hashResponse(ctx, f.salter, entry.Response, f.config.hmacAccessor, data.NonHMACRespDataKeys); err != nil {
return nil, err
}
}
}
return entry, nil
}