options.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package main
  2. import (
  3. "os"
  4. "path/filepath"
  5. cliconfig "github.com/docker/docker/cli/config"
  6. "github.com/docker/docker/daemon/config"
  7. "github.com/docker/docker/opts"
  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. dockerCertPath = os.Getenv("DOCKER_CERT_PATH")
  27. dockerTLSVerify = os.Getenv("DOCKER_TLS_VERIFY") != ""
  28. )
  29. type daemonOptions struct {
  30. configFile string
  31. daemonConfig *config.Config
  32. flags *pflag.FlagSet
  33. Debug bool
  34. Hosts []string
  35. LogLevel string
  36. TLS bool
  37. TLSVerify bool
  38. TLSOptions *tlsconfig.Options
  39. Validate bool
  40. }
  41. // newDaemonOptions returns a new daemonFlags
  42. func newDaemonOptions(config *config.Config) *daemonOptions {
  43. return &daemonOptions{
  44. daemonConfig: config,
  45. }
  46. }
  47. // InstallFlags adds flags for the common options on the FlagSet
  48. func (o *daemonOptions) InstallFlags(flags *pflag.FlagSet) {
  49. if dockerCertPath == "" {
  50. // cliconfig.Dir returns $DOCKER_CONFIG or ~/.docker.
  51. // cliconfig.Dir does not look up $XDG_CONFIG_HOME
  52. dockerCertPath = cliconfig.Dir()
  53. }
  54. flags.BoolVarP(&o.Debug, "debug", "D", false, "Enable debug mode")
  55. flags.BoolVar(&o.Validate, "validate", false, "Validate daemon configuration and exit")
  56. flags.StringVarP(&o.LogLevel, "log-level", "l", "info", `Set the logging level ("debug"|"info"|"warn"|"error"|"fatal")`)
  57. flags.BoolVar(&o.TLS, FlagTLS, DefaultTLSValue, "Use TLS; implied by --tlsverify")
  58. flags.BoolVar(&o.TLSVerify, FlagTLSVerify, dockerTLSVerify || DefaultTLSValue, "Use TLS and verify the remote")
  59. // TODO use flag flags.String("identity"}, "i", "", "Path to libtrust key file")
  60. o.TLSOptions = &tlsconfig.Options{
  61. CAFile: filepath.Join(dockerCertPath, DefaultCaFile),
  62. CertFile: filepath.Join(dockerCertPath, DefaultCertFile),
  63. KeyFile: filepath.Join(dockerCertPath, DefaultKeyFile),
  64. }
  65. tlsOptions := o.TLSOptions
  66. flags.Var(opts.NewQuotedString(&tlsOptions.CAFile), "tlscacert", "Trust certs signed only by this CA")
  67. flags.Var(opts.NewQuotedString(&tlsOptions.CertFile), "tlscert", "Path to TLS certificate file")
  68. flags.Var(opts.NewQuotedString(&tlsOptions.KeyFile), "tlskey", "Path to TLS key file")
  69. hostOpt := opts.NewNamedListOptsRef("hosts", &o.Hosts, opts.ValidateHost)
  70. flags.VarP(hostOpt, "host", "H", "Daemon socket(s) to connect to")
  71. }
  72. // SetDefaultOptions sets default values for options after flag parsing is
  73. // complete
  74. func (o *daemonOptions) SetDefaultOptions(flags *pflag.FlagSet) {
  75. // Regardless of whether the user sets it to true or false, if they
  76. // specify --tlsverify at all then we need to turn on TLS
  77. // TLSVerify can be true even if not set due to DOCKER_TLS_VERIFY env var, so we need
  78. // to check that here as well
  79. if flags.Changed(FlagTLSVerify) || o.TLSVerify {
  80. o.TLS = true
  81. }
  82. if o.TLS && !flags.Changed(FlagTLSVerify) {
  83. // Enable tls verification unless explicitly disabled
  84. o.TLSVerify = true
  85. }
  86. if !o.TLS {
  87. o.TLSOptions = nil
  88. } else {
  89. tlsOptions := o.TLSOptions
  90. tlsOptions.InsecureSkipVerify = !o.TLSVerify
  91. // Reset CertFile and KeyFile to empty string if the user did not specify
  92. // the respective flags and the respective default files were not found.
  93. if !flags.Changed("tlscert") {
  94. if _, err := os.Stat(tlsOptions.CertFile); os.IsNotExist(err) {
  95. tlsOptions.CertFile = ""
  96. }
  97. }
  98. if !flags.Changed("tlskey") {
  99. if _, err := os.Stat(tlsOptions.KeyFile); os.IsNotExist(err) {
  100. tlsOptions.KeyFile = ""
  101. }
  102. }
  103. }
  104. }