options.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. cliconfig "github.com/docker/docker/cli/config"
  7. "github.com/docker/docker/daemon/config"
  8. "github.com/docker/docker/opts"
  9. "github.com/docker/go-connections/tlsconfig"
  10. "github.com/sirupsen/logrus"
  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. )
  23. var (
  24. dockerCertPath = os.Getenv("DOCKER_CERT_PATH")
  25. dockerTLSVerify = os.Getenv("DOCKER_TLS_VERIFY") != ""
  26. )
  27. type daemonOptions struct {
  28. configFile string
  29. daemonConfig *config.Config
  30. flags *pflag.FlagSet
  31. Debug bool
  32. Hosts []string
  33. LogLevel string
  34. TLS bool
  35. TLSVerify bool
  36. TLSOptions *tlsconfig.Options
  37. }
  38. // newDaemonOptions returns a new daemonFlags
  39. func newDaemonOptions(config *config.Config) *daemonOptions {
  40. return &daemonOptions{
  41. daemonConfig: config,
  42. }
  43. }
  44. // InstallFlags adds flags for the common options on the FlagSet
  45. func (o *daemonOptions) InstallFlags(flags *pflag.FlagSet) {
  46. if dockerCertPath == "" {
  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. }
  95. // setLogLevel sets the logrus logging level
  96. func setLogLevel(logLevel string) {
  97. if logLevel != "" {
  98. lvl, err := logrus.ParseLevel(logLevel)
  99. if err != nil {
  100. fmt.Fprintf(os.Stderr, "Unable to parse logging level: %s\n", logLevel)
  101. os.Exit(1)
  102. }
  103. logrus.SetLevel(lvl)
  104. } else {
  105. logrus.SetLevel(logrus.InfoLevel)
  106. }
  107. }