-
Notifications
You must be signed in to change notification settings - Fork 17
/
azure-metadata-import-cert.ps1
123 lines (93 loc) · 5.1 KB
/
azure-metadata-import-cert.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<#
.SYNOPSIS
example script to import certificate from keyvault using azure managed identity from a configured vm scaleset node
enable system / user managed identity on scaleset:
Update-AzVmss -ResourceGroupName sfcluster -Name nt0 -IdentityType "SystemAssigned"
add vmss managed identity to keyvault with read secrets permission
convert certificate to base64 string and add as new secret value
$base64String = [convert]::ToBase64String([io.file]::ReadAllBytes($certFile))
use custom script extension (cse) or similar to deploy a script to vmss with ARM template
.LINK
to download and test from vmss node with managed identity enabled:
[net.servicePointManager]::Expect100Continue = $true;[net.servicePointManager]::SecurityProtocol = [net.SecurityProtocolType]::Tls12;
invoke-webrequest https://raw.githubusercontent.com/jagilber/powershellScripts/master/azure-metadata-import-cert.ps1 -outfile $pwd/azure-metadata-import-cert.ps1;
.EXAMPLE
.\azure-metadata-import-cert.ps1 -secretUrl 'https://<keyvaultName>.vault.azure.net/secrets/<secretName>/<secretVersion>' -base64
MIT License
Copyright (c) Microsoft Corporation. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE
#>
param(
[Parameter(Mandatory = $true)]
[string]$secretUrl = '',
[string]$certStoreLocation = 'cert:\LocalMachine\My', #'cert:\LocalMachine\Root', #'cert:\LocalMachine\CA',
[switch]$base64,
[bool]$pfx = $true
)
$error.Clear()
$ErrorActionPreference = "continue"
# acquire system managed identity oauth token from within node
$global:vaultOauthResult = (Invoke-WebRequest -Method GET `
-UseBasicParsing `
-Uri "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://vault.azure.net" `
-Headers @{'Metadata' = 'true' }).content | convertfrom-json
write-host $global:vaultOauthResult
$secretPattern = 'https://(?<keyvaultName>.+?).vault.azure.net/secrets/(?<secretName>.+?)/(?<secretVersion>.+)'
$results = [regex]::Match($secretUrl, $secretPattern, [text.RegularExpressions.RegexOptions]::IgnoreCase)
$keyvaultName = $results.groups['keyvaultName'].Value
$secretName = $results.groups['secretName'].Value
$secretVersion = $results.groups['secretVersion'].Value
if (!$global:vaultOauthResult.access_token) {
write-error 'no vault token'
return
}
if ($keyvaultName -and $secretName -and $secretVersion) {
# get cert with private key from keyvault
$headers = @{
Authorization = "Bearer $($global:vaultOauthResult.access_token)"
}
write-host "invoke-WebRequest "$secretUrl?api-version=7.0" -UseBasicParsing -Headers $headers"
$global:certificateSecrets = invoke-WebRequest "$($secretUrl)?api-version=7.0" -UseBasicParsing -Headers $headers
write-host "$certStoreLocation before"
Get-ChildItem $certStoreLocation
# save secrets cert with private key to pfx
$global:secret = ($global:certificateSecrets.content | convertfrom-json).value
if ($global:secret) {
$certFile = [io.path]::GetTempFileName()
if ($base64) {
$global:secret = [text.encoding]::UNICODE.GetString([convert]::FromBase64String($global:secret))
}
write-host "out-file -InputObject `$global:secret -FilePath $certFile"
out-file -InputObject $global:secret -FilePath $certFile
if ($pfx) {
write-host "Import-PfxCertificate -Exportable -CertStoreLocation $certStoreLocation -FilePath $certFile"
Import-PfxCertificate -Exportable -CertStoreLocation $certStoreLocation -FilePath $certFile
}
else {
write-host "Import-Certificate -Exportable -CertStoreLocation $certStoreLocation -FilePath $certFile"
Import-Certificate -CertStoreLocation $certStoreLocation -FilePath $certFile
}
write-host "$certStoreLocation after"
Get-ChildItem $certStoreLocation
}
write-host $global:certificateSecrets.content | convertfrom-json | convertto-json
}
else {
write-error 'no keyvault or certificate name'
}
write-host "objects stored in `$global:secret `$global:certificateSecrets"
write-host "finished."