|
5 | 5 | from azure import identity
|
6 | 6 |
|
7 | 7 | def get_mssql_connection():
|
8 |
| - logging.info('Getting MSSQL connection') |
9 |
| - logging.info(' - Getting EntraID credentials...') |
| 8 | + print('Getting MSSQL connection') |
10 | 9 | mssql_connection_string = os.environ["MSSQL"]
|
11 |
| - credential = identity.DefaultAzureCredential(exclude_interactive_browser_credential=False) |
12 |
| - token_bytes = credential.get_token("https://database.windows.net/.default").token.encode("UTF-16-LE") |
13 |
| - token_struct = struct.pack(f'<I{len(token_bytes)}s', len(token_bytes), token_bytes) |
14 |
| - SQL_COPT_SS_ACCESS_TOKEN = 1256 # This connection option is defined by microsoft in msodbcsql.h |
15 |
| - logging.info(' - Connecting to MSSQL...') |
16 |
| - conn = pyodbc.connect(mssql_connection_string, attrs_before={SQL_COPT_SS_ACCESS_TOKEN: token_struct}) |
| 10 | + print(mssql_connection_string) |
| 11 | + if any(s in mssql_connection_string.lower() for s in ["uid"]): |
| 12 | + print(' - Using SQL Server authentication') |
| 13 | + attrs_before = None |
| 14 | + else: |
| 15 | + print(' - Getting EntraID credentials...') |
| 16 | + mssql_connection_string = os.environ["MSSQL"] |
| 17 | + credential = identity.DefaultAzureCredential(exclude_interactive_browser_credential=False) |
| 18 | + token_bytes = credential.get_token("https://database.windows.net/.default").token.encode("UTF-16-LE") |
| 19 | + token_struct = struct.pack(f'<I{len(token_bytes)}s', len(token_bytes), token_bytes) |
| 20 | + SQL_COPT_SS_ACCESS_TOKEN = 1256 # This connection option is defined by microsoft in msodbcsql.h |
| 21 | + attrs_before = {SQL_COPT_SS_ACCESS_TOKEN: token_struct} |
| 22 | + |
| 23 | + print(' - Connecting to MSSQL...') |
| 24 | + conn = pyodbc.connect(mssql_connection_string, attrs_before=attrs_before) |
| 25 | + |
17 | 26 | return conn
|
0 commit comments