Skip to content
This repository was archived by the owner on Jun 28, 2024. It is now read-only.

Commit 69baf08

Browse files
Merge pull request #5543 from fitzthum/seves-test
Add SEV-ES Test
2 parents c66376c + b2f6e22 commit 69baf08

2 files changed

Lines changed: 67 additions & 35 deletions

File tree

integration/kubernetes/confidential/fixtures/service.yaml.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ spec:
2626
app: $NAME
2727
annotations:
2828
io.katacontainers.config.pre_attestation.uri: "$KBS_URI"
29+
io.katacontainers.config.sev.policy: "$POLICY"
2930
spec:
3031
runtimeClassName: $RUNTIMECLASS
3132
containers:

integration/kubernetes/confidential/sev.bats

Lines changed: 66 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ generate_service_yaml() {
5555
local name="${1}"
5656
local image="${2}"
5757

58+
# Default policy is 3:
59+
# - NODBG (1): Debugging of the guest is disallowed when set
60+
# - NOKS (2): Sharing keys with other guests is disallowed when set
61+
local policy="${3:-3}"
62+
5863
local kbs_ip="$(ip -o route get to 8.8.8.8 | sed -n 's/.*src \([0-9.]\+\).*/\1/p')"
5964
local service_yaml_template="${FIXTURES_DIR}/service.yaml.in"
6065

@@ -63,6 +68,7 @@ generate_service_yaml() {
6368

6469
NAME="${name}" IMAGE="${image}" RUNTIMECLASS="${RUNTIMECLASS}" \
6570
KBS_URI="${kbs_ip}:44444" \
71+
POLICY="$policy" \
6672
envsubst < "${service_yaml_template}" > "${service_yaml}"
6773
}
6874

@@ -143,15 +149,19 @@ delete_pods() {
143149
# Retrieve pod names
144150
local encrypted_pod_name=$(esudo kubectl get pod -o wide | grep encrypted-image-tests | awk '{print $1;}' || true)
145151
local unencrypted_pod_name=$(esudo kubectl get pod -o wide | grep unencrypted-image-tests | awk '{print $1;}' || true)
152+
local encrypted_pod_name_es=$(esudo kubectl get pod -o wide | grep encrypted-image-tests-es | awk '{print $1;}' || true)
146153

147154
# Delete both encrypted and unencrypted pods
148155
esudo kubectl delete -f \
149156
"${TEST_DIR}/unencrypted-image-tests.yaml" 2>/dev/null || true
150157
esudo kubectl delete -f \
151158
"${TEST_DIR}/encrypted-image-tests.yaml" 2>/dev/null || true
159+
esudo kubectl delete -f \
160+
"${TEST_DIR}/encrypted-image-tests-es.yaml" 2>/dev/null || true
152161

153162
[ -z "${encrypted_pod_name}" ] || (kubernetes_wait_for_pod_delete_state "${encrypted_pod_name}" || true)
154163
[ -z "${unencrypted_pod_name}" ] || (kubernetes_wait_for_pod_delete_state "${unencrypted_pod_name}" || true)
164+
[ -z "${encrypted_pod_name_es}" ] || (kubernetes_wait_for_pod_delete_state "${encrypted_pod_name_es}" || true)
155165
}
156166

157167
run_kbs() {
@@ -207,13 +217,11 @@ pull_unencrypted_image_and_set_keys() {
207217
}
208218

209219
generate_firmware_measurement_with_append() {
210-
local append_default="tsc=reliable no_timer_check rcupdate.rcu_expedited=1 \
211-
i8042.direct=1 i8042.dumbkbd=1 i8042.nopnp=1 i8042.noaux=1 noreplace-smp reboot=k \
212-
cryptomgr.notests net.ifnames=0 pci=lastbus=0 console=hvc0 console=hvc1 quiet panic=1 \
213-
nr_cpus=1 scsi_mod.scan=none agent.config_file=/etc/agent-config.toml"
214220

215221
# Gather firmware locations and kernel append for measurement
216-
local append=${1:-${append_default}}
222+
local append="${1}"
223+
local mode="${2:-sev}"
224+
local vcpu_sig=$(cpuid -1 --leaf 0x1 --raw | cut -s -f2 -d= | cut -f1 -d" ")
217225
local ovmf_path=$(grep "firmware = " $SEV_CONFIG | cut -d'"' -f2)
218226
local kernel_path="$(esudo /opt/confidential-containers/bin/kata-runtime \
219227
--config ${SEV_CONFIG} kata-env --json | jq -r .Kernel.Path)"
@@ -226,7 +234,11 @@ nr_cpus=1 scsi_mod.scan=none agent.config_file=/etc/agent-config.toml"
226234
[ -f "${initrd_path}" ] || return 1
227235

228236
# Generate digest from sev-snp-measure output - this also inserts measurement values inside OVMF image
229-
measurement=$(PATH="${PATH}:${HOME}/.local/bin" sev-snp-measure --mode=sev --output-format=base64 \
237+
measurement=$(PATH="${PATH}:${HOME}/.local/bin" sev-snp-measure \
238+
--mode="${mode}" \
239+
--vcpus=1 \
240+
--vcpu-sig="${vcpu_sig}" \
241+
--output-format=base64 \
230242
--ovmf="${ovmf_path}" \
231243
--kernel="${kernel_path}" \
232244
--initrd="${initrd_path}" \
@@ -236,40 +248,18 @@ nr_cpus=1 scsi_mod.scan=none agent.config_file=/etc/agent-config.toml"
236248
echo ${measurement}
237249
}
238250

239-
# KBS must be accessible from inside the guest, so update the config file
240-
# with the IP of the host
241-
update_kbs_uri() {
242-
243-
# If we have new config file, don't update the kernel params
244-
local legacy_kbs_config=$(cat "${SEV_CONFIG}" | grep "guest_pre_attestation_proxy" || true)
245-
if [ -z "${legacy_kbs_config}" ]; then
246-
return 0
247-
fi
248-
249-
local kbs_ip="$(ip -o route get to 8.8.8.8 | sed -n 's/.*src \([0-9.]\+\).*/\1/p')"
250-
local aa_kbc_params="agent.aa_kbc_params=online_sev_kbc::${kbs_ip}:44444"
251-
252-
# Only add the aa_kbc_params if it is not already set
253-
aa_kbc_params_set=$(cat "${SEV_CONFIG}" | grep "kernel_params" | grep "${aa_kbc_params}" || true)
254-
if [ -z "${aa_kbc_params_set}" ]; then
255-
esudo sed -i -e 's#^\(kernel_params\) = "\(.*\)"#\1 = "\2 '"${aa_kbc_params}"'"#g' "${SEV_CONFIG}"
256-
fi
257-
}
258-
259251
add_key_to_kbs_db() {
260252
measurement=${1}
261253

262254
# Add key and keyset to DB; If set, add policy with measurement to DB
263255
if [ -n "${measurement}" ]; then
264256
mysql -u${KBS_DB_USER} -p${KBS_DB_PW} -h ${KBS_DB_HOST} -D ${KBS_DB} <<EOF
265257
INSERT INTO secrets VALUES (10, 'key_id1', '${ENCRYPTION_KEY}', 10);
266-
INSERT INTO keysets VALUES (10, 'KEYSET-1', '["key_id1"]', 10);
267258
INSERT INTO policy VALUES (10, '["${measurement}"]', '[]', 0, 0, '[]', now(), NULL, 1);
268259
EOF
269260
else
270261
mysql -u${KBS_DB_USER} -p${KBS_DB_PW} -h ${KBS_DB_HOST} -D ${KBS_DB} <<EOF
271262
INSERT INTO secrets VALUES (10, 'key_id1', '${ENCRYPTION_KEY}', NULL);
272-
INSERT INTO keysets VALUES (10, 'KEYSET-1', '["key_id1"]', 10);
273263
EOF
274264
fi
275265
}
@@ -307,7 +297,8 @@ setup_file() {
307297
python3-pip \
308298
jq \
309299
mysql-client \
310-
docker-compose
300+
docker-compose \
301+
cpuid
311302
pip install sev-snp-measure
312303
"${TESTS_REPO_DIR}/.ci/install_yq.sh" >&2
313304

@@ -325,6 +316,12 @@ setup_file() {
325316
generate_service_yaml "unencrypted-image-tests" "${IMAGE_REPO}:unencrypted"
326317
generate_service_yaml "encrypted-image-tests" "${IMAGE_REPO}:encrypted"
327318

319+
# SEV-ES policy is 7:
320+
# - NODBG (1): Debugging of the guest is disallowed when set
321+
# - NOKS (2): Sharing keys with other guests is disallowed when set
322+
# - ES (4): SEV-ES is required when set
323+
generate_service_yaml "encrypted-image-tests-es" "${IMAGE_REPO}:encrypted" "7"
324+
328325
echo "SETUP FILE - COMPLETE"
329326
echo "###############################################################################"
330327
}
@@ -377,13 +374,8 @@ EOF
377374
# Make sure pre-attestation is enabled.
378375
esudo sed -i 's/guest_pre_attestation = false/guest_pre_attestation = true/g' ${SEV_CONFIG}
379376

380-
# Update kata config to point to KBS
381-
# This test expects an invalid measurement, but we still update
382-
# config so that the kernel params (which are saved) are correct
383-
update_kbs_uri
384-
385377
# Generate firmware measurement
386-
local append="INVALID INPUT"
378+
local append="INVALID-INPUT"
387379
measurement=$(generate_firmware_measurement_with_append ${append})
388380
echo "Firmware Measurement: ${measurement}"
389381

@@ -485,6 +477,45 @@ EOF
485477
fi
486478
}
487479

480+
@test "$test_tag Test SEV-ES encrypted container launch success with VALID measurement" {
481+
482+
# Generate firmware measurement
483+
local append=$(cat ${TEST_DIR}/guest-kernel-append)
484+
echo "Kernel Append: ${append}"
485+
measurement=$(generate_firmware_measurement_with_append "${append}" "seves")
486+
echo "Firmware Measurement: ${measurement}"
487+
488+
# Add key to KBS with policy measurement
489+
add_key_to_kbs_db ${measurement}
490+
491+
# Start the service/deployment/pod
492+
esudo kubectl apply -f "${TEST_DIR}/encrypted-image-tests-es.yaml"
493+
494+
# Retrieve pod name, wait for it to come up, retrieve pod ip
495+
pod_name=$(esudo kubectl get pod -o wide | grep encrypted-image-tests-es | awk '{print $1;}')
496+
kubernetes_wait_for_pod_ready_state "$pod_name" 20
497+
pod_ip=$(esudo kubectl get pod -o wide | grep encrypted-image-tests-es | awk '{print $6;}')
498+
499+
print_service_info
500+
501+
# Look for SEV-ES enabled in container dmesg output
502+
seves_enabled=$(ssh -i ${SSH_KEY_FILE} \
503+
-o "StrictHostKeyChecking no" \
504+
-o "PasswordAuthentication=no" \
505+
-t root@${pod_ip} \
506+
'dmesg | grep SEV-ES' || true)
507+
508+
if [ -z "$seves_enabled" ]; then
509+
>&2 echo -e "${RED}KATA CC TEST - FAIL: SEV-ES is NOT Enabled${NC}"
510+
return 1
511+
else
512+
echo "DMESG REPORT: $seves_enabled"
513+
echo -e "${GREEN}KATA CC TEST - PASS: SEV-ES is Enabled${NC}"
514+
fi
515+
}
516+
517+
518+
488519
teardown_file() {
489520
echo "###############################################################################"
490521
echo -e "TEARDOWN - STARTED\n"

0 commit comments

Comments
 (0)