Export/Private/Save-AzureCertificateToLocalFile.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Will be called in VM
function Global:Save-AzureCertificateToLocalFile {
    [CmdletBinding()]
    <#
    .SYNOPSIS
        ...
    .DESCRIPTION
        ...
    #>

    param(
        [Parameter(Mandatory = $true)]
        [string]
        $KeyVaultName,
        [Parameter(Mandatory = $true)]
        $Certificate,
        [Parameter(Mandatory = $true)]
        [string]
        $CertificateType,
        [Parameter(Mandatory = $true)]
        [string]
        $TargetFilename
    )
    process {
        New-Item -Path (Split-Path $TargetFilename -Parent) -ItemType Directory -ErrorAction SilentlyContinue

        $certPasswordSecret = Get-AzKeyVaultSecret -VaultName $KeyVaultName -Name "$($CertificateType)-CertPassword"
        
        $certSecret = Get-AzKeyVaultSecret -VaultName $KeyVaultName -Name $Certificate.SecretId -ErrorAction SilentlyContinue
        if (-not($certSecret)) {
            # MS Documentation says that the first command should work (see example2 here: https://docs.microsoft.com/en-us/powershell/module/az.keyvault/get-azkeyvaultcertificate?view=azps-3.3.0)
            # However, when I tested it, SecretID contained the complete URI (like: https://kvname.vault.azure.net:443/secrets/ServiceInstance/00000000000000)
            # So lets here call the same, but only with the last part
            $certSecret = Get-AzKeyVaultSecret -VaultName $KeyVaultName -Name $Certificate.SecretId.Substring($Certificate.SecretId.IndexOf("secrets/") + 8)
        }        

        $secretByte = [Convert]::FromBase64String($certSecret.SecretValueText)
        $x509Cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
        $x509Cert.Import($secretByte, "", "Exportable,PersistKeySet")
        $type = [System.Security.Cryptography.X509Certificates.X509ContentType]::Pfx
        $pfxFileByte = $x509Cert.Export($type, $certPasswordSecret.SecretValueText)
        # Write to a file
        [System.IO.File]::WriteAllBytes($TargetFilename, $pfxFileByte)

        $certificateInfo = @{
            Name       = $CertificateType
            Password   = $certPasswordSecret.SecretValueText
            Thumbprint = $Certificate.Thumbprint
            Path       = $TargetFilename
        }
        $certificateInfo
    }
}