eBPF-based security platform
contrib: give the libguestfs appliance the host's resolvers and proxy
Puranjay Mohana933a7fdfa09
diff --git a/contrib/prepare-test-vm-runtime.sh b/contrib/prepare-test-vm-runtime.shindex 6d443f62..814f9100 100755--- a/contrib/prepare-test-vm-runtime.sh+++ b/contrib/prepare-test-vm-runtime.sh@@ -69,18 +69,48 @@ fiTMP_IMAGE="$CACHE_DIR/.${RUNTIME_KEY}.tmp.$$"MARKER="${TMP_IMAGE}.marker"-cleanup() { rm -f -- "$TMP_IMAGE" "$MARKER"; }+cleanup() { rm -f -- "$TMP_IMAGE" "$MARKER" "${TMP_IMAGE}.nameservers"; }trap cleanup EXIT# The cloud image is intentionally small. Enlarge the backing overlay and# grow its existing root partition before installing the runtime packages.qemu-img create -q -f qcow2 -F qcow2 -b "$ROOTFS" "$TMP_IMAGE"qemu-img resize -q "$TMP_IMAGE" "$RUNTIME_DISK_SIZE"-paste -sd, "$PACKAGE_FILE" >"${TMP_IMAGE}.packages"-PACKAGES="$(<"${TMP_IMAGE}.packages")"-rm -f -- "${TMP_IMAGE}.packages"+PACKAGES="$(paste -sd' ' "$PACKAGE_FILE")"printf 'shirudo-test-vm-runtime-v1 %s\n' "$RUNTIME_KEY" >"$MARKER"+# The appliance gets IPv4 DHCP only, so on a host with IPv6 or stub+# resolvers it has no nameserver. It rebuilds resolv.conf for every command,+# so the host's resolvers are added in the same command as apt rather than+# through virt-customize --install.+NAMESERVERS="${TMP_IMAGE}.nameservers"+awk '$1 == "nameserver" && $2 !~ /^(127\.|::1$|fe80:)/ { print "nameserver " $2 }' \+ "${TEST_VM_RESOLV_CONF:-/etc/resolv.conf}" >"$NAMESERVERS" 2>/dev/null ||+ : >"$NAMESERVERS"+# The appliance does not inherit the caller's proxy; pass it to apt.+# TEST_VM_APT_PROXY overrides http_proxy and https_proxy. The URL goes into+# a shell command, so only a plain one is accepted.+APT_HTTP_PROXY="${TEST_VM_APT_PROXY:-${http_proxy:-${HTTP_PROXY:-}}}"+APT_HTTPS_PROXY="${TEST_VM_APT_PROXY:-${https_proxy:-${HTTPS_PROXY:-$APT_HTTP_PROXY}}}"+PROXY_OPTS=""+for proxy_url in "$APT_HTTP_PROXY" "$APT_HTTPS_PROXY"; do+ [[ -z "$proxy_url" || "$proxy_url" =~ ^https?://[]A-Za-z0-9._:@%/[-]+$ ]] ||+ die "the apt proxy must be a plain http:// or https:// URL"+done+[[ -z "$APT_HTTP_PROXY" ]] ||+ PROXY_OPTS+=" -o Acquire::http::Proxy=$APT_HTTP_PROXY"+[[ -z "$APT_HTTPS_PROXY" ]] ||+ PROXY_OPTS+=" -o Acquire::https::Proxy=$APT_HTTPS_PROXY"+# shellcheck disable=SC2016 # expanded in the appliance+INSTALL_SCRIPT='set -e+export DEBIAN_FRONTEND=noninteractive+if ! grep -q "^nameserver" /etc/resolv.conf; then+ cat /root/shirudo-nameservers >>/etc/resolv.conf+fi+apt_opts="-q -y -o Dpkg::Options::=--force-confnew'"$PROXY_OPTS"'"+apt-get $apt_opts update+apt-get $apt_opts install '"$PACKAGES"+printf '[test-vm-runtime] preparing %s\n' "$IMAGE" >&2CUSTOMIZE_LOG="${TMP_IMAGE}.virt-customize.log"if ! TMPDIR="$GUESTFS_TMP_DIR" XDG_RUNTIME_DIR="$GUESTFS_RUNTIME_DIR" \@@ -88,7 +118,9 @@ if ! TMPDIR="$GUESTFS_TMP_DIR" XDG_RUNTIME_DIR="$GUESTFS_RUNTIME_DIR" \LIBGUESTFS_CACHEDIR="$GUESTFS_CACHE_DIR" virt-customize \-a "$TMP_IMAGE" --network --no-selinux-relabel \--run-command 'growpart /dev/sda 1 && resize2fs /dev/sda1' \- --install "$PACKAGES" \+ --upload "$NAMESERVERS:/root/shirudo-nameservers" \+ --run-command "$INSTALL_SCRIPT" \+ --run-command 'rm -f /root/shirudo-nameservers' \--upload "$MARKER:/etc/shirudo-test-vm-runtime" \--run-command 'apt-get clean' \--run-command 'rm -rf /var/lib/apt/lists/*' \diff --git a/testing/t0015-test-vm-cli.sh b/testing/t0015-test-vm-cli.shindex 6bb9ea79..98366ce8 100755--- a/testing/t0015-test-vm-cli.sh+++ b/testing/t0015-test-vm-cli.sh@@ -219,6 +219,75 @@ test_expect_success 'the runtime image is built with the direct libguestfs backetest "$(guestfs_env TEST_VM_QEMU="$WORK/gf-bin/none")" = "direct unset"'+# Stand-ins for the tools prepare-test-vm-runtime.sh calls; every command it+# runs is recorded, so the tests below read what it would have done.+mkdir -p "$WORK/dns-bin" "$WORK/dns-cache"+printf "%s\n" "#!/bin/sh" \+ "printf \"%s\\n\" \"\$@\" >\"\$GF_ARGS\"" \+ "for a in \"\$@\"; do case \$a in" \+ "*:/root/shirudo-nameservers) cp \"\${a%%:/root/shirudo-nameservers}\" \"\$GF_NS\" ;;" \+ "esac; done" >"$WORK/dns-bin/virt-customize"+printf "%s\n" "#!/bin/sh" \+ "if [ \"\$1\" = create ]; then for a in \"\$@\"; do last=\$a; done; : >\"\$last\"; fi" \+ >"$WORK/dns-bin/qemu-img"+chmod +x "$WORK/dns-bin/virt-customize" "$WORK/dns-bin/qemu-img"+printf rootfs >"$WORK/dns-rootfs"+printf "%s\n" "search example.com" "nameserver 127.0.0.53" \+ "nameserver 2001:db8::53" "nameserver ::1" \+ "nameserver fe80::1%eth0" "nameserver 192.0.2.53" \+ >"$WORK/dns-resolv.conf"++# Run prepare-test-vm-runtime.sh against stand-ins for the tools it needs:+# run_dns RESOLV_CONF [VAR=value...].+run_dns() {+ resolv=$1+ shift+ rm -f "$WORK/dns-cache/img/dns-test.qcow2" &&+ env -u http_proxy -u HTTP_PROXY -u https_proxy -u HTTPS_PROXY \+ -u TEST_VM_APT_PROXY "$@" \+ PATH="$WORK/dns-bin:$PATH" TEST_VM_ARCH=x86_64 TMPDIR="$WORK" \+ GF_ARGS="$WORK/dns-args" GF_NS="$WORK/dns-ns" \+ LIBGUESTFS_CACHEDIR="$WORK/dns-cache" \+ TEST_VM_RESOLV_CONF="$resolv" \+ ../contrib/prepare-test-vm-runtime.sh \+ --rootfs "$WORK/dns-rootfs" --cache-dir "$WORK/dns-cache/img" \+ --package-file ../contrib/test-vm-runtime-packages \+ --runtime-key dns-test --key-output "$WORK/dns-cache/key" \+ >"$WORK/dns-run.out" 2>&1+}++test_expect_success 'the runtime image gives the appliance the host resolvers in the apt command' '+ run_dns "$WORK/dns-resolv.conf" &&+ printf "%s\n" "nameserver 2001:db8::53" "nameserver 192.0.2.53" \+ >"$WORK/dns-expect" &&+ cmp "$WORK/dns-expect" "$WORK/dns-ns" &&+ ! grep -qx -- "--install" "$WORK/dns-args" &&+ grep -q "cat /root/shirudo-nameservers >>/etc/resolv.conf" "$WORK/dns-args" &&+ grep -q "apt-get \$apt_opts update" "$WORK/dns-args" &&+ grep -q "apt-get \$apt_opts install ca-certificates keyutils" "$WORK/dns-args" &&+ grep -q "ripgrep rsync" "$WORK/dns-args" &&+ run_dns "$WORK/no-such-resolv.conf" &&+ ! test -s "$WORK/dns-ns" &&+ ! grep -q "Acquire::http::Proxy" "$WORK/dns-args"+'++test_expect_success 'the runtime image passes the caller proxy to apt in the appliance' '+ run_dns "$WORK/dns-resolv.conf" http_proxy=http://proxy.example:8080 &&+ grep -q "Acquire::http::Proxy=http://proxy.example:8080" "$WORK/dns-args" &&+ grep -q "Acquire::https::Proxy=http://proxy.example:8080" "$WORK/dns-args" &&+ run_dns "$WORK/dns-resolv.conf" http_proxy=http://a.example:1 \+ https_proxy=http://b.example:2 &&+ grep -q "Acquire::http::Proxy=http://a.example:1" "$WORK/dns-args" &&+ grep -q "Acquire::https::Proxy=http://b.example:2" "$WORK/dns-args" &&+ run_dns "$WORK/dns-resolv.conf" http_proxy=http://a.example:1 \+ TEST_VM_APT_PROXY=http://override.example:3 &&+ grep -q "Acquire::http::Proxy=http://override.example:3" "$WORK/dns-args" &&+ ! grep -q "a.example" "$WORK/dns-args" &&+ ! run_dns "$WORK/dns-resolv.conf" "http_proxy=http://x;touch /tmp/pwned" &&+ ! run_dns "$WORK/dns-resolv.conf" "TEST_VM_APT_PROXY=ftp://x.example" &&+ grep -q "plain http" "$WORK/dns-run.out"+'+test_expect_success 'the kernel package format follows the host package manager' 'printf "print:\n\t@echo \$(PACKAGE_FORMAT)\n" >"$WORK/print.mk" &&format() {