-
Notifications
You must be signed in to change notification settings - Fork 712
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alfonso Acosta
committed
Mar 29, 2016
1 parent
7b03f01
commit c1c40ad
Showing
12 changed files
with
208 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#! /bin/bash | ||
|
||
. ./config.sh | ||
|
||
start_suite "Test host controls" | ||
|
||
weave_on $HOST1 launch | ||
scope_on $HOST1 launch | ||
|
||
sleep 10 | ||
|
||
PROBEID=$(docker_on $HOST1 logs weavescope 2>&1 | grep "probe starting" | sed -n 's/^.*ID \([0-9a-f]*\)$/\1/p') | ||
HOSTID=$($SSH $HOST1 hostname) | ||
|
||
# Execute 'echo foo' in the host tty and check its output | ||
PIPEID=$(curl -s -f -X POST "http://$HOST1:4040/api/control/$PROBEID/$HOSTID;<host>/host_exec" | jq -r '.pipe' ) | ||
assert "(sleep 1 && echo \"PS1=''; echo foo\" && sleep 1) | wscat -b 'ws://$HOST1:4040/api/pipe/$PIPEID' | col -pb | tail -n 1" "foo\n" | ||
|
||
scope_end_suite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package host | ||
|
||
import ( | ||
"os/exec" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
"github.com/kr/pty" | ||
|
||
"github.com/weaveworks/scope/common/xfer" | ||
"github.com/weaveworks/scope/probe/controls" | ||
) | ||
|
||
// Control IDs used by the host integration. | ||
const ( | ||
ExecHost = "host_exec" | ||
) | ||
|
||
// Controls handles controls for a hosts. | ||
type Controls struct { | ||
pipes controls.PipeClient | ||
} | ||
|
||
// NewControls creates new host controls. | ||
func NewControls(pipes controls.PipeClient) *Controls { | ||
c := &Controls{pipes: pipes} | ||
controls.Register(ExecHost, c.execHost) | ||
return c | ||
} | ||
|
||
// Stop stops the host controls. | ||
func (*Controls) Stop() { | ||
controls.Rm(ExecHost) | ||
} | ||
|
||
func (c *Controls) execHost(req xfer.Request) xfer.Response { | ||
cmd := exec.Command(hostShellCmd[0], hostShellCmd[1:]...) | ||
cmd.Env = []string{"TERM=xterm"} | ||
ptyPipe, err := pty.Start(cmd) | ||
if err != nil { | ||
return xfer.ResponseError(err) | ||
} | ||
|
||
id, pipe, err := controls.NewPipeFromEnds(nil, ptyPipe, c.pipes, req.AppID) | ||
if err != nil { | ||
return xfer.ResponseError(err) | ||
} | ||
pipe.OnClose(func() { | ||
if err := cmd.Process.Kill(); err != nil { | ||
log.Errorf("Error closing host shell: %v", err) | ||
return | ||
} | ||
log.Info("Host shell closed.") | ||
}) | ||
go func() { | ||
if err := cmd.Wait(); err != nil { | ||
log.Errorf("Error waiting on host shell: %v", err) | ||
} | ||
ptyPipe.Close() | ||
pipe.Close() | ||
}() | ||
|
||
return xfer.Response{ | ||
Pipe: id, | ||
RawTTY: true, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package host | ||
|
||
var hostShellCmd = []string{"/bin/bash"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package host | ||
|
||
import ( | ||
"bytes" | ||
"os/exec" | ||
"syscall" | ||
|
||
"github.com/willdonnelly/passwd" | ||
) | ||
|
||
var hostShellCmd []string | ||
|
||
func init() { | ||
if isProbeContainerized() { | ||
// Escape the container namespaces and jump into the ones from | ||
// the host's init process. | ||
// Note: There should be no need to enter into the host network | ||
// and PID namespace because we should already already be there | ||
// but it doesn't hurt. | ||
readPasswdCmd := []string{"/usr/bin/nsenter", "-t1", "-m", "--no-fork", "cat", "/etc/passwd"} | ||
uid, gid, shell := getRootUserDetails(readPasswdCmd) | ||
hostShellCmd = []string{ | ||
"/usr/bin/nsenter", "-t1", "-m", "-i", "-n", "-p", "--no-fork", | ||
"--setuid", uid, | ||
"--setgid", gid, | ||
shell, | ||
} | ||
return | ||
} | ||
|
||
_, _, shell := getRootUserDetails([]string{"cat", "/etc/passwd"}) | ||
hostShellCmd = []string{shell} | ||
} | ||
|
||
func getRootUserDetails(readPasswdCmd []string) (uid, gid, shell string) { | ||
uid = "0" | ||
gid = "0" | ||
shell = "/bin/sh" | ||
|
||
cmd := exec.Command(readPasswdCmd[0], readPasswdCmd[1:]...) | ||
cmdBuffer := &bytes.Buffer{} | ||
cmd.Stdout = cmdBuffer | ||
if err := cmd.Run(); err != nil { | ||
return | ||
} | ||
|
||
entries, err := passwd.ParseReader(cmdBuffer) | ||
if err != nil { | ||
return | ||
} | ||
|
||
entry, ok := entries["root"] | ||
if !ok { | ||
return | ||
} | ||
|
||
return entry.Uid, entry.Gid, entry.Shell | ||
} | ||
|
||
func isProbeContainerized() bool { | ||
// Figure out whether we are running in a container by checking if our | ||
// mount namespace matches the one from init process. This works | ||
// because, when containerized, the Scope probes run in the host's PID | ||
// namespace (and if they weren't due to a configuration problem, we | ||
// wouldn't have a way to escape the container anyhow). | ||
var statT syscall.Stat_t | ||
|
||
if err := syscall.Stat("/proc/self/ns/mnt", &statT); err != nil { | ||
return false | ||
} | ||
selfMountNamespaceID := statT.Ino | ||
|
||
if err := syscall.Stat("/proc/1/ns/mnt", &statT); err != nil { | ||
return false | ||
} | ||
|
||
return selfMountNamespaceID != statT.Ino | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters