mirror of
https://github.com/status-im/consul.git
synced 2025-02-03 01:14:23 +00:00
agent: simplify socket address helper
This commit is contained in:
parent
c44e41a741
commit
b42916e1ff
@ -805,13 +805,13 @@ type UnixSocketConfig struct {
|
|||||||
UnixSocketPermissions `mapstructure:",squash"`
|
UnixSocketPermissions `mapstructure:",squash"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// unixSocketAddr tests if a given address describes a domain socket,
|
// socketPath tests if a given address describes a domain socket,
|
||||||
// and returns the relevant path part of the string if it is.
|
// and returns the relevant path part of the string if it is.
|
||||||
func unixSocketAddr(addr string) (string, bool) {
|
func socketPath(addr string) string {
|
||||||
if !strings.HasPrefix(addr, "unix://") {
|
if !strings.HasPrefix(addr, "unix://") {
|
||||||
return "", false
|
return ""
|
||||||
}
|
}
|
||||||
return strings.TrimPrefix(addr, "unix://"), true
|
return strings.TrimPrefix(addr, "unix://")
|
||||||
}
|
}
|
||||||
|
|
||||||
type dirEnts []os.FileInfo
|
type dirEnts []os.FileInfo
|
||||||
@ -921,7 +921,7 @@ func (c *Config) ClientListener(override string, port int) (net.Addr, error) {
|
|||||||
addr = c.ClientAddr
|
addr = c.ClientAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
if path, ok := unixSocketAddr(addr); ok {
|
if path := socketPath(addr); path != "" {
|
||||||
return &net.UnixAddr{Name: path, Net: "unix"}, nil
|
return &net.UnixAddr{Name: path, Net: "unix"}, nil
|
||||||
}
|
}
|
||||||
ip := net.ParseIP(addr)
|
ip := net.ParseIP(addr)
|
||||||
|
@ -1893,14 +1893,11 @@ func TestReadConfigPaths_dir(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUnixSockets(t *testing.T) {
|
func TestUnixSockets(t *testing.T) {
|
||||||
path1, ok := unixSocketAddr("unix:///path/to/socket")
|
if p := socketPath("unix:///path/to/socket"); p != "/path/to/socket" {
|
||||||
if !ok || path1 != "/path/to/socket" {
|
t.Fatalf("bad: %q", p)
|
||||||
t.Fatalf("bad: %v %v", ok, path1)
|
|
||||||
}
|
}
|
||||||
|
if p := socketPath("notunix://blah"); p != "" {
|
||||||
path2, ok := unixSocketAddr("notunix://blah")
|
t.Fatalf("bad: %q", p)
|
||||||
if ok || path2 != "" {
|
|
||||||
t.Fatalf("bad: %v %v", ok, path2)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,12 +95,12 @@ func NewHTTPServers(agent *Agent) ([]*HTTPServer, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Error if we are trying to bind a domain socket to an existing path
|
// Error if we are trying to bind a domain socket to an existing path
|
||||||
socketPath, isSocket := unixSocketAddr(config.Addresses.HTTP)
|
path := socketPath(config.Addresses.HTTP)
|
||||||
if isSocket {
|
if path != "" {
|
||||||
if _, err := os.Stat(socketPath); !os.IsNotExist(err) {
|
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
||||||
agent.logger.Printf("[WARN] agent: Replacing socket %q", socketPath)
|
agent.logger.Printf("[WARN] agent: Replacing socket %q", path)
|
||||||
}
|
}
|
||||||
if err := os.Remove(socketPath); err != nil && !os.IsNotExist(err) {
|
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
|
||||||
return nil, fmt.Errorf("error removing socket file: %s", err)
|
return nil, fmt.Errorf("error removing socket file: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -111,9 +111,9 @@ func NewHTTPServers(agent *Agent) ([]*HTTPServer, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var list net.Listener
|
var list net.Listener
|
||||||
if isSocket {
|
if path != "" {
|
||||||
// Set up ownership/permission bits on the socket file
|
// Set up ownership/permission bits on the socket file
|
||||||
if err := setFilePermissions(socketPath, config.UnixSockets); err != nil {
|
if err := setFilePermissions(path, config.UnixSockets); err != nil {
|
||||||
return nil, fmt.Errorf("Failed setting up HTTP socket: %s", err)
|
return nil, fmt.Errorf("Failed setting up HTTP socket: %s", err)
|
||||||
}
|
}
|
||||||
list = ln
|
list = ln
|
||||||
|
@ -109,7 +109,7 @@ func TestHTTPServer_UnixSocket(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Ensure we can get a response from the socket.
|
// Ensure we can get a response from the socket.
|
||||||
path, _ := unixSocketAddr(srv.agent.config.Addresses.HTTP)
|
path := socketPath(srv.agent.config.Addresses.HTTP)
|
||||||
trans := cleanhttp.DefaultTransport()
|
trans := cleanhttp.DefaultTransport()
|
||||||
trans.DialContext = func(_ context.Context, _, _ string) (net.Conn, error) {
|
trans.DialContext = func(_ context.Context, _, _ string) (net.Conn, error) {
|
||||||
return net.Dial("unix", path)
|
return net.Dial("unix", path)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user