简体   繁体   中英

Azure Functions (python) adal authentication timeout

I am trying out Azure Functions with Python (linux app service plan). I have written a basic code that will authenticate my function with Azure. It uses Service principal details (set in AppSettings) via ADAL authentication. I have deployed the function via Visual Studio Code. When I run the function it runs forever, and gives me a '503' error.

As you can see the code is pretty simple, it just does an authentication. I want to know why it is running for 5 minutes and how to fix it. Am I missing anything that is causing this issue.

Code:

import logging
import os, adal
import azure.functions as func
from azure.mgmt.resource import SubscriptionClient
from msrestazure.azure_active_directory import AdalAuthentication
from msrestazure.azure_cloud import AZURE_PUBLIC_CLOUD


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    
    # Retrieve the IDs and secret to use with ServicePrincipalCredentials 
    logging.info('Logging environment Variables...')
    client_id = os.environ["AZURE_CLIENT_ID"]
    client_secret = os.environ["AZURE_CLIENT_SECRET"]
    client_subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
    client_tenant_id = os.environ["AZURE_TENANT_ID"]
    
    LOGIN_ENDPOINT = AZURE_PUBLIC_CLOUD.endpoints.active_directory
    logging.info(f'login endoint: {LOGIN_ENDPOINT}')
    
    RESOURCE = AZURE_PUBLIC_CLOUD.endpoints.active_directory_resource_id
    logging.info(f'resource: {RESOURCE}')
    
    context = adal.AuthenticationContext(LOGIN_ENDPOINT + '/' + client_tenant_id)
    
    credential = AdalAuthentication(context.acquire_token_with_client_credentials, RESOURCE, client_id, client_secret)
    subscription_client = SubscriptionClient(credential)
    
    subscription = next(subscription_client.subscriptions.list())
    logging.info(f'Client ID: {client_id}, Client Subscription ID: {subscription}')
    
    #return func.HttpResponse(f'Client ID: {client_id}, Client Subscription ID: {subscription}')
    return func.HttpResponse(f'Client ID: {client_id}')

Error:

Timeout value of 00:30:00 exceeded by function 'Functions.StorageAccountDeployer' (Id: '2ecd0e1e-ef17-44fc-a1d7-d0134b9f9e3e'). Initiating cancellation.

Take a look on Azure AD if you've granted the permissions for the Service Principal to retrieve the token.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM