从Azure Batch访问Azure Key Vault

Apleroy

我正在尝试从Azure批处理节点池中运行的VM访问Azure密钥保管库中的机密。

但是,我不断遇到异常:

异常消息:尝试过1个证书。无法获取访问令牌。

带有指纹MY-THUMBPRINT的证书#1的异常:密钥集不存在

到目前为止,我一直在按照此处概述的说明进行操作:https : //docs.microsoft.com/zh-cn/azure/key-vault/service-to-service-authentication

本文概述了Azure Batch的方案,并指出我应该使用服务主体。我想确保版本控制中没有秘密或密钥,因此我正在使用本地密钥库中证书的第一种方法登录Azure AD。

作为可执行文件在本地运行以下所有程序,效果很好,但是在Azure Batch池节点上运行时失败。

到目前为止,我要做的步骤是:

  1. 在keyvault中创建服务主体和关联的证书:az ad sp create-for-rbac --name myserviceprincipal --create-cert --cert mycertname --keyvault mykeyvaultname保留服务主体应用程序ID和租户ID,以在AzureServicesAuthConnectionString中使用。

  2. 为创建的服务主体创建密钥库访问策略(在Azure门户UI中完成)。

  3. 以PFX / PEM格式下载创建的证书(在Azure Portal UI中完成)。

  4. 确保证书上的PFX密码(我这样做是因为在步骤6中将证书上传到Azure批处理需要关联的密码):https : //coombes.nz/blog/azure-keyvault-export-certificate/

# Replace these variables with your own values
$vaultName = "YOUR-KEYVAULT-NAME"
$certificateName = "YOUR-CERTIFICATE-NAME"
$pfxPath = [Environment]::GetFolderPath("Desktop") + "\$certificateName.pfx"
$password = "YOUR-CERTIFICATE-PASSWORD"

$pfxSecret = Get-AzureKeyVaultSecret -VaultName $vaultName -Name $certificateName
$pfxUnprotectedBytes = [Convert]::FromBase64String($pfxSecret.SecretValueText)
$pfx = New-Object Security.Cryptography.X509Certificates.X509Certificate2
$pfx.Import($pfxUnprotectedBytes, $null, [Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable)
$pfxProtectedBytes = $pfx.Export([Security.Cryptography.X509Certificates.X509ContentType]::Pkcs12, $password)
[IO.File]::WriteAllBytes($pfxPath, $pfxProtectedBytes)
  1. 将证书本地安装到计算机的LocalMachine存储中(用于本地测试)。

  2. 将证书上传到Azure Batch(使用Azure Portal UI上传)。

  3. 将证书与适当的节点池相关联,并重新启动节点(暂时使用Azure Portal UI)。

我在Azure Batch上运行的应用程序包是一个简单的控制台可执行文件。AzureServicesAuthConnectionString设置为RunAs=App;AppId={AppId};TenantId={TenantId};CertificateThumbprint={Thumbprint};CertificateStoreLocation={LocalMachine},其余的用于恢复机密的密钥库代码如下所示:

Environment.SetEnvironmentVariable("AzureServicesAuthConnectionString", "RunAs=App;AppId=<MY-APP-ID>;TenantId=<MY-TENANT>;CertificateThumbprint=<MY-THUMBPRINT>;CertificateStoreLocation=LocalMachine");

AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider(Environment.GetEnvironmentVariable("AzureServicesAuthConnectionString"));
KeyVaultClient keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
var secret = await keyVaultClient.GetSecretAsync("<MY-SECRET>").ConfigureAwait(false);
var message = secret.Value;
Console.WriteLine(message);

在本地一切正常,但在远程节点上失败。我能够RDP进入Azure Batch节点,并看到已为本地计算机安装证书。

我想知道如何解决我的错误,或者以上步骤在某种程度上是否错误?

Apleroy

为了访问证书,必须为“当前用户”关联并安装该证书。可能是LocalMachine没有适当级别的权限?

在Azure Batch上,确保上载的证书与以下内容关联:

商店名称:“我的”商店位置:“ CurrentUser”

这篇文章很有帮助:https : //github.com/nabhishek/customactivity_sample/tree/linkedservice

就像这个帖子一样:X509Certificate-密钥集不存在

C#exe中的连接字符串如下所示:

Environment.SetEnvironmentVariable("AzureServicesAuthConnectionString",
                    "RunAs=App;" +
                    "AppId=<the app id of my active directory app registration> ;" +
                    "TenantId=<my subscription tenant id>;" +
                    "CertificateThumbprint=<the thumbprint of my cert>;" +
                    "CertificateStoreLocation=CurrentUser");

                AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider(Environment.GetEnvironmentVariable("AzureServicesAuthConnectionString"));
                KeyVaultClient keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
                var secret = await keyVaultClient.GetSecretAsync("https://<my vault name>.vault.azure.net/secrets/<secret name>/<secret id>")
                        .ConfigureAwait(false);
                message = secret.Value;
                Console.WriteLine(message);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章