github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/cmd/dockerd/options.go (about)

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