common.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. tlsOptions := commonOpts.TLSOptions
  54. flags.StringVar(&tlsOptions.CAFile, "tlscacert", filepath.Join(dockerCertPath, DefaultCaFile), "Trust certs signed only by this CA")
  55. flags.StringVar(&tlsOptions.CertFile, "tlscert", filepath.Join(dockerCertPath, DefaultCertFile), "Path to TLS certificate file")
  56. flags.StringVar(&tlsOptions.KeyFile, "tlskey", filepath.Join(dockerCertPath, DefaultKeyFile), "Path to TLS key file")
  57. hostOpt := opts.NewNamedListOptsRef("hosts", &commonOpts.Hosts, opts.ValidateHost)
  58. flags.VarP(hostOpt, "host", "H", "Daemon socket(s) to connect to")
  59. }
  60. // SetDefaultOptions sets default values for options after flag parsing is
  61. // complete
  62. func (commonOpts *CommonOptions) SetDefaultOptions(flags *pflag.FlagSet) {
  63. // Regardless of whether the user sets it to true or false, if they
  64. // specify --tlsverify at all then we need to turn on TLS
  65. // TLSVerify can be true even if not set due to DOCKER_TLS_VERIFY env var, so we need
  66. // to check that here as well
  67. if flags.Changed(FlagTLSVerify) || commonOpts.TLSVerify {
  68. commonOpts.TLS = true
  69. }
  70. if !commonOpts.TLS {
  71. commonOpts.TLSOptions = nil
  72. } else {
  73. tlsOptions := commonOpts.TLSOptions
  74. tlsOptions.InsecureSkipVerify = !commonOpts.TLSVerify
  75. // Reset CertFile and KeyFile to empty string if the user did not specify
  76. // the respective flags and the respective default files were not found.
  77. if !flags.Changed("tlscert") {
  78. if _, err := os.Stat(tlsOptions.CertFile); os.IsNotExist(err) {
  79. tlsOptions.CertFile = ""
  80. }
  81. }
  82. if !flags.Changed("tlskey") {
  83. if _, err := os.Stat(tlsOptions.KeyFile); os.IsNotExist(err) {
  84. tlsOptions.KeyFile = ""
  85. }
  86. }
  87. }
  88. }
  89. // SetLogLevel sets the logrus logging level
  90. func SetLogLevel(logLevel string) {
  91. if logLevel != "" {
  92. lvl, err := logrus.ParseLevel(logLevel)
  93. if err != nil {
  94. fmt.Fprintf(os.Stderr, "Unable to parse logging level: %s\n", logLevel)
  95. os.Exit(1)
  96. }
  97. logrus.SetLevel(lvl)
  98. } else {
  99. logrus.SetLevel(logrus.InfoLevel)
  100. }
  101. }