Skip to content

Commit 5a9f1a7

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 5a9f1a7

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
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

+18
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,23 @@ function build_static_artifact_and_install() {
180181
popd >/dev/null
181182
}
182183

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

integration/containerd/cri/integration-tests.sh

+61
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,65 @@ EOF
431431
create_containerd_config "${containerd_runtime_test}"
432432
}
433433

434+
TestContainerGuestApparmor() {
435+
info "Test container guest AppArmor"
436+
437+
original_image=$(sudo sed -n 's/^image = \(.*\)/\1/p' ${kata_config})
438+
sudo sed -i "/image =/c image = "\"${KATA_APPARMOR_IMAGE}\""" "${kata_config}"
439+
sudo sed -i '/^disable_guest_apparmor/ s/true/false/g' "${kata_config}"
440+
sudo sed -i 's/^#\(debug_console_enabled\).*=.*$/\1 = true/g' "${kata_config}"
441+
442+
local container_yaml="${REPORT_DIR}/container.yaml"
443+
local image="busybox:latest"
444+
cat << EOF > "${container_yaml}"
445+
metadata:
446+
name: busybox-apparmor
447+
image:
448+
image: "$image"
449+
command:
450+
- top
451+
EOF
452+
453+
testContainerStart 1
454+
455+
info "check kata-runtime exec"
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 "/pause.*kata-default" || true)
467+
[ -n "$ret" ] || die "not found /pause kata-default profile"
468+
ret=$(echo "$aa_status" | grep "/bin/top.*kata-default" || true)
469+
[ -n "$ret" ] || die "not found /bin/top kata-default profile"
470+
471+
info "check crictl exec"
472+
sudo -E crictl exec $cid sleep 10 &
473+
474+
aa_status=$(expect -c "
475+
spawn -noecho kata-runtime exec $podid
476+
expect "root@localhost:/#"
477+
send \"aa-status\n\"
478+
expect "root@localhost:/#"
479+
send \"exit\n\"
480+
expect eof
481+
")
482+
echo "aa-status results:"
483+
echo "${aa_status}"
484+
ret=$(echo "$aa_status" | grep "/bin/sleep.*kata-default" || true)
485+
[ -n "$ret" ] || die "not found /bin/sleep kata-default profile"
486+
487+
testContainerStop
488+
489+
sudo sed -i '/^disable_guest_apparmor/ s/false/true/g' "${kata_config}"
490+
sudo sed -i "/image =/c image = "\"${original_image}\""" "$kata_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)