ClusterUnaryServerInterceptor checks if the incoming gRPC metadata contains any cluster label and if so, checks if the latter corresponds to one of the given cluster labels. If it is the case, the request is further propagated. If empty cluster labels or nil logger are provided, ClusterUnaryServerIn
(clusters []string, softValidation bool, invalidClusterRequests *prometheus.CounterVec, logger log.Logger)
| 74 | // If the softValidation parameter is true, errors related to the cluster label validation are logged, but not returned. |
| 75 | // Otherwise, an error is returned. |
| 76 | func ClusterUnaryServerInterceptor(clusters []string, softValidation bool, invalidClusterRequests *prometheus.CounterVec, logger log.Logger) grpc.UnaryServerInterceptor { |
| 77 | validateClusterServerInterceptorInputParameters(clusters, logger) |
| 78 | return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { |
| 79 | // We skip the gRPC health check. |
| 80 | if _, ok := info.Server.(healthpb.HealthServer); ok { |
| 81 | return handler(ctx, req) |
| 82 | } |
| 83 | |
| 84 | if err := checkClusterFromIncomingContext(ctx, info.FullMethod, clusters, softValidation, invalidClusterRequests, logger); err != nil { |
| 85 | stat := grpcutil.Status(codes.FailedPrecondition, err.Error(), &grpcutil.ErrorDetails{Cause: grpcutil.WRONG_CLUSTER_VALIDATION_LABEL}) |
| 86 | return nil, stat.Err() |
| 87 | } |
| 88 | return handler(ctx, req) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func validateClusterServerInterceptorInputParameters(clusters []string, logger log.Logger) { |
| 93 | if len(clusters) == 0 { |