server.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. "google.golang.org/grpc"
  30. "google.golang.org/grpc/health"
  31. "google.golang.org/grpc/health/grpc_health_v1"
  32. "google.golang.org/grpc/metadata"
  33. "github.com/docker/api/client"
  34. "github.com/docker/api/config"
  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.UnaryInterceptor(unaryServerInterceptor(ctx)),
  43. grpc.StreamInterceptor(streamServerInterceptor(ctx)),
  44. )
  45. hs := health.NewServer()
  46. grpc_health_v1.RegisterHealthServer(s, hs)
  47. return s
  48. }
  49. //CreateListener creates a listener either on tcp://, or local listener, supporting unix:// for unix socket or npipe:// for named pipes on windows
  50. func CreateListener(address string) (net.Listener, error) {
  51. if strings.HasPrefix(address, "tcp://") {
  52. return net.Listen("tcp", strings.TrimPrefix(address, "tcp://"))
  53. }
  54. return createLocalListener(address)
  55. }
  56. // unaryServerInterceptor configures the context and sends it to the next handler
  57. func unaryServerInterceptor(clictx context.Context) func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
  58. return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
  59. configuredCtx, err := configureContext(clictx, info.FullMethod)
  60. if err != nil {
  61. return nil, err
  62. }
  63. return handler(configuredCtx, req)
  64. }
  65. }
  66. // streamServerInterceptor configures the context and sends it to the next handler
  67. func streamServerInterceptor(clictx context.Context) func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
  68. return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
  69. ctx, err := configureContext(clictx, info.FullMethod)
  70. if err != nil {
  71. return err
  72. }
  73. return handler(srv, newServerStream(ctx, ss))
  74. }
  75. }
  76. // configureContext populates the request context with objects the client
  77. // needs: the context store and the api client
  78. func configureContext(ctx context.Context, method string) (context.Context, error) {
  79. configDir := config.Dir(ctx)
  80. configFile, err := config.LoadFile(configDir)
  81. if err != nil {
  82. return nil, err
  83. }
  84. if configFile.CurrentContext != "" {
  85. ctx = apicontext.WithCurrentContext(ctx, configFile.CurrentContext)
  86. }
  87. // The contexts service doesn't need the client
  88. if !strings.Contains(method, "/com.docker.api.protos.context.v1.Contexts") {
  89. c, err := client.New(ctx)
  90. if err != nil {
  91. return nil, err
  92. }
  93. ctx, err = proxy.WithClient(ctx, c)
  94. if err != nil {
  95. return nil, err
  96. }
  97. }
  98. s, err := store.New(store.WithRoot(configDir))
  99. if err != nil {
  100. return nil, err
  101. }
  102. ctx = store.WithContextStore(ctx, s)
  103. return ctx, nil
  104. }
  105. // A gRPC server stream will only let you get its context but
  106. // there is no way to set a new (augmented context) to the next
  107. // handler (like we do for a unary request). We need to wrap the grpc.ServerSteam
  108. // to be able to set a new context that will be sent to the next stream interceptor.
  109. type contextServerStream struct {
  110. s grpc.ServerStream
  111. ctx context.Context
  112. }
  113. func newServerStream(ctx context.Context, s grpc.ServerStream) grpc.ServerStream {
  114. return &contextServerStream{
  115. s: s,
  116. ctx: ctx,
  117. }
  118. }
  119. func (css *contextServerStream) SetHeader(md metadata.MD) error {
  120. return css.s.SetHeader(md)
  121. }
  122. func (css *contextServerStream) SendHeader(md metadata.MD) error {
  123. return css.s.SendHeader(md)
  124. }
  125. func (css *contextServerStream) SetTrailer(md metadata.MD) {
  126. css.s.SetTrailer(md)
  127. }
  128. func (css *contextServerStream) Context() context.Context {
  129. return css.ctx
  130. }
  131. func (css *contextServerStream) SendMsg(m interface{}) error {
  132. return css.s.SendMsg(m)
  133. }
  134. func (css *contextServerStream) RecvMsg(m interface{}) error {
  135. return css.s.RecvMsg(m)
  136. }