install-ca.ps1
· 2.1 KiB · PowerShell
Неформатований
param(
[string]$CaUrl = "https://10.10.40.53",
[string]$Fingerprint = "5fc8c379cab1119c1a9ac7038225f6bcf3a2ffb0e71b257af796b2bd6c71d594",
[string]$StepVersion = "0.28.7",
[switch]$Force
)
$ErrorActionPreference = "Stop"
function Write-Log {
param([string]$Message)
Write-Host ""
Write-Host "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] $Message"
}
function Ensure-Step {
if (Get-Command step -ErrorAction SilentlyContinue) {
Write-Log "step CLI already installed"
return
}
$msi = "step-cli_${StepVersion}_amd64.msi"
$url = "https://github.com/smallstep/cli/releases/download/v${StepVersion}/${msi}"
$tmp = Join-Path $env:TEMP $msi
Write-Log "Downloading step CLI"
Invoke-WebRequest -Uri $url -OutFile $tmp
Write-Log "Installing step CLI"
Start-Process msiexec.exe -Wait -ArgumentList "/i `"$tmp`" /qn"
Remove-Item $tmp -Force
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" +
[System.Environment]::GetEnvironmentVariable("Path", "User")
if (-not (Get-Command step -ErrorAction SilentlyContinue)) {
throw "step CLI installation failed"
}
}
function Bootstrap-Step {
if ($Force) {
$stepDir = Join-Path $HOME ".step"
if (Test-Path $stepDir) {
Remove-Item -Recurse -Force $stepDir
}
}
Write-Log "Bootstrapping against $CaUrl"
& step ca bootstrap --ca-url $CaUrl --fingerprint $Fingerprint --install --force
}
function Install-WindowsTrust {
$rootCert = Join-Path $HOME ".step\certs\root_ca.crt"
if (-not (Test-Path $rootCert)) {
throw "Root certificate not found at $rootCert"
}
Write-Log "Installing root CA into Local Machine Root store"
Import-Certificate -FilePath $rootCert -CertStoreLocation "Cert:\LocalMachine\Root" | Out-Null
}
function Verify-Install {
$rootCert = Join-Path $HOME ".step\certs\root_ca.crt"
Write-Log "Installed root CA:"
& step certificate inspect $rootCert --short
Write-Host ""
Write-Host "Done."
}
Ensure-Step
Bootstrap-Step
Install-WindowsTrust
Verify-Install
| 1 | param( |
| 2 | [string]$CaUrl = "https://10.10.40.53", |
| 3 | [string]$Fingerprint = "5fc8c379cab1119c1a9ac7038225f6bcf3a2ffb0e71b257af796b2bd6c71d594", |
| 4 | [string]$StepVersion = "0.28.7", |
| 5 | [switch]$Force |
| 6 | ) |
| 7 | |
| 8 | $ErrorActionPreference = "Stop" |
| 9 | |
| 10 | function Write-Log { |
| 11 | param([string]$Message) |
| 12 | Write-Host "" |
| 13 | Write-Host "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] $Message" |
| 14 | } |
| 15 | |
| 16 | function Ensure-Step { |
| 17 | if (Get-Command step -ErrorAction SilentlyContinue) { |
| 18 | Write-Log "step CLI already installed" |
| 19 | return |
| 20 | } |
| 21 | |
| 22 | $msi = "step-cli_${StepVersion}_amd64.msi" |
| 23 | $url = "https://github.com/smallstep/cli/releases/download/v${StepVersion}/${msi}" |
| 24 | $tmp = Join-Path $env:TEMP $msi |
| 25 | |
| 26 | Write-Log "Downloading step CLI" |
| 27 | Invoke-WebRequest -Uri $url -OutFile $tmp |
| 28 | |
| 29 | Write-Log "Installing step CLI" |
| 30 | Start-Process msiexec.exe -Wait -ArgumentList "/i `"$tmp`" /qn" |
| 31 | Remove-Item $tmp -Force |
| 32 | |
| 33 | $env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + |
| 34 | [System.Environment]::GetEnvironmentVariable("Path", "User") |
| 35 | |
| 36 | if (-not (Get-Command step -ErrorAction SilentlyContinue)) { |
| 37 | throw "step CLI installation failed" |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | function Bootstrap-Step { |
| 42 | if ($Force) { |
| 43 | $stepDir = Join-Path $HOME ".step" |
| 44 | if (Test-Path $stepDir) { |
| 45 | Remove-Item -Recurse -Force $stepDir |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | Write-Log "Bootstrapping against $CaUrl" |
| 50 | & step ca bootstrap --ca-url $CaUrl --fingerprint $Fingerprint --install --force |
| 51 | } |
| 52 | |
| 53 | function Install-WindowsTrust { |
| 54 | $rootCert = Join-Path $HOME ".step\certs\root_ca.crt" |
| 55 | if (-not (Test-Path $rootCert)) { |
| 56 | throw "Root certificate not found at $rootCert" |
| 57 | } |
| 58 | |
| 59 | Write-Log "Installing root CA into Local Machine Root store" |
| 60 | Import-Certificate -FilePath $rootCert -CertStoreLocation "Cert:\LocalMachine\Root" | Out-Null |
| 61 | } |
| 62 | |
| 63 | function Verify-Install { |
| 64 | $rootCert = Join-Path $HOME ".step\certs\root_ca.crt" |
| 65 | Write-Log "Installed root CA:" |
| 66 | & step certificate inspect $rootCert --short |
| 67 | Write-Host "" |
| 68 | Write-Host "Done." |
| 69 | } |
| 70 | |
| 71 | Ensure-Step |
| 72 | Bootstrap-Step |
| 73 | Install-WindowsTrust |
| 74 | Verify-Install |