summaryrefslogtreecommitdiffhomepage
path: root/test/test_python_isolation.py
blob: c524aea0d7df517d79f34e9cd8bf263788eb2a96 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import os
import re
import subprocess
from pathlib import Path

import pytest
from unit.applications.lang.python import TestApplicationPython
from unit.option import option
from unit.utils import findmnt
from unit.utils import waitformount
from unit.utils import waitforunmount


class TestPythonIsolation(TestApplicationPython):
    prerequisites = {'modules': {'python': 'any'}, 'features': ['isolation']}

    def get_cgroup(self, app_name):
        output = subprocess.check_output(
            ['ps', 'ax', '-o', 'pid', '-o', 'cmd']
        ).decode()

        pid = re.search(
            fr'(\d+)\s*unit: "{app_name}" application', output
        ).group(1)

        cgroup = f'/proc/{pid}/cgroup'

        if not os.path.isfile(cgroup):
            pytest.skip(f'no cgroup at {cgroup}')

        with open(cgroup, 'r') as f:
            return f.read().rstrip()

    def test_python_isolation_rootfs(self, is_su, temp_dir):
        isolation_features = option.available['features']['isolation'].keys()

        if not is_su:
            if not 'unprivileged_userns_clone' in isolation_features:
                pytest.skip('requires unprivileged userns or root')

            if 'user' not in isolation_features:
                pytest.skip('user namespace is not supported')

            if 'mnt' not in isolation_features:
                pytest.skip('mnt namespace is not supported')

            if 'pid' not in isolation_features:
                pytest.skip('pid namespace is not supported')

        isolation = {'rootfs': temp_dir}

        if not is_su:
            isolation['namespaces'] = {
                'mount': True,
                'credential': True,
                'pid': True,
            }

        self.load('ns_inspect', isolation=isolation)

        assert (
            self.getjson(url=f'/?path={temp_dir}')['body']['FileExists']
            == False
        ), 'temp_dir does not exists in rootfs'

        assert (
            self.getjson(url='/?path=/proc/self')['body']['FileExists'] == True
        ), 'no /proc/self'

        assert (
            self.getjson(url='/?path=/dev/pts')['body']['FileExists'] == False
        ), 'no /dev/pts'

        assert (
            self.getjson(url='/?path=/sys/kernel')['body']['FileExists']
            == False
        ), 'no /sys/kernel'

        ret = self.getjson(url='/?path=/app/python/ns_inspect')

        assert ret['body']['FileExists'] == True, 'application exists in rootfs'

    def test_python_isolation_rootfs_no_language_deps(self, is_su, temp_dir):
        if not is_su:
            pytest.skip('requires root')

        isolation = {'rootfs': temp_dir, 'automount': {'language_deps': False}}
        self.load('empty', isolation=isolation)

        python_path = f'{temp_dir}/usr'

        assert findmnt().find(python_path) == -1
        assert self.get()['status'] != 200, 'disabled language_deps'
        assert findmnt().find(python_path) == -1

        isolation['automount']['language_deps'] = True

        self.load('empty', isolation=isolation)

        assert findmnt().find(python_path) == -1
        assert self.get()['status'] == 200, 'enabled language_deps'
        assert waitformount(python_path), 'language_deps mount'

        self.conf({"listeners": {}, "applications": {}})

        assert waitforunmount(python_path), 'language_deps unmount'

    def test_python_isolation_procfs(self, is_su, temp_dir):
        if not is_su:
            pytest.skip('requires root')

        isolation = {'rootfs': temp_dir, 'automount': {'procfs': False}}

        self.load('ns_inspect', isolation=isolation)

        assert (
            self.getjson(url='/?path=/proc/self')['body']['FileExists'] == False
        ), 'no /proc/self'

        isolation['automount']['procfs'] = True

        self.load('ns_inspect', isolation=isolation)

        assert (
            self.getjson(url='/?path=/proc/self')['body']['FileExists'] == True
        ), '/proc/self'

    def test_python_isolation_cgroup(self, is_su, temp_dir):
        if not is_su:
            pytest.skip('requires root')

        if not 'cgroup' in option.available['features']['isolation']:
            pytest.skip('cgroup is not supported')

        def set_cgroup_path(path):
            isolation = {'cgroup': {'path': path}}
            self.load('empty', processes=1, isolation=isolation)

        set_cgroup_path('scope/python')

        cgroup_rel = Path(self.get_cgroup('empty'))
        assert cgroup_rel.parts[-2:] == ('scope', 'python'), 'cgroup rel'

        set_cgroup_path('/scope2/python')

        cgroup_abs = Path(self.get_cgroup('empty'))
        assert cgroup_abs.parts[-2:] == ('scope2', 'python'), 'cgroup abs'

        assert len(cgroup_rel.parts) >= len(cgroup_abs.parts)

    def test_python_isolation_cgroup_two(self, is_su, temp_dir):
        if not is_su:
            pytest.skip('requires root')

        if not 'cgroup' in option.available['features']['isolation']:
            pytest.skip('cgroup is not supported')

        def set_two_cgroup_path(path, path2):
            script_path = f'{option.test_dir}/python/empty'

            assert 'success' in self.conf(
                {
                    "listeners": {
                        "*:7080": {"pass": "applications/one"},
                        "*:7081": {"pass": "applications/two"},
                    },
                    "applications": {
                        "one": {
                            "type": "python",
                            "processes": 1,
                            "path": script_path,
                            "working_directory": script_path,
                            "module": "wsgi",
                            "isolation": {
                                'cgroup': {'path': path},
                            },
                        },
                        "two": {
                            "type": "python",
                            "processes": 1,
                            "path": script_path,
                            "working_directory": script_path,
                            "module": "wsgi",
                            "isolation": {
                                'cgroup': {'path': path2},
                            },
                        },
                    },
                }
            )

        set_two_cgroup_path('/scope/python', '/scope/python')
        assert self.get_cgroup('one') == self.get_cgroup('two')

        set_two_cgroup_path('/scope/python', '/scope2/python')
        assert self.get_cgroup('one') != self.get_cgroup('two')

    def test_python_isolation_cgroup_invalid(self, is_su):
        if not is_su:
            pytest.skip('requires root')

        if not 'cgroup' in option.available['features']['isolation']:
            pytest.skip('cgroup is not supported')

        def check_invalid(path):
            script_path = f'{option.test_dir}/python/empty'
            assert 'error' in self.conf(
                {
                    "listeners": {"*:7080": {"pass": "applications/empty"}},
                    "applications": {
                        "empty": {
                            "type": "python",
                            "processes": {"spare": 0},
                            "path": script_path,
                            "working_directory": script_path,
                            "module": "wsgi",
                            "isolation": {
                                'cgroup': {'path': path},
                            },
                        }
                    },
                }
            )

        check_invalid('')
        check_invalid('../scope')
        check_invalid('scope/../python')