shellout.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 compose
  14. import (
  15. "context"
  16. "os"
  17. "os/exec"
  18. "path/filepath"
  19. "github.com/compose-spec/compose-go/v2/types"
  20. "github.com/docker/cli/cli-plugins/metadata"
  21. "github.com/docker/cli/cli/command"
  22. "github.com/docker/cli/cli/flags"
  23. "github.com/docker/compose/v2/internal"
  24. "github.com/docker/docker/client"
  25. "go.opentelemetry.io/otel"
  26. "go.opentelemetry.io/otel/propagation"
  27. )
  28. // prepareShellOut prepare a shell-out command to be ran by Compose
  29. func (s *composeService) prepareShellOut(gctx context.Context, env types.Mapping, cmd *exec.Cmd) error {
  30. env = env.Clone()
  31. // remove DOCKER_CLI_PLUGIN... variable so a docker-cli plugin will detect it run standalone
  32. delete(env, metadata.ReexecEnvvar)
  33. // propagate opentelemetry context to child process, see https://github.com/open-telemetry/oteps/blob/main/text/0258-env-context-baggage-carriers.md
  34. carrier := propagation.MapCarrier{}
  35. otel.GetTextMapPropagator().Inject(gctx, &carrier)
  36. env.Merge(types.Mapping(carrier))
  37. cmd.Env = env.Values()
  38. return nil
  39. }
  40. // propagateDockerEndpoint produces DOCKER_* env vars for a child CLI plugin to target the same docker endpoint
  41. // `cleanup` func MUST be called after child process completion to enforce removal of cert files
  42. func (s *composeService) propagateDockerEndpoint() ([]string, func(), error) {
  43. cleanup := func() {}
  44. env := types.Mapping{}
  45. env[command.EnvOverrideContext] = s.dockerCli.CurrentContext()
  46. env["USER_AGENT"] = "compose/" + internal.Version
  47. endpoint := s.dockerCli.DockerEndpoint()
  48. env[client.EnvOverrideHost] = endpoint.Host
  49. if endpoint.TLSData != nil {
  50. certs, err := os.MkdirTemp("", "compose")
  51. if err != nil {
  52. return nil, cleanup, err
  53. }
  54. cleanup = func() {
  55. _ = os.RemoveAll(certs)
  56. }
  57. env[client.EnvOverrideCertPath] = certs
  58. if !endpoint.SkipTLSVerify {
  59. env[client.EnvTLSVerify] = "1"
  60. }
  61. err = os.WriteFile(filepath.Join(certs, flags.DefaultKeyFile), endpoint.TLSData.Key, 0o600)
  62. if err != nil {
  63. return nil, cleanup, err
  64. }
  65. err = os.WriteFile(filepath.Join(certs, flags.DefaultCaFile), endpoint.TLSData.Cert, 0o600)
  66. if err != nil {
  67. return nil, cleanup, err
  68. }
  69. err = os.WriteFile(filepath.Join(certs, flags.DefaultCaFile), endpoint.TLSData.CA, 0o600)
  70. if err != nil {
  71. return nil, cleanup, err
  72. }
  73. }
  74. return env.Values(), cleanup, nil
  75. }