Skip to content

Commit 542a587

Browse files
committed
test:cri: Add guest AppArmor support
Add a test case which check whether AppArmor inside the guest works properly using containerd. The test creates a container configured to apply the `kata-default` profile, then it checks the container process is running with the profile enforced. Fixes: kata-containers#5748 Depends-on: github.com/kata-containers/kata-containers#7587 Signed-off-by: Manabu Sugimoto <[email protected]>
1 parent de2c828 commit 542a587

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

.ci/install_kata_image.sh

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ source "${cidir}/lib.sh"
1616
main() {
1717
build_static_artifact_and_install "rootfs-image"
1818
build_static_artifact_and_install "rootfs-initrd"
19+
20+
# Build and install an image for the guest AppArmor
21+
build_install_apparmor_image
1922
}
2023

2124
main

.ci/lib.sh

+19
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ fi
1717
export KATA_KSM_THROTTLER=${KATA_KSM_THROTTLER:-no}
1818
export KATA_QEMU_DESTDIR=${KATA_QEMU_DESTDIR:-"/usr"}
1919
export KATA_ETC_CONFIG_PATH="/etc/kata-containers/configuration.toml"
20+
export KATA_APPARMOR_IMAGE="/opt/kata/share/kata-containers/kata-containers-apparmor.img"
2021

2122
export katacontainers_repo=${katacontainers_repo:="github.com/kata-containers/kata-containers"}
2223
export katacontainers_repo_git="https://${katacontainers_repo}.git"
@@ -180,6 +181,24 @@ function build_static_artifact_and_install() {
180181
popd >/dev/null
181182
}
182183

184+
build_install_apparmor_image() {
185+
USE_DOCKER=${USE_DOCKER:-"true"}
186+
187+
info "Build AppArmor guest image"
188+
local rootfs_builder_dir="${katacontainers_repo_dir}/tools/osbuilder/rootfs-builder"
189+
local rootfs_dir="${rootfs_builder_dir}/rootfs-apparmor"
190+
pushd "$rootfs_builder_dir" >/dev/null
191+
sudo -E AGENT_INIT=no APPARMOR=yes USE_DOCKER="${USE_DOCKER}" ./rootfs.sh -r "${rootfs_dir}" ubuntu
192+
popd >/dev/null
193+
194+
info "Install AppArmor guest image"
195+
local image_builder_dir="${katacontainers_repo_dir}/tools/osbuilder/image-builder"
196+
pushd "${image_builder_dir}" >/dev/null
197+
sudo -E AGENT_INIT=no USE_DOCKER="${USE_DOCKER}" ./image_builder.sh "${rootfs_dir}"
198+
popd >/dev/null
199+
sudo install -o root -g root -m 0640 -D "${image_builder_dir}/kata-containers.img" "${KATA_APPARMOR_IMAGE}"
200+
}
201+
183202
function get_dep_from_yaml_db(){
184203
local versions_file="$1"
185204
local dependency="$2"

integration/containerd/cri/integration-tests.sh

+73-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ source "${SCRIPT_PATH}/../../../.ci/lib.sh"
1818
# runc is installed in /usr/local/sbin/ add that path
1919
export PATH="$PATH:/usr/local/sbin"
2020

21+
TEST_INITRD="${TEST_INITRD:-no}"
22+
2123
containerd_tarball_version=$(get_version "externals.containerd.version")
2224

2325
# Runtime to be used for testing
@@ -97,7 +99,7 @@ ci_cleanup() {
9799
fi
98100

99101
[ -f "$kata_config_backup" ] && sudo mv "$kata_config_backup" "$kata_config" || \
100-
sudo rm "$kata_config"
102+
sudo rm "$kata_config" || true
101103
}
102104

103105
create_containerd_config() {
@@ -431,6 +433,74 @@ EOF
431433
create_containerd_config "${containerd_runtime_test}"
432434
}
433435

436+
TestContainerGuestApparmor() {
437+
info "Test container guest AppArmor"
438+
439+
if [[ "${TEST_INITRD}" == "yes" ]]; then
440+
info "Skip the test because the guest AppArmor doesn't work with the agent init"
441+
return
442+
fi
443+
if [ ! -e "${KATA_APPARMOR_IMAGE}" ]; then
444+
info "Skip the test becasue the guest AppArmor image doesn't exist"
445+
return
446+
fi
447+
448+
# Set the guest AppArmor rootfs image because the guest AppArmor doesn't work with the agent init.
449+
sudo sed -i "/^image =/c image = "\"${KATA_APPARMOR_IMAGE}\""" "${kata_config}"
450+
# Enable the guest AppArmor.
451+
sudo sed -i '/^disable_guest_apparmor/ s/true/false/g' "${kata_config}"
452+
sudo sed -i 's/^#\(debug_console_enabled\).*=.*$/\1 = true/g' "${kata_config}"
453+
454+
local container_yaml="${REPORT_DIR}/container.yaml"
455+
local image="busybox:latest"
456+
cat << EOF > "${container_yaml}"
457+
metadata:
458+
name: busybox-apparmor
459+
image:
460+
image: "$image"
461+
command:
462+
- top
463+
EOF
464+
465+
info "Check the AppArmor profile is applied to the container executed by crictl start"
466+
testContainerStart 1
467+
aa_status=$(expect -c "
468+
spawn -noecho kata-runtime exec $podid
469+
expect "root@localhost:/#"
470+
send \"aa-status\n\"
471+
expect "root@localhost:/#"
472+
send \"exit\n\"
473+
expect eof
474+
")
475+
echo "aa-status results:"
476+
echo "${aa_status}"
477+
ret=$(echo "$aa_status" | grep "/bin/top.*kata-default" || true)
478+
[ -n "$ret" ] || die "not found /bin/top kata-default profile"
479+
480+
info "Check the AppArmor profile is applied to the process executed by crictl exec"
481+
sudo -E crictl exec $cid sleep 10 &
482+
# sleep for 1s to make sure the exec process started.
483+
sleep 1
484+
aa_status=$(expect -c "
485+
spawn -noecho kata-runtime exec $podid
486+
expect "root@localhost:/#"
487+
send \"aa-status\n\"
488+
expect "root@localhost:/#"
489+
send \"exit\n\"
490+
expect eof
491+
")
492+
echo "aa-status results:"
493+
echo "${aa_status}"
494+
ret=$(echo "$aa_status" | grep "/bin/sleep.*kata-default" || true)
495+
[ -n "$ret" ] || die "not found /bin/sleep kata-default profile"
496+
497+
testContainerStop
498+
499+
# Reset the Kata configuration file.
500+
sudo rm "${kata_config}"
501+
ci_config
502+
}
503+
434504
# k8s may restart docker which will impact on containerd stop
435505
stop_containerd() {
436506
local tmp=$(pgrep kubelet || true)
@@ -509,6 +579,8 @@ main() {
509579
TestContainerMemoryUpdate 0
510580
fi
511581
582+
TestContainerGuestApparmor
583+
512584
TestKilledVmmCleanup
513585
514586
popd

0 commit comments

Comments
 (0)