generated from tonyjameshart/Radware-WAF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRadware Alteon ADC v2.ps1
882 lines (792 loc) · 38.2 KB
/
Radware Alteon ADC v2.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
<#//-----------------------------------------------------------------------// Radware Alteon ADC.ps1//// Copyright (c) 2016 Venafi, Inc. All rights reserved.//// This sample script and its contents are provided by Venafi to customers// and authorized technology partners for the purposes of integrating with// services and platforms that are not owned or supported by Venafi. Any// sharing of this script or its contents without consent from Venafi is// prohibited.//// This sample is provided "as is" without warranty of any kind.// Requires Posh-SSH : Install-Module -Name Posh-SSH//// Added Push to management servers and Debug functionality - Caterpillar, Inc 3/17/2022//-----------------------------------------------------------------------<#
Adaptable Application Driver Behavior Customization Script
<field name>|<label text>|<flags>
Bit 1 = Enabled
Bit 2 = Policyable
Bit 3 = Mandatory
-----BEGIN FIELD DEFINITIONS-----
Text1|Virtual Server|111
Text2|Service Port|111
Text3|SSL Policy|110
Text4|Certificate Group|110
Text5|Not Used|000
Option1|Save to Startup Config|111
Option2|Push to Management (not VIP)|110
Passwd|Not Used|000
-----END FIELD DEFINITIONS-----
#>
$global:latency_factor = 1.0
$global:error_log = (Get-ItemProperty "HKLM:\SOFTWARE\Venafi\Platform")."Base Path" + "Logs\radwareADC-error.log"
Import-Module -Name Posh-SSH
if ((Get-Module -Name "Posh-SSH") -eq $null)
{
throw "Posh-SSH failed to import"
}
<##################################################################################################
.NAME
Prepare-KeyStore
.DESCRIPTION
Remotely create and/or verify keystore on the hosting platform. Remote generation is considered UNSUPPORTED if this
function is ommitted or commented out.
.PARAMETER General
A hashtable containing the general set of variables needed by all or most functions
HostAddress : a string containing the hostname or IP address specified by the device object
TcpPort : an integer value containing the TCP port specified by the application object
UserName : a string containing the username portion of the credential assigned to the device or application object
UserPass : a string containing the password portion of the credential assigned to the device or application object
AppObjectDN : a string containing the TPP distiguished name of the calling application object
AssetName : a string containing a Venafi standard automatically generated name that can be used for provisioning (<Common Name>-<ValidTo as YYMMDD>-<Last 4 of SerialNum>)
VarText1 : a string value for the text custom field defined by the header at the top of this script
VarText2 : a string value for the text custom field defined by the header at the top of this script
VarText3 : a string value for the text custom field defined by the header at the top of this script
VarText4 : a string value for the text custom field defined by the header at the top of this script
VarText5 : a string value for the text custom field defined by the header at the top of this script
VarBool1 : a boolean value for the yes/no custom field defined by the header at the top of this script (true|false)
VarBool2 : a boolean value for the yes/no custom field defined by the header at the top of this script (true|false)
VarPass : a string value for the password custom field defined by the header at the top of this script
.NOTES
Returns...
Result : 'Success' or 'NotUsed' to indicate the non-error completion state
##################################################################################################>
function Prepare-KeyStore
{
Param(
[Parameter(Mandatory=$true,HelpMessage="General Parameters")]
[System.Collections.Hashtable]$General
)
return @{ Result="NotUsed"; }
}
<##################################################################################################
.NAME
Generate-KeyPair
.DESCRIPTION
Remotely generates a public-private key pair on the hosting platform. Remote generation is
considered UNSUPPORTED if this function is ommitted or commented out.
.PARAMETER General
A hashtable containing the general set of variables needed by all or most functions (see Prepare-Keystore)
.PARAMETER Specific
A hashtable containing the specific set of variables needed by this function
KeySize : the integer key size to be used when creating a key pair
.NOTES
Returns...
Result : 'Success' or 'NotUsed' to indicate the non-error completion state
################################################################################################## >
function Generate-KeyPair
{
Param(
[Parameter(Mandatory=$true,HelpMessage="General Parameters")]
[System.Collections.Hashtable]$General,
[Parameter(Mandatory=$true,HelpMessage="Function Specific Parameters")]
[System.Collections.Hashtable]$Specific
)
return @{ Result="NotUsed"; }
}
<##################################################################################################
.NAME
Generate-CSR
.DESCRIPTION
Remotely generates a CSR on the hosting platform. Remote generation is considered UNSUPPORTED
if this function is ommitted or commented out.
.PARAMETER General
A hashtable containing the general set of variables needed by all or most functions (see Prepare-Keystore)
.PARAMETER Specific
A hashtable containing the specific set of variables needed by this function
SubjectDN : the requested subject distiguished name as a hashtable; OU is a string array; all others are strings
SubjAltNames : hashtable keyed by SAN type; values are string arrays
KeySize : the integer key size to be used when creating a key pair
.NOTES
Returns...
Result : 'Success' or 'NotUsed' to indicate the non-error completion state
Pkcs10 : a string representation of the CSR in PKCS#10 format
################################################################################################## >
function Generate-CSR
{
Param(
[Parameter(Mandatory=$true,HelpMessage="General Parameters")]
[System.Collections.Hashtable]$General,
[Parameter(Mandatory=$true,HelpMessage="Function Specific Parameters")]
[System.Collections.Hashtable]$Specific
)
return @{ Result="NotUsed"; }
}
<##################################################################################################
.NAME
Install-Chain
.DESCRIPTION
Installs the certificate chain on the hosting platform.
.PARAMETER General
A hashtable containing the general set of variables needed by all or most functions (see Prepare-Keystore)
.PARAMETER Specific
A hashtable containing the specific set of variables needed by this function
ChainPem : all chain certificates concatentated together one after the other
ChainPkcs7 : byte array PKCS#7 collection that includes all chain certificates
.NOTES
Returns...
Result : 'Success', 'AlreadyInstalled' or 'NotUsed' to indicate the non-error completion state
##################################################################################################>
function Install-Chain
{
Param(
[Parameter(Mandatory=$true,HelpMessage="General Parameters")]
[System.Collections.Hashtable]$General,
[Parameter(Mandatory=$true,HelpMessage="Function Specific Parameters")]
[System.Collections.Hashtable]$Specific
)
"Install Chain" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$chain = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection
$chain | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
if ( $Specific.ChainPkcs7 )
{
$chain.Import( $Specific.ChainPkcs7 )
}
else # there is no chain for the certificate being provisioned
{
return @{ Result="Success"; }
}
$passwd = ConvertTo-SecureString -String $General.UserPass -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential($General.UserName, $passwd)
$session = New-SSHSession -ComputerName $General.HostAddress -Port $General.TcpPort -AcceptKey -Credential $creds
$stream = $session.Session.CreateShellStream("dumb", 0, 0, 0, 0, 1000)
Start-Sleep -Milliseconds (2000 * $global:latency_factor)
$output = $stream.Read()
$output | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
if ($output -like '*Confirm seeing above note `[y`]:*')
{
# acknowledge that there are pending changes
$stream.Write("y`n")
$stream | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
}
foreach ( $cert in $chain )
{
$b64 = [Convert]::ToBase64String($cert.RawData, [Base64FormattingOptions]::InsertLineBreaks)
$b64 = "-----BEGIN CERTIFICATE-----`n" + $b64 + "`n-----END CERTIFICATE-----"
$stream.Write("/cfg/slb/ssl/certs/import intermca $($cert.SerialNumber) text`n")
$stream.Write("$b64`n`n")
Start-Sleep -Milliseconds (5000 * $global:latency_factor)
$output = $stream.Read()
$output | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
if ( $output -like '*intermca added.*' )
{
if ( $General.VarText4 ) # there is a certificate group to create/update
{
$stream.Write("/cfg/slb/ssl/certs/group $($General.VarText4)`n")
$stream.Write("add`n")
$stream.Write("$($cert.SerialNumber)`n")
$stream | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
Start-Sleep -Milliseconds (3000 * $global:latency_factor)
}
$stream.Write("apply`n")
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
}
}
if ( $General.VarText3 ) # there is an ssl policy to create/update
{
$stream.Write("/cfg/slb/ssl/sslpol $($General.VarText3)`n")
$stream.Write("intermca`n")
$stream.Write("group`n")
$stream.Write("$($General.VarText4)`n")
$stream.Write("ena`n")
$stream | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
Start-Sleep -Milliseconds (2000 * $global:latency_factor)
}
$stream.Write("apply`n")
$stream | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
$stream.Write("exit`n")
$stream | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
$stream.Read() | Out-Null
$stream.Close()
Remove-SSHSession -SSHSession $session | Out-Null
"Removed install chain Session" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
return @{ Result="Success"; }
}
<##################################################################################################
.NAME
Install-PrivateKey
.DESCRIPTION
Installs the private key on the hosting platform.
.PARAMETER General
A hashtable containing the general set of variables needed by all or most functions (see Prepare-Keystore)
.PARAMETER Specific
A hashtable containing the specific set of variables needed by this function
PrivKeyPem : the non-encrypted private key in RSA Base64 PEM format
PrivKeyPemEncrypted : the password encrypted private key in RSA Base64 PEM format
EncryptPass : the string password that was used to encrypt the private key and PKCS#12 keystore
.NOTES
Returns...
Result : 'Success', 'AlreadyInstalled' or 'NotUsed' to indicate the non-error completion state
AssetName : (optional) the name used to reference the private key as it was installed on the device; if not supplied the automatically generated name is assumed
##################################################################################################>
function Install-PrivateKey
{
Param(
[Parameter(Mandatory=$true,HelpMessage="General Parameters")]
[System.Collections.Hashtable]$General,
[Parameter(Mandatory=$true,HelpMessage="Function Specific Parameters")]
[System.Collections.Hashtable]$Specific
)
$result = $null
Assert-NotNullOrEmpty "Private Key Name" $General.AssetName
Assert-NotNullOrEmpty "Private Key PEM" $Specific.PrivKeyPem
$passwd = ConvertTo-SecureString -String $General.UserPass -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential($General.UserName, $passwd)
$session = New-SSHSession -ComputerName $General.HostAddress -Port $General.TcpPort -AcceptKey -Credential $creds
$stream = $session.Session.CreateShellStream("dumb", 0, 0, 0, 0, 1000)
"Install Private Key" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
Start-Sleep -Milliseconds (2000 * $global:latency_factor)
$output = $stream.Read()
$output | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
if ($output -like '*Confirm seeing above note `[y`]:*')
{
# acknowledge that there are pending changes
$stream.Write("y`n")
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
}
$name = Fix-CertKeyName $General.AssetName
$name | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$stream.Write("/cfg/slb/ssl/certs/import key $name text`n")
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
$output = $stream.Read()
$output | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
if ($output -like '*already exists in the repository*')
{
# private key already exists so we're done
$stream.Write("n`n")
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
$result = "AlreadyInstalled"
}
else
{
$stream.Write("$($Specific.PrivKeyPem)`n`n")
Start-Sleep -Milliseconds (4000 * $global:latency_factor)
$output = $stream.Read()
$soutput | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
if ( $output -like '*key added.*' )
{
$stream.Write("apply`n")
$result = "Success"
}
}
$stream.Write("exit`n")
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
$stream.Read() | Out-Null
$stream.Close()
Remove-SSHSession -SSHSession $session | Out-Null
"Removed install priveate key session" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
if ( $result )
{
# this function sets the asset name that all subsequent function calls will need
return @{ Result=$result; AssetName=$name }
}
else
{
$output | Out-File -Append $global:error_log
$soutput | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
throw "Private Key installation failed. " + $($output.Split("`n") | Select-String "Error" | Select -First 1 | % {$_.Line} )
}
}
<##################################################################################################
.NAME
Install-Certificate
.DESCRIPTION
Installs the certificate on the hosting platform. May optionally be used to also install the private key and chain.
Implementing logic for this function is REQUIRED.
.PARAMETER General
A hashtable containing the general set of variables needed by all or most functions (see Prepare-Keystore)
.PARAMETER Specific
A hashtable containing the specific set of variables needed by this function
CertPem : the X509 certificate to be provisioned in Base64 PEM format
PrivKeyPem : the non-encrypted private key in RSA Base64 PEM format
PrivKeyPemEncrypted : the password encrypted private key in RSA Base64 PEM format
ChainPem : all chain certificates concatentated together one after the other
ChainPkcs7 : byte array PKCS#7 collection that includes all chain certificates
Pkcs12 : byte array PKCS#12 collection that includes certificate, private key, and chain
EncryptPass : the string password that was used to encrypt the private key and PKCS#12 keystore
.NOTES
Returns...
Result : 'Success', 'AlreadyInstalled' or 'NotUsed' to indicate the non-error completion state (may only be 'NotUsed' if Install-PrivateKey did not return 'NotUsed')
AssetName : (optional) the name used to reference the certificate as it was installed on the device; if not supplied the automatically generated name is assumed
##################################################################################################>
function Install-Certificate
{
Param(
[Parameter(Mandatory=$true,HelpMessage="General Parameters")]
[System.Collections.Hashtable]$General,
[Parameter(Mandatory=$true,HelpMessage="Function Specific Parameters")]
[System.Collections.Hashtable]$Specific
)
$result = $null
Assert-NotNullOrEmpty "Certificate Name" $General.AssetName
Assert-NotNullOrEmpty "Certificate PEM" $Specific.CertPem
$passwd = ConvertTo-SecureString -String $General.UserPass -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential($General.UserName, $passwd)
$session = New-SSHSession -ComputerName $General.HostAddress -Port $General.TcpPort -AcceptKey -Credential $creds
$stream = $session.Session.CreateShellStream("dumb", 0, 0, 0, 0, 1000)
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
$output = $stream.Read()
"Install Certificate" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$soutput | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
if ($output -like '*Confirm seeing above note `[y`]:*')
{
# acknowledge that there are pending changes
$stream.Write("y`n")
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
}
$stream.Write("/cfg/slb/ssl/certs/import certificate $($General.AssetName) text`n") # srvcrt changed to certificate as per 30.2 and above ---RAJ
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
$output = $stream.Read()
$soutput | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
if ($output -like '*already exists in the repository*')
{
# private key already exists so we're done
$stream.Write("n`n")
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
$result = "AlreadyInstalled"
}
else
{
$stream.Write("$($Specific.CertPem)`n")
Start-Sleep -Milliseconds (5000 * $global:latency_factor)
$output = $stream.Read()
$soutput | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
if ( $output -like '*certificate added.*' )# modified '*srvrcrt added* to * added * as the string changed from srvrcrt to certificate ---RAJ
{
$stream.Write("apply`n")
$result = "Success"
}
}
$stream.Write("exit`n")
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
$stream.Read() | Out-Null
$stream.Close()
Remove-SSHSession -SSHSession $session | Out-Null
"Removed install certificate session" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
if ( $result )
{
return @{ Result=$result; }
}
else
{
$output | Out-File -Append $global:error_log
$soutput | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
throw "Certificate installation failed. " + $($output.Split("`n") | Select-String "Error" | Select -First 1 | % {$_.Line})
}
}
<##################################################################################################
.NAME
Update-Binding
.DESCRIPTION
Binds the installed certificate with the consuming application or service on the hosting platform
.PARAMETER General
A hashtable containing the general set of variables needed by all or most functions (see Prepare-Keystore)
.NOTES
Returns...
Result : 'Success' or 'NotUsed' to indicate the non-error completion state
##################################################################################################>
function Update-Binding
{
Param(
[Parameter(Mandatory=$true,HelpMessage="General Parameters")]
[System.Collections.Hashtable]$General
)
"Update Binding" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
if ( $General.VarBool2 )
{
"Update Management $General.HostAddress" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
Update-Management
}
else{
"Update $General.VarText1" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
Assert-NotNullOrEmpty "Certificate Name" $General.AssetName
Assert-NotNullOrEmpty "Virtual Server" $General.VarText1
Assert-NotNullOrEmpty "Service Port" $General.VarText2
$passwd = ConvertTo-SecureString -String $General.UserPass -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential($General.UserName, $passwd)
$session = New-SSHSession -ComputerName $General.HostAddress -Port $General.TcpPort -AcceptKey -Credential $creds
$stream = $session.Session.CreateShellStream("dumb", 0, 0, 0, 0, 1000)
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
$output = $stream.Read()
if ($output -like '*Confirm seeing above note `[y`]:*')
{
# acknowledge that there are pending changes
$stream.Write("y`n")
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
}
$stream.Write("/cfg/slb/virt $($General.VarText1)`n")
$stream.Write("service $($General.VarText2) https`n`n")
Start-Sleep -Milliseconds (2000 * $global:latency_factor)
if ( $General.VarText3 ) # there is an ssl policy
{
"SSL Policy: $($General.VarText3)"| out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$stream.Write("ssl/sslpol`n")
$stream.Write("$($General.VarText3)`n")
Start-Sleep -Milliseconds (2000 * $global:latency_factor)
$stream.Write("/cfg/slb/virt $($General.VarText1)`n")
$stream.Write("service $($General.VarText2) https`n`n")
Start-Sleep -Milliseconds (2000 * $global:latency_factor)
}
$stream.Write("ssl/srvrcert cert $($General.AssetName)`n")
Start-Sleep -Milliseconds (2000 * $global:latency_factor)
$output = $stream.Read()
$output | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
if ( $output -like '*For certificate*' )
{
$stream.Write("apply`n")
if ( $General.VarBool1 )
{
"Save to Startup"| out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
Start-Sleep -Milliseconds (9000 * $global:latency_factor)
$stream.Write("save`n")
$stream.Write("y`n")
}
$stream.Write("exit`n")
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
$stream.Read() | Out-Null
$stream.Close()
Remove-SSHSession -SSHSession $session | Out-Null
"End Update $($General.VarText3) Binding session"| out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
return @{ Result="Success"; }
}
else
{
$output | Out-File -Append $global:error_log
$output | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
throw "Bind certificate to consumer failed. " + $($output.Split("`n") | Select-String "Error" | Select -First 1 | % {$_.Line})
$stream.Write("exit`n")
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
$stream.Read() | Out-Null
$stream.Close()
Remove-SSHSession -SSHSession $session | Out-Null
}
}
}
<##################################################################################################
.NAME
Update-Management
.DESCRIPTION
Binds the installed certificate with the consuming application or service on the hosting platform
Management Devices
.PARAMETER General
A hashtable containing the general set of variables needed by all or most functions (see Prepare-Keystore)
.NOTES
Returns...
Result : 'Success' or 'NotUsed' to indicate the non-error completion state
##################################################################################################>
function Update-Management
{
Param(
[Parameter(Mandatory=$true,HelpMessage="General Parameters")]
[System.Collections.Hashtable]$General
)
"Update Binding on Management" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
Assert-NotNullOrEmpty "Certificate Name" $General.AssetName
$passwd = ConvertTo-SecureString -String $General.UserPass -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential($General.UserName, $passwd)
$session = New-SSHSession -ComputerName $General.HostAddress -Port $General.TcpPort -AcceptKey -Credential $creds
$stream = $session.Session.CreateShellStream("dumb", 0, 0, 0, 0, 1000)
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
$output = $stream.Read()
$output | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
if ($output -like '*Confirm seeing above note `[y`]:*')
{
# acknowledge that there are pending changes
$stream.Write("y`n")
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
}
$stream.Write("/cfg/sys/access/https/ cert $($General.AssetName)`n")
Start-Sleep -Milliseconds (2000 * $global:latency_factor)
$output = $stream.Read()
$output | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$output + "`n" | Out-File -FilePath C:\Users\Administrator\Desktop\script_debug.txt -Append
if ( $output -like '*For certificate*' ) #Removed "Server" and truncated the string, as there is no "Server" ---RAJ
{
$stream.Write("apply`n")
if ( $General.VarBool1 )
{"Save to Startup" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
Start-Sleep -Milliseconds (10000 * $global:latency_factor) #Added sleep ---RAJ
$stream.Write("save`n")
$stream.Write("y`n")
}
$stream.Write("exit`n")
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
$stream.Read() | Out-Null
$stream.Close()
Remove-SSHSession -SSHSession $session | Out-Null
return @{ Result="Success"; }
}
else
{
$output | Out-File -Append $global:error_log
$output | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
throw "Bind certificate to consumer failed. " + $($output.Split("`n") | Select-String "Error" | Select -First 1 | % {$_.Line})
$stream.Write("exit`n")
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
$stream.Read() | Out-Null
$stream.Close()
Remove-SSHSession -SSHSession $session | Out-Null
}
}
<##################################################################################################
.NAME
Activate-Certificate
.DESCRIPTION
Performs any post-installation operations necessary to make the certificate active (such as restarting a service)
.PARAMETER General
A hashtable containing the general set of variables needed by all or most functions (see Prepare-Keystore)
.NOTES
Returns...
Result : 'Success' or 'NotUsed' to indicate the non-error completion state
##################################################################################################>
function Activate-Certificate
{
Param(
[Parameter(Mandatory=$true,HelpMessage="General Parameters")]
[System.Collections.Hashtable]$General
)
return @{ Result="NotUsed"; }
}
<##################################################################################################
.NAME
Extract-Certificate
.DESCRIPTION
Extracts the active certificate from the hosting platform. If the platform does not provide a method for exporting the
raw certificate then it is sufficient to return only the Serial and Thumprint. This function is REQUIRED.
.PARAMETER General
A hashtable containing the general set of variables needed by all or most functions (see Prepare-Keystore)
.NOTES
Returns...
Result : 'Success' or 'NotUsed' to indicate the non-error completion state
CertPem : the extracted X509 certificate referenced by AssetName in Base64 PEM format
Serial : the serial number of the X509 certificate refernced by AssetName
Thumbprint : the SHA1 thumprint of the X509 certificate referenced by AssetName
##################################################################################################>
function Extract-Certificate
{
Param(
[Parameter(Mandatory=$true,HelpMessage="General Parameters")]
[System.Collections.Hashtable]$General
)
"Extract Certificate"| out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
Assert-NotNullOrEmpty "Certificate Key Name" $General.AssetName
$passwd = ConvertTo-SecureString -String $General.UserPass -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential($General.UserName, $passwd)
$session = New-SSHSession -ComputerName $General.HostAddress -Port $General.TcpPort -AcceptKey -Credential $creds
$stream = $session.Session.CreateShellStream("dumb", 0, 0, 0, 0, 1000)
Start-Sleep -Milliseconds (2000 * $global:latency_factor)
$output = $stream.Read()
$output | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
if ($output -like '*Confirm seeing above note `[y`]:*')
{
# acknowledge that there are pending changes
$stream.Write("y`n")
}
$stream.Write("/cfg/slb/ssl/certs/export srvrcert $($General.AssetName) text`n`n")
Start-Sleep -Milliseconds (5000 * $global:latency_factor)
$output = $stream.Read()
$output | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$header = "-----BEGIN CERTIFICATE-----"
$footer = "-----END CERTIFICATE-----"
if ( $output -like "*$header*" )
{
$CertPem = ($output -split $header,2)[1]
$CertPem = ($CertPem -split $footer,2)[0]
$CertPem = $header + "`n" + $CertPem.Trim() + "`n" + $footer
$stream.Write("exit`n")
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
$stream.Read() | Out-Null
$stream.Close()
Remove-SSHSession -SSHSession $session | Out-Null
return @{ Result="Success"; CertPem=$CertPem }
}
else
{
$output | Out-File -Append $global:error_log
$output | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
throw "Certificate extraction failed. " + $($output.Split("`n") | Select-String "Error" | Select -First 1 | % {$_.Line} )
}
}
<##################################################################################################
.NAME
Extract-PrivateKey
.DESCRIPTION
Extracts the private key associated with the certificate from the hosting platform
.PARAMETER General
A hashtable containing the general set of variables needed by all or most functions (see Prepare-Keystore)
.PARAMETER Specific
A hashtable containing the specific set of variables needed by this function
EncryptPass : the string password to use when encrypting the private key
.NOTES
Returns...
Result : 'Success' or 'NotUsed' to indicate the non-error completion state
PrivKeyPem : the extracted private key in RSA Base64 PEM format (encrypted or not)
##################################################################################################>
function Extract-PrivateKey
{
Param(
[Parameter(Mandatory=$true,HelpMessage="General Parameters")]
[System.Collections.Hashtable]$General,
[Parameter(Mandatory=$true,HelpMessage="Function Specific Parameters")]
[System.Collections.Hashtable]$Specific
)
"Extract Private Key" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
Assert-NotNullOrEmpty "Private Key Name" $General.AssetName
Assert-NotNullOrEmpty "Private Key Password" $Specific.EncryptPass
$passwd = ConvertTo-SecureString -String $General.UserPass -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential($General.UserName, $passwd)
$session = New-SSHSession -ComputerName $General.HostAddress -Port $General.TcpPort -AcceptKey -Credential $creds
$stream = $session.Session.CreateShellStream("dumb", 0, 0, 0, 0, 1000)
Start-Sleep -Milliseconds (2000 * $global:latency_factor)
$output = $stream.Read()
$output | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
if ($output -like '*Confirm seeing above note `[y`]:*')
{
# acknowledge that there are pending changes
$stream.Write("y`n")
}
$stream.Write("/cfg/slb/ssl/certs/export key $($General.AssetName) `"$($Specific.EncryptPass)`" text`n`n")
Start-Sleep -Milliseconds (5000 * $global:latency_factor)
$output = $stream.Read()
$output | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$header = "-----BEGIN RSA PRIVATE KEY-----"
$footer = "-----END RSA PRIVATE KEY-----"
if ( $output -like "*$header*" )
{
$KeyPem = ($output -split $header,2)[1]
$KeyPem = ($KeyPem -split $footer,2)[0]
$KeyPem = $header + "`n" + $KeyPem.Trim() + "`n" + $footer
$stream.Write("exit`n")
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
$stream.Read() | Out-Null
$stream.Close()
Remove-SSHSession -SSHSession $session | Out-Null
return @{ Result="Success"; PrivKeyPem=$KeyPem }
}
else
{
$output | Out-File -Append $global:error_log
$output | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
throw "Private Key extraction failed. " + $($output.Split("`n") | Select-String "Error" | Select -First 1 | % {$_.Line} )
}
}
<##################################################################################################
.NAME
Remove-Certificate
.DESCRIPTION
Removes an existing certificate from IAM. Only implement the body of this function
if TPP can/should remove old generations of the same certificate.
.PARAMETER General
A hashtable containing the general set of variables needed by all or most functions (see Prepare-Keystore)
.PARAMETER Specific
A hashtable containing the specific set of variables needed by this function
AssetNameOld : the name of a certificate that was previously replaced and should be deleted
.NOTES
Returns...
Result : 'Success' or 'NotUsed' to indicate the non-error completion state
##################################################################################################>
function Remove-Certificate
{
Param(
[Parameter(Mandatory=$true,HelpMessage="General Parameters")]
[System.Collections.Hashtable]$General,
[Parameter(Mandatory=$true,HelpMessage="Function Specific Parameters")]
[System.Collections.Hashtable]$Specific
)
"Remove Certificate" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
Assert-NotNullOrEmpty "Old Certificate Name" $Specific.AssetNameOld
$passwd = ConvertTo-SecureString -String $General.UserPass -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential($General.UserName, $passwd)
$session = New-SSHSession -ComputerName $General.HostAddress -Port $General.TcpPort -AcceptKey -Credential $creds
$stream = $session.Session.CreateShellStream("dumb", 0, 0, 0, 0, 1000)
Start-Sleep -Milliseconds (2000 * $global:latency_factor)
$output = $stream.Read()
$output | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
if ($output -like '*Confirm seeing above note `[y`]:*')
{
# acknowledge that there are pending changes
$stream.Write("y`n")
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
}
$stream.Write("/cfg/slb/ssl/certs/key $($Specific.AssetNameOld)`n")
$stream.Write("del`n")
$stream.Write("both`n") # requires an additional argument to remove certificate and CSR
Start-Sleep -Milliseconds (5000 * $global:latency_factor)
$output = $stream.Read()
$output | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
if ( $output -like "*request *$($Specific.AssetNameOld)* deleted*" ) #Change
{
$stream.Write("apply`n")
if ( $General.VarBool1 )
{
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
$stream.Write("save`n")
$stream.Write("y`n")
}
$stream.Write("exit`n")
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
$stream.Read() | Out-Null
$stream.Close()
Remove-SSHSession -SSHSession $session | Out-Null
return @{ Result="Success"; }
}
# that didn't work as expected so verify that the srvrcert does not exist
$stream.Write("/cfg/slb/ssl/certs/srvrcert $($Specific.AssetNameOld)`n")
$stream.Write("cur`n")
Start-Sleep -Milliseconds (5000 * $global:latency_factor)
$output = $stream.Read()
$output | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
if ( $output -like "*Certificate : not generated*" ) # removed "Server" as part of the string
{
$stream.Write("exit`n")
Start-Sleep -Milliseconds (1000 * $global:latency_factor)
$stream.Read() | Out-Null
$stream.Close()
Remove-SSHSession -SSHSession $session | Out-Null
return @{ Result="Success"; }
}
else
{
$output | Out-File -Append $global:error_log
$output | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
throw "Certificate removal failed. " + $($output.Split("`n") | Select-String "Error" | Select -First 1 | % {$_.Line} )
}
}
<########## THE FUNCTIONS AND CODE BELOW THIS LINE ARE NOT CALLED DIRECTLY BY VENAFI ##########>
function Assert-NotNullOrEmpty( [string]$name, [string]$value )
{
if ( !$value )
{
throw "$($name) is required."
}
}
function Fix-CertKeyName ( [string]$name )
{
# cert/key name must be less than 32 characters
if ( $name.Length -gt 32 )
{
$parts = $name.Split("_")
$name = "_" + $parts[1] + "_" + $parts[2] # keep the suffix as-is
$name = $parts[0].Substring(0, 32 - $name.Length) + $name
}
$name | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
# cert/key name may not include periods or special characters other than - and _
return $name -Replace "\.","-"
}