options.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "github.com/containerd/log"
  7. "github.com/docker/docker/daemon/config"
  8. "github.com/docker/docker/opts"
  9. "github.com/docker/docker/pkg/homedir"
  10. "github.com/docker/go-connections/tlsconfig"
  11. "github.com/spf13/pflag"
  12. )
  13. const (
  14. // DefaultCaFile is the default filename for the CA pem file
  15. DefaultCaFile = "ca.pem"
  16. // DefaultKeyFile is the default filename for the key pem file
  17. DefaultKeyFile = "key.pem"
  18. // DefaultCertFile is the default filename for the cert pem file
  19. DefaultCertFile = "cert.pem"
  20. // FlagTLSVerify is the flag name for the TLS verification option
  21. FlagTLSVerify = "tlsverify"
  22. // FlagTLS is the flag name for the TLS option
  23. FlagTLS = "tls"
  24. // DefaultTLSValue is the default value used for setting the tls option for tcp connections
  25. DefaultTLSValue = false
  26. )
  27. var (
  28. // The configDir (and "DOCKER_CONFIG" environment variable) is now only used
  29. // for the default location for TLS certificates to secure the daemon API.
  30. // It is a leftover from when the "docker" and "dockerd" CLI shared the
  31. // same binary, allowing the DOCKER_CONFIG environment variable to set
  32. // the location for certificates to be used by both.
  33. //
  34. // We need to change this, as there's various issues:
  35. //
  36. // - DOCKER_CONFIG only affects TLS certificates, but does not change the
  37. // location for the actual *daemon configuration* (which defaults to
  38. // "/etc/docker/daemon.json").
  39. // - If no value is set, configDir uses "~/.docker/" as default, but does
  40. // not take $XDG_CONFIG_HOME into account (it uses pkg/homedir.Get, which
  41. // is not XDG_CONFIG_HOME-aware).
  42. // - Using the home directory can be problematic in cases where the CLI and
  43. // daemon actually live on the same host; if DOCKER_CONFIG is set to set
  44. // the "docker" CLI configuration path (and if the daemon shares that
  45. // environment variable, e.g. "sudo -E dockerd"), the daemon may create
  46. // the "~/.docker/" directory, but now the directory may be owned by "root".
  47. //
  48. // We should:
  49. //
  50. // - deprecate DOCKER_CONFIG for the daemon
  51. // - decide where the TLS certs should live by default ("/etc/docker/"?)
  52. // - look at "when" (and when _not_) XDG_CONFIG_HOME should be used. Its
  53. // needed for rootless, but perhaps could be used for non-rootless(?)
  54. // - When changing the location for TLS config, (ideally) they should
  55. // live in a directory separate from "non-sensitive" (configuration-)
  56. // files, so that general configuration can be shared (dotfiles repo
  57. // etc) separate from "sensitive" config (TLS certificates).
  58. //
  59. // TODO(thaJeztah): deprecate DOCKER_CONFIG and re-design daemon config locations. See https://github.com/moby/moby/issues/44640
  60. configDir = os.Getenv("DOCKER_CONFIG")
  61. configFileDir = ".docker"
  62. dockerCertPath = os.Getenv("DOCKER_CERT_PATH")
  63. dockerTLSVerify = os.Getenv("DOCKER_TLS_VERIFY") != ""
  64. )
  65. type daemonOptions struct {
  66. configFile string
  67. daemonConfig *config.Config
  68. flags *pflag.FlagSet
  69. Debug bool
  70. Hosts []string
  71. LogLevel string
  72. LogFormat string
  73. TLS bool
  74. TLSVerify bool
  75. TLSOptions *tlsconfig.Options
  76. Validate bool
  77. }
  78. // defaultCertPath uses $DOCKER_CONFIG or ~/.docker, and does not look up
  79. // $XDG_CONFIG_HOME. See the comment on configDir above for further details.
  80. func defaultCertPath() string {
  81. if configDir == "" {
  82. // Set the default path if DOCKER_CONFIG is not set.
  83. configDir = filepath.Join(homedir.Get(), configFileDir)
  84. }
  85. return configDir
  86. }
  87. // newDaemonOptions returns a new daemonFlags
  88. func newDaemonOptions(config *config.Config) *daemonOptions {
  89. return &daemonOptions{
  90. daemonConfig: config,
  91. }
  92. }
  93. // installFlags adds flags for the common options on the FlagSet
  94. func (o *daemonOptions) installFlags(flags *pflag.FlagSet) {
  95. if dockerCertPath == "" {
  96. dockerCertPath = defaultCertPath()
  97. }
  98. flags.BoolVarP(&o.Debug, "debug", "D", false, "Enable debug mode")
  99. flags.BoolVar(&o.Validate, "validate", false, "Validate daemon configuration and exit")
  100. flags.StringVarP(&o.LogLevel, "log-level", "l", "info", `Set the logging level ("debug"|"info"|"warn"|"error"|"fatal")`)
  101. flags.StringVar(&o.LogFormat, "log-format", string(log.TextFormat), fmt.Sprintf(`Set the logging format ("%s"|"%s")`, log.TextFormat, log.JSONFormat))
  102. flags.BoolVar(&o.TLS, FlagTLS, DefaultTLSValue, "Use TLS; implied by --tlsverify")
  103. flags.BoolVar(&o.TLSVerify, FlagTLSVerify, dockerTLSVerify || DefaultTLSValue, "Use TLS and verify the remote")
  104. // TODO(thaJeztah): set default TLSOptions in config.New()
  105. o.TLSOptions = &tlsconfig.Options{}
  106. tlsOptions := o.TLSOptions
  107. flags.StringVar(&tlsOptions.CAFile, "tlscacert", filepath.Join(dockerCertPath, DefaultCaFile), "Trust certs signed only by this CA")
  108. flags.StringVar(&tlsOptions.CertFile, "tlscert", filepath.Join(dockerCertPath, DefaultCertFile), "Path to TLS certificate file")
  109. flags.StringVar(&tlsOptions.KeyFile, "tlskey", filepath.Join(dockerCertPath, DefaultKeyFile), "Path to TLS key file")
  110. hostOpt := opts.NewNamedListOptsRef("hosts", &o.Hosts, opts.ValidateHost)
  111. flags.VarP(hostOpt, "host", "H", "Daemon socket(s) to connect to")
  112. }
  113. // setDefaultOptions sets default values for options after flag parsing is
  114. // complete
  115. func (o *daemonOptions) setDefaultOptions() {
  116. // Regardless of whether the user sets it to true or false, if they
  117. // specify --tlsverify at all then we need to turn on TLS
  118. // TLSVerify can be true even if not set due to DOCKER_TLS_VERIFY env var, so we need
  119. // to check that here as well
  120. if o.flags.Changed(FlagTLSVerify) || o.TLSVerify {
  121. o.TLS = true
  122. }
  123. if o.TLS && !o.flags.Changed(FlagTLSVerify) {
  124. // Enable tls verification unless explicitly disabled
  125. o.TLSVerify = true
  126. }
  127. if !o.TLS {
  128. o.TLSOptions = nil
  129. } else {
  130. o.TLSOptions.InsecureSkipVerify = !o.TLSVerify
  131. // Reset CertFile and KeyFile to empty string if the user did not specify
  132. // the respective flags and the respective default files were not found.
  133. if !o.flags.Changed("tlscert") {
  134. if _, err := os.Stat(o.TLSOptions.CertFile); os.IsNotExist(err) {
  135. o.TLSOptions.CertFile = ""
  136. }
  137. }
  138. if !o.flags.Changed("tlskey") {
  139. if _, err := os.Stat(o.TLSOptions.KeyFile); os.IsNotExist(err) {
  140. o.TLSOptions.KeyFile = ""
  141. }
  142. }
  143. }
  144. }