88import re
99from typing import Optional
1010
11+ # Application config loader
12+ from utils .app_config import get_app_config
13+
1114# Set up logging
1215logging .basicConfig (format = "%(asctime)s-%(process)d-%(levelname)s- %(message)s" , level = os .environ .get ("LOGLEVEL" , "ERROR" ))
1316log = logging .getLogger (__name__ )
1417
18+ app_config = get_app_config ()
19+
1520
1621# Lazy-load the Sysdig client configuration
1722def 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 (
@@ -67,20 +90,30 @@ def get_api_env_vars() -> dict:
6790
6891def _get_public_api_url (base_url : str ) -> str :
6992 """
70- Get the public API URL from the base URL.
93+ Maps a Sysdig base URL to its corresponding public API URL.
94+ This function extracts the region from the base URL and constructs the public API URL in the format
95+ https://api.{region}.sysdig.com.
96+
97+ If the base URL does not match any known patterns, it returns an empty string.
7198
7299 Args:
73100 base_url: The base URL of the Sysdig API
74101
75102 Returns:
76- str: The public API URL in the format https://api.< region> .sysdig.com
103+ str: The public API URL in the format https://api.{ region} .sysdig.com
77104 """
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"
105+
106+ patterns = [
107+ (r"^https://secure\.sysdig\.com$" , lambda m : "us1" ),
108+ (r"^https://([a-z]{2}\d)\.app\.sysdig\.com$" , lambda m : m .group (1 )),
109+ (r"^https://app\.([a-z]{2}\d)\.sysdig\.com$" , lambda m : m .group (1 )),
110+ ]
111+
112+ for pattern , region_fn in patterns :
113+ match = re .match (pattern , base_url )
114+ if match :
115+ region = region_fn (match )
116+ return f"https://api.{ region } .sysdig.com"
117+
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