diff --git a/command/agent/config.go b/command/agent/config.go index 05fa770e7..c49af33f0 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -805,13 +805,13 @@ type UnixSocketConfig struct { 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. -func unixSocketAddr(addr string) (string, bool) { +func socketPath(addr string) string { if !strings.HasPrefix(addr, "unix://") { - return "", false + return "" } - return strings.TrimPrefix(addr, "unix://"), true + return strings.TrimPrefix(addr, "unix://") } type dirEnts []os.FileInfo @@ -921,7 +921,7 @@ func (c *Config) ClientListener(override string, port int) (net.Addr, error) { addr = c.ClientAddr } - if path, ok := unixSocketAddr(addr); ok { + if path := socketPath(addr); path != "" { return &net.UnixAddr{Name: path, Net: "unix"}, nil } ip := net.ParseIP(addr) diff --git a/command/agent/config_test.go b/command/agent/config_test.go index 2d64264f0..1bb95bd40 100644 --- a/command/agent/config_test.go +++ b/command/agent/config_test.go @@ -1893,14 +1893,11 @@ func TestReadConfigPaths_dir(t *testing.T) { } func TestUnixSockets(t *testing.T) { - path1, ok := unixSocketAddr("unix:///path/to/socket") - if !ok || path1 != "/path/to/socket" { - t.Fatalf("bad: %v %v", ok, path1) + if p := socketPath("unix:///path/to/socket"); p != "/path/to/socket" { + t.Fatalf("bad: %q", p) } - - path2, ok := unixSocketAddr("notunix://blah") - if ok || path2 != "" { - t.Fatalf("bad: %v %v", ok, path2) + if p := socketPath("notunix://blah"); p != "" { + t.Fatalf("bad: %q", p) } } diff --git a/command/agent/http.go b/command/agent/http.go index 19831b0f6..e81bf7b7b 100644 --- a/command/agent/http.go +++ b/command/agent/http.go @@ -95,12 +95,12 @@ func NewHTTPServers(agent *Agent) ([]*HTTPServer, error) { } // Error if we are trying to bind a domain socket to an existing path - socketPath, isSocket := unixSocketAddr(config.Addresses.HTTP) - if isSocket { - if _, err := os.Stat(socketPath); !os.IsNotExist(err) { - agent.logger.Printf("[WARN] agent: Replacing socket %q", socketPath) + path := socketPath(config.Addresses.HTTP) + if path != "" { + if _, err := os.Stat(path); !os.IsNotExist(err) { + 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) } } @@ -111,9 +111,9 @@ func NewHTTPServers(agent *Agent) ([]*HTTPServer, error) { } var list net.Listener - if isSocket { + if path != "" { // 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) } list = ln diff --git a/command/agent/http_test.go b/command/agent/http_test.go index 9ff6fc3ed..db4fa1068 100644 --- a/command/agent/http_test.go +++ b/command/agent/http_test.go @@ -109,7 +109,7 @@ func TestHTTPServer_UnixSocket(t *testing.T) { } // 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.DialContext = func(_ context.Context, _, _ string) (net.Conn, error) { return net.Dial("unix", path)