How to connect to azure sql from pod in an AKS where workload identity is enabled?

Tianqi Zhang 0 Reputation points
2023-10-24T06:13:32.13+00:00

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 
Azure SQL Database
Azure Kubernetes Service
Azure Kubernetes Service
An Azure service that provides serverless Kubernetes, an integrated continuous integration and continuous delivery experience, and enterprise-grade security and governance.
2,456 questions
{count} votes

1 answer

Sort by: Most helpful
  1. AlaaBarqawi_MSFT 942 Reputation points Microsoft Employee
    2023-10-26T06:32:07.8333333+00:00

    Hi @Tianqi Zhang ODBC driver not supporting AKS

    please check this URL https://techcommunity.microsoft.com/t5/azure-database-support-blog/lesson-learned-384-odbc-driver-not-supporting-aks-workload/ba-p/3858209

    you can check the workaround yo use AccessToken 

    please mark as answer if this help

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.