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

Commit 2474c08

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: #5748 Depends-on: github.com/kata-containers/kata-containers#7587 Signed-off-by: Manabu Sugimoto <[email protected]>
1 parent de2c828 commit 2474c08

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

Diff for: .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

Diff for: .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"

Diff for: integration/containerd/cri/integration-tests.sh

+74-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,75 @@ EOF
431433
create_containerd_config "${containerd_runtime_test}"
432434
}
433435

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

0 commit comments

Comments
 (0)