-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAvi-Networks.ps1
1123 lines (1013 loc) · 50.6 KB
/
Avi-Networks.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
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
//-----------------------------------------------------------------------
// Avi Networks.ps1
//
// Copyright (c) 2017 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.
//-----------------------------------------------------------------------
Modifications to Avi Networks driver coding:
- changed naming convention for discovered certificates to "vs_name (tenant_name)" to better avoid collisions
- extract and save certificate name for proper validation after discovery ... bad bug!
- fix get-allvs to actually return more than 25 virtual services by supporting pagination ... another bad bug!
- logging has been extensively overhauled to utilize better separation of logs, more useful log filenames,
-- and an end to multiple discovery processes writing into the same logs which greatly reduces their usefulness.
<field name>|<label text>|<flags>
Bit 1 = Enabled
Bit 2 = Policyable
Bit 3 = Mandatory
-----BEGIN FIELD DEFINITIONS-----
Text1|Virtual Service|100
Text2|Tenant|100
Text3|Not Used|000
Text4|Not Used|000
Text5|Not Used|000
Option1|Debug Avi Driver|110
Option2|Not Used|000
Passwd|Not Used|000
-----END FIELD DEFINITIONS-----
#>
$Script:AdaptableAppVer = "202404301045"
$Script:AdaptableAppDrv = "Avi-Networks"
# need the following to interface with an untrusted certificate
Add-Type -TypeDefinition @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
<##################################################################################################
.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
{
# This line tells VS Code to not flag this function's name as a "problem"
[Diagnostics.CodeAnalysis.SuppressMessage('PSUseApprovedVerbs', '', Justification='Forced by Venafi', Scope='function')]
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
{
# This line tells VS Code to not flag this function's name as a "problem"
[Diagnostics.CodeAnalysis.SuppressMessage('PSUseApprovedVerbs', '', Justification='Forced by Venafi', Scope='function')]
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
{
# This line tells VS Code to not flag this function's name as a "problem"
[Diagnostics.CodeAnalysis.SuppressMessage('PSUseApprovedVerbs', '', Justification='Forced by Venafi', Scope='function')]
Param(
[Parameter(Mandatory=$true,HelpMessage="General Parameters")]
[System.Collections.Hashtable]$General,
[Parameter(Mandatory=$true,HelpMessage="Function Specific Parameters")]
[System.Collections.Hashtable]$Specific
)
Initialize-VenDebugLog -General $General
if ( -not $($Specific.SubjectDn.CN) ) {
throw "Common Name is required by Avi Vantage for remotely generated CSRs."
}
else {
$subject = @{ "common_name"=$($Specific.SubjectDn.CN) }
}
if ( $Specific.SubjectDn.OU ) {
# only one OU is currently supported so we always use the first if there are many
$subject.Add( "organization_unit", $Specific.SubjectDn.OU[0] )
}
if ( $Specific.SubjectDn.O ) {
$subject.Add( "organization", $Specific.SubjectDn.O )
}
if ( $Specific.SubjectDn.L ) {
$subject.Add( "locality", $Specific.SubjectDn.L )
}
if ( $Specific.SubjectDn.ST ) {
$subject.Add( "state", $Specific.SubjectDn.ST )
}
if ( $Specific.SubjectDn.C ) {
$subject.Add( "country", $Specific.SubjectDn.C )
}
$sans_dns = @() + $($Specific.SubjAltNames.DNS | Where-Object {$_}) # an array of DNS Subject Alternative Names, possibly empty
try {
$session = Get-AviSession $General.HostAddress $General.UserName $General.UserPass
$headers = @{
"content-type"="application/json";
"referer"="https://$($General.HostAddress)";
"X-Avi-Tenant"=$General.VarText2;
"x-csrftoken"=$($session.Cookies.GetCookies("https://$($General.HostAddress)")["csrftoken"].Value)
}
[string] $url = "https://$($General.HostAddress)/api/sslkeyandcertificate"
$body = @{
"name"=$General.AssetName;
"certificate"=@{
"self_signed"=$false;
"subject"=$subject;
"subject_alt_names"=$sans_dns;
};
"type"="SSL_CERTIFICATE_TYPE_VIRTUALSERVICE";
"key_params"=@{
"algorithm"="SSL_KEY_ALGORITHM_RSA";
"rsa_params"=@{
"key_size"=$("SSL_KEY_" + $Specific.KeySize + "_BITS")
}
}
} | ConvertTo-Json
Write-VenDebugLog "Creating new remotely generated CSR for '$($General.AssetName)'"
$resp_obj = Invoke-AviRestMethod -Uri $url -Method Post -Headers $headers -Body $body -ContentType "application/json" -WebSession $session
$csr_uuid = $resp_obj.response.uuid
Write-VenDebugLog "New CSR created (CSR UUID: $($csr_uuid))"
[string] $url = "https://$($General.HostAddress)/api/sslkeyandcertificate/$csr_uuid"
$resp_obj = Invoke-AviRestMethod -Uri $url -Method Get -Headers $headers -ContentType "application/json" -WebSession $session
$csr = $resp_obj.response.certificate.certificate_signing_request
Write-VenDebugLog 'Returning control to Venafi'
return @{ Result="Success"; Pkcs10=$csr }
}
catch {
throw Select-ErrorMessage($_.Exception)
}
}
<##################################################################################################
.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
)
Initialize-VenDebugLog -General $General
try
{
$session = Get-AviSession $General.HostAddress $General.UserName $General.UserPass
$headers = @{
"content-type"="application/json";
"referer"="https://$($General.HostAddress)";
"X-Avi-Tenant"=$General.VarText2;
"x-csrftoken"=$($session.Cookies.GetCookies("https://$($General.HostAddress)")["csrftoken"].Value)
}
if ( $Specific.ChainPkcs7 )
{
$chain = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection
$chain.Import( $Specific.ChainPkcs7 )
foreach ( $cert in $chain )
{
$is_installed = $false
$cacert_name = $cacert_basename = Get-CACertName $cert
for ( $i=0; $i -lt 10; $i++ )
{
Write-VenDebugLog "Preparing to upload CA certificate: [$($cacert_name)]"
# check to see if a certificate already exists by the name
$url = "https://$($General.HostAddress)/api/sslkeyandcertificate?name=$cacert_name&export_key=false"
$resp_obj = Invoke-AviRestMethod -Uri $url -Method Get -Headers $headers -ContentType "application/json" -WebSession $session
$response = $resp_obj.response
if ( $response.Count -ne 0 ) # name collision, is it the same cert?
{
$pem = $response.results.certificate.certificate
$pem = $pem.Replace("-----BEGIN CERTIFICATE-----","").Replace("-----END CERTIFICATE-----","")
$cert_existing = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$cert_existing.Import([Convert]::FromBase64String($pem))
if ( $cert_existing.Equals($cert) ) # this certificate is already installed
{
Write-VenDebugLog "CA certificate '$($cacert_name)' has already been installed."
$is_installed = $true
break
}
else # append an integer and test that name for uniqueness
{
Write-VenDebugLog "CA certificate '$($cacert_name)' exists, but does not match!"
$cacert_name = $cacert_basename + "_" + $idx
}
}
else # install the CA certificate
{
$pem = [Convert]::ToBase64String($cert.RawData, [System.Base64FormattingOptions]::InsertLineBreaks)
$url = "https://$($General.HostAddress)/api/sslkeyandcertificate"
$body = @{
"certificate"=@{
"certificate"="-----BEGIN CERTIFICATE-----`n$pem`n-----END CERTIFICATE-----"
};
"name"=$cacert_name;
"type"="SSL_CERTIFICATE_TYPE_CA"
} | ConvertTo-Json
Invoke-AviRestMethod -Uri $url -Method Post -Headers $headers -Body $body -ContentType "application/json" -WebSession $session
Write-VenDebugLog "CA certificate '$($cacert_name)' has been uploaded."
$is_installed = $true
break
}
}
if ( -not $is_installed )
{
Write-VenDebugLog "Conflict resolution failed for '$($cacert_basename)'"
throw "Automatic name conflict resolution threshold was exceeded for '" + $cacert_basename + "'"
}
}
}
Write-VenDebugLog "CA chain installed - Returning control to Venafi"
return @{ Result="Success"; }
}
catch
{
throw Select-ErrorMessage($_.Exception)
}
}
<##################################################################################################
.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="NotUsed" }
}
<##################################################################################################
.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
)
Initialize-VenDebugLog -General $General
try {
$session = Get-AviSession $General.HostAddress $General.UserName $General.UserPass
$cert = $Specific.CertPem
$key = $Specific.PrivKeyPem
$headers = @{
"content-type"="application/json";
"referer"="https://$($General.HostAddress)";
"X-Avi-Tenant"=$General.VarText2;
"x-csrftoken"=$($session.Cookies.GetCookies("https://$($General.HostAddress)")["csrftoken"].Value)
}
Write-VenDebugLog "Preparing to upload certificate: [$($General.AssetName)]"
if ( $key ) # this is central generation
{
# check to see if a certificate already exists by the name
$url = "https://$($General.HostAddress)/api/sslkeyandcertificate?name=$($General.AssetName)&export_key=false"
$resp_obj = Invoke-AviRestMethod -Uri $url -Method Get -Headers $headers -ContentType "application/json" -WebSession $session
$response = $resp_obj.response
if ( $response.Count -ne 0 ) # name collision, is it the same cert?
{
$pem = $cert.Replace("-----BEGIN CERTIFICATE-----","").Replace("-----END CERTIFICATE-----","")
$cert_installing = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$cert_installing.Import([Convert]::FromBase64String($pem))
$pem = $response.results.certificate.certificate
$pem = $pem.Replace("-----BEGIN CERTIFICATE-----","").Replace("-----END CERTIFICATE-----","")
$cert_existing = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$cert_existing.Import([Convert]::FromBase64String($pem))
if ( $cert_existing.Equals($cert_installing) ) # this certificate is already installed
{
Write-VenDebugLog "Certificate is already installed - Returning control to Venafi"
return @{ Result="AlreadyInstalled"; }
}
else {
Write-VenDebugLog "ERROR: A different certificate is installed as '$($General.AssetName)' - Returning control to Venafi"
throw "A different certificate already exists with the name '" + $General.AssetName + "'"
}
}
$url = "https://$($General.HostAddress)/api/sslkeyandcertificate"
$body = @{
"certificate"=@{
"certificate"=$cert;
};
"key"=$key;
"name"=$General.AssetName;
"type"="SSL_CERTIFICATE_TYPE_VIRTUALSERVICE"
} | ConvertTo-Json
# Note: this call requires the x-csrftoken HTTP header to avoid (401) Unauthorized
Invoke-AviRestMethod -Uri $url -Method Post -Headers $headers -Body $body -ContentType "application/json" -WebSession $session
Write-VenDebugLog "Certificate has been uploaded as '$($General.AssetName)' - Returning control to Venafi"
return @{ Result="Success"; }
}
else # this is remote generation
{
# lookup the CSR
Write-VenDebugLog "Searching for remotely generated CSR for '$($General.AssetName)'"
$url = "https://$($General.HostAddress)/api/sslkeyandcertificate?name=$($General.AssetName)&export_key=false"
$resp_obj = Invoke-AviRestMethod -Uri $url -Method Get -Headers $headers -ContentType "application/json" -WebSession $session
$response = $resp_obj.response
if ( $response.Count -ne 0 ) # found it
{
if ( -not $response.results.certificate.certificate ) {
# install the certificate
$url = $response.results.url
$response.results.certificate | Add-Member -MemberType NoteProperty -Name 'certificate' -Value $cert
$body = $response.results | ConvertTo-Json
# Note: this call requires the x-csrftoken HTTP header to avoid (401) Unauthorized
Invoke-AviRestMethod -Uri $url -Method Put -Headers $headers -Body $body -ContentType "application/json" -WebSession $session
Write-VenDebugLog "Certificate has been uploaded as '$($General.AssetName)' - Returning control to Venafi"
return @{ Result="Success"; }
}
else {
Write-VenDebugLog "ERROR: A different certificate is installed as '$($General.AssetName)' - Returning control to Venafi"
throw "A certificate is unexpectedly already installed on '" + $General.AssetName + "'"
}
}
else {
Write-VenDebugLog "ERROR: Remotely generated CSR not found for '$($General.AssetName)'"
throw "Remotely generated CSR was not found with name '" + $General.AssetName + "'"
}
}
}
catch {
throw Select-ErrorMessage($_.Exception)
}
}
<##################################################################################################
.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
)
Initialize-VenDebugLog -General $General
$session = Get-AviSession $General.HostAddress $General.UserName $General.UserPass
$tenant_name = $General.VarText2
$headers = @{
"content-type"="application/json";
"referer"="https://$($General.HostAddress)";
"X-Avi-Tenant"=$tenant_name;
"x-csrftoken"=$($session.Cookies.GetCookies("https://$($General.HostAddress)")["csrftoken"].Value)
}
$vs_name = $General.VarText1
# Get Virtual Service UUID
$url = "https://$($General.HostAddress)/api/virtualservice?name={0}" -f $vs_name
Write-VenDebugLog "Retrieving configuration for virtual service '$($vs_name)'"
$resp_obj = Invoke-AviRestMethod -Uri $url -Method Get -Headers $headers -WebSession $session
$resp_vs = $resp_obj.response
if ($resp_vs.count -eq 0) {
Write-VenDebugLog "Could not find virtual service '$($vs_name)'"
throw "Could not find virtual service '$($vs_name)'"
}
$vs_uuid = $resp_vs.results[0].uuid
# Get Certificate UUID
$cert_avi_name = $General.AssetName
$url = "https://$($General.HostAddress)/api/sslkeyandcertificate?name={0}" -f $cert_avi_name
$resp_obj = Invoke-AviRestMethod -Uri $url -Method Get -Headers $headers -WebSession $session
$resp = $resp_obj.response
if($resp.count -eq 0) {
Write-VenDebugLog "Certificate '$($cert_avi_name)' not found!"
throw "Certificate '$($cert_avi_name)' not found!"
}
$cert_uuid = $resp.results[0].url
# Associate certificate with virtual service instance
$url = "https://$($General.HostAddress)/api/virtualservice/" + $vs_uuid
$body = $resp_vs.results[0]
$body.ssl_key_and_certificate_refs=@( $cert_uuid )
$body = $body | ConvertTo-Json -depth 100
# Note: this call requires the x-csrftoken HTTP header to avoid (401) Unauthorized
Write-VenDebugLog "Binding certificate '$($cert_avi_name)' to virtual service '$($vs_name)'"
Invoke-AviRestMethod -Uri $url -Method Put -Headers $headers -Body $body -ContentType "application/json" -WebSession $session
Write-VenDebugLog "Virtual service has been updated - Returning control to Venafi"
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
{
# This line tells VS Code to not flag this function's name as a "problem"
[Diagnostics.CodeAnalysis.SuppressMessage('PSUseApprovedVerbs', '', Justification='Forced by Venafi', Scope='function')]
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
{
# This line tells VS Code to not flag this function's name as a "problem"
[Diagnostics.CodeAnalysis.SuppressMessage('PSUseApprovedVerbs', '', Justification='Forced by Venafi', Scope='function')]
Param(
[Parameter(Mandatory=$true,HelpMessage="General Parameters")]
[System.Collections.Hashtable]$General
)
Initialize-VenDebugLog -General $General
try {
$session = Get-AviSession $General.HostAddress $General.UserName $General.UserPass
$headers = @{
"content-type"="application/json";
"referer"="https://$($General.HostAddress)";
"X-Avi-Tenant"=$General.VarText2;
"x-csrftoken"=$($session.Cookies.GetCookies("https://$($General.HostAddress)")["csrftoken"].Value)
}
$url = "https://$($General.HostAddress)/api/sslkeyandcertificate?name=$($General.AssetName)&export_key=false"
$resp_obj = Invoke-AviRestMethod -Uri $url -Method Get -Headers $headers -ContentType "application/json" -WebSession $session
$response = $resp_obj.response
if ( $response.Count -ne 0 ) # certificate exists
{
$pem = $response.results.certificate.certificate
$idx = $response.results.certificate.fingerprint.IndexOf('=')
$thumbprint = $response.results.certificate.fingerprint.Remove(0,$idx+1).Replace(":","").ToLower().Trim()
$serial = "{0:x}" -f [bigint]$($response.results.certificate.serial_number)
Write-VenDebugLog "Certificate Asset Name: $($General.AssetName)"
Write-VenDebugLog "Certificate Serial Number: $($serial)"
Write-VenDebugLog "Certificate Thumbprint: $($thumbprint)"
# Write-VenDebugLog "Certificate Valid Until: $()"
Write-VenDebugLog "Certificate extracted successfully - Returning control to Venafi"
return @{Result="Success"; CertPem="$pem"; Serial="$serial"; Thumprint="$thumbprint"}
}
else {
Write-VenDebugLog "NOT FOUND: Certificate with asset name $($General.AssetName) - Returning control to Venafi"
throw "No certificate named '" + $General.AssetName + "' was found."
}
}
catch {
Write-VenDebugLog "API error: $($_.Exception)"
Write-VenDebugLog "FAILED to extract certificate - Returning control to Venafi"
throw Select-ErrorMessage($_.Exception)
}
}
<##################################################################################################
.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
{
# This line tells VS Code to not flag this function's name as a "problem"
[Diagnostics.CodeAnalysis.SuppressMessage('PSUseApprovedVerbs', '', Justification='Forced by Venafi', Scope='function')]
Param(
[Parameter(Mandatory=$true,HelpMessage="General Parameters")]
[System.Collections.Hashtable]$General,
[Parameter(Mandatory=$true,HelpMessage="Function Specific Parameters")]
[System.Collections.Hashtable]$Specific
)
Initialize-VenDebugLog -General $General
try {
$session = Get-AviSession $General.HostAddress $General.UserName $General.UserPass
$tenant_name = $General.VarText2
$headers = @{
"content-type"="application/json";
"referer"="https://$($General.HostAddress)";
"X-Avi-Tenant"=$tenant_name;
"x-csrftoken"=$($session.Cookies.GetCookies("https://$($General.HostAddress)")["csrftoken"].Value)
}
$url = "https://$($General.HostAddress)/api/sslkeyandcertificate?name=$($General.AssetName)&export_key=true"
$resp_obj = Invoke-AviRestMethod -Uri $url -Method Get -Headers $headers -ContentType "application/json" -WebSession $session
$response = $resp_obj.response
if ( $response.Count -ne 0 ) # certificate and private key exist
{
Write-VenDebugLog "Private key found for certificate '$($General.AssetName)' - Returning control to Venafi"
$pem = $response.results.key
return @{ Result="Success"; PrivKeyPem="$pem" }
}
else {
Write-VenDebugLog "Export failed (Certificate '$($General.AssetName)' not found) - Returning control to Venafi"
throw "No certificate named '" + $General.AssetName + "' was found to export private key."
}
}
catch {
throw Select-ErrorMessage($_.Exception)
}
}
<##################################################################################################
.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
)
Initialize-VenDebugLog -General $General
$cert_avi_name = $Specific.AssetNameOld
$session = Get-AviSession $General.HostAddress $General.UserName $General.UserPass
$tenant_name = $General.VarText2
$headers = @{
"content-type"="application/json";
"referer"="https://$($General.HostAddress)";
"X-Avi-Tenant"=$tenant_name;
"x-csrftoken"=$($session.Cookies.GetCookies("https://$($General.HostAddress)")["csrftoken"].Value)
}
$url = "https://$($General.HostAddress)/api/sslkeyandcertificate?name=$($Specific.AssetNameOld)&export_key=false"
Write-VenDebugLog "Checking for old certificate '$($cert_avi_name)'"
$resp_obj = Invoke-AviRestMethod -Uri $url -Method Get -Headers $headers -ContentType "application/json" -WebSession $session
$response = $resp_obj.response
if ( $response.Count -ne 0 ) # certificate exists so try to delete it
{
$url = $response.results.url
Write-VenDebugLog "Found old certificate '$($cert_avi_name)' - Attempting to delete it"
Invoke-AviRestMethod -Uri $url -Method Delete -Headers $headers -WebSession $session
Write-VenDebugLog "Old certificate '$($cert_avi_name)' deleted - Returning control to Venafi"
}
else {
Write-VenDebugLog "Old certificate '$($cert_avi_name)' not found"
}
return @{ Result="Success"; }
}
<##################################################################################################
.NAME
Discover-Certificates
.DESCRIPTION
Used for Onboard Discovery. Returns a list of applications with certificates which are added under
corresponding device.
.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
Applications : An array of hashtables with discovered application data
##################################################################################################>
function Discover-Certificates
{
# This line tells VS Code to not flag this function's name as a "problem"
[Diagnostics.CodeAnalysis.SuppressMessage('PSUseApprovedVerbs', '', Justification='Forced by Venafi', Scope='function')]
Param(
[Parameter(Mandatory=$true,HelpMessage="General Parameters")]
[System.Collections.Hashtable]$General
)
Initialize-VenDebugLog -General $General
$login_session = Get-AviSession $General.HostAddress $General.UserName $General.UserPass
$all_vs = Get-AllVS $General $login_session
$ref_content_map = @{}
$results = @()
$sitesTotal=$sitesSSL=$sitesClear=0
foreach ($vs in $all_vs) {
$sitesTotal++
if ($vs.ssl_key_and_certificate_refs) {
$sitesSSL++
$temp = @{}
# modified - lookup the actual certificate filename so that it can be stored - otherwise validation won't work!
$cert_filename = Get-Cert-Name-By-Ref $General $vs.ssl_key_and_certificate_refs[0] $ref_content_map $login_session
# modified - replace bad naming convention to prevent collisions
$tenant_name = Get-Tenant-Name-By-Ref $General $vs.tenant_ref $ref_content_map $login_session
$vsIP = Get-IPv4-VIP $General $vs $login_session
$vsPort = Get-SSL-Service-Port $vs
$temp["Name"] = "{0} ({1})" -f $vs.name, $tenant_name
$temp["ApplicationClass"] = "Adaptable App"
$temp["PEM"] = Get-Certificate-By-Ref $General $vs.ssl_key_and_certificate_refs[0] $ref_content_map $login_session
$temp["ValidationAddress"] = $vsIP
$temp["ValidationPort"] = $vsPort
$temp["Attributes"] = @{
"Text Field 1"= $vs.name;
"Text Field 2"= $tenant_name;
# modified - insert required certificate filename so that validation actually works
"Certificate Name"= $cert_filename
}
$results += $temp
Write-VenDebugLog "Discovered: [$($vs.name)] on tenant [$($tenant_name)] at $($vsIP):$($vsPort)"
}
else{
$sitesClear++
Write-VenDebugLog "Ignored: [$($vs.name)] is unencrypted"
}
}
Write-VenDebugLog "$($sitesTotal) virtual servers ($($sitesSSL) discovered, $($sitesClear) ignored) - Returning control to Venafi"
return @{
Result="Success";
Applications = $results
}
}
<########## THE FUNCTIONS AND CODE BELOW THIS LINE ARE NOT CALLED DIRECTLY BY VENAFI ##########>
# replace the original Write-AviLog function with an improved function set that will
# not mix up logs (especially for discoveries) and provide more useful file names
# Take a message, prepend a timestamp, output it to a debug log ... if DEBUG_FILE is set
# Otherwise do nothing and return nothing
function Write-VenDebugLog
{
Param(
[Parameter(Position=0, Mandatory)][string]$LogMessage,
[switch]$NoFunctionTag
)
filter Add-TS {"$(Get-Date -Format o): $_"}
# if the logfile isn't initialized then do nothing and return immediately
if ($null -eq $Script:venDebugFile) { return }
if ($NoFunctionTag.IsPresent) {
$taggedLog = $LogMessage
}
else {
$taggedLog = "[$((Get-PSCallStack)[1].Command)] $($LogMessage)"
}
# write the message to the debug file
Write-Output "$($taggedLog)" | Add-TS | Add-Content -Path $Script:venDebugFile
}
# Adding support for policy-level debug flag instead of forcing every app to be flagged
function Initialize-VenDebugLog
{
Param(
[Parameter(Position=0, Mandatory)][System.Collections.Hashtable]$General
)
if ($null -ne $Script:venDebugFile) {
Write-VenDebugLog "Called by $((Get-PSCallStack)[1].Command)"
Write-VenDebugLog 'WARNING: Initialize-VenDebugLog() called more than once!'
return
}
if ($null -eq $DEBUG_FILE) {
# do nothing and return immediately if debug isn't on
if ($General.VarBool1 -eq $false) { return }
# pull Venafi base directory from registry for global debug flag
$logPath = "$((Get-ItemProperty HKLM:\Software\Venafi\Platform).'Base Path')Logs"
}
else {
# use the path but discard the filename from the DEBUG_FILE variable
$logPath = "$(Split-Path -Path $DEBUG_FILE)"
}
$Script:venDebugFile = "$($logPath)\$($Script:AdaptableAppDrv.Replace(' ',''))"
if ($General.HostAddress -ne '') {
$Script:venDebugFile += "-$($General.HostAddress)"
}
$Script:venDebugFile += ".log"
Write-Output '' | Add-Content -Path $Script:venDebugFile
Write-VenDebugLog -NoFunctionTag -LogMessage "$($Script:AdaptableAppDrv) v$($Script:AdaptableAppVer): Venafi called $((Get-PSCallStack)[1].Command)"
Write-VenDebugLog -NoFunctionTag -LogMessage "PowerShell Environment: $($PSVersionTable.PSEdition) Edition, Version $($PSVersionTable.PSVersion.Major)"
Write-VenDebugLog "Called by $((Get-PSCallStack)[1].Command)"
# Write-VenDebugLog 'GENERAL VARIABLES'
# $General.keys | %{ Add-Content -Path $Script:venDebugFile "$_ : $($General.$_)" }
}
function Invoke-AviRestMethod
{
Param(
[Uri] $Uri,
[Microsoft.PowerShell.Commands.WebRequestMethod] $Method,
[System.Object] $Body,
[string] $ContentType,
[string] $SessionVariable,
[int] $TimeoutSec,
[System.Collections.IDictionary] $Headers,
[Microsoft.PowerShell.Commands.WebRequestSession] $WebSession
)
Write-VenDebugLog "$((Get-PSCallStack)[1].Command)/$($Method): $($Uri)"
try {
if($WebSession){
$response = Invoke-RestMethod -Uri $Uri -Method $Method -Headers $Headers -Body $Body -ContentType $ContentType -WebSession $WebSession -TimeoutSec $TimeoutSec
return @{
response=$response;
session=$WebSession;
}
}
if($SessionVariable){
$response = Invoke-RestMethod -Uri $Uri -Method $Method -Headers $Headers -Body $Body -ContentType $ContentType -SessionVariable new_session -TimeoutSec $TimeoutSec
return @{
response=$response;
session=$new_session
}
}
}
catch [System.Net.WebException] {
Write-VenDebugLog "REST call failed to $($Uri.AbsoluteUri)"
if ($_.Exception.Response) {
[string] $debug_msg = "Status Code of response : {0}" -f $_.Exception.Response.StatusCode.value__.ToString()
Write-VenDebugLog $debug_msg
$debug_msg = Select-ErrorMessage $_.Exception
Write-VenDebugLog $debug_msg
}
else {
$debug_msg = Select-ErrorMessage $_.Exception
Write-VenDebugLog $debug_msg
}
throw $_
}
}
function Get-AviSession( [string] $addr, [string] $user, [string] $pass )
{
Write-VenDebugLog "Called by $((Get-PSCallStack)[1].Command)"
[string] $url = "https://{0}/login" -f $addr
$body = @{
"username"=$user;
"password"=$pass
} | ConvertTo-Json
Write-VenDebugLog "Login to [$($addr)] as [$($user)]"
$resp_obj = Invoke-AviRestMethod -Uri $url -Method Post -Body $body -ContentType "application/json" -SessionVariable session -TimeoutSec 300
$response = $resp_obj.response
$session = $resp_obj.session
$x_avi_version = $response.version.Version
Write-VenDebugLog "Logged into AVI controller [$($addr)] (Version $($x_avi_version)) as [$($user)]"
$session.Headers.Add([string] "X-Avi-Version", $x_avi_version)
$session.Headers.Add([string] "content-type", [string] "application/json")
$session.Headers.Add([string] "referer", [string] "https://$addr")
$session.Headers.Add([string] "x-csrftoken", $($session.Cookies.GetCookies("https://$addr")["csrftoken"].Value))
return $session
}
function Get-AllVs([System.Collections.Hashtable] $General, [Microsoft.PowerShell.Commands.WebRequestSession] $session)
{
Write-VenDebugLog "Called by $((Get-PSCallStack)[1].Command)"
[string] $all_vs_request_url = "https://{0}/api/virtualservice" -f $General.HostAddress
$headers = @{
"X-Avi-Tenant"="*";
}
$resp_obj = Invoke-AviRestMethod -Uri $all_vs_request_url -Method Get -Headers $headers -ContentType "application/json" -WebSession $session -TimeoutSec 300
# modified - everything below this line changed in some way...
$vsCount = $resp_obj.response.count
$vsList = $resp_obj.response.results
$apiCall = 1
Write-VenDebugLog "API returned $(@($resp_obj.response.results).Count) virtual servers"
while ($null -ne $resp_obj.response.next) {
$apiCall++
$resp_obj = Invoke-AviRestMethod -Uri ($resp_obj.response.next) -Method Get -Headers $headers -ContentType "application/json" -WebSession $session -TimeoutSec 300
$vsList += $resp_obj.response.results
Write-VenDebugLog "API returned $(@($resp_obj.response.results).Count) more virtual servers"
}
Write-VenDebugLog "Retrieved [$($vsCount)] virtual servers via [$($apiCall)] API calls to [$($General.HostAddress)]"
return $vsList
}
function Get-Tenant-Name-By-Ref([System.Collections.Hashtable] $General, [string] $tref, [System.Collections.Hashtable] $ref_content_map, [Microsoft.PowerShell.Commands.WebRequestSession] $session)
{
Write-VenDebugLog "Called by $((Get-PSCallStack)[1].Command)"
if(-Not $ref_content_map.$tref){
$headers = @{
"content-type"="application/json";
"referer"="https://$($General.HostAddress)";
"x-csrftoken"=$($session.Cookies.GetCookies("https://$($General.HostAddress)")["csrftoken"].Value)
}
$resp_obj = Invoke-AviRestMethod -Uri $tref -Method Get -Headers $headers -ContentType "application/json" -WebSession $session -TimeoutSec 300
$response = $resp_obj.response
$ref_content_map.$tref = $response.name
Write-VenDebugLog "Added tenant name to cache: [$($ref_content_map.$tref)]"
}
else {
Write-VenDebugLog "Tenant name retrieved from cache: [$($ref_content_map.$tref)]"