Skip to content

Commit

Permalink
Issue #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 authored and praveenkumar committed Sep 6, 2019
1 parent fe39a0d commit 99e8d7e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
12 changes: 12 additions & 0 deletions pkg/crc/preflight/preflight_checks_common.go
Expand Up @@ -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")
}
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 @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions pkg/crc/preflight/preflight_linux.go
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
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 @@ -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,
Expand Down

0 comments on commit 99e8d7e

Please sign in to comment.