简体   繁体   English

Azure 密钥保管库:拒绝访问

[英]Azure key vault: access denied

I have the following code for obtaining a secret from the Azure key vault:我有以下代码用于从 Azure Key Vault 获取机密:

public static async Task<string> GetToken(string authority, string resource, string scope)
    {
        var authContext = new AuthenticationContext(authority);
        ClientCredential clientCred = new ClientCredential(...); //app id, app secret
        AuthenticationResult result = await authContext.AcquireTokenAsync(resource, clientCred);

        if (result == null)
            throw new InvalidOperationException("Failed to obtain the JWT token");

        return result.AccessToken;
    }

    public static string GetSecret(string secretName)
    {
        KeyVaultClient keyVaultClient = new KeyVaultClient(GetToken);
        try
        {
            return keyVaultClient.GetSecretAsync("my-key-vault-url", secretName).Result.Value;
        }
        catch(Exception ex)
        {
            return "Error";
        }
    }

The error I am getting is "access denied", which (I think) means that the id, secret and the vault's url are fine.我得到的错误是“访问被拒绝”,这(我认为)意味着 id、secret 和保险库的 url 没问题。 However, I don't know what I can do differently to fix this error, is there maybe a setting in the Azure portal which is preventing me from reading a secret?但是,我不知道我可以做些什么来修复此错误,Azure 门户中是否有设置阻止我阅读机密?

To fix access denied you need to configure Active Directory permissions.要修复拒绝访问,您需要配置 Active Directory 权限。 Grant access to KeyVault.授予对 KeyVault 的访问权限。

1. Using PowerShell Run next command: 1. 使用 PowerShell运行下一个命令:

Set-AzureRmKeyVaultAccessPolicy -VaultName 'XXXXXXX' -ServicePrincipalName XXXXX -PermissionsToKeys decrypt,sign,get,unwrapKey

2. Using the Azure portal 2. 使用 Azure 门户

  1. Open Key Vaults打开密钥保管库
  2. Select Access Policies from the Key Vault resource blade从 Key Vault 资源边栏选项卡中选择访问策略
  3. Click the [+ Add Access Policy] button at the top of the blade单击刀片顶部的 [+ 添加访问策略] 按钮
  4. Click Select Principal to select the application you created earlier单击Select Principal选择您之前创建的应用程序
  5. From the Key permissions drop down, select "Decrypt", "Sign", "Get", "UnwrapKey" permissions从密钥权限下拉列表中,选择“解密”、“签名”、“获取”、“UnwrapKey”权限
  6. Save changes保存更改

Authorize the application to use the key or secret 授权应用程序使用密钥或秘密

The question did specify using the Azure Portal, I've documented creating a service principal for Key Vault access here .该问题确实使用 Azure 门户指定,我已在此处记录为 Key Vault 访问创建服务主体。

Specifically from Step 2:具体从第 2 步开始:

Open the Key Vault in the Azure Portal and select the Access policies blade under Settings.在 Azure 门户中打开 Key Vault,然后选择“设置”下的“访问策略”边栏选项卡。 Click Add New and click on Select principal - you'll have to enter the full name of the registered app you created in the previous step in the search box before it'll show up, at which point you'll be able to select it.单击添加新并单击选择主体 - 您必须在搜索框中输入您在上一步中创建的已注册应用程序的全名,然后它才会显示出来,此时您就可以选择它.

You can either select an appropriate template from the top dropdown or choose Key, Secret or Certificate permissions manually.您可以从顶部下拉列表中选择适当的模板,也可以手动选择密钥、机密或证书权限。 Don't worry about Authorized application at this stage.在这个阶段不要担心授权申请。

IMPORTANT: pressing the OK button will add your new policy to the list, but it will not be saved!重要提示:按 OK 按钮会将您的新策略添加到列表中,但不会保存! Be sure to click Save before continuing.在继续之前,请务必单击“保存”。

What is happening - your service principal doesn't have permissions to perform said operation.发生了什么 - 您的服务主体无权执行上述操作。 Take a look at this thread.看看这个线程。

How do I fix an "Operation 'set' not allowed" error when creating an Azure KeyVault secret programmatically? 以编程方式创建 Azure KeyVault 机密时,如何修复“不允许‘设置’操作”错误?

If you want to authorize that same application to read secrets in your vault, run the following:如果要授权同一应用程序读取保管库中的机密,请运行以下命令:

Set-AzureRmKeyVaultAccessPolicy -VaultName 'yourKeyVaultName' -ServicePrincipalName ClientId -PermissionsToSecrets Get

When you register application in Azure ClientId is generated.当您在 Azure 中注册应用程序时会生成 ClientId。

Access Key Vault in .Net code Azure Setting:- App Service- 1-Enable-MSI(Managed service identity)-ON在 .Net 代码 Azure 设置中访问 Key Vault:- App Service- 1-Enable-MSI(托管服务标识)-ON

Key Vault: 1-Open Key Vault 2-Select Access Policies from the Key Vault resource blade Key Vault:1-打开 Key Vault 2-从 Key Vault 资源刀片中选择访问策略

3- Click the [+ Add new] button at the top of the blade 4-Click Select Principal to select the application(App Service) you created earlier 3- 单击刀片顶部的 [+ Add new] 按钮 4-单击 Select Principal 以选择您之前创建的应用程序(App Service)

.Net Code:- Code to Access key vault secrets in .Net Code .Net 代码:- 在 .Net 代码中访问密钥保管库机密的代码

 var azureServiceTokenProvider = new AzureServiceTokenProvider();
        var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
        var secret = keyVaultClient.GetSecretAsync("https://test.vault.azure.net/", "clientid").Result.Value;

我遇到了同样的问题,我在 KeyVault 防火墙下添加了我的 IP 地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM