Skip to content

Commit

Permalink
lxd/provider: Use os dependant socket check
Browse files Browse the repository at this point in the history
Signed-off-by: Din Music <[email protected]>
  • Loading branch information
MusicDin committed Oct 30, 2023
1 parent 74d97de commit e6e83b5
Showing 1 changed file with 3 additions and 15 deletions.
18 changes: 3 additions & 15 deletions lxd/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/canonical/lxd/shared"
lxd_api "github.com/canonical/lxd/shared/api"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"golang.org/x/sys/unix"
)

// A global mutex.
Expand Down Expand Up @@ -770,7 +769,7 @@ func determineDaemonAddr(lxdRemote terraformLXDConfig) (string, error) {
func determineLxdDir() (string, error) {
lxdSocket, ok := os.LookupEnv("LXD_SOCKET")
if ok {
if isSocketWritable(lxdSocket) {
if IsSocketWritable(lxdSocket) {
return path.Dir(lxdSocket), nil
}

Expand All @@ -780,7 +779,7 @@ func determineLxdDir() (string, error) {
lxdDir, ok := os.LookupEnv("LXD_DIR")
if ok {
socketPath := path.Join(lxdDir, "unix.socket")
if isSocketWritable(socketPath) {
if IsSocketWritable(socketPath) {
return lxdDir, nil
}

Expand All @@ -795,21 +794,10 @@ func determineLxdDir() (string, error) {
// Iterate over LXD directories and find a writable unix socket.
for _, lxdDir := range lxdDirs {
socketPath := path.Join(lxdDir, "unix.socket")
if isSocketWritable(socketPath) {
if IsSocketWritable(socketPath) {
return lxdDir, nil
}
}

return "", fmt.Errorf("LXD socket with write permissions not found. Searched LXD directories: %v", lxdDirs)
}

// isSocketWritable returns true if user has write permissions for socket on the given path.
func isSocketWritable(socketPath string) bool {
err := unix.Access(socketPath, unix.W_OK)
if err != nil {
log.Printf("[DEBUG] Unix socket %q: %v", socketPath, err)
return false
}

return true
}

0 comments on commit e6e83b5

Please sign in to comment.