尝试使用MSI身份验证从Azure ML服务连接Azure SQL数据库(没有用户名和密码连接Azure数据库)

问题描述 投票:1回答:1

我正在尝试将Azure机器学习服务中的Azure SQL数据库MSI身份验证(没有用户名和密码)连接起来。

我正在尝试在azure机器学习服务上使用机器学习模型,目的是我需要数据,这就是为什么我想使用MSI身份验证从Azure机器学习服务连接Azure SQL数据库的原因。

但是我得到以下错误:-

 "error": {"message": "Activity Failed:\n{\n    \"error\": {\n        \"code\": \"UserError\",\n        \"message\": \"User program failed with KeyError: 'MSI_ENDPOINT'\",\n

请检查以下我用于数据库连接的代码。

import logging
import struct
import pyodbc
import os
import requests


class AzureDbConnect:
    def __init__(self):
        print("Inside msi database")
        msi_endpoint = os.environ["MSI_ENDPOINT"]
        msi_secret = os.environ["MSI_SECRET"]

        resource_uri = 'https://database.windows.net/'

        logging.info(msi_endpoint)
        print(msi_endpoint)
        logging.info(msi_secret)
        print(msi_secret)
        print("Inside token")

        token_auth_uri = f"{msi_endpoint}?resource={resource_uri}&api-version=2017-09-01"
        head_msi = {'Secret': msi_secret}
        resp = requests.get(token_auth_uri, headers=head_msi)
        access_token = resp.json()['access_token']
        logging.info(access_token)
        print("Token is :- ")
        print(access_token)

        accesstoken = bytes(access_token, 'utf-8')
        exptoken = b""
        for i in accesstoken:
            exptoken += bytes({i})
            exptoken += bytes(1)
        tokenstruct = struct.pack("=i", len(exptoken)) + exptoken

        conn = pyodbc.connect("Driver={ODBC Driver 17 for SQL Server};"
                              "Server=tcp:<Server Name>"
                              "1433;Database=<Database Name>",
                              attrs_before={1256: bytearray(tokenstruct)})

        print(conn)

        self.sql_db = conn.cursor()

是否可以通过MSI身份验证从Azure机器学习服务连接Azure,SQL数据库?] >>

我正在尝试使用MSI身份验证(没有用户名和密码)连接来自Azure机器学习服务的Azure SQL数据库。我正在尝试在azure机器上进行机器学习模型...

python python-3.x azure-sql-database azure-machine-learning-service azure-managed-identity
1个回答
0
投票

当前不支持从Azure ML连接Azure SQL DB的MSI身份验证,它将在将来添加。您通常可以与在数据库中设置服务主体有关,“附件”是获取此Azure ML设置的分步指南。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.