2017-06-01 20:34:31 +00:00
|
|
|
package main
|
2015-05-05 04:18:28 +00:00
|
|
|
|
|
|
|
import (
|
2023-06-13 21:54:14 +00:00
|
|
|
"fmt"
|
2015-05-05 04:18:28 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2023-09-13 15:41:45 +00:00
|
|
|
"github.com/containerd/log"
|
2017-06-01 20:34:31 +00:00
|
|
|
"github.com/docker/docker/daemon/config"
|
2015-05-05 04:18:28 +00:00
|
|
|
"github.com/docker/docker/opts"
|
2022-12-14 11:15:55 +00:00
|
|
|
"github.com/docker/docker/pkg/homedir"
|
2015-12-30 00:27:12 +00:00
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
2016-06-21 20:42:47 +00:00
|
|
|
"github.com/spf13/pflag"
|
2015-05-05 04:18:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-02-19 22:42:51 +00:00
|
|
|
// DefaultCaFile is the default filename for the CA pem file
|
|
|
|
DefaultCaFile = "ca.pem"
|
|
|
|
// DefaultKeyFile is the default filename for the key pem file
|
|
|
|
DefaultKeyFile = "key.pem"
|
|
|
|
// DefaultCertFile is the default filename for the cert pem file
|
|
|
|
DefaultCertFile = "cert.pem"
|
2016-12-20 11:14:41 +00:00
|
|
|
// FlagTLSVerify is the flag name for the TLS verification option
|
2016-06-21 20:42:47 +00:00
|
|
|
FlagTLSVerify = "tlsverify"
|
2020-07-28 23:01:08 +00:00
|
|
|
// FlagTLS is the flag name for the TLS option
|
|
|
|
FlagTLS = "tls"
|
|
|
|
// DefaultTLSValue is the default value used for setting the tls option for tcp connections
|
|
|
|
DefaultTLSValue = false
|
2015-05-05 04:18:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-12-14 11:15:55 +00:00
|
|
|
// The configDir (and "DOCKER_CONFIG" environment variable) is now only used
|
|
|
|
// for the default location for TLS certificates to secure the daemon API.
|
|
|
|
// It is a leftover from when the "docker" and "dockerd" CLI shared the
|
|
|
|
// same binary, allowing the DOCKER_CONFIG environment variable to set
|
|
|
|
// the location for certificates to be used by both.
|
|
|
|
//
|
|
|
|
// We need to change this, as there's various issues:
|
|
|
|
//
|
|
|
|
// - DOCKER_CONFIG only affects TLS certificates, but does not change the
|
|
|
|
// location for the actual *daemon configuration* (which defaults to
|
|
|
|
// "/etc/docker/daemon.json").
|
|
|
|
// - If no value is set, configDir uses "~/.docker/" as default, but does
|
|
|
|
// not take $XDG_CONFIG_HOME into account (it uses pkg/homedir.Get, which
|
|
|
|
// is not XDG_CONFIG_HOME-aware).
|
|
|
|
// - Using the home directory can be problematic in cases where the CLI and
|
|
|
|
// daemon actually live on the same host; if DOCKER_CONFIG is set to set
|
|
|
|
// the "docker" CLI configuration path (and if the daemon shares that
|
|
|
|
// environment variable, e.g. "sudo -E dockerd"), the daemon may create
|
|
|
|
// the "~/.docker/" directory, but now the directory may be owned by "root".
|
|
|
|
//
|
|
|
|
// We should:
|
|
|
|
//
|
|
|
|
// - deprecate DOCKER_CONFIG for the daemon
|
|
|
|
// - decide where the TLS certs should live by default ("/etc/docker/"?)
|
|
|
|
// - look at "when" (and when _not_) XDG_CONFIG_HOME should be used. Its
|
|
|
|
// needed for rootless, but perhaps could be used for non-rootless(?)
|
|
|
|
// - When changing the location for TLS config, (ideally) they should
|
|
|
|
// live in a directory separate from "non-sensitive" (configuration-)
|
|
|
|
// files, so that general configuration can be shared (dotfiles repo
|
|
|
|
// etc) separate from "sensitive" config (TLS certificates).
|
|
|
|
//
|
|
|
|
// TODO(thaJeztah): deprecate DOCKER_CONFIG and re-design daemon config locations. See https://github.com/moby/moby/issues/44640
|
|
|
|
configDir = os.Getenv("DOCKER_CONFIG")
|
|
|
|
configFileDir = ".docker"
|
2015-05-05 04:18:28 +00:00
|
|
|
dockerCertPath = os.Getenv("DOCKER_CERT_PATH")
|
|
|
|
dockerTLSVerify = os.Getenv("DOCKER_TLS_VERIFY") != ""
|
|
|
|
)
|
|
|
|
|
2017-06-01 20:34:31 +00:00
|
|
|
type daemonOptions struct {
|
|
|
|
configFile string
|
|
|
|
daemonConfig *config.Config
|
|
|
|
flags *pflag.FlagSet
|
|
|
|
Debug bool
|
|
|
|
Hosts []string
|
|
|
|
LogLevel string
|
2023-06-13 21:54:14 +00:00
|
|
|
LogFormat string
|
2017-06-01 20:34:31 +00:00
|
|
|
TLS bool
|
|
|
|
TLSVerify bool
|
|
|
|
TLSOptions *tlsconfig.Options
|
2018-11-05 00:52:26 +00:00
|
|
|
Validate bool
|
2016-04-21 21:51:28 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 11:15:55 +00:00
|
|
|
// defaultCertPath uses $DOCKER_CONFIG or ~/.docker, and does not look up
|
|
|
|
// $XDG_CONFIG_HOME. See the comment on configDir above for further details.
|
|
|
|
func defaultCertPath() string {
|
|
|
|
if configDir == "" {
|
|
|
|
// Set the default path if DOCKER_CONFIG is not set.
|
|
|
|
configDir = filepath.Join(homedir.Get(), configFileDir)
|
|
|
|
}
|
|
|
|
return configDir
|
|
|
|
}
|
|
|
|
|
2017-06-01 20:34:31 +00:00
|
|
|
// newDaemonOptions returns a new daemonFlags
|
|
|
|
func newDaemonOptions(config *config.Config) *daemonOptions {
|
|
|
|
return &daemonOptions{
|
|
|
|
daemonConfig: config,
|
|
|
|
}
|
2016-06-21 20:42:47 +00:00
|
|
|
}
|
2016-02-19 22:42:51 +00:00
|
|
|
|
2022-04-22 14:49:26 +00:00
|
|
|
// installFlags adds flags for the common options on the FlagSet
|
|
|
|
func (o *daemonOptions) installFlags(flags *pflag.FlagSet) {
|
2015-05-05 04:18:28 +00:00
|
|
|
if dockerCertPath == "" {
|
2022-12-14 11:15:55 +00:00
|
|
|
dockerCertPath = defaultCertPath()
|
2015-05-05 04:18:28 +00:00
|
|
|
}
|
|
|
|
|
2017-06-01 20:34:31 +00:00
|
|
|
flags.BoolVarP(&o.Debug, "debug", "D", false, "Enable debug mode")
|
2021-06-24 15:43:28 +00:00
|
|
|
flags.BoolVar(&o.Validate, "validate", false, "Validate daemon configuration and exit")
|
2017-06-01 20:34:31 +00:00
|
|
|
flags.StringVarP(&o.LogLevel, "log-level", "l", "info", `Set the logging level ("debug"|"info"|"warn"|"error"|"fatal")`)
|
2023-09-15 10:56:42 +00:00
|
|
|
flags.StringVar(&o.LogFormat, "log-format", string(log.TextFormat), fmt.Sprintf(`Set the logging format ("%s"|"%s")`, log.TextFormat, log.JSONFormat))
|
2020-07-28 23:01:08 +00:00
|
|
|
flags.BoolVar(&o.TLS, FlagTLS, DefaultTLSValue, "Use TLS; implied by --tlsverify")
|
|
|
|
flags.BoolVar(&o.TLSVerify, FlagTLSVerify, dockerTLSVerify || DefaultTLSValue, "Use TLS and verify the remote")
|
2015-05-05 04:18:28 +00:00
|
|
|
|
2022-12-14 11:15:55 +00:00
|
|
|
// TODO(thaJeztah): set default TLSOptions in config.New()
|
2022-02-16 20:38:57 +00:00
|
|
|
o.TLSOptions = &tlsconfig.Options{}
|
2017-06-01 20:34:31 +00:00
|
|
|
tlsOptions := o.TLSOptions
|
2022-02-16 20:38:57 +00:00
|
|
|
flags.StringVar(&tlsOptions.CAFile, "tlscacert", filepath.Join(dockerCertPath, DefaultCaFile), "Trust certs signed only by this CA")
|
|
|
|
flags.StringVar(&tlsOptions.CertFile, "tlscert", filepath.Join(dockerCertPath, DefaultCertFile), "Path to TLS certificate file")
|
|
|
|
flags.StringVar(&tlsOptions.KeyFile, "tlskey", filepath.Join(dockerCertPath, DefaultKeyFile), "Path to TLS key file")
|
2015-05-05 04:18:28 +00:00
|
|
|
|
2017-06-01 20:34:31 +00:00
|
|
|
hostOpt := opts.NewNamedListOptsRef("hosts", &o.Hosts, opts.ValidateHost)
|
2016-06-22 22:36:51 +00:00
|
|
|
flags.VarP(hostOpt, "host", "H", "Daemon socket(s) to connect to")
|
2015-05-05 04:18:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 14:49:26 +00:00
|
|
|
// setDefaultOptions sets default values for options after flag parsing is
|
2016-06-21 20:42:47 +00:00
|
|
|
// complete
|
2022-04-22 14:49:26 +00:00
|
|
|
func (o *daemonOptions) setDefaultOptions() {
|
2015-05-05 04:18:28 +00:00
|
|
|
// Regardless of whether the user sets it to true or false, if they
|
2016-12-20 11:14:41 +00:00
|
|
|
// specify --tlsverify at all then we need to turn on TLS
|
2016-02-19 22:42:51 +00:00
|
|
|
// TLSVerify can be true even if not set due to DOCKER_TLS_VERIFY env var, so we need
|
|
|
|
// to check that here as well
|
2022-04-22 14:49:26 +00:00
|
|
|
if o.flags.Changed(FlagTLSVerify) || o.TLSVerify {
|
2017-06-01 20:34:31 +00:00
|
|
|
o.TLS = true
|
2015-05-05 04:18:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 14:49:26 +00:00
|
|
|
if o.TLS && !o.flags.Changed(FlagTLSVerify) {
|
2020-07-28 23:01:08 +00:00
|
|
|
// Enable tls verification unless explicitly disabled
|
|
|
|
o.TLSVerify = true
|
|
|
|
}
|
|
|
|
|
2017-06-01 20:34:31 +00:00
|
|
|
if !o.TLS {
|
|
|
|
o.TLSOptions = nil
|
2015-05-05 04:18:28 +00:00
|
|
|
} else {
|
2022-04-22 14:49:26 +00:00
|
|
|
o.TLSOptions.InsecureSkipVerify = !o.TLSVerify
|
2015-05-05 04:18:28 +00:00
|
|
|
|
|
|
|
// Reset CertFile and KeyFile to empty string if the user did not specify
|
|
|
|
// the respective flags and the respective default files were not found.
|
2022-04-22 14:49:26 +00:00
|
|
|
if !o.flags.Changed("tlscert") {
|
|
|
|
if _, err := os.Stat(o.TLSOptions.CertFile); os.IsNotExist(err) {
|
|
|
|
o.TLSOptions.CertFile = ""
|
2015-05-05 04:18:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-22 14:49:26 +00:00
|
|
|
if !o.flags.Changed("tlskey") {
|
|
|
|
if _, err := os.Stat(o.TLSOptions.KeyFile); os.IsNotExist(err) {
|
|
|
|
o.TLSOptions.KeyFile = ""
|
2015-05-05 04:18:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|