2017-07-13 03:45:53 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
2018-01-16 22:30:10 +00:00
|
|
|
"context"
|
2017-07-13 03:45:53 +00:00
|
|
|
"net"
|
2021-06-07 05:55:27 +00:00
|
|
|
"strings"
|
2017-07-13 03:45:53 +00:00
|
|
|
|
2020-04-23 00:10:31 +00:00
|
|
|
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
2018-02-02 00:05:24 +00:00
|
|
|
"github.com/moby/buildkit/identity"
|
2020-04-23 00:10:31 +00:00
|
|
|
"github.com/moby/buildkit/util/grpcerrors"
|
2017-07-13 03:45:53 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-06-07 04:57:58 +00:00
|
|
|
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
|
|
|
|
"go.opentelemetry.io/otel/propagation"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2017-07-13 03:45:53 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/health"
|
|
|
|
"google.golang.org/grpc/health/grpc_health_v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-07-14 18:05:54 +00:00
|
|
|
headerSessionID = "X-Docker-Expose-Session-Uuid"
|
2017-07-13 03:45:53 +00:00
|
|
|
headerSessionName = "X-Docker-Expose-Session-Name"
|
|
|
|
headerSessionSharedKey = "X-Docker-Expose-Session-Sharedkey"
|
|
|
|
headerSessionMethod = "X-Docker-Expose-Session-Grpc-Method"
|
|
|
|
)
|
|
|
|
|
2021-06-07 04:57:58 +00:00
|
|
|
var propagators = propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})
|
|
|
|
|
2017-07-13 03:45:53 +00:00
|
|
|
// Dialer returns a connection that can be used by the session
|
|
|
|
type Dialer func(ctx context.Context, proto string, meta map[string][]string) (net.Conn, error)
|
|
|
|
|
2018-11-17 23:10:57 +00:00
|
|
|
// Attachable defines a feature that can be exposed on a session
|
2017-07-13 03:45:53 +00:00
|
|
|
type Attachable interface {
|
|
|
|
Register(*grpc.Server)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Session is a long running connection between client and a daemon
|
|
|
|
type Session struct {
|
2017-07-14 18:05:54 +00:00
|
|
|
id string
|
2017-07-13 03:45:53 +00:00
|
|
|
name string
|
|
|
|
sharedKey string
|
|
|
|
ctx context.Context
|
|
|
|
cancelCtx func()
|
|
|
|
done chan struct{}
|
|
|
|
grpcServer *grpc.Server
|
2018-01-06 16:54:10 +00:00
|
|
|
conn net.Conn
|
2017-07-13 03:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSession returns a new long running session
|
2018-01-06 16:54:10 +00:00
|
|
|
func NewSession(ctx context.Context, name, sharedKey string) (*Session, error) {
|
2018-02-02 00:05:24 +00:00
|
|
|
id := identity.NewID()
|
2018-01-06 16:54:10 +00:00
|
|
|
|
2020-04-23 00:10:31 +00:00
|
|
|
var unary []grpc.UnaryServerInterceptor
|
|
|
|
var stream []grpc.StreamServerInterceptor
|
|
|
|
|
2018-01-06 16:54:10 +00:00
|
|
|
serverOpts := []grpc.ServerOption{}
|
2021-06-07 04:57:58 +00:00
|
|
|
|
|
|
|
if span := trace.SpanFromContext(ctx); span != nil {
|
2018-01-06 16:54:10 +00:00
|
|
|
tracer := span.Tracer()
|
2021-06-07 05:55:27 +00:00
|
|
|
unary = append(unary, filterServer(otelgrpc.UnaryServerInterceptor(otelgrpc.WithTracerProvider(constTracerProvider{tracer: tracer}), otelgrpc.WithPropagators(propagators))))
|
2021-06-07 04:57:58 +00:00
|
|
|
stream = append(stream, otelgrpc.StreamServerInterceptor(otelgrpc.WithTracerProvider(constTracerProvider{tracer: tracer}), otelgrpc.WithPropagators(propagators)))
|
2020-04-23 00:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unary = append(unary, grpcerrors.UnaryServerInterceptor)
|
|
|
|
stream = append(stream, grpcerrors.StreamServerInterceptor)
|
|
|
|
|
|
|
|
if len(unary) == 1 {
|
|
|
|
serverOpts = append(serverOpts, grpc.UnaryInterceptor(unary[0]))
|
|
|
|
} else if len(unary) > 1 {
|
|
|
|
serverOpts = append(serverOpts, grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(unary...)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(stream) == 1 {
|
|
|
|
serverOpts = append(serverOpts, grpc.StreamInterceptor(stream[0]))
|
|
|
|
} else if len(stream) > 1 {
|
|
|
|
serverOpts = append(serverOpts, grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(stream...)))
|
2018-01-06 16:54:10 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 03:45:53 +00:00
|
|
|
s := &Session{
|
2017-07-14 18:05:54 +00:00
|
|
|
id: id,
|
2017-07-13 03:45:53 +00:00
|
|
|
name: name,
|
|
|
|
sharedKey: sharedKey,
|
2018-01-06 16:54:10 +00:00
|
|
|
grpcServer: grpc.NewServer(serverOpts...),
|
2017-07-13 03:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
grpc_health_v1.RegisterHealthServer(s.grpcServer, health.NewServer())
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2018-11-17 23:10:57 +00:00
|
|
|
// Allow enables a given service to be reachable through the grpc session
|
2017-07-13 03:45:53 +00:00
|
|
|
func (s *Session) Allow(a Attachable) {
|
|
|
|
a.Register(s.grpcServer)
|
|
|
|
}
|
|
|
|
|
2017-07-14 18:05:54 +00:00
|
|
|
// ID returns unique identifier for the session
|
|
|
|
func (s *Session) ID() string {
|
|
|
|
return s.id
|
2017-07-13 03:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run activates the session
|
|
|
|
func (s *Session) Run(ctx context.Context, dialer Dialer) error {
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
s.cancelCtx = cancel
|
|
|
|
s.done = make(chan struct{})
|
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
defer close(s.done)
|
|
|
|
|
|
|
|
meta := make(map[string][]string)
|
2017-07-14 18:05:54 +00:00
|
|
|
meta[headerSessionID] = []string{s.id}
|
2017-07-13 03:45:53 +00:00
|
|
|
meta[headerSessionName] = []string{s.name}
|
|
|
|
meta[headerSessionSharedKey] = []string{s.sharedKey}
|
|
|
|
|
|
|
|
for name, svc := range s.grpcServer.GetServiceInfo() {
|
|
|
|
for _, method := range svc.Methods {
|
|
|
|
meta[headerSessionMethod] = append(meta[headerSessionMethod], MethodURL(name, method.Name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
conn, err := dialer(ctx, "h2c", meta)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to dial gRPC")
|
|
|
|
}
|
2018-01-06 16:54:10 +00:00
|
|
|
s.conn = conn
|
2017-07-13 03:45:53 +00:00
|
|
|
serve(ctx, s.grpcServer, conn)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the session
|
|
|
|
func (s *Session) Close() error {
|
|
|
|
if s.cancelCtx != nil && s.done != nil {
|
2018-01-06 16:54:10 +00:00
|
|
|
if s.conn != nil {
|
|
|
|
s.conn.Close()
|
|
|
|
}
|
2017-07-11 17:12:12 +00:00
|
|
|
s.grpcServer.Stop()
|
2017-07-13 03:45:53 +00:00
|
|
|
<-s.done
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) context() context.Context {
|
|
|
|
return s.ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) closed() bool {
|
|
|
|
select {
|
|
|
|
case <-s.context().Done():
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MethodURL returns a gRPC method URL for service and method name
|
|
|
|
func MethodURL(s, m string) string {
|
|
|
|
return "/" + s + "/" + m
|
|
|
|
}
|
2018-01-07 01:17:45 +00:00
|
|
|
|
2021-06-07 05:55:27 +00:00
|
|
|
// updates needed in opentelemetry-contrib to avoid this
|
|
|
|
func filterServer(intercept grpc.UnaryServerInterceptor) grpc.UnaryServerInterceptor {
|
|
|
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
|
|
|
if strings.HasSuffix(info.FullMethod, "Health/Check") {
|
|
|
|
return handler(ctx, req)
|
|
|
|
}
|
|
|
|
return intercept(ctx, req, info, handler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterClient(intercept grpc.UnaryClientInterceptor) grpc.UnaryClientInterceptor {
|
|
|
|
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
|
|
|
|
if strings.HasSuffix(method, "Health/Check") {
|
|
|
|
return invoker(ctx, method, req, reply, cc, opts...)
|
|
|
|
}
|
|
|
|
return intercept(ctx, method, req, reply, cc, invoker, opts...)
|
|
|
|
}
|
|
|
|
}
|