【问题标题】:Get Function & Host Keys of Azure Function In Powershell在 Powershell 中获取 Azure 函数的函数和主机密钥
【发布时间】:2017-09-01 09:10:18
【问题描述】:

我已经使用 Arm 模板部署了 Azure 功能。我需要在 Powershell 中部署 Azure Function 的 Function Key 和 host Key。 目前我正在尝试从 ARM 模板的输出部分获取密钥

 "outputs": {
"FunctionAppName": {
  "type": "string",
  "value": "[variables('functionAppName')]"
},
"Key": {
  "type": "string",
  "value": "[listKeys(resourceId('Microsoft.Web/sites', '[variables('functionAppName')]'),'2015-08-01').keys]"
}

}

我尝试了不同的组合,但失败了。 有没有办法在 Powershell 中检索密钥?

【问题讨论】:

  • 是的。它将错误请求作为错误抛出

标签: powershell azure


【解决方案1】:

我使用以下方法让它工作:

    "outputs": {
    "FunctionAppName": {
        "type": "string",
        "value": "[parameters('functionName')]"
    },
    "Key": {
        "type": "string",
        "value": "[listsecrets(resourceId('Microsoft.Web/sites/functions', parameters('existingFunctionAppName'), parameters('functionName')),'2015-08-01').key]"
    },        
    "Url": {
        "type": "string",
        "value": "[listsecrets(resourceId('Microsoft.Web/sites/functions', parameters('existingFunctionAppName'), parameters('functionName')),'2015-08-01').trigger_url]"
    }        
}

我也找不到任何示例。 但是通过使用上述内容、GitHub 的快速入门示例和resource functions 的文档以及一些试验和错误,我得到了它。

请注意变量/参数和名称已更改。

【讨论】:

  • ARM 模板如何知道函数应用中存在哪些函数?如果代码在 ARM 模板运行后才部署(以便将其部署到某个地方)?
  • @DanielEarwicker 在函数的服务级别设置了一个“默认”主机级别密钥。这不会给您返回单个函数的密钥,而只是提供通用主机级别的密钥(适用于应用程序中的所有函数)。
  • @technophile 太棒了,我知道Function-level auth 意味着您必须为每个函数使用不同的密钥。
  • @technophile - 好吧,结果不是那么棒。不仅 listsecrets 似乎没有记录,而且someone claims here 也表示“这种方法有一个限制。它只能访问单个功能键,不能访问主机密钥或主密钥。”
  • 您需要切换到listkeys('function-resource-id', '2018-11-01') 以获取特定于功能的键。请参阅Robb's answer 获取主机密钥(当尚未/单独部署 if 函数时)
【解决方案2】:

我无法获得接受的答案来检索默认主机密钥。 @4c74356b41 的答案非常接近。您可以使用下面的代码取出钥匙。默认主机密钥位于Outputs.functionKeys.Value.functionKeys.default.Value

  "outputs": {
    "functionKeys": {
      "type": "object",
      "value": "[listkeys(concat(resourceId('Microsoft.Web/sites', variables('functionAppName')), '/host/default'), '2018-11-01')]"
    }
  }

【讨论】:

  • 值得注意的是,这是 V2 运行时的正确答案,而 V1 使用了listsecrets。这让我在今年早些时候疯狂了一段时间,尤其是当我第一次尝试这样做时,API 版本似乎还没有发布。
  • 这里缺少右括号。
  • 任性的括号放回原位
【解决方案3】:

问题似乎没有得到回答,因为它要求从 Powershell 而不是 ARM 模板中获取功能键。 我正在使用下面的脚本从 Azure DevOps 中的 Powershell 获取功能键。

$accountInfo = az account show
$accountInfoObject = $accountInfo | ConvertFrom-Json
$subscriptionId  = $accountInfoObject.id

$resourceGroup = "your-resource-group"
$functionName = "your-function-name"

$functionkeylist = az rest --method post --uri "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.Web/sites/$functionName/host/default/listKeys?api-version=2018-11-01"
$keylistobject = $functionkeylist | ConvertFrom-Json
$functionKey = $keylistobject.functionKeys.default

希望这会有所帮助。

【讨论】:

  • 另请参阅此处stackoverflow.com/a/62058134,了解我见过的最干净的 Az Powershell 单线器Get-AzResource -Name RESOURCE-NAME | Invoke-AzResourceAction -Action host/default/listkeys -Force
【解决方案4】:

首先,你的语法有错误:

  "value": "[listKeys(resourceId('Microsoft.Web/sites', variables('functionAppName')),'2015-08-01').keys]"

但这无济于事,我不认为它是为 Azure Functions 实现的,我对此不是 100% 确定,但我检索密钥的努力是徒劳的

【讨论】:

    【解决方案5】:

    因此,要使此功能适用于应用程序 MyFunctionApp 中的 MyHttpFunction 的功能特定键,我必须在 ARM 模板的 Outputs 部分中使用以下内容:

    "MyHttpFunctionKey": {
        "type": "string",
        "value": "[listkeys(resourceId('Microsoft.Web/sites/functions', 'MyFunctionApp', 'MyHttpFunction'), '2019-08-01').default]"
    }
    

    如果使用 New-AzResourceGroupDeployment 和参数 -OutVariable arm 从 Powershell 调用,则以下 Powershell 命令将打印密钥:$arm.Outputs.myHttpFunctionKey.Value

    【讨论】:

      【解决方案6】:

      以下代码将获得字符串格式的确切密钥,我使用此密钥创建可用性测试。

      "outputs": {
            "Key":{
                   "type": "string", 
                   "value": "[listkeys(concat(resourceId('Microsoft.Web/sites', 'functionAppName'), '/functions', '/FunctionName'), '2018-11-01').default]"    
                   }
                  }
      

      【讨论】:

        【解决方案7】:

        使用 Azure PowerShell

        我想提供另一种解决此问题的方法,即使用尽可能接近纯 Azure PowerShell 的方法。它仍然依赖于编写 Azure“操作”,但只需几行代码即可完成。

        注意:这假设您有一个已通过身份验证的 PowerShell 会话。如果您没有看到:Connect-AzAccount 了解更多信息。

        选项 1 - 检索功能应用的密钥以用于所有功能

        本示例基于此操作:Web Apps - List Host Keys 并使用此 PowerShell cmdlet 执行操作:Invoke-AzRestMethod

        ## lookup the resource id for your Azure Function App ##
        $resourceId = (Get-AzResource -ResourceGroupName $rg -ResourceName $functionAppName -ResourceType "Microsoft.Web/sites").ResourceId
        
        ## compose the operation path for listing keys ##
        $path = "$resourceId/host/default/listkeys?api-version=2021-02-01"
        
        ## invoke the operation ##
        $result = Invoke-AzRestMethod -Path $urlPath -Method POST
        if($result -and $result.StatusCode -eq 200)
        {
           ## Retrieve result from Content body as a JSON object ##
           $contentBody = $result.Content | ConvertFrom-Json
        
           ## Output the default function key. In reality you would do something more ##
           ## meaningful with this ##
           Write-Host $contentBody.functionKeys.default
        }
        

        选项 2 - 检索特定功能的密钥

        这个例子是基于这个操作来检索一个特定于函数的键。这通常是更好的做法,这样您就不会拥有所有功能的一键式。但是,您可能需要任何一个都有正当的理由。在此处查看此操作:Web Apps - List Function Keys

        ## Lookup function name here ##
        $functionName = "MyFunction"
        
        ## lookup the resource id for your Azure Function App ##
        $resourceId = (Get-AzResource -ResourceGroupName $rg -ResourceName $functionAppName -ResourceType "Microsoft.Web/sites").ResourceId
        
        ## compose the operation path for listing keys ##
        $path = "$resourceId/functions/$functionName/listkeys?api-version=2021-02-01"
        
        ## invoke the operation ##
        $result = Invoke-AzRestMethod -Path $urlPath -Method POST
        if($result -and $result.StatusCode -eq 200)
        {
           ## Retrieve result from Content body as a JSON object ##
           $contentBody = $result.Content | ConvertFrom-Json
        
           ## Output the default function key. In reality you would do something more ##
           ## meaningful with this. ##
           Write-Host $contentBody.default
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-24
          • 2021-02-19
          • 2022-11-19
          • 2019-03-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多