2022-09-26 15:25:34 -05:00
|
|
|
package grpcserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2022-10-04 05:27:14 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
2022-09-26 15:25:34 -05:00
|
|
|
"github.com/grafana/grafana/pkg/registry"
|
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
2022-10-04 05:27:14 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/grpcserver/interceptors"
|
2022-09-26 15:25:34 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2022-10-04 05:27:14 -05:00
|
|
|
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
2022-09-26 15:25:34 -05:00
|
|
|
grpcAuth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Provider interface {
|
|
|
|
registry.BackgroundService
|
|
|
|
GetServer() *grpc.Server
|
2022-09-29 07:42:01 -05:00
|
|
|
GetAddress() string
|
2022-09-26 15:25:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type GPRCServerService struct {
|
2022-09-29 07:42:01 -05:00
|
|
|
cfg *setting.Cfg
|
|
|
|
logger log.Logger
|
|
|
|
server *grpc.Server
|
|
|
|
address string
|
2022-09-26 15:25:34 -05:00
|
|
|
}
|
|
|
|
|
2022-10-04 15:31:02 -05:00
|
|
|
func ProvideService(cfg *setting.Cfg, authenticator interceptors.Authenticator, tracer tracing.Tracer) (Provider, error) {
|
2022-09-26 15:25:34 -05:00
|
|
|
s := &GPRCServerService{
|
|
|
|
cfg: cfg,
|
|
|
|
logger: log.New("grpc-server"),
|
|
|
|
}
|
|
|
|
|
|
|
|
var opts []grpc.ServerOption
|
|
|
|
|
|
|
|
// Default auth is admin token check, but this can be overridden by
|
|
|
|
// services which implement ServiceAuthFuncOverride interface.
|
|
|
|
// See https://github.com/grpc-ecosystem/go-grpc-middleware/blob/master/auth/auth.go#L30.
|
|
|
|
opts = append(opts, []grpc.ServerOption{
|
2022-10-04 05:27:14 -05:00
|
|
|
grpc.UnaryInterceptor(
|
|
|
|
grpc_middleware.ChainUnaryServer(
|
|
|
|
grpcAuth.UnaryServerInterceptor(authenticator.Authenticate),
|
|
|
|
interceptors.TracingUnaryInterceptor(tracer),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
grpc.StreamInterceptor(
|
|
|
|
grpc_middleware.ChainStreamServer(
|
|
|
|
interceptors.TracingStreamInterceptor(tracer),
|
|
|
|
grpcAuth.StreamServerInterceptor(authenticator.Authenticate),
|
|
|
|
),
|
|
|
|
),
|
2022-09-26 15:25:34 -05:00
|
|
|
}...)
|
|
|
|
|
|
|
|
if s.cfg.GRPCServerTLSConfig != nil {
|
|
|
|
opts = append(opts, grpc.Creds(credentials.NewTLS(cfg.GRPCServerTLSConfig)))
|
|
|
|
}
|
|
|
|
|
2022-09-28 13:00:46 -05:00
|
|
|
s.server = grpc.NewServer(opts...)
|
2022-09-26 15:25:34 -05:00
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GPRCServerService) Run(ctx context.Context) error {
|
|
|
|
s.logger.Info("Running GRPC server", "address", s.cfg.GRPCServerAddress, "network", s.cfg.GRPCServerNetwork, "tls", s.cfg.GRPCServerTLSConfig != nil)
|
|
|
|
|
|
|
|
listener, err := net.Listen(s.cfg.GRPCServerNetwork, s.cfg.GRPCServerAddress)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GRPC server: failed to listen: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-09-29 07:42:01 -05:00
|
|
|
s.address = listener.Addr().String()
|
|
|
|
|
2022-09-26 15:25:34 -05:00
|
|
|
serveErr := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
s.logger.Info("GRPC server: starting")
|
|
|
|
err := s.server.Serve(listener)
|
|
|
|
if err != nil {
|
|
|
|
backend.Logger.Error("GRPC server: failed to serve", "err", err)
|
|
|
|
serveErr <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-serveErr:
|
|
|
|
backend.Logger.Error("GRPC server: failed to serve", "err", err)
|
|
|
|
return err
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
s.logger.Warn("GRPC server: shutting down")
|
|
|
|
s.server.Stop()
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GPRCServerService) IsDisabled() bool {
|
|
|
|
if s.cfg == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return !s.cfg.IsFeatureToggleEnabled(featuremgmt.FlagGrpcServer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GPRCServerService) GetServer() *grpc.Server {
|
|
|
|
return s.server
|
|
|
|
}
|
2022-09-29 07:42:01 -05:00
|
|
|
|
|
|
|
func (s *GPRCServerService) GetAddress() string {
|
|
|
|
return s.address
|
|
|
|
}
|