Skip to content

Commit

Permalink
Issue crc-org#570 Print a reason why root access is needed before usi…
Browse files Browse the repository at this point in the history
…ng it

The RunWithPrivilege func now takes a string as its first argument
which is a 'reason' why root access is needed, this change is made
so that users are not  scared by seeing unreasonable prompt asking
them to enter their password by sudo.
  • Loading branch information
anjannath committed Sep 12, 2019
1 parent 7901366 commit 7e751d5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
16 changes: 8 additions & 8 deletions pkg/crc/preflight/preflight_checks_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ func fixVirtualBoxInstallation() (bool, error) {
}
defer os.Remove(tempFilePath)
logging.Debug("Installing VirtualBox")
stdOut, stdErr, err := crcos.RunWithPrivilege("hdiutil", "attach", tempFilePath)
stdOut, stdErr, err := crcos.RunWithPrivilege("mount VirtualBox disk image", "hdiutil", "attach", tempFilePath)
if err != nil {
return false, fmt.Errorf("Could not mount the virtualbox.dmg file: %s %v: %s", stdOut, err, stdErr)
}
stdOut, stdErr, err = crcos.RunWithPrivilege("installer", "-package", virtualBoxPkgLocation, "-target", "/")
stdOut, stdErr, err = crcos.RunWithPrivilege("run VirtualBox installation", "installer", "-package", virtualBoxPkgLocation, "-target", "/")
if err != nil {
return false, fmt.Errorf("Could not install VirtualBox.pkg: %s %v: %s", stdOut, err, stdErr)
}
stdOut, stdErr, err = crcos.RunWithPrivilege("hdiutil", "detach", virtualBoxMountLocation)
stdOut, stdErr, err = crcos.RunWithPrivilege("unmount VirtualBox disk image", "hdiutil", "detach", virtualBoxMountLocation)
if err != nil {
return false, fmt.Errorf("Could not install VirtualBox.pkg: %s %v: %s", stdOut, err, stdErr)
}
Expand Down Expand Up @@ -124,14 +124,14 @@ func download(url string, destDir string, mode os.FileMode) (string, error) {
func setSuid(path string) error {
logging.Debugf("Making %s suid", path)

stdOut, stdErr, err := crcos.RunWithPrivilege("chown", "root:wheel", path)
stdOut, stdErr, err := crcos.RunWithPrivilege(fmt.Sprintf("change ownership of %s", path), "chown", "root:wheel", path)
if err != nil {
return fmt.Errorf("Unable to set ownership of %s to root:wheel: %s %v: %s",
path, stdOut, err, stdErr)
}

/* Can't do this before the chown as the chown will reset the suid bit */
stdOut, stdErr, err = crcos.RunWithPrivilege("chmod", "u+s", path)
stdOut, stdErr, err = crcos.RunWithPrivilege(fmt.Sprintf("set suid for %s", path), "chmod", "u+s", path)
if err != nil {
return fmt.Errorf("Unable to set suid bit on %s: %s %v: %s", path, stdOut, err, stdErr)
}
Expand Down Expand Up @@ -222,13 +222,13 @@ func fixResolverFilePermissions() (bool, error) {
// Check if resolver directory available or not
if _, err := os.Stat(resolverDir); os.IsNotExist(err) {
logging.Debugf("Creating %s directory", resolverDir)
stdOut, stdErr, err := crcos.RunWithPrivilege("mkdir", resolverDir)
stdOut, stdErr, err := crcos.RunWithPrivilege(fmt.Sprintf("create dir %s", resolverDir), "mkdir", resolverDir)
if err != nil {
return false, fmt.Errorf("Unable to create the resolver Dir: %s %v: %s", stdOut, err, stdErr)
}
}
logging.Debugf("Making %s readable/writable by the current user", resolverFile)
stdOut, stdErr, err := crcos.RunWithPrivilege("touch", resolverFile)
stdOut, stdErr, err := crcos.RunWithPrivilege(fmt.Sprintf("create file %s", resolverFile), "touch", resolverFile)
if err != nil {
return false, fmt.Errorf("Unable to create the resolver file: %s %v: %s", stdOut, err, stdErr)
}
Expand Down Expand Up @@ -259,7 +259,7 @@ func addFileWritePermissionToUser(filename string) (bool, error) {
return false, err
}

stdOut, stdErr, err := crcos.RunWithPrivilege("chown", currentUser.Username, filename)
stdOut, stdErr, err := crcos.RunWithPrivilege(fmt.Sprintf("change ownership of %s", filename), "chown", currentUser.Username, filename)
if err != nil {
return false, fmt.Errorf("Unable to change ownership of the filename: %s %v: %s", stdOut, err, stdErr)
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/crc/preflight/preflight_checks_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func checkLibvirtInstalled() (bool, error) {

func fixLibvirtInstalled() (bool, error) {
logging.Debug("Trying to install libvirt")
stdOut, stdErr, err := crcos.RunWithPrivilege("yum", "install", "-y", "libvirt", "libvirt-daemon-kvm", "qemu-kvm")
stdOut, stdErr, err := crcos.RunWithPrivilege("install virtualization related packages", "yum", "install", "-y", "libvirt", "libvirt-daemon-kvm", "qemu-kvm")
if err != nil {
return false, fmt.Errorf("Could not install required packages: %s %v: %s", stdOut, err, stdErr)
}
Expand Down Expand Up @@ -138,7 +138,7 @@ func fixLibvirtEnabled() (bool, error) {
if err != nil {
return false, err
}
stdOut, stdErr, err := crcos.RunWithPrivilege(path, "enable", "libvirtd")
stdOut, stdErr, err := crcos.RunWithPrivilege("enable libvirtd service", path, "enable", "libvirtd")
if err != nil {
return false, fmt.Errorf("%s, %v : %s", stdOut, err, stdErr)
}
Expand Down Expand Up @@ -175,7 +175,7 @@ func fixUserPartOfLibvirtGroup() (bool, error) {
if err != nil {
return false, err
}
stdOut, stdErr, err := crcos.RunWithPrivilege("usermod", "-a", "-G", "libvirt", currentUser.Username)
stdOut, stdErr, err := crcos.RunWithPrivilege("add user to libvirt group", "usermod", "-a", "-G", "libvirt", currentUser.Username)
if err != nil {
return false, fmt.Errorf("%s %v : %s", stdOut, err, stdErr)
}
Expand Down Expand Up @@ -206,7 +206,7 @@ func fixLibvirtServiceRunning() (bool, error) {
if err != nil {
return false, err
}
stdOut, stdErr, err := crcos.RunWithPrivilege(path, "start", "libvirtd")
stdOut, stdErr, err := crcos.RunWithPrivilege("start libvirtd service", path, "start", "libvirtd")
if err != nil {
return false, fmt.Errorf("%s %v : %s", stdOut, err, stdErr)
}
Expand Down Expand Up @@ -263,7 +263,7 @@ func checkOldMachineDriverLibvirtInstalled() (bool, error) {
func fixOldMachineDriverLibvirtInstalled() (bool, error) {
oldLibvirtDriverPath := filepath.Join("/usr/local/bin/", libvirtDriverCommand)
logging.Debugf("Removing %s", oldLibvirtDriverPath)
_, _, err := crcos.RunWithPrivilege("rm", "-f", oldLibvirtDriverPath)
_, _, err := crcos.RunWithPrivilege("remove old libvirt driver", "rm", "-f", oldLibvirtDriverPath)
if err != nil {
logging.Debugf("Removal of %s failed", oldLibvirtDriverPath)
/* Ignoring error, an obsolete file being still present is not a fatal error */
Expand Down Expand Up @@ -398,6 +398,7 @@ func fixCrcDnsmasqConfigFile() (bool, error) {
cmd.Stdin = strings.NewReader(crcDnsmasqConfig)
buf := new(bytes.Buffer)
cmd.Stderr = buf
logging.Infof("Will use root access: write dnsmasq configuration in %s", crcDnsmasqConfigPath)
if err := cmd.Run(); err != nil {
return false, fmt.Errorf("Failed to write dnsmasq config file: %s: %s: %v", crcDnsmasqConfigPath, buf.String(), err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/crc/systemd/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (c HostSystemdCommander) Disable(name string) (bool, error) {
}

func (c HostSystemdCommander) DaemonReload() (bool, error) {
stdOut, stdErr, err := crcos.RunWithPrivilege("systemctl", "daemon-reload")
stdOut, stdErr, err := crcos.RunWithPrivilege("execute systemctl daemon-reload command", "systemctl", "daemon-reload")
if err != nil {
return false, fmt.Errorf("Executing systemctl daemon-reload failed: %s %v: %s", stdOut, err, stdErr)
}
Expand Down Expand Up @@ -83,7 +83,7 @@ func (c HostSystemdCommander) Status(name string) (states.State, error) {
}

func (c HostSystemdCommander) service(name string, action actions.Action) (states.State, error) {
stdOut, stdErr, err := crcos.RunWithPrivilege("systemctl", action.String(), name)
stdOut, stdErr, err := crcos.RunWithPrivilege("execute systemctl stop/start command", "systemctl", action.String(), name)
if err != nil {
return states.Error, fmt.Errorf("Executing systemctl action failed: %s %v: %s", stdOut, err, stdErr)
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/os/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package os

import (
"bytes"
"github.com/code-ready/crc/pkg/crc/logging"
"os"
"os/exec"
)

func RunWithPrivilege(cmdAndArgs ...string) (string, string, error) {
// RunWithPrivilege executes a command using sudo
// provide a reason why root is needed as the first argument
func RunWithPrivilege(reason string, cmdAndArgs ...string) (string, string, error) {
sudo, err := exec.LookPath("sudo")
if err != nil {
return "", "", err
Expand All @@ -16,6 +19,7 @@ func RunWithPrivilege(cmdAndArgs ...string) (string, string, error) {
stdErr := new(bytes.Buffer)
cmd.Stdout = stdOut
cmd.Stderr = stdErr
logging.Infof("Will use root access: %s\n", reason)
err = cmd.Run()
return stdOut.String(), stdErr.String(), err
}
Expand Down

0 comments on commit 7e751d5

Please sign in to comment.