Skip to content

Commit

Permalink
Merge pull request #1520 from nerdalert/nexctl-priv-log
Browse files Browse the repository at this point in the history
Better logging for privileges notification for nexctl nexd cmds
  • Loading branch information
mergify[bot] authored Mar 18, 2024
2 parents 20c7f8b + 1057a59 commit 67390ab
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/nexctl/local_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ func init() {
Required: false,
},
},
Before: func(ctx context.Context, command *cli.Command) error {
if err := hasPrivileges(); err != nil {
return err
}
return nil
},
Commands: []*cli.Command{
{
Name: "version",
Expand Down
84 changes: 84 additions & 0 deletions cmd/nexctl/privileges_check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package main

import (
"fmt"
"github.com/nexodus-io/nexodus/internal/api"
"net"
"os"
"path/filepath"
"runtime"
)

// hasPrivileges checks to see if we can access nexd over the admin interface (unix socket)
func hasPrivileges() error {

// the true test if we have privileges is if we can open the socket file.
err := canAccessSocketAPI()
if err == nil {
return nil
}

// If we can't open it's likely we are not running as root/admin. Return a helpful error message....
switch osType := getOSType(); osType {
case "linux":
if !isLinuxRoot() {
return fmt.Errorf("'nexctl nexd' commands must be run with sudo on Linux")
}
case "darwin":
if !isDarwinRoot() {
return fmt.Errorf("'nexctl nexd' commands must be run with sudo on macOS")
}
case "windows":
if !isWindowsAdmin() {
return fmt.Errorf("'nexctl nexd' commands must be run with administrator privileges on Windows")
}
default:
return fmt.Errorf("unsupported operating system type: %s", osType)
}

// we are not running as root/admin and we can't open the socket file. It's likely nexd is not running.
return fmt.Errorf("is nexd running?: %w", err)
}

// getOSType gets the operating system.
func getOSType() string {
switch os := runtime.GOOS; os {
case "linux":
return "linux"
case "darwin":
return "darwin"
case "windows":
return "windows"
default:
return "unknown"
}
}

// isLinuxRoot checks if the program is running as root on Linux.
func isLinuxRoot() bool {
return os.Geteuid() == 0
}

// isDarwinRoot checks if the program is running as root on macOS.
func isDarwinRoot() bool {
return os.Geteuid() == 0
}

// isWindowsAdmin checks if the program is running with administrative privileges on Windows.
func isWindowsAdmin() bool {
// Check for admin privileges on Windows.
_, err := os.Open("\\\\.\\PHYSICALDRIVE0")
return err == nil
}

func canAccessSocketAPI() error {
conn, err := net.Dial("unix", api.UnixSocketPath)
if err != nil {
conn, err = net.Dial("unix", filepath.Base(api.UnixSocketPath))
if err != nil {
return fmt.Errorf("failed to connect to nexd at '%s': %w\n", api.UnixSocketPath, err)
}
}
conn.Close()
return nil
}

0 comments on commit 67390ab

Please sign in to comment.