-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRADWare WAF DEV man.ps1
781 lines (694 loc) · 39.2 KB
/
RADWare WAF DEV man.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
<#
//-----------------------------------------------------------------------
// Radware Cloud WAF.ps1
//
// Copyright (c) 2021 Caterpillar, Inc. All rights reserved.
//
// This script is specIFic for CAT use and under development.
//-----------------------------------------------------------------------
<field name>|<label text>|<flags>
Bit 1 = Enabled
Bit 2 = Policyable
Bit 3 = Mandatory
-----BEGIN FIELD DEFINITIONS-----
Text1|Text1|000
Text2|Text2|000
Text3|Application Name|110
Text4|Main Domain|110
Text5|IP|110
Text6|Region|110
Option1|Self Signed|110
Option2|Create Application|100
Passwd|Password Field|000
-----END FIELD DEFINITIONS-----
#>
<######################################################################################################################
.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
UserPrivKey : the non-encrypted PEM of the private key 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 auto-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
EncryptPass : the password string to use IF encrypting the remotely generated private key
.NOTES
Returns...
Result : 'Success' or 'NotUsed' to indicate the non-error completion state
AssetName : (optional) the base name used to reference the certIFicate as it was installed on the device;
IF not supplied the auto-generated name is assumed
######################################################################################################################>
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 of the individual SANs
KeySize : the integer key size to be used when creating a key pair
EncryptPass : the password string to use IF encrypting the remotely generated private key
.NOTES
Returns...
Result : 'Success' or 'NotUsed' to indicate the non-error completion state
Pkcs10 : a string representation of the CSR in PKCS#10 format
AssetName : (optional) the base name used to reference the certIFicate as it was installed on the device;
IF not supplied the auto-generated name is assumed
######################################################################################################################>
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="Success"}
}
<######################################################################################################################
.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
)
return @{ Result="NotUsed"; }
}
<######################################################################################################################
.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 base name used to reference the private key as it was installed on the device;
IF not supplied the auto-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
)
return @{ Result="Success"; }
}
<######################################################################################################################
.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 base name used to reference the certIFicate as it was installed on the device;
IF not supplied the auto-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
)
try
{
Get-Date | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
################################################## Auth ##############################################################
$api_user = $General.UserName
$api_pass = $General.UserPass
$global:tenantID = $general.HostAddress
#$certCN = $SpecIFic.SubjectDN.CN
$url = 'https://radware-public.okta.com/api/v1/authn'
#Set up API credentials"
$api_user = $General.UserName
$api_pass = $General.UserPass
$body = @"
{"username":"$api_user",
"password":"$api_pass",
"options":
{
"multiOptionalFactorEnroll": true,
"warnBeforePasswordExpired": true
}
}
"@
<##########################
Authentication request
##########################>
$API_result = Invoke-RestMethod -Uri $url -Method POST -Body $body -ContentType "application/json"
IF ($API_result.status -eq 'SUCCESS') {
$SessionToken = $API_result.sessionToken #Required to get client authorization token
$session_id = $API_result._embedded.user.id
"Session Token" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
<###########################
Get Authorization Token
###########################>
$url = (
"https://radware-public.okta.com/oauth2/aus2m0h583tl6JWsL1t7/v1/authorize?client_id=M1Bx6MXpRXqsv3M1JKa6" +
"&nonce=n-0S6_WzA2M&" +
"prompt=none&" +
"redirect_uri=https%3A%2F%2Fportal.radwarecloud.com%2F&" +
"response_mode=okta_post_message&" +
"response_type=token&" +
"scope=openid%20email%20roles%20tenant&" +
"sessionToken=$SessionToken&" +
"state=af0IFjsldkj"
)
"URL $url" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$API_result = Invoke-RestMethod -Uri $url -Method GET -ContentType "application/json" -UseBasicParsing
"API_result $API_result" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$authorization_token = $null
$authorization_token0 = $null
IF ($API_result.html.head.script.'#text' -match "data.access_token = '(?<ACCESS_TOKEN>.*)'") {
$authorization_token0 = $matches["ACCESS_TOKEN"]
$authorization_token = $authorization_token0.replace("\x2D","-")
}
else
{
return @{ Result= "API Token failure: $($API_result.status)"}
}
}
"End of Auth" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
<##########################
Get all certs loaded in tenant
##########################>
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $authorization_token")
$headers.Add("requestEntityIds", "$TenantID`n")
$headers.Add("applicationID", "")
$Tenantcerts = Invoke-RestMethod 'https://portal.radwarecloud.com/v1/configuration/sslcertificates/' -Method 'GET' -Headers $headers
"All Certs in Tenant" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$Tenantcerts | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
# Gets common name from Venafi
################################ Thumbprint from CertIFicate in Venafi ################################################
$file = (Get-ItemProperty "HKLM:\SOFTWARE\Venafi\Platform")."Base Path" + "Logs\cert.txt"
$SpecIFic.CertPem > $file
$vcert = New-Object System.Security.Cryptography.X509CertIFicates.X509CertIFicate2 $file
$thumbprint = $vcert.thumbprint
"Veanfi Cert fingerprint: $thumbprint" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$certs = @()
$vsub = $vcert.Subject
"vsub: $vsub" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$v = $vsub.Substring(0, $vsub.IndexOf(','))
$vcn = $v.Trim("CN=")
"VCN $vcn" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
# Gets common name from Radware
foreach ($Tcert in $Tenantcerts){
$PD = $Tcert.protectedDomains
IF ($PD -ne '*.generic.com' -and $PD -ne ""){
$Tcert.certificateChain | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$rsub = $Tcert.protectedDomains
"rsub $rsub" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$Tcert.protectedDomains | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$PD | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$r = $rsub.Substring(0, $rsub.IndexOf(';'))
$cn = $r.Trim("CN=")
$certs += $cn #creates array of all certificate common names in Tenant
IF ($vcn -eq $cn) { # Gets Cert data from Radware (IF common name from Venafi matches the common name in Radware tenant)
$global:rcert = $Tcert
$rcn = $cn}
$global:thumb = $rcert.fingerprint
"RCert $rcert" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
}
IF ($PD -eq '*.generic.com'){ # Gets Generic cert data
$Gcert = $Tcert
$fingerprint = $Gcert.fingerprint
}
}
"Certs: $certs" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
"radware cert Thumb : $thumb" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
"Venafi cert thumbprint : $thumbprint" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
"Place holder cert fingerprint : $fingerprint" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
"VCN $vcn" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
"RCN $rcn" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
<######################################################################################################################
######################################################################################################################
New Certificate
######################################################################################################################
$rcn is common name from Radware
$vcn is common name from Venafi
######################################################################################################################>
IF ($vcn -notin $certs){
"If $vcn not in $certs" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
<###############################
Install New Cert
###############################>
$selfsigned = $General.VarBool1.ToString() -eq "True"
$selfsigned| out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
If ($selfsigned) { #Checks if Private or Public CA
#Install Cert
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $authorization_token")
$headers.Add("requestEntityIds", "$TenantID`n")
$headers.Add("selfSigned", "true")
$headers.Add("Content-Type", "application/json")
$body = "{`"certificate`":`"$($Specific.CertPem)`",`"chain`":`"$($Specific.ChainPem)`",`"key`":`"$($Specific.PrivKeyPem)`",`"passphrase`":`"$($Specific.EncryptPass)`"}"
$response = Invoke-RestMethod 'https://portal.radwarecloud.com/v1/configuration/sslcertificates/secret' -Method 'POST' -Headers $headers -Body $body -TimeoutSec 30
$response | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
}
else {
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $authorization_token")
$headers.Add("requestEntityIds", "$TenantID`n")
$headers.Add("selfSigned", "false")
$headers.Add("Content-Type", "application/json")
$body = "{`"certificate`":`"$($Specific.CertPem)`",`"chain`":`"$($Specific.ChainPem)`",`"key`":`"$($Specific.PrivKeyPem)`",`"passphrase`":`"$($Specific.EncryptPass)`"}"
$response = Invoke-RestMethod 'https://portal.radwarecloud.com/v1/configuration/sslcertificates/secret' -Method 'POST' -Headers $headers -Body $body -TimeoutSec 30
$response | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
}
<###############################
Create Application
###############################
$newapp = $General.VarBool2.ToString() -eq "True"
IF ($newapp){$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
"Create Application" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$headers.Add("Authorization", "Bearer $authorization_token")
$headers.Add("requestEntityIds", "$TenantID`n")
$headers.Add("Content-Type", "text/plain")
$body = "{
`n `"applicationName`": `"$Text3`",
`n `"mainDomain`": `"$Text4`",
`n `"fingerprint`": `"$thumb`",
`n `"originServers`": [
`n {
`n `"address`": `"$Text5`",
`n `"addressType`": `"IP`"
`n }
`n ],
`n `"protocol`": `"BOTH`",
`n `"region`": `"$Text6`",
`n `"securityPolicy`": {
`n `"protectionMode`": `"IMMEDIATE`",
`n `"technology`": `"ASP_NET`"
`n }
`n }"
#$response = Invoke-RestMethod 'https://portal.radwarecloud.com/v1/configuration/applications/' -Method 'POST' -Headers $headers -Body $body}
$response | out-file -Append $global:log
#>
} #end IF new cert
"End New Cert" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
<######################################################################################################################
Renewed Certificate
######################################################################################################################
Unbound Certificate
######################################################################################################################
$fingerprint is the app Place holder certificate (*.generic.com) fingerprint
$thumb is the fingerprint of the current certificate in radware
$thumbprint is the fingerprint of the renewed certificate from Venafi
######################################################################################################################>
IF ($vcn -in $certs) { # re-check IF cert exists
"Common name exists" |out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
IF ($rcert.applications.count -eq 0) { # Check IF not bound to any apps
"Unbound Certifgicate" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$rcert.applications.count | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
<###############################
Delete Certificate
###############################>
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("requestEntityIds", "$TenantID`n")
$headers.Add("Authorization","Bearer $authorization_token")
$body = "[{
`n
`n}]"
"Delete Unbound Cert"|out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
"headers" |out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$headers | fl >> $DEBUG_FILE -ErrorAction SilentlyContinue
"Body $body" |out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
"$thumb :: $rcert" |out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$response = Invoke-RestMethod "https://portal.radwarecloud.com/v1/configuration/sslcertificates/$thumb" -Method 'DELETE' -Headers $headers -Body $body
$response | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
Start-Sleep -Milliseconds (5000 * $global:latency_factor)
<###############################
Upload Certificate
###############################>
"Upload Unbound Cert"|out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$selfsigned = $General.VarBool1.ToString() -eq "True"
If ($selfsigned) { #Checks if Private or Public CA
#Install Cert
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $authorization_token")
$headers.Add("requestEntityIds", "$TenantID`n")
$headers.Add("selfSigned", "true")
$headers.Add("Content-Type", "application/json")
$body = "{`"certificate`":`"$($Specific.CertPem)`",`"chain`":`"$($Specific.ChainPem)`",`"key`":`"$($Specific.PrivKeyPem)`",`"passphrase`":`"$($Specific.EncryptPass)`"}"
$response = Invoke-RestMethod 'https://portal.radwarecloud.com/v1/configuration/sslcertificates/secret' -Method 'POST' -Headers $headers -Body $body -TimeoutSec 30
$response | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
}
else {
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $authorization_token")
$headers.Add("requestEntityIds", "$TenantID`n")
$headers.Add("selfSigned", "false")
$headers.Add("Content-Type", "application/json")
$body = "{`"certificate`":`"$($Specific.CertPem)`",`"chain`":`"$($Specific.ChainPem)`",`"key`":`"$($Specific.PrivKeyPem)`",`"passphrase`":`"$($Specific.EncryptPass)`"}"
$response = Invoke-RestMethod 'https://portal.radwarecloud.com/v1/configuration/sslcertificates/secret' -Method 'POST' -Headers $headers -Body $body -TimeoutSec 30
$response | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
}
} #end IF Unbound Cert
<####################################################################################################################
Bound Certificate
######################################################################################################################
$apps isall the applications bound to the certificate
$app is the application being unbound/rebound
######################################################################################################################>
$rcert | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
"App Count $($rcert.applications.count)" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
IF ($rcert.applications.count -ne 0 ) {
"Bound Certificate" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$apps = $rcert.applications |FL
"Apps $apps" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$rcert.applications | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
foreach ($appID in $rcert.applications.applicationUUID) {
<###############################
Get Application Data
###############################>
###############################>
$headers3 = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers3.Add("Authorization", "Bearer $authorization_token")
$headers3.Add("requestEntityIds", "$TenantID`n")
$headers3.Add("applicationID", "$appID")
$App = Invoke-RestMethod "https://portal.radwarecloud.com/v1/gms/applications/$appID" -Method 'GET' -Headers $headers3
"APP $app" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$appname = $app.name
$servicesID =($app.applicationServices[0]).id
$healthID =($app.healthChecks[0]).id
<###############################
Unbind Application
###############################>
"Unbind App" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
set app variables
$GcertID =$Gcert.id
"GcertID $GcertID" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
# UNbind Applications
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("requestEntityIds", "$TenantID`n")
$headers.Add("Authorization","Bearer $authorization_token")
$headers.Add("Content-Type", "application/json")
$headers | FL >> $DEBUG_FILE -ErrorAction SilentlyContinue
$body = "{`"applicationServices`":[{`"id`":`"$servicesID`",`"frontPort`":443,`"backPort`":443,`"type`":`"HTTPS`",`"description`":null,`"enabled`":true}],`"certificateId`":`"$Gcertid`",`"redirect`":null,`"healthChecks`":[{`"id`":`"$healthID`",`"type`":`"TCP`",`"port`":443,`"hostname`":null,`"url`":null,`"responseCode`":null}]}"
$body | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$response = Invoke-RestMethod "https://portal.radwarecloud.com/v1/configuration/applications/$appID/networkConfiguration" -Method 'PUT' -Headers $headers -Body $body
$response | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
}
<###############################
Delete Certificate
###############################>
"5 min wait to delete cert"| out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
Start-Sleep -Seconds (300 * $global:latency_factor) # 5min
" Delete previously bound cert" |out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("requestEntityIds", "$TenantID`n")
$headers.Add("Authorization","Bearer $authorization_token")
$body = "[{
`n
`n}]"
"headers" |out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$headers | fl >> $DEBUG_FILE -ErrorAction SilentlyContinue
"Body $body" |out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
"$thumb :: $rcert" |out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$response = Invoke-RestMethod "https://portal.radwarecloud.com/v1/configuration/sslcertificates/$thumb" -Method 'DELETE' -Headers $headers -Body $body
$response | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
Start-Sleep -Milliseconds (6000 * $global:latency_factor)
"Uploading Cert"| out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
<###############################
Upload Certificate
###############################>
$selfsigned = $General.VarBool1.ToString() -eq "True"
$selfsigned| out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
If ($selfsigned) { #Checks if Private or Public CA
#Install Cert
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $authorization_token")
$headers.Add("requestEntityIds", "$TenantID`n")
$headers.Add("selfSigned", "true")
$headers.Add("Content-Type", "application/json")
$body = "{`"certificate`":`"$($Specific.CertPem)`",`"chain`":`"$($Specific.ChainPem)`",`"key`":`"$($Specific.PrivKeyPem)`",`"passphrase`":`"$($Specific.EncryptPass)`"}"
$response = Invoke-RestMethod 'https://portal.radwarecloud.com/v1/configuration/sslcertificates/secret' -Method 'POST' -Headers $headers -Body $body -TimeoutSec 30
$response | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
}
else {
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $authorization_token")
$headers.Add("requestEntityIds", "$TenantID`n")
$headers.Add("selfSigned", "false")
$headers.Add("Content-Type", "application/json")
$body = "{`"certificate`":`"$($Specific.CertPem)`",`"chain`":`"$($Specific.ChainPem)`",`"key`":`"$($Specific.PrivKeyPem)`",`"passphrase`":`"$($Specific.EncryptPass)`"}"
$response = Invoke-RestMethod 'https://portal.radwarecloud.com/v1/configuration/sslcertificates/secret' -Method 'POST' -Headers $headers -Body $body -TimeoutSec 30
$response | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
}
Start-Sleep -Seconds (20 * $global:latency_factor)
"re-binding $($rcert.applications)"| out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
<###############################
Re-Bind Application
###############################>
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $authorization_token")
$headers.Add("requestEntityIds", "$TenantID`n")
$headers.Add("applicationID", "")
$Tenantcerts = Invoke-RestMethod 'https://portal.radwarecloud.com/v1/configuration/sslcertificates/' -Method 'GET' -Headers $headers
"All Certs in Tenant" | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$Tenantcerts | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
Foreach ($cert in $Tenantcerts){
if ($cert.fingerprint -eq $thumbprint){
$certID = $cert.id }}
foreach ($appID in $rcert.applications.applicationUUID) {
"Rebind $apps" |out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
# Get each app details
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $authorization_token")
$headers.Add("requestEntityIds", "$tenantID")
$certapp = Invoke-RestMethod "https://portal.radwarecloud.com/v1/gms/applications/$appID" -Method 'GET' -Headers $headers
#set app variables
$applicationId = $app.Id
$servicesID =($certapp.applicationServices[0]).id
$healthID =($certapp.healthChecks[0]).id
# Rebind Applications
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("requestEntityIds", "$TenantID`n")
$headers.Add("Authorization","Bearer $authorization_token")
$headers.Add("Content-Type", "application/json")
$headers | FL >> $DEBUG_FILE -ErrorAction SilentlyContinue
$body = "{`"applicationServices`":[{`"id`":`"$servicesID`",`"frontPort`":443,`"backPort`":443,`"type`":`"HTTPS`",`"description`":null,`"enabled`":true}],`"certificateId`":`"$certid`",`"redirect`":null,`"healthChecks`":[{`"id`":`"$healthID`",`"type`":`"TCP`",`"port`":443,`"hostname`":null,`"url`":null,`"responseCode`":null}]}"
$body | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
$response = Invoke-RestMethod "https://portal.radwarecloud.com/v1/configuration/applications/$appID/networkConfiguration" -Method 'PUT' -Headers $headers -Body $body
$response | out-file -Append $DEBUG_FILE -ErrorAction SilentlyContinue
} # Rebind Each previously bound App
} #end If cert is Bound
} # end Renewed Cert
################################################ End of Try #######################################################################
} #end TRY
catch
{
throw $_.Exception.message
}
"Install CertIFicate Finished Successfully"
clear-variable * -Scope Script -ErrorAction SilentlyContinue
return @{ Result="Success"}
}
<######################################################################################################################
.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
)
return @{ Result="Success"}
}
<######################################################################################################################
.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
)
return @{ Result="Success"; CertPem="-----BEGIN CERTIFICATE-----..."; Serial="ABC123"; Thumprint="DEF456" }
}
<######################################################################################################################
.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
)
return @{ Result="NotUsed"; }
}
<######################################################################################################################
.NAME
Remove-CertIFicate
.DESCRIPTION
Removes an existing certIFicate (or private key) from the device. Only implement the body of
this function IF TPP can/should remove old generations of the same asset.
.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 asset 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
)
return @{ Result="Success"; }
}
<###################### THE FUNCTIONS AND CODE BELOW THIS LINE ARE NOT CALLED DIRECTLY BY VENAFI ######################>
$global:latency_factor = 1.0
$global:error_log = (Get-ItemProperty "HKLM:\SOFTWARE\Venafi\Platform")."Base Path" + "Logs\radware-error.log"