Skip to content

Commit 75a49c2

Browse files
committed
fix: Updating regex validation for the Sysdig public API url
Signed-off-by: S3B4SZ17 <[email protected]>
1 parent 05dee46 commit 75a49c2

File tree

3 files changed

+74
-12
lines changed

3 files changed

+74
-12
lines changed

tests/config_test.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
Testing overall configuration of the MCP server.
3+
"""
4+
5+
6+
def test_api_url_format() -> None:
7+
"""
8+
Test that the API URL is formatted correctly.
9+
This test checks if the public API URL is constructed properly
10+
when the old API format is not used.
11+
"""
12+
from utils.sysdig.client_config import _get_public_api_url
13+
14+
# URL regions refer to https://docs.sysdig.com/en/administration/saas-regions-and-ip-ranges/
15+
region_urls = {
16+
"us1": {"url": "https://secure.sysdig.com", "public_url": "https://api.us1.sysdig.com"},
17+
"us2": {"url": "https://us2.app.sysdig.com", "public_url": "https://api.us2.sysdig.com"},
18+
"eu1": {"url": "https://eu1.app.sysdig.com", "public_url": "https://api.eu1.sysdig.com"},
19+
"au1": {"url": "https://app.au1.sysdig.com", "public_url": "https://api.au1.sysdig.com"},
20+
"me2": {"url": "https://app.me2.sysdig.com", "public_url": "https://api.me2.sysdig.com"},
21+
# Edge case that does not follow the standard pattern it should return the same passed URL
22+
"edge": {"url": "https://edge.something.com", "public_url": "https://edge.something.com"},
23+
}
24+
25+
# Check if the public API URL is formatted correctly
26+
public_api_us1 = _get_public_api_url(region_urls["us1"]["url"])
27+
public_api_us2 = _get_public_api_url(region_urls["us2"]["url"])
28+
public_api_eu1 = _get_public_api_url(region_urls["eu1"]["url"])
29+
public_api_au1 = _get_public_api_url(region_urls["au1"]["url"])
30+
public_api_me2 = _get_public_api_url(region_urls["me2"]["url"])
31+
public_api_edge = _get_public_api_url(region_urls["edge"]["url"])
32+
33+
assert public_api_us1 == region_urls["us1"]["public_url"], (
34+
f"Expected {region_urls['us1']['public_url']}, got {public_api_us1}"
35+
)
36+
assert public_api_us2 == region_urls["us2"]["public_url"], (
37+
f"Expected {region_urls['us2']['public_url']}, got {public_api_us2}"
38+
)
39+
assert public_api_eu1 == region_urls["eu1"]["public_url"], (
40+
f"Expected {region_urls['eu1']['public_url']}, got {public_api_eu1}"
41+
)
42+
assert public_api_au1 == region_urls["au1"]["public_url"], (
43+
f"Expected {region_urls['au1']['public_url']}, got {public_api_au1}"
44+
)
45+
assert public_api_me2 == region_urls["me2"]["public_url"], (
46+
f"Expected {region_urls['me2']['public_url']}, got {public_api_me2}"
47+
)
48+
assert public_api_edge == region_urls["edge"]["public_url"], (
49+
f"Expected {region_urls['edge']['public_url']}, got {public_api_edge}"
50+
)
51+
print("All public API URLs are formatted correctly.")

tests/events_feed_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ def test_get_event_info(mock_success_response: MagicMock | AsyncMock, mock_creds
2323
mock_success_response (MagicMock | AsyncMock): Mocked response object.
2424
mock_creds: Mocked credentials.
2525
"""
26-
26+
# Override the environment variable for MCP transport
27+
os.environ["MCP_TRANSPORT"] = "stdio"
2728
# Successful response
2829
mock_success_response.return_value.json.return_value = EVENT_INFO_RESPONSE
2930
mock_success_response.return_value.status_code = HTTPStatus.OK

utils/sysdig/client_config.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,30 @@ def get_api_env_vars() -> dict:
6767

6868
def _get_public_api_url(base_url: str) -> str:
6969
"""
70-
Get the public API URL from the base URL.
70+
Maps a Sysdig base URL to its corresponding public API URL.
71+
This function extracts the region from the base URL and constructs the public API URL in the format
72+
https://api.{region}.sysdig.com.
73+
74+
If the base URL does not match any known patterns, it returns the original base URL.
7175
7276
Args:
7377
base_url: The base URL of the Sysdig API
7478
7579
Returns:
76-
str: The public API URL in the format https://api.<region>.sysdig.com
80+
str: The public API URL in the format https://api.{region}.sysdig.com
7781
"""
78-
# Regex to capture the region pattern (like us2, us3, au1, etc.)
79-
# This assumes the region is a subdomain that starts with 2 lowercase letters and ends with a digit
80-
pattern = re.search(r"https://(?:(?P<region1>[a-z]{2}\d)\.app|app\.(?P<region2>[a-z]{2}\d))\.sysdig\.com", base_url)
81-
if pattern:
82-
region = pattern.group("region1") or pattern.group("region2") # Extract the region
83-
return f"https://api.{region}.sysdig.com"
84-
else:
85-
# Edge case for the secure API URL that is us1
86-
return "https://api.us1.sysdig.com"
82+
83+
patterns = [
84+
(r"^https://secure\.sysdig\.com$", lambda m: "us1"),
85+
(r"^https://([a-z]{2}\d)\.app\.sysdig\.com$", lambda m: m.group(1)),
86+
(r"^https://app\.([a-z]{2}\d)\.sysdig\.com$", lambda m: m.group(1)),
87+
]
88+
89+
for pattern, region_fn in patterns:
90+
match = re.match(pattern, base_url)
91+
if match:
92+
region = region_fn(match)
93+
return f"https://api.{region}.sysdig.com"
94+
95+
log.warning("A not recognized Sysdig URL was provided, returning the same URL. This may lead to unexpected behavior.")
96+
return base_url

0 commit comments

Comments
 (0)