-
Notifications
You must be signed in to change notification settings - Fork 124
/
audit.go
223 lines (183 loc) · 5.42 KB
/
audit.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
// Copyright 2021-2024 Zenauth Ltd.
// SPDX-License-Identifier: Apache-2.0
package audit
import (
"bufio"
"context"
"fmt"
"io"
"strings"
"github.com/alecthomas/chroma/v2"
"github.com/alecthomas/chroma/v2/formatters"
"github.com/alecthomas/chroma/v2/lexers"
"github.com/alecthomas/chroma/v2/styles"
"github.com/alecthomas/kong"
"github.com/jwalton/gchalk"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"github.com/cerbos/cerbos-sdk-go/cerbos"
auditv1 "github.com/cerbos/cerbos/api/genpb/cerbos/audit/v1"
cmdclient "github.com/cerbos/cerbos/cmd/cerbosctl/internal/client"
"github.com/cerbos/cerbos/cmd/cerbosctl/internal/flagset"
)
var newline = []byte("\n")
const (
dashLen = 54
help = `View audit logs.
Requires audit logging to be enabled on the server. Supports several ways of filtering the data.
tail: View the last N records
between: View records captured between two timestamps. The timestamps must be formatted as ISO-8601
since: View records from X hours/minutes/seconds ago to now. Unit suffixes are: h=hours, m=minutes s=seconds
lookup: View a specific record using the Cerbos Call ID
# View the last 10 access logs
cerbosctl audit --kind=access --tail=10
# View the decision logs from midnight 2021-07-01 to midnight 2021-07-02
cerbosctl audit --kind=decision --between=2021-07-01T00:00:00Z,2021-07-02T00:00:00Z
# View the decision logs from midnight 2021-07-01 to now
cerbosctl audit --kind=decision --between=2021-07-01T00:00:00Z
# View the access logs from 3 hours ago to now as newline-delimited JSON
cerbosctl audit --kind=access --since=3h --raw
# View a specific access log entry by call ID
cerbosctl audit --kind=access --lookup=01F9Y5MFYTX7Y87A30CTJ2FB0S`
)
type Cmd struct {
Kind string `default:"access" enum:"access,decision" help:"Kind of log entry (${enum})"`
flagset.AuditFilters
Raw bool `help:"Output results without formatting or colours"`
}
func (c *Cmd) Run(k *kong.Kong, ctx *cmdclient.Context) error {
var writer auditLogWriter
if c.Raw {
writer = newRawAuditLogWriter(k.Stdout)
} else {
writer = newRichAuditLogWriter(k.Stdout)
}
defer writer.flush()
logOptions := c.AuditFilters.GenOptions()
switch kind := c.Kind; kind {
case "access":
logOptions.Type = cerbos.AccessLogs
case "decision":
logOptions.Type = cerbos.DecisionLogs
}
logs, err := ctx.AdminClient.AuditLogs(context.Background(), logOptions)
if err != nil {
return fmt.Errorf("could not get decision logs: %w", err)
}
if err = streamLogsToWriter(writer, logs); err != nil {
return fmt.Errorf("could not write decision logs: %w", err)
}
return nil
}
func (c *Cmd) Help() string {
return help
}
func (c *Cmd) Validate() error {
return c.AuditFilters.Validate()
}
func streamLogsToWriter(writer auditLogWriter, entries <-chan *cerbos.AuditLogEntry) error {
for e := range entries {
aLog, err := e.AccessLog()
if err != nil {
return fmt.Errorf("error while receiving access logs: %w", err)
}
if aLog != nil {
if err := writer.write(aLog); err != nil {
return err
}
continue
}
dLog, err := e.DecisionLog()
if err != nil {
return fmt.Errorf("error while receiving decision logs: %w", err)
}
if dLog != nil {
if err := writer.write(dLog); err != nil {
return err
}
}
}
return nil
}
type auditLogWriter interface {
write(proto.Message) error
flush()
}
func newRawAuditLogWriter(out io.Writer) *rawAuditLogWriter {
return &rawAuditLogWriter{out: out}
}
type rawAuditLogWriter struct {
out io.Writer
}
func (r *rawAuditLogWriter) write(entry proto.Message) error {
outBytes, err := protojson.Marshal(entry)
if err != nil {
return err
}
if _, err := r.out.Write(outBytes); err != nil {
return err
}
_, err = r.out.Write(newline)
return err
}
func (r *rawAuditLogWriter) flush() {}
type richAuditLogWriter struct {
out *bufio.Writer
lexer chroma.Lexer
formatter chroma.Formatter
rowStyle func(...string) string
jsonStyle *chroma.Style
}
func newRichAuditLogWriter(out io.Writer) *richAuditLogWriter {
lexer := lexers.Get("json")
if lexer == nil {
lexer = lexers.Fallback
}
var formatter chroma.Formatter
switch gchalk.GetLevel() {
case gchalk.LevelAnsi256:
formatter = formatters.TTY256
case gchalk.LevelAnsi16m:
formatter = formatters.TTY16m
default:
formatter = formatters.TTY
}
return &richAuditLogWriter{
out: bufio.NewWriter(out),
lexer: chroma.Coalesce(lexer),
formatter: formatter,
rowStyle: gchalk.WithHex("#eeeeee").WithBgHex("#005fff").Bold,
jsonStyle: styles.Get("solarized-dark256"),
}
}
func (r *richAuditLogWriter) write(entry proto.Message) error {
switch e := entry.(type) {
case *auditv1.AccessLogEntry:
r.header(fmt.Sprintf("%s %s", e.CallId, strings.Repeat("┈", dashLen)))
return r.formattedJSON(e)
case *auditv1.DecisionLogEntry:
r.header(fmt.Sprintf("%s %s", e.CallId, strings.Repeat("┈", dashLen)))
return r.formattedJSON(e)
default:
return nil
}
}
func (r *richAuditLogWriter) header(h string) {
_, _ = r.out.WriteString("\n\n")
_, _ = r.out.WriteString(r.rowStyle(h))
_, _ = r.out.WriteString("\n")
}
func (r *richAuditLogWriter) formattedJSON(msg proto.Message) error {
iterator, err := r.lexer.Tokenise(nil, protojson.Format(msg))
if err != nil {
return err
}
if err := r.formatter.Format(r.out, r.jsonStyle, iterator); err != nil {
return err
}
_, err = r.out.Write(newline)
return err
}
func (r *richAuditLogWriter) flush() {
_ = r.out.Flush()
}