Skip to content

Commit cdfcfaf

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 cdfcfaf

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-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

+67-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ ci_cleanup() {
9797
fi
9898

9999
[ -f "$kata_config_backup" ] && sudo mv "$kata_config_backup" "$kata_config" || \
100-
sudo rm "$kata_config"
100+
sudo rm "$kata_config" || true
101101
}
102102

103103
create_containerd_config() {
@@ -431,6 +431,70 @@ EOF
431431
create_containerd_config "${containerd_runtime_test}"
432432
}
433433

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

0 commit comments

Comments
 (0)