Skip to content

Commit ba4df5c

Browse files
committed
ruff
1 parent d2b242e commit ba4df5c

2 files changed

Lines changed: 3 additions & 56 deletions

File tree

mindsdb/integrations/utilities/handlers/auth_utilities/oauth2/client_credentials.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,7 @@ def _ip_in_forbidden_range(ip: ipaddress._BaseAddress) -> Optional[str]:
5858

5959

6060
def _validate_token_url(token_url: str) -> None:
61-
"""Raise ValueError if token_url violates SSRF safety rules.
62-
63-
Rejects: non-http(s) schemes, localhost/aliases, IP literals or DNS-resolved
64-
hosts in private/loopback/link-local/multicast/reserved ranges. Logs a
65-
WARNING when scheme is http (allowed for staging/internal setups).
66-
"""
61+
"""Raise ValueError if token_url violates SSRF safety rules."""
6762
if not isinstance(token_url, str) or not token_url:
6863
raise ValueError("token_url must be a non-empty string")
6964

@@ -153,8 +148,6 @@ def __init__(
153148
self._memory_cache: Optional[dict] = None
154149
self._missing_expires_in_logged = False
155150

156-
# ------------------------------------------------------------------ public
157-
158151
def get_token(self) -> str:
159152
"""Return a valid access token, refreshing if needed."""
160153
cached = self._read_cache()
@@ -200,8 +193,6 @@ def current_secrets(self) -> list:
200193
return [token]
201194
return []
202195

203-
# -------------------------------------------------------- request handling
204-
205196
def _request_token(self) -> dict:
206197
body: dict = {"grant_type": "client_credentials"}
207198

@@ -226,7 +217,7 @@ def _request_token(self) -> dict:
226217
if self.token_auth_method == "client_secret_post":
227218
body["client_id"] = self.client_id
228219
body["client_secret"] = self.client_secret
229-
else: # client_secret_basic
220+
else:
230221
credentials = f"{self.client_id}:{self.client_secret}".encode("utf-8")
231222
headers["Authorization"] = "Basic " + base64.b64encode(credentials).decode("ascii")
232223

@@ -351,8 +342,6 @@ def _read_capped(self, response: requests.Response) -> bytes:
351342
) from self._sanitize_exception(exc)
352343
return b"".join(chunks)
353344

354-
# ----------------------------------------------------------------- caching
355-
356345
def _read_cache(self) -> Optional[dict]:
357346
if self.handler_storage is not None:
358347
try:
@@ -392,8 +381,6 @@ def _is_expired(cached: dict) -> bool:
392381
return True
393382
return time.time() >= expires_at
394383

395-
# ------------------------------------------------------------- diagnostics
396-
397384
def _safe_host(self) -> str:
398385
try:
399386
return urlparse(self.token_url).hostname or "<unknown>"

tests/unit/utilities/handlers/auth_utilities/oauth2/test_client_credentials.py

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@
2222
CLIENT_SECRET = "client-secret-xyz"
2323

2424

25-
# ---------------------------------------------------------------------------
26-
# Helpers
27-
# ---------------------------------------------------------------------------
28-
29-
3025
class FakeResponse:
3126
"""Minimal stand-in for requests.Response used by token-request tests."""
3227

@@ -105,11 +100,6 @@ def fake_getaddrinfo(host, *args, **kwargs):
105100
monkeypatch.setattr(cc_module.socket, "getaddrinfo", fake_getaddrinfo)
106101

107102

108-
# ---------------------------------------------------------------------------
109-
# Construction validation
110-
# ---------------------------------------------------------------------------
111-
112-
113103
class TestConstructionValidation:
114104
def test_unsupported_auth_method_raises(self, monkeypatch):
115105
_bypass_dns(monkeypatch)
@@ -184,11 +174,6 @@ def test_https_url_accepted_no_warning(self, monkeypatch, caplog):
184174
assert not any(r.levelno == logging.WARNING for r in caplog.records)
185175

186176

187-
# ---------------------------------------------------------------------------
188-
# Request shape
189-
# ---------------------------------------------------------------------------
190-
191-
192177
class TestRequestShape:
193178
def _provider(self, monkeypatch, **kwargs):
194179
_bypass_dns(monkeypatch)
@@ -398,11 +383,6 @@ def test_invalid_expires_in_defaults_to_300(self, monkeypatch, caplog, expires_i
398383
assert any("expires_in" in r.message for r in caplog.records)
399384

400385

401-
# ---------------------------------------------------------------------------
402-
# Caching
403-
# ---------------------------------------------------------------------------
404-
405-
406386
class TestCaching:
407387
def _provider(self, monkeypatch, **kwargs):
408388
_bypass_dns(monkeypatch)
@@ -475,11 +455,6 @@ def fake_post(*a, **kw):
475455
assert calls["n"] == 2
476456

477457

478-
# ---------------------------------------------------------------------------
479-
# Concurrency — double-checked locking
480-
# ---------------------------------------------------------------------------
481-
482-
483458
class TestConcurrency:
484459
def test_concurrent_get_token_makes_single_http_call(self, monkeypatch):
485460
"""Two threads call get_token() with empty cache simultaneously.
@@ -545,11 +520,6 @@ def worker(idx):
545520
)
546521

547522

548-
# ---------------------------------------------------------------------------
549-
# Storage
550-
# ---------------------------------------------------------------------------
551-
552-
553523
class TestStorage:
554524
def test_token_persists_across_provider_instances(self, monkeypatch):
555525
_bypass_dns(monkeypatch)
@@ -647,11 +617,6 @@ def test_cache_does_not_contain_credentials(self, monkeypatch):
647617
assert "https://api.example.com" not in as_text
648618

649619

650-
# ---------------------------------------------------------------------------
651-
# current_secrets
652-
# ---------------------------------------------------------------------------
653-
654-
655620
class TestCurrentSecrets:
656621
def _provider(self, monkeypatch):
657622
_bypass_dns(monkeypatch)
@@ -687,11 +652,6 @@ def test_after_invalidate_returns_empty(self, monkeypatch):
687652
assert provider.current_secrets() == []
688653

689654

690-
# ---------------------------------------------------------------------------
691-
# Error sanitization
692-
# ---------------------------------------------------------------------------
693-
694-
695655
class TestErrorSanitization:
696656
def _provider(self, monkeypatch):
697657
_bypass_dns(monkeypatch)
@@ -751,7 +711,7 @@ def test_401_includes_provider_error_fields(self, monkeypatch):
751711
assert "invalid_client" in msg
752712
assert "Client authentication failed" in msg
753713
assert CLIENT_SECRET not in msg
754-
assert CLIENT_ID in msg or "client_id" in msg # client_id is not a secret
714+
assert CLIENT_ID in msg or "client_id" in msg
755715

756716
def test_redirect_response_treated_as_error(self, monkeypatch):
757717
provider = self._provider(monkeypatch)

0 commit comments

Comments
 (0)