簡體   English   中英

Accessing Azure SQL Server using 1) Python (local script--not going to be Azure function), 2) pyodbc & 3) Azure user-managed identity

[英]Accessing Azure SQL Server using 1) Python (local script--not going to be Azure function), 2) pyodbc & 3) Azure user-managed identity

我在我的本地機器上創建了一個 Python 腳本 & 我正在嘗試驗證到 Azure SQL 服務器(無服務器(即非托管)實例) 我不想將憑據存儲在代碼中,而是想利用用戶管理的身份 (UMI) 對我的 SQL 服務器進行身份驗證。

我在 Azure 門戶中創建了 UMI,將其分配給我的 SQL 服務器並在 SQL 服務器中授予它讀取、寫入和管理權限。

我在我的腳本中使用了 pyodbc 並且我相信我在使用連接字符串時遇到了問題。 在查看文檔和視頻后,我認為可能是我可以簡單地使用 UMI 客戶端 ID 而不是使用 Key Vault(因為我寧願盡可能不使用它); 同樣,如果我不需要,我不想使用 ODBC GUI 客戶端(即 ODBC 數據源管理員)來存儲憑據。

我的想法是至少我必須通過 pyodbc 連接字符串傳入 UMI 客戶端 ID 字符串,但話又說回來,我真的沒有太多這方面的經驗。

這是我的連接字符串:

db_connect = pyodbc.connect(f"DRIVER={pyodbc_driver}; SERVER={pyodbc_server}; DATABASE={pyodbc_db}; UID={pyodbc_umi_client_id}; Authentication=ActiveDirectoryMsi", autocommit=True)

這是我得到的錯誤:

...無法在 Active Directory 中對用戶“pyodbc_umi_client_id”進行身份驗證(身份驗證選項為“ActiveDirectoryMSI”)。\r\n錯誤代碼 0xA190; state 41360\r\n (0); [CE267] [Microsoft][用於 SQL 服務器的 ODBC 驅動程序 17]TCP 提供程序:超時錯誤 [258]。 (258); [CE267] [Microsoft][ODBC Driver 17 for SQL Server]登錄超時(0); [CE267] [Microsoft][ODBC Driver 17 for SQL Server]由於登錄響應延遲(258)無法完成登錄過程”)

對於 UID,我在 Azure 門戶網站上嘗試了來自 UMI 的客戶端 ID 字符串。 此外,我還嘗試從 Azure 模塊之一導入以下內容:

from azure.identity import DefaultAzureCredential
...
pyodbc_umi_client_id = 'client_id' # client id string from umi in azure portal
db_umi_crd = DefaultAzureCredential(managed_identity_client_id=pyodbc_umi_client_id)

這是所有刪除了識別信息的腳本,它以某種方式很有幫助:

"""Dec 27, 2021

Want to connect to SQL db by using Azure
user-managed identity (UMI).
"""

import datetime
from azure.identity import DefaultAzureCredential
import pyodbc


# global vars
program_name = 'AZURE SQL UMI CONNECTION' 
original_date = datetime.datetime(2021, 12, 27)


def main():
    """Run main part (i.e., all functions) of the program

    Arguments:
        None

    Returns:
        None

    Raises:
        None

    """
    print_header(program_name, original_date)
    db_work()


def db_work():
    """Connect to the db and do work

        Arguments:
        None

    Returns:
        None

    Raises:
        None
    """
    # connection string vars
    pyodbc_driver = '{ODBC Driver 17 for SQL Server}'
    pyodbc_server = 'tcp:server_url,1433'
    pyodbc_db = 'sql_db'
    pyodbc_umi_client_id = 'client_id' # client id string from umi in azure portal
    db_umi_crd = DefaultAzureCredential(managed_identity_client_id=pyodbc_umi_client_id)

    # connection string
    # db_connect = pyodbc.connect(f"DRIVER={pyodbc_driver}; SERVER={pyodbc_server}; DATABASE={pyodbc_db}; UID={db_umi_crd}")
    db_connect = pyodbc.connect(f"DRIVER={pyodbc_driver}; SERVER={pyodbc_server}; DATABASE={pyodbc_db}; UID={pyodbc_umi_client_id}; Authentication=ActiveDirectoryMsi", autocommit=True)

    # db cursor
    db_cursor = db_connect.cursor()

    # do work
    rows = db_cursor.execute('select * from orderitems').fetchall()
    for row in rows:
        print(row)


def print_header(program_name, original_date, border='*'):
    """Print header indicating name of program

    Arguments:
        Program name: Positional arg. This is global var.
        Original Date: Positional arg. Date script was originally created.
        Border: Keyword arg. Border that is to print around name of program.

    Returns:
        None

    Raises:
        None
    """
    program_name_len = len(program_name) + len(str(original_date))
    print()
    print(border * program_name_len)
    print(program_name, ' ', str(original_date))
    print(border * program_name_len)
    print()



if __name__ == '__main__':
    main()

謝謝你的時間。

我想利用用戶管理的身份 (UMI) 對我的 SQL 服務器進行身份驗證。

正如@Ondrej 在這里所建議的那樣

目前,Azure SQL 的服務器標識不支持用戶分配的托管標識 (UMI)

基於MS DOC

The ODBC Driver on Linux and macOS before version 17.6 only supports Azure Active Directory authentication directly against Azure Active Directory. 如果您從 Linux 或 macOS 客戶端使用 Azure Active Directory 用戶名/密碼身份驗證,並且您的 Active Directory 配置要求客戶端針對 Active Directory 聯合服務端點進行身份驗證,則身份驗證可能會失敗。 從驅動程序版本 17.6 開始,此限制已被刪除。

如果嘗試使用訪問令牌進行身份驗證:

ODBC 驅動程序版本 13.1 僅支持 Windows 上的此身份驗證。

@AjayKumarGhose-MT

使用 Microsoft Q&A 后收到來自 Microsoft 的以下信息:

感謝您使用微軟問答,。 You are getting this error as you cannot use either user assigned identity or >system assigned managed identity to access from SQL server from you local >environment as these identities are meant for accessing Azure AD protected >resources from other Azure services like Azure functions, web應用程序等

有關詳細信息,請參閱托管身份文檔。 您需要在代碼中提供用戶名和密碼,或者您可以 > 將它們存儲為環境變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM