Skip to content

Commit 889fb3e

Browse files
authored
Merge pull request #1072 from kernelkit/container-remote-exec
Verify Host Command Execution from Container
2 parents 9629e2e + 2a6e7e5 commit 889fb3e

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

test/case/infix_containers/Readme.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ include::container_veth/Readme.adoc[]
2323
include::container_volume/Readme.adoc[]
2424

2525
include::container_firewall_basic/Readme.adoc[]
26+
27+
include::container_host_commands/Readme.adoc[]
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env python3
2+
r"""Host Command Execution from Container
3+
4+
This test verifies that a container running on Infix can execute commands
5+
that affect the host system. Specifically, it confirms that the container
6+
can change the hostname of the host.
7+
"""
8+
9+
import infamy
10+
from infamy.util import until, to_binary
11+
12+
with infamy.Test() as test:
13+
cont_image = f"oci-archive:{infamy.Container.NFTABLES_IMAGE}"
14+
cont_name = "cont0"
15+
hostname_init = "container-host"
16+
hostname_new = "coffee"
17+
18+
with test.step("Set up topology and attach to target DUT"):
19+
env = infamy.Env()
20+
target = env.attach("target", "mgmt")
21+
22+
if not target.has_model("infix-containers"):
23+
test.skip()
24+
25+
with test.step("Set initial hostname"):
26+
target.put_config_dict("ietf-system", {
27+
"system": {
28+
"hostname": hostname_init
29+
}
30+
})
31+
32+
with test.step("Verify initial hostname in operational"):
33+
oper = target.get_data("/ietf-system:system")
34+
name = oper["system"]["hostname"]
35+
36+
if name != hostname_init:
37+
print(f"Expected hostname: {hostname_init}, actual hostname: {name}")
38+
test.fail()
39+
40+
with test.step("Include script in OCI image to modify host hostname"):
41+
commands = to_binary(f"""#!/bin/sh
42+
nsenter -m/1/ns/mnt -u/1/ns/uts -i/1/ns/ipc -n/1/ns/net hostname {hostname_new}
43+
""")
44+
45+
target.put_config_dict("infix-containers", {
46+
"containers": {
47+
"container": [
48+
{
49+
"name": cont_name,
50+
"image": cont_image,
51+
"network": {
52+
"host": True
53+
},
54+
"mount": [
55+
{
56+
"name": "rc.local",
57+
"content": commands,
58+
"target": "/etc/rc.local",
59+
"mode": "0755"
60+
},
61+
{
62+
"name": "proc1ns",
63+
"source": "/proc/1/ns",
64+
"target": "/1/ns",
65+
}
66+
],
67+
"privileged": True
68+
}
69+
]
70+
}
71+
})
72+
73+
with test.step("Verify container has started"):
74+
c = infamy.Container(target)
75+
until(lambda: c.running(cont_name), attempts=10)
76+
77+
with test.step("Verify the new hostname set by the container"):
78+
oper = target.get_data("/ietf-system:system")
79+
name = oper["system"]["hostname"]
80+
81+
if name != hostname_new:
82+
print(f"Expected hostname: {hostname_new}, actual hostname: {name}")
83+
test.fail()
84+
85+
test.succeed()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../infamy/topologies/1x1.dot

test/case/infix_containers/infix_containers.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@
1818
- name: container_firewall_basic
1919
case: container_firewall_basic/test.py
2020

21+
- name: container_host_commands
22+
case: container_host_commands/test.py
23+

0 commit comments

Comments
 (0)