Last active 4 hours ago

install-ca.sh Raw
1param(
2 [string]$CaUrl = "https://10.10.40.53",
3 [string]$Fingerprint = "5fc8c379cab1119c1a9ac7038225f6bcf3a2ffb0e71b257af796b2bd6c71d594",
4 [switch]$Force
5)
6
7$ErrorActionPreference = "Stop"
8
9function Write-Log {
10 param([string]$Message)
11 Write-Host ""
12 Write-Host "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] $Message"
13}
14
15function Fail {
16 param([string]$Message)
17 throw $Message
18}
19
20function Test-IsAdmin {
21 $currentIdentity = [Security.Principal.WindowsIdentity]::GetCurrent()
22 $principal = New-Object Security.Principal.WindowsPrincipal($currentIdentity)
23 return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
24}
25
26function Require-Command {
27 param([string]$Name)
28 if (-not (Get-Command $Name -ErrorAction SilentlyContinue)) {
29 Fail "Required command not found: $Name"
30 }
31}
32
33function Refresh-Path {
34 $machinePath = [System.Environment]::GetEnvironmentVariable("Path", "Machine")
35 $userPath = [System.Environment]::GetEnvironmentVariable("Path", "User")
36 $env:Path = "$machinePath;$userPath"
37}
38
39function Ensure-Step {
40 if (Get-Command step -ErrorAction SilentlyContinue) {
41 Write-Log "step CLI already installed"
42 return
43 }
44
45 Require-Command winget
46
47 Write-Log "Installing step CLI with winget"
48 # Machine-wide install when admin, user install otherwise.
49 if (Test-IsAdmin) {
50 winget install --exact --id Smallstep.step --accept-package-agreements --accept-source-agreements --scope machine
51 } else {
52 winget install --exact --id Smallstep.step --accept-package-agreements --accept-source-agreements --scope user
53 }
54
55 Refresh-Path
56
57 if (-not (Get-Command step -ErrorAction SilentlyContinue)) {
58 Fail "step CLI installation failed or is not yet on PATH"
59 }
60}
61
62function Reset-StepConfig {
63 $stepDir = Join-Path $HOME ".step"
64 if (Test-Path $stepDir) {
65 Write-Log "Removing previous step configuration"
66 Remove-Item -Recurse -Force $stepDir
67 }
68}
69
70function Bootstrap-Step {
71 if ($Force) {
72 Reset-StepConfig
73 }
74
75 Write-Log "Bootstrapping against $CaUrl"
76 & step ca bootstrap --ca-url $CaUrl --fingerprint $Fingerprint --install --force
77 if ($LASTEXITCODE -ne 0) {
78 Fail "step bootstrap failed"
79 }
80}
81
82function Get-RootCertPath {
83 $rootCert = Join-Path $HOME ".step\certs\root_ca.crt"
84 if (-not (Test-Path $rootCert)) {
85 Fail "Root certificate not found at $rootCert"
86 }
87 return $rootCert
88}
89
90function Install-TrustStore {
91 $rootCert = Get-RootCertPath
92
93 if (Test-IsAdmin) {
94 Write-Log "Installing root CA into LocalMachine Root store"
95 Import-Certificate -FilePath $rootCert -CertStoreLocation "Cert:\LocalMachine\Root" | Out-Null
96 } else {
97 Write-Log "Not running as Administrator; installing root CA into CurrentUser Root store"
98 Import-Certificate -FilePath $rootCert -CertStoreLocation "Cert:\CurrentUser\Root" | Out-Null
99 }
100}
101
102function Verify-Install {
103 $rootCert = Get-RootCertPath
104
105 Write-Log "Installed root CA:"
106 & step certificate inspect $rootCert --short
107
108 if (Test-IsAdmin) {
109 Write-Log "Verified using LocalMachine Root store"
110 Get-ChildItem "Cert:\LocalMachine\Root" |
111 Where-Object { $_.Thumbprint -eq (Get-PfxCertificate $rootCert).Thumbprint } |
112 Select-Object Subject, Thumbprint |
113 Format-Table -AutoSize
114 } else {
115 Write-Log "Verified using CurrentUser Root store"
116 Get-ChildItem "Cert:\CurrentUser\Root" |
117 Where-Object { $_.Thumbprint -eq (Get-PfxCertificate $rootCert).Thumbprint } |
118 Select-Object Subject, Thumbprint |
119 Format-Table -AutoSize
120 }
121
122 Write-Host ""
123 Write-Host "Done."
124}
125
126function Main {
127 Ensure-Step
128 Bootstrap-Step
129 Install-TrustStore
130 Verify-Install
131}
132
133Main