Skip to content

Commit 774f6d3

Browse files
author
S3B4SZ17
committed
feat: Updating logic to conditionally the public API url from the yaml config if no regex match
Signed-off-by: S3B4SZ17 <[email protected]>
1 parent 3ad2414 commit 774f6d3

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

tests/config_test.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,5 @@ def test_api_url_format() -> None:
4545
assert public_api_me2 == region_urls["me2"]["public_url"], (
4646
f"Expected {region_urls['me2']['public_url']}, got {public_api_me2}"
4747
)
48-
assert public_api_edge == region_urls["edge"]["public_url"], (
49-
f"Expected {region_urls['edge']['public_url']}, got {public_api_edge}"
50-
)
48+
assert not public_api_edge, f"Expected empty string, got {public_api_edge}"
5149
print("All public API URLs are formatted correctly.")

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ def mock_creds():
4343
Fixture to set up mocked credentials.
4444
"""
4545
os.environ["SYSDIG_SECURE_TOKEN"] = "mocked_token"
46-
os.environ["SYSDIG_HOST"] = "https://mocked.secure"
46+
os.environ["SYSDIG_HOST"] = "https://us2.app.sysdig.com"

utils/sysdig/client_config.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@
88
import re
99
from typing import Optional
1010

11+
# Application config loader
12+
from utils.app_config import get_app_config
13+
1114
# Set up logging
1215
logging.basicConfig(format="%(asctime)s-%(process)d-%(levelname)s- %(message)s", level=os.environ.get("LOGLEVEL", "ERROR"))
1316
log = logging.getLogger(__name__)
1417

18+
app_config = get_app_config()
19+
1520

1621
# Lazy-load the Sysdig client configuration
1722
def get_configuration(
@@ -22,18 +27,36 @@ def get_configuration(
2227
2328
Args:
2429
token (str): The Sysdig Secure token.
25-
sysdig_host_url (str): The base URL of the Sysdig API.
26-
old_api (bool): If True, uses the old Sysdig API URL format. Defaults to False.
30+
sysdig_host_url (str): The base URL of the Sysdig API,
31+
refer to the docs https://docs.sysdig.com/en/administration/saas-regions-and-ip-ranges/#sysdig-platform-regions.
32+
old_api (bool): If True, uses the old Sysdig API URL format.
33+
Defaults to False using the public API URL format https://api.{region}.sysdig.com.
2734
Returns:
2835
sysdig_client.Configuration: A configured Sysdig client instance.
36+
Raises:
37+
ValueError: If the Sysdig host URL is not provided or is invalid.
2938
"""
3039
# Check if the token and sysdig_host_url are provided, otherwise fetch from environment variables
3140
if not token and not sysdig_host_url:
3241
env_vars = get_api_env_vars()
3342
token = env_vars["SYSDIG_SECURE_TOKEN"]
3443
sysdig_host_url = env_vars["SYSDIG_HOST"]
3544
if not old_api:
45+
"""
46+
Client expecting the public API URL in the format https://api.{region}.sysdig.com. We will check the following:
47+
- A valid Sysdig host URL is provided by matching the expected patterns with a regex.
48+
- If not, we will try to fetch the public API URL from the app config yaml 'sysdig.public_api_url'.
49+
- If neither is available, we will raise an error.
50+
"""
3651
sysdig_host_url = _get_public_api_url(sysdig_host_url)
52+
if not sysdig_host_url:
53+
sysdig_host_url = app_config.get("sysdig", {}).get("public_api_url")
54+
if not sysdig_host_url:
55+
raise ValueError(
56+
"No valid Sysdig public API URL found. Please check your Sysdig host URL or"
57+
"explicitly set the public API URL in the app config 'sysdig.public_api_url'."
58+
"The expected format is https://api.{region}.sysdig.com."
59+
)
3760
log.info(f"Using public API URL: {sysdig_host_url}")
3861

3962
configuration = sysdig_client.Configuration(
@@ -92,5 +115,5 @@ def _get_public_api_url(base_url: str) -> str:
92115
region = region_fn(match)
93116
return f"https://api.{region}.sysdig.com"
94117

95-
log.warning("A not recognized Sysdig URL was provided, returning the same URL. This may lead to unexpected behavior.")
96-
return base_url
118+
log.warning("A not recognized Sysdig URL was provided, returning an empty string. This may lead to unexpected behavior.")
119+
return ""

0 commit comments

Comments
 (0)