Skip to content

FEAT: Azure Interactive support for Mac and Linux #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from

Conversation

jahnvi480
Copy link
Contributor

@jahnvi480 jahnvi480 commented Jul 9, 2025

ADO Work Item Reference

AB#34946


Summary

This pull request introduces significant enhancements to the handling of connection strings in the mssql_python package, focusing on improved support for Active Directory Interactive Authentication (AAD) and more robust handling of attributes passed to the database driver. The changes ensure better compatibility across platforms and allow the seamless merging of attributes required by the driver.

Enhancements to Connection String Handling:

  • Updated the _construct_connection_string method in mssql_python/connection.py to support returning a tuple containing the connection string and a dictionary of attributes (attrs_before) when needed. This allows for more flexible handling of attributes required by the database driver. [1] [2] [3]
  • Modified the add_driver_to_connection_str function in mssql_python/helpers.py to return either a string or a tuple of the connection string and attrs_before. This change ensures that attributes required by the driver are preserved and passed correctly. [1] [2]

Support for Active Directory Interactive Authentication:

  • Enhanced the add_driver_name_to_app_parameter function in mssql_python/helpers.py to handle AAD Interactive Authentication. On non-Windows platforms, it now uses the azure-identity library to fetch a token and includes it in the connection attributes. This ensures compatibility with Azure SQL databases requiring AAD authentication.

These updates improve the flexibility and platform compatibility of the mssql_python package, making it more robust for a variety of use cases.

@Copilot Copilot AI review requested due to automatic review settings July 9, 2025 10:51
Copy link
Contributor

@Copilot 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

This PR enhances connection string construction in the mssql_python package by enabling return of driver attributes for AAD Interactive Authentication and by merging attributes more robustly.

  • Extended _construct_connection_string to return either a simple string or a tuple (connection_str, attrs_before) and merged any existing attrs_before.
  • Updated add_driver_to_connection_str and add_driver_name_to_app_parameter to support returning driver attributes (attrs_before) and to fetch AAD tokens on non-Windows platforms.
  • Adjusted tests to expect the corrected format without duplicate semicolons.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
tests/test_003_connection.py Removed extra semicolon in expected connection string tests.
mssql_python/helpers.py Modified driver helper to handle tuple returns and AAD tokens; enhanced docstrings.
mssql_python/connection.py Updated constructor and _construct_connection_string to unpack tuple returns and merge attributes.
Comments suppressed due to low confidence (3)

mssql_python/helpers.py:19

  • Typo in docstring: "DDBC" should be corrected to "ODBC" to match the driver being added.
    Add the DDBC driver to the connection string if not present.

mssql_python/helpers.py:135

  • Missing import for platform. Add import platform at the top of the module to avoid a NameError.
    if has_aad_interactive and platform.system().lower() != "windows":

mssql_python/connection.py:69

  • Variable attrs_before is undefined here; the tuple unpack used attrs_from_driver. It should be self._attrs_before = attrs_from_driver or {}.
            self._attrs_before = attrs_before or {}

@jahnvi480 jahnvi480 changed the title Jahnvi/interactive mac linux FEAT: Azure Interactive support for Mac and Linux Jul 9, 2025
@github-actions github-actions bot added the pr-size: small Minimal code update label Jul 9, 2025
@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: small Minimal code update labels Jul 9, 2025
@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: medium Moderate update size labels Jul 10, 2025
@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: medium Moderate update size labels Jul 10, 2025
@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: medium Moderate update size labels Jul 10, 2025
@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: medium Moderate update size labels Jul 10, 2025
Copy link
Collaborator

@bewithgaurav bewithgaurav left a comment

Choose a reason for hiding this comment

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

need some major structural changes for auth related code and create another PR for row.py changes.

def __str__(self):
"""Return a string representation of the row"""
return f"Row({', '.join(map(str, self._values))})"
Copy link
Collaborator

Choose a reason for hiding this comment

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

we wouldn't be needing Row(...) in the string representations, for end users the output should look like a tuple (e.g., (1, 2, 3)).

I suggest updating both the methods as follows:

def __str__(self):
    return str(tuple(self._values))

def __repr__(self):
    return repr(tuple(self._values))

this makes sure that printing or representing a Row instance makes it behave just like a tuple, which is the expected behaviour, also please add tests for this inside test cursor to test fetch functions results.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe these changes should be a separate PR of its own

if not param:
continue

if param.lower().startswith("authentication="):
Copy link
Collaborator

Choose a reason for hiding this comment

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

The auth logic is currently spread across multiple helpers and is resulting in cascading function changes and unstable signatures.
I recommend refactoring all authentication-related code into a dedicated mssql_python/auth.py module. This will centralize and simplify authentication logic and help maintain single responsibility in other modules (like helpers and connection).

@jahnvi480 jahnvi480 closed this Jul 16, 2025
@jahnvi480
Copy link
Contributor Author

Added a new PR with required changes #135

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