Skip to content

Commit b34f0b8

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 b34f0b8

File tree

3 files changed

+84
-1
lines changed

3 files changed

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

+62-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,65 @@ EOF
431431
create_containerd_config "${containerd_runtime_test}"
432432
}
433433

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

0 commit comments

Comments
 (0)