server.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. Copyright (c) 2020 Docker Inc.
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use, copy,
  7. modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED,
  14. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  16. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM,
  18. DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT,
  20. TORT OR OTHERWISE,
  21. ARISING FROM, OUT OF OR IN CONNECTION WITH
  22. THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. package server
  25. import (
  26. "context"
  27. "net"
  28. "strings"
  29. grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
  30. "google.golang.org/grpc"
  31. "google.golang.org/grpc/health"
  32. "google.golang.org/grpc/health/grpc_health_v1"
  33. "google.golang.org/grpc/metadata"
  34. "github.com/docker/api/client"
  35. apicontext "github.com/docker/api/context"
  36. "github.com/docker/api/context/store"
  37. "github.com/docker/api/server/proxy"
  38. )
  39. // New returns a new GRPC server.
  40. func New(ctx context.Context) *grpc.Server {
  41. s := grpc.NewServer(
  42. grpc.ChainUnaryInterceptor(
  43. unaryMeta(ctx),
  44. unary,
  45. ),
  46. grpc.ChainStreamInterceptor(
  47. grpc.StreamServerInterceptor(stream),
  48. grpc.StreamServerInterceptor(streamMeta(ctx)),
  49. ),
  50. )
  51. hs := health.NewServer()
  52. grpc_health_v1.RegisterHealthServer(s, hs)
  53. return s
  54. }
  55. //CreateListener creates a listener either on tcp://, or local listener, supporting unix:// for unix socket or npipe:// for named pipes on windows
  56. func CreateListener(address string) (net.Listener, error) {
  57. if strings.HasPrefix(address, "tcp://") {
  58. return net.Listen("tcp", strings.TrimPrefix(address, "tcp://"))
  59. }
  60. return createLocalListener(address)
  61. }
  62. func unary(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
  63. return grpc_prometheus.UnaryServerInterceptor(ctx, req, info, handler)
  64. }
  65. func stream(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
  66. return grpc_prometheus.StreamServerInterceptor(srv, ss, info, handler)
  67. }
  68. func unaryMeta(clictx context.Context) func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
  69. return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
  70. configuredCtx, err := configureContext(ctx, clictx)
  71. if err != nil {
  72. return nil, err
  73. }
  74. return handler(configuredCtx, req)
  75. }
  76. }
  77. func streamMeta(clictx context.Context) func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
  78. return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
  79. ctx, err := configureContext(ss.Context(), clictx)
  80. if err != nil {
  81. return err
  82. }
  83. nss := newServerStream(ctx, ss)
  84. return handler(srv, nss)
  85. }
  86. }
  87. // nolint: golint
  88. func configureContext(ctx context.Context, clictx context.Context) (context.Context, error) {
  89. md, ok := metadata.FromIncomingContext(ctx)
  90. if !ok {
  91. return ctx, nil
  92. }
  93. key, ok := md[apicontext.Key]
  94. if !ok {
  95. return ctx, nil
  96. }
  97. if len(key) == 1 {
  98. s := store.ContextStore(clictx)
  99. ctx = store.WithContextStore(ctx, s)
  100. ctx = apicontext.WithCurrentContext(ctx, key[0])
  101. c, err := client.New(ctx)
  102. if err != nil {
  103. return nil, err
  104. }
  105. ctx, err = proxy.WithClient(ctx, c)
  106. if err != nil {
  107. return nil, err
  108. }
  109. }
  110. return ctx, nil
  111. }
  112. type contextServerStream struct {
  113. s grpc.ServerStream
  114. ctx context.Context
  115. }
  116. func newServerStream(ctx context.Context, s grpc.ServerStream) grpc.ServerStream {
  117. return &contextServerStream{
  118. s: s,
  119. ctx: ctx,
  120. }
  121. }
  122. func (css *contextServerStream) SetHeader(md metadata.MD) error {
  123. return css.s.SetHeader(md)
  124. }
  125. func (css *contextServerStream) SendHeader(md metadata.MD) error {
  126. return css.s.SendHeader(md)
  127. }
  128. func (css *contextServerStream) SetTrailer(md metadata.MD) {
  129. css.s.SetTrailer(md)
  130. }
  131. func (css *contextServerStream) Context() context.Context {
  132. return css.ctx
  133. }
  134. func (css *contextServerStream) SendMsg(m interface{}) error {
  135. return css.s.SendMsg(m)
  136. }
  137. func (css *contextServerStream) RecvMsg(m interface{}) error {
  138. return css.s.RecvMsg(m)
  139. }