This repository has been archived by the owner on Jul 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Install-LE-LDAPS.ps1
105 lines (94 loc) · 3.99 KB
/
Install-LE-LDAPS.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
<#
.SYNOPSIS
.
.DESCRIPTION
.PARAMETER LEserver
Set this to LE_STAGE for testing
Set this to LE_PROD for production
.PARAMETER domain
Set this to the FQDN for AADDS - ad.contoso.com
Don't specify anything other than the FQDN of the domain.
Wildcard certificate will be issued for this domain but is handled by the script.
.PARAMETER contact
Set this to the contact email for certificate related notifications
.PARAMETER dnsProvider
Set this to Azure, Cloudflare, or GoDaddy
This can support other providers but the script should be extended appropriately
Azure will require the context this script is run under to have permission to modify the DNS Zone.
dnsApiId and dnsApiSecret don't need to be set in this case.
Cloudflare only supports Global API key as the API token feature appears to be broken on Cloudflare
GoDaddy only has an option to create a key/secret
.PARAMETER dnsParameter1
Azure - update this
Cloudflare - Cloudflare zone edit token
GoDaddy - API key here
.PARAMETER dnsParameter2
Azure - update this
Cloudflare - Cloudflare all zone read token
GoDaddy - API secret here
.NOTES
Version: 0.1
Author: Zachary Choate
Creation Date: 02/26/2020
URL:
#>
param(
[string] $LEserver,
[string] $domain,
[string] $contact,
[string] $dnsProvider,
[string] $dnsParameter1,
[string] $dnsParameter2
)
$paServer = $LEserver
$wildcardDomain = "*.$domain"
If($dnsProvider -eq "GoDaddy") {
$dnsArguments = @{GDKey=$dnsParameter1;GDSecret=$dnsParameter2}
} elseif ($dnsProvider -eq "Cloudflare") {
$dnsArguments = @{ CFTokenInsecure = $dnsParameter1 }
$dnsArguments.CFTokenReadAllInsecure = $dnsParameter2
} elseif ($dnsProvider -eq "Azure") {
$dnsArguments = @{AZSubscriptionId=$context.Subscription.Id;AZAccessToken=$accessToken}
} else { Write-Output "There isn't a supported DNS provider selected. Please choose from Azure, Cloudflare, or GoDaddy. If you need another configured, please modify the script appropriately."}
## Check for Posh-ACME module
If(!(Get-Module -ListAvailable -Name "Posh-ACME")) {
Write-Output "Install Posh-ACME module by running the command Install-Module Posh-ACME."
Exit
}
## Import Posh-ACME module
Import-Module -Name Posh-ACME
# Set server (staging or prod)
Set-PAServer $paServer
# Get current account, update contact if account has been updated, or create a new account.
$acct = Get-PAAccount
If(-not $acct) {
$acct = New-PAAccount -Contact $contact -KeyLength 4096 -AcceptTOS
} elseif ($acct.contact -ne "mailto:$contact") {
Set-PAAccount -id $acct.id -Contact $contact
}
# See if there's been an order created
$paOrder = Get-PAOrder -MainDomain $wildcardDomain
If(-not $paOrder) {
# Run request for new certificate
$certificate = New-PACertificate $wildcardDomain,$domain -DnsPlugin $dnsProvider -PluginArgs $dnsArguments -AcceptTOS -Contact $contact -Install -Verbose
} else {
# Insert request for renewal of certificate
Set-PAOrder -MainDomain $wildcardDomain -DnsPlugin $dnsProvider -PluginArgs $dnsArguments -Install -Verbose
$certificate = Submit-Renewal -Verbose -Force
}
$thumbprint = $certificate.Thumbprint
$copyParameters = @{
'Path' = "HKLM:\Software\Microsoft\SystemCertificates\MY\Certificates\$thumbprint"
'Destination' = "HKLM:\SOFTWARE\Microsoft\Cryptography\Services\NTDS\SystemCertificates\My\Certificates\$thumbprint"
'Recurse' = $true
}
If(!(Test-Path "HKLM:\SOFTWARE\Microsoft\Cryptography\Services\NTDS\SystemCertificates\My\Certificates")) {
New-Item -Path "HKLM:\SOFTWARE\Microsoft\Cryptography\Services\NTDS\SystemCertificates\My\Certificates" -Force
}
Copy-Item @copyParameters
"dn:
changetype: modify
add: renewServerCertificate
renewServerCertificate: 1
-" | Out-File -FilePath $env:TEMP\ldap-reload.txt
Start-Process ldifde -ArgumentList "-i -f $env:Temp\ldap-reload.txt"