Skip to content

Commit 4c54a21

Browse files
authored
Merge pull request #3101 from mvo5/explicit-use-of-snapd-sockets-2.23
cmd: explicit use of snapd sockets (for 2.23)
2 parents 8181a37 + 6929f96 commit 4c54a21

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

client/client.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,15 @@ import (
3030
"net/url"
3131
"os"
3232
"path"
33-
"syscall"
3433
"time"
3534

3635
"github.com/snapcore/snapd/dirs"
3736
)
3837

39-
func unixDialer() func(string, string) (net.Conn, error) {
40-
// We have two sockets available: the SnapdSocket (which provides
41-
// administrative access), and the SnapSocket (which doesn't). Use the most
42-
// powerful one available (e.g. from within snaps, SnapdSocket is hidden by
43-
// apparmor unless the snap has the snapd-control interface).
44-
socketPath := dirs.SnapdSocket
45-
file, err := os.OpenFile(socketPath, os.O_RDWR, 0666)
46-
if err == nil {
47-
file.Close()
48-
} else if e, ok := err.(*os.PathError); ok && (e.Err == syscall.ENOENT || e.Err == syscall.EACCES) {
49-
socketPath = dirs.SnapSocket
38+
func unixDialer(socketPath string) func(string, string) (net.Conn, error) {
39+
if socketPath == "" {
40+
socketPath = dirs.SnapdSocket
5041
}
51-
5242
return func(_, _ string) (net.Conn, error) {
5343
return net.Dial("unix", socketPath)
5444
}
@@ -67,6 +57,9 @@ type Config struct {
6757
// DisableAuth controls whether the client should send an
6858
// Authorization header from reading the auth.json data.
6959
DisableAuth bool
60+
61+
// Socket is the path to the unix socket to use
62+
Socket string
7063
}
7164

7265
// A Client knows how to talk to the snappy daemon.
@@ -91,7 +84,7 @@ func New(config *Config) *Client {
9184
Host: "localhost",
9285
},
9386
doer: &http.Client{
94-
Transport: &http.Transport{Dial: unixDialer()},
87+
Transport: &http.Transport{Dial: unixDialer(config.Socket)},
9588
},
9689
disableAuth: config.DisableAuth,
9790
}

client/client_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,9 @@ func (cs *clientSuite) TestSnapClientIntegration(c *C) {
252252
srv.Start()
253253
defer srv.Close()
254254

255-
cli := client.New(nil)
255+
cli := client.New(&client.Config{
256+
Socket: dirs.SnapSocket,
257+
})
256258
options := &client.SnapCtlOptions{
257259
ContextID: "foo",
258260
Args: []string{"bar", "--baz"},

cmd/snap/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,10 @@ snaps on the system. Start with 'snap list' to see installed snaps.
209209
}
210210

211211
// ClientConfig is the configuration of the Client used by all commands.
212-
var ClientConfig client.Config
212+
var ClientConfig = client.Config{
213+
// we need the powerful snapd socket
214+
Socket: dirs.SnapdSocket,
215+
}
213216

214217
// Client returns a new client using ClientConfig as configuration.
215218
func Client() *client.Client {

cmd/snapctl/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@ import (
2424
"os"
2525

2626
"github.com/snapcore/snapd/client"
27+
"github.com/snapcore/snapd/dirs"
2728
)
2829

2930
var clientConfig = client.Config{
3031
// snapctl should not try to read $HOME/.snap/auth.json, this will
3132
// result in apparmor denials and configure task failures
3233
// (LP: #1660941)
3334
DisableAuth: true,
35+
36+
// we need the less privileged snap socket in snapctl
37+
Socket: dirs.SnapSocket,
3438
}
3539

3640
func main() {

0 commit comments

Comments
 (0)