common.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package flags
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "github.com/Sirupsen/logrus"
  7. "github.com/docker/docker/cliconfig"
  8. "github.com/docker/docker/opts"
  9. flag "github.com/docker/docker/pkg/mflag"
  10. "github.com/docker/go-connections/tlsconfig"
  11. )
  12. const (
  13. // DefaultTrustKeyFile is the default filename for the trust key
  14. DefaultTrustKeyFile = "key.json"
  15. // DefaultCaFile is the default filename for the CA pem file
  16. DefaultCaFile = "ca.pem"
  17. // DefaultKeyFile is the default filename for the key pem file
  18. DefaultKeyFile = "key.pem"
  19. // DefaultCertFile is the default filename for the cert pem file
  20. DefaultCertFile = "cert.pem"
  21. // TLSVerifyKey is the default flag name for the tls verification option
  22. TLSVerifyKey = "tlsverify"
  23. )
  24. var (
  25. dockerCertPath = os.Getenv("DOCKER_CERT_PATH")
  26. dockerTLSVerify = os.Getenv("DOCKER_TLS_VERIFY") != ""
  27. )
  28. // CommonFlags are flags common to both the client and the daemon.
  29. type CommonFlags struct {
  30. FlagSet *flag.FlagSet
  31. PostParse func()
  32. Debug bool
  33. Hosts []string
  34. LogLevel string
  35. TLS bool
  36. TLSVerify bool
  37. TLSOptions *tlsconfig.Options
  38. TrustKey string
  39. }
  40. // InitCommonFlags initializes flags common to both client and daemon
  41. func InitCommonFlags() *CommonFlags {
  42. var commonFlags = &CommonFlags{FlagSet: new(flag.FlagSet)}
  43. if dockerCertPath == "" {
  44. dockerCertPath = cliconfig.ConfigDir()
  45. }
  46. commonFlags.PostParse = func() { postParseCommon(commonFlags) }
  47. cmd := commonFlags.FlagSet
  48. cmd.BoolVar(&commonFlags.Debug, []string{"D", "-debug"}, false, "Enable debug mode")
  49. cmd.StringVar(&commonFlags.LogLevel, []string{"l", "-log-level"}, "info", "Set the logging level")
  50. cmd.BoolVar(&commonFlags.TLS, []string{"-tls"}, false, "Use TLS; implied by --tlsverify")
  51. cmd.BoolVar(&commonFlags.TLSVerify, []string{"-tlsverify"}, dockerTLSVerify, "Use TLS and verify the remote")
  52. // TODO use flag flag.String([]string{"i", "-identity"}, "", "Path to libtrust key file")
  53. var tlsOptions tlsconfig.Options
  54. commonFlags.TLSOptions = &tlsOptions
  55. cmd.StringVar(&tlsOptions.CAFile, []string{"-tlscacert"}, filepath.Join(dockerCertPath, DefaultCaFile), "Trust certs signed only by this CA")
  56. cmd.StringVar(&tlsOptions.CertFile, []string{"-tlscert"}, filepath.Join(dockerCertPath, DefaultCertFile), "Path to TLS certificate file")
  57. cmd.StringVar(&tlsOptions.KeyFile, []string{"-tlskey"}, filepath.Join(dockerCertPath, DefaultKeyFile), "Path to TLS key file")
  58. cmd.Var(opts.NewNamedListOptsRef("hosts", &commonFlags.Hosts, opts.ValidateHost), []string{"H", "-host"}, "Daemon socket(s) to connect to")
  59. return commonFlags
  60. }
  61. func postParseCommon(commonFlags *CommonFlags) {
  62. cmd := commonFlags.FlagSet
  63. SetDaemonLogLevel(commonFlags.LogLevel)
  64. // Regardless of whether the user sets it to true or false, if they
  65. // specify --tlsverify at all then we need to turn on tls
  66. // TLSVerify can be true even if not set due to DOCKER_TLS_VERIFY env var, so we need
  67. // to check that here as well
  68. if cmd.IsSet("-"+TLSVerifyKey) || commonFlags.TLSVerify {
  69. commonFlags.TLS = true
  70. }
  71. if !commonFlags.TLS {
  72. commonFlags.TLSOptions = nil
  73. } else {
  74. tlsOptions := commonFlags.TLSOptions
  75. tlsOptions.InsecureSkipVerify = !commonFlags.TLSVerify
  76. // Reset CertFile and KeyFile to empty string if the user did not specify
  77. // the respective flags and the respective default files were not found.
  78. if !cmd.IsSet("-tlscert") {
  79. if _, err := os.Stat(tlsOptions.CertFile); os.IsNotExist(err) {
  80. tlsOptions.CertFile = ""
  81. }
  82. }
  83. if !cmd.IsSet("-tlskey") {
  84. if _, err := os.Stat(tlsOptions.KeyFile); os.IsNotExist(err) {
  85. tlsOptions.KeyFile = ""
  86. }
  87. }
  88. }
  89. }
  90. // SetDaemonLogLevel sets the logrus logging level
  91. // TODO: this is a bad name, it applies to the client as well.
  92. func SetDaemonLogLevel(logLevel string) {
  93. if logLevel != "" {
  94. lvl, err := logrus.ParseLevel(logLevel)
  95. if err != nil {
  96. fmt.Fprintf(os.Stderr, "Unable to parse logging level: %s\n", logLevel)
  97. os.Exit(1)
  98. }
  99. logrus.SetLevel(lvl)
  100. } else {
  101. logrus.SetLevel(logrus.InfoLevel)
  102. }
  103. }