-
Notifications
You must be signed in to change notification settings - Fork 2
/
New-AzsPaaSCertificate.ps1
115 lines (97 loc) · 3.15 KB
/
New-AzsPaaSCertificate.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
[CmdletBinding()]
param (
[Parameter(mandatory=$true)]
[ValidateSet("EventHubs" , "IoTHub", "ccc")]
[string]
$rp,
[Parameter(mandatory=$true)]
[string]
$subject,
[Parameter(mandatory=$true)]
[string]
$regionName,
[Parameter(mandatory=$true)]
[string]
$externalFQDN,
[Parameter(mandatory=$true)]
[string]
$email,
[Parameter(mandatory=$true)]
[string]
$subscriptionId,
[Parameter(mandatory=$true)]
[string]
$pfxpass
)
# This script supports only EventHubs now.
#Requires -Modules @{ ModuleName="Microsoft.AzureStack.ReadinessChecker"; ModuleVersion="1.2100.1396.426" }
#Requires -Modules Posh-ACME
#Requires -Modules @{ ModuleName="Az.Accounts"; ModuleVersion="2.2.2" }
#Requires -RunAsAdministrator
$ErrorActionPreference = "stop"
Function Write-Log
{
param(
[string]$Message,
[string]$Color = 'White'
)
$Date = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Write-Host "[$Date] $Message" -ForegroundColor $Color
}
function New-AzsEventHubCertificate {
param (
[Parameter(mandatory=$true)]
$reqs
)
$csr = ($reqs | Where-Object { $_.name -like "wildcard_eventhub_*"} | Sort-Object -Property LastWriteTime -Descending)[0]
$result = New-PACertificate `
-CSRPath $csr.FullName `
-Contact $email `
-AcceptTOS `
-DnsPlugin Azure `
-PluginArgs @{`
AZSubscriptionId=$subscriptionId;`
AZAccessToken=$token;`
} `
-DNSSleep 5 `
-Verbose -force
return $result
}
$outputDirectory = "$HOME\azscsr"
if ( (Test-Path $outputDirectory ) -eq $false ){
$outputDirectory = "$HOME\azscsr"
Write-Log "$OutputDirectory doesn't exist. Create it." "Green"
New-Item -ItemType Directory -Path $outputDirectory
} else {
Write-Output "$OutputDirectory already exist."
}
Write-Log "Create the CSR for $rp" "Green"
switch($rp){
"EventHubs" {
New-AzsHubEventHubsCertificateSigningRequest -RegionName $regionName -FQDN $externalFQDN `
-subject $subject -OutputRequestPath $outputDirectory
}
"IoTHub" {
New-AzsHubIoTHubCertificateSigningRequest -RegionName $regionName -FQDN $externalFQDN `
-subject $subject -OutputRequestPath $outputDirectory
}
}
$reqs = Get-ChildItem "$HOME\azscsr" | Where-Object { $_.Name -like "*.req"}
Write-Log "Get the access token to call Azure DNS" "Green"
Get-AzSubscription -subscriptionId $subscriptionId | Select-AzSubscription
$token = (Get-AzAccessToken).Token
#Set-PAServer LE_STAGE
Write-Log "Sign the certificate for $rp by Let's encrypt" "Green"
switch($rp){
"EventHubs" {
$result = New-AzsEventHubCertificate -reqs $reqs
$certPath = ($result.FullChainFile).Replace("\fullchain.cer","")
Write-Output $result
}
"IoTHub" {
}
}
Write-Log "Export the certificate as a pfx file" "Green"
Get-ChildItem $certpath |Where-Object { $_.Name -ne "fullchain.cer"} | Rename-Item -NewName{$_.Name + ".tmp"}
$securePfxpass = ConvertTo-SecureString -String $pfxpass -Force -AsPlainText
ConvertTo-AzsPFX -Path $certPath -pfxPassword $securePfxpass -ExportPath $outputDirectory