Skip to content

Commit 5da711e

Browse files
authored
testcase for bgp container in container hardening (#8694)
Description of PR HLD implementation: Container Hardening (sonic-net/SONiC#1364) Dependency: sonic-net/sonic-buildimage#14932 #### What is the motivation for this PR? Check bgp container has access to /dev/sda* or /dev/vda* after limiting privileged flag to less Linux capabilities. #### How did you do it? #### How did you verify/test it? ``` container_hardening/test_container_hardening.py::test_bgp_dev PASSED [100%] ``` Signed-off-by: Mai Bui <[email protected]>
1 parent aab4a86 commit 5da711e

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pytest
2+
import logging
3+
from tests.common.helpers.assertions import pytest_assert
4+
5+
pytestmark = [
6+
pytest.mark.topology('any'),
7+
]
8+
9+
logger = logging.getLogger(__name__)
10+
11+
NO_PRIVILEGED_CONTAINERS = [
12+
'bgp',
13+
]
14+
15+
16+
def test_container_privileged(duthost):
17+
"""
18+
Test container without --privileged flag has no access to /dev/vda* or /dev/sda*
19+
"""
20+
for container_name in NO_PRIVILEGED_CONTAINERS:
21+
docker_exec_cmd = 'docker exec {} bash -c '.format(container_name)
22+
cmd = duthost.shell(docker_exec_cmd + "'df -h | grep /etc/hosts' | awk '{print $1}'")
23+
rc, device = cmd['rc'], cmd['stdout']
24+
pytest_assert(rc == 0, 'Failed to get the device name.')
25+
pytest_assert(device.startswith('/dev/'), 'Invalid device {}.'.format(device))
26+
output = duthost.shell(docker_exec_cmd + "'ls {}'".format(device), module_ignore_errors=True)['stdout']
27+
pytest_assert(not output, 'The partition {} exists.'.format(device))

tests/python3_test_files.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ console/test_console_loopback.py
3737
console/test_console_reversessh.py
3838
console/test_console_udevrule.py
3939
container_checker/test_container_checker.py
40+
container_hardening/test_container_hardening.py
4041
copp/test_copp.py
4142
crm/test_crm.py
4243
dash/test_dash_vnet.py

0 commit comments

Comments
 (0)