Skip to content

Commit 189bafa

Browse files
committed
Resolving comments
1 parent 20aefe1 commit 189bafa

3 files changed

Lines changed: 25 additions & 10 deletions

File tree

mssql_python/auth.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@
1010
from typing import Tuple, Dict, Optional
1111

1212
from mssql_python.logging import logger
13-
from mssql_python.constants import AuthType, ConstantsDDBC
13+
from mssql_python.constants import (
14+
AuthType,
15+
ConstantsDDBC,
16+
_KEY_AUTHENTICATION,
17+
_KEY_UID,
18+
_KEY_PWD,
19+
_KEY_TRUSTED_CONNECTION,
20+
)
1421

1522
# Module-level credential instance cache.
1623
# Reusing credential objects allows the Azure Identity SDK's built-in
@@ -23,7 +30,7 @@
2330
_credential_cache_lock = threading.Lock()
2431

2532
# Canonical keys to strip when handing an Entra-token connection to ODBC.
26-
_SENSITIVE_KEYS = frozenset({"UID", "PWD", "Trusted_Connection", "Authentication"})
33+
_SENSITIVE_KEYS = frozenset({_KEY_UID, _KEY_PWD, _KEY_TRUSTED_CONNECTION, _KEY_AUTHENTICATION})
2734

2835
# Map Authentication connection-string values to internal short names.
2936
_AUTH_TYPE_MAP: Dict[str, str] = {
@@ -229,5 +236,5 @@ def extract_auth_type(parsed_params: Dict[str, str]) -> Optional[str]:
229236
no platform checks — use :func:`process_auth_parameters` when you need
230237
the Windows-Interactive suppression logic.
231238
"""
232-
auth_value = parsed_params.get("Authentication", "").strip().lower()
239+
auth_value = parsed_params.get(_KEY_AUTHENTICATION, "").strip().lower()
233240
return _AUTH_TYPE_MAP.get(auth_value)

mssql_python/connection.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
from mssql_python.constants import ConstantsDDBC, GetInfoConstants
5050
from mssql_python.connection_string_parser import _ConnectionStringParser
5151
from mssql_python.connection_string_builder import _ConnectionStringBuilder
52-
from mssql_python.constants import _RESERVED_PARAMETERS
52+
from mssql_python.constants import _RESERVED_PARAMETERS, _KEY_AUTHENTICATION, _KEY_UID
5353

5454
if TYPE_CHECKING:
5555
from mssql_python.row import Row
@@ -292,7 +292,7 @@ def __init__(
292292
raise ValueError("native_uuid must be a boolean value or None")
293293
self._native_uuid = native_uuid
294294

295-
self.connection_str, self._parsed_params = self._construct_connection_string(
295+
self.connection_str, parsed_params = self._construct_connection_string(
296296
connection_str, **kwargs
297297
)
298298
self._attrs_before = attrs_before or {}
@@ -337,20 +337,20 @@ def __init__(
337337

338338
# Handle Entra ID authentication if specified.
339339
# The parsed dict is used directly — no re-parsing of the connection string.
340-
if "Authentication" in self._parsed_params:
341-
auth_type = process_auth_parameters(self._parsed_params)
340+
if _KEY_AUTHENTICATION in parsed_params:
341+
auth_type = process_auth_parameters(parsed_params)
342342

343343
if auth_type:
344344
# Capture credential kwargs (e.g. user-assigned MSI client_id)
345345
# from the parsed dict *before* remove_sensitive_params strips UID.
346346
credential_kwargs: Optional[Dict[str, str]] = None
347347
if auth_type == "msi":
348-
uid = (self._parsed_params.get("UID") or "").strip()
348+
uid = (parsed_params.get(_KEY_UID) or "").strip()
349349
if uid:
350350
credential_kwargs = {"client_id": uid}
351351

352352
# Strip sensitive params and rebuild the connection string.
353-
sanitized = remove_sensitive_params(self._parsed_params)
353+
sanitized = remove_sensitive_params(parsed_params)
354354
self.connection_str = _ConnectionStringBuilder(sanitized).build()
355355
token = get_auth_token(auth_type, credential_kwargs)
356356
if token:
@@ -360,7 +360,7 @@ def __init__(
360360
# Store auth type so bulkcopy() can acquire a fresh token later.
361361
# On Windows Interactive, process_auth_parameters returns None
362362
# (DDBC handles auth natively), so fall back to extract_auth_type.
363-
self._auth_type = auth_type or extract_auth_type(self._parsed_params)
363+
self._auth_type = auth_type or extract_auth_type(parsed_params)
364364

365365
self._closed = False
366366
self._timeout = timeout

mssql_python/constants.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,14 @@ def get_attribute_set_timing(attribute):
519519
"packetsize": "PacketSize",
520520
}
521521

522+
# Canonical normalized key names produced by _ConnectionStringParser._normalize_params.
523+
# Consumer code should reference these instead of hard-coding raw strings so that
524+
# a rename in _ALLOWED_CONNECTION_STRING_PARAMS is caught at import time.
525+
_KEY_AUTHENTICATION = "Authentication"
526+
_KEY_UID = "UID"
527+
_KEY_PWD = "PWD"
528+
_KEY_TRUSTED_CONNECTION = "Trusted_Connection"
529+
522530

523531
def get_info_constants() -> Dict[str, int]:
524532
"""

0 commit comments

Comments
 (0)