Skip to content

Commit

Permalink
Merge pull request #365 from MusicDin/fix/socket-win
Browse files Browse the repository at this point in the history
Fix unix socket check on Windows
  • Loading branch information
adamcstephens authored Oct 30, 2023
2 parents f129f13 + e6e83b5 commit 2bd174b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
20 changes: 20 additions & 0 deletions lxd/fs_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build !windows

package lxd

import (
"log"

"golang.org/x/sys/unix"
)

// 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
}
8 changes: 8 additions & 0 deletions lxd/fs_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build windows

package lxd

// IsSocketWritable always returns true when os is windows.
func IsSocketWritable(socketPath string) bool {
return true
}
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 2bd174b

Please sign in to comment.