首页 > 解决方案 > 如何在函数设置为只读时创建 Azure 函数、函数键

问题描述

摘要:我直接在 Visual Studio 中创作了一个函数,它通过设计导致门户中的只读函数管理。我的问题是如何轻松地为所述 webhook 创建功能键?

上下文:我正在尝试将通用 webhook 连接到事件网格。这个过程让我走上了一条需要触发 SubscriptionValidationEvent 的道路,这反过来又要求我的 webhook 在 URL 上提供一个“代码”,我假设它是一个功能键。

同样在我被否决之前,我很清楚这个问题的多种变体已经在这里提出和回答。我已经尝试了所有这些方法,但出于某种原因,涉及使用 Kudu 凭据针对文档记录不佳的 Keys API 编写 PowerShell 的解决方案似乎都不适合我。

我希望有人知道用 CLI 解决这个问题的方法,或者更简单的方法,functionName.json手动创建一个文件并将其放到 secrets 目录中。

最后,尽管使用预发布 EventGrid 绑定对我来说很诱人,但我目前无法在我的环境中推送预发布代码。

标签: azureazure-functionsazure-functions-runtimeazure-eventgridazure-functions-core-tools

解决方案


发现这篇关于如何管理 azure 功能键的有趣文章Powershell

还有官方文档(很难找到这个 wiki):

以下是关键点:

  1. 获取发布凭据
  2. 生成 Kudu API 授权令牌
  3. 调用 Kudu /api/functions/admin/token 获取可与 Functions Key API 配合使用的 JWT
  4. 然后你可以做任何你想做的事

这是我现有的脚本

    Param(
    [string] [Parameter(Mandatory=$true)] $resourceGroupName,
    [string] [Parameter(Mandatory=$true)] $functionappName,
    [string] [Parameter(Mandatory=$true)] $keyname,
    [string] [Parameter()] $slot
)

if (![string]::IsNullOrWhiteSpace($slot)){
    $apiBaseUrl = "https://$functionappName-$slot.scm.azurewebsites.net/api"
    $siteBaseUrl = "https://$functionappName-$slot.azurewebsites.net"
    $resourceType = "Microsoft.Web/sites/slots/config"
    $resourceName = "$functionappName/$slot/publishingcredentials"
}
else {
    $apiBaseUrl = "https://$functionappName.scm.azurewebsites.net/api"
    $siteBaseUrl = "https://$functionappName.azurewebsites.net"
    $resourceType = "Microsoft.Web/sites/config"
    $resourceName = "$functionappName/publishingcredentials"
}

Write-Host "Get the publishing credentials"
$publishingCredentials = Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName -ResourceType $resourceType -ResourceName $resourceName -Action list -ApiVersion 2015-08-01 -Force

Write-Host "Generate the Kudu API Authorisation Token"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName, $publishingCredentials.Properties.PublishingPassword)))

Write-Host "Call Kudu /api/functions/admin/token to get a JWT that can be used with the Functions Key API"
$jwt = Invoke-RestMethod -Uri "$apiBaseUrl/functions/admin/token" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method GET

Write-Host "Creates or updates an host key at the specified resource with an auto generated key"
$mynewkey = (Invoke-RestMethod -Uri "$siteBaseUrl/admin/host/keys/$keyname" -Headers @{Authorization=("Bearer {0}" -f $jwt)} -Method Post).value

编辑

新创建的函数应用默认使用 TLS 1.2,因此您需要在 Powershell 脚本的顶部添加此行:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

推荐阅读