forked from minio/kms-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
267 lines (239 loc) · 6.07 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
// Copyright 2023 - 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 (
"encoding/json"
"errors"
"io"
"net"
"time"
)
// ErrorEvent is the event a KES server generates when
// it encounters and logs an error.
type ErrorEvent struct {
Message string // The logged error message
}
// NewErrorStream returns a new ErrorStream that
// reads from r.
func NewErrorStream(r io.Reader) *ErrorStream {
s := &ErrorStream{
decoder: json.NewDecoder(r),
}
if closer, ok := r.(io.Closer); ok {
s.closer = closer
}
return s
}
// ErrorStream iterates over a stream of ErrorEvents.
// Close the ErrorStream to release associated resources.
type ErrorStream struct {
decoder *json.Decoder
closer io.Closer
event ErrorEvent
err error
closed bool
}
// Event returns the most recent ErrorEvent, generated by Next.
func (s *ErrorStream) Event() ErrorEvent { return s.event }
// Message returns the current error message or the ErrorEvent.
// It is a short-hand for Event().Message.
func (s *ErrorStream) Message() string { return s.event.Message }
// Next advances the stream to the next ErrorEvent and
// returns true if there is another one. It returns
// false if there are no more ErrorEvents or when
// the ErrorStream encountered an error.
func (s *ErrorStream) Next() bool {
type Response struct {
Message string `json:"message"`
}
if s.err != nil || s.closed {
return false
}
var resp Response
if err := s.decoder.Decode(&resp); err != nil {
if errors.Is(err, io.EOF) {
s.err = s.Close()
} else {
s.err = err
}
return false
}
s.event = ErrorEvent(resp)
return true
}
// WriteTo writes the entire ErrorEvent stream to w.
// It returns the number of bytes written to w and
// the first error encounterred, if any.
func (s *ErrorStream) WriteTo(w io.Writer) (int64, error) {
type Response struct {
Message string `json:"message"`
}
if s.err != nil || s.closed {
return 0, s.err
}
cw := countWriter{W: w}
encoder := json.NewEncoder(&cw)
for {
var resp Response
if err := s.decoder.Decode(&resp); err != nil {
if errors.Is(err, io.EOF) {
s.err = s.Close()
} else {
s.err = err
}
return cw.N, s.err
}
if err := encoder.Encode(resp); err != nil {
s.err = err
return cw.N, err
}
}
}
// Close closes the ErrorStream and releases
// any associated resources.
func (s *ErrorStream) Close() error {
if !s.closed {
s.closed = true
if s.closer != nil {
err := s.closer.Close()
if s.err == nil {
s.err = err
}
return err
}
}
return s.err
}
// AuditEvent is the event a KES server generates when
// it response to a request.
type AuditEvent struct {
Timestamp time.Time // The point in time when the KES server received the request
APIPath string // The API called by the client. May contain API arguments
ClientIP net.IP // The client's IP address
ClientIdentity Identity // The client's KES identity
StatusCode int // The response status code sent to the client
ResponseTime time.Duration // Time it took to process the request
}
// NewAuditStream returns a new AuditStream that
// reads from r.
func NewAuditStream(r io.Reader) *AuditStream {
s := &AuditStream{
decoder: json.NewDecoder(r),
}
if closer, ok := r.(io.Closer); ok {
s.closer = closer
}
return s
}
// AuditStream iterates over a stream of AuditEvents.
// Close the AuditStream to release associated resources.
type AuditStream struct {
decoder *json.Decoder
closer io.Closer
event AuditEvent
err error
closed bool
}
// Event returns the most recent AuditEvent, generated by Next.
func (s *AuditStream) Event() AuditEvent { return s.event }
// Next advances the stream to the next AuditEvent and
// returns true if there is another one. It returns
// false if there are no more AuditEvents or when the
// AuditStream encountered an error.
func (s *AuditStream) Next() bool {
type Response struct {
Timestamp time.Time `json:"time"`
Request struct {
IP net.IP `json:"ip"`
APIPath string `json:"path"`
Identity Identity `json:"identity"`
} `json:"request"`
Response struct {
StatusCode int `json:"code"`
Time time.Duration `json:"time"`
} `json:"response"`
}
if s.closed || s.err != nil {
return false
}
var resp Response
if err := s.decoder.Decode(&resp); err != nil {
if errors.Is(err, io.EOF) {
s.err = s.Close()
} else {
s.err = err
}
return false
}
s.event = AuditEvent{
Timestamp: resp.Timestamp,
APIPath: resp.Request.APIPath,
ClientIP: resp.Request.IP,
ClientIdentity: resp.Request.Identity,
StatusCode: resp.Response.StatusCode,
ResponseTime: resp.Response.Time,
}
return true
}
// WriteTo writes the entire AuditEvent stream to w.
// It returns the number of bytes written to w and
// the first error encountered, if any.
func (s *AuditStream) WriteTo(w io.Writer) (int64, error) {
type Response struct {
Timestamp time.Time `json:"time"`
Request struct {
IP net.IP `json:"ip"`
APIPath string `json:"path"`
Identity Identity `json:"identity"`
} `json:"request"`
Response struct {
StatusCode int `json:"code"`
Time time.Duration `json:"time"`
} `json:"response"`
}
if s.err != nil || s.closed {
return 0, s.err
}
cw := countWriter{W: w}
encoder := json.NewEncoder(&cw)
for {
var resp Response
if err := s.decoder.Decode(&resp); err != nil {
if errors.Is(err, io.EOF) {
s.err = s.Close()
} else {
s.err = err
}
return cw.N, s.err
}
if err := encoder.Encode(resp); err != nil {
s.err = err
return cw.N, err
}
}
}
// Close closes the AuditStream and releases
// any associated resources.
func (s *AuditStream) Close() (err error) {
if !s.closed {
s.closed = true
if s.closer != nil {
err := s.closer.Close()
if s.err == nil {
s.err = err
}
return err
}
}
return s.err
}
type countWriter struct {
W io.Writer
N int64
}
func (w *countWriter) Write(p []byte) (int, error) {
n, err := w.W.Write(p)
w.N += int64(n)
return n, err
}