|
| 1 | +import multiprocessing as mp |
| 2 | +import os |
| 3 | + |
| 4 | +from event import Event, EventType, Process |
| 5 | + |
| 6 | + |
| 7 | +def test_chmod(fact, monitored_dir, server): |
| 8 | + """ |
| 9 | + Tests changing permissions on a file and verifies the corresponding |
| 10 | + event is captured by the server |
| 11 | +
|
| 12 | + Args: |
| 13 | + fact: Fixture for file activity (only required to be runing). |
| 14 | + monitored_dir: Temporary directory path for creating the test file. |
| 15 | + server: The server instance to communicate with. |
| 16 | + """ |
| 17 | + # File Under Test |
| 18 | + fut = os.path.join(monitored_dir, 'test.txt') |
| 19 | + mode = 0o666 |
| 20 | + os.chmod(fut, mode) |
| 21 | + |
| 22 | + e = Event(process=Process.from_proc(), event_type=EventType.PERMISSION, |
| 23 | + file=fut, host_path=fut, mode=mode) |
| 24 | + |
| 25 | + print(f'Waiting for event: {e}') |
| 26 | + |
| 27 | + server.wait_events([e]) |
| 28 | + |
| 29 | + |
| 30 | +def test_multiple(fact, monitored_dir, server): |
| 31 | + """ |
| 32 | + Tests modifying permissions on multiple files. |
| 33 | +
|
| 34 | + Args: |
| 35 | + fact: Fixture for file activity (only required to be runing). |
| 36 | + monitored_dir: Temporary directory path for creating the test file. |
| 37 | + server: The server instance to communicate with. |
| 38 | + """ |
| 39 | + events = [] |
| 40 | + process = Process.from_proc() |
| 41 | + mode = 0o646 |
| 42 | + |
| 43 | + for i in range(3): |
| 44 | + fut = os.path.join(monitored_dir, f'{i}.txt') |
| 45 | + with open(fut, 'w') as f: |
| 46 | + f.write('This is a test') |
| 47 | + os.chmod(fut, mode) |
| 48 | + |
| 49 | + events.extend([ |
| 50 | + Event(process=process, event_type=EventType.CREATION, |
| 51 | + file=fut, host_path=''), |
| 52 | + Event(process=process, event_type=EventType.PERMISSION, |
| 53 | + file=fut, host_path='', mode=mode), |
| 54 | + ]) |
| 55 | + |
| 56 | + server.wait_events(events) |
| 57 | + |
| 58 | + |
| 59 | +def test_ignored(fact, test_file, ignored_dir, server): |
| 60 | + """ |
| 61 | + Tests that permission events on ignored files are not captured. |
| 62 | +
|
| 63 | + Args: |
| 64 | + fact: Fixture for file activity (only required to be running). |
| 65 | + monitored_dir: Temporary directory path for creating the test file. |
| 66 | + ignored_dir: Temporary directory path that is not monitored by fact. |
| 67 | + server: The server instance to communicate with. |
| 68 | + """ |
| 69 | + process = Process.from_proc() |
| 70 | + mode = 0o666 |
| 71 | + |
| 72 | + # Ignored file, must not show up in the server |
| 73 | + ignored_file = os.path.join(ignored_dir, 'test.txt') |
| 74 | + with open(ignored_file, 'w') as f: |
| 75 | + f.write('This is to be ignored') |
| 76 | + os.chmod(ignored_file, mode) |
| 77 | + |
| 78 | + ignored_event = Event(process=process, event_type=EventType.PERMISSION, |
| 79 | + file=ignored_file, host_path='', mode=mode) |
| 80 | + print(f'Ignoring: {ignored_event}') |
| 81 | + |
| 82 | + # File Under Test |
| 83 | + os.chmod(test_file, mode) |
| 84 | + |
| 85 | + e = Event(process=process, event_type=EventType.PERMISSION, |
| 86 | + file=test_file, host_path=test_file, mode=mode) |
| 87 | + print(f'Waiting foor event: {e}') |
| 88 | + |
| 89 | + server.wait_events([e], ignored=[ignored_event]) |
| 90 | + |
| 91 | + |
| 92 | +def do_test(fut: str, mode: int, stop_event: mp.Event): |
| 93 | + with open(fut, 'w') as f: |
| 94 | + f.write('This is a test') |
| 95 | + os.chmod(fut, mode) |
| 96 | + |
| 97 | + # Wait for test to be done |
| 98 | + stop_event.wait() |
| 99 | + |
| 100 | + |
| 101 | +def test_external_process(fact, monitored_dir, server): |
| 102 | + """ |
| 103 | + Tests permission change of a file by an external process and |
| 104 | + verifies that the corresponding event is captured by the server. |
| 105 | +
|
| 106 | + Args: |
| 107 | + fact: Fixture for file activity (only required to be running). |
| 108 | + monitored_dir: Temporary directory path for creating the test file. |
| 109 | + server: The server instance to communicate with. |
| 110 | + """ |
| 111 | + # File Under Test |
| 112 | + fut = os.path.join(monitored_dir, 'test2.txt') |
| 113 | + mode = 0o666 |
| 114 | + stop_event = mp.Event() |
| 115 | + proc = mp.Process(target=do_test, args=(fut, mode, stop_event)) |
| 116 | + proc.start() |
| 117 | + process = Process.from_proc(proc.pid) |
| 118 | + |
| 119 | + event = Event(process=process, event_type=EventType.PERMISSION, |
| 120 | + file=fut, host_path='', mode=mode) |
| 121 | + print(f'Waiting for event: {event}') |
| 122 | + |
| 123 | + try: |
| 124 | + server.wait_events([event]) |
| 125 | + finally: |
| 126 | + stop_event.set() |
| 127 | + proc.join(1) |
| 128 | + |
| 129 | + |
| 130 | +def test_overlay(fact, test_container, server): |
| 131 | + """ |
| 132 | + Test permission changes on an overlayfs file (inside a container) |
| 133 | +
|
| 134 | + Args: |
| 135 | + fact: Fixture for file activity (only required to be running). |
| 136 | + test_container: A container for running commands in. |
| 137 | + server: The server instance to communicate with. |
| 138 | + """ |
| 139 | + # File Under Test |
| 140 | + fut = '/container-dir/test.txt' |
| 141 | + mode = '666' |
| 142 | + |
| 143 | + # Create the exec and an equivalent event that it will trigger |
| 144 | + test_container.exec_run(f'touch {fut}') |
| 145 | + test_container.exec_run(f'chmod {mode} {fut}') |
| 146 | + |
| 147 | + loginuid = pow(2, 32)-1 |
| 148 | + touch = Process(pid=None, |
| 149 | + uid=0, |
| 150 | + gid=0, |
| 151 | + exe_path='/usr/bin/touch', |
| 152 | + args=f'touch {fut}', |
| 153 | + name='touch', |
| 154 | + container_id=test_container.id[:12], |
| 155 | + loginuid=loginuid) |
| 156 | + chmod = Process(pid=None, |
| 157 | + uid=0, |
| 158 | + gid=0, |
| 159 | + exe_path='/usr/bin/chmod', |
| 160 | + args=f'chmod {mode} {fut}', |
| 161 | + name='chmod', |
| 162 | + container_id=test_container.id[:12], |
| 163 | + loginuid=loginuid) |
| 164 | + events = [ |
| 165 | + Event(process=touch, event_type=EventType.CREATION, |
| 166 | + file=fut, host_path=''), |
| 167 | + Event(process=touch, event_type=EventType.OPEN, |
| 168 | + file=fut, host_path=''), |
| 169 | + Event(process=chmod, event_type=EventType.PERMISSION, |
| 170 | + file=fut, host_path='', mode=int(mode, 8)), |
| 171 | + ] |
| 172 | + |
| 173 | + for e in events: |
| 174 | + print(f'Waiting for event: {e}') |
| 175 | + |
| 176 | + server.wait_events(events) |
| 177 | + |
| 178 | + |
| 179 | +def test_mounted_dir(fact, test_container, ignored_dir, server): |
| 180 | + """ |
| 181 | + Test permission changes on a file bind mounted into a container |
| 182 | +
|
| 183 | + Args: |
| 184 | + fact: Fixture for file activity (only required to be running). |
| 185 | + test_container: A container for running commands in. |
| 186 | + ignored_dir: This directory is ignored on the host, and mounted to the container. |
| 187 | + server: The server instance to communicate with. |
| 188 | + """ |
| 189 | + # File Under Test |
| 190 | + fut = '/mounted/test.txt' |
| 191 | + mode = '666' |
| 192 | + |
| 193 | + # Create the exec and an equivalent event that it will trigger |
| 194 | + test_container.exec_run(f'touch {fut}') |
| 195 | + test_container.exec_run(f'chmod {mode} {fut}') |
| 196 | + |
| 197 | + loginuid = pow(2, 32)-1 |
| 198 | + touch = Process(pid=None, |
| 199 | + uid=0, |
| 200 | + gid=0, |
| 201 | + exe_path='/usr/bin/touch', |
| 202 | + args=f'touch {fut}', |
| 203 | + name='touch', |
| 204 | + container_id=test_container.id[:12], |
| 205 | + loginuid=loginuid) |
| 206 | + chmod = Process(pid=None, |
| 207 | + uid=0, |
| 208 | + gid=0, |
| 209 | + exe_path='/usr/bin/chmod', |
| 210 | + args=f'chmod {mode} {fut}', |
| 211 | + name='chmod', |
| 212 | + container_id=test_container.id[:12], |
| 213 | + loginuid=loginuid) |
| 214 | + events = [ |
| 215 | + Event(process=touch, event_type=EventType.CREATION, file=fut, |
| 216 | + host_path=''), |
| 217 | + Event(process=chmod, event_type=EventType.PERMISSION, file=fut, |
| 218 | + host_path='', mode=int(mode, 8)), |
| 219 | + ] |
| 220 | + |
| 221 | + for e in events: |
| 222 | + print(f'Waiting for event: {e}') |
| 223 | + |
| 224 | + server.wait_events(events) |
| 225 | + |
| 226 | + |
| 227 | +def test_unmonitored_mounted_dir(fact, test_container, test_file, server): |
| 228 | + """ |
| 229 | + Test permission changes on a file bind mounted to a container and |
| 230 | + monitored on the host. |
| 231 | +
|
| 232 | + Args: |
| 233 | + fact: Fixture for file activity (only required to be running). |
| 234 | + test_container: A container for running commands in. |
| 235 | + test_file: File monitored on the host, mounted to the container. |
| 236 | + server: The server instance to communicate with. |
| 237 | + """ |
| 238 | + # File Under Test |
| 239 | + fut = '/unmonitored/test.txt' |
| 240 | + mode = '666' |
| 241 | + |
| 242 | + # Create the exec and an equivalent event that it will trigger |
| 243 | + test_container.exec_run(f'chmod {mode} {fut}') |
| 244 | + |
| 245 | + process = Process(pid=None, |
| 246 | + uid=0, |
| 247 | + gid=0, |
| 248 | + exe_path='/usr/bin/chmod', |
| 249 | + args=f'chmod {mode} {fut}', |
| 250 | + name='chmod', |
| 251 | + container_id=test_container.id[:12], |
| 252 | + loginuid=pow(2, 32)-1) |
| 253 | + event = Event(process=process, event_type=EventType.PERMISSION, |
| 254 | + file=fut, host_path=test_file, mode=int(mode, 8)) |
| 255 | + print(f'Waiting for event: {event}') |
| 256 | + |
| 257 | + server.wait_events([event]) |
0 commit comments