common.go 3.9 KB

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