Skip to content

Commit a0659fb

Browse files
author
Sean Christopherson
committed
Fix a panic in 'remote add'
Use strings.TrimPrefix to strip 'unix://' from the URL/path in both remoteCmd.addServer() and connectViaUnix. This resolves the direct cause of the panic and makes the handling of relative paths consistent in both flows. Add a shared util IsUnixSocket() and use it when determining whether a path without an explicit scheme should be treated as HTTPS or UNIX. Remove an extra '/' from the path used for TestLocalLXDError, adding a single slash makes an absolute path look like a relative path with a 'unix://' scheme. Fixes canonical#2089 Signed-off-by: Sean Christopherson <[email protected]>
1 parent 4090c39 commit a0659fb

File tree

4 files changed

+15
-19
lines changed

4 files changed

+15
-19
lines changed

Diff for: client.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,7 @@ func connectViaUnix(c *Client, remote *RemoteConfig) error {
255255
// unix:///path/to/socket
256256
// unix:/path/to/socket
257257
// unix:path/to/socket
258-
path := strings.TrimPrefix(remote.Addr, "unix:")
259-
if strings.HasPrefix(path, "///") {
260-
// translate unix:///path/to, to just "/path/to"
261-
path = path[2:]
262-
}
258+
path := strings.TrimPrefix(strings.TrimPrefix(remote.Addr, "unix:"), "//")
263259
raddr, err := net.ResolveUnixAddr("unix", path)
264260
if err != nil {
265261
return nil, err

Diff for: client_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestLocalLXDError(t *testing.T) {
2323
Name: "test",
2424
Config: DefaultConfig,
2525
Remote: &RemoteConfig{
26-
Addr: fmt.Sprintf("unix:/%s", f.Name()),
26+
Addr: fmt.Sprintf("unix:%s", f.Name()),
2727
Static: true,
2828
Public: false,
2929
},

Diff for: lxc/remote.go

+3-13
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func (c *remoteCmd) addServer(config *lxd.Config, server string, addr string, ac
126126
} else if addr[0] == '/' {
127127
rScheme = "unix"
128128
} else {
129-
if !shared.PathExists(addr) {
129+
if !shared.IsUnixSocket(addr) {
130130
rScheme = "https"
131131
} else {
132132
rScheme = "unix"
@@ -148,17 +148,7 @@ func (c *remoteCmd) addServer(config *lxd.Config, server string, addr string, ac
148148
}
149149

150150
if rScheme == "unix" {
151-
if addr[0:5] == "unix:" {
152-
if addr[0:7] == "unix://" {
153-
if len(addr) > 8 {
154-
rHost = addr[8:]
155-
} else {
156-
rHost = ""
157-
}
158-
} else {
159-
rHost = addr[6:]
160-
}
161-
}
151+
rHost = strings.TrimPrefix(strings.TrimPrefix(addr, "unix:"), "//")
162152
rPort = ""
163153
}
164154

@@ -181,7 +171,7 @@ func (c *remoteCmd) addServer(config *lxd.Config, server string, addr string, ac
181171
return err
182172
}
183173

184-
if len(addr) > 5 && addr[0:5] == "unix:" {
174+
if strings.HasPrefix(addr, "unix:") {
185175
// NewClient succeeded so there was a lxd there (we fingered
186176
// it) so just accept it
187177
return nil

Diff for: shared/util.go

+10
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ func IsDir(name string) bool {
7070
return stat.IsDir()
7171
}
7272

73+
// IsUnixSocket returns true if the given path is either a Unix socket
74+
// or a symbolic link pointing at a Unix socket.
75+
func IsUnixSocket(path string) bool {
76+
stat, err := os.Stat(path)
77+
if err != nil {
78+
return false
79+
}
80+
return (stat.Mode() & os.ModeSocket) == os.ModeSocket
81+
}
82+
7383
// VarPath returns the provided path elements joined by a slash and
7484
// appended to the end of $LXD_DIR, which defaults to /var/lib/lxd.
7585
func VarPath(path ...string) string {

0 commit comments

Comments
 (0)