9
9
10
10
class Site
11
11
{
12
- public function __construct (public Brew $ brew , public Configuration $ config , public CommandLine $ cli , public Filesystem $ files )
13
- {
14
- }
12
+ public function __construct (public Brew $ brew , public Configuration $ config , public CommandLine $ cli , public Filesystem $ files ) {}
15
13
16
14
/**
17
15
* Get the name of the site.
@@ -477,17 +475,18 @@ public function secure(string $url, ?string $siteConf = null, int $certificateEx
477
475
// Extract in order to later preserve custom PHP version config when securing
478
476
$ phpVersion = $ this ->customPhpVersion ($ url );
479
477
480
- $ this -> unsecure ( $ url );
481
-
478
+ // Create the CA if it doesn't exist.
479
+ // If the user cancels the trust operation, the old certificate will not be removed.
482
480
$ this ->files ->ensureDirExists ($ this ->caPath (), user ());
481
+ $ caExpireInDate = (new \DateTime ())->diff (new \DateTime ("+ {$ caExpireInYears } years " ));
482
+ $ this ->createCa ($ caExpireInDate ->format ('%a ' ));
483
+
484
+ $ this ->unsecure ($ url );
483
485
484
486
$ this ->files ->ensureDirExists ($ this ->certificatesPath (), user ());
485
487
486
488
$ this ->files ->ensureDirExists ($ this ->nginxPath (), user ());
487
489
488
- $ caExpireInDate = (new \DateTime ())->diff (new \DateTime ("+ {$ caExpireInYears } years " ));
489
-
490
- $ this ->createCa ($ caExpireInDate ->format ('%a ' ));
491
490
$ this ->createCertificate ($ url , $ certificateExpireInDays );
492
491
493
492
$ siteConf = $ this ->buildSecureNginxServer ($ url , $ siteConf );
@@ -525,6 +524,15 @@ public function createCa(int $caExpireInDays): void
525
524
$ caKeyPath = $ this ->caPath ('LaravelValetCASelfSigned.key ' );
526
525
527
526
if ($ this ->files ->exists ($ caKeyPath ) && $ this ->files ->exists ($ caPemPath )) {
527
+
528
+ $ isTrusted = $ this ->cli ->run (sprintf (
529
+ 'security verify-cert -c "%s" ' , $ caPemPath
530
+ ));
531
+
532
+ if (strpos ($ isTrusted , '...certificate verification successful. ' ) === false ) {
533
+ $ this ->trustCa ($ caPemPath );
534
+ }
535
+
528
536
return ;
529
537
}
530
538
@@ -607,8 +615,6 @@ public function createCertificate(string $url, int $caExpireInDays): void
607
615
$ caExpireInDays , $ caPemPath , $ caKeyPath , $ caSrlParam , $ csrPath , $ crtPath , $ confPath
608
616
));
609
617
}
610
-
611
- $ this ->trustCertificate ($ crtPath );
612
618
}
613
619
614
620
/**
@@ -635,9 +641,14 @@ public function createSigningRequest(string $url, string $keyPath, string $csrPa
635
641
*/
636
642
public function trustCa (string $ caPemPath ): void
637
643
{
638
- $ this ->cli ->run (sprintf (
639
- 'sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain "%s" ' , $ caPemPath
644
+ info ('Trusting Laravel Valet Certificate Authority... ' );
645
+ $ result = $ this ->cli ->run (sprintf (
646
+ 'sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain "%s" ' ,
647
+ $ caPemPath
640
648
));
649
+ if ($ result ) {
650
+ throw new DomainException ('The Certificate Authority must be trusted. Please run the command again. ' );
651
+ }
641
652
}
642
653
643
654
/**
0 commit comments