You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
FEAT: EntraID Access Token Support for BulkCopy (#426)
### Work Item / Issue Reference
<!--
IMPORTANT: Please follow the PR template guidelines below.
For mssql-python maintainers: Insert your ADO Work Item ID below
For external contributors: Insert Github Issue number below
Only one reference is required - either GitHub issue OR ADO Work Item.
-->
<!-- mssql-python maintainers: ADO Work Item -->
> AB#42282
>
[AB#42283](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/42283)
-------------------------------------------------------------------
### Summary
<!-- Insert your summary of changes below. Minimum 10 characters
required. -->
This pull request introduces significant improvements to Azure AD
authentication handling for bulk copy operations, ensuring fresh token
acquisition to prevent expired-token errors, and refactors related code
for clarity and robustness. It also updates the connection and
authentication APIs to propagate and utilize authentication type
information more reliably.
Azure AD authentication enhancements:
* Added `AADAuth.get_raw_token` and refactored token acquisition logic
to ensure a fresh Azure AD token is acquired each time bulkcopy is
called, preventing expired-token errors. The new method avoids
credential/token caching and is used specifically for bulk copy
operations. (`mssql_python/auth.py`,
[mssql_python/auth.pyL33-R51](diffhunk://#diff-19a0c93fc8573a5a7bfcadda0a2fb8f1b340c4502e1308c4f8a1e4508136c6e1L33-R51))
* Updated bulk copy logic to use the new `get_raw_token` method, storing
the auth type on the connection and acquiring a fresh token at bulk copy
time. Sensitive data is now removed from memory after use for improved
security. (`mssql_python/cursor.py`,
[[1]](diffhunk://#diff-deceea46ae01082ce8400e14fa02f4b7585afb7b5ed9885338b66494f5f38280L2610-R2633)
[[2]](diffhunk://#diff-deceea46ae01082ce8400e14fa02f4b7585afb7b5ed9885338b66494f5f38280L2656-R2674)
Connection and authentication API changes:
* Refactored `process_connection_string` to return the authentication
type as a third value, and added `extract_auth_type` to reliably extract
auth type from the connection string when not propagated (e.g., Windows
Interactive). Connection objects now store the auth type for later use.
(`mssql_python/auth.py`,
[[1]](diffhunk://#diff-19a0c93fc8573a5a7bfcadda0a2fb8f1b340c4502e1308c4f8a1e4508136c6e1R219-R249)
[[2]](diffhunk://#diff-19a0c93fc8573a5a7bfcadda0a2fb8f1b340c4502e1308c4f8a1e4508136c6e1L262-R296)
[[3]](diffhunk://#diff-19a0c93fc8573a5a7bfcadda0a2fb8f1b340c4502e1308c4f8a1e4508136c6e1L272-R306);
`mssql_python/connection.py`,
[[4]](diffhunk://#diff-29bb94de45aae51c23a6426d40133c28e4161e68769e08d046059c7186264e90L42-R42)
[[5]](diffhunk://#diff-29bb94de45aae51c23a6426d40133c28e4161e68769e08d046059c7186264e90R266-R270)
[[6]](diffhunk://#diff-29bb94de45aae51c23a6426d40133c28e4161e68769e08d046059c7186264e90R280-R283)
Testing improvements:
* Expanded test coverage to verify raw token acquisition, connection
string processing, and correct storage of authentication type on
connection objects. Tests ensure that the new APIs and behaviors work as
expected. (`tests/test_008_auth.py`,
[[1]](diffhunk://#diff-83e8bc8183c8cc53e88bf74d3cb8ef1751be6854edd9a727602fe618e691ecdbR86-R90)
[[2]](diffhunk://#diff-83e8bc8183c8cc53e88bf74d3cb8ef1751be6854edd9a727602fe618e691ecdbL329-R365)
[[3]](diffhunk://#diff-83e8bc8183c8cc53e88bf74d3cb8ef1751be6854edd9a727602fe618e691ecdbR380-R390)
Error handling and logging:
* Improved error handling and logging in token acquisition, providing
clearer messages for unsupported authentication types and unexpected
errors. (`mssql_python/auth.py`,
[[1]](diffhunk://#diff-19a0c93fc8573a5a7bfcadda0a2fb8f1b340c4502e1308c4f8a1e4508136c6e1L56-L79)
[[2]](diffhunk://#diff-19a0c93fc8573a5a7bfcadda0a2fb8f1b340c4502e1308c4f8a1e4508136c6e1L91)
Documentation and naming consistency:
* Updated docstrings and comments throughout the authentication code for
clarity and consistency, reflecting the new behaviors and APIs.
(`mssql_python/auth.py`,
[mssql_python/auth.pyL183-R197](diffhunk://#diff-19a0c93fc8573a5a7bfcadda0a2fb8f1b340c4502e1308c4f8a1e4508136c6e1L183-R197))
0 commit comments