使用 ARM 输出或 powershell 获取 Azure Function 默认密钥的方法
Way to get Azure Function default key with ARM output or powershell
我正在尝试为 Azure Functions 应用设置集成测试。部署进展顺利,但我需要一种方法来以编程方式获取 运行 我的集成测试的默认密钥。
我已经尝试了此处链接的内容 - - 但无法在我的 ARM 部署模板中使用 listsecrets。无法识别 Listsecrets。
有谁知道如何使用 ARM 模板 and/or powershell 获取此密钥?
我最终能够 运行 VSTS 任务中的 Azure Powershell 脚本并将变量输出到构建密钥。我附上脚本以便其他人可以使用。
#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)
更新 Microsoft 的 ARM API 后,现在可以直接从 ARM 部署输出中检索 Azure 功能密钥。
例子
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"appServiceName": {
"type": "string"
}
},
"variables": {
"appServiceId": "[resourceId('Microsoft.Web/sites', parameters('appServiceName'))]"
},
//... implementation omitted
"outputs": {
"functionKeys": {
"type": "object",
"value": "[listkeys(concat(variables('appServiceId'), '/host/default'), '2018-11-01')]"
}
}
}
产出
输出 属性 将包含一个 Newtonsoft.Json.Linq.JObject
条目,其中包含 Azure 函数的所有键,即主键、系统键和功能键(包括默认键)。不幸的是,结合部署变量类型的 JObject 有点难以理解,您应该被警告,它区分大小写。 (如果你在 PowerShell 中工作,它可以被按摩到 hashtables
以供消费。请参阅下面的奖励。)
$results = New-AzResourceGroupDeployment...
$keys = results.Outputs.functionKeys.Value.functionKeys.default.Value
奖金
下面的代码去掉了额外的 .Value
调用。
function Convert-OutputsToHashtable {
param (
[ValidateNotNull()]
[object]$Outputs
)
$Outputs.GetEnumerator() | ForEach-Object { $ht = @{} } {
if ($_.Value.Value -is [Newtonsoft.Json.Linq.JObject]) {
$ht[$_.Key] = ConvertFrom-Json $_.Value.Value.ToString() -AsHashtable
} else {
$ht[$_.Key] = $_.Value.Value
}
} { $ht }
}
我正在尝试为 Azure Functions 应用设置集成测试。部署进展顺利,但我需要一种方法来以编程方式获取 运行 我的集成测试的默认密钥。
我已经尝试了此处链接的内容 -
有谁知道如何使用 ARM 模板 and/or powershell 获取此密钥?
我最终能够 运行 VSTS 任务中的 Azure Powershell 脚本并将变量输出到构建密钥。我附上脚本以便其他人可以使用。
#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)
更新 Microsoft 的 ARM API 后,现在可以直接从 ARM 部署输出中检索 Azure 功能密钥。
例子
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"appServiceName": {
"type": "string"
}
},
"variables": {
"appServiceId": "[resourceId('Microsoft.Web/sites', parameters('appServiceName'))]"
},
//... implementation omitted
"outputs": {
"functionKeys": {
"type": "object",
"value": "[listkeys(concat(variables('appServiceId'), '/host/default'), '2018-11-01')]"
}
}
}
产出
输出 属性 将包含一个 Newtonsoft.Json.Linq.JObject
条目,其中包含 Azure 函数的所有键,即主键、系统键和功能键(包括默认键)。不幸的是,结合部署变量类型的 JObject 有点难以理解,您应该被警告,它区分大小写。 (如果你在 PowerShell 中工作,它可以被按摩到 hashtables
以供消费。请参阅下面的奖励。)
$results = New-AzResourceGroupDeployment...
$keys = results.Outputs.functionKeys.Value.functionKeys.default.Value
奖金
下面的代码去掉了额外的 .Value
调用。
function Convert-OutputsToHashtable {
param (
[ValidateNotNull()]
[object]$Outputs
)
$Outputs.GetEnumerator() | ForEach-Object { $ht = @{} } {
if ($_.Value.Value -is [Newtonsoft.Json.Linq.JObject]) {
$ht[$_.Key] = ConvertFrom-Json $_.Value.Value.ToString() -AsHashtable
} else {
$ht[$_.Key] = $_.Value.Value
}
} { $ht }
}