【问题标题】:Way to get Azure Function default key with ARM output or powershell使用 ARM 输出或 powershell 获取 Azure Function 默认密钥的方法
【发布时间】:2018-03-25 23:00:49
【问题描述】:

我正在尝试为 Azure Function 应用设置集成测试。部署进展顺利,但我需要一种以编程方式获取默认密钥的方法来运行我的集成测试。

我已经尝试了此处链接的内容 - Get Function & Host Keys of Azure Function In Powershell - 但无法让 listsecrets 在我的 ARM 部署模板中工作。无法识别 Listsecrets。

有人知道如何使用 ARM 模板和/或 powershell 获取此密钥吗?

【问题讨论】:

    标签: powershell azure azure-functions


    【解决方案1】:

    我最终能够在 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)
    

    【讨论】:

      【解决方案2】:

      更新 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 }
      
      }
      

      【讨论】:

      • 而我只需要通过六个 GitHub 问题来追踪我的方式,就可以弄清楚如何去做。
      猜你喜欢
      • 2021-03-17
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      • 2019-03-31
      • 2023-03-08
      • 1970-01-01
      • 2022-11-19
      • 1970-01-01
      相关资源
      最近更新 更多