Hi, I have an aks with workload identity enabled. I have a user assigned managed identity created for WI and this user is the aad admin for the sql server. I have a pod on the AKS trying to connect to the same db using different type of connection strings. (ADO, JDBC, ODBC, SQLALCHEMY PYODBC). The ADO and JDBC connections were successful using workload identity. But I got [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Login failed for user '<token-identified principal>' for the ODBCs.
Could you help on identifying the issues? Thanks.
For ODBC connection:
using System.Data.Odbc;
var connectionString="SERVER=something.database.windows.net;DATABASE=dbname;DRIVER={ODBC Driver 18 for SQL Server};Authentication=ActiveDirectoryMsi;";
DbConnection connection;
connection = new OdbcConnection(connectionString);
connection.Open();
For PYODBC:
Reference: https://github.com/sqlalchemy/sqlalchemy/blob/main/lib/sqlalchemy/dialects/mssql/pyodbc.py#L122
import sqlalchemy
from azure.identity import DefaultAzureCredential
SQL_COPT_SS_ACCESS_TOKEN = 1256
TOKEN_URL = "https://database.windows.net/"
connection_string="mssql+pyodbc://:@something.database.windows.net:1433/dbname?driver=ODBC+Driver+18+for+SQL+Server&authentication=ActiveDirectoryMsi"
engine = sqlalchemy.create_engine(connection_string)
azure_credentials = DefaultAzureCredential()
connection = engine.connect()
@sqlalchemy.event.listens_for(engine, "do_connect")
def provide_token(dialect, conn_rec, cargs, cparams):
cargs[0] = cargs[0].replace(";Trusted_Connection=Yes", "")
raw_token = azure_credentials.get_token(TOKEN_URL).token.encode("utf-16-le") token_struct = struct.pack(f"<I{len(raw_token)}s", len(raw_token), raw_token)
cparams["attrs_before"] = {SQL_COPT_SS_ACCESS_TOKEN: token_struct}
Part of the dockerfile for the pod image
FROM registry.access.redhat.com/ubi8/ubi-minimal:8.8 as ubi
WORKDIR /bin
ENV ACCEPT_EULA=Y
RUN curl https://packages.microsoft.com/config/rhel/8/prod.repo > /etc/yum.repos.d/mssql-release.repo
RUN microdnf -y install unixODBC openssl libtool-ltdl dotnet-runtime-6.0 java-1.8.0-openjdk-headless python3 python3-pip unixODBC-devel gcc-c++ python3-devel krb5-workstation krb5-libs mssql-tools
RUN microdnf -y download msodbcsql18
RUN microdnf -y install msodbcsql18-*.x86_64
RUN microdnf clean all
RUN pip3 install sqlalchemy pyodbc setuptools-rust azure.identity