options.go 3.5 KB

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