Skip to content
This repository was archived by the owner on Jul 28, 2020. It is now read-only.

Commit 35368e4

Browse files
author
Anthony Emengo
committedApr 4, 2019
Make linux work as normal user that escalates to root
- Run servicew commands with sudo access - Retrieve dynamic IP address with sudo access - SudoShell -> Sudo
1 parent dc2e061 commit 35368e4

File tree

9 files changed

+36
-35
lines changed

9 files changed

+36
-35
lines changed
 

‎driver/hyperkit/driver.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type Hyperkit struct {
1818
Config config.Config
1919
DaemonRunner driver.DaemonRunner
2020
CFDevD *client.Client
21-
SudoShell *runner.SudoShell
21+
SudoShell *runner.Sudo
2222
}
2323

2424
func New(
@@ -32,7 +32,7 @@ func New(
3232
Config: cfg,
3333
DaemonRunner: daemonRunner,
3434
CFDevD: cfdevdClient,
35-
SudoShell: &runner.SudoShell{},
35+
SudoShell: &runner.Sudo{},
3636
}
3737
}
3838

‎driver/ip_linux.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,33 @@ import (
55
"encoding/json"
66
"fmt"
77
"io/ioutil"
8+
"os/exec"
89
"path/filepath"
910
)
1011

1112
func IP(cfg config.Config) (string, error) {
1213
var (
14+
ipPath = filepath.Join(cfg.StateLinuxkit, "ip")
1315
macAddrPath = filepath.Join(cfg.StateLinuxkit, "mac-addr")
14-
vBridgeInfoPath = filepath.Join("/var/lib/libvirt/dnsmasq/virbr0.status")
16+
vBridgeInfoPath = "/var/lib/libvirt/dnsmasq/virbr0.status"
1517
)
1618

17-
macAddr, err := ioutil.ReadFile(macAddrPath)
19+
// The logic below is a bit of a hack.
20+
// Since the services will get started as root, the qemu files containing the ip address will be written as root.
21+
// We don't want to escalate to root every time we need the ip throughout the lifecycle of the program, so we write
22+
// the ip address as a normal file when we first get it. This logic is making an assumption that root privileges
23+
// has been retrieved as part of a prior step and has not yet timed out.
24+
data, err := ioutil.ReadFile(ipPath)
25+
if err == nil {
26+
return string(data), nil
27+
}
28+
29+
macAddr, err := readAsSudo(macAddrPath)
1830
if err != nil {
1931
return "", err
2032
}
2133

22-
vBridgeInfo, err := ioutil.ReadFile(vBridgeInfoPath)
34+
vBridgeInfo, err := readAsSudo(vBridgeInfoPath)
2335
if err != nil {
2436
return "", err
2537
}
@@ -36,10 +48,15 @@ func IP(cfg config.Config) (string, error) {
3648

3749
for _, result := range results {
3850
if result.MacAddr == string(macAddr) {
51+
ioutil.WriteFile(ipPath, []byte(result.IPAddr), 0600)
52+
3953
return result.IPAddr, nil
4054
}
4155
}
4256

4357
return "", fmt.Errorf("unable to find VM IP address from '%s'", vBridgeInfoPath)
4458
}
4559

60+
func readAsSudo(path string) ([]byte, error) {
61+
return exec.Command("sudo", "-S", "cat", path).Output()
62+
}

‎driver/kvm/driver.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type KVM struct {
2020
UI driver.UI
2121
Config config.Config
2222
DaemonRunner driver.DaemonRunner
23-
SudoShell *runner.SudoShell
23+
SudoShell *runner.Sudo
2424
}
2525

2626
func New(
@@ -32,7 +32,7 @@ func New(
3232
UI: ui,
3333
Config: cfg,
3434
DaemonRunner: daemonRunner,
35-
SudoShell: &runner.SudoShell{},
35+
SudoShell: &runner.Sudo{},
3636
}
3737
}
3838

‎pkg/servicew/integration/integration_suite_test.go ‎pkg/servicew/acceptance/integration_suite_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package integration_test
1+
package acceptance_test
22

33
import (
44
"github.com/onsi/gomega/gexec"

‎pkg/servicew/integration/integration_test.go ‎pkg/servicew/acceptance/integration_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package integration_test
1+
package acceptance_test
22

33
import (
44
"code.cloudfoundry.org/cfdev/pkg/servicew/client"

‎pkg/servicew/client/client.go

+8-24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package client
33
import (
44
"code.cloudfoundry.org/cfdev/pkg/servicew/config"
55
"code.cloudfoundry.org/cfdev/pkg/servicew/program"
6+
"code.cloudfoundry.org/cfdev/runner"
67
"fmt"
78
"gopkg.in/yaml.v2"
89
"io"
@@ -15,12 +16,14 @@ import (
1516
type ServiceWrapper struct {
1617
binaryPath string
1718
workdir string
19+
runner *runner.Sudo
1820
}
1921

2022
func New(binaryPath string, workdir string) *ServiceWrapper {
2123
return &ServiceWrapper{
2224
binaryPath: binaryPath,
2325
workdir: workdir,
26+
runner: &runner.Sudo{},
2427
}
2528
}
2629

@@ -46,13 +49,7 @@ func (s *ServiceWrapper) Install(cfg config.Config) error {
4649
return err
4750
}
4851

49-
command := exec.Command(swrapperPath, "install")
50-
output, err := command.CombinedOutput()
51-
if err != nil {
52-
return fmt.Errorf("failed to install '%s': %s: %s", cfg.Label, err, output)
53-
}
54-
55-
return nil
52+
return s.runner.Run(swrapperPath, "install")
5653
}
5754

5855
func (s *ServiceWrapper) Uninstall(label string) error {
@@ -65,10 +62,9 @@ func (s *ServiceWrapper) Uninstall(label string) error {
6562
return nil
6663
}
6764

68-
command := exec.Command(swrapperPath, "uninstall")
69-
output, err := command.CombinedOutput()
65+
err := s.runner.Run(swrapperPath, "uninstall")
7066
if err != nil {
71-
return fmt.Errorf("failed to uninstall '%s': %s: %s", label, err, output)
67+
return fmt.Errorf("failed to uninstall '%s': %s", label, err)
7268
}
7369

7470
err = os.RemoveAll(swrapperPath)
@@ -80,27 +76,15 @@ func (s *ServiceWrapper) Uninstall(label string) error {
8076
}
8177

8278
func (s *ServiceWrapper) Start(label string) error {
83-
command := exec.Command(s.swrapperPath(label), "start")
84-
output, err := command.CombinedOutput()
85-
if err != nil {
86-
return fmt.Errorf("failed to start '%s': %s: %s", label, err, output)
87-
}
88-
89-
return nil
79+
return s.runner.Run(s.swrapperPath(label), "start")
9080
}
9181

9282
func (s *ServiceWrapper) Stop(label string) error {
9383
if s.swrapperNotExist(label) {
9484
return nil
9585
}
9686

97-
command := exec.Command(s.swrapperPath(label), "stop")
98-
output, err := command.CombinedOutput()
99-
if err != nil {
100-
return fmt.Errorf("failed to stop '%s': %s: %s", label, err, output)
101-
}
102-
103-
return nil
87+
return s.runner.Run(s.swrapperPath(label), "stop")
10488
}
10589

10690
func (s *ServiceWrapper) IsRunning(label string) (bool, error) {

‎runner/sudo_shell.go ‎runner/sudo.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"os/exec"
66
)
77

8-
type SudoShell struct{}
8+
type Sudo struct{}
99

10-
func (s *SudoShell) Run(args ...string) error {
10+
func (s *Sudo) Run(args ...string) error {
1111
var (
1212
invocation = append([]string{"-S"}, args...)
1313
cmd = exec.Command("sudo", invocation...)

0 commit comments

Comments
 (0)
This repository has been archived.