From 032f86bc78cb263522950aa94da3ae81b178fa49 Mon Sep 17 00:00:00 2001 From: Mahmood Ali Date: Wed, 7 Nov 2018 11:53:11 -0800 Subject: [PATCH] Add a helper functions for checking unix root --- drivers/exec/driver.go | 3 +-- plugins/drivers/utils/utils_unix.go | 7 +++++++ plugins/drivers/utils/utils_windows.go | 5 +++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/drivers/exec/driver.go b/drivers/exec/driver.go index 7a2ab157e..e088f0694 100644 --- a/drivers/exec/driver.go +++ b/drivers/exec/driver.go @@ -22,7 +22,6 @@ import ( "github.com/hashicorp/nomad/plugins/shared/hclspec" "github.com/hashicorp/nomad/plugins/shared/loader" "golang.org/x/net/context" - "golang.org/x/sys/unix" ) const ( @@ -207,7 +206,7 @@ func (d *Driver) buildFingerprint() *drivers.Fingerprint { return fp } - if unix.Geteuid() != 0 { + if !utils.IsUnixRoot() { fp.Health = drivers.HealthStateUnhealthy fp.HealthDescription = "exec driver must run as root" return fp diff --git a/plugins/drivers/utils/utils_unix.go b/plugins/drivers/utils/utils_unix.go index f720a6e22..85d9fe6c0 100644 --- a/plugins/drivers/utils/utils_unix.go +++ b/plugins/drivers/utils/utils_unix.go @@ -5,6 +5,8 @@ package utils import ( "os/exec" "syscall" + + "golang.org/x/sys/unix" ) // isolateCommand sets the setsid flag in exec.Cmd to true so that the process @@ -16,3 +18,8 @@ func isolateCommand(cmd *exec.Cmd) { } cmd.SysProcAttr.Setsid = true } + +// IsUnixRoot returns true if system is unix and user running is effectively root +func IsUnixRoot() bool { + return unix.Geteuid() == 0 +} diff --git a/plugins/drivers/utils/utils_windows.go b/plugins/drivers/utils/utils_windows.go index a12de4d9e..8243e498a 100644 --- a/plugins/drivers/utils/utils_windows.go +++ b/plugins/drivers/utils/utils_windows.go @@ -6,3 +6,8 @@ import ( // TODO Figure out if this is needed in Windows func isolateCommand(cmd *exec.Cmd) {} + +// IsUnixRoot returns true if system is a unix system and the effective uid of user is root +func IsUnixRoot() bool { + return false +}