Skip to content

Instantly share code, notes, and snippets.

@flacito
Last active May 22, 2019 03:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flacito/4bbbe2c8f02d65b0f14d7a3439879d4c to your computer and use it in GitHub Desktop.
Save flacito/4bbbe2c8f02d65b0f14d7a3439879d4c to your computer and use it in GitHub Desktop.
Two days and 5 lines of code to get the Azure Functions master code
#!/bin/bash
USER_NAME=$(az webapp deployment list-publishing-profiles -n your-functions-app -g yourfunctionsrg --query "[?publishMethod=='MSDeploy'].userName" --output tsv)
USER_PASSWORD=$(az webapp deployment list-publishing-profiles -n your-functions -g yourfunctionsrg --query "[?publishMethod=='MSDeploy'].userPWD" --output tsv)
BEARER_TOKEN=$(curl -u ${USER_NAME}:${USER_PASSWORD} -sb -X GET https://your-functions-app.scm.azurewebsites.net/api/functions/admin/token | sed 's/.\(.*\)/\1/' | sed 's/\(.*\)./\1/')
MASTER_CODE=$(curl -sb -X GET -H "Authorization: Bearer ${BEARER_TOKEN}" https://your-functions-app.azurewebsites.net/admin/host/systemkeys/_master | jq '.value' | sed 's/.\(.*\)/\1/' | sed 's/\(.*\)./\1/')
echo $MASTER_CODE
@GBSmulders
Copy link

Hi, thanks for sharing!
I used and updated your script to set a predefined host key and documented it here: https://gsmulders.nl/2018/11/08/azure-how-to-add-a-host-key-to-your-azure-function-app-using-a-script/

@Dave76
Copy link

Dave76 commented Dec 4, 2018

Thanks so much for sharing. Not sure why there is not a method built into the CLI to get at the keys. We have it for all the other resource types.

@iyerusad
Copy link

Az Powershell edition (for use in VSTS windows agent)

$RSGROUP="myrsgroup"
$WEBAPPNAME="myfunctionapp"

$DeploymentUrl = Get-AzWebAppContainerContinuousDeploymentUrl -ResourceGroupName $RSGROUP -Name $WEBAPPNAME

$userpass = $DeploymentUrl.split("@")[0].Replace("https://","")
$kuduCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($userpass))

$jwt = Invoke-RestMethod -Uri "https://$WEBAPPNAME.scm.azurewebsites.net/api/functions/admin/token" -Headers @{Authorization=("Basic {0}" -f $kuduCreds)} -Method GET

$keyURL="https://$WEBAPPNAME.azurewebsites.net/admin/host/systemkeys/_master"

$masterkey=(Invoke-RestMethod $keyURL -Headers @{Authorization="Bearer $jwt"}).value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment