Skip to content

Commit f0f9c29

Browse files
authored
Handle invalid client ID in ApitallyClient (#161)
* Handle invalid client ID in ApitallyClient * Fix * Add test * Fix * Fix
1 parent 4f23114 commit f0f9c29

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

.github/workflows/tests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ jobs:
6464
- django-ninja==0.18.0 django
6565
- litestar
6666
- litestar==2.0.1
67-
- blacksheep
67+
- blacksheep==2.4.1 # current version v2.4.2 is broken
6868
- blacksheep==2.1.0
6969
exclude:
7070
- python: "3.12"

apitally/client/client_asyncio.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ def get_http_client(self) -> httpx.AsyncClient:
5050
return httpx.AsyncClient(base_url=self.hub_url, timeout=REQUEST_TIMEOUT, proxies=self.proxy)
5151

5252
def start_sync_loop(self) -> None:
53+
if not self.enabled:
54+
return # pragma: no cover
5355
self._stop_sync_loop = False
5456
self._sync_loop_task = asyncio.create_task(self._run_sync_loop())
5557

apitally/client/client_base.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,23 @@ def __new__(cls: Type[TApitallyClient], *args, **kwargs) -> TApitallyClient:
4343
def __init__(self, client_id: str, env: str, request_logging_config: Optional[RequestLoggingConfig] = None) -> None:
4444
if hasattr(self, "client_id"):
4545
raise RuntimeError("Apitally client is already initialized") # pragma: no cover
46+
47+
client_id_valid = True
48+
env_valid = True
49+
4650
try:
4751
UUID(client_id)
4852
except ValueError:
49-
raise ValueError(f"invalid client_id '{client_id}' (expecting hexadecimal UUID format)")
53+
logger.error(f"invalid client_id '{client_id}' (expecting hexadecimal UUID format)")
54+
client_id_valid = False
55+
5056
if re.match(r"^[\w-]{1,32}$", env) is None:
51-
raise ValueError(f"invalid env '{env}' (expecting 1-32 alphanumeric lowercase characters and hyphens only)")
57+
logger.error(f"invalid env '{env}' (expecting 1-32 alphanumeric lowercase characters and hyphens only)")
58+
env_valid = False
5259

5360
self.client_id = client_id
5461
self.env = env
55-
self.enabled = True
62+
self.enabled = client_id_valid and env_valid
5663
self.instance_uuid = str(uuid4())
5764
self.request_counter = RequestCounter()
5865
self.validation_error_counter = ValidationErrorCounter()

apitally/client/client_threading.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ def __init__(
6161
self._sync_data_queue: Queue[Dict[str, Any]] = Queue()
6262

6363
def start_sync_loop(self) -> None:
64+
if not self.enabled:
65+
return # pragma: no cover
6466
self._stop_sync_loop.clear()
6567
if self._thread is None or not self._thread.is_alive():
6668
self._thread = Thread(target=self._run_sync_loop, daemon=True)

tests/test_client_base.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def test_client_id_validation():
2+
from apitally.client.client_base import ApitallyClientBase
3+
4+
ApitallyClientBase._instance = None
5+
client = ApitallyClientBase(client_id="xxx", env="test")
6+
assert client.enabled is False
7+
8+
ApitallyClientBase._instance = None
9+
client = ApitallyClientBase(client_id="9003a5c6-0725-4502-8e57-963a21ba97b6", env="")
10+
assert client.enabled is False
11+
12+
ApitallyClientBase._instance = None
13+
client = ApitallyClientBase(client_id="9003a5c6-0725-4502-8e57-963a21ba97b6", env="test")
14+
assert client.enabled is True
15+
16+
ApitallyClientBase._instance = None

0 commit comments

Comments
 (0)