Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与Azure功能(powershell运行时)的KeyVault集成不起作用_Powershell_Azure Functions_Azure Keyvault_Azure Managed Identity - Fatal编程技术网

与Azure功能(powershell运行时)的KeyVault集成不起作用

与Azure功能(powershell运行时)的KeyVault集成不起作用,powershell,azure-functions,azure-keyvault,azure-managed-identity,Powershell,Azure Functions,Azure Keyvault,Azure Managed Identity,在使用azure功能时,我正试图遵循这一点从我的密钥库安全地获取我的秘密 My key vault有一个访问策略,允许通过功能应用程序的系统管理的标识获取机密 以下是高级编辑器中显示的相关应用程序设置(不管插槽设置是真是假,已经尝试过了。顺便说一句,不确定它的作用) { “名称”:“超级机密”, “值”:“@Microsoft.KeyVault(SecretUri=https://.vault.azure.net/secrets//)", “开槽”:真的吗 }如果您想访问Azure函数中的密钥

在使用azure功能时,我正试图遵循这一点从我的密钥库安全地获取我的秘密

My key vault有一个访问策略,允许通过功能应用程序的
系统管理的标识
获取机密

以下是高级编辑器中显示的相关应用程序设置(不管插槽设置是真是假,已经尝试过了。顺便说一句,不确定它的作用)

{
“名称”:“超级机密”,
“值”:“@Microsoft.KeyVault(SecretUri=https://.vault.azure.net/secrets//)",
“开槽”:真的吗

}
如果您想访问Azure函数中的密钥保管库机密,您有两个选择

  • 将其设置为环境变量
如果要将Azure Key vault secret设置为环境变量,可以使用Azure CLI完成。有关更多详细信息,请参阅

  • 代码
  • 有关详细信息,请参阅

    更新

    如何检查我们是否通过Kudu成功设置应用程序设置

    我在与您类似的代码上遇到了相同的错误,我发现解决权限错误可以解决问题

    在“平台功能-->配置”下,查看源列中的设置,并检查其是否有效。您可以编辑该项以获取该条目的详细错误消息

    有效的

    错误

    在我的情况下,功能应用程序没有访问密钥库的权限

    使用Azure门户打开功能应用程序刀片

  • 选择平台功能选项卡
  • 选择标识
  • 在“系统分配”选项卡中,将状态切换为“打开”
  • 单击保存
  • 将对象ID复制到剪贴板
  • 导航到密钥库

  • 选择访问策略
  • 添加访问策略
  • 在“机密”下,选择“获取、列出”
  • 在“选择主体”下,粘贴对象ID
  • 选择您的功能应用程序名称
  • 单击添加
  • 单击保存
  • 返回到函数应用程序,并验证平台功能-->配置下的引用是否有效


    请参阅:

    看来我不是唯一一个。。。这里有一个github问题,我可以进一步确认它与MSI本身无关。我能够成功地(从az函数中)对密钥vault API进行手动REST调用,查询机密并在函数中使用它。因此,我可能需要(作为一种解决方法)手动设置环境变量中的秘密,而不是使用官方方式。谢谢。我尝试通过az cli进行设置。但是,我必须键入az functionapp config appsettings set-n testfun08-g testfun07--settings“Secret1=@Microsoft.KeyVault(SecretUri=^^^^)”仍然,我不知道为什么它可以手动/REST工作,但通过app_设置(这应该是官方的和文档化的方式)完成时它不会工作是通过Azure门户直接添加的吗?无论是通过门户,还是通过az cli,还是通过模板部署(terraform)都不适合我。仅手动调用密钥库的rest API有效。但是,我没有办法持久化为env变量,因为函数app只允许设置流程范围env变量,而不允许设置用户范围和机器范围。即使是在高级计划上。。。因此,新问题就在这里。请检查您是否已成功设置应用程序设置?关于如何做到这一点,请参阅我的更新。
    az functionapp config appsettings set -n testfun08 -g testfun07 --settings "Secret1=@Microsoft.KeyVault(SecretUri=<$secretId>)"
    
        Connect-AzAccount
    
        $app=Get-AzADServicePrincipal -DisplayName "your function app name"
    
        Set-AzKeyVaultAccessPolicy -VaultName "your key vault name" -ResourceGroupName "your group name" -ObjectId $app.Id -PermissionsToSecrets list, get
    
    using namespace System.Net
    
    # Input bindings are passed in via param block.
    param($Request, $TriggerMetadata)
    
    # Write to the Azure Functions log stream.
    Write-Host "PowerShell HTTP trigger function processed a request."
    
    # Interact with query parameters or the body of the request.
    $name = $Request.Query.Name
    if (-not $name) {
        $name = $Request.Body.Name
    }
    
    if ($name) {
    # get access token with MSI. For more details, please refer to https://docs.microsoft.com/en-us/azure/app-service/overview-managed-identity#rest-protocol-examples
        $tokenAuthURI = $Env:MSI_ENDPOINT +"?resource=https://vault.azure.net&api-version=2017-09-01"
       $tokenResponse = Invoke-RestMethod -Method Get -Headers @{"Secret"="$env:MSI_SECRET"} -Uri $tokenAuthURI
    $accessToken = $tokenResponse.access_token
    # get secret value
    $headers = @{ 'Authorization' = "Bearer $accessToken" }
    $queryUrl = "the url of secret" + "?api-version=7.0"
    
    $keyResponse = Invoke-RestMethod -Method GET -Uri $queryUrl -Headers $headers
    $value= $keyResponse.value
    Write-Host "$vaule"  
        $status = [HttpStatusCode]::OK
        $body = "Hello $name $value"
    }
    else {
        $status = [HttpStatusCode]::BadRequest
        $body = "Please pass a name on the query string or in the request body."
    }
    
    # Associate values to output bindings by calling 'Push-OutputBinding'.
    Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
        StatusCode = $status
        Body = $body
    })