【问题标题】:Accessing Secrets on Azure Key Vault from Service Fabric从 Service Fabric 访问 Azure Key Vault 上的机密
【发布时间】:2020-01-04 08:05:33
【问题描述】:

我构建了服务结构应用程序,我想保护 Azure Key Vault 中的机密,我实施了与应用服务相同的步骤,但它不起作用,感谢您的重播。

对于应用服务: 1. 在 Main 方法上配置 Key Vault 2. 在应用服务上启用分配的托管标识,应用于 SF 的规模集。 3. 在密钥保管库上添加访问策略。

【问题讨论】:

  • 什么步骤?而且“不起作用”并没有给我们任何帮助您的线索。你能发布一些代码吗?如果您希望我们能够为您提供帮助,您需要提供更多信息。
  • 嗨@Peter 我刚刚更新了查询,
  • 你有什么错误?

标签: service azure-service-fabric azure-keyvault


【解决方案1】:

1) Azure 配置(VM Scale set + Key Vault):

Login-AzureRmAccount # Login into Azure account

$targetRg = "testfabric-rg" # Target resource group name
$targetVmss = "jxewcyinq" # Target virtual machine scale set name
$targetKeyVault = "az-ure-two20190115153549" # Target Key Vault name

# 1. Enable Managed Identity for target Virtual Machine Scale Set
Update-AzureRmVmss `
    -ResourceGroupName $targetRg `
    -VMScaleSetName $targetVmss `
    -IdentityType SystemAssigned
# 2. Retrieve virtual machine scale set
$vmss = Get-AzureRmVmss `
    -ResourceGroupName $targetRg `
    -Name $targetVmss
# 3. Create new Key vault access policy allowing Virtual Machine Scale Set to read secrets by their IDs
Set-AzureRmKeyVaultAccessPolicy `
    -VaultName $targetKeyVault `
    -ObjectId $vmss.Identity.PrincipalId `
    -PermissionsToSecrets Get # set only necessary permissions!

2) 使用 C# 时获取密钥库机密:

// https://www.nuget.org/packages/Microsoft.Azure.KeyVault/
using Microsoft.Azure.KeyVault;
// https://www.nuget.org/packages/Microsoft.Azure.Services.AppAuthentication
using Microsoft.Azure.Services.AppAuthentication;

public async Task<string> GetSecretById(string id)
{
    // URL of the target Key Vault
    var keyVaultUrl = "https://az-ure-two20190115153549.vault.azure.net";

    var azureServiceTokenProvider = new AzureServiceTokenProvider();

    var keyVaultClient = new KeyVaultClient(
        new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));

    var secret = await keyVaultClient.GetSecretAsync($"{keyVaultUrl}/secrets/{id}");

    return secret.Value;
}

【讨论】:

  • 谢谢@Nicklaus。很高兴看到这个清晰的解释。
  • @ABDULAZIZBIRHANU 如果答案解决了您的问题,请将问题标记为已解决
猜你喜欢
  • 2022-12-22
  • 2020-08-27
  • 2020-02-11
  • 1970-01-01
  • 2017-09-24
  • 2019-03-13
  • 2018-09-26
  • 1970-01-01
  • 2021-07-22
相关资源
最近更新 更多