mirror of https://github.com/hashicorp/consul
agent: simplify socket address helper
parent
c44e41a741
commit
b42916e1ff
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue