Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to get Azure Function default key with ARM output or powershell

Tags:

I'm attempting to set up integration testing for an Azure Function app. Deployment is going fine, but I need a way to programatically get the default key to run my integration tests.

I've tried what is linked here - Get Function & Host Keys of Azure Function In Powershell - but cannot get the listsecrets working in my ARM deployment template. Listsecrets is not recognized.

Does anyone know how to get this key with an ARM template and/or powershell?

like image 893
krb224 Avatar asked Oct 13 '17 18:10

krb224


1 Answers

I ended up being able to run an Azure Powershell script in a VSTS task and output the variable to a build key. I am attaching the script so others can use.

#Requires -Version 3.0

Param(
    [string] [Parameter(Mandatory=$true)] $ResourceGroup,
    [string] [Parameter(Mandatory=$true)] $FunctionAppName
)

$content = Get-AzureRmWebAppPublishingProfile -ResourceGroupName $ResourceGroup -Name $FunctionAppName -OutputFile creds.xml -Format WebDeploy
$username = Select-Xml -Content $content -XPath "//publishProfile[@publishMethod='MSDeploy']/@userName"
$password = Select-Xml -Content $content -XPath "//publishProfile[@publishMethod='MSDeploy']/@userPWD"
$accessToken = "Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password)))

$masterApiUrl = "https://$FunctionAppName.scm.azurewebsites.net/api/functions/admin/masterkey"
$masterKeyResult = Invoke-RestMethod -Uri $masterApiUrl -Headers @{"Authorization"=$accessToken;"If-Match"="*"}
$masterKey = $masterKeyResult.Masterkey

$functionApiUrl = "https://$FunctionAppName.azurewebsites.net/admin/host/keys?code=$masterKey"
$functionApiResult = Invoke-WebRequest -UseBasicParsing -Uri $functionApiUrl
$keysCode = $functionApiResult.Content | ConvertFrom-Json
$functionKey = $keysCode.Keys[0].Value

$saveString = "##vso[task.setvariable variable=FunctionAppKey;]{0}" -f $functionKey

Write-Host ("Writing: {0}" -f $saveString)
Write-Output ("{0}" -f $saveString)
like image 138
krb224 Avatar answered Oct 11 '22 12:10

krb224