在部署时从 Azure Function 获取 Azure Function 密钥?

Tom*_*Tom 5 c# azure azure-functions

我正在使用SendGrid 绑定在 Azure Functions 中发送电子邮件。作为该电子邮件内容的一部分,我想包含一个指向Azure Functions 实例中的一种HTTP 方法的链接,以获取更多信息。我的所有 HTTP 功能都使用AuthorizationLevel.Function.

我已经看到了一种在 PowerShell 中从 ARM 和 Kudu 中抓取密钥的解决方案(还有这个),以及一种仅使用 ARM 输出密钥的解决方案,但是这些都依赖于我的 Azure Functions 没有的东西:对 ARM 的权限( Azure 资源管理)API。

我还找到了Azure Functions 主机密钥管理 API,它在本地完全按照我的需要工作,但是401 Unauthorized一旦部署了 Azure Functions ,我不知道如何通过。我可以使用_master功能键手动通过它,但是我又回到不知道如何在运行时获取该键的状态。

问题是:是否可以在运行时以某种方式从 Azure 函数主机获取 Azure 函数的密钥?我非常希望不需要 ARM 权限来做到这一点。

Rom*_*iss 7

尝试以下两个步骤:

  1. 获取主机主密钥:

    GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourcegroupName}/providers/Microsoft.Web/sites/{functionApp}/functions/admin/masterkey?api-version=2016-08-01
    
    Run Code Online (Sandbox Code Playgroud)
  2. 获取功能键:

    GET https://{functionApp}.azurewebsites.net/admin/functions/{functionName}/keys?code={masterKeyFromStep1}
    
    Run Code Online (Sandbox Code Playgroud)

来自步骤 2 的响应:

    {
      "keys": [
        {
          "name": "default",
          "value": "xxxxxxxxxxxxxxxxxxxxxx"
        }
      ],
      "links": [
        {
          "rel": "self",
          "href": "https://myFncApp.azurewebsites.net/admin/functions/myFunction/keys"
        }
      ]
 }
Run Code Online (Sandbox Code Playgroud)

更新:

请注意,第 1 步需要采用以下格式的授权标头:

Authorization: Bearer bearerToken
Run Code Online (Sandbox Code Playgroud)

其中可从 Azure Active Directory (AAD) 获取 BearerToken 字符串,请参阅示例的以下代码片段:

    private string AccessToken(string clientID)
    {
        string redirectUri = "https://login.live.com/oauth20_desktop.srf";
        authContext = new AuthenticationContext("https://login.windows.net/common/oauth2/authorize", TokenCache.DefaultShared);
        var ar = authContext.AcquireTokenAsync("https://management.azure.com/", clientID, new Uri(redirectUri), new PlatformParameters(PromptBehavior.SelectAccount)).Result;
        return ar.AccessToken;
    }
Run Code Online (Sandbox Code Playgroud)

请注意,clientID是您在 AAD 中注册的应用程序的 quid,具有Windows Azure 服务管理 API的 API 访问权限。


小智 2

要使用 ARM 模板在 CI 管道中执行此操作,您需要确保 Key Vault 和 Function up 位于同一资源组中。

  1. 使用 ARM 部署函数应用
  2. 将函数部署到函数应用程序 - 更新此代码以从 keyvault 中查找密钥,就像您提到的对 SendGrid API 密钥所做的那样
  3. 将以下代码作为 ARM 模板运行,确保其作为增量运行。这将从指定函数中获取密钥并将其放入所需的密钥保管库中。

    {
        "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "functionAppName":: {
                "type": "string",
                "metadata": {
                    "description": "The name of the function app that you wish to get the key from."
                }
            },
            "functionName": {
                "type": "string",
                "metadata": {
                     "description": "The name of the function that you wish to get the key from."
              }
            },
            "keyVaultName": {
                "type": "string",
                "metadata": {
                    "description": "The name of the key vault you wish to put the key in."
                }
            }
        },
        "variables": {
            "functionAppName": "[parameters('functionAppName')]",
            "keyVaultName": "[parameters('keyVaultName')]",
            "functionName": "[parameters('functionName')]"
        },
        "resources": [
            {
                "type": "Microsoft.KeyVault/vaults/secrets",
                "name": "[concat(variables('keyVaultName'),'/', variables('functionAppName'))]",
                "apiVersion": "2015-06-01",
                "properties": {
                    "contentType": "text/plain",
                    "value": "[listsecrets(resourceId('Microsoft.Web/sites/functions', variables('functionAppName'),  variables('functionName'),'2015-08-01').key]"
                },
                "dependsOn": []
            }
        ]
    }
    
    Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

4941 次

最近记录:

5 年,1 月 前