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