从Azure功能获取托管身份访问令牌时出错

时间:2020-06-18 17:07:00

标签: python azure azure-functions azure-managed-identity

从我的功能应用程序检索Azure托管身份访问令牌时遇到问题。该函数获取令牌,然后使用该令牌作为密码访问Mysql数据库。

我从函数中得到以下响应:

9103 (HY000): An error occurred while validating the access token. Please acquire a new token and retry.

代码:

import logging
import mysql.connector
import requests
import azure.functions as func


def main(req: func.HttpRequest) -> func.HttpResponse:
    
    def get_access_token():

        URL = "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fossrdbms-aad.database.windows.net&client_id=<client_id>"
        headers = {"Metadata":"true"}

        try:
            req = requests.get(URL, headers=headers)
        except Exception as e:
            print(str(e))
            return str(e)
        else:
            password = req.json()["access_token"]

        return password

    def get_mysql_connection(password):
        """
        Get a Mysql Connection.
        """
        try:
            con = mysql.connector.connect(

            host='<host>.mysql.database.azure.com', 
            user='<user>@<db>',
            password=password,
            database = 'materials_db',
            auth_plugin='mysql_clear_password'
            )
        except Exception as e:

            print(str(e))
            return str(e)

        else:
            return "Connected to DB!"

    password = get_access_token()

    return func.HttpResponse(get_mysql_connection(password))

使用我的托管身份在VM上运行此代码的修改后的版本有效。似乎不允许功能应用程序获取访问令牌。任何帮助将不胜感激。

注意:我以前以AzureAD管理器身份登录到数据库,并创建了具有该数据库所有特权的用户。

编辑:不再为VM调用端点。

def get_access_token():

    identity_endpoint = os.environ["IDENTITY_ENDPOINT"] # Env var provided by Azure. Local to service doing the requesting.
    identity_header = os.environ["IDENTITY_HEADER"] # Env var provided by Azure. Local to service doing the requesting.
    api_version = "2019-08-01" # "2018-02-01" #"2019-03-01" #"2019-08-01"
    CLIENT_ID = "<client_id>"
    resource_requested = "https%3A%2F%2Fossrdbms-aad.database.windows.net"
    # resource_requested = "https://ossrdbms-aad.database.windows.net"


    URL = f"{identity_endpoint}?api-version={api_version}&resource={resource_requested}&client_id={CLIENT_ID}"
    headers = {"X-IDENTITY-HEADER":identity_header}

    try:
        req = requests.get(URL, headers=headers)
    except Exception as e:
        print(str(e))
        return str(e)
    else:
        try:
            password = req.json()["access_token"]
        except:
            password = str(req.text)

    return password

但是现在我收到此错误:

{"error":{"code":"UnsupportedApiVersion","message":"The HTTP resource that matches the request URI 'http://localhost:8081/msi/token?api-version=2019-08-01&resource=https%3A%2F%2Fossrdbms-aad.database.windows.net&client_id=<client_idxxxxx>' does not support the API version '2019-08-01'.","innerError":null}}

经检查,这似乎是一个普遍错误。即使不是根本问题,此错误消息也会传播。在Github中被多次提及。

我的端点现在正确吗?

2 个答案:

答案 0 :(得分:1)

对于此问题,是由您请求访问令牌的端点错误引起的。我们只能在azure VM中使用端点http://169.254.169.254/metadata/identity.....,但是如果在azure函数中,我们将无法使用它。

在azure函数中,我们需要从环境中获取IDENTITY_ENDPOINT

identity_endpoint = os.environ["IDENTITY_ENDPOINT"]

端点就像:

http://127.0.0.1:xxxxx/MSI/token/

您可以参考此tutorial,还可以在本教程中找到python代码示例。 enter image description here

在我的功能代码中,我还添加了在token_auth_uri中创建的受管身份的客户端ID,但是我不确定此处是否需要client_id(在我的情况下,我使用用户分配的身份,而不是系统分配的身份。

token_auth_uri = f"{identity_endpoint}?resource={resource_uri}&api-version=2019-08-01&client_id={client_id}"

更新

#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    string resource="https://ossrdbms-aad.database.windows.net";
    string clientId="xxxxxxxx";
    log.LogInformation("C# HTTP trigger function processed a request.");
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(String.Format("{0}/?resource={1}&api-version=2019-08-01&client_id={2}", Environment.GetEnvironmentVariable("IDENTITY_ENDPOINT"), resource,clientId));
    request.Headers["X-IDENTITY-HEADER"] = Environment.GetEnvironmentVariable("IDENTITY_HEADER");
    request.Method = "GET";

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    StreamReader streamResponse = new StreamReader(response.GetResponseStream());
    string stringResponse = streamResponse.ReadToEnd();
    log.LogInformation("test:"+stringResponse);

    string name = req.Query["name"];

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    name = name ?? data?.name;

    return name != null
        ? (ActionResult)new OkObjectResult($"Hello, {name}")
        : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}

答案 1 :(得分:0)

对于您的最新一期,您看到的是UnsupportedApiVersion,可能是以下问题:https://github.com/MicrosoftDocs/azure-docs/issues/53726

以下几种对我有用的选项:

我假设您正在Linux上托管Function应用。我注意到ApiVersion 2017-09-01可以使用,但是您需要进行其他更改(而不是“ X-IDENTITY-HEADER”,请使用“ secret”头)。并为您的功能应用程序使用系统分配的托管身份,而不是用户分配的身份。

当我在Windows上托管功能应用程序时,我没有遇到同样的问题。因此,如果要使用用户分配的托管身份,可以尝试使用此选项。 (带有api-version = 2019-08-01和X-IDENTITY-HEADER。