Skip to content

Commit 8c32b1e

Browse files
wip: testing that content was actually propagated inside the microvm
1 parent f99defc commit 8c32b1e

File tree

2 files changed

+63
-42
lines changed

2 files changed

+63
-42
lines changed

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,4 @@ test_results/*
1414
*.bin
1515
/resources/linux
1616
/resources/x86_64
17-
/resources/aarch64
18-
.venv
17+
/resources/aarch64

tests/integration_tests/security/test_jail.py

+62-40
Original file line numberDiff line numberDiff line change
@@ -666,74 +666,96 @@ def test_cgroupsv2_written_only_once(uvm_plain, cgroups_info):
666666
assert len(mkdir_lines) == 1
667667

668668

669-
def test_mount_proagation_to_root(uvm_plain, tmp_path, guest_kernel, rootfs_rw):
669+
def test_jail_mount(uvm_plain, guest_kernel, rootfs_rw):
670670
"""
671671
Test that the jailer mounts are propagated to the root mount namespace.
672-
673-
This is a test for
674-
https://github.com/firecracker-microvm/firecracker/pull/#1093
675672
"""
676-
673+
# setup the microvm
677674
test_microvm = uvm_plain
678675

676+
chroot_base = test_microvm.jailer.chroot_base / str(test_microvm.id)[:8]
679677
# make a directory to hold the original content
680-
original_content_dir = tmp_path / "original"
681-
original_content_dir.mkdir(parents=True)
678+
original_content_dir = chroot_base / "original_content"
679+
original_content_dir.mkdir(parents=True, exist_ok=True)
682680

683681
# make a directory to hold the jailed content
684-
jailed_content_dir = tmp_path / "firecracker" / "testbindmount" / "root"
685-
jailed_content_dir.mkdir(parents=True)
686-
687-
test_microvm.jailer.jailer_id = "testbindmount"
688-
test_microvm.jailer.chroot_base = tmp_path
689-
test_microvm.jailer.daemonize = True
690-
test_microvm.jailer.gid = 0
691-
test_microvm.jailer.uid = 0
692-
test_microvm.extra_args = {"seccomp-level": 0}
682+
jailed_content_dir = chroot_base / "firecracker" / "testbindmount" / "root"
683+
jailed_content_dir.mkdir(parents=True, exist_ok=True)
684+
jailed_content_dir_ssh = jailed_content_dir / ".ssh"
685+
jailed_content_dir_ssh.mkdir(parents=True, exist_ok=True)
693686

694687
# assert that the directory was created
688+
assert original_content_dir.exists()
695689
assert jailed_content_dir.exists()
690+
assert jailed_content_dir_ssh.exists()
696691

697-
# Create the guest kernel and rootfs in the jailed content directory
698-
# and mount them in the jailed content directory
699-
os.system(f"cp {guest_kernel} {original_content_dir}")
700-
os.system(f"cp {rootfs_rw} {original_content_dir}")
701-
guest_kernel_mount_path = jailed_content_dir / os.path.basename(guest_kernel)
702-
rootfs_mount_path = jailed_content_dir / os.path.basename(rootfs_rw)
703-
guest_kernel_mount_path.touch()
704-
rootfs_mount_path.touch()
692+
# add the ssh key to the jailed content dir so we can ssh into the microvm
693+
pub_key_contents = Path("/srv/img/x86_64/id_rsa.pub").read_text()
694+
ssh_key = jailed_content_dir_ssh / "authorized_keys"
695+
ssh_key.write_text(pub_key_contents)
705696

706-
# assert that the files were created
707-
assert guest_kernel_mount_path.exists()
708-
assert rootfs_mount_path.exists()
697+
# create the files that will be mounted
698+
test_data = original_content_dir / "test_data"
699+
test_data.touch()
700+
assert test_data.exists()
701+
test_data.write_text("test_data")
702+
assert test_data.read_text() == "test_data"
709703

710-
# mount the rootfs
704+
os.system(f"cp {guest_kernel} {original_content_dir}")
705+
os.system(f"cp {rootfs_rw} {original_content_dir}")
706+
assert (original_content_dir / guest_kernel.name).exists()
707+
assert (original_content_dir / rootfs_rw.name).exists()
708+
709+
jailed_test_data = jailed_content_dir / "test_data"
710+
jailed_test_data.touch()
711+
assert jailed_test_data.exists()
712+
assert jailed_test_data.read_text() == ""
713+
jailed_kernel = jailed_content_dir / guest_kernel.name
714+
jailed_rootfs = jailed_content_dir / rootfs_rw.name
715+
jailed_kernel.touch()
716+
jailed_rootfs.touch()
717+
assert (jailed_content_dir / guest_kernel.name).exists()
718+
assert (jailed_content_dir / rootfs_rw.name).exists()
719+
720+
pid_file = jailed_content_dir / "firecracker.pid"
721+
pid_file.touch()
722+
723+
# mount the data
724+
subprocess.run(
725+
["mount", "--bind", test_data, jailed_test_data],
726+
check=True,
727+
)
711728
subprocess.run(
712729
[
713730
"mount",
714731
"--bind",
715-
original_content_dir / os.path.basename(guest_kernel),
716-
guest_kernel_mount_path,
732+
original_content_dir / guest_kernel.name,
733+
jailed_content_dir / guest_kernel.name,
717734
],
718735
check=True,
719736
)
720737
subprocess.run(
721738
[
722739
"mount",
723740
"--bind",
724-
original_content_dir / os.path.basename(rootfs_rw),
725-
rootfs_mount_path,
741+
original_content_dir / rootfs_rw.name,
742+
jailed_content_dir / rootfs_rw.name,
726743
],
727744
check=True,
728745
)
729746

730-
# assert that the mounts are present
731-
assert guest_kernel_mount_path.exists()
732-
assert rootfs_mount_path.exists()
733-
734-
# run
747+
# spawn the microvm
735748
test_microvm.spawn()
749+
test_microvm.basic_config()
750+
# set params for the microvm
751+
test_microvm.kernel_file = str(jailed_content_dir / guest_kernel.name)
752+
test_microvm.rootfs_file = str(jailed_content_dir / rootfs_rw.name)
753+
test_microvm.jailer.chroot_base = chroot_base
754+
test_microvm.jailer.jailer_id = "testbindmount"
755+
test_microvm.add_net_iface()
756+
test_microvm.start()
736757

737-
# assert that the mounts are present
738-
assert guest_kernel_mount_path.exists()
739-
assert rootfs_mount_path.exists()
758+
# and assert the content is there in the microvm
759+
_, stdout, stderr = test_microvm.ssh.run("cat /root/test_data")
760+
assert stdout == "test_data"
761+
assert stderr == ""

0 commit comments

Comments
 (0)