1
votes

I'm having an issue retrieving an Azure Managed Identity access token from my Function App. The function gets a token then accesses a Mysql database using that token as the password.

I am getting this response from the function:

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

Code:

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))

Running a modified version of this code on a VM with my managed identity works. It seems that the Function App is not allowed to get an access token. Any help would be appreciated.

Note: I have previously logged in as AzureAD Manager to the DB and created this user with all privileges to this DB.

Edit: No longer calling endpoint for VMs.

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

But now I am getting this Error:

{"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}}

Upon inspection this seems to be a general error. This error message is propagated even if it's not the underlying issue. Noted several times in Github.

Is my endpoint correct now?

2

2 Answers

1
votes

For this problem, it was caused by the wrong endpoint you request for the access token. We can just use the endpoint http://169.254.169.254/metadata/identity..... in azure VM, but if in azure function we can not use it.

In azure function, we need to get the IDENTITY_ENDPOINT from the environment.

identity_endpoint = os.environ["IDENTITY_ENDPOINT"]

The endpoint is like:

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

You can refer to this tutorial about it, you can also find the python code sample in the tutorial. enter image description here

In my function code, I also add the client id of the managed identity I created in the token_auth_uri but I'm not sure if the client_id is necessary here (In my case, I use user-assigned identity but not system-assigned identity).

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

Update:

#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");
}
0
votes

For your latest issue, where you are seeing UnsupportedApiVersion, it is probably this issue: https://github.com/MicrosoftDocs/azure-docs/issues/53726

Here are a couple of options that worked for me:

I am assuming you are hosting the Function app on Linux. I noticed that ApiVersion 2017-09-01 works, but you need to make additional changes (instead of "X-IDENTITY-HEADER", use "secret" header). And also use a system-assigned managed identity for your function app, and not a user assigned identity.

When I hosted the function app on Windows, I didn't have the same issues. So if you want to use an user-assigned managed identity, you can try this option instead. (with the api-version=2019-08-01, and X-IDENTITY-HEADER.