8
8
import re
9
9
from typing import Optional
10
10
11
+ # Application config loader
12
+ from utils .app_config import get_app_config
13
+
11
14
# Set up logging
12
15
logging .basicConfig (format = "%(asctime)s-%(process)d-%(levelname)s- %(message)s" , level = os .environ .get ("LOGLEVEL" , "ERROR" ))
13
16
log = logging .getLogger (__name__ )
14
17
18
+ app_config = get_app_config ()
19
+
15
20
16
21
# Lazy-load the Sysdig client configuration
17
22
def get_configuration (
@@ -22,18 +27,36 @@ def get_configuration(
22
27
23
28
Args:
24
29
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.
27
34
Returns:
28
35
sysdig_client.Configuration: A configured Sysdig client instance.
36
+ Raises:
37
+ ValueError: If the Sysdig host URL is not provided or is invalid.
29
38
"""
30
39
# Check if the token and sysdig_host_url are provided, otherwise fetch from environment variables
31
40
if not token and not sysdig_host_url :
32
41
env_vars = get_api_env_vars ()
33
42
token = env_vars ["SYSDIG_SECURE_TOKEN" ]
34
43
sysdig_host_url = env_vars ["SYSDIG_HOST" ]
35
44
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
+ """
36
51
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
+ )
37
60
log .info (f"Using public API URL: { sysdig_host_url } " )
38
61
39
62
configuration = sysdig_client .Configuration (
@@ -67,20 +90,30 @@ def get_api_env_vars() -> dict:
67
90
68
91
def _get_public_api_url (base_url : str ) -> str :
69
92
"""
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.
71
98
72
99
Args:
73
100
base_url: The base URL of the Sysdig API
74
101
75
102
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
77
104
"""
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