2018-02-05 16:05:59 -05:00
|
|
|
package opts // import "github.com/docker/docker/opts"
|
2015-12-15 20:53:17 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"net/url"
|
2018-10-15 03:52:53 -04:00
|
|
|
"path/filepath"
|
2015-12-15 20:53:17 -05:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2018-10-15 03:52:53 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/homedir"
|
2022-04-03 15:38:24 -04:00
|
|
|
"github.com/pkg/errors"
|
2015-12-15 20:53:17 -05:00
|
|
|
)
|
|
|
|
|
2020-10-30 16:17:34 -04:00
|
|
|
const (
|
2016-12-26 09:27:56 -05:00
|
|
|
// DefaultHTTPPort Default HTTP Port used if only the protocol is provided to -H flag e.g. dockerd -H tcp://
|
2015-12-15 20:53:17 -05:00
|
|
|
// These are the IANA registered port numbers for use with Docker
|
|
|
|
// see http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml?search=docker
|
|
|
|
DefaultHTTPPort = 2375 // Default HTTP Port
|
|
|
|
// DefaultTLSHTTPPort Default HTTP Port used when TLS enabled
|
|
|
|
DefaultTLSHTTPPort = 2376 // Default TLS encrypted HTTP Port
|
|
|
|
// DefaultUnixSocket Path for the unix socket.
|
|
|
|
// Docker daemon by default always listens on the default unix socket
|
|
|
|
DefaultUnixSocket = "/var/run/docker.sock"
|
|
|
|
// DefaultTCPHost constant defines the default host string used by docker on Windows
|
2020-10-30 16:17:34 -04:00
|
|
|
DefaultTCPHost = "tcp://" + DefaultHTTPHost + ":2375"
|
2015-12-15 20:53:17 -05:00
|
|
|
// DefaultTLSHost constant defines the default host string used by docker for TLS sockets
|
2020-10-30 16:17:34 -04:00
|
|
|
DefaultTLSHost = "tcp://" + DefaultHTTPHost + ":2376"
|
2016-01-30 21:45:49 -05:00
|
|
|
// DefaultNamedPipe defines the default named pipe used by docker on Windows
|
|
|
|
DefaultNamedPipe = `//./pipe/docker_engine`
|
2020-10-30 16:17:34 -04:00
|
|
|
// HostGatewayName is the string value that can be passed
|
|
|
|
// to the IPAddr section in --add-host that is replaced by
|
|
|
|
// the value of HostGatewayIP daemon config value
|
|
|
|
HostGatewayName = "host-gateway"
|
2015-12-15 20:53:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// ValidateHost validates that the specified string is a valid host and returns it.
|
|
|
|
func ValidateHost(val string) (string, error) {
|
2016-01-30 21:45:49 -05:00
|
|
|
host := strings.TrimSpace(val)
|
2017-10-25 08:39:51 -04:00
|
|
|
// The empty string means default and is not handled by parseDaemonHost
|
2016-01-30 21:45:49 -05:00
|
|
|
if host != "" {
|
2017-10-25 08:39:51 -04:00
|
|
|
_, err := parseDaemonHost(host)
|
2016-01-30 21:45:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return val, err
|
|
|
|
}
|
2015-12-15 20:53:17 -05:00
|
|
|
}
|
|
|
|
// Note: unlike most flag validators, we don't return the mutated value here
|
2016-12-20 06:14:41 -05:00
|
|
|
// we need to know what the user entered later (using ParseHost) to adjust for TLS
|
2015-12-15 20:53:17 -05:00
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
2018-10-15 03:52:53 -04:00
|
|
|
// ParseHost and set defaults for a Daemon host string.
|
2019-04-19 03:53:58 -04:00
|
|
|
// defaultToTLS is preferred over defaultToUnixXDG.
|
|
|
|
func ParseHost(defaultToTLS, defaultToUnixXDG bool, val string) (string, error) {
|
2016-01-30 21:45:49 -05:00
|
|
|
host := strings.TrimSpace(val)
|
|
|
|
if host == "" {
|
|
|
|
if defaultToTLS {
|
|
|
|
host = DefaultTLSHost
|
2019-04-19 03:53:58 -04:00
|
|
|
} else if defaultToUnixXDG {
|
2018-10-15 03:52:53 -04:00
|
|
|
runtimeDir, err := homedir.GetRuntimeDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
socket := filepath.Join(runtimeDir, "docker.sock")
|
|
|
|
host = "unix://" + socket
|
2016-01-30 21:45:49 -05:00
|
|
|
} else {
|
|
|
|
host = DefaultHost
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var err error
|
2017-10-25 08:39:51 -04:00
|
|
|
host, err = parseDaemonHost(host)
|
2016-01-30 21:45:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return val, err
|
|
|
|
}
|
2015-12-15 20:53:17 -05:00
|
|
|
}
|
|
|
|
return host, nil
|
|
|
|
}
|
|
|
|
|
2017-10-25 08:39:51 -04:00
|
|
|
// parseDaemonHost parses the specified address and returns an address that will be used as the host.
|
2022-04-03 16:00:08 -04:00
|
|
|
// Depending on the address specified, this may return one of the global Default* strings defined in hosts.go.
|
2017-10-25 08:39:51 -04:00
|
|
|
func parseDaemonHost(addr string) (string, error) {
|
2016-06-21 20:14:55 -04:00
|
|
|
addrParts := strings.SplitN(addr, "://", 2)
|
2016-01-30 21:45:49 -05:00
|
|
|
if len(addrParts) == 1 && addrParts[0] != "" {
|
2015-12-15 20:53:17 -05:00
|
|
|
addrParts = []string{"tcp", addrParts[0]}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch addrParts[0] {
|
|
|
|
case "tcp":
|
2022-04-03 16:00:08 -04:00
|
|
|
return ParseTCPAddr(addr, DefaultTCPHost)
|
2015-12-15 20:53:17 -05:00
|
|
|
case "unix":
|
2016-01-30 21:45:49 -05:00
|
|
|
return parseSimpleProtoAddr("unix", addrParts[1], DefaultUnixSocket)
|
|
|
|
case "npipe":
|
|
|
|
return parseSimpleProtoAddr("npipe", addrParts[1], DefaultNamedPipe)
|
2015-12-15 20:53:17 -05:00
|
|
|
case "fd":
|
|
|
|
return addr, nil
|
|
|
|
default:
|
2022-04-03 16:00:08 -04:00
|
|
|
return "", errors.Errorf("invalid bind address (%s): unsupported proto '%s'", addr, addrParts[0])
|
2015-12-15 20:53:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-30 21:45:49 -05:00
|
|
|
// parseSimpleProtoAddr parses and validates that the specified address is a valid
|
|
|
|
// socket address for simple protocols like unix and npipe. It returns a formatted
|
|
|
|
// socket address, either using the address parsed from addr, or the contents of
|
|
|
|
// defaultAddr if addr is a blank string.
|
|
|
|
func parseSimpleProtoAddr(proto, addr, defaultAddr string) (string, error) {
|
|
|
|
addr = strings.TrimPrefix(addr, proto+"://")
|
2015-12-15 20:53:17 -05:00
|
|
|
if strings.Contains(addr, "://") {
|
2022-04-03 16:00:08 -04:00
|
|
|
return "", errors.Errorf("invalid proto, expected %s: %s", proto, addr)
|
2015-12-15 20:53:17 -05:00
|
|
|
}
|
|
|
|
if addr == "" {
|
|
|
|
addr = defaultAddr
|
|
|
|
}
|
2022-04-03 16:00:08 -04:00
|
|
|
return proto + "://" + addr, nil
|
2015-12-15 20:53:17 -05:00
|
|
|
}
|
|
|
|
|
2016-06-21 17:27:04 -04:00
|
|
|
// ParseTCPAddr parses and validates that the specified address is a valid TCP
|
2015-12-15 20:53:17 -05:00
|
|
|
// address. It returns a formatted TCP address, either using the address parsed
|
|
|
|
// from tryAddr, or the contents of defaultAddr if tryAddr is a blank string.
|
|
|
|
// tryAddr is expected to have already been Trim()'d
|
|
|
|
// defaultAddr must be in the full `tcp://host:port` form
|
2016-06-21 17:27:04 -04:00
|
|
|
func ParseTCPAddr(tryAddr string, defaultAddr string) (string, error) {
|
2022-04-03 16:00:08 -04:00
|
|
|
def, err := parseTCPAddr(defaultAddr, true)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrapf(err, "invalid default address (%s)", defaultAddr)
|
2015-12-15 20:53:17 -05:00
|
|
|
}
|
|
|
|
|
2022-04-03 16:00:08 -04:00
|
|
|
addr, err := parseTCPAddr(tryAddr, false)
|
2015-12-15 20:53:17 -05:00
|
|
|
if err != nil {
|
2022-04-03 16:00:08 -04:00
|
|
|
return "", errors.Wrapf(err, "invalid bind address (%s)", tryAddr)
|
2015-12-15 20:53:17 -05:00
|
|
|
}
|
2022-04-03 16:00:08 -04:00
|
|
|
|
|
|
|
host := addr.Hostname()
|
|
|
|
if host == "" {
|
|
|
|
host = def.Hostname()
|
2015-12-15 20:53:17 -05:00
|
|
|
}
|
2022-04-03 16:00:08 -04:00
|
|
|
port := addr.Port()
|
|
|
|
if port == "" {
|
|
|
|
port = def.Port()
|
2016-06-21 17:27:04 -04:00
|
|
|
}
|
2022-04-03 16:00:08 -04:00
|
|
|
|
|
|
|
return "tcp://" + net.JoinHostPort(host, port), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseTCPAddr parses the given addr and validates if it is in the expected
|
|
|
|
// format. If strict is enabled, the address must contain a scheme (tcp://),
|
|
|
|
// a host (or IP-address) and a port number.
|
|
|
|
func parseTCPAddr(address string, strict bool) (*url.URL, error) {
|
|
|
|
if !strict && !strings.Contains(address, "://") {
|
|
|
|
address = "tcp://" + address
|
|
|
|
}
|
|
|
|
parsedURL, err := url.Parse(address)
|
2015-12-15 20:53:17 -05:00
|
|
|
if err != nil {
|
2022-04-03 16:00:08 -04:00
|
|
|
return nil, err
|
2015-12-15 20:53:17 -05:00
|
|
|
}
|
2022-04-03 16:00:08 -04:00
|
|
|
if parsedURL.Scheme != "tcp" {
|
|
|
|
return nil, errors.Errorf("unsupported proto '%s'", parsedURL.Scheme)
|
2015-12-15 20:53:17 -05:00
|
|
|
}
|
2022-04-03 16:00:08 -04:00
|
|
|
if parsedURL.Path != "" {
|
|
|
|
return nil, errors.New("should not contain a path element")
|
2015-12-15 20:53:17 -05:00
|
|
|
}
|
2022-04-03 16:00:08 -04:00
|
|
|
if strict && parsedURL.Host == "" {
|
|
|
|
return nil, errors.New("no host or IP address")
|
2015-12-15 20:53:17 -05:00
|
|
|
}
|
2022-04-03 16:00:08 -04:00
|
|
|
if parsedURL.Port() != "" || strict {
|
|
|
|
if p, err := strconv.Atoi(parsedURL.Port()); err != nil || p == 0 {
|
|
|
|
return nil, errors.Errorf("invalid port: %s", parsedURL.Port())
|
|
|
|
}
|
2022-04-03 15:38:24 -04:00
|
|
|
}
|
2022-04-03 16:00:08 -04:00
|
|
|
return parsedURL, nil
|
2015-12-15 20:53:17 -05:00
|
|
|
}
|
2016-12-23 14:09:12 -05:00
|
|
|
|
|
|
|
// ValidateExtraHost validates that the specified string is a valid extrahost and returns it.
|
|
|
|
// ExtraHost is in the form of name:ip where the ip has to be a valid ip (IPv4 or IPv6).
|
|
|
|
func ValidateExtraHost(val string) (string, error) {
|
|
|
|
// allow for IPv6 addresses in extra hosts by only splitting on first ":"
|
|
|
|
arr := strings.SplitN(val, ":", 2)
|
|
|
|
if len(arr) != 2 || len(arr[0]) == 0 {
|
2022-04-03 16:00:08 -04:00
|
|
|
return "", errors.Errorf("bad format for add-host: %q", val)
|
2016-12-23 14:09:12 -05:00
|
|
|
}
|
2019-11-01 20:09:40 -04:00
|
|
|
// Skip IPaddr validation for special "host-gateway" string
|
2020-10-30 16:17:34 -04:00
|
|
|
if arr[1] != HostGatewayName {
|
2019-11-01 20:09:40 -04:00
|
|
|
if _, err := ValidateIPAddress(arr[1]); err != nil {
|
2022-04-03 16:00:08 -04:00
|
|
|
return "", errors.Errorf("invalid IP address in add-host: %q", arr[1])
|
2019-11-01 20:09:40 -04:00
|
|
|
}
|
2016-12-23 14:09:12 -05:00
|
|
|
}
|
|
|
|
return val, nil
|
|
|
|
}
|