interceptor.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package server
  2. import (
  3. "context"
  4. "errors"
  5. "strings"
  6. "google.golang.org/grpc"
  7. "google.golang.org/grpc/metadata"
  8. "github.com/docker/api/client"
  9. "github.com/docker/api/config"
  10. apicontext "github.com/docker/api/context"
  11. "github.com/docker/api/context/store"
  12. "github.com/docker/api/server/proxy"
  13. )
  14. // key is the key where the current docker context is stored in the metadata
  15. // of a gRPC request
  16. const key = "context_key"
  17. // unaryServerInterceptor configures the context and sends it to the next handler
  18. func unaryServerInterceptor(clictx context.Context) grpc.UnaryServerInterceptor {
  19. return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
  20. currentContext, err := getIncomingContext(ctx)
  21. if err != nil {
  22. currentContext, err = getConfigContext(clictx)
  23. if err != nil {
  24. return nil, err
  25. }
  26. }
  27. configuredCtx, err := configureContext(clictx, currentContext, info.FullMethod)
  28. if err != nil {
  29. return nil, err
  30. }
  31. return handler(configuredCtx, req)
  32. }
  33. }
  34. // streamServerInterceptor configures the context and sends it to the next handler
  35. func streamServerInterceptor(clictx context.Context) grpc.StreamServerInterceptor {
  36. return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
  37. currentContext, err := getIncomingContext(ss.Context())
  38. if err != nil {
  39. currentContext, err = getConfigContext(clictx)
  40. if err != nil {
  41. return err
  42. }
  43. }
  44. ctx, err := configureContext(clictx, currentContext, info.FullMethod)
  45. if err != nil {
  46. return err
  47. }
  48. return handler(srv, &contextServerStream{
  49. ss: ss,
  50. ctx: ctx,
  51. })
  52. }
  53. }
  54. // Returns the current context from the configuration file
  55. func getConfigContext(ctx context.Context) (string, error) {
  56. configDir := config.Dir(ctx)
  57. configFile, err := config.LoadFile(configDir)
  58. if err != nil {
  59. return "", err
  60. }
  61. return configFile.CurrentContext, nil
  62. }
  63. // Returns the context set by the caller if any, error otherwise
  64. func getIncomingContext(ctx context.Context) (string, error) {
  65. if md, ok := metadata.FromIncomingContext(ctx); ok {
  66. if key, ok := md[key]; ok {
  67. return key[0], nil
  68. }
  69. }
  70. return "", errors.New("not found")
  71. }
  72. // configureContext populates the request context with objects the client
  73. // needs: the context store and the api client
  74. func configureContext(ctx context.Context, currentContext string, method string) (context.Context, error) {
  75. configDir := config.Dir(ctx)
  76. ctx = apicontext.WithCurrentContext(ctx, currentContext)
  77. // The contexts service doesn't need the client
  78. if !strings.Contains(method, "/com.docker.api.protos.context.v1.Contexts") {
  79. c, err := client.New(ctx)
  80. if err != nil {
  81. return nil, err
  82. }
  83. ctx, err = proxy.WithClient(ctx, c)
  84. if err != nil {
  85. return nil, err
  86. }
  87. }
  88. s, err := store.New(store.WithRoot(configDir))
  89. if err != nil {
  90. return nil, err
  91. }
  92. ctx = store.WithContextStore(ctx, s)
  93. return ctx, nil
  94. }