Default the tcp port to 2376 if tls is on, and 2375 if not

Refactor so that the Host flag validation doesn't destroy the user's input,
and then post process the flags when we know the TLS options

Signed-off-by: Sven Dowideit <SvenDowideit@home.org.au>
This commit is contained in:
Sven Dowideit 2015-08-21 23:28:49 +10:00 committed by Vincent Demeester
parent aca36e11f2
commit 6ad0875074
2 changed files with 29 additions and 3 deletions

View File

@ -17,16 +17,23 @@ var (
domainRegexp = regexp.MustCompile(`^(:?(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]))(:?\.(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])))*)\.?\s*$`) domainRegexp = regexp.MustCompile(`^(:?(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]))(:?\.(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])))*)\.?\s*$`)
// DefaultHTTPHost Default HTTP Host used if only port is provided to -H flag e.g. docker daemon -H tcp://:8080 // DefaultHTTPHost Default HTTP Host used if only port is provided to -H flag e.g. docker daemon -H tcp://:8080
DefaultHTTPHost = "127.0.0.1" DefaultHTTPHost = "127.0.0.1"
// DefaultHTTPPort Default HTTP Port used if only the protocol is provided to -H flag e.g. docker daemon -H tcp:// // DefaultHTTPPort Default HTTP Port used if only the protocol is provided to -H flag e.g. docker daemon -H tcp://
// TODO Windows. DefaultHTTPPort is only used on Windows if a -H parameter // TODO Windows. DefaultHTTPPort is only used on Windows if a -H parameter
// is not supplied. A better longer term solution would be to use a named // is not supplied. A better longer term solution would be to use a named
// pipe as the default on the Windows daemon. // pipe as the default on the Windows daemon.
// 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 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. // DefaultUnixSocket Path for the unix socket.
// Docker daemon by default always listens on the default unix socket // Docker daemon by default always listens on the default unix socket
DefaultUnixSocket = "/var/run/docker.sock" DefaultUnixSocket = "/var/run/docker.sock"
// DefaultTCPHost constant defines the default host string used by docker on Windows // DefaultTCPHost constant defines the default host string used by docker on Windows
DefaultTCPHost = fmt.Sprintf("tcp://%s:%d", DefaultHTTPHost, DefaultHTTPPort) DefaultTCPHost = fmt.Sprintf("tcp://%s:%d", DefaultHTTPHost, DefaultHTTPPort)
// DefaultTLSHost constant defines the default host string used by docker for TLS sockets
DefaultTLSHost = fmt.Sprintf("tcp://%s:%d", DefaultHTTPHost, DefaultTLSHTTPPort)
) )
// ListOpts holds a list of values and a validation function. // ListOpts holds a list of values and a validation function.
@ -335,6 +342,17 @@ func ValidateLabel(val string) (string, error) {
// ValidateHost validates that the specified string is a valid host and returns it. // ValidateHost validates that the specified string is a valid host and returns it.
func ValidateHost(val string) (string, error) { func ValidateHost(val string) (string, error) {
_, err := parsers.ParseDockerDaemonHost(DefaultTCPHost, DefaultUnixSocket, val)
if err != nil {
return val, err
}
// Note: unlike most flag validators, we don't return the mutated value here
// we need to know what the user entered later (using ParseHost) to adjust for tls
return val, nil
}
// ParseHost and set defaults for a Daemon host string
func ParseHost(defaultHTTPHost, val string) (string, error) {
host, err := parsers.ParseDockerDaemonHost(DefaultTCPHost, DefaultUnixSocket, val) host, err := parsers.ParseDockerDaemonHost(DefaultTCPHost, DefaultUnixSocket, val)
if err != nil { if err != nil {
return val, err return val, err

View File

@ -3,6 +3,7 @@ package opts
import ( import (
"fmt" "fmt"
"os" "os"
"runtime"
"strings" "strings"
"testing" "testing"
) )
@ -423,7 +424,7 @@ func TestValidateLabel(t *testing.T) {
} }
} }
func TestValidateHost(t *testing.T) { func TestParseHost(t *testing.T) {
invalid := map[string]string{ invalid := map[string]string{
"anything": "Invalid bind address format: anything", "anything": "Invalid bind address format: anything",
"something with spaces": "Invalid bind address format: something with spaces", "something with spaces": "Invalid bind address format: something with spaces",
@ -433,7 +434,14 @@ func TestValidateHost(t *testing.T) {
"tcp://invalid": "Invalid bind address format: invalid", "tcp://invalid": "Invalid bind address format: invalid",
"tcp://invalid:port": "Invalid bind address format: invalid:port", "tcp://invalid:port": "Invalid bind address format: invalid:port",
} }
const defaultHTTPHost = "tcp://127.0.0.1:2375"
var defaultHOST = "unix:///var/run/docker.sock"
if runtime.GOOS == "windows" {
defaultHOST = defaultHTTPHost
}
valid := map[string]string{ valid := map[string]string{
"": defaultHOST,
"fd://": "fd://", "fd://": "fd://",
"fd://something": "fd://something", "fd://something": "fd://something",
"tcp://host:": "tcp://host:2375", "tcp://host:": "tcp://host:2375",
@ -450,12 +458,12 @@ func TestValidateHost(t *testing.T) {
} }
for value, errorMessage := range invalid { for value, errorMessage := range invalid {
if _, err := ValidateHost(value); err == nil || err.Error() != errorMessage { if _, err := ParseHost(defaultHTTPHost, value); err == nil || err.Error() != errorMessage {
t.Fatalf("Expected an error for %v with [%v], got [%v]", value, errorMessage, err) t.Fatalf("Expected an error for %v with [%v], got [%v]", value, errorMessage, err)
} }
} }
for value, expected := range valid { for value, expected := range valid {
if actual, err := ValidateHost(value); err != nil || actual != expected { if actual, err := ParseHost(defaultHTTPHost, value); err != nil || actual != expected {
t.Fatalf("Expected for %v [%v], got [%v, %v]", value, expected, actual, err) t.Fatalf("Expected for %v [%v], got [%v, %v]", value, expected, actual, err)
} }
} }