forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_json.go
53 lines (43 loc) · 1.04 KB
/
format_json.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
package audit
import (
"context"
"encoding/json"
"fmt"
"io"
"github.com/hashicorp/vault/helper/salt"
)
// JSONFormatWriter is an AuditFormatWriter implementation that structures data into
// a JSON format.
type JSONFormatWriter struct {
Prefix string
SaltFunc func(context.Context) (*salt.Salt, error)
}
func (f *JSONFormatWriter) WriteRequest(w io.Writer, req *AuditRequestEntry) error {
if req == nil {
return fmt.Errorf("request entry was nil, cannot encode")
}
if len(f.Prefix) > 0 {
_, err := w.Write([]byte(f.Prefix))
if err != nil {
return err
}
}
enc := json.NewEncoder(w)
return enc.Encode(req)
}
func (f *JSONFormatWriter) WriteResponse(w io.Writer, resp *AuditResponseEntry) error {
if resp == nil {
return fmt.Errorf("response entry was nil, cannot encode")
}
if len(f.Prefix) > 0 {
_, err := w.Write([]byte(f.Prefix))
if err != nil {
return err
}
}
enc := json.NewEncoder(w)
return enc.Encode(resp)
}
func (f *JSONFormatWriter) Salt(ctx context.Context) (*salt.Salt, error) {
return f.SaltFunc(ctx)
}