繁体   English   中英

使用自定义脚本扩展从Azure Key Vault下载证书

[英]Downloading Certificate from Azure Key Vault using Custom script extension

我正在使用python创建虚拟机,并使用自定义脚本扩展名将证书下载到虚拟机中,然后将其保存在本地计算机证书受信任的根证书中。

我唯一的查询是在网络上,可用于从Azure Key Vault下载证书的资源非常有限。 每个人都建议登录,然后使用cmdlet下载,这对于Custom Script Extension不适用。

以下是示例powershell cmdlet,要求登录并下载证书。 但是,由于我们正在运行自定义脚本扩展,因此我们将无法进行身份验证。

$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)

您可以在azure广告中创建一个AD App,然后使用服务主体以非交互方式登录。

请按照以下步骤操作:

1. 创建一个Azure Active Directory应用程序并为该应用程序 创建一个秘密 ,自己保存该秘密并获取用于登录的值

2.在门户中导航到您的密钥库-> Access policies -> Add new -> Select principal (只需搜索AD A​​pp的名称,如果创建AD App,它将在您的租户中自动创建服务主体) ->选择正确的Secret/Key/Certificate permissions (这取决于您要执行的操作,在这种情况下,您需要选择Get in Secret permissions ,也可以轻松选择所有Secret permissions )->单击OK > Save

3.然后,服务主体将具有运行Get-AzureKeyVaultSecret的权限,您只需要使用它以非交互方式登录。

$azureAplicationId ="Azure AD Application Id"
$azureTenantId= "Your Tenant Id"
$azurePassword = ConvertTo-SecureString "your AD App secret" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($azureAplicationId , $azurePassword)
Add-AzureRmAccount -Credential $psCred -TenantId $azureTenantId  -ServicePrincipal 

Get-AzureKeyVaultSecret -VaultName $vaultName -Name $certificateName

暂无
暂无

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

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