Skip to content

Commit 440bb12

Browse files
authoredDec 10, 2024
Merge pull request #1498 from adrum/feature/renew-ca
Add the ability to renew the Certificate Authority certificate
2 parents 1a0077a + 3c0015c commit 440bb12

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed
 

‎cli/Valet/Site.php

+22-4
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,9 @@ public function secured(): array
437437
/**
438438
* Get all of the URLs with expiration dates that are currently secured.
439439
*/
440-
public function securedWithDates(): array
440+
public function securedWithDates($ca = false): array
441441
{
442-
return collect($this->secured())->map(function ($site) {
442+
$sites = collect($this->secured())->map(function ($site) {
443443
$filePath = $this->certificatesPath().'/'.$site.'.crt';
444444

445445
$expiration = $this->cli->run("openssl x509 -enddate -noout -in $filePath");
@@ -450,7 +450,22 @@ public function securedWithDates(): array
450450
'site' => $site,
451451
'exp' => new DateTime($expiration),
452452
];
453-
})->unique()->values()->all();
453+
})->unique()->values();
454+
455+
if ($ca) {
456+
$filePath = $this->caPath('LaravelValetCASelfSigned.pem');
457+
458+
$expiration = $this->cli->run("openssl x509 -enddate -noout -in $filePath");
459+
460+
$expiration = str_replace('notAfter=', '', $expiration);
461+
462+
$sites->prepend([
463+
'site' => 'Certificate Authority',
464+
'exp' => new DateTime($expiration),
465+
]);
466+
}
467+
468+
return $sites->all();
454469
}
455470

456471
public function isSecured(string $site): bool
@@ -502,8 +517,11 @@ public function secure(string $url, ?string $siteConf = null, int $certificateEx
502517
/**
503518
* Renews all domains with a trusted TLS certificate.
504519
*/
505-
public function renew($expireIn = 368): void
520+
public function renew($expireIn = 368, $ca = false): void
506521
{
522+
if ($ca) {
523+
$this->removeCa();
524+
}
507525
collect($this->securedWithDates())->each(function ($row) use ($expireIn) {
508526
$url = $this->domain($row['site']);
509527

‎cli/app.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,9 @@ function (ConsoleCommandEvent $event) {
285285
/**
286286
* Display all of the currently secured sites.
287287
*/
288-
$app->command('secured [--expiring] [--days=]', function (OutputInterface $output, $expiring = null, $days = 60) {
288+
$app->command('secured [--expiring] [--days=] [--ca]', function (OutputInterface $output, $expiring = null, $days = 60, $ca = null) {
289289
$now = (new Datetime)->add(new DateInterval('P'.$days.'D'));
290-
$sites = collect(Site::securedWithDates())
290+
$sites = collect(Site::securedWithDates($ca))
291291
->when($expiring, fn ($collection) => $collection->filter(fn ($row) => $row['exp'] < $now))
292292
->map(function ($row) {
293293
return [
@@ -301,16 +301,18 @@ function (ConsoleCommandEvent $event) {
301301
})->descriptions('Display all of the currently secured sites', [
302302
'--expiring' => 'Limits the results to only sites expiring within the next 60 days.',
303303
'--days' => 'To be used with --expiring. Limits the results to only sites expiring within the next X days. Default is set to 60.',
304+
'--ca' => 'Include the Certificate Authority certificate in the list of site certificates.',
304305
]);
305306

306307
/**
307308
* Renews all domains with a trusted TLS certificate.
308309
*/
309-
$app->command('renew [--expireIn=]', function (OutputInterface $output, $expireIn = 368) {
310-
Site::renew($expireIn);
310+
$app->command('renew [--expireIn=] [--ca]', function (OutputInterface $output, $expireIn = 368, $ca = null) {
311+
Site::renew($expireIn, $ca);
311312
Nginx::restart();
312313
})->descriptions('Renews all domains with a trusted TLS certificate.', [
313314
'--expireIn' => 'The amount of days the self signed certificate is valid for. Default is set to "368"',
315+
'--ca' => 'Renew the Certificate Authority certificate before renewing the site certificates.',
314316
]);
315317

316318
/**

0 commit comments

Comments
 (0)