options.go 6.0 KB

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