interceptor.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package server
  14. import (
  15. "context"
  16. "errors"
  17. "strings"
  18. "google.golang.org/grpc"
  19. "google.golang.org/grpc/metadata"
  20. "github.com/docker/compose-cli/api/client"
  21. "github.com/docker/compose-cli/cli/server/proxy"
  22. "github.com/docker/compose-cli/config"
  23. apicontext "github.com/docker/compose-cli/context"
  24. "github.com/docker/compose-cli/context/store"
  25. )
  26. // key is the key where the current docker context is stored in the metadata
  27. // of a gRPC request
  28. const key = "context_key"
  29. // unaryServerInterceptor configures the context and sends it to the next handler
  30. func unaryServerInterceptor(clictx context.Context) grpc.UnaryServerInterceptor {
  31. return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
  32. currentContext, err := getIncomingContext(ctx)
  33. if err != nil {
  34. currentContext, err = getConfigContext(clictx)
  35. if err != nil {
  36. return nil, err
  37. }
  38. }
  39. configuredCtx, err := configureContext(clictx, currentContext, info.FullMethod)
  40. if err != nil {
  41. return nil, err
  42. }
  43. return handler(configuredCtx, req)
  44. }
  45. }
  46. // streamServerInterceptor configures the context and sends it to the next handler
  47. func streamServerInterceptor(clictx context.Context) grpc.StreamServerInterceptor {
  48. return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
  49. currentContext, err := getIncomingContext(ss.Context())
  50. if err != nil {
  51. currentContext, err = getConfigContext(clictx)
  52. if err != nil {
  53. return err
  54. }
  55. }
  56. ctx, err := configureContext(clictx, currentContext, info.FullMethod)
  57. if err != nil {
  58. return err
  59. }
  60. return handler(srv, &contextServerStream{
  61. ss: ss,
  62. ctx: ctx,
  63. })
  64. }
  65. }
  66. // Returns the current context from the configuration file
  67. func getConfigContext(ctx context.Context) (string, error) {
  68. configDir := config.Dir(ctx)
  69. configFile, err := config.LoadFile(configDir)
  70. if err != nil {
  71. return "", err
  72. }
  73. return configFile.CurrentContext, nil
  74. }
  75. // Returns the context set by the caller if any, error otherwise
  76. func getIncomingContext(ctx context.Context) (string, error) {
  77. if md, ok := metadata.FromIncomingContext(ctx); ok {
  78. if key, ok := md[key]; ok {
  79. return key[0], nil
  80. }
  81. }
  82. return "", errors.New("not found")
  83. }
  84. // configureContext populates the request context with objects the client
  85. // needs: the context store and the api client
  86. func configureContext(ctx context.Context, currentContext string, method string) (context.Context, error) {
  87. configDir := config.Dir(ctx)
  88. ctx = apicontext.WithCurrentContext(ctx, currentContext)
  89. // The contexts service doesn't need the client
  90. if !strings.Contains(method, "/com.docker.api.protos.context.v1.Contexts") {
  91. c, err := client.New(ctx)
  92. if err != nil {
  93. return nil, err
  94. }
  95. ctx = proxy.WithClient(ctx, c)
  96. }
  97. s, err := store.New(configDir)
  98. if err != nil {
  99. return nil, err
  100. }
  101. ctx = store.WithContextStore(ctx, s)
  102. return ctx, nil
  103. }