Skip to content

Commit cd75443

Browse files
authored
Improve validation of client ID and env (#179)
1 parent 46b6add commit cd75443

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

apitally/client/client_base.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,19 @@ def __init__(self, client_id: str, env: str, request_logging_config: Optional[Re
4444
if hasattr(self, "client_id"):
4545
raise RuntimeError("Apitally client is already initialized") # pragma: no cover
4646

47-
client_id_valid = True
48-
env_valid = True
47+
self.client_id = str(client_id)
48+
self.env = str(env)
49+
self.enabled = True
50+
51+
if not self.validate_client_id(self.client_id):
52+
self.enabled = False
53+
logger.error(f"invalid client_id '{self.client_id}' (expecting string in hexadecimal UUID format)")
54+
if not self.validate_env(self.env):
55+
self.enabled = False
56+
logger.error(
57+
f"invalid env '{self.env}' (expecting string with 1-32 alphanumeric characters and hyphens only)"
58+
)
4959

50-
try:
51-
UUID(client_id)
52-
except ValueError:
53-
logger.error(f"invalid client_id '{client_id}' (expecting hexadecimal UUID format)")
54-
client_id_valid = False
55-
56-
if re.match(r"^[\w-]{1,32}$", env) is None:
57-
logger.error(f"invalid env '{env}' (expecting 1-32 alphanumeric lowercase characters and hyphens only)")
58-
env_valid = False
59-
60-
self.client_id = client_id
61-
self.env = env
62-
self.enabled = client_id_valid and env_valid
6360
self.instance_uuid = str(uuid4())
6461
self.request_counter = RequestCounter()
6562
self.validation_error_counter = ValidationErrorCounter()
@@ -104,3 +101,15 @@ def get_sync_data(self) -> Dict[str, Any]:
104101
"consumers": self.consumer_registry.get_and_reset_updated_consumers(),
105102
}
106103
return self.add_uuids_to_data(data)
104+
105+
@staticmethod
106+
def validate_client_id(client_id: str) -> bool:
107+
try:
108+
UUID(client_id)
109+
return True
110+
except ValueError:
111+
return False
112+
113+
@staticmethod
114+
def validate_env(env: str) -> bool:
115+
return re.match(r"^[\w-]{1,32}$", env) is not None

0 commit comments

Comments
 (0)