From 99e8d7e34044316ad1f55fdb801c4e81aa578538 Mon Sep 17 00:00:00 2001 From: Anjan Nath Date: Fri, 30 Aug 2019 16:38:47 +0530 Subject: [PATCH] Issue #497 Add preflight check to see if ran as root/admin --- pkg/crc/preflight/preflight_checks_common.go | 12 ++++++++++++ pkg/crc/preflight/preflight_checks_windows.go | 14 +++++++++++++- pkg/crc/preflight/preflight_darwin.go | 11 +++++++++++ pkg/crc/preflight/preflight_linux.go | 11 +++++++++++ pkg/crc/preflight/preflight_windows.go | 11 +++++++++++ 5 files changed, 58 insertions(+), 1 deletion(-) diff --git a/pkg/crc/preflight/preflight_checks_common.go b/pkg/crc/preflight/preflight_checks_common.go index 0cb386321b..e50f7e9fff 100644 --- a/pkg/crc/preflight/preflight_checks_common.go +++ b/pkg/crc/preflight/preflight_checks_common.go @@ -69,3 +69,15 @@ func fixOcBinaryCached() (bool, error) { logging.Debug("oc binary cached") return true, nil } + +func checkIfRunningAsNormalUser() (bool, error) { + if os.Geteuid() != 0 { + return true, nil + } + logging.Debug("Ran as root") + return false, fmt.Errorf("crc should be ran as a normal user") +} + +func fixRunAsNormalUser() (bool, error) { + return false, fmt.Errorf("crc should be ran as a normal user") +} diff --git a/pkg/crc/preflight/preflight_checks_windows.go b/pkg/crc/preflight/preflight_checks_windows.go index dbb42defaf..c1235006f2 100644 --- a/pkg/crc/preflight/preflight_checks_windows.go +++ b/pkg/crc/preflight/preflight_checks_windows.go @@ -1,13 +1,13 @@ package preflight import ( - //"errors" "fmt" "os" "strconv" "strings" "github.com/code-ready/crc/pkg/crc/errors" + "github.com/code-ready/crc/pkg/crc/logging" "github.com/code-ready/crc/pkg/os/windows/powershell" ) @@ -126,3 +126,15 @@ func checkIfHyperVVirtualSwitchExists() (bool, error) { func fixHyperVVirtualSwitch() (bool, error) { return false, errors.New("Please override the default by adding an external virtual switch and set configuration") } + +func checkIfRunningAsNormalUserInWindows() (bool, error) { + if !powershell.IsAdmin() { + return true, nil + } + logging.Debug("Ran as administrator") + return false, fmt.Errorf("crc should be ran as a normal user") +} + +func fixRunAsNormalUserInWindows() (bool, error) { + return false, fmt.Errorf("crc should be ran as a normal user") +} diff --git a/pkg/crc/preflight/preflight_darwin.go b/pkg/crc/preflight/preflight_darwin.go index a4e65cc724..873cf93a76 100644 --- a/pkg/crc/preflight/preflight_darwin.go +++ b/pkg/crc/preflight/preflight_darwin.go @@ -8,6 +8,11 @@ import ( // StartPreflightChecks performs the preflight checks before starting the cluster func StartPreflightChecks(vmDriver string) { + preflightCheckSucceedsOrFails(false, + checkIfRunningAsNormalUser, + "Checking if running as root", + false, + ) preflightCheckSucceedsOrFails(false, checkOcBinaryCached, "Checking if oc binary is cached", @@ -49,6 +54,12 @@ func StartPreflightChecks(vmDriver string) { // SetupHost performs the prerequisite checks and setups the host to run the cluster func SetupHost(vmDriver string) { + preflightCheckAndFix(false, + checkIfRunningAsNormalUser, + fixRunAsNormalUser, + "Checking if running as root", + false, + ) preflightCheckAndFix(false, checkOcBinaryCached, fixOcBinaryCached, diff --git a/pkg/crc/preflight/preflight_linux.go b/pkg/crc/preflight/preflight_linux.go index 1c47dc89c8..0128b0adde 100644 --- a/pkg/crc/preflight/preflight_linux.go +++ b/pkg/crc/preflight/preflight_linux.go @@ -7,6 +7,11 @@ import ( // StartPreflightChecks performs the preflight checks before starting the cluster func StartPreflightChecks(vmDriver string) { + preflightCheckSucceedsOrFails(false, + checkIfRunningAsNormalUser, + "Checking if running as root", + false, + ) preflightCheckSucceedsOrFails(false, checkOcBinaryCached, "Checking if oc binary is cached", @@ -81,6 +86,12 @@ func StartPreflightChecks(vmDriver string) { // SetupHost performs the prerequisite checks and setups the host to run the cluster func SetupHost(vmDriver string) { + preflightCheckAndFix(false, + checkIfRunningAsNormalUser, + fixRunAsNormalUser, + "Checking if running as root", + false, + ) preflightCheckAndFix(false, checkOcBinaryCached, fixOcBinaryCached, diff --git a/pkg/crc/preflight/preflight_windows.go b/pkg/crc/preflight/preflight_windows.go index 01495809cc..ea4bb8e046 100644 --- a/pkg/crc/preflight/preflight_windows.go +++ b/pkg/crc/preflight/preflight_windows.go @@ -9,6 +9,11 @@ import ( // StartPreflightChecks performs the preflight checks before starting the cluster func StartPreflightChecks(vmDriver string) { + preflightCheckSucceedsOrFails(false, + checkIfRunningAsNormalUserInWindows, + "Checking if running as adminstrator", + false, + ) preflightCheckSucceedsOrFails(false, checkOcBinaryCached, "Checking if oc binary is cached", @@ -41,6 +46,12 @@ func StartPreflightChecks(vmDriver string) { // SetupHost performs the prerequisite checks and setups the host to run the cluster func SetupHost(vmDriver string) { + preflightCheckAndFix(false, + checkIfRunningAsNormalUserInWindows, + fixRunAsNormalUserInWindows, + "Checking if running as adminstrator", + false, + ) preflightCheckAndFix(false, checkOcBinaryCached, fixOcBinaryCached,