mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
The phase's review found helpers that the section ports copied: - firstLine (tools) duplicated FirstLine: every caller uses FirstLine. - tailLines moves beside FirstLine and LastLine (text_test.go), and LastLine and link's errorsOf are built on it. - Launch and launchWith share one launch (S.launch), which records the VM through OwnVM, in place of two copies of OwnVM's body. - Paths.RunLogs is the one reader of devvm's run-log folder: RunLogHas, NewestLog, the heartbeat, from's corpLog and network's two log reads use it, in place of six globs. - MountsOf is built on MountLine, in place of a second limactl list --json reader. - Procs reads the processes through psAll, as the process watch does, so the listing stays out of the check's log. - network's dialable goes: its check uses ReachesWithin and Local. - Res.Has is HasLine on its output; CA.Line is pemLine on its PEM; link's out is Trim; tools' lockSum is readText. - FilesHave goes: the desktop password's and agent save's checks that a secret is in no file here use NoLeak, which also fails on a file it can't read and names the file, never the secret. - project's tarList reads the export with exportFiles (Go's tar) in place of host tar -tzf. Tests: TestHarnessNetReaders covers LastLine and Res.Has; TestHarnessExport covers exportFiles and tarList. Contributing's testing page lists RunLogs, MountsOf and tailLines, and drops FilesHave. Co-Authored-By: Claude Opus 5.5 <[email protected]>
614 lines
20 KiB
Go
614 lines
20 KiB
Go
//go:build e2e
|
|
|
|
package e2e
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"runtime"
|
|
"slices"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// The link section's helpers: its VMs, their
|
|
// servers, the links' files and processes on this machine.
|
|
|
|
// link is the link section's VMs: the targets' and the client's folders
|
|
// (p1, p2, pc) and VMs (v1, v2, vc), the targets' servers' words (w1,
|
|
// w2), the second's link name (n2) and the folders' prefix (x).
|
|
type link struct {
|
|
s *S
|
|
x string
|
|
p1, p2, pc string
|
|
v1, v2, vc string
|
|
w1, w2, n2 string
|
|
g string // the group
|
|
recorded string // what the GatewayPorts check saw, printed after it
|
|
}
|
|
|
|
// p is slot 0's port 47240+n as this slot has it ($P47240…$P47249).
|
|
func (l *link) p(n int) string { return strconv.Itoa(l.s.Port(47240 + n)) }
|
|
|
|
// lkPl is the VMs' helper, /var/tmp/e2e-link.pl (perl: every base has
|
|
// it). serve ADDR PORT WORD answers each connection with an HTTP response
|
|
// whose body is WORD; hold ADDR PORT keeps every connection it accepts
|
|
// open (each writes its pid to /var/tmp/e2e-link-ADDR-PORT.pid); conns
|
|
// HOST PORT N opens N connections, all at once but the last, 5 s after
|
|
// the others, and prints those the far end closed (a burst past the
|
|
// backlog of the link's socket on macOS, 128, which devvm _links accepts
|
|
// whole before it starts their relays); listeners prints each TCP
|
|
// listener as "PROCESS ADDR:PORT" (as root, to see every process's).
|
|
const lkPl = `use strict; use warnings; use IO::Socket::INET; use IO::Select;
|
|
my $mode = shift // "";
|
|
sub v4 { join ".", reverse map { hex } $_[0] =~ /(..)/g }
|
|
sub addr { my $a = uc shift;
|
|
return v4($a) if length $a == 8;
|
|
return "[::]" if $a =~ /^0{32}$/;
|
|
return "[::1]" if $a eq "00000000000000000000000001000000";
|
|
return "[::ffff:" . v4($1) . "]" if $a =~ /^0{16}FFFF0000(.{8})$/;
|
|
return "[$a]"; }
|
|
if ($mode eq "serve" or $mode eq "hold") {
|
|
my ($host, $port, $word) = @ARGV;
|
|
my $s = IO::Socket::INET->new(LocalAddr => $host, LocalPort => $port, Listen => 1024, ReuseAddr => 1) or die "$host:$port: $!\n";
|
|
open(my $pf, ">", "/var/tmp/e2e-link-$host-$port.pid") or die "$!\n"; print $pf "$$\n"; close $pf;
|
|
my @held;
|
|
while (my $c = $s->accept) {
|
|
if ($mode eq "hold") { push @held, $c; next }
|
|
my $sel = IO::Select->new($c); my $buf = "";
|
|
while ($buf !~ /\n\r?\n/ and $sel->can_read(2)) { sysread($c, $buf, 4096, length $buf) or last }
|
|
my $body = "$word\n";
|
|
print $c "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nContent-Length: " . length($body) . "\r\nConnection: close\r\n\r\n$body";
|
|
close $c;
|
|
}
|
|
} elsif ($mode eq "conns") {
|
|
my ($host, $port, $n) = @ARGV; my @c;
|
|
for my $i (1 .. $n) {
|
|
sleep 5 if $i == $n;
|
|
push @c, IO::Socket::INET->new(PeerAddr => $host, PeerPort => $port, Timeout => 10) || die "connection $i: $!\n";
|
|
}
|
|
sleep 5;
|
|
my @closed;
|
|
for my $i (0 .. $#c) { push @closed, $i + 1 if IO::Select->new($c[$i])->can_read(0) and !sysread($c[$i], my $x, 1) }
|
|
print "closed: @closed\n";
|
|
} elsif ($mode eq "listeners") {
|
|
my %own;
|
|
for my $d (glob "/proc/[0-9]*") {
|
|
open(my $f, "<", "$d/comm") or next; my $n = <$f> // "?"; chomp $n;
|
|
opendir(my $fd, "$d/fd") or next;
|
|
for my $e (readdir $fd) { my $l = readlink("$d/fd/$e") // next; $own{$1} = $n if $l =~ /^socket:\[(\d+)\]$/ }
|
|
}
|
|
for my $t ("tcp", "tcp6") {
|
|
open(my $h, "<", "/proc/net/$t") or next; my $head = <$h>;
|
|
while (my $line = <$h>) {
|
|
my @f = split " ", $line; next unless $f[3] eq "0A";
|
|
my ($a, $p) = split /:/, $f[1];
|
|
printf "%s %s:%d\n", $own{$f[9]} // "?", addr($a), hex $p;
|
|
}
|
|
}
|
|
} else { die "e2e-link.pl: serve, hold, conns or listeners\n" }`
|
|
|
|
// lkHostileSh loosens the client's sshd as root in it would (any listen
|
|
// address, all forwarding; with h=gateway, GatewayPorts yes too) and
|
|
// makes it read that; lkHonestSh puts it back.
|
|
const lkHostileSh = `if [ -f /etc/devvm/ssh/sshd_config ]; then c=/etc/devvm/ssh/sshd_config
|
|
[ -f $c.e2e ] || cp $c $c.e2e
|
|
sed -i -e "s/^PermitListen .*/PermitListen any/" -e "s/^AllowTcpForwarding .*/AllowTcpForwarding yes/" $c
|
|
[ "$h" != gateway ] || sed -i "s/^GatewayPorts .*/GatewayPorts yes/" $c
|
|
[ ! -s /run/devvm-sshd.pid ] || kill -HUP "$(cat /run/devvm-sshd.pid)"
|
|
else { echo "PermitListen any"; echo "AllowTcpForwarding yes"; [ "$h" != gateway ] || echo "GatewayPorts yes"; } > /etc/ssh/sshd_config.d/00-aaa-e2e.conf
|
|
systemctl reload ssh 2>/dev/null || systemctl reload sshd 2>/dev/null || rc-service sshd reload 2>/dev/null || true
|
|
fi`
|
|
|
|
const lkHonestSh = `if [ -f /etc/devvm/ssh/sshd_config.e2e ]; then mv /etc/devvm/ssh/sshd_config.e2e /etc/devvm/ssh/sshd_config
|
|
[ ! -s /run/devvm-sshd.pid ] || kill -HUP "$(cat /run/devvm-sshd.pid)"
|
|
else rm -f /etc/ssh/sshd_config.d/00-aaa-e2e.conf
|
|
systemctl reload ssh 2>/dev/null || systemctl reload sshd 2>/dev/null || rc-service sshd reload 2>/dev/null || true
|
|
fi`
|
|
|
|
// lkRoot runs cmd in the VM as root (lk_root); lkUser, in its user's
|
|
// login shell (lk_user), is s.InVM.
|
|
func lkRoot(s *S, vm, cmd string) (Res, error) {
|
|
return s.Limactl("shell", "--workdir", "/", vm, "sudo", "sh", "-c", cmd)
|
|
}
|
|
|
|
// lkBg runs cmd in the VM as root, left running.
|
|
func lkBg(s *S, vm, cmd string) error {
|
|
_, err := lkRoot(s, vm, "setsid -f "+cmd+" </dev/null >/dev/null 2>&1")
|
|
return err
|
|
}
|
|
|
|
// lkRunning is whether the VM runs, as Lima says.
|
|
func lkRunning(s *S, vm string) bool { return s.Status(vm) == "Running" }
|
|
|
|
// out is a command's output with its last newlines taken off, as $(…)
|
|
// has it.
|
|
func out(r Res) string { return Trim(r.Stdout) }
|
|
|
|
// yesNo is yes or no.
|
|
func yesNo(b bool) string {
|
|
if b {
|
|
return "yes"
|
|
}
|
|
return "no"
|
|
}
|
|
|
|
// not is an error when ok, why saying so: for Until's waits for
|
|
// something to go.
|
|
func not(ok bool, why string) error {
|
|
if ok {
|
|
return errors.New(why)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// lkPrep writes the helper to the VM, and reserves the link checks'
|
|
// ports ($P47240-$P47249) in it, at once and at every boot. Linux hands
|
|
// out ports from 32768 to 60999 to a connection's own end, these among
|
|
// them, and a connection the VM's side closed keeps its port for 60 s
|
|
// (TIME_WAIT), which no server can bind on 127.0.0.1 or every address
|
|
// then, SO_REUSEADDR or not: lkCap's 257 connections from the client
|
|
// would leave a later server or ssh -R there without its port.
|
|
func lkPrep(l *link, vm string) error {
|
|
s := l.s
|
|
_, err := s.Run(Cmd{Args: []string{"limactl", "shell", "--workdir", "/", vm, "sudo", "sh", "-c", `cat > "$1"`, "_", "/var/tmp/e2e-link.pl"}, Stdin: lkPl + "\n"})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
r := l.p(0) + "-" + l.p(9)
|
|
_, err = lkRoot(s, vm, "echo 'net.ipv4.ip_local_reserved_ports = "+r+"' > /etc/sysctl.d/90-e2e-link.conf && "+
|
|
"sysctl -q -p /etc/sysctl.d/90-e2e-link.conf && [ \"$(cat /proc/sys/net/ipv4/ip_local_reserved_ports)\" = "+r+" ]")
|
|
return err
|
|
}
|
|
|
|
// lkStart starts the helper's server (serve or hold) in the VM, as root,
|
|
// and waits until it listens (its pid file) or has ended (the wrapper's
|
|
// "exited N" in its log). On a failure, the log has what it printed and
|
|
// every TCP listener in the VM.
|
|
func lkStart(l *link, vm, mode, addr, port, word string) error {
|
|
s := l.s
|
|
f, lg := "/var/tmp/e2e-link-"+addr+"-"+port+".pid", "/var/tmp/e2e-link-"+addr+"-"+port+".log"
|
|
_, err := lkRoot(s, vm, "rm -f "+f+" "+lg)
|
|
if err == nil {
|
|
err = lkBg(s, vm, "sh -c 'perl /var/tmp/e2e-link.pl "+mode+" "+addr+" "+port+" "+word+" >"+lg+" 2>&1; echo exited $? >>"+lg+"'")
|
|
}
|
|
if err == nil {
|
|
_ = s.Until(20*time.Second, func() error {
|
|
_, err := lkRoot(s, vm, "test -s "+f+" || grep -q '^exited ' "+lg)
|
|
return err
|
|
})
|
|
_, err = lkRoot(s, vm, "test -s "+f)
|
|
}
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
running := yesNo(lkRunning(s, vm))
|
|
_, _ = lkRoot(s, vm, "ls -l "+f+" "+lg+"; tail -n 20 "+lg)
|
|
Note(s, "the VM's listeners:\n%s", strings.Join(lkListens(s, vm), "\n"))
|
|
return fmt.Errorf("the helper's %s on %s:%s in %s didn't start; the VM runs: %s", mode, addr, port, vm, running)
|
|
}
|
|
|
|
// lkEnd ends the helper's server on addr:port in the VM.
|
|
func lkEnd(s *S, vm, addr, port string) error {
|
|
_, err := lkRoot(s, vm, "f=/var/tmp/e2e-link-"+addr+"-"+port+".pid; [ ! -s $f ] || kill $(cat $f); rm -f $f")
|
|
return err
|
|
}
|
|
|
|
// lkServe is lkPrep, and a server on the VM's 127.0.0.1 on each port,
|
|
// answering WORD-PORT.
|
|
func lkServe(l *link, vm, word string, ports ...string) error {
|
|
if err := lkPrep(l, vm); err != nil {
|
|
return err
|
|
}
|
|
for _, p := range ports {
|
|
if err := lkStart(l, vm, "serve", "127.0.0.1", p, word+"-"+p); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// lkRaw is what a raw TCP client in the VM (bash's /dev/tcp) gets back
|
|
// for a request: the body's line.
|
|
func lkRaw(s *S, vm, host, port string) (string, error) {
|
|
r, err := s.InVM(vm, "exec 3<>/dev/tcp/"+host+"/"+port+` && printf 'GET / HTTP/1.0\r\n\r\n' >&3 && timeout 10 cat <&3 | tail -n 1`)
|
|
return out(r), err
|
|
}
|
|
|
|
// lkGets: the raw client gets word from host:port.
|
|
func lkGets(s *S, vm, host, port, word string) error {
|
|
a, _ := lkRaw(s, vm, host, port)
|
|
if a != word {
|
|
return fmt.Errorf("%s:%s answered: %s, not %q", host, port, orNothing(a), word)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// lkCurls: curl in the VM gets word from u, through the VM's proxy
|
|
// settings when it has them.
|
|
func lkCurls(s *S, vm, u, word string) error {
|
|
r, _ := s.InVM(vm, "curl -fsS --max-time 10 "+u)
|
|
if a := out(r); a != word {
|
|
return fmt.Errorf("%s answered: %s, not %q", u, orNothing(a), word)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func orNothing(a string) string {
|
|
if a == "" {
|
|
return "nothing"
|
|
}
|
|
return a
|
|
}
|
|
|
|
// lkResolves is whether the name resolves in the VM.
|
|
func lkResolves(s *S, vm, name string) bool {
|
|
_, err := s.InVM(vm, "getent hosts "+name)
|
|
return err == nil
|
|
}
|
|
|
|
// lkUp launches devvm args in dir in names mode, with a shell that ends
|
|
// at once (lk_up).
|
|
func lkUp(s *S, dir string, args ...string) (Res, error) {
|
|
return s.Run(Cmd{Args: append([]string{s.sb.devvm}, args...), Dir: dir, Stdin: "exit\n", Env: []string{"DEVVM_MESSAGES=names"}})
|
|
}
|
|
|
|
// said: devvm's output r has that line.
|
|
func said(r Res, lines ...string) error {
|
|
for _, line := range lines {
|
|
if !r.Has(line) {
|
|
return errors.New("not said: " + line)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// lkShows is whether devvm args' output in dir has a line matching re.
|
|
func lkShows(s *S, dir, re string, args ...string) bool {
|
|
r, _ := s.Devvm(dir, args...)
|
|
return Matches(r.Stdout, re)
|
|
}
|
|
|
|
// lkSSH is ssh through devvm's ssh config, without an agent (the
|
|
// sandbox has none).
|
|
func lkSSH(s *S) []string {
|
|
return []string{"ssh", "-F", filepath.Join(s.Paths.Devvm, "ssh_config"), "-o", "BatchMode=yes"}
|
|
}
|
|
|
|
// lkAddr is the address the VM's links named name listen on, from its
|
|
// state.
|
|
func lkAddr(s *S, vm, name string) string {
|
|
st := s.Paths.State(vm)
|
|
for i := 0; ; i++ {
|
|
n, ok := st["links.links."+strconv.Itoa(i)+".name"]
|
|
if !ok {
|
|
return ""
|
|
}
|
|
if n == name {
|
|
return st["links.links."+strconv.Itoa(i)+".addr"]
|
|
}
|
|
}
|
|
}
|
|
|
|
// lkHash is the hash devvm names a link session's files by.
|
|
func lkHash(parts ...string) string {
|
|
h := sha256.Sum256([]byte(strings.Join(parts, "\x00")))
|
|
return hex.EncodeToString(h[:])[:16]
|
|
}
|
|
|
|
// lkSock is a link's socket on this machine; lkCtl, its session's
|
|
// control socket.
|
|
func lkSock(s *S, vm, addr, port string) string {
|
|
return filepath.Join(s.Paths.Devvm, "run", "link-"+lkHash(vm, "socket", addr, port)+".sock")
|
|
}
|
|
|
|
func lkCtl(s *S, vm string) string {
|
|
return filepath.Join(s.Paths.Devvm, "run", "link-"+lkHash(vm, "session")+".ctl")
|
|
}
|
|
|
|
// firstPid is the first host process whose command line matches pattern,
|
|
// nothing without one.
|
|
func firstPid(s *S, pattern string) string {
|
|
if p := pids(s, pattern); len(p) > 0 {
|
|
return p[0]
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// lkSession is the pid of the VM's devvm _links; lkMaster, of its ssh
|
|
// master to the VM.
|
|
func lkSession(s *S, vm string) string { return firstPid(s, "[_]links "+vm+"$") }
|
|
|
|
func lkMaster(s *S, vm string) string { return firstPid(s, "[-]M -S "+lkCtl(s, vm)+" ") }
|
|
|
|
// lkDesc is the pids and their descendants'.
|
|
func lkDesc(s *S, pids ...string) []string {
|
|
var all []string
|
|
for _, p := range pids {
|
|
all = append(all, p)
|
|
r, _ := s.Run(Cmd{Args: []string{"pgrep", "-P", p}})
|
|
all = append(all, lkDesc(s, strings.Fields(r.Stdout)...)...)
|
|
}
|
|
return all
|
|
}
|
|
|
|
// lkHostTCP is those processes' TCP sockets on this machine, "LOCAL
|
|
// (LISTEN)" or "LOCAL->PEER (STATE)".
|
|
func lkHostTCP(s *S, pids []string) []string {
|
|
var lines []string
|
|
if runtime.GOOS == "darwin" {
|
|
r, _ := s.Run(Cmd{Args: []string{"lsof", "-nP", "-a", "-p", strings.Join(pids, ","), "-iTCP"}})
|
|
for i, line := range strings.Split(strings.TrimRight(r.Stdout, "\n"), "\n") {
|
|
if f := strings.Fields(line); i > 0 && len(f) >= 9 {
|
|
lines = append(lines, strings.TrimSpace(strings.Join(f[8:min(10, len(f))], " ")))
|
|
}
|
|
}
|
|
return lines
|
|
}
|
|
r, _ := s.Run(Cmd{Args: []string{"ss", "-Htanp"}})
|
|
mine := regexp.MustCompile(`pid=(` + strings.Join(pids, "|") + `),`)
|
|
for _, line := range strings.Split(r.Stdout, "\n") {
|
|
f := strings.Fields(line)
|
|
if len(f) < 5 || !mine.MatchString(line) {
|
|
continue
|
|
}
|
|
if f[0] == "LISTEN" {
|
|
lines = append(lines, f[3]+" ("+f[0]+")")
|
|
} else {
|
|
lines = append(lines, f[3]+"->"+f[4]+" ("+f[0]+")")
|
|
}
|
|
}
|
|
return lines
|
|
}
|
|
|
|
// lkHostClean: the client's link session and every process it started
|
|
// listen on no TCP port of this machine's, and connect only to the ssh
|
|
// ports of vms on its loopback.
|
|
func lkHostClean(s *S, client string, vms ...string) error {
|
|
pid := lkSession(s, client)
|
|
if pid == "" {
|
|
return errors.New("no link session")
|
|
}
|
|
t := lkHostTCP(s, lkDesc(s, pid))
|
|
Note(s, "the link session's TCP: %s", strings.Join(t, " "))
|
|
var bad []string
|
|
for _, line := range t {
|
|
if strings.Contains(line, "LISTEN") {
|
|
return errors.New("the link session listens on a TCP port of this machine's: " + line)
|
|
}
|
|
_, peer, ok := strings.Cut(strings.Fields(line)[0], "->")
|
|
if !ok {
|
|
continue
|
|
}
|
|
if !slices.ContainsFunc(vms, func(v string) bool { return peer == "127.0.0.1:"+s.Paths.State(v)["ports.host.ssh"] }) {
|
|
bad = append(bad, peer)
|
|
}
|
|
}
|
|
if len(bad) > 0 {
|
|
return errors.New("connected to: " + strings.Join(bad, " "))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// lkListens is the VM's TCP listeners, "PROCESS ADDR:PORT", read as root
|
|
// and as its user: root in a container may not read the processes of the
|
|
// user's (no CAP_SYS_PTRACE), whose sshd has the links'.
|
|
func lkListens(s *S, vm string) []string {
|
|
names := map[string]string{}
|
|
var order []string
|
|
for _, as := range []func() (Res, error){
|
|
func() (Res, error) { return lkRoot(s, vm, "perl /var/tmp/e2e-link.pl listeners") },
|
|
func() (Res, error) { return s.InVM(vm, "perl /var/tmp/e2e-link.pl listeners") },
|
|
} {
|
|
r, _ := as()
|
|
for _, line := range strings.Split(r.Stdout, "\n") {
|
|
f := strings.Fields(line)
|
|
if len(f) != 2 {
|
|
continue
|
|
}
|
|
n, ok := names[f[1]]
|
|
if !ok {
|
|
order = append(order, f[1])
|
|
}
|
|
if !ok || n == "?" {
|
|
names[f[1]] = f[0]
|
|
}
|
|
}
|
|
}
|
|
lines := make([]string, 0, len(order))
|
|
for _, a := range order {
|
|
lines = append(lines, names[a]+" "+a)
|
|
}
|
|
sort.Strings(lines)
|
|
return lines
|
|
}
|
|
|
|
// lkListening is whether something in the VM listens on addr
|
|
// (ADDR:PORT).
|
|
func lkListening(s *S, vm, addr string) bool {
|
|
return slices.ContainsFunc(lkListens(s, vm), func(l string) bool { return strings.HasSuffix(l, " "+addr) })
|
|
}
|
|
|
|
// lkVMClean: the VM's sshd listens only on its own port (22) and links'
|
|
// addresses, and on each port sshd listens on a link's address and
|
|
// nothing listens anywhere else (127.0.0.1, ::1, every address).
|
|
func lkVMClean(s *S, vm string, ports ...string) error {
|
|
l := lkListens(s, vm)
|
|
if len(l) == 0 {
|
|
return errors.New("no listeners read")
|
|
}
|
|
var bad []string
|
|
for _, line := range l {
|
|
f := strings.Fields(line)
|
|
if strings.HasPrefix(f[0], "sshd") && !strings.HasSuffix(f[1], ":22") && !strings.HasPrefix(f[1], "127.77.") {
|
|
bad = append(bad, line)
|
|
}
|
|
}
|
|
if len(bad) > 0 {
|
|
return errors.New("sshd also listens on: " + strings.Join(bad, ", "))
|
|
}
|
|
for _, p := range ports {
|
|
var on, other []string
|
|
link := false
|
|
for _, line := range l {
|
|
if !strings.HasSuffix(line, ":"+p) {
|
|
continue
|
|
}
|
|
on = append(on, line)
|
|
if Matches(line, `^sshd[^ ]* 127\.77\.[0-9]+\.[0-9]+:`+p+`$`) {
|
|
link = true
|
|
}
|
|
if !strings.Contains(line, " 127.77.") {
|
|
other = append(other, line)
|
|
}
|
|
}
|
|
if !link {
|
|
return fmt.Errorf("no link listens on %s: %s", p, strings.Join(on, ", "))
|
|
}
|
|
if len(other) > 0 {
|
|
return fmt.Errorf("on %s: %s", p, strings.Join(other, ", "))
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// lkEvents is the VM's network events, its rotated log's first when
|
|
// rotated is set.
|
|
func lkEvents(s *S, vm string, rotated bool) []string {
|
|
dir := filepath.Join(s.Paths.VMDir(vm), "logs")
|
|
var b []byte
|
|
if rotated {
|
|
b, _ = os.ReadFile(filepath.Join(dir, "network.jsonl.1"))
|
|
}
|
|
c, _ := os.ReadFile(filepath.Join(dir, "network.jsonl"))
|
|
return strings.Split(string(append(b, c...)), "\n")
|
|
}
|
|
|
|
// lkOf is the events for the link name:port.
|
|
func lkOf(events []string, name, port string) []string {
|
|
var of []string
|
|
for _, e := range events {
|
|
if strings.Contains(e, `"host":"`+name+`","port":`+port+`,`) {
|
|
of = append(of, e)
|
|
}
|
|
}
|
|
return of
|
|
}
|
|
|
|
// lkEvent is whether the VM's event log has that event for the link
|
|
// name:port, with text.
|
|
func lkEvent(s *S, vm, event, name, port, text string) bool {
|
|
return slices.ContainsFunc(lkEvents(s, vm, false), func(e string) bool {
|
|
return strings.Contains(e, `"event":"`+event+`","host":"`+name+`","port":`+port+`,`) && strings.Contains(e, text)
|
|
})
|
|
}
|
|
|
|
// lkUntilEvent waits up to seconds for lkEvent.
|
|
func lkUntilEvent(s *S, seconds int, vm, event, name, port, text string) error {
|
|
return s.Until(time.Duration(seconds)*time.Second, func() error {
|
|
if lkEvent(s, vm, event, name, port, text) {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("no %s event for %s:%s with %s", event, name, port, text)
|
|
})
|
|
}
|
|
|
|
// lkSetNet sets the VM's tier by hand, as its state has it (on:
|
|
// allowlist, off: open), as nothing devvm runs would: the rule broken
|
|
// under devvm, which checks it again on every connection and at each
|
|
// launch.
|
|
func lkSetNet(s *S, vm, tier string) error {
|
|
switch tier {
|
|
case "on":
|
|
tier = "allowlist"
|
|
case "off":
|
|
tier = "open"
|
|
}
|
|
return SetState(s.Paths, vm, "net.tier", strconv.Quote(tier))
|
|
}
|
|
|
|
// lkHostile has root in the VM loosen its sshd (gateway: GatewayPorts
|
|
// too); lkHonest puts it back.
|
|
func lkHostile(s *S, vm, h string) error {
|
|
_, err := lkRoot(s, vm, "h="+h+"; "+lkHostileSh)
|
|
return err
|
|
}
|
|
|
|
func lkHonest(s *S, vm string) error {
|
|
_, err := lkRoot(s, vm, lkHonestSh)
|
|
return err
|
|
}
|
|
|
|
// lkReconnect ends the VM's link session's connection (its master
|
|
// killed), so the session makes a new one, to sshd as it is now; the link
|
|
// name:port answers word again through it.
|
|
func lkReconnect(s *S, vm, name, port, word string) error {
|
|
m := lkMaster(s, vm)
|
|
if m == "" {
|
|
return errors.New("no master")
|
|
}
|
|
_, _ = s.Run(Cmd{Args: []string{"kill", m}})
|
|
if err := s.Until(30*time.Second, func() error {
|
|
if n := lkMaster(s, vm); n != "" && n != m {
|
|
return nil
|
|
}
|
|
return errors.New("no new master")
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
return s.Until(30*time.Second, func() error { return lkGets(s, vm, name, port, word) })
|
|
}
|
|
|
|
// lkKnownHosts is the VM's host keys as devvm recorded them, sorted.
|
|
func lkKnownHosts(s *S, vm string) string {
|
|
b, _ := os.ReadFile(filepath.Join(s.Paths.VMDir(vm), "run", "known_hosts"))
|
|
var keys []string
|
|
for _, line := range strings.Split(string(b), "\n") {
|
|
if strings.HasPrefix(line, vm+" ") {
|
|
keys = append(keys, line)
|
|
}
|
|
}
|
|
sort.Strings(keys)
|
|
return strings.Join(keys, "\n")
|
|
}
|
|
|
|
// lkVia is the status curl in the VM gets for u through the VM's own
|
|
// proxy, whatever its NO_PROXY says (connect: by CONNECT, its answer's
|
|
// status), and the body's line after it.
|
|
func lkVia(s *S, vm, u string, connect bool) (string, string) {
|
|
w, p := "%{http_code}", ""
|
|
if connect {
|
|
w, p = "%{http_connect}", "-p "
|
|
}
|
|
r, _ := s.InVM(vm, `curl -s -m 10 --noproxy '' -x "$HTTP_PROXY" `+p+`-w '`+w+`\n' `+u+` 2>/dev/null | tail -2 | tac 2>/dev/null || true`)
|
|
lines := strings.Split(out(r), "\n")
|
|
code, body := lines[0], ""
|
|
if len(lines) > 1 {
|
|
body = lines[1]
|
|
}
|
|
return code, body
|
|
}
|
|
|
|
// lkViaGets: through the VM's proxy, u answers 200 with word.
|
|
func lkViaGets(s *S, vm, u, word string, connect bool) error {
|
|
code, body := lkVia(s, vm, u, connect)
|
|
if code != "200" || body != word {
|
|
return fmt.Errorf("%s (CONNECT: %v): %s %s", u, connect, code, body)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// lkBoot is the VM's boot and its init's start, which a restart of the
|
|
// VM changes on every backend.
|
|
func lkBoot(s *S, vm string) string {
|
|
r, _ := lkRoot(s, vm, `cat /proc/sys/kernel/random/boot_id; cut -d" " -f22 /proc/1/stat`)
|
|
return strings.Join(strings.Fields(r.Stdout), " ")
|
|
}
|