github.com/kunnos/engine@v1.13.1/cli/flags/common.go (about)

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