从Service Fabric访问Azure Key Vault中的机密

3
我正在构建一个服务面向fabric应用程序,我希望能够在Azure Key Vault中保护秘密信息。我按照与应用程序服务相同的步骤进行实现,但是不起作用。期待您的回复。
对于应用程序服务: 1. 在主方法上配置Key Vault。 2. 在应用程序服务上启用已分配的托管标识,并应用于SF的比例集。 3. 在密钥保管库中添加访问策略。

什么步骤?而且“不起作用”并没有给我们任何提示来帮助你。你能贴一些代码吗?如果你想让我们能够帮助你,你需要提供更多的信息。 - Peter Bons
嗨@Peter,我刚刚更新了查询。 - ABDULAZIZ BIRHANU
你有什么错误? - Nicklaus Brain
1个回答

4

1)Azure配置(VM规模集+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。很高兴看到这么清晰的解释。 - ABDULAZIZ BIRHANU
如果答案解决了您的问题,请@ABDULAZIZBIRHANU将问题标记为已解决。 - Nicklaus Brain

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接