|
1 | 1 | import logging
|
2 | 2 | import os
|
3 | 3 | from http import HTTPStatus
|
| 4 | +from threading import Thread |
4 | 5 | from time import sleep
|
5 | 6 |
|
6 | 7 | import pytest
|
| 8 | +import requests |
7 | 9 |
|
8 | 10 | from envs.monkey_zoo.blackbox.analyzers.communication_analyzer import CommunicationAnalyzer
|
9 | 11 | from envs.monkey_zoo.blackbox.analyzers.zerologon_analyzer import ZerologonAnalyzer
|
|
38 | 40 | start_machines,
|
39 | 41 | stop_machines,
|
40 | 42 | )
|
| 43 | +from monkey_island.cc.services.authentication_service.flask_resources.agent_otp import ( |
| 44 | + MAX_OTP_REQUESTS_PER_SECOND, |
| 45 | +) |
41 | 46 |
|
42 | 47 | DEFAULT_TIMEOUT_SECONDS = 2 * 60 + 30
|
43 | 48 | MACHINE_BOOTUP_WAIT_SECONDS = 30
|
|
48 | 53 |
|
49 | 54 | @pytest.fixture(autouse=True, scope="session")
|
50 | 55 | def GCPHandler(request, no_gcp, gcp_machines_to_start):
|
51 |
| - if not no_gcp: |
| 56 | + if no_gcp: |
| 57 | + return |
| 58 | + if len(gcp_machines_to_start) == 0: |
| 59 | + LOGGER.info("No GCP machines to start.") |
| 60 | + else: |
52 | 61 | LOGGER.info(f"MACHINES TO START: {gcp_machines_to_start}")
|
53 | 62 |
|
54 | 63 | try:
|
@@ -156,6 +165,27 @@ def test_logout_invalidates_all_tokens(island):
|
156 | 165 | assert resp.status_code == HTTPStatus.UNAUTHORIZED
|
157 | 166 |
|
158 | 167 |
|
| 168 | +def test_agent_otp_rate_limit(island): |
| 169 | + threads = [] |
| 170 | + response_codes = [] |
| 171 | + agent_otp_endpoint = f"https://{island}/api/agent-otp" |
| 172 | + |
| 173 | + def make_request(): |
| 174 | + response = requests.get(agent_otp_endpoint, verify=False) # noqa: DUO123 |
| 175 | + response_codes.append(response.status_code) |
| 176 | + |
| 177 | + for _ in range(0, MAX_OTP_REQUESTS_PER_SECOND + 1): |
| 178 | + t = Thread(target=make_request, daemon=True) |
| 179 | + t.start() |
| 180 | + threads.append(t) |
| 181 | + |
| 182 | + for t in threads: |
| 183 | + t.join() |
| 184 | + |
| 185 | + assert response_codes.count(HTTPStatus.OK) == MAX_OTP_REQUESTS_PER_SECOND |
| 186 | + assert response_codes.count(HTTPStatus.TOO_MANY_REQUESTS) == 1 |
| 187 | + |
| 188 | + |
159 | 189 | # NOTE: These test methods are ordered to give time for the slower zoo machines
|
160 | 190 | # to boot up and finish starting services.
|
161 | 191 | # noinspection PyUnresolvedReferences
|
|
0 commit comments