-
Notifications
You must be signed in to change notification settings - Fork 186
/
middleware.go
622 lines (537 loc) · 20.3 KB
/
middleware.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
package grpc_middleware
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/blang/semver/v4"
"github.com/gofrs/uuid"
errs "go.flipt.io/flipt/errors"
"go.flipt.io/flipt/internal/cache"
"go.flipt.io/flipt/internal/server/analytics"
"go.flipt.io/flipt/internal/server/audit"
"go.flipt.io/flipt/internal/server/authn"
"go.flipt.io/flipt/internal/server/metrics"
flipt "go.flipt.io/flipt/rpc/flipt"
fauth "go.flipt.io/flipt/rpc/flipt/auth"
"go.flipt.io/flipt/rpc/flipt/evaluation"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
)
// ValidationUnaryInterceptor validates incoming requests
func ValidationUnaryInterceptor(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
if v, ok := req.(flipt.Validator); ok {
if err := v.Validate(); err != nil {
return nil, err
}
}
return handler(ctx, req)
}
// ErrorUnaryInterceptor intercepts known errors and returns the appropriate GRPC status code
func ErrorUnaryInterceptor(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
resp, err = handler(ctx, req)
if err == nil {
return resp, nil
}
metrics.ErrorsTotal.Add(ctx, 1)
// given already a *status.Error then forward unchanged
if _, ok := status.FromError(err); ok {
return
}
if errors.Is(err, context.Canceled) {
err = status.Error(codes.Canceled, err.Error())
return
}
if errors.Is(err, context.DeadlineExceeded) {
err = status.Error(codes.DeadlineExceeded, err.Error())
return
}
code := codes.Internal
switch {
case errs.AsMatch[errs.ErrNotFound](err):
code = codes.NotFound
case errs.AsMatch[errs.ErrInvalid](err),
errs.AsMatch[errs.ErrValidation](err):
code = codes.InvalidArgument
case errs.AsMatch[errs.ErrUnauthenticated](err):
code = codes.Unauthenticated
}
err = status.Error(code, err.Error())
return
}
type RequestIdentifiable interface {
// SetRequestIDIfNotBlank attempts to set the provided ID on the instance
// If the ID was blank, it returns the ID provided to this call.
// If the ID was not blank, it returns the ID found on the instance.
SetRequestIDIfNotBlank(id string) string
}
type ResponseDurationRecordable interface {
// SetTimestamps records the start and end times on the target instance.
SetTimestamps(start, end time.Time)
}
// EvaluationUnaryInterceptor sets required request/response fields.
// Note: this should be added before any caching interceptor to ensure the request id/response fields are unique.
func EvaluationUnaryInterceptor(analyticsEnabled bool) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
startTime := time.Now().UTC()
// set request ID if not present
requestID := uuid.Must(uuid.NewV4()).String()
if r, ok := req.(RequestIdentifiable); ok {
requestID = r.SetRequestIDIfNotBlank(requestID)
resp, err = handler(ctx, req)
if err != nil {
return resp, err
}
// set request ID on response
if r, ok := resp.(RequestIdentifiable); ok {
_ = r.SetRequestIDIfNotBlank(requestID)
}
// record start, end, duration on response types
if r, ok := resp.(ResponseDurationRecordable); ok {
r.SetTimestamps(startTime, time.Now().UTC())
}
if analyticsEnabled {
span := trace.SpanFromContext(ctx)
switch r := resp.(type) {
case *evaluation.VariantEvaluationResponse:
// This "should" always be an evalution request under these circumstances.
if evaluationRequest, ok := req.(*evaluation.EvaluationRequest); ok {
var variantKey *string = nil
if r.GetVariantKey() != "" {
variantKey = &r.VariantKey
}
evaluationResponses := []*analytics.EvaluationResponse{
{
NamespaceKey: evaluationRequest.GetNamespaceKey(),
FlagKey: r.GetFlagKey(),
FlagType: evaluation.EvaluationFlagType_VARIANT_FLAG_TYPE.String(),
Match: &r.Match,
Reason: r.GetReason().String(),
Timestamp: r.GetTimestamp().AsTime(),
EvaluationValue: variantKey,
EntityId: evaluationRequest.EntityId,
},
}
if evaluationResponsesBytes, err := json.Marshal(evaluationResponses); err == nil {
keyValue := []attribute.KeyValue{
{
Key: "flipt.evaluation.response",
Value: attribute.StringValue(string(evaluationResponsesBytes)),
},
}
span.AddEvent("evaluation_response", trace.WithAttributes(keyValue...))
}
}
case *evaluation.BooleanEvaluationResponse:
if evaluationRequest, ok := req.(*evaluation.EvaluationRequest); ok {
evaluationValue := fmt.Sprint(r.GetEnabled())
evaluationResponses := []*analytics.EvaluationResponse{
{
NamespaceKey: evaluationRequest.GetNamespaceKey(),
FlagKey: r.GetFlagKey(),
FlagType: evaluation.EvaluationFlagType_BOOLEAN_FLAG_TYPE.String(),
Reason: r.GetReason().String(),
Timestamp: r.GetTimestamp().AsTime(),
Match: nil,
EvaluationValue: &evaluationValue,
EntityId: evaluationRequest.EntityId,
},
}
if evaluationResponsesBytes, err := json.Marshal(evaluationResponses); err == nil {
keyValue := []attribute.KeyValue{
{
Key: "flipt.evaluation.response",
Value: attribute.StringValue(string(evaluationResponsesBytes)),
},
}
span.AddEvent("evaluation_response", trace.WithAttributes(keyValue...))
}
}
case *evaluation.BatchEvaluationResponse:
if batchEvaluationRequest, ok := req.(*evaluation.BatchEvaluationRequest); ok {
evaluationResponses := make([]*analytics.EvaluationResponse, 0, len(r.GetResponses()))
for idx, response := range r.GetResponses() {
switch response.GetType() {
case evaluation.EvaluationResponseType_VARIANT_EVALUATION_RESPONSE_TYPE:
variantResponse := response.GetVariantResponse()
var variantKey *string = nil
if variantResponse.GetVariantKey() != "" {
variantKey = &variantResponse.VariantKey
}
evaluationResponses = append(evaluationResponses, &analytics.EvaluationResponse{
NamespaceKey: batchEvaluationRequest.Requests[idx].GetNamespaceKey(),
FlagKey: variantResponse.GetFlagKey(),
FlagType: evaluation.EvaluationFlagType_VARIANT_FLAG_TYPE.String(),
Match: &variantResponse.Match,
Reason: variantResponse.GetReason().String(),
Timestamp: variantResponse.Timestamp.AsTime(),
EvaluationValue: variantKey,
EntityId: batchEvaluationRequest.Requests[idx].EntityId,
})
case evaluation.EvaluationResponseType_BOOLEAN_EVALUATION_RESPONSE_TYPE:
booleanResponse := response.GetBooleanResponse()
evaluationValue := fmt.Sprint(booleanResponse.GetEnabled())
evaluationResponses = append(evaluationResponses, &analytics.EvaluationResponse{
NamespaceKey: batchEvaluationRequest.Requests[idx].GetNamespaceKey(),
FlagKey: booleanResponse.GetFlagKey(),
FlagType: evaluation.EvaluationFlagType_BOOLEAN_FLAG_TYPE.String(),
Reason: booleanResponse.GetReason().String(),
Timestamp: booleanResponse.Timestamp.AsTime(),
Match: nil,
EvaluationValue: &evaluationValue,
EntityId: batchEvaluationRequest.Requests[idx].EntityId,
})
}
}
if evaluationResponsesBytes, err := json.Marshal(evaluationResponses); err == nil {
keyValue := []attribute.KeyValue{
{
Key: "flipt.evaluation.response",
Value: attribute.StringValue(string(evaluationResponsesBytes)),
},
}
span.AddEvent("evaluation_response", trace.WithAttributes(keyValue...))
}
}
}
}
return resp, nil
}
return handler(ctx, req)
}
}
var (
legacyEvalCachePrefix evaluationCacheKey[*flipt.EvaluationRequest] = "ev1"
newEvalCachePrefix evaluationCacheKey[*evaluation.EvaluationRequest] = "ev2"
)
// CacheUnaryInterceptor caches the response of a request if the request is cacheable.
// TODO: we could clean this up by using generics in 1.18+ to avoid the type switch/duplicate code.
func CacheUnaryInterceptor(cache cache.Cacher, logger *zap.Logger) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
if cache == nil {
return handler(ctx, req)
}
switch r := req.(type) {
case *flipt.EvaluationRequest:
key, err := legacyEvalCachePrefix.Key(r)
if err != nil {
logger.Error("getting cache key", zap.Error(err))
return handler(ctx, req)
}
cached, ok, err := cache.Get(ctx, key)
if err != nil {
// if error, log and without cache
logger.Error("getting from cache", zap.Error(err))
return handler(ctx, req)
}
if ok {
resp := &flipt.EvaluationResponse{}
if err := proto.Unmarshal(cached, resp); err != nil {
logger.Error("unmarshalling from cache", zap.Error(err))
return handler(ctx, req)
}
logger.Debug("evaluate cache hit", zap.Stringer("response", resp))
return resp, nil
}
logger.Debug("evaluate cache miss")
resp, err := handler(ctx, req)
if err != nil {
return resp, err
}
// marshal response
data, merr := proto.Marshal(resp.(*flipt.EvaluationResponse))
if merr != nil {
logger.Error("marshalling for cache", zap.Error(err))
return resp, err
}
// set in cache
if cerr := cache.Set(ctx, key, data); cerr != nil {
logger.Error("setting in cache", zap.Error(err))
}
return resp, err
case *flipt.GetFlagRequest:
key := flagCacheKey(r.GetNamespaceKey(), r.GetKey())
cached, ok, err := cache.Get(ctx, key)
if err != nil {
// if error, log and continue without cache
logger.Error("getting from cache", zap.Error(err))
return handler(ctx, req)
}
if ok {
// if cached, return it
flag := &flipt.Flag{}
if err := proto.Unmarshal(cached, flag); err != nil {
logger.Error("unmarshalling from cache", zap.Error(err))
return handler(ctx, req)
}
logger.Debug("flag cache hit", zap.Stringer("flag", flag))
return flag, nil
}
logger.Debug("flag cache miss")
resp, err := handler(ctx, req)
if err != nil {
return nil, err
}
// marshal response
data, merr := proto.Marshal(resp.(*flipt.Flag))
if merr != nil {
logger.Error("marshalling for cache", zap.Error(err))
return resp, err
}
// set in cache
if cerr := cache.Set(ctx, key, data); cerr != nil {
logger.Error("setting in cache", zap.Error(err))
}
return resp, err
case *flipt.UpdateFlagRequest, *flipt.DeleteFlagRequest:
// need to do this assertion because the request type is not known in this block
keyer := r.(flagKeyer)
// delete from cache
if err := cache.Delete(ctx, flagCacheKey(keyer.GetNamespaceKey(), keyer.GetKey())); err != nil {
logger.Error("deleting from cache", zap.Error(err))
}
case *flipt.CreateVariantRequest, *flipt.UpdateVariantRequest, *flipt.DeleteVariantRequest:
// need to do this assertion because the request type is not known in this block
keyer := r.(variantFlagKeyger)
// delete from cache
if err := cache.Delete(ctx, flagCacheKey(keyer.GetNamespaceKey(), keyer.GetFlagKey())); err != nil {
logger.Error("deleting from cache", zap.Error(err))
}
case *evaluation.EvaluationRequest:
key, err := newEvalCachePrefix.Key(r)
if err != nil {
logger.Error("getting cache key", zap.Error(err))
return handler(ctx, req)
}
cached, ok, err := cache.Get(ctx, key)
if err != nil {
// if error, log and without cache
logger.Error("getting from cache", zap.Error(err))
return handler(ctx, req)
}
if ok {
resp := &evaluation.EvaluationResponse{}
if err := proto.Unmarshal(cached, resp); err != nil {
logger.Error("unmarshalling from cache", zap.Error(err))
return handler(ctx, req)
}
logger.Debug("evaluate cache hit", zap.Stringer("response", resp))
switch r := resp.Response.(type) {
case *evaluation.EvaluationResponse_VariantResponse:
return r.VariantResponse, nil
case *evaluation.EvaluationResponse_BooleanResponse:
return r.BooleanResponse, nil
default:
logger.Error("unexpected eval cache response type", zap.String("type", fmt.Sprintf("%T", resp.Response)))
}
return handler(ctx, req)
}
logger.Debug("evaluate cache miss")
resp, err := handler(ctx, req)
if err != nil {
return resp, err
}
evalResponse := &evaluation.EvaluationResponse{}
switch r := resp.(type) {
case *evaluation.VariantEvaluationResponse:
evalResponse.Type = evaluation.EvaluationResponseType_VARIANT_EVALUATION_RESPONSE_TYPE
evalResponse.Response = &evaluation.EvaluationResponse_VariantResponse{
VariantResponse: r,
}
case *evaluation.BooleanEvaluationResponse:
evalResponse.Type = evaluation.EvaluationResponseType_BOOLEAN_EVALUATION_RESPONSE_TYPE
evalResponse.Response = &evaluation.EvaluationResponse_BooleanResponse{
BooleanResponse: r,
}
}
// marshal response
data, merr := proto.Marshal(evalResponse)
if merr != nil {
logger.Error("marshalling for cache", zap.Error(err))
return resp, err
}
// set in cache
if cerr := cache.Set(ctx, key, data); cerr != nil {
logger.Error("setting in cache", zap.Error(err))
}
return resp, err
}
return handler(ctx, req)
}
}
// EventPairChecker is the middleware side contract for checking if an event pair exists.
type EventPairChecker interface {
Check(eventPair string) bool
}
// AuditUnaryInterceptor sends audit logs to configured sinks upon successful RPC requests for auditable events.
func AuditUnaryInterceptor(logger *zap.Logger, eventPairChecker EventPairChecker) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
resp, err := handler(ctx, req)
if err != nil {
return resp, err
}
actor := authn.ActorFromContext(ctx)
var event *audit.Event
defer func() {
if event != nil {
ts := string(event.Type)
as := string(event.Action)
eventPair := fmt.Sprintf("%s:%s", ts, as)
exists := eventPairChecker.Check(eventPair)
if exists {
span := trace.SpanFromContext(ctx)
span.AddEvent("event", trace.WithAttributes(event.DecodeToAttributes()...))
}
}
}()
// Delete request(s) have to be handled separately because they do not
// return the concrete type but rather an *empty.Empty response.
switch r := req.(type) {
case *flipt.DeleteFlagRequest:
event = audit.NewEvent(audit.FlagType, audit.Delete, actor, r)
case *flipt.DeleteVariantRequest:
event = audit.NewEvent(audit.VariantType, audit.Delete, actor, r)
case *flipt.DeleteSegmentRequest:
event = audit.NewEvent(audit.SegmentType, audit.Delete, actor, r)
case *flipt.DeleteDistributionRequest:
event = audit.NewEvent(audit.DistributionType, audit.Delete, actor, r)
case *flipt.DeleteConstraintRequest:
event = audit.NewEvent(audit.ConstraintType, audit.Delete, actor, r)
case *flipt.DeleteNamespaceRequest:
event = audit.NewEvent(audit.NamespaceType, audit.Delete, actor, r)
case *flipt.OrderRulesRequest:
event = audit.NewEvent(audit.RuleType, audit.Update, actor, r)
case *flipt.DeleteRuleRequest:
event = audit.NewEvent(audit.RuleType, audit.Delete, actor, r)
case *flipt.OrderRolloutsRequest:
event = audit.NewEvent(audit.RolloutType, audit.Update, actor, r)
case *flipt.DeleteRolloutRequest:
event = audit.NewEvent(audit.RolloutType, audit.Delete, actor, r)
}
// Short circuiting the middleware here since we have a non-nil event from
// detecting a delete.
if event != nil {
return resp, err
}
action := audit.GRPCMethodToAction(info.FullMethod)
switch r := resp.(type) {
case *flipt.Flag:
if action != "" {
event = audit.NewEvent(audit.FlagType, action, actor, audit.NewFlag(r))
}
case *flipt.Variant:
if action != "" {
event = audit.NewEvent(audit.VariantType, action, actor, audit.NewVariant(r))
}
case *flipt.Segment:
if action != "" {
event = audit.NewEvent(audit.SegmentType, action, actor, audit.NewSegment(r))
}
case *flipt.Distribution:
if action != "" {
event = audit.NewEvent(audit.DistributionType, action, actor, audit.NewDistribution(r))
}
case *flipt.Constraint:
if action != "" {
event = audit.NewEvent(audit.ConstraintType, action, actor, audit.NewConstraint(r))
}
case *flipt.Namespace:
if action != "" {
event = audit.NewEvent(audit.NamespaceType, action, actor, audit.NewNamespace(r))
}
case *flipt.Rollout:
if action != "" {
event = audit.NewEvent(audit.RolloutType, action, actor, audit.NewRollout(r))
}
case *flipt.Rule:
if action != "" {
event = audit.NewEvent(audit.RuleType, action, actor, audit.NewRule(r))
}
case *fauth.CreateTokenResponse:
event = audit.NewEvent(audit.TokenType, audit.Create, actor, r.Authentication.Metadata)
}
return resp, err
}
}
type namespaceKeyer interface {
GetNamespaceKey() string
}
type flagKeyer interface {
namespaceKeyer
GetKey() string
}
type variantFlagKeyger interface {
namespaceKeyer
GetFlagKey() string
}
func flagCacheKey(namespaceKey, key string) string {
// for backward compatibility
if namespaceKey != "" {
return fmt.Sprintf("f:%s:%s", namespaceKey, key)
}
return fmt.Sprintf("f:%s", key)
}
type evaluationRequest interface {
GetNamespaceKey() string
GetFlagKey() string
GetEntityId() string
GetContext() map[string]string
}
type evaluationCacheKey[T evaluationRequest] string
func (e evaluationCacheKey[T]) Key(r T) (string, error) {
out, err := json.Marshal(r.GetContext())
if err != nil {
return "", fmt.Errorf("marshalling req to json: %w", err)
}
// for backward compatibility
if r.GetNamespaceKey() != "" {
return fmt.Sprintf("%s:%s:%s:%s:%s", string(e), r.GetNamespaceKey(), r.GetFlagKey(), r.GetEntityId(), out), nil
}
return fmt.Sprintf("%s:%s:%s:%s", string(e), r.GetFlagKey(), r.GetEntityId(), out), nil
}
// x-flipt-accept-server-version represents the maximum version of the flipt server that the client can handle.
const fliptAcceptServerVersionHeaderKey = "x-flipt-accept-server-version"
type fliptServerVersionContextKey struct{}
// WithFliptAcceptServerVersion sets the flipt version in the context.
func WithFliptAcceptServerVersion(ctx context.Context, version semver.Version) context.Context {
return context.WithValue(ctx, fliptServerVersionContextKey{}, version)
}
// The last version that does not support the x-flipt-accept-server-version header.
var preFliptAcceptServerVersion = semver.MustParse("1.37.1")
// FliptAcceptServerVersionFromContext returns the flipt-accept-server-version from the context if it exists or the default version.
func FliptAcceptServerVersionFromContext(ctx context.Context) semver.Version {
v, ok := ctx.Value(fliptServerVersionContextKey{}).(semver.Version)
if !ok {
return preFliptAcceptServerVersion
}
return v
}
// FliptAcceptServerVersionUnaryInterceptor is a grpc client interceptor that sets the flipt-accept-server-version in the context if provided in the metadata/header.
func FliptAcceptServerVersionUnaryInterceptor(logger *zap.Logger) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return handler(ctx, req)
}
if fliptVersionHeader := md.Get(fliptAcceptServerVersionHeaderKey); len(fliptVersionHeader) > 0 {
version := fliptVersionHeader[0]
if version != "" {
cv, err := semver.ParseTolerant(version)
if err != nil {
logger.Warn("parsing x-flipt-accept-server-version header", zap.String("version", version), zap.Error(err))
return handler(ctx, req)
}
logger.Debug("x-flipt-accept-server-version header", zap.String("version", version))
ctx = WithFliptAcceptServerVersion(ctx, cv)
}
}
return handler(ctx, req)
}
}