mirror of
https://github.com/grafana/grafana.git
synced 2024-12-27 09:21:35 -06:00
55aae79744
Co-authored-by: Todd Treece <todd.treece@grafana.com> Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
99 lines
2.7 KiB
Go
99 lines
2.7 KiB
Go
package grpcserver
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
"github.com/grafana/grafana/pkg/registry"
|
|
"github.com/grafana/grafana/pkg/services/apikey"
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
|
"github.com/grafana/grafana/pkg/services/user"
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
grpcAuth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials"
|
|
"google.golang.org/grpc/reflection"
|
|
)
|
|
|
|
type Provider interface {
|
|
registry.BackgroundService
|
|
GetServer() *grpc.Server
|
|
}
|
|
|
|
type GPRCServerService struct {
|
|
cfg *setting.Cfg
|
|
logger log.Logger
|
|
server *grpc.Server
|
|
}
|
|
|
|
func ProvideService(cfg *setting.Cfg, apiKey apikey.Service, userService user.Service) (Provider, error) {
|
|
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.
|
|
authenticator := newAuthenticator(apiKey, userService)
|
|
opts = append(opts, []grpc.ServerOption{
|
|
grpc.StreamInterceptor(grpcAuth.StreamServerInterceptor(authenticator.authenticate)),
|
|
grpc.UnaryInterceptor(grpcAuth.UnaryServerInterceptor(authenticator.authenticate)),
|
|
}...)
|
|
|
|
if s.cfg.GRPCServerTLSConfig != nil {
|
|
opts = append(opts, grpc.Creds(credentials.NewTLS(cfg.GRPCServerTLSConfig)))
|
|
}
|
|
|
|
grpcServer := grpc.NewServer(opts...)
|
|
reflection.Register(grpcServer)
|
|
s.server = grpcServer
|
|
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)
|
|
}
|
|
|
|
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
|
|
}
|