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

Commit add2a28

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 add2a28

File tree

3 files changed

+85
-0
lines changed

3 files changed

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

+63
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,67 @@ 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+
# sleep for 1s to make sure the exec process started.
474+
sleep 1
475+
476+
aa_status=$(expect -c "
477+
spawn -noecho kata-runtime exec $podid
478+
expect "root@localhost:/#"
479+
send \"aa-status\n\"
480+
expect "root@localhost:/#"
481+
send \"exit\n\"
482+
expect eof
483+
")
484+
echo "aa-status results:"
485+
echo "${aa_status}"
486+
ret=$(echo "$aa_status" | grep "/bin/sleep.*kata-default" || true)
487+
[ -n "$ret" ] || die "not found /bin/sleep kata-default profile"
488+
489+
testContainerStop
490+
491+
sudo sed -i '/^disable_guest_apparmor/ s/false/true/g' "${kata_config}"
492+
sudo sed -i "/image =/c image = "\"${original_image}\""" "$kata_config"
493+
}
494+
434495
# k8s may restart docker which will impact on containerd stop
435496
stop_containerd() {
436497
local tmp=$(pgrep kubelet || true)
@@ -509,6 +570,8 @@ main() {
509570
TestContainerMemoryUpdate 0
510571
fi
511572
573+
TestContainerGuestApparmor
574+
512575
TestKilledVmmCleanup
513576
514577
popd

0 commit comments

Comments
 (0)