Skip to content

FIX: Avoid parsing connection string multiple times in auth path (#580)#590

Open
jahnvi480 wants to merge 1 commit into
mainfrom
jahnvi/auth_conn_string_parsing
Open

FIX: Avoid parsing connection string multiple times in auth path (#580)#590
jahnvi480 wants to merge 1 commit into
mainfrom
jahnvi/auth_conn_string_parsing

Conversation

@jahnvi480
Copy link
Copy Markdown
Contributor

@jahnvi480 jahnvi480 commented May 19, 2026

Work Item / Issue Reference

AB#45135

GitHub Issue: #580


Summary

This pull request refactors and modernizes the authentication and connection string handling logic for Entra ID (Azure AD) authentication in the mssql_python package. The changes transition from string-based connection parameter processing to using normalized parameter dictionaries, improving code clarity, maintainability, and security. The update also streamlines Entra ID token handling and ensures sensitive information is managed more robustly.

Authentication and connection string handling improvements:

  • Refactored process_auth_parameters and related functions to operate on parsed parameter dictionaries instead of raw parameter lists, making the code more robust and less error-prone.
  • Added _SENSITIVE_KEYS and _AUTH_TYPE_MAP for centralized management of sensitive parameters and authentication type mappings.
  • Updated remove_sensitive_params to filter sensitive keys from parameter dictionaries rather than string lists.

Connection initialization and construction:

  • Modified the connection initialization in connection.py to use the parsed parameter dictionary throughout authentication handling, reducing redundant parsing and improving logic clarity.
  • Changed _construct_connection_string to return both the constructed connection string and the normalized parameter dictionary, and updated all usages and tests accordingly.

Code cleanup and test updates:

  • Removed obsolete functions and updated imports to reflect the new, dictionary-based API.
  • Updated tests to accommodate the new return signature of _construct_connection_string.

_construct_connection_string now returns the normalized parameter dict
alongside the connection string.  Auth helpers in auth.py read keys
directly from the dict instead of re-splitting the raw string.

Key changes:
- _construct_connection_string returns (str, Dict[str, str])
- process_connection_string removed; Connection.__init__ calls
  process_auth_parameters, remove_sensitive_params, get_auth_token
  and _ConnectionStringBuilder directly
- extract_auth_type and process_auth_parameters share a single
  _AUTH_TYPE_MAP; no duplicated mapping logic
- _SENSITIVE_KEYS promoted to module-level frozenset
- auth.py no longer imports _ConnectionStringBuilder

Net: -195 lines, one parse per connect instead of three.
Copilot AI review requested due to automatic review settings May 19, 2026 08:42
@github-actions github-actions Bot added the pr-size: medium Moderate update size label May 19, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the Entra ID auth handling path so the connection string is parsed only once. Connection._construct_connection_string now returns both the built string and the normalized parameter dict, which is then passed directly to the auth helpers; process_connection_string (and the duplicate auth parsing it contained) is removed.

Changes:

  • _construct_connection_string returns (conn_str, normalized_params); Connection.__init__ reuses the parsed dict for auth handling instead of regex-checking and re-parsing the string.
  • auth.py is reworked to operate on dicts: process_auth_parameters and extract_auth_type take a parsed dict, remove_sensitive_params filters a dict via the new _SENSITIVE_KEYS, and _AUTH_TYPE_MAP centralizes the auth value→short-name mapping. process_connection_string is removed.
  • Tests updated to the new dict-based signatures; the obsolete TestProcessConnectionString* suites and the empty/no-equals-sign edge cases are dropped in favor of dict-input edge cases.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
mssql_python/auth.py Replaces string-list APIs with dict-based ones; removes process_connection_string; adds _SENSITIVE_KEYS and _AUTH_TYPE_MAP.
mssql_python/connection.py Uses the parsed param dict for auth handling; switches _construct_connection_string to return a tuple and builds the sanitized string via _ConnectionStringBuilder.
tests/test_003_connection.py Updates _construct_connection_string call sites to unpack the new tuple.
tests/test_008_auth.py Migrates tests to dict-based APIs; removes process_connection_string tests and obsolete edge cases.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions
Copy link
Copy Markdown

📊 Code Coverage Report

🔥 Diff Coverage

100%


🎯 Overall Coverage

25%


📈 Total Lines Covered: 6984 out of 27100
📁 Project: mssql-python


Diff Coverage

Diff: main...HEAD, staged and unstaged changes

  • mssql_python/auth.py (100%)
  • mssql_python/connection.py (100%)

Summary

  • Total: 31 lines
  • Missing: 0 lines
  • Coverage: 100%

📋 Files Needing Attention

📉 Files with overall lowest coverage (click to expand)
mssql_python.pybind.build._deps.simdutf-src.src.haswell.implementation.cpp: 0.4%
mssql_python.pybind.build._deps.simdutf-src.src.implementation.cpp: 6.7%
mssql_python.pybind.build._deps.simdutf-src.include.simdutf.implementation.h: 10.4%
mssql_python.pybind.build._deps.simdutf-src.include.simdutf.scalar.utf16_to_utf8.utf16_to_utf8.h: 25.3%
mssql_python.pybind.logger_bridge.cpp: 59.2%
mssql_python.pybind.ddbc_bindings.h: 59.7%
mssql_python.pybind.build._deps.simdutf-src.include.simdutf.internal.isadetection.h: 65.3%
mssql_python.row.py: 70.5%
mssql_python.pybind.logger_bridge.hpp: 70.8%
mssql_python.pybind.ddbc_bindings.cpp: 74.2%

🔗 Quick Links

⚙️ Build Summary 📋 Coverage Details

View Azure DevOps Build

Browse Full Coverage Report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-size: medium Moderate update size

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants