Windows: Add support for named pipe protocol

This adds an npipe protocol option for Windows hosts, akin to unix
sockets for Linux hosts. This should become the default transport
for Windows, but this change does not yet do that.

It also does not add support for the client side yet since that
code is in engine-api, which will have to be revendored separately.

Signed-off-by: John Starks <jostarks@microsoft.com>
This commit is contained in:
John Starks 2016-01-30 18:45:49 -08:00
parent a39ad952ec
commit 0906195fbb
9 changed files with 97 additions and 92 deletions

View File

@ -176,12 +176,7 @@ func getServerHost(hosts []string, tlsOptions *tlsconfig.Options) (host string,
return "", errors.New("Please specify only one -H") return "", errors.New("Please specify only one -H")
} }
defaultHost := opts.DefaultTCPHost host, err = opts.ParseHost(tlsOptions != nil, host)
if tlsOptions != nil {
defaultHost = opts.DefaultTLSHost
}
host, err = opts.ParseHost(defaultHost, host)
return return
} }

View File

@ -4,8 +4,11 @@ package server
import ( import (
"errors" "errors"
"fmt"
"github.com/Microsoft/go-winio"
"net" "net"
"net/http" "net/http"
"strings"
) )
// NewServer sets up the required Server and does protocol specific checking. // NewServer sets up the required Server and does protocol specific checking.
@ -21,8 +24,26 @@ func (s *Server) newServer(proto, addr string) ([]*HTTPServer, error) {
} }
ls = append(ls, l) ls = append(ls, l)
case "npipe":
// allow Administrators and SYSTEM, plus whatever additional users or groups were specified
sddl := "D:P(A;;GA;;;BA)(A;;GA;;;SY)"
if s.cfg.SocketGroup != "" {
for _, g := range strings.Split(s.cfg.SocketGroup, ",") {
sid, err := winio.LookupSidByName(g)
if err != nil {
return nil, err
}
sddl += fmt.Sprintf("(A;;GRGW;;;%s)", sid)
}
}
l, err := winio.ListenPipe(addr, sddl)
if err != nil {
return nil, err
}
ls = append(ls, l)
default: default:
return nil, errors.New("Invalid protocol format. Windows only supports tcp.") return nil, errors.New("Invalid protocol format. Windows only supports tcp and npipe.")
} }
var res []*HTTPServer var res []*HTTPServer

View File

@ -59,6 +59,7 @@ type CommonConfig struct {
Pidfile string `json:"pidfile,omitempty"` Pidfile string `json:"pidfile,omitempty"`
RawLogs bool `json:"raw-logs,omitempty"` RawLogs bool `json:"raw-logs,omitempty"`
Root string `json:"graph,omitempty"` Root string `json:"graph,omitempty"`
SocketGroup string `json:"group,omitempty"`
TrustKeyPath string `json:"-"` TrustKeyPath string `json:"-"`
// ClusterStore is the storage backend used for the cluster information. It is used by both // ClusterStore is the storage backend used for the cluster information. It is used by both

View File

@ -29,7 +29,6 @@ type Config struct {
EnableCors bool `json:"api-enable-cors,omitempty"` EnableCors bool `json:"api-enable-cors,omitempty"`
EnableSelinuxSupport bool `json:"selinux-enabled,omitempty"` EnableSelinuxSupport bool `json:"selinux-enabled,omitempty"`
RemappedRoot string `json:"userns-remap,omitempty"` RemappedRoot string `json:"userns-remap,omitempty"`
SocketGroup string `json:"group,omitempty"`
CgroupParent string `json:"cgroup-parent,omitempty"` CgroupParent string `json:"cgroup-parent,omitempty"`
Ulimits map[string]*units.Ulimit `json:"default-ulimits,omitempty"` Ulimits map[string]*units.Ulimit `json:"default-ulimits,omitempty"`
} }

View File

@ -38,4 +38,5 @@ func (config *Config) InstallFlags(cmd *flag.FlagSet, usageFn func(string) strin
// Then platform-specific install flags. // Then platform-specific install flags.
cmd.StringVar(&config.bridgeConfig.VirtualSwitchName, []string{"b", "-bridge"}, "", "Attach containers to a virtual switch") cmd.StringVar(&config.bridgeConfig.VirtualSwitchName, []string{"b", "-bridge"}, "", "Attach containers to a virtual switch")
cmd.StringVar(&config.SocketGroup, []string{"G", "-group"}, "", usageFn("Users or groups that can access the named pipe"))
} }

View File

@ -200,11 +200,11 @@ func (cli *DaemonCli) CmdDaemon(args ...string) error {
serverConfig := &apiserver.Config{ serverConfig := &apiserver.Config{
AuthorizationPluginNames: cli.Config.AuthorizationPlugins, AuthorizationPluginNames: cli.Config.AuthorizationPlugins,
Logging: true, Logging: true,
SocketGroup: cli.Config.SocketGroup,
Version: dockerversion.Version, Version: dockerversion.Version,
} }
serverConfig = setPlatformServerConfig(serverConfig, cli.Config) serverConfig = setPlatformServerConfig(serverConfig, cli.Config)
defaultHost := opts.DefaultHost
if cli.Config.TLS { if cli.Config.TLS {
tlsOptions := tlsconfig.Options{ tlsOptions := tlsconfig.Options{
CAFile: cli.Config.CommonTLSOptions.CAFile, CAFile: cli.Config.CommonTLSOptions.CAFile,
@ -221,7 +221,6 @@ func (cli *DaemonCli) CmdDaemon(args ...string) error {
logrus.Fatal(err) logrus.Fatal(err)
} }
serverConfig.TLSConfig = tlsConfig serverConfig.TLSConfig = tlsConfig
defaultHost = opts.DefaultTLSHost
} }
if len(cli.Config.Hosts) == 0 { if len(cli.Config.Hosts) == 0 {
@ -229,7 +228,7 @@ func (cli *DaemonCli) CmdDaemon(args ...string) error {
} }
for i := 0; i < len(cli.Config.Hosts); i++ { for i := 0; i < len(cli.Config.Hosts); i++ {
var err error var err error
if cli.Config.Hosts[i], err = opts.ParseHost(defaultHost, cli.Config.Hosts[i]); err != nil { if cli.Config.Hosts[i], err = opts.ParseHost(cli.Config.TLS, cli.Config.Hosts[i]); err != nil {
logrus.Fatalf("error parsing -H %s : %v", cli.Config.Hosts[i], err) logrus.Fatalf("error parsing -H %s : %v", cli.Config.Hosts[i], err)
} }

View File

@ -19,7 +19,6 @@ import (
const defaultDaemonConfigFile = "/etc/docker/daemon.json" const defaultDaemonConfigFile = "/etc/docker/daemon.json"
func setPlatformServerConfig(serverConfig *apiserver.Config, daemonCfg *daemon.Config) *apiserver.Config { func setPlatformServerConfig(serverConfig *apiserver.Config, daemonCfg *daemon.Config) *apiserver.Config {
serverConfig.SocketGroup = daemonCfg.SocketGroup
serverConfig.EnableCors = daemonCfg.EnableCors serverConfig.EnableCors = daemonCfg.EnableCors
serverConfig.CorsHeaders = daemonCfg.CorsHeaders serverConfig.CorsHeaders = daemonCfg.CorsHeaders

View File

@ -4,16 +4,12 @@ import (
"fmt" "fmt"
"net" "net"
"net/url" "net/url"
"runtime"
"strconv" "strconv"
"strings" "strings"
) )
var ( var (
// 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
// is not supplied. A better longer term solution would be to use a named
// pipe as the default on the Windows daemon.
// These are the IANA registered port numbers for use with Docker // 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 // 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
@ -26,13 +22,19 @@ var (
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 constant defines the default host string used by docker for TLS sockets
DefaultTLSHost = fmt.Sprintf("tcp://%s:%d", DefaultHTTPHost, DefaultTLSHTTPPort) DefaultTLSHost = fmt.Sprintf("tcp://%s:%d", DefaultHTTPHost, DefaultTLSHTTPPort)
// DefaultNamedPipe defines the default named pipe used by docker on Windows
DefaultNamedPipe = `//./pipe/docker_engine`
) )
// 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 := parseDockerDaemonHost(DefaultTCPHost, DefaultTLSHost, DefaultUnixSocket, "", val) host := strings.TrimSpace(val)
if err != nil { // The empty string means default and is not handled by parseDockerDaemonHost
return val, err if host != "" {
_, err := parseDockerDaemonHost(host)
if err != nil {
return val, err
}
} }
// Note: unlike most flag validators, we don't return the mutated value here // 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 // we need to know what the user entered later (using ParseHost) to adjust for tls
@ -40,39 +42,39 @@ func ValidateHost(val string) (string, error) {
} }
// ParseHost and set defaults for a Daemon host string // ParseHost and set defaults for a Daemon host string
func ParseHost(defaultHost, val string) (string, error) { func ParseHost(defaultToTLS bool, val string) (string, error) {
host, err := parseDockerDaemonHost(DefaultTCPHost, DefaultTLSHost, DefaultUnixSocket, defaultHost, val) host := strings.TrimSpace(val)
if err != nil { if host == "" {
return val, err if defaultToTLS {
host = DefaultTLSHost
} else {
host = DefaultHost
}
} else {
var err error
host, err = parseDockerDaemonHost(host)
if err != nil {
return val, err
}
} }
return host, nil return host, nil
} }
// parseDockerDaemonHost parses the specified address and returns an address that will be used as the host. // parseDockerDaemonHost parses the specified address and returns an address that will be used as the host.
// Depending of the address specified, will use the defaultTCPAddr or defaultUnixAddr // Depending of the address specified, this may return one of the global Default* strings defined in hosts.go.
// defaultUnixAddr must be a absolute file path (no `unix://` prefix) func parseDockerDaemonHost(addr string) (string, error) {
// defaultTCPAddr must be the full `tcp://host:port` form
func parseDockerDaemonHost(defaultTCPAddr, defaultTLSHost, defaultUnixAddr, defaultAddr, addr string) (string, error) {
addr = strings.TrimSpace(addr)
if addr == "" {
if defaultAddr == defaultTLSHost {
return defaultTLSHost, nil
}
if runtime.GOOS != "windows" {
return fmt.Sprintf("unix://%s", defaultUnixAddr), nil
}
return defaultTCPAddr, nil
}
addrParts := strings.Split(addr, "://") addrParts := strings.Split(addr, "://")
if len(addrParts) == 1 { if len(addrParts) == 1 && addrParts[0] != "" {
addrParts = []string{"tcp", addrParts[0]} addrParts = []string{"tcp", addrParts[0]}
} }
switch addrParts[0] { switch addrParts[0] {
case "tcp": case "tcp":
return parseTCPAddr(addrParts[1], defaultTCPAddr) return parseTCPAddr(addrParts[1], DefaultTCPHost)
case "unix": case "unix":
return parseUnixAddr(addrParts[1], defaultUnixAddr) return parseSimpleProtoAddr("unix", addrParts[1], DefaultUnixSocket)
case "npipe":
return parseSimpleProtoAddr("npipe", addrParts[1], DefaultNamedPipe)
case "fd": case "fd":
return addr, nil return addr, nil
default: default:
@ -80,19 +82,19 @@ func parseDockerDaemonHost(defaultTCPAddr, defaultTLSHost, defaultUnixAddr, defa
} }
} }
// parseUnixAddr parses and validates that the specified address is a valid UNIX // parseSimpleProtoAddr parses and validates that the specified address is a valid
// socket address. It returns a formatted UNIX socket address, either using the // socket address for simple protocols like unix and npipe. It returns a formatted
// address parsed from addr, or the contents of defaultAddr if addr is a blank // socket address, either using the address parsed from addr, or the contents of
// string. // defaultAddr if addr is a blank string.
func parseUnixAddr(addr string, defaultAddr string) (string, error) { func parseSimpleProtoAddr(proto, addr, defaultAddr string) (string, error) {
addr = strings.TrimPrefix(addr, "unix://") addr = strings.TrimPrefix(addr, proto+"://")
if strings.Contains(addr, "://") { if strings.Contains(addr, "://") {
return "", fmt.Errorf("Invalid proto, expected unix: %s", addr) return "", fmt.Errorf("Invalid proto, expected %s: %s", proto, addr)
} }
if addr == "" { if addr == "" {
addr = defaultAddr addr = defaultAddr
} }
return fmt.Sprintf("unix://%s", addr), nil return fmt.Sprintf("%s://%s", proto, addr), nil
} }
// parseTCPAddr parses and validates that the specified address is a valid TCP // parseTCPAddr parses and validates that the specified address is a valid TCP

View File

@ -1,7 +1,7 @@
package opts package opts
import ( import (
"runtime" "fmt"
"testing" "testing"
) )
@ -15,51 +15,41 @@ func TestParseHost(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, "": DefaultHost,
" ": DefaultHost,
" ": DefaultHost,
"fd://": "fd://", "fd://": "fd://",
"fd://something": "fd://something", "fd://something": "fd://something",
"tcp://host:": "tcp://host:2375", "tcp://host:": fmt.Sprintf("tcp://host:%d", DefaultHTTPPort),
"tcp://": "tcp://localhost:2375", "tcp://": DefaultTCPHost,
"tcp://:2375": "tcp://localhost:2375", // default ip address "tcp://:2375": fmt.Sprintf("tcp://%s:2375", DefaultHTTPHost),
"tcp://:2376": "tcp://localhost:2376", // default ip address "tcp://:2376": fmt.Sprintf("tcp://%s:2376", DefaultHTTPHost),
"tcp://0.0.0.0:8080": "tcp://0.0.0.0:8080", "tcp://0.0.0.0:8080": "tcp://0.0.0.0:8080",
"tcp://192.168.0.0:12000": "tcp://192.168.0.0:12000", "tcp://192.168.0.0:12000": "tcp://192.168.0.0:12000",
"tcp://192.168:8080": "tcp://192.168:8080", "tcp://192.168:8080": "tcp://192.168:8080",
"tcp://0.0.0.0:1234567890": "tcp://0.0.0.0:1234567890", // yeah it's valid :P "tcp://0.0.0.0:1234567890": "tcp://0.0.0.0:1234567890", // yeah it's valid :P
" tcp://:7777/path ": fmt.Sprintf("tcp://%s:7777/path", DefaultHTTPHost),
"tcp://docker.com:2375": "tcp://docker.com:2375", "tcp://docker.com:2375": "tcp://docker.com:2375",
"unix://": "unix:///var/run/docker.sock", // default unix:// value "unix://": "unix://" + DefaultUnixSocket,
"unix://path/to/socket": "unix://path/to/socket", "unix://path/to/socket": "unix://path/to/socket",
"npipe://": "npipe://" + DefaultNamedPipe,
"npipe:////./pipe/foo": "npipe:////./pipe/foo",
} }
for value, errorMessage := range invalid { for value, errorMessage := range invalid {
if _, err := ParseHost(defaultHTTPHost, value); err == nil || err.Error() != errorMessage { if _, err := ParseHost(false, value); err == nil || err.Error() != errorMessage {
t.Fatalf("Expected an error for %v with [%v], got [%v]", value, errorMessage, err) t.Errorf("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 := ParseHost(defaultHTTPHost, value); err != nil || actual != expected { if actual, err := ParseHost(false, value); err != nil || actual != expected {
t.Fatalf("Expected for %v [%v], got [%v, %v]", value, expected, actual, err) t.Errorf("Expected for %v [%v], got [%v, %v]", value, expected, actual, err)
} }
} }
} }
func TestParseDockerDaemonHost(t *testing.T) { func TestParseDockerDaemonHost(t *testing.T) {
var (
defaultHTTPHost = "tcp://localhost:2375"
defaultHTTPSHost = "tcp://localhost:2376"
defaultUnix = "/var/run/docker.sock"
defaultHOST = "unix:///var/run/docker.sock"
)
if runtime.GOOS == "windows" {
defaultHOST = defaultHTTPHost
}
invalids := map[string]string{ invalids := map[string]string{
"0.0.0.0": "Invalid bind address format: 0.0.0.0", "0.0.0.0": "Invalid bind address format: 0.0.0.0",
"tcp:a.b.c.d": "Invalid bind address format: tcp:a.b.c.d", "tcp:a.b.c.d": "Invalid bind address format: tcp:a.b.c.d",
@ -67,9 +57,11 @@ func TestParseDockerDaemonHost(t *testing.T) {
"udp://127.0.0.1": "Invalid bind address format: udp://127.0.0.1", "udp://127.0.0.1": "Invalid bind address format: udp://127.0.0.1",
"udp://127.0.0.1:2375": "Invalid bind address format: udp://127.0.0.1:2375", "udp://127.0.0.1:2375": "Invalid bind address format: udp://127.0.0.1:2375",
"tcp://unix:///run/docker.sock": "Invalid bind address format: unix", "tcp://unix:///run/docker.sock": "Invalid bind address format: unix",
"tcp": "Invalid bind address format: tcp", " tcp://:7777/path ": "Invalid bind address format: tcp://:7777/path ",
"unix": "Invalid bind address format: unix", "tcp": "Invalid bind address format: tcp",
"fd": "Invalid bind address format: fd", "unix": "Invalid bind address format: unix",
"fd": "Invalid bind address format: fd",
"": "Invalid bind address format: ",
} }
valids := map[string]string{ valids := map[string]string{
"0.0.0.1:": "tcp://0.0.0.1:2375", "0.0.0.1:": "tcp://0.0.0.1:2375",
@ -79,17 +71,13 @@ func TestParseDockerDaemonHost(t *testing.T) {
"[::1]:5555/path": "tcp://[::1]:5555/path", "[::1]:5555/path": "tcp://[::1]:5555/path",
"[0:0:0:0:0:0:0:1]:": "tcp://[0:0:0:0:0:0:0:1]:2375", "[0:0:0:0:0:0:0:1]:": "tcp://[0:0:0:0:0:0:0:1]:2375",
"[0:0:0:0:0:0:0:1]:5555/path": "tcp://[0:0:0:0:0:0:0:1]:5555/path", "[0:0:0:0:0:0:0:1]:5555/path": "tcp://[0:0:0:0:0:0:0:1]:5555/path",
":6666": "tcp://localhost:6666", ":6666": fmt.Sprintf("tcp://%s:6666", DefaultHTTPHost),
":6666/path": "tcp://localhost:6666/path", ":6666/path": fmt.Sprintf("tcp://%s:6666/path", DefaultHTTPHost),
"": defaultHOST, "tcp://": DefaultTCPHost,
" ": defaultHOST, "tcp://:7777": fmt.Sprintf("tcp://%s:7777", DefaultHTTPHost),
" ": defaultHOST, "tcp://:7777/path": fmt.Sprintf("tcp://%s:7777/path", DefaultHTTPHost),
"tcp://": defaultHTTPHost,
"tcp://:7777": "tcp://localhost:7777",
"tcp://:7777/path": "tcp://localhost:7777/path",
" tcp://:7777/path ": "tcp://localhost:7777/path",
"unix:///run/docker.sock": "unix:///run/docker.sock", "unix:///run/docker.sock": "unix:///run/docker.sock",
"unix://": "unix:///var/run/docker.sock", "unix://": "unix://" + DefaultUnixSocket,
"fd://": "fd://", "fd://": "fd://",
"fd://something": "fd://something", "fd://something": "fd://something",
"localhost:": "tcp://localhost:2375", "localhost:": "tcp://localhost:2375",
@ -97,12 +85,12 @@ func TestParseDockerDaemonHost(t *testing.T) {
"localhost:5555/path": "tcp://localhost:5555/path", "localhost:5555/path": "tcp://localhost:5555/path",
} }
for invalidAddr, expectedError := range invalids { for invalidAddr, expectedError := range invalids {
if addr, err := parseDockerDaemonHost(defaultHTTPHost, defaultHTTPSHost, defaultUnix, "", invalidAddr); err == nil || err.Error() != expectedError { if addr, err := parseDockerDaemonHost(invalidAddr); err == nil || err.Error() != expectedError {
t.Errorf("tcp %v address expected error %v return, got %s and addr %v", invalidAddr, expectedError, err, addr) t.Errorf("tcp %v address expected error %v return, got %s and addr %v", invalidAddr, expectedError, err, addr)
} }
} }
for validAddr, expectedAddr := range valids { for validAddr, expectedAddr := range valids {
if addr, err := parseDockerDaemonHost(defaultHTTPHost, defaultHTTPSHost, defaultUnix, "", validAddr); err != nil || addr != expectedAddr { if addr, err := parseDockerDaemonHost(validAddr); err != nil || addr != expectedAddr {
t.Errorf("%v -> expected %v, got (%v) addr (%v)", validAddr, expectedAddr, err, addr) t.Errorf("%v -> expected %v, got (%v) addr (%v)", validAddr, expectedAddr, err, addr)
} }
} }
@ -152,13 +140,13 @@ func TestParseTCP(t *testing.T) {
} }
func TestParseInvalidUnixAddrInvalid(t *testing.T) { func TestParseInvalidUnixAddrInvalid(t *testing.T) {
if _, err := parseUnixAddr("tcp://127.0.0.1", "unix:///var/run/docker.sock"); err == nil || err.Error() != "Invalid proto, expected unix: tcp://127.0.0.1" { if _, err := parseSimpleProtoAddr("unix", "tcp://127.0.0.1", "unix:///var/run/docker.sock"); err == nil || err.Error() != "Invalid proto, expected unix: tcp://127.0.0.1" {
t.Fatalf("Expected an error, got %v", err) t.Fatalf("Expected an error, got %v", err)
} }
if _, err := parseUnixAddr("unix://tcp://127.0.0.1", "/var/run/docker.sock"); err == nil || err.Error() != "Invalid proto, expected unix: tcp://127.0.0.1" { if _, err := parseSimpleProtoAddr("unix", "unix://tcp://127.0.0.1", "/var/run/docker.sock"); err == nil || err.Error() != "Invalid proto, expected unix: tcp://127.0.0.1" {
t.Fatalf("Expected an error, got %v", err) t.Fatalf("Expected an error, got %v", err)
} }
if v, err := parseUnixAddr("", "/var/run/docker.sock"); err != nil || v != "unix:///var/run/docker.sock" { if v, err := parseSimpleProtoAddr("unix", "", "/var/run/docker.sock"); err != nil || v != "unix:///var/run/docker.sock" {
t.Fatalf("Expected an %v, got %v", v, "unix:///var/run/docker.sock") t.Fatalf("Expected an %v, got %v", v, "unix:///var/run/docker.sock")
} }
} }