2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-07-13 15:33:48 +00:00
|
|
|
package external
|
2022-03-22 12:40:24 +00:00
|
|
|
|
|
|
|
import (
|
2022-08-24 16:31:38 +00:00
|
|
|
"time"
|
|
|
|
|
2022-10-11 22:00:32 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2022-03-22 12:40:24 +00:00
|
|
|
middleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
|
|
|
recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
|
|
|
|
"google.golang.org/grpc"
|
2022-10-28 20:34:41 +00:00
|
|
|
"google.golang.org/grpc/credentials"
|
2022-07-18 23:12:03 +00:00
|
|
|
"google.golang.org/grpc/keepalive"
|
2022-03-22 12:40:24 +00:00
|
|
|
|
2022-12-13 20:09:55 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/rate"
|
2022-07-13 15:33:48 +00:00
|
|
|
agentmiddleware "github.com/hashicorp/consul/agent/grpc-middleware"
|
2022-10-28 20:34:41 +00:00
|
|
|
"github.com/hashicorp/consul/tlsutil"
|
2022-03-22 12:40:24 +00:00
|
|
|
)
|
|
|
|
|
2022-10-11 22:00:32 +00:00
|
|
|
var (
|
|
|
|
metricsLabels = []metrics.Label{{
|
|
|
|
Name: "server_type",
|
|
|
|
Value: "external",
|
|
|
|
}}
|
|
|
|
)
|
|
|
|
|
2022-07-13 15:33:48 +00:00
|
|
|
// NewServer constructs a gRPC server for the external gRPC port, to which
|
2022-03-22 12:40:24 +00:00
|
|
|
// handlers can be registered.
|
2022-12-13 20:09:55 +00:00
|
|
|
func NewServer(logger agentmiddleware.Logger, metricsObj *metrics.Metrics, tls *tlsutil.Configurator, limiter rate.RequestLimitsHandler) *grpc.Server {
|
2022-10-11 22:00:32 +00:00
|
|
|
if metricsObj == nil {
|
|
|
|
metricsObj = metrics.Default()
|
|
|
|
}
|
2022-03-22 12:40:24 +00:00
|
|
|
recoveryOpts := agentmiddleware.PanicHandlerMiddlewareOpts(logger)
|
|
|
|
|
2022-10-28 20:34:41 +00:00
|
|
|
unaryInterceptors := []grpc.UnaryServerInterceptor{
|
|
|
|
// Add middlware interceptors to recover in case of panics.
|
|
|
|
recovery.UnaryServerInterceptor(recoveryOpts...),
|
|
|
|
}
|
|
|
|
streamInterceptors := []grpc.StreamServerInterceptor{
|
|
|
|
// Add middlware interceptors to recover in case of panics.
|
|
|
|
recovery.StreamServerInterceptor(recoveryOpts...),
|
|
|
|
agentmiddleware.NewActiveStreamCounter(metricsObj, metricsLabels).Intercept,
|
|
|
|
}
|
|
|
|
|
|
|
|
if tls != nil {
|
|
|
|
// Attach TLS middleware if TLS is provided.
|
|
|
|
authInterceptor := agentmiddleware.AuthInterceptor{TLS: tls, Logger: logger}
|
|
|
|
unaryInterceptors = append(unaryInterceptors, authInterceptor.InterceptUnary)
|
|
|
|
streamInterceptors = append(streamInterceptors, authInterceptor.InterceptStream)
|
|
|
|
}
|
2022-03-22 12:40:24 +00:00
|
|
|
opts := []grpc.ServerOption{
|
|
|
|
grpc.MaxConcurrentStreams(2048),
|
2022-10-13 20:46:51 +00:00
|
|
|
grpc.MaxRecvMsgSize(50 * 1024 * 1024),
|
2023-01-04 16:07:02 +00:00
|
|
|
grpc.InTapHandle(agentmiddleware.ServerRateLimiterMiddleware(limiter, agentmiddleware.NewPanicHandler(logger), logger)),
|
2022-10-11 22:00:32 +00:00
|
|
|
grpc.StatsHandler(agentmiddleware.NewStatsHandler(metricsObj, metricsLabels)),
|
2022-10-28 20:34:41 +00:00
|
|
|
middleware.WithUnaryServerChain(unaryInterceptors...),
|
|
|
|
middleware.WithStreamServerChain(streamInterceptors...),
|
2022-07-18 23:12:03 +00:00
|
|
|
grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{
|
|
|
|
// This must be less than the keealive.ClientParameters Time setting, otherwise
|
|
|
|
// the server will disconnect the client for sending too many keepalive pings.
|
|
|
|
// Currently the client param is set to 30s.
|
|
|
|
MinTime: 15 * time.Second,
|
|
|
|
}),
|
2022-03-22 12:40:24 +00:00
|
|
|
}
|
2022-10-28 20:34:41 +00:00
|
|
|
|
|
|
|
if tls != nil {
|
|
|
|
// Attach TLS credentials, if provided.
|
|
|
|
tlsCreds := agentmiddleware.NewOptionalTransportCredentials(
|
|
|
|
credentials.NewTLS(tls.IncomingGRPCConfig()),
|
|
|
|
logger)
|
|
|
|
opts = append(opts, grpc.Creds(tlsCreds))
|
|
|
|
}
|
2022-03-22 12:40:24 +00:00
|
|
|
return grpc.NewServer(opts...)
|
|
|
|
}
|