-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
285 lines (253 loc) · 8.39 KB
/
log.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
// Copyright 2019 - MinIO, Inc. All rights reserved.
// Use of this source code is governed by the AGPLv3
// license that can be found in the LICENSE file.
package kes
import (
"bufio"
"encoding/json"
"fmt"
"io"
"time"
)
// NewErrorStream returns an new ErrorStream that
// splits r into lines and tries to parse each
// line as JSON-encoded ErrorEvent.
func NewErrorStream(r io.Reader) *ErrorStream {
s := &ErrorStream{
scanner: bufio.NewScanner(r),
}
if closer, ok := r.(io.Closer); ok {
s.closer = closer
}
return s
}
// ErrorStream provides a convenient interface for
// iterating over a stream of ErrorEvents. Successive
// calls to the Next method will step through the error
// events of an io.Reader.
//
// By default, the ErrorStream breaks the underlying
// stream into lines and expects a JSON-encoded ErrorEvent
// per line - unless the line is empty. Empty lines will
// be ignored.
//
// Iterating stops at the end of the stream, the first I/O
// error, a ErrorEvent event too large to fit in the buffer,
// or when the stream gets closed.
//
// Closing an ErrorStream closes the underlying io.Reader,
// if it implements io.Closer, and any subsequent call to
// Next will return false.
type ErrorStream struct {
scanner *bufio.Scanner
event ErrorEvent
err error
closer io.Closer
closed bool
}
// Err returns the first non-EOF error that was encountered
// while iterating over the stream and un-marshaling ErrorEvents.
//
// Err does not return any error returned from Close.
func (s *ErrorStream) Err() error { return s.err }
// Event returns the most recent ErrorEvent generated by a
// call to Next.
func (s *ErrorStream) Event() ErrorEvent { return s.event }
// Bytes returns the most recent raw ErrorEvent content generated
// by a call to Next. It may not contain valid JSON.
//
// The underlying array may point to data that will be overwritten
// by a subsequent call to Next. It does no allocation.
func (s *ErrorStream) Bytes() []byte { return s.scanner.Bytes() }
// Next advances the stream to the next ErrorEvent, which will then
// be available through the Event and Bytes method. It returns false
// when the stream iteration stops - i.e. by reaching the end of the
// stream, closing the stream or in case of an error.
// After Next returns false, the Err method will return any error that
// occurred while iterating and parsing the stream.
func (s *ErrorStream) Next() bool {
if s.err != nil || s.closed {
return false
}
// Iterate over the stream until we find a non-empty line.
for {
if !s.scanner.Scan() {
if !s.closed { // Once the stream is closed we ignore the error
s.err = s.scanner.Err()
}
return false
}
if len(s.scanner.Bytes()) != 0 {
break
}
}
if err := json.Unmarshal(s.scanner.Bytes(), &s.event); err != nil {
if !s.closed { // Once the stream is closed we ignore the error
s.err = err
}
return false
}
return true
}
// Close closes the underlying stream - i.e. the io.Reader if
// if implements io.Closer. After Close has been called once
// the Next method will return false.
func (s *ErrorStream) Close() (err error) {
if s.closer != nil {
s.closed = true
err = s.closer.Close()
}
return err
}
// ErrorEvent is the event type the KES server produces when it
// encounters and logs an error.
//
// When a clients subscribes to the KES server error log it
// receives a stream of JSON-encoded error events separated
// by a newline.
type ErrorEvent struct {
Message string `json:"message"` // The logged error message
}
// NewAuditStream returns a new AuditStream that
// splits r into lines and tries to parse each
// line as JSON-encoded AuditEvent.
func NewAuditStream(r io.Reader) *AuditStream {
s := &AuditStream{
scanner: bufio.NewScanner(r),
}
if closer, ok := r.(io.Closer); ok {
s.closer = closer
}
return s
}
// AuditStream provides a convenient interface for
// iterating over a stream of AuditEvents. Successive
// calls to the Next method will step through the audit
// events of an io.Reader.
//
// By default, the AuditStream breaks the underlying
// stream into lines and expects a JSON-encoded AuditEvent
// per line - unless the line is empty. Empty lines will
// be ignored.
//
// Iterating stops at the end of the stream, the first I/O
// error, an AuditEvent event too large to fit in the buffer,
// or when the stream gets closed.
//
// Closing an AuditStream closes the underlying io.Reader,
// if it implements io.Closer, and any subsequent call to
// Next will return false.
type AuditStream struct {
scanner *bufio.Scanner
event AuditEvent
err error
closer io.Closer
closed bool
}
// Err returns the first non-EOF error that was encountered
// while iterating over the stream and un-marshaling AuditEvents.
//
// Err does not return any error returned from Close.
func (s *AuditStream) Err() error { return s.err }
// Event returns the most recent AuditEvent generated by a
// call to Next.
func (s *AuditStream) Event() AuditEvent { return s.event }
// Bytes returns the most recent raw AuditEvent content generated
// by a call to Next. It may not contain valid JSON.
//
// The underlying array may point to data that will be overwritten
// by a subsequent call to Next. It does no allocation.
func (s *AuditStream) Bytes() []byte { return s.scanner.Bytes() }
// Next advances the stream to the next AuditEvent, which will then
// be available through the Event and Bytes method. It returns false
// when the stream iteration stops - i.e. by reaching the end of the
// stream, closing the stream or in case of an error.
// After Next returns false, the Err method will return any error that
// occurred while iterating and parsing the stream.
func (s *AuditStream) Next() bool {
if s.err != nil || s.closed {
return false
}
// Iterate over the stream until we find a non-empty line.
for {
if !s.scanner.Scan() {
if !s.closed { // Once the stream is closed we ignore the error
s.err = s.scanner.Err()
}
return false
}
if len(s.scanner.Bytes()) != 0 {
break
}
}
if err := json.Unmarshal(s.scanner.Bytes(), &s.event); err != nil {
if !s.closed { // Once the stream is closed we ignore the error
s.err = err
}
return false
}
return true
}
// Close closes the underlying stream - i.e. the io.Reader if
// if implements io.Closer. After Close has been called once
// the Next method will return false.
func (s *AuditStream) Close() (err error) {
if s.closer != nil {
s.closed = true
err = s.closer.Close()
}
return err
}
// AuditEvent is the event type the KES server produces when it
// has handled a request right before responding to the client.
//
// When a clients subscribes to the KES server audit log it
// receives a stream of JSON-encoded audit events separated
// by a newline.
type AuditEvent struct {
// Time is the point in time when the
// audit event has been created.
Time time.Time `json:"time"`
// Request contains audit log information
// about the request received from a client.
Request AuditEventRequest `json:"request"`
// Response contains audit log information
// about the response sent to the client.
Response AuditEventResponse `json:"response"`
}
// String returns the AuditEvent's string representation
// which is valid JSON.
func (a *AuditEvent) String() string {
const format = `{"time":"%s","request":%s,"response":%s}`
return fmt.Sprintf(format, a.Time.Format(time.RFC3339), a.Request.String(), a.Response.String())
}
// AuditEventRequest contains the audit information
// about a request sent by a client to a KES server.
//
// In particular, it contains the identity of the
// client and other audit-related information.
type AuditEventRequest struct {
Path string `json:"path"`
Identity string `json:"identity"`
}
// String returns the AuditEventRequest's string representation
// which is valid JSON.
func (a *AuditEventRequest) String() string {
const format = `{"path":"%s","identity":"%s"}`
return fmt.Sprintf(format, a.Path, a.Identity)
}
// AuditEventResponse contains the audit information
// about a response sent to a client by a KES server.
//
// In particular, it contains the response status code
// and other audit-related information.
type AuditEventResponse struct {
StatusCode int `json:"code"`
Time time.Duration `json:"time"`
}
// String returns the AuditEventResponse's string
// representation which is valid JSON.
func (a *AuditEventResponse) String() string {
const format = `{"code":%d,"time":%d}`
return fmt.Sprintf(format, a.StatusCode, a.Time)
}