Skip to content

Commit 5d4821b

Browse files
authored
Merge pull request #1488 from adrum/fix/trust-ca
Require Trusting CA when securing sites
2 parents 35b32b6 + e7c0dba commit 5d4821b

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

cli/Valet/Site.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -475,17 +475,18 @@ public function secure(string $url, ?string $siteConf = null, int $certificateEx
475475
// Extract in order to later preserve custom PHP version config when securing
476476
$phpVersion = $this->customPhpVersion($url);
477477

478-
$this->unsecure($url);
479-
478+
// Create the CA if it doesn't exist.
479+
// If the user cancels the trust operation, the old certificate will not be removed.
480480
$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);
481485

482486
$this->files->ensureDirExists($this->certificatesPath(), user());
483487

484488
$this->files->ensureDirExists($this->nginxPath(), user());
485489

486-
$caExpireInDate = (new \DateTime())->diff(new \DateTime("+{$caExpireInYears} years"));
487-
488-
$this->createCa($caExpireInDate->format('%a'));
489490
$this->createCertificate($url, $certificateExpireInDays);
490491

491492
$siteConf = $this->buildSecureNginxServer($url, $siteConf);
@@ -640,9 +641,14 @@ public function createSigningRequest(string $url, string $keyPath, string $csrPa
640641
*/
641642
public function trustCa(string $caPemPath): void
642643
{
643-
$this->cli->run(sprintf(
644-
'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
645648
));
649+
if ($result) {
650+
throw new DomainException('The Certificate Authority must be trusted. Please run the command again.');
651+
}
646652
}
647653

648654
/**

0 commit comments

Comments
 (0)