sfu-db / connector-x

Fastest library to load data from DB to DataFrames in Rust and Python
https://sfu-db.github.io/connector-x
MIT License
1.94k stars 154 forks source link

Support for MS SQL Server Token Authentication. #569

Open akelloway opened 8 months ago

akelloway commented 8 months ago

I would like to be able to access an Azure SQL Server using the token based authentication method.

Using pyodbc this would look like this:

def get_conn():
    connection_string = 'Driver={ODBC Driver 18 for SQL Server};Server=tcp:<SERVER_ADDRESS>,1433;Database=<DATABASE_NAME>;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'    
    credential = DefaultAzureCredential(exclude_interactive_browser_credential=False)
    token_bytes = credential.get_token("https://database.windows.net/.default").token.encode("UTF-16-LE")
    token_struct = struct.pack(f'<I{len(token_bytes)}s', len(token_bytes), token_bytes)
    SQL_COPT_SS_ACCESS_TOKEN = 1256  # This connection option is defined by microsoft in msodbcsql.h
    conn = pyodbc.connect(connection_string, attrs_before={SQL_COPT_SS_ACCESS_TOKEN: token_struct})
    return conn

which used SQL_COPT_SS_ACCESS_TOKEN further described here in the attrs_before parameter - described as "_attrsbefore: Set low-level connection attributes before a connection is attempted."

I wonder if this is possible today using connectorX or if this could be added at a later date? (I'm no Rust dev but I see that tiberius does support AADToken auth - perhaps this can be leveraged to enable this feature in connectorX?)

Thanks!

MuhammadPathan commented 4 months ago

@akelloway Was there a solution to this?

akelloway commented 3 months ago

@MuhammadPathan - I have not yet found a solution to this.

MuhammadPathan commented 3 months ago

@akelloway thanks for response was trying to do the same thing. in the end went with pyodbc and loaded data into a pandas df then converted to polars afterwards.