Skip to content

Commit

Permalink
Certificate expiring status added
Browse files Browse the repository at this point in the history
  • Loading branch information
Uksusov Kirill committed Sep 8, 2023
1 parent 92218ec commit cc75c8f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
12 changes: 12 additions & 0 deletions api/app/Models/Enums/CertificateStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,28 @@

enum CertificateStatus: string
{
case EXPIRING = 'expiring';
case INVALID = 'invalid';
case NOT_YET_CHECKED = 'not yet checked';
case VALID = 'valid';

public function emoji(): string
{
return match($this) {
self::EXPIRING => '⚠️',
self::INVALID => '🚨',
self::NOT_YET_CHECKED => '⚠️',
self::VALID => '',
};
}

public function priority(): string
{
return match($this) {
self::EXPIRING => 'warning',
self::INVALID => 'danger',
self::NOT_YET_CHECKED => 'warning',
self::VALID => 'success',
};
}
}
9 changes: 9 additions & 0 deletions api/app/Models/Enums/UptimeStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@ public function emoji(): string
self::UP => '',
};
}

public function priority(): string
{
return match($this) {
self::DOWN => 'danger',
self::NOT_YET_CHECKED => 'warning',
self::UP => 'success',
};
}
}
18 changes: 15 additions & 3 deletions api/app/Models/Traits/SupportsCertificateCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Events\CertificateStatusSucceeded;
use App\Models\Enums\CertificateStatus;
use App\Models\MonitorCertificateStatus;
use Carbon\Carbon;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasOne;
Expand Down Expand Up @@ -49,9 +50,7 @@ public function setCertificate(SslCertificate $certificate): void
{
$isStatusChanged = $this->certificate->certificate_status !== CertificateStatus::VALID;

$newStatus = $certificate->isValid($this->url)
? CertificateStatus::VALID
: CertificateStatus::INVALID;
$newStatus = $this->getCertificateStatus($certificate);

$status = new MonitorCertificateStatus();

Expand Down Expand Up @@ -85,4 +84,17 @@ public function setCertificateException(Exception $exception): void
event(new CertificateStatusFailed($this, $status));
}
}

protected function getCertificateStatus(SslCertificate $certificate): CertificateStatus
{
if (!$certificate->isValid()) {
return CertificateStatus::INVALID;
}

if ($certificate->expirationDate()->isBefore(Carbon::now()->addMonth())) {
return CertificateStatus::EXPIRING;
}

return CertificateStatus::VALID;
}
}
5 changes: 5 additions & 0 deletions api/app/Orchid/Models/Monitor/MonitorListLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ public function columns(): array
->render(function(Monitor $monitor) {
return view('admin.td.status', [
'status' => $monitor->uptime->uptime_status->value,
'priority' => $monitor->uptime->uptime_status->priority(),
]);
}),
TD::make('status_date', __('Last check'))->render(fn(Monitor $monitor) => $monitor->uptime->created_at->diffForHumans()),
TD::make('cert', __('Certificate'))
->render(function(Monitor $monitor) {
return view('admin.td.status', [
'status' => $monitor->certificate->certificate_status->value,
'priority' => $monitor->certificate->certificate_status->priority(),
'small' => $monitor->certificate->certificate_expiration_date
? $monitor->certificate->certificate_expiration_date->format('Y-m-d')
: null,
]);
}),
TD::make('cert_date', __('Last check'))->render(fn(Monitor $monitor) => $monitor->certificate->created_at->diffForHumans()),
Expand Down
2 changes: 1 addition & 1 deletion api/resources/views/admin/td/status.blade.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<h3><span class="badge {{ ($status === 'valid' || $status === 'up') ? 'bg-success' : 'bg-danger' }}">{{ $status }}</span></h3>
<h3><span class="badge bg-{{ $priority }}">{{ $status }}<br/><small>{{ $small ?? '' }}</small></span></h3>

0 comments on commit cc75c8f

Please sign in to comment.