-
Notifications
You must be signed in to change notification settings - Fork 110
/
FixExpiredCert.ps1
382 lines (304 loc) · 15.7 KB
/
FixExpiredCert.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<#
.SYNOPSIS
Script to attempt to replace expired certificate on service fabric cluster
.DESCRIPTION
Usage Instructions: Refer to https://github.com/Azure/Service-Fabric-Troubleshooting-Guides/blob/master/Security/Fix%20Expired%20Cluster%20Certificate%20-%20Automated%20Script
.PARAMETER clusterDataRootPath
service fabric data installation path. default d:\svcfab in azure
.PARAMETER oldThumbPrint
[required][string] old / expired existing certificate thumbprint that needs to be replaced
.PARAMETER newThumbPrint
[required][string] new / valid certificate thumbprint for cluster
.PARAMETER certStoreLocation
[string]default certificate store location 'Cert:\LocalMachine\My\',
.PARAMETER nodeIpArray
[string[]] string array of ip addresses of nodes in cluster
.PARAMETER cacheCredentials
[switch] enable storing credentials in $global:creds variable.
to clear, execute: $global:creds=$null
.PARAMETER localOnly
switch to optionally run script only on local node.
use when there are connectivity issues between nodes by rdp'ing to each node and running with this switch.
.LINK
iwr https://raw.githubusercontent.com/Azure/Service-Fabric-Troubleshooting-Guides/master/Scripts/FixExpiredCert.ps1 -out $pwd/FixExpiredCert.ps1
#>
Param(
[ValidateNotNullOrEmpty()]
[string] $clusterDataRootPath = "D:\SvcFab",
[ValidateNotNullOrEmpty()]
[string]$oldThumbprint = "replace with expired thumbprint",
[ValidateNotNullOrEmpty()]
[string]$newThumbprint = "replace with new thumbprint",
[ValidateNotNullOrEmpty()]
[string]$certStoreLocation = 'Cert:\LocalMachine\My\',
[ValidateNotNullOrEmpty()]
[string[]]$nodeIpArray = @("10.0.0.4", "10.0.0.5", "10.0.0.6" ),
[switch]$cacheCredentials,
[switch]$localOnly
)
$error.Clear()
$ErrorActionPreference = 'continue'
$startTime = get-date
$global:failNodes = @()
$global:successNodes = @()
$count = 0
$creds = $null
If (!(Test-Path $clusterDataRootPath)) {
Write-Host $clusterDataRootPath " not found, exiting."
return
}
$curValue = (get-item wsman:\localhost\Client\TrustedHosts).value
if (![regex]::IsMatch($oldThumbprint, '^[0-9A-Fa-f]{24,}$') -or ![regex]::IsMatch($newThumbprint, '^[0-9A-Fa-f]{24,}$')) {
$errMessage = "verify oldthumbprint:($oldthumbprint) and newthumbprint:($newthumbprint) are specified and are correct."
if($oldThumbprint.Contains(' ') -or $oldThumbprint.Contains(' ')){
write-error $errMessage
return
}
write-warning $errMessage
}
$scriptBlock = { param($clusterDataRootPath, $oldThumbprint, $newThumbprint, $certStoreLocation)
Write-Host "$env:computername : Running on $((Get-WmiObject win32_computersystem).DNSHostName)" -ForegroundColor Green
function StopServiceFabricServices {
if ($(Get-Process | ? ProcessName -like "*FabricInstaller*" | measure).Count -gt 0) {
Write-Warning "$env:computername : Found FabricInstaller running, may cause issues if not stopped, consult manual guide..."
Write-Host "$env:computername : Pausing (15s)..." -ForegroundColor Green
Start-Sleep -Seconds 15
}
$bootstrapAgent = "ServiceFabricNodeBootstrapAgent"
$fabricHost = "FabricHostSvc"
$bootstrapService = Get-Service -Name $bootstrapAgent
if ($bootstrapService.Status -eq "Running") {
Stop-Service $bootstrapAgent -ErrorAction SilentlyContinue
Write-Host "$env:computername : Stopping $bootstrapAgent service" -ForegroundColor Green
}
Do {
Start-Sleep -Seconds 1
$bootstrapService = Get-Service -Name $bootstrapAgent
if(!$bootstrapService) {
break
}
if ($bootstrapService.Status -eq "Stopped") {
Write-Host "$env:computername : $bootstrapAgent now stopped" -ForegroundColor Green
}
else {
Write-Host "$env:computername : $bootstrapAgent current status: $($bootstrapService.Status)" -ForegroundColor Green
}
} While ($bootstrapService.Status -ne "Stopped")
$fabricHostService = Get-Service -Name $fabricHost
if ($fabricHostService.Status -eq "Running") {
Stop-Service $fabricHost -ErrorAction SilentlyContinue
Write-Host "$env:computername : Stopping $fabricHost service" -ForegroundColor Green
}
Do {
Start-Sleep -Seconds 1
$fabricHostService = Get-Service -Name $fabricHost
if(!$fabricHostService) {
break
}
if ($fabricHostService.Status -eq "Stopped") {
Write-Host "$env:computername : $fabricHost now stopped" -ForegroundColor Green
}
else {
Write-Host "$env:computername : $fabricHost current status: $($fabricHostService.Status)" -ForegroundColor Green
}
} While ($fabricHostService.Status -ne "Stopped")
}
function StartServiceFabricServices {
$bootstrapAgent = "ServiceFabricNodeBootstrapAgent"
$fabricHost = "FabricHostSvc"
$fabricHostService = Get-Service -Name $fabricHost
if ($fabricHostService.Status -eq "Stopped") {
Start-Service $fabricHost -ErrorAction SilentlyContinue
Write-Host "$env:computername : Starting $fabricHost service" -ForegroundColor Green
}
Do {
Start-Sleep -Seconds 1
$fabricHostService = Get-Service -Name $fabricHost
if(!$fabricHostService) {
break
}
if ($fabricHostService.Status -eq "Running") {
Write-Host "$env:computername : $fabricHost now running" -ForegroundColor Green
}
else {
Write-Host "$env:computername : $fabricHost current status: $($fabricHostService.Status)" -ForegroundColor Green
}
} While ($fabricHostService.Status -ne "Running")
$bootstrapService = Get-Service -Name $bootstrapAgent
if ($bootstrapService.Status -eq "Stopped") {
Start-Service $bootstrapAgent -ErrorAction SilentlyContinue
Write-Host "$env:computername : Starting $bootstrapAgent service" -ForegroundColor Green
}
Do {
Start-Sleep -Seconds 1
$bootstrapService = Get-Service -Name $bootstrapAgent
if(!$bootstrapService) {
break
}
if ($bootstrapService.Status -eq "Running") {
Write-Host "$env:computername : $bootstrapAgent now running" -ForegroundColor Green
}
else {
Write-Host "$env:computername : $bootstrapAgent current status: $($bootstrapService.Status)" -ForegroundColor Green
}
} While ($bootstrapService.Status -ne "Running")
}
#config files we need
#"D:\SvcFab\clusterManifest.xml"
#"D:\SvcFab\_sys_0\Fabric\Fabric.Data\InfrastructureManifest.xml"
#"D:\SvcFab\_sys_0\Fabric\Fabric.Config.1.131523081591497214\Settings.xml"
$result = Get-ChildItem -Path $clusterDataRootPath -Filter "Fabric.Data" -Directory -Recurse
$hostPath = $result.Parent.Parent.Name
Write-Host "---------------------------------------------------------------------------------------------------------"
Write-Host "---- Working on ip:" $hostPath
Write-Host "---------------------------------------------------------------------------------------------------------"
$manifestPath = $clusterDataRootPath + "\" + $hostPath + "\Fabric\ClusterManifest.current.xml"
$currentPackage = $clusterDataRootPath + "\" + $hostPath + "\Fabric\Fabric.Package.current.xml"
$infrastructureManifest = $clusterDataRootPath + "\" + $hostPath + "\Fabric\Fabric.Data\InfrastructureManifest.xml"
#to get the settings.xml we need to determine the current version
#"D:\SvcFab\_sys_0\Fabric\Fabric.Package.current.xml" --> Read to determine verion# <ConfigPackage Name="Fabric.Config" Version="1.131523081591497214" />
$currentPackageXml = [xml](Get-Content $currentPackage)
$packageName = $currentPackageXml.ServicePackage.DigestedConfigPackage.ConfigPackage | Select-Object -ExpandProperty Name
$packageVersion = $currentPackageXml.ServicePackage.DigestedConfigPackage.ConfigPackage | Select-Object -ExpandProperty Version
$SettingsFile = $clusterDataRootPath + "\" + $hostPath + "\Fabric\" + $packageName + "." + $packageVersion + "\settings.xml"
$SettingsPath = $clusterDataRootPath + "\" + $hostPath + "\Fabric\" + $packageName + "." + $packageVersion
Write-Host "$env:computername : settings file: " $SettingsFile
Write-Host "$env:computername : Settings path: " $SettingsPath
$settings = [xml](Get-Content $SettingsFile)
#TODO: validate newThumbprint is installed
$thumbprintPath = $certStoreLocation + $newThumbprint
If (!(Test-Path $thumbprintPath)) {
Write-Host "$env:computername : $newThumbprint not installed"
return
}
#TODO: validate newThumbprint is ACL'd for NETWORK_SERVICE
#------------------------------------------------------- start ACL
#Change to the location of the local machine certificates
$currentLocation = Get-Location
Set-Location $certStoreLocation
#display list of installed certificates in this store
Get-ChildItem | Format-Table Subject, Thumbprint, SerialNumber -AutoSize
Set-Location $currentLocation
$thumbprint = $certStoreLocation + $newThumbprint
Write-Host "$env:computername : Setting ACL for $thumbprint" -ForegroundColor Green
#get the container name
$cert = get-item $thumbprint
# Specify the user, the permissions and the permission type
$permission = "$("NETWORK SERVICE")", "FullControl", "Allow"
$accessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $permission
# Location of the machine related keys
$keyPath = Join-Path -Path $env:ProgramData -ChildPath "\Microsoft\Crypto\RSA\MachineKeys"
$keyName = $cert.PrivateKey.CspKeyContainerInfo.UniqueKeyContainerName
$keyFullPath = Join-Path -Path $keyPath -ChildPath $keyName
# Get the current acl of the private key
$acl = (Get-Item $keyFullPath).GetAccessControl('Access')
# Add the new ace to the acl of the private key
$acl.SetAccessRule($accessRule)
# Write back the new acl
Set-Acl -Path $keyFullPath -AclObject $acl -ErrorAction Stop
# Observe the access rights currently assigned to this certificate.
get-acl $keyFullPath | Format-List
#------------------------------------------------------- done ACL
# create a temp folder
New-Item -ItemType Directory -Force -Path 'd:\temp\certwork' | out-null
#copy current config to the temp folder
Copy-Item -Path $manifestPath -Destination 'd:\temp\certwork' -Force -Verbose
$newManifest = "D:\temp\certwork\modified_clustermanifest.xml"
Copy-Item -Path $infrastructureManifest -Destination 'd:\temp\certwork' -Force -Verbose
$newInfraManifest = "D:\temp\certwork\modified_InfrastructureManifest.xml"
Copy-Item -Path $SettingsFile -Destination 'd:\temp\certwork' -Force -Verbose
$newSettingsManifest = "D:\temp\certwork\modified_settings.xml"
# find and replace old thumbprint with the new one
(Get-Content "d:\temp\certwork\clustermanifest.current.xml" |
Foreach-Object { $_ -replace $oldThumbprint, $newThumbprint } |
Set-Content $newManifest)
# find and replace old thumbprint with the new one
(Get-Content "d:\temp\certwork\InfrastructureManifest.xml" |
Foreach-Object { $_ -replace $oldThumbprint, $newThumbprint } |
Set-Content $newInfraManifest)
# find and replace old thumbprint with the new one
(Get-Content "d:\temp\certwork\settings.xml" |
Foreach-Object { $_ -replace $oldThumbprint, $newThumbprint } |
Set-Content $newSettingsManifest)
$backupSettingsFile = $SettingsPath + "\settings_backup.xml"
Copy-Item -Path $SettingsFile -Destination $backupSettingsFile -Force -Verbose
Copy-Item -Path $newSettingsManifest -Destination $SettingsFile -Force -Verbose
#stop these services
Write-Host "$env:computername : Stopping services " -ForegroundColor Green
StopServiceFabricServices
#update the node configuration
$logRoot = $clusterDataRootPath + "\Log"
Write-Host "$env:computername : Updating Node configuration with new cert: $newThumbprint" -ForegroundColor Green
New-ServiceFabricNodeConfiguration -FabricDataRoot $clusterDataRootPath -FabricLogRoot $logRoot -ClusterManifestPath $newManifest -InfrastructureManifestPath $newInfraManifest
Write-Host "$env:computername : Updating Node configuration with new cert: complete" -ForegroundColor Green
#restart these services
Write-Host "$env:computername : Starting services " -ForegroundColor Green
StartServiceFabricServices
}
if ($localOnly) {
write-host "executing on local node only"
invoke-command -ScriptBlock $scriptBlock -ArgumentList $clusterDataRootPath, $oldThumbprint, $newThumbprint, $certStoreLocation
return
}
if (!$global:creds) {
Write-Host "Enter your RDP Credentials"
#Get the RDP User Name and Password
$creds = Get-Credential
if ($cacheCredentials) {
$global:creds = $creds
}
}
else{
$creds = $global:creds
}
ForEach ($nodeIpAddress in $nodeIpArray) {
$count++
#Verifying whether corresponding VM is up and running
if (Test-Connection -ComputerName $nodeIpAddress -Quiet) {
$activity = "total minutes: $(((get-date) - $startTime).TotalMinutes.tostring("0.0")). connecting to: $nodeIpAddress ($count of $($nodeIpArray.Count))"
$status = "success: $($global:successNodes | sort -Unique) fail: $($global:failNodes | sort -Unique)"
Write-Progress -Activity $activity `
-Status $status `
-PercentComplete (($count / $nodeIpArray.Count) * 100)
write-host "$env:computername : updating trustedhosts list" -foregroundcolor green
set-item wsman:\localhost\Client\TrustedHosts -value $nodeIpAddress -Force
Start-Sleep(1)
$error.clear()
Invoke-Command -Authentication Negotiate -ComputerName $nodeIpAddress {
Set-NetFirewallRule -DisplayGroup 'File and Printer Sharing' -Enabled True -PassThru |
Select-Object DisplayName, Enabled
} -Credential ($creds)
if ($error) {
$global:failNodes += $nodeIpAddress
continue
}
$error.clear()
Invoke-Command -Authentication Negotiate -Computername $nodeIpAddress -Scriptblock $scriptBlock `
-ArgumentList $clusterDataRootPath, $oldThumbprint, $newThumbprint, $certStoreLocation
if ($error) {
$global:failNodes += $nodeIpAddress
}
else {
$global:successNodes += $nodeIpAddress
}
}
else {
Write-Warning "$env:computername : unable to connect to node: $nodeIpAddress"
$global:failNodes += $nodeIpAddress
}
}
write-host "reset trusted hosts to original values" -foregroundcolor green
set-item wsman:\localhost\Client\TrustedHosts -value $curValue -Force
Write-Progress -Completed -Activity "complete"
if ($global:successNodes) {
$successUnique = $global:successNodes | sort-object -Unique
write-host "total node success: $(@($successUnique).Count)" -ForegroundColor green
write-host ($successUnique | fl * | out-string)
}
if ($global:failNodes) {
$failUnique = $global:failNodes | sort-object -Unique
write-warning "`r`ntotal node connection errors: $(@($failUnique).Count). review output"
write-host ($failUnique | fl * | out-string)
write-warning "for any failed nodes, rdp to node and run this script with '-localOnly' switch"
}
write-host "finished. total minutes: $(((get-date) - $startTime).TotalMinutes.ToString("0.0"))" -foregroundcolor green