通过CLI获取功能应用程序的默认主机密钥

时间:2017-12-08 11:57:01

标签: azure-functions azure-cli

我使用 az cli ,我需要检索给定功能应用的默认主机密钥

我在az functionapp下尝试了各种选项,但输出似乎都没有包含密钥。

例如,下面输出大量有关功能应用程序的信息,但不输出任何主机或功能键。

az functionapp show --resource-group my-resource-group --name my-function-app

2 个答案:

答案 0 :(得分:1)

我需要Linux兼容的解决方案,因此PowerShell是不可能的。但是,我能够翻译Working example中提供的解决方案。

TENANT="XXX"
CLIENT_ID="XXX"
CLIENT_SECRET="XXX"
SUBSCRIPTION_ID="XXX"
RESOURCE_GROUP="my-resource-group"
FUNCTION_APP_NAME="my-function-app"
API_URL="https://$FUNCTION_APP_NAME.scm.azurewebsites.net/api/functions/admin/token"
SITE_URL="https://$FUNCTION_APP_NAME.azurewebsites.net/admin/host/systemkeys/_master"

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

答案 1 :(得分:0)

FOR /F "tokens=* USEBACKQ" %%F IN (`CALL az rest --method post --uri "/subscriptions/{your 
subscriptionId}/resourceGroups/{yourresourcegroup}/providers/Microsoft.Web/sites/{yourfunctionname}/host/default/listKeys?api-version=2018-11-01" --query functionKeys.default --output tsv`) DO (
SET apikey=%%F
)
echo ##vso[task.setvariable variable=functionhostKey;]%apikey%

apikey获取值并将其放入变量functionhostKey