-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_self_signed_cert.ps1
89 lines (69 loc) · 4.18 KB
/
generate_self_signed_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
# Source: https://stackoverflow.com/a/62060315
# Generate self-signed certificate to be used by IdentityServer.
# When using localhost - API cannot see the IdentityServer from within the docker-compose'd network.
# You have to run this script as Administrator (open Powershell by right click -> Run as Administrator).
# Source 2: https://github.com/mjarosie/IdentityServerDockerHttpsDemo
# Slightly modified mjarosie's poweshell scenario to fit NetReact.
# Article for more information: https://mjarosie.github.io/dev/2020/09/24/running-identityserver4-on-docker-with-https.html
$ErrorActionPreference = "Stop"
$rootCN = "NetReactRootCert"
$identityServerCNs = "net-react-identity", "localhost"
$webApiCNs = "net-react-api", "localhost"
$alreadyExistingCertsRoot = Get-ChildItem -Path Cert:\LocalMachine\My -Recurse | Where-Object {$_.Subject -eq "CN=$rootCN"}
$alreadyExistingCertsIdentityServer = Get-ChildItem -Path Cert:\LocalMachine\My -Recurse | Where-Object {$_.Subject -eq ("CN={0}" -f $identityServerCNs[0])}
$alreadyExistingCertsApi = Get-ChildItem -Path Cert:\LocalMachine\My -Recurse | Where-Object {$_.Subject -eq ("CN={0}" -f $webApiCNs[0])}
if ($alreadyExistingCertsRoot.Count -eq 1) {
Write-Output "Skipping creating Root CA certificate as it already exists."
$testRootCA = [Microsoft.CertificateServices.Commands.Certificate] $alreadyExistingCertsRoot[0]
} else {
$testRootCA = New-SelfSignedCertificate -Subject $rootCN -KeyUsageProperty Sign -KeyUsage CertSign -CertStoreLocation Cert:\LocalMachine\My
}
if ($alreadyExistingCertsIdentityServer.Count -eq 1) {
Write-Output "Skipping creating Identity Server certificate as it already exists."
$identityServerCert = [Microsoft.CertificateServices.Commands.Certificate] $alreadyExistingCertsIdentityServer[0]
} else {
# Create a SAN cert for both identity-server and localhost.
$identityServerCert = New-SelfSignedCertificate -DnsName $identityServerCNs -Signer $testRootCA -CertStoreLocation Cert:\LocalMachine\My
}
if ($alreadyExistingCertsApi.Count -eq 1) {
Write-Output "Skipping creating API certificate as it already exists."
$webApiCert = [Microsoft.CertificateServices.Commands.Certificate] $alreadyExistingCertsApi[0]
} else {
# Create a SAN cert for both web-api and localhost.
$webApiCert = New-SelfSignedCertificate -DnsName $webApiCNs -Signer $testRootCA -CertStoreLocation Cert:\LocalMachine\My
}
# Export it for docker container to pick up later.
$password = ConvertTo-SecureString -String "password" -Force -AsPlainText
$rootCertPathPfx = "src/certs"
$identityServerCertPath = "src/NetReact.IdentityServer/certs"
$webApiCertPath = "src/NetReact.API/certs"
If (!(test-path $rootCertPathPfx))
{
New-Item -ItemType Directory -Path $rootCertPathPfx | Out-Null
}
If (!(test-path $identityServerCertPath))
{
New-Item -ItemType Directory -Path $identityServerCertPath | Out-Null
}
If (!(test-path $webApiCertPath))
{
New-Item -ItemType Directory -Path $webApiCertPath | Out-Null
}
Export-PfxCertificate -Cert $testRootCA -FilePath "$rootCertPathPfx/aspnetapp-root-cert.pfx" -Password $password | Out-Null
Export-PfxCertificate -Cert $identityServerCert -FilePath "$identityServerCertPath/aspnetapp-identity-server.pfx" -Password $password | Out-Null
Export-PfxCertificate -Cert $webApiCert -FilePath "$webApiCertPath/aspnetapp-web-api.pfx" -Password $password | Out-Null
# Export .cer to be converted to .crt to be trusted within the Docker container.
$rootCertPathCer = "src/certs/aspnetapp-root-cert.cer"
Export-Certificate -Cert $testRootCA -FilePath $rootCertPathCer -Type CERT | Out-Null
# Trust it on your host machine.
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store "Root","LocalMachine"
$store.Open("ReadWrite")
$rootCertAlreadyTrusted = ($store.Certificates | Where-Object {$_.Subject -eq "CN=$rootCN"} | Measure-Object).Count -eq 1
if ($rootCertAlreadyTrusted -eq $false) {
Write-Output "Adding the root CA certificate to the trust store."
# $trustCert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $rootCertPathCer
$store.Add($testRootCA)
} else {
Write-Output "Skipping trusting Root CA certificate as it already trusted."
}
$store.Close()