-
Notifications
You must be signed in to change notification settings - Fork 289
/
context.go
366 lines (343 loc) · 10.8 KB
/
context.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
package event
import (
"context"
"fmt"
"os"
"runtime"
"strings"
"time"
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/go-hclog"
)
type key int
const cancelledSendTimeout = 3 * time.Second
const (
eventerKey key = iota
requestInfoKey
)
// NewEventerContext will return a context containing a value of the provided Eventer
func NewEventerContext(ctx context.Context, eventer *Eventer) (context.Context, error) {
const op = "event.NewEventerContext"
if ctx == nil {
return nil, fmt.Errorf("%s: missing context: %w", op, ErrInvalidParameter)
}
if eventer == nil {
return nil, fmt.Errorf("%s: missing eventer: %w", op, ErrInvalidParameter)
}
return context.WithValue(ctx, eventerKey, eventer), nil
}
// EventerFromContext attempts to get the eventer value from the context provided
func EventerFromContext(ctx context.Context) (*Eventer, bool) {
if ctx == nil {
return nil, false
}
eventer, ok := ctx.Value(eventerKey).(*Eventer)
return eventer, ok
}
// NewRequestInfoContext will return a context containing a value for the
// provided RequestInfo
func NewRequestInfoContext(ctx context.Context, info *RequestInfo) (context.Context, error) {
const op = "event.NewRequestInfoContext"
if ctx == nil {
return nil, fmt.Errorf("%s: missing context: %w", op, ErrInvalidParameter)
}
if info == nil {
return nil, fmt.Errorf("%s: missing request info: %w", op, ErrInvalidParameter)
}
if info.Id == "" {
return nil, fmt.Errorf("%s: missing request info id: %w", op, ErrInvalidParameter)
}
if info.EventId == "" {
return nil, fmt.Errorf("%s: missing request info event id: %w", op, ErrInvalidParameter)
}
return context.WithValue(ctx, requestInfoKey, info), nil
}
// RequestInfoFromContext attempts to get the RequestInfo value from the context
// provided
func RequestInfoFromContext(ctx context.Context) (*RequestInfo, bool) {
if ctx == nil {
return nil, false
}
reqInfo, ok := ctx.Value(requestInfoKey).(*RequestInfo)
return reqInfo, ok
}
// WriteObservation will write an observation event. It will first check the
// ctx for an eventer, then try event.SysEventer() and if no eventer can be
// found an error is returned.
//
// At least one and any combination of the supported options may be used:
// WithHeader, WithDetails, WithId, WithFlush and WithRequestInfo. All other
// options are ignored.
func WriteObservation(ctx context.Context, caller Op, opt ...Option) error {
const op = "event.WriteObservation"
if ctx == nil {
return fmt.Errorf("%s: missing context: %w", op, ErrInvalidParameter)
}
if caller == "" {
return fmt.Errorf("%s: missing operation: %w", op, ErrInvalidParameter)
}
eventer, ok := EventerFromContext(ctx)
if !ok {
eventer = SysEventer()
if eventer == nil {
return fmt.Errorf("%s: missing both context and system eventer: %w", op, ErrInvalidParameter)
}
}
opts := getOpts(opt...)
if opts.withDetails == nil && opts.withHeader == nil && !opts.withFlush {
return fmt.Errorf("%s: specify either header or details options for an event payload: %w", op, ErrInvalidParameter)
}
if opts.withRequestInfo == nil {
var err error
if opt, err = addCtxOptions(ctx, opt...); err != nil {
return fmt.Errorf("%s: %w", op, err)
}
}
e, err := newObservation(caller, opt...)
if err != nil {
return fmt.Errorf("%s: %w", op, err)
}
sendCtx, sendCancel := newSendCtx(ctx)
if sendCancel != nil {
defer sendCancel()
}
if err := eventer.writeObservation(sendCtx, e); err != nil {
return fmt.Errorf("%s: %w", op, err)
}
return nil
}
// WriteError will write an error event. It will first check the
// ctx for an eventer, then try event.SysEventer() and if no eventer can be
// found an hclog.Logger will be created and used.
//
// The options WithInfoMsg, WithInfo, WithId and WithRequestInfo are supported
// and all other options are ignored.
func WriteError(ctx context.Context, caller Op, e error, opt ...Option) {
const op = "event.WriteError"
// EventerFromContext will handle a nil ctx appropriately. If e or caller is
// missing, newError(...) will handle them appropriately.
eventer, ok := EventerFromContext(ctx)
if !ok {
eventer = SysEventer()
if eventer == nil {
logger := hclog.New(nil)
logger.Error(fmt.Sprintf("%s: no eventer available to write error: %v", op, e))
return
}
}
opts := getOpts(opt...)
if opts.withRequestInfo == nil {
var err error
if opt, err = addCtxOptions(ctx, opt...); err != nil {
eventer.logger.Error(fmt.Sprintf("%s: %v", op, err))
eventer.logger.Error(fmt.Sprintf("%s: unable to process context options to write error: %v", op, e))
return
}
}
ev, err := newError(caller, e, opt...)
if err != nil {
eventer.logger.Error(fmt.Sprintf("%s: %v", op, err))
eventer.logger.Error(fmt.Sprintf("%s: unable to create new error to write error: %v", op, e))
return
}
sendCtx, sendCancel := newSendCtx(ctx)
if sendCancel != nil {
defer sendCancel()
}
if err := eventer.writeError(sendCtx, ev); err != nil {
eventer.logger.Error(fmt.Sprintf("%s: %v", op, err))
eventer.logger.Error(fmt.Sprintf("%s: unable to write error: %v", op, e))
return
}
}
// WriteAudit will write an audit event. It will first check the ctx for an
// eventer, then try event.SysEventer() and if no eventer can be found an error
// is returned.
//
// At least one and any combination of the supported options may be used:
// WithRequest, WithResponse, WithAuth, WithId, WithFlush and WithRequestInfo.
// All other options are ignored.
func WriteAudit(ctx context.Context, caller Op, opt ...Option) error {
// TODO (jimlambrt) 6/2021: remove this feature flag envvar when events are
// generally available.
if !strings.EqualFold(os.Getenv(globals.BOUNDARY_DEVELOPER_ENABLE_EVENTS), "true") {
return nil
}
const op = "event.WriteAudit"
if ctx == nil {
return fmt.Errorf("%s: missing context: %w", op, ErrInvalidParameter)
}
if caller == "" {
return fmt.Errorf("%s: missing operation: %w", op, ErrInvalidParameter)
}
eventer, ok := EventerFromContext(ctx)
if !ok {
eventer = SysEventer()
if eventer == nil {
return fmt.Errorf("%s: missing both context and system eventer: %w", op, ErrInvalidParameter)
}
}
opts := getOpts(opt...)
if opts.withRequestInfo == nil {
var err error
if opt, err = addCtxOptions(ctx, opt...); err != nil {
return fmt.Errorf("%s: %w", op, err)
}
}
e, err := newAudit(caller, opt...)
if err != nil {
return fmt.Errorf("%s: %w", op, err)
}
sendCtx, sendCancel := newSendCtx(ctx)
if sendCancel != nil {
defer sendCancel()
}
if err := eventer.writeAudit(sendCtx, e); err != nil {
return fmt.Errorf("%s: %w", op, err)
}
return nil
}
func addCtxOptions(ctx context.Context, opt ...Option) ([]Option, error) {
const op = "event.addCtxOptions"
opts := getOpts(opt...)
retOpts := make([]Option, 0, len(opt))
retOpts = append(retOpts, opt...)
if opts.withRequestInfo == nil {
reqInfo, ok := RequestInfoFromContext(ctx)
if !ok {
// there's no RequestInfo, so there's no id associated with the
// event and we'll generate one and flush the event
// since there will never be another with the same id
id, err := NewId(IdPrefix)
if err != nil {
return nil, fmt.Errorf("%s: %w", op, err)
}
retOpts = append(retOpts, WithId(id))
if !opts.withFlush {
retOpts = append(retOpts, WithFlush())
}
return retOpts, nil
}
retOpts = append(retOpts, WithRequestInfo(reqInfo))
switch reqInfo.EventId {
case "":
// there's no RequestInfo.EventId associated with the observation,
// so we'll generate one and flush the observation since there will
// never be another with the same id
id, err := NewId("e")
if err != nil {
return nil, fmt.Errorf("%s: %w", op, err)
}
retOpts = append(retOpts, WithId(id))
if !opts.withFlush {
retOpts = append(retOpts, WithFlush())
}
return retOpts, nil
default:
retOpts = append(retOpts, WithId(reqInfo.EventId))
}
}
return retOpts, nil
}
// WriteSysEvent will write a sysevent using the eventer from
// event.SysEventer() if no eventer can be found an hclog.Logger will be created
// and used. The args are and optional set of key/value pairs about the event.
//
// This function should never be used when sending events while
// handling API requests.
func WriteSysEvent(ctx context.Context, caller Op, msg string, args ...interface{}) {
const op = "event.WriteSysEvent"
info := ConvertArgs(args...)
if msg == "" && info == nil {
return
}
if info == nil {
info = make(map[string]interface{}, 1)
}
info[msgField] = msg
if caller == "" {
pc, _, _, ok := runtime.Caller(1)
details := runtime.FuncForPC(pc)
if ok && details != nil {
caller = Op(details.Name())
} else {
caller = "unknown operation"
}
}
eventer, ok := EventerFromContext(ctx)
if !ok {
eventer = SysEventer()
if eventer == nil {
logger := hclog.New(nil)
logger.Error(fmt.Sprintf("%s: no eventer available to write sysevent: (%s) %+v", op, caller, info))
return
}
}
id, err := NewId(string(SystemType))
if err != nil {
eventer.logger.Error(fmt.Sprintf("%s: %v", op, err))
eventer.logger.Error(fmt.Sprintf("%s: unable to generate id while writing sysevent: (%s) %+v", op, caller, info))
}
e := &sysEvent{
Id: Id(id),
Version: sysVersion,
Op: caller,
Data: info,
}
sendCtx, sendCancel := newSendCtx(ctx)
if sendCancel != nil {
defer sendCancel()
}
if err := eventer.writeSysEvent(sendCtx, e); err != nil {
eventer.logger.Error(fmt.Sprintf("%s: %v", op, err))
eventer.logger.Error(fmt.Sprintf("%s: unable to write sysevent: (%s) %+v", op, caller, e))
return
}
}
func newSendCtx(ctx context.Context) (context.Context, context.CancelFunc) {
var sendCtx context.Context
var sendCancel context.CancelFunc
switch {
case ctx == nil:
return context.Background(), nil
case ctx.Err() == context.Canceled:
sendCtx, sendCancel = context.WithTimeout(context.Background(), cancelledSendTimeout)
info, ok := RequestInfoFromContext(ctx)
if ok {
reqCtx, err := NewRequestInfoContext(sendCtx, info)
if err == nil {
sendCtx = reqCtx
}
}
default:
sendCtx = ctx
}
return sendCtx, sendCancel
}
// MissingKey defines a key to be used as the "missing key" when ConvertArgs has
// an odd number of args (it's missing a key in its key/value pairs)
const MissingKey = "EXTRA_VALUE_AT_END"
// ConvertArgs will convert the key/value pair args to a map. If the args
// provided are an odd number (they're missing a key in their key/value pairs)
// then MissingKey is used to the missing key.
func ConvertArgs(args ...interface{}) map[string]interface{} {
if len(args) == 0 {
return nil
}
if len(args)%2 != 0 {
extra := args[len(args)-1]
args = append(args[:len(args)-1], MissingKey, extra)
}
m := map[string]interface{}{}
for i := 0; i < len(args); i = i + 2 {
var key string
switch st := args[i].(type) {
case string:
key = st
default:
key = fmt.Sprintf("%v", st)
}
m[key] = args[i+1]
}
return m
}