簡體   English   中英

使用ARM輸出或PowerShell獲取Azure功能默認密鑰的方法

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

我正在嘗試為Azure Function應用程序設置集成測試。 部署進展順利,但我需要一種以編程方式獲取默認密鑰來運行集成測試的方法。

我已經嘗試了這里鏈接的內容 - 在Powershell中獲取Azure功能的功能和主機密鑰 - 但無法獲得在我的ARM部署模板中工作的listsecrets。 Listsecrets無法識別。

有誰知道如何使用ARM模板和/或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')]"
    }
  }
}

輸出

Outputs屬性將包含一個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 }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM