eBPF-based security platform
testing: let make vm enroll its key in the local stack
John Fastabend82e348bb1ad6
diff --git a/Documentation/contribute/testing/testing.rst b/Documentation/contribute/testing/testing.rstindex 3622f026..85f5d0ca 100644--- a/Documentation/contribute/testing/testing.rst+++ b/Documentation/contribute/testing/testing.rst@@ -704,7 +704,36 @@ the keepalives. The VM then shows in ``shirudo.node_status``, and$ make vm$ CLICKHOUSE_URL=http://127.0.0.1:18124/ tools/xtop/dist/xtop-Without the checker it starts the telemetry stack alone and says so. A+Every ``make vm`` acts as the builder of :doc:`/operate/run/attest`:+before the guest first boots, :src:`testing/vm/tpm-builder.sh` creates+its attestation and endorsement keys in the fresh vTPM, and writes+``enroll.json`` in its state directory, naming the node by the VM's name,+which is also its host name. ``make vm-enroll`` gathers the+``enroll.json`` of every VM under ``build/test-vm/vms/`` into+``build/test-vm/enroll.json`` and loads it into the stack's backend with+one ``enroll-put``, so that the VMs read ``enrolled``. It prints the file+and the command before it runs them:++.. code-block:: console++ $ make vm-enroll+ $ cat build/test-vm/enroll.json+ {"source": "test-vm", "nodes": [+ {"ak_name": "000b…", "ek_name": "000b…", "node_id": "interactive-Xa3k9q"},+ {"ak_name": "000b…", "ek_name": "000b…", "node_id": "interactive-pL2m7c"}+ ]}+ $ CLICKHOUSE_CLIENT='docker exec -i shirudo-dev-clickhouse clickhouse-client' tools/backend/jobs/enroll-put build/test-vm/enroll.json+ enroll-put: 000b… (ek_name 000b…, node_id interactive-Xa3k9q): already enrolled+ enroll-put: 000b… (ek_name 000b…, node_id interactive-pL2m7c): enrolled+ enroll-put: loaded bindings: 2++A VM enrolled before is loaded again, which changes nothing, so run it+after each ``make vm``, before the VM's first keepalive or after. A VM+stopped but not removed with ``make vm-clean`` keeps its state directory+and is enrolled too. ``VM_NAME=<name>`` enrolls that VM alone, from its+own ``enroll.json``.++Without the checker, ``make telemetry`` starts the telemetry stack alone and says so. Astack started before the checker was built keeps its collector: run``make stack-down`` before ``make telemetry`` to add the backend.@@ -880,7 +909,8 @@ and leaves it running until ``host.sh`` stops it. Before the guest boots,node mode acts as the builder of :doc:`/operate/run/attest`::src:`testing/vm/tpm-builder.sh` creates the attestation key and theendorsement key in the node's fresh vTPM state, with the tpm2-tools-commands that page gives, and writes their names to ``tpm-names`` in the+commands that page gives, and writes their names to ``tpm-names`` and to+``enroll.json``, the file :doc:`/operate/backend/enroll-put` loads, in theVM's state directory. As the vTPM's maker, it also writes anendorsement certificate for the endorsement key to the ECC index of theTCG profile, ``0x01C0000A``, signed by a CA of its own whose key it@@ -1015,8 +1045,9 @@ and the nodes' directory in its environment; its output isother state of an SBOM, and runs the backend's sbom-scan job with thestub ``trivy`` of the fixtures, which prints one ``openssl`` finding;#. checks that node B's records carry the attestation key its builder- recorded, and enrolls that key, inserting ``enrolled`` for it into- ``shirudo.nodes`` as an operator does;+ recorded, and enrolls that key from the builder's ``enroll.json`` with+ :doc:`/operate/backend/enroll-put`, as an operator does, and checks+ the endorsement key's name it recorded;#. seeds the kernel advisory of the fixtures, whose range holds every testkernel, and the nodes' build, and refreshes ``node_status`` and``node_vulns``.diff --git a/Makefile b/Makefileindex 446fd1f7..db7719d9 100644--- a/Makefile+++ b/Makefile@@ -89,7 +89,7 @@ help:'shirudo make targets:' \' Build: make, make target, make target-deps, make bpf-deps, make install' \' Operator tools: make shirudo-build, make quote-check' \- ' Local stack: make stack-up, make oci, make telemetry, make vm, make stack-down' \+ ' Local stack: make stack-up, make oci, make telemetry, make vm, make vm-enroll, make stack-down' \' Checks: make sparse' \' Source index: make tags (ctags), make cscope' \' Install roles: make install-target, make install-operator' \@@ -196,6 +196,6 @@ distclean: clean$(Q)rm -rf build/quote-check-deps "$(QUOTE_CHECK_OPENSSL_PREFIX)"$(Q)$(MAKE) -f Makefile.shirudo distclean-.PHONY: vm vm-console vm-ssh vm-stop vm-clean oci telemetry stack-up stack-down-vm vm-console vm-ssh vm-stop vm-clean oci telemetry stack-up stack-down:+.PHONY: vm vm-console vm-ssh vm-enroll vm-stop vm-clean oci telemetry stack-up stack-down+vm vm-console vm-ssh vm-enroll vm-stop vm-clean oci telemetry stack-up stack-down:+$(Q)$(MAKE) -C testing $@diff --git a/testing/Makefile b/testing/Makefileindex f68bb54a..f1036d88 100644--- a/testing/Makefile+++ b/testing/Makefile@@ -29,7 +29,7 @@ test-vm-arch = $(shell TEST_VM_ARCH='$(TEST_VM_ARCH)'; \.DEFAULT_GOAL := all.PHONY: all help clean unit ci-checks functional end2end build-operator build-unit \test-helpers \- fuzz afl bundle test-vm-bundle require-vm-kernel vm vm-console vm-ssh vm-stop vm-clean \+ fuzz afl bundle test-vm-bundle require-vm-kernel vm vm-console vm-ssh vm-enroll vm-stop vm-clean \vm-operator-end2endPACKAGE_FORMAT ?= $(call test-vm-arch,TEST_VM_HOST_PKG_FORMAT)@@ -211,6 +211,10 @@ vm-ssh:$(Q)VM_NAME="$(VM_NAME)" \TEST_VM_WORK_DIR="$(TEST_VM_WORK_DIR)" $(ROOT)/testing/vm/run.sh --ssh+vm-enroll:+ $(Q)VM_NAME="$(VM_NAME)" \+ TEST_VM_WORK_DIR="$(TEST_VM_WORK_DIR)" $(ROOT)/testing/vm/run.sh --enroll+define run-target-vmVM_MODE=$(or $(4),$(if $(filter $(1),$(VM_SERVE_SUITES)),serve,test)) \TEST_VM_SUITE="$(1)" VM_VERBOSE="$(TEST_VM_VERBOSE)" VM_NAME="$(2)" VM_TENANT="$(VM_TENANT)" \@@ -393,7 +397,7 @@ help:' make -j boots several at once.' \' make -C testing fuzz [FUZZ_SECONDS=60] [FUZZ_CORPUS=<dir>] [FUZZERS=<target>...]' \' make -C testing afl [AFL_SECONDS=600] [FUZZ_CORPUS=<dir>] [FUZZERS=<target>...]' \- ' make -C testing vm, vm-console, vm-ssh, vm-stop, or vm-clean' \+ ' make -C testing vm, vm-console, vm-ssh, vm-enroll, vm-stop, or vm-clean' \' VM_KERNEL=bpf-next (default) selects the shirudo-dev/kernel artifact' \' make -C testing bundle or test-helpers' \' make -C testing stack-up, oci, telemetry or stack-down' \diff --git a/testing/end2end/host.sh b/testing/end2end/host.shindex c3d25553..26deebdf 100755--- a/testing/end2end/host.sh+++ b/testing/end2end/host.sh@@ -92,6 +92,7 @@ node_boot() {>"$W/node-$1$NODE_ROUND.log" 2>&1 &&cp "$run/ssh.port" "$NODES_DIR/node-$1/port" &&cp "$run/tpm-names" "$NODES_DIR/node-$1/tpm-names" &&+ cp "$run/enroll.json" "$NODES_DIR/node-$1/enroll.json" &&cp "$run/ek-ca.pem" "$NODES_DIR/node-$1/ek-ca.pem" &&install -m 0600 "$run/ssh/id_ed25519" "$NODES_DIR/node-$1/key"; then: >"$NODES_DIR/node-$1/ready"diff --git a/testing/end2end/node-lib.sh b/testing/end2end/node-lib.shindex 88ea9bf4..bc8018b5 100644--- a/testing/end2end/node-lib.sh+++ b/testing/end2end/node-lib.sh@@ -10,8 +10,9 @@# Where host.sh records each node, in node-<name>/: port, the host port of# its SSH server, key, the key of its root, tpm-names, the names of its-# keys its builder recorded, ek-ca.pem, the CA of its endorsement-# certificate, and ready once it is prepared, else failed.+# keys its builder recorded, enroll.json, the same as jobs/enroll-put+# loads them, ek-ca.pem, the CA of its endorsement certificate, and ready+# once it is prepared, else failed.: "${NODES_DIR:=/mnt/shirudo-src/build/test-vm/end2end}"# Where the nodes' SSH ports are forwarded: QEMU's address for the host,# as node A sees it.diff --git a/testing/end2end/t9109-pipeline.sh b/testing/end2end/t9109-pipeline.shindex 6cda775a..6164212b 100755--- a/testing/end2end/t9109-pipeline.sh+++ b/testing/end2end/t9109-pipeline.sh@@ -166,8 +166,9 @@ test_expect_success 'node B keeps the attestation key its builder recorded' ''test_expect_success 'the operator enrolls node B from its builder'\''s records' '- ch "INSERT INTO shirudo.nodes (ak_name, trust)- VALUES ('\''$(node_builder_name b ak)'\'', '\''enrolled'\'')"+ "$BACKEND/jobs/enroll-put" "$NODES_DIR/node-b/enroll.json" >&2 &&+ test "$(ch "SELECT ek_name FROM shirudo.enrollments FINAL+ WHERE ak_name = '\''$(node_builder_name b ak)'\''")" = "$(node_builder_name b ek)"'test_expect_success 'the backend keeps node B enrolled, the edge node untrusted, and every record' 'diff --git a/testing/vm/run.sh b/testing/vm/run.shindex e327c983..4380ad0e 100755--- a/testing/vm/run.sh+++ b/testing/vm/run.sh@@ -508,19 +508,56 @@ validate_host_bundle()die "prepared runtime image is missing: $runtime_image; run 'make -C testing bundle'"}+vm_has_builder()+{+ [[ "$VM_MODE" == node || "$VM_MODE" == interactive ]]+}++# The bindings of every VM whose builder wrote an enroll.json, in one file+# for one enroll-put: those enrolled before are loaded again, which+# changes nothing.+merge_enroll_files()+{+ local files=("$VMS_DIR"/*/enroll.json)++ [[ -s "${files[0]}" ]] ||+ die "no VM under $VMS_DIR has an enroll.json; start one with 'make -C testing vm'"+ python3 - "$VM_ROOT/enroll.json" "${files[@]}" <<'PY'+import json, sys+nodes = [n for path in sys.argv[2:] for n in json.load(open(path))["nodes"]]+with open(sys.argv[1], "w") as f:+ f.write('{"source": "test-vm", "nodes": [\n')+ f.write(",\n".join(" " + json.dumps(n) for n in nodes))+ f.write("\n]}\n")+PY+}++run_enroll()+{+ if [[ -z "$VM_NAME_REQUEST" ]]; then+ merge_enroll_files+ exec "$REPO_ROOT/testing/vm/stack.sh" enroll "$VM_ROOT/enroll.json"+ fi+ select_named_run_dir+ [[ -s "$RUN_DIR/enroll.json" ]] ||+ die "VM '$VM_NAME' has no enroll.json: only 'make vm' and end2end nodes have a builder"+ exec "$REPO_ROOT/testing/vm/stack.sh" enroll "$RUN_DIR/enroll.json"+}+start_swtpm(){local _chmod 0700 "$RUN_DIR/tpm"- # An end2end node's TPM comes from a builder the operator trusts, which- # records the names of its keys before the guest boots, and carries an- # endorsement certificate from its maker's CA.- if [[ "$VM_MODE" == node ]]; then+ # An end2end node's or an interactive VM's TPM comes from a builder the+ # operator trusts, which records the names of its keys before the guest+ # boots, and carries an endorsement certificate from its maker's CA.+ if vm_has_builder; then"$REPO_ROOT/testing/vm/tpm-builder.sh" "$RUN_DIR/tpm" "$RUN_DIR/tpm-names" \- "$RUN_DIR/ek-ca.pem" \+ "$RUN_DIR/ek-ca.pem" "$RUN_DIR/enroll.json" "$VM_NAME" \>>"$RUN_DIR/swtpm.log" 2>&1 ||die "could not create the keys of the vTPM; see $RUN_DIR/swtpm.log"+ log "builder's records of the vTPM keys: $RUN_DIR/enroll.json (make vm-enroll loads them)"fiif user_systemd_available; thenSWTPM_UNIT_FILE="${RUN_DIR}/swtpm.unit"@@ -1495,6 +1532,10 @@ while [[ "$#" -gt 0 ]]; dorun_sshexit 0;;+ --enroll)+ [[ "$#" -eq 1 ]] || die "--enroll does not accept additional arguments"+ run_enroll+ ;;--verbose)VM_VERBOSE=1shift@@ -1519,7 +1560,7 @@ validate_bpm_ociif [[ "$VM_INITRD" == 1 ]]; thenHOST_DEPENDENCIES+=("dracut:dracut-core:dracut" "lsinitrd:dracut-core:dracut" "file:file:file")fi-if [[ "$VM_MODE" == node ]]; then+if vm_has_builder; thenHOST_DEPENDENCIES+=("tpm2_createprimary:tpm2-tools:tpm2-tools""swtpm_ioctl:swtpm-tools:swtpm-tools" "openssl:openssl:openssl")fi@@ -1527,7 +1568,7 @@ check_host_dependencies# tpm2-tools load tpm2-tss's swtpm TCTI only when tpm-builder.sh runs. grep# reads all of ldconfig's output, which pipefail would otherwise fail on# SIGPIPE.-if [[ "$VM_MODE" == node ]] &&+if vm_has_builder &&! PATH="$PATH:/sbin:/usr/sbin" ldconfig -p |grep 'libtss2-tcti-swtpm\.so' >/dev/null; thenlog "error: missing the swtpm TCTI of tpm2-tss, libtss2-tcti-swtpm.so"diff --git a/testing/vm/stack.sh b/testing/vm/stack.shindex e85aaf92..2601dc17 100755--- a/testing/vm/stack.sh+++ b/testing/vm/stack.sh@@ -22,7 +22,7 @@ fiexec 9>"$W/.lock"flock 9case "$TLS_DIR/" in "$REPO/"*) echo 'stack private keys must be outside the checkout' >&2; exit 1 ;; esac-command=${1:?usage: stack.sh prepare|oci|telemetry|down|vm}+command=${1:?usage: stack.sh prepare|oci|telemetry|down|enroll|vm}shiftprepare(){@@ -51,6 +51,18 @@ telemetry)fi;;down) stack_down ;;+enroll)+ # Shown before it runs, so that a demo shows what the step is.+ file=$(realpath --relative-to="$REPO" "${1:?usage: stack.sh enroll <enroll.json>}")+ [ "$(docker container inspect -f '{{.State.Running}}' "$STACK_NAME-clickhouse" 2>/dev/null)" = true ] ||+ { echo 'the local stack is not up: run make stack-up' >&2; exit 1; }+ cd "$REPO"+ client="docker exec -i $STACK_NAME-clickhouse clickhouse-client"+ printf '$ cat %s\n' "$file"+ cat "$file"+ printf "\$ CLICKHOUSE_CLIENT='%s' tools/backend/jobs/enroll-put %s\n" "$client" "$file"+ CLICKHOUSE_CLIENT=$client tools/backend/jobs/enroll-put "$file"+ ;;vm)prepareexport TEST_VM_LOCAL_STACK=1diff --git a/testing/vm/tpm-builder.sh b/testing/vm/tpm-builder.shindex 392a8a16..8db97995 100755--- a/testing/vm/tpm-builder.sh+++ b/testing/vm/tpm-builder.sh@@ -1,17 +1,20 @@#!/usr/bin/env bash# SPDX-License-Identifier: BSD-2-Clause# Copyright (c) 2026 Authors of shirudo-# testing/vm/tpm-builder.sh <state-dir> <names> <ek-ca> -- act as the builder-# of "Enroll from a builder's records": create the attestation key and the-# endorsement key in the vTPM state before the guest boots, with the commands-# that page gives, and write their names to <names>. As the vTPM's maker, it-# also stores an endorsement certificate from a CA of its own, in <ek-ca>.+# testing/vm/tpm-builder.sh <state-dir> <names> <ek-ca> <enroll> <node-id> --+# act as the builder of "Enroll from a builder's records": create the keys in+# the vTPM state before the guest boots, with the commands that page gives,+# write their names to <names> and the file jobs/enroll-put loads, naming+# the node <node-id>, to <enroll>. As the vTPM's maker, it also stores an+# endorsement certificate from a CA of its own, in <ek-ca>.set -euo pipefailstate=$1names=$2ek_ca=$3+enroll=$4+node_id=$5work=$(mktemp -d)# swtpm drops the lock on the state directory only when it exits, and the@@ -60,6 +63,9 @@ tpm2_readpublic -c ak.ctx -o ak.pub >/dev/nulltpm2_flushcontext -ttpm2_createek -c - -G ecc -u ek.pub >/dev/nullprintf 'ak-name %s\nek-name %s\n' "$(name ak.pub)" "$(name ek.pub)" >"$names"+# node_id is a VM name, which needs no JSON escape.+printf '{"source": "test-vm", "nodes": [{"ak_name": "%s", "ek_name": "%s", "node_id": "%s"}]}\n' \+ "$(name ak.pub)" "$(name ek.pub)" "$node_id" >"$enroll"# The certificate goes where a TPM's maker puts it, the ECC index of the TCG# EK credential profile, written under the platform hierarchy the firmware