Ensure a socket is created for permissions adjustment tests and fix some

items pointed out in the code review

This code is copyright 2014 Akamai Technologies, Inc. <opensource@akamai.com>
This commit is contained in:
Jeff Mitchell 2015-01-14 21:25:12 +00:00
parent 5a9bcd36ef
commit 9fcea08dbb
6 changed files with 61 additions and 51 deletions

View File

@ -249,7 +249,7 @@ func (r *request) toHTTP() (*http.Request, error) {
req.Host = r.url.Host req.Host = r.url.Host
// Setup auth // Setup auth
if err == nil && r.config.HttpAuth != nil { if r.config.HttpAuth != nil {
req.SetBasicAuth(r.config.HttpAuth.Username, r.config.HttpAuth.Password) req.SetBasicAuth(r.config.HttpAuth.Username, r.config.HttpAuth.Password)
} }

View File

@ -20,7 +20,7 @@ type testServer struct {
configFile string configFile string
} }
type TestPortConfig struct { type testPortConfig struct {
DNS int `json:"dns,omitempty"` DNS int `json:"dns,omitempty"`
HTTP int `json:"http,omitempty"` HTTP int `json:"http,omitempty"`
RPC int `json:"rpc,omitempty"` RPC int `json:"rpc,omitempty"`
@ -29,24 +29,25 @@ type TestPortConfig struct {
Server int `json:"server,omitempty"` Server int `json:"server,omitempty"`
} }
type TestAddressConfig struct { type testAddressConfig struct {
HTTP string `json:"http,omitempty"` HTTP string `json:"http,omitempty"`
} }
type TestServerConfig struct { type testServerConfig struct {
Bootstrap bool `json:"bootstrap,omitempty"` Bootstrap bool `json:"bootstrap,omitempty"`
Server bool `json:"server,omitempty"` Server bool `json:"server,omitempty"`
DataDir string `json:"data_dir,omitempty"` DataDir string `json:"data_dir,omitempty"`
LogLevel string `json:"log_level,omitempty"` LogLevel string `json:"log_level,omitempty"`
Addresses *TestAddressConfig `json:"addresses,omitempty"` Addresses *testAddressConfig `json:"addresses,omitempty"`
Ports TestPortConfig `json:"ports,omitempty"` Ports testPortConfig `json:"ports,omitempty"`
} }
var consulConfig = &TestServerConfig{ func defaultConfig() *testServerConfig {
return &testServerConfig{
Bootstrap: true, Bootstrap: true,
Server: true, Server: true,
LogLevel: "debug", LogLevel: "debug",
Ports: TestPortConfig{ Ports: testPortConfig{
DNS: 19000, DNS: 19000,
HTTP: 18800, HTTP: 18800,
RPC: 18600, RPC: 18600,
@ -55,6 +56,7 @@ var consulConfig = &TestServerConfig{
Server: 18000, Server: 18000,
}, },
} }
}
func (s *testServer) stop() { func (s *testServer) stop() {
defer os.RemoveAll(s.dataDir) defer os.RemoveAll(s.dataDir)
@ -67,10 +69,10 @@ func (s *testServer) stop() {
} }
func newTestServer(t *testing.T) *testServer { func newTestServer(t *testing.T) *testServer {
return newTestServerWithConfig(t, func(c *TestServerConfig) {}) return newTestServerWithConfig(t, func(c *testServerConfig) {})
} }
func newTestServerWithConfig(t *testing.T, cb func(c *TestServerConfig)) *testServer { func newTestServerWithConfig(t *testing.T, cb func(c *testServerConfig)) *testServer {
if path, err := exec.LookPath("consul"); err != nil || path == "" { if path, err := exec.LookPath("consul"); err != nil || path == "" {
t.Log("consul not found on $PATH, skipping") t.Log("consul not found on $PATH, skipping")
t.SkipNow() t.SkipNow()
@ -93,6 +95,7 @@ func newTestServerWithConfig(t *testing.T, cb func(c *TestServerConfig)) *testSe
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
consulConfig := defaultConfig()
consulConfig.DataDir = dataDir consulConfig.DataDir = dataDir
cb(consulConfig) cb(consulConfig)
@ -125,10 +128,10 @@ func newTestServerWithConfig(t *testing.T, cb func(c *TestServerConfig)) *testSe
func makeClient(t *testing.T) (*Client, *testServer) { func makeClient(t *testing.T) (*Client, *testServer) {
return makeClientWithConfig(t, func(c *Config) { return makeClientWithConfig(t, func(c *Config) {
c.Address = "127.0.0.1:18800" c.Address = "127.0.0.1:18800"
}, func(c *TestServerConfig) {}) }, func(c *testServerConfig) {})
} }
func makeClientWithConfig(t *testing.T, clientConfig func(c *Config), serverConfig func(c *TestServerConfig)) (*Client, *testServer) { func makeClientWithConfig(t *testing.T, clientConfig func(c *Config), serverConfig func(c *testServerConfig)) (*Client, *testServer) {
server := newTestServerWithConfig(t, serverConfig) server := newTestServerWithConfig(t, serverConfig)
conf := DefaultConfig() conf := DefaultConfig()
clientConfig(conf) clientConfig(conf)

View File

@ -38,14 +38,14 @@ func TestStatusLeaderUnix(t *testing.T) {
c.Address = socket c.Address = socket
} }
serverConfig := func(c *TestServerConfig) { serverConfig := func(c *testServerConfig) {
user, err := user.Current() user, err := user.Current()
if err != nil { if err != nil {
t.Fatal("Could not get current user") t.Fatal("Could not get current user")
} }
if c.Addresses == nil { if c.Addresses == nil {
c.Addresses = &TestAddressConfig{} c.Addresses = &testAddressConfig{}
} }
c.Addresses.HTTP = socket + ";" + user.Uid + ";" + user.Gid + ";640" c.Addresses.HTTP = socket + ";" + user.Uid + ";" + user.Gid + ";640"
} }

View File

@ -384,16 +384,15 @@ func populateUnixSocket(addr string) (*UnixSocket, error) {
} else { } else {
userVal, err = user.Lookup(splitAddr[1]) userVal, err = user.Lookup(splitAddr[1])
} }
if err != nil { if err != nil {
return nil, fmt.Errorf("Invalid user given for Unix socket ownership: %v", splitAddr[1]) return nil, fmt.Errorf("Invalid user given for Unix socket ownership: %v", splitAddr[1])
} else { }
if uid64, err := strconv.ParseInt(userVal.Uid, 10, 32); err != nil { if uid64, err := strconv.ParseInt(userVal.Uid, 10, 32); err != nil {
return nil, fmt.Errorf("Failed to parse given user ID of %v into integer", userVal.Uid) return nil, fmt.Errorf("Failed to parse given user ID of %v into integer", userVal.Uid)
} else { } else {
ret.Uid = int(uid64) ret.Uid = int(uid64)
} }
}
// Go doesn't currently have a way to look up gid from group name, // Go doesn't currently have a way to look up gid from group name,
// so require a numeric gid; see // so require a numeric gid; see

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/base64" "encoding/base64"
"io/ioutil" "io/ioutil"
"net"
"os" "os"
"os/user" "os/user"
"path/filepath" "path/filepath"
@ -1078,12 +1079,12 @@ func TestUnixSockets(t *testing.T) {
usr, err := user.Current() usr, err := user.Current()
if err != nil { if err != nil {
t.Fatal("Could not get current user") t.Fatal("Could not get current user: ", err)
} }
tempdir, err := ioutil.TempDir("", "consul-test-") tempdir, err := ioutil.TempDir("", "consul-test-")
if err != nil { if err != nil {
t.Fatal("Could not create a working directory") t.Fatal("Could not create a working directory: ", err)
} }
type SocketTestData struct { type SocketTestData struct {
@ -1124,12 +1125,6 @@ func TestUnixSockets(t *testing.T) {
t.Fatal("Did not error on invalid username") t.Fatal("Did not error on invalid username")
} }
std.Uid = "999999"
_, err = testUnixSocketPopulation(std)
if err == nil {
t.Fatal("Did not error on invalid uid")
}
std.Uid = usr.Username std.Uid = usr.Username
std.Gid = "foinfphawepofhewof" std.Gid = "foinfphawepofhewof"
_, err = testUnixSocketPopulation(std) _, err = testUnixSocketPopulation(std)
@ -1137,13 +1132,6 @@ func TestUnixSockets(t *testing.T) {
t.Fatal("Did not error on invalid group (a name, must be gid)") t.Fatal("Did not error on invalid group (a name, must be gid)")
} }
std.Uid = usr.Uid
std.Gid = "999999"
_, err = testUnixSocketPopulation(std)
if err == nil {
t.Fatal("Did not error on invalid uid")
}
std.Gid = usr.Gid std.Gid = usr.Gid
std.Mode = "999" std.Mode = "999"
_, err = testUnixSocketPopulation(std) _, err = testUnixSocketPopulation(std)
@ -1155,17 +1143,37 @@ func TestUnixSockets(t *testing.T) {
std.Mode = "640" std.Mode = "640"
_, err = testUnixSocketPopulation(std) _, err = testUnixSocketPopulation(std)
if err != nil { if err != nil {
t.Fatal("Unix socket test failed for no obvious reason (using username)") t.Fatal("Unix socket test failed (using username): ", err)
} }
std.Uid = usr.Uid std.Uid = usr.Uid
_, err = testUnixSocketPopulation(std) sock, err := testUnixSocketPopulation(std)
if err != nil { if err != nil {
t.Fatal("Unix socket test failed for no obvious reason (using uid)") t.Fatal("Unix socket test failed (using uid): ", err)
} }
addr := &net.UnixAddr{Name: sock.Path, Net: "unix"}
_, err = net.Listen(addr.Network(), addr.String())
if err != nil {
t.Fatal("Error creating socket for futher tests: ", err)
}
std.Uid = "-999999"
err = testUnixSocketPermissions(std)
if err == nil {
t.Fatal("Did not error on invalid uid")
}
std.Uid = usr.Uid
std.Gid = "-999999"
err = testUnixSocketPermissions(std)
if err == nil {
t.Fatal("Did not error on invalid uid")
}
std.Gid = usr.Gid
err = testUnixSocketPermissions(std) err = testUnixSocketPermissions(std)
if err != nil { if err != nil {
t.Fatal("Adjusting socket permissions failed for no obvious reason") t.Fatal("Adjusting socket permissions failed: ", err)
} }
} }

View File

@ -223,12 +223,12 @@ func TestRPCClientStatsUnix(t *testing.T) {
tempdir, err := ioutil.TempDir("", "consul-test-") tempdir, err := ioutil.TempDir("", "consul-test-")
if err != nil { if err != nil {
t.Fatal("Could not create a working directory") t.Fatal("Could not create a working directory: ", err)
} }
user, err := user.Current() user, err := user.Current()
if err != nil { if err != nil {
t.Fatal("Could not get current user") t.Fatal("Could not get current user: ", err)
} }
cb := func(c *Config) { cb := func(c *Config) {