azure - 当函数设置为只读时如何创建 Azure 函数、函数键

标签 azure azure-functions azure-functions-runtime azure-eventgrid azure-functions-core-tools

摘要:我直接在 Visual Studio 中编写一个函数,该函数的设计结果是在门户中进行只读函数管理。我的问题是如何轻松为所述 webhook 创建功能键?

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

此外,在我投票之前,我非常清楚这里已经提出并回答了这个问题的多种变体。我已经尝试过所有这些方法,但出于某种原因,涉及使用 Kudu 凭据针对记录不充分的 Keys API 编写 PowerShell 的解决方案似乎都不适合我。

我希望有人知道使用 CLI 解决此问题的方法,甚至更简单,手动创建一个 functionName.json 文件并将其放入 Secrets 目录中。

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

最佳答案

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

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

以下是要点:

  1. 获取发布凭据
  2. 生成 Kudu API 授权 token
  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

关于azure - 当函数设置为只读时如何创建 Azure 函数、函数键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50246239/

相关文章:

Azure-一驱动器传输

ASP.net URL 重写到不同的域

azure - 如何持续将数据从本地 SQL 数据库迁移到 Azure SQL 数据库

azure - HTTP 基本身份验证和 Azure 函数

python - 单个Python脚本的Azure函数部署以及Azure Functions中requirements.txt的安装过程

Azure功能失败: ModuleNotFoundError: No module named _cffi_backend

Azure Function ARM 模板删除不属于模板文件的现有应用程序设置

azure - 基于自定义逻辑同时处理 Azure Blob 存储中的多个文件

azure - 如何使我的 Windows Azure 应用程序能够抵御 Azure 数据中心灾难性事件?

python-3.x - TimeTrigger Azure Function 在 Azure 门户中没有模块错误,但在 VSC 中运行良好