listKeys for Azure function app
Asked Answered
A

3

6

How do I list the keys of an azure function app using the listKeys ARM function?

My template:

 {
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {},
    "resources": [],
    "outputs": {
        "referenceOutput": {
            "type": "object",
            "value": "[listkeys(resourceId('Microsoft.Web/sites/functions', 'my-function-app','my-function'),'2016-08-01').key]"
        }
    }

And then run with:

az group deployment create -g my-rg --template-file ./arm.json --mode incremental

Error:

No route registered for '/api/functions/my-function/listkeys?api-version=2016-08-01'
Adda answered 27/12, 2017 at 1:36 Comment(0)
L
11

Try following template.

  "outputs": {
    "FunctionAppName": {
        "type": "string",
        "value": "[parameters('functionName')]"
    },
    "Key": {
        "type": "string",
        "value": "[listsecrets(resourceId('Microsoft.Web/sites/functions', parameters('existingFunctionAppName'), parameters('functionName')),'2015-08-01').key]"
    },        
    "Url": {
        "type": "string",
        "value": "[listsecrets(resourceId('Microsoft.Web/sites/functions', parameters('existingFunctionAppName'), parameters('functionName')),'2015-08-01').trigger_url]"
    }        
}

More information about this please refer to this question.

You also could use Function App Api to list this.

GET /admin/functions/{functionname}/keys

If you use bash shell, you could use the following example.

TENANT=""
CLIENT_ID=""
CLIENT_SECRET=""
SUBSCRIPTION_ID=""
RESOURCE_GROUP="shuiapp"
FUNCTION_APP_NAME="shuifunction"
API_URL="https://$FUNCTION_APP_NAME.scm.azurewebsites.net/api/functions/admin/token"
SITE_URL="https://$FUNCTION_APP_NAME.azurewebsites.net/admin/functions/HttpTriggerPowerShell1/keys"

### Grab a fresh bearer access token.
ACCESS_TOKEN=$(curl -s -X POST -F grant_type=client_credentials -F resource=https://management.azure.com/ -F client_id=$CLIENT_ID -F client_secret=$CLIENT_SECRET https://login.microsoftonline.com/$TENANT/oauth2/token | jq '.access_token' -r)

### Grab the publish data for the Funciton App and output it to an XML file.
PUBLISH_DATA=$(curl -s -X POST -H "Content-Length: 0" -H "Authorization: Bearer $ACCESS_TOKEN" https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.Web/sites/$FUNCTION_APP_NAME/publishxml?api-version=2016-08-01)
echo $PUBLISH_DATA > publish_data.xml

### Grab the Kudu username and password from the publish data XML file.
USER_NAME=$(xmlstarlet sel -t -v "//publishProfile[@profileName='$FUNCTION_APP_NAME - Web Deploy']/@userName" publish_data.xml)
USER_PASSWORD=$(xmlstarlet sel -t -v "//publishProfile[@profileName='$FUNCTION_APP_NAME - Web Deploy']/@userPWD" publish_data.xml)

### Generate a JWT that can be used with the Functions Key API.
JWT=$(curl -s -X GET -u $USER_NAME:$USER_PASSWORD $API_URL | tr -d '"')

### Grab the '_master' key from the Functions Key API.
KEY=$(curl -s -X GET -H "Authorization: Bearer $JWT" $SITE_URL | jq -r '.value')
Licence answered 27/12, 2017 at 1:57 Comment(3)
If you use bash shell script, you need install jq and xmlstarlet firstly. apt install jq apt install xmlstarletLicence
Sorry, I am not test template, I test the linux script, it works for me. Hope it helps.Licence
Thanks, the issue was that I was using listKeys, not listSecretsAdda
D
2

This approach for retrieving keys appears to be an issue right now per an announcement on Azure Wiki.

I have had luck using a scripted version that is built around a call similar to this:

az rest --method post --uri "/subscriptions/%subscriptionId%/resourceGroups/%resourceGroup%/providers/Microsoft.Web/sites/%webAppName%/host/default/listKeys?api-version=2018-11-01" --query functionKeys.default --output tsv

The original blog post that I got this info from is here.

Doubletime answered 24/7, 2019 at 15:17 Comment(0)
A
0

This question is a bit old and the author asks for retrieving the function keys with an ARM template. Although my solution doesn't use ARM templates, I found this service from Microsoft helpful in retrieving your function keys as long as you are logged in with your Azure DevOps account.

https://learn.microsoft.com/en-us/rest/api/appservice/web-apps/list-function-keys

In case you need the host keys, you can use this other service:

https://learn.microsoft.com/en-us/rest/api/appservice/web-apps/list-host-keys

Advisee answered 13/11, 2021 at 0:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.