github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/cmd/dockerd/options.go (about)

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