Skip to content

Commit

Permalink
Issue crc-org#497 Add preflight check to see if ran as root/admin
Browse files Browse the repository at this point in the history
  • Loading branch information
anjannath committed Sep 3, 2019
1 parent 77d64b4 commit 3a4870f
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
12 changes: 12 additions & 0 deletions pkg/crc/preflight/preflight_checks_common.go
Expand Up @@ -71,3 +71,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")
}
14 changes: 13 additions & 1 deletion 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"
)

Expand Down Expand Up @@ -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")
}
11 changes: 11 additions & 0 deletions pkg/crc/preflight/preflight_darwin.go
Expand Up @@ -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",
Expand Down Expand Up @@ -54,6 +59,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,
Expand Down
33 changes: 33 additions & 0 deletions pkg/crc/preflight/preflight_linux.go
Expand Up @@ -7,6 +7,21 @@ import (

// StartPreflightChecks performs the preflight checks before starting the cluster
func StartPreflightChecks(vmDriver string) {
preflightCheckSucceedsOrFails(false,
checkIfRunningAsNormalUser,
"Checking if running as root",
false,
)
preflightCheckSucceedsOrFails(config.GetBool(cmdConfig.SkipCheckNetworkManagerInstalled.Name),
checkNetworkManagerInstalled,
"Checking if NetworkManager is installed",
config.GetBool(cmdConfig.WarnCheckNetworkManagerInstalled.Name),
)
preflightCheckSucceedsOrFails(config.GetBool(cmdConfig.SkipCheckNetworkManagerRunning.Name),
CheckNetworkManagerIsRunning,
"Checking if NetworkManager service is running",
config.GetBool(cmdConfig.WarnCheckNetworkManagerRunning.Name),
)
preflightCheckSucceedsOrFails(false,
checkOcBinaryCached,
"Checking if oc binary is cached",
Expand Down Expand Up @@ -86,6 +101,24 @@ 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(config.GetBool(cmdConfig.SkipCheckNetworkManagerInstalled.Name),
checkNetworkManagerInstalled,
fixNetworkManagerInstalled,
"Checking if NetworkManager is installed",
config.GetBool(cmdConfig.WarnCheckNetworkManagerInstalled.Name),
)
preflightCheckAndFix(config.GetBool(cmdConfig.SkipCheckNetworkManagerRunning.Name),
CheckNetworkManagerIsRunning,
fixNetworkManagerIsRunning,
"Checking if NetworkManager service is running",
config.GetBool(cmdConfig.WarnCheckNetworkManagerRunning.Name),
)
preflightCheckAndFix(false,
checkOcBinaryCached,
fixOcBinaryCached,
Expand Down
11 changes: 11 additions & 0 deletions pkg/crc/preflight/preflight_windows.go
Expand Up @@ -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",
Expand Down Expand Up @@ -46,6 +51,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,
Expand Down

0 comments on commit 3a4870f

Please sign in to comment.