-
Notifications
You must be signed in to change notification settings - Fork 712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
host-level ttys #1208
host-level ttys #1208
Changes from all commits
7b03f01
c1c40ad
59af5d2
4a49607
2c4de62
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
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" | ||
) | ||
|
||
func (r *Reporter) registerControls() { | ||
controls.Register(ExecHost, r.execHost) | ||
} | ||
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
Sorry, something went wrong. |
||
|
||
func (*Reporter) deregisterControls() { | ||
controls.Rm(ExecHost) | ||
} | ||
|
||
func (r *Reporter) execHost(req xfer.Request) xfer.Response { | ||
cmd := exec.Command(r.hostShellCmd[0], r.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, r.pipes, req.AppID) | ||
if err != nil { | ||
return xfer.ResponseError(err) | ||
} | ||
pipe.OnClose(func() { | ||
if err := cmd.Process.Kill(); err != nil { | ||
log.Errorf("Error stopping host shell: %v", err) | ||
} | ||
if err := ptyPipe.Close(); err != nil { | ||
log.Errorf("Error closing host shell's pty: %v", err) | ||
} | ||
log.Info("Host shell closed.") | ||
}) | ||
go func() { | ||
if err := cmd.Wait(); err != nil { | ||
log.Errorf("Error waiting on host shell: %v", err) | ||
} | ||
pipe.Close() | ||
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
Sorry, something went wrong. |
||
}() | ||
|
||
return xfer.Response{ | ||
Pipe: id, | ||
RawTTY: true, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package host | ||
|
||
func getHostShellCmd() []string { | ||
return []string{"/bin/bash"} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package host | ||
|
||
import ( | ||
"bytes" | ||
"os/exec" | ||
"strings" | ||
"syscall" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
"github.com/willdonnelly/passwd" | ||
) | ||
|
||
func getHostShellCmd() []string { | ||
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) | ||
return []string{ | ||
"/usr/bin/nsenter", "-t1", "-m", "-i", "-n", "-p", "--no-fork", | ||
"--setuid", uid, | ||
"--setgid", gid, | ||
shell, | ||
} | ||
} | ||
|
||
_, _, shell := getRootUserDetails([]string{"cat", "/etc/passwd"}) | ||
return []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 { | ||
log.Warnf( | ||
"getRootUserDetails(): error running read passwd command %q: %s", | ||
strings.Join(readPasswdCmd, " "), | ||
err, | ||
) | ||
return | ||
} | ||
|
||
entries, err := passwd.ParseReader(cmdBuffer) | ||
if err != nil { | ||
log.Warnf("getRootUserDetails(): error parsing passwd: %s", err) | ||
return | ||
} | ||
|
||
entry, ok := entries["root"] | ||
if !ok { | ||
log.Warnf("getRootUserDetails(): no root entry in passwd") | ||
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 | ||
|
||
path := "/proc/self/ns/mnt" | ||
if err := syscall.Stat(path, &statT); err != nil { | ||
log.Warnf("isProbeContainerized(): stat() error on %q: %s", path, err) | ||
return false | ||
} | ||
selfMountNamespaceID := statT.Ino | ||
|
||
path = "/proc/1/ns/mnt" | ||
if err := syscall.Stat(path, &statT); err != nil { | ||
log.Warnf("isProbeContainerized(): stat() error on %q: %s", path, err) | ||
return false | ||
} | ||
|
||
return selfMountNamespaceID != statT.Ino | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
Sorry, something went wrong.