@@ -10,6 +10,7 @@ import (
10
10
"fmt"
11
11
"net"
12
12
"net/url"
13
+ "os"
13
14
"reflect"
14
15
"strings"
15
16
"testing"
@@ -275,6 +276,112 @@ type parseCertificateTestCase struct {
275
276
wantErr bool
276
277
}
277
278
279
+ // TestDisableVerifyCertificateEnvVar verifies that env var VAULT_DISABLE_PKI_CONSTRAINTS_VERIFICATION
280
+ // can be used to disable cert verification.
281
+ func TestDisableVerifyCertificateEnvVar (t * testing.T ) {
282
+ caData := map [string ]any {
283
+ // Copied from the "full CA" test case of TestParseCertificate,
284
+ // with tweaked permitted_dns_domains and ttl
285
+ "common_name" : "the common name" ,
286
+ "alt_names" : "user@example.com,admin@example.com,example.com,www.example.com" ,
287
+ "ip_sans" : "1.2.3.4,1.2.3.5" ,
288
+ "uri_sans" : "https://example.com,https://www.example.com" ,
289
+ "other_sans" : "1.3.6.1.4.1.311.20.2.3;utf8:caadmin@example.com" ,
290
+ "ttl" : "3h" ,
291
+ "max_path_length" : 2 ,
292
+ "permitted_dns_domains" : ".example.com,.www.example.com" ,
293
+ "ou" : "unit1, unit2" ,
294
+ "organization" : "org1, org2" ,
295
+ "country" : "US, CA" ,
296
+ "locality" : "locality1, locality2" ,
297
+ "province" : "province1, province2" ,
298
+ "street_address" : "street_address1, street_address2" ,
299
+ "postal_code" : "postal_code1, postal_code2" ,
300
+ "not_before_duration" : "45s" ,
301
+ "key_type" : "rsa" ,
302
+ "use_pss" : true ,
303
+ "key_bits" : 2048 ,
304
+ "signature_bits" : 384 ,
305
+ }
306
+
307
+ roleData := map [string ]any {
308
+ "allow_any_name" : true ,
309
+ "cn_validations" : "disabled" ,
310
+ "allow_ip_sans" : true ,
311
+ "allowed_other_sans" : "1.3.6.1.4.1.311.20.2.3;utf8:*@example.com" ,
312
+ "allowed_uri_sans" : "https://example.com,https://www.example.com" ,
313
+ "allowed_user_ids" : "*" ,
314
+ "not_before_duration" : "45s" ,
315
+ "signature_bits" : 384 ,
316
+ "key_usage" : "KeyAgreement" ,
317
+ "ext_key_usage" : "ServerAuth" ,
318
+ "ext_key_usage_oids" : "1.3.6.1.5.5.7.3.67,1.3.6.1.5.5.7.3.68" ,
319
+ "client_flag" : false ,
320
+ "server_flag" : false ,
321
+ "policy_identifiers" : "1.2.3.4.5.6.7.8.9.0" ,
322
+ }
323
+
324
+ certData := map [string ]any {
325
+ // using the same order as in https://developer.hashicorp.com/vault/api-docs/secret/pki#generate-certificate-and-key
326
+ "common_name" : "the common name non ca" ,
327
+ "alt_names" : "user@example.com,admin@example.com,example.com,www.example.com" ,
328
+ "ip_sans" : "1.2.3.4,1.2.3.5" ,
329
+ "uri_sans" : "https://example.com,https://www.example.com" ,
330
+ "other_sans" : "1.3.6.1.4.1.311.20.2.3;utf8:caadmin@example.com" ,
331
+ "ttl" : "2h" ,
332
+ // format
333
+ // private_key_format
334
+ "exclude_cn_from_sans" : true ,
335
+ // not_after
336
+ // remove_roots_from_chain
337
+ "user_ids" : "humanoid,robot" ,
338
+ }
339
+
340
+ defer func () {
341
+ os .Unsetenv ("VAULT_DISABLE_PKI_CONSTRAINTS_VERIFICATION" )
342
+ }()
343
+
344
+ b , s := CreateBackendWithStorage (t )
345
+
346
+ // Create the CA
347
+ resp , err := CBWrite (b , s , "root/generate/internal" , caData )
348
+ require .NoError (t , err )
349
+ require .NotNil (t , resp )
350
+
351
+ // Create the role
352
+ resp , err = CBWrite (b , s , "roles/test" , roleData )
353
+ require .NoError (t , err )
354
+ require .NotNil (t , resp )
355
+
356
+ // Try to create the cert -- should fail verification, since example.com is not allowed
357
+ t .Run ("no VAULT_DISABLE_PKI_CONSTRAINTS_VERIFICATION env var" , func (t * testing.T ) {
358
+ resp , err = CBWrite (b , s , "issue/test" , certData )
359
+ require .ErrorContains (t , err , `DNS name "example.com" is not permitted by any constraint` )
360
+ })
361
+
362
+ // Try to create the cert -- should fail verification, since example.com is not allowed
363
+ t .Run ("VAULT_DISABLE_PKI_CONSTRAINTS_VERIFICATION=false" , func (t * testing.T ) {
364
+ os .Setenv ("VAULT_DISABLE_PKI_CONSTRAINTS_VERIFICATION" , "false" )
365
+ resp , err = CBWrite (b , s , "issue/test" , certData )
366
+ require .ErrorContains (t , err , `DNS name "example.com" is not permitted by any constraint` )
367
+ })
368
+
369
+ // Create the cert, should succeed with the disable env var set
370
+ t .Run ("VAULT_DISABLE_PKI_CONSTRAINTS_VERIFICATION=true" , func (t * testing.T ) {
371
+ os .Setenv ("VAULT_DISABLE_PKI_CONSTRAINTS_VERIFICATION" , "true" )
372
+ resp , err = CBWrite (b , s , "issue/test" , certData )
373
+ require .NoError (t , err )
374
+ require .NotNil (t , resp )
375
+ })
376
+
377
+ // Invalid env var
378
+ t .Run ("invalid VAULT_DISABLE_PKI_CONSTRAINTS_VERIFICATION" , func (t * testing.T ) {
379
+ os .Setenv ("VAULT_DISABLE_PKI_CONSTRAINTS_VERIFICATION" , "invalid" )
380
+ resp , err = CBWrite (b , s , "issue/test" , certData )
381
+ require .ErrorContains (t , err , "failed parsing environment variable VAULT_DISABLE_PKI_CONSTRAINTS_VERIFICATION" )
382
+ })
383
+ }
384
+
278
385
func TestParseCertificate (t * testing.T ) {
279
386
t .Parallel ()
280
387
@@ -364,7 +471,7 @@ func TestParseCertificate(t *testing.T) {
364
471
"other_sans" : "1.3.6.1.4.1.311.20.2.3;utf8:caadmin@example.com" ,
365
472
"ttl" : "2h" ,
366
473
"max_path_length" : 2 ,
367
- "permitted_dns_domains" : ".example.com,.www.example.com" ,
474
+ "permitted_dns_domains" : "example.com, .example.com,.www.example.com" ,
368
475
"ou" : "unit1, unit2" ,
369
476
"organization" : "org1, org2" ,
370
477
"country" : "US, CA" ,
@@ -409,7 +516,7 @@ func TestParseCertificate(t *testing.T) {
409
516
UsePSS : true ,
410
517
ForceAppendCaChain : false ,
411
518
UseCSRValues : false ,
412
- PermittedDNSDomains : []string {".example.com" , ".www.example.com" },
519
+ PermittedDNSDomains : []string {"example.com" , " .example.com" , ".www.example.com" },
413
520
URLs : nil ,
414
521
MaxPathLength : 2 ,
415
522
NotBeforeDuration : 45 * time .Second ,
@@ -433,7 +540,7 @@ func TestParseCertificate(t *testing.T) {
433
540
"serial_number" : "" ,
434
541
"ttl" : "2h0m45s" ,
435
542
"max_path_length" : 2 ,
436
- "permitted_dns_domains" : ".example.com,.www.example.com" ,
543
+ "permitted_dns_domains" : "example.com, .example.com,.www.example.com" ,
437
544
"use_pss" : true ,
438
545
"key_type" : "rsa" ,
439
546
"key_bits" : 2048 ,
@@ -532,49 +639,50 @@ func TestParseCertificate(t *testing.T) {
532
639
},
533
640
}
534
641
for _ , tt := range tests {
642
+ t .Run (tt .name , func (t * testing.T ) {
643
+ b , s := CreateBackendWithStorage (t )
535
644
536
- b , s := CreateBackendWithStorage (t )
645
+ var cert * x509.Certificate
646
+ issueTime := time .Now ()
647
+ if tt .wantParams .IsCA {
648
+ resp , err := CBWrite (b , s , "root/generate/internal" , tt .data )
649
+ require .NoError (t , err )
650
+ require .NotNil (t , resp )
537
651
538
- var cert * x509.Certificate
539
- issueTime := time .Now ()
540
- if tt .wantParams .IsCA {
541
- resp , err := CBWrite (b , s , "root/generate/internal" , tt .data )
542
- require .NoError (t , err )
543
- require .NotNil (t , resp )
652
+ certData := resp .Data ["certificate" ].(string )
653
+ cert , err = parsing .ParseCertificateFromString (certData )
654
+ require .NoError (t , err )
655
+ require .NotNil (t , cert )
656
+ } else {
657
+ // use the "simple CA" data to create the internal CA
658
+ caData := tests [1 ].data
659
+ caData ["ttl" ] = "3h"
660
+ resp , err := CBWrite (b , s , "root/generate/internal" , caData )
661
+ require .NoError (t , err )
662
+ require .NotNil (t , resp )
544
663
545
- certData := resp .Data ["certificate" ].(string )
546
- cert , err = parsing .ParseCertificateFromString (certData )
547
- require .NoError (t , err )
548
- require .NotNil (t , cert )
549
- } else {
550
- // use the "simple CA" data to create the internal CA
551
- caData := tests [1 ].data
552
- caData ["ttl" ] = "3h"
553
- resp , err := CBWrite (b , s , "root/generate/internal" , caData )
554
- require .NoError (t , err )
555
- require .NotNil (t , resp )
664
+ // create a role
665
+ resp , err = CBWrite (b , s , "roles/test" , tt .roleData )
666
+ require .NoError (t , err )
667
+ require .NotNil (t , resp )
556
668
557
- // create a role
558
- resp , err = CBWrite (b , s , "roles /test" , tt .roleData )
559
- require .NoError (t , err )
560
- require .NotNil (t , resp )
669
+ // create the cert
670
+ resp , err = CBWrite (b , s , "issue /test" , tt .data )
671
+ require .NoError (t , err )
672
+ require .NotNil (t , resp )
561
673
562
- // create the cert
563
- resp , err = CBWrite (b , s , "issue/test" , tt .data )
564
- require .NoError (t , err )
565
- require .NotNil (t , resp )
566
-
567
- certData := resp .Data ["certificate" ].(string )
568
- cert , err = parsing .ParseCertificateFromString (certData )
569
- require .NoError (t , err )
570
- require .NotNil (t , cert )
571
- }
674
+ certData := resp .Data ["certificate" ].(string )
675
+ cert , err = parsing .ParseCertificateFromString (certData )
676
+ require .NoError (t , err )
677
+ require .NotNil (t , cert )
678
+ }
572
679
573
- t .Run (tt .name + " parameters" , func (t * testing.T ) {
574
- testParseCertificateToCreationParameters (t , issueTime , tt , cert )
575
- })
576
- t .Run (tt .name + " fields" , func (t * testing.T ) {
577
- testParseCertificateToFields (t , issueTime , tt , cert )
680
+ t .Run (tt .name + " parameters" , func (t * testing.T ) {
681
+ testParseCertificateToCreationParameters (t , issueTime , tt , cert )
682
+ })
683
+ t .Run (tt .name + " fields" , func (t * testing.T ) {
684
+ testParseCertificateToFields (t , issueTime , tt , cert )
685
+ })
578
686
})
579
687
}
580
688
}
0 commit comments