22
33namespace App \Jobs \SSL ;
44
5- use App \Actions \SSL \CertificateParser ;
5+ use App \Actions \SSL \CheckSslExpiry ;
66use App \Enums \SslStatus ;
77use App \Enums \SslType ;
8- use App \Facades \Notifier ;
98use App \Models \Server ;
109use App \Models \Ssl ;
11- use App \Notifications \SslCertificateExpiring ;
1210use Illuminate \Contracts \Queue \ShouldQueue ;
1311use Illuminate \Foundation \Queue \Queueable ;
1412use Illuminate \Support \Facades \Log ;
@@ -18,8 +16,6 @@ class CheckSslExpiryJob implements ShouldQueue
1816{
1917 use Queueable;
2018
21- private const EXPIRY_WARNING_DAYS = 14 ;
22-
2319 public function __construct (protected Server $ server ) {}
2420
2521 public function handle (): void
@@ -38,79 +34,17 @@ public function handle(): void
3834 }
3935
4036 $ ssh = $ this ->server ->ssh ();
37+ $ action = app (CheckSslExpiry::class);
4138
4239 foreach ($ ssls as $ ssl ) {
43- $ this ->checkCertificate ($ ssh , $ ssl );
44- }
45- }
46-
47- private function checkCertificate (mixed $ ssh , Ssl $ ssl ): void
48- {
49- try {
50- $ certificate = trim ($ ssh ->exec ("sudo cat {$ ssl ->certificate_path }" ));
51-
52- if (empty ($ certificate ) || ! str_contains ($ certificate , 'BEGIN CERTIFICATE ' )) {
53- return ;
40+ try {
41+ $ action ->check ($ ssl , notify: true , ssh: $ ssh );
42+ } catch (Throwable $ e ) {
43+ Log::warning ('[SSL expiry check] Failed to check certificate ' , [
44+ 'ssl_id ' => $ ssl ->id ,
45+ 'error ' => $ e ->getMessage (),
46+ ]);
5447 }
55-
56- $ parsed = CertificateParser::parse ($ certificate );
57- } catch (Throwable $ e ) {
58- Log::warning ('[SSL expiry check] Failed to read certificate ' , [
59- 'ssl_id ' => $ ssl ->id ,
60- 'error ' => $ e ->getMessage (),
61- ]);
62-
63- return ;
64- }
65-
66- $ dirty = false ;
67-
68- if (! $ ssl ->expires_at ?->equalTo($ parsed ['expires_at ' ])) {
69- $ ssl ->expires_at = $ parsed ['expires_at ' ];
70- $ dirty = true ;
71- }
72-
73- if ($ ssl ->domains !== $ parsed ['domains ' ]) {
74- $ ssl ->domains = $ parsed ['domains ' ];
75- $ dirty = true ;
76- }
77-
78- $ dirty = $ this ->handleExpiryNotification ($ ssl ) || $ dirty ;
79-
80- if ($ dirty ) {
81- $ ssl ->save ();
8248 }
8349 }
84-
85- private function handleExpiryNotification (Ssl $ ssl ): bool
86- {
87- if ($ ssl ->expires_at === null ) {
88- return false ;
89- }
90-
91- if ($ ssl ->expires_at ->isAfter (now ()->addDays (self ::EXPIRY_WARNING_DAYS ))) {
92- if ($ ssl ->expiry_notified_at !== null ) {
93- $ ssl ->expiry_notified_at = null ;
94-
95- return true ;
96- }
97-
98- return false ;
99- }
100-
101- if ($ ssl ->expiry_notified_at !== null ) {
102- return false ;
103- }
104-
105- $ server = $ ssl ->site ?->server;
106-
107- if ($ server === null ) {
108- return false ;
109- }
110-
111- Notifier::send ($ server , new SslCertificateExpiring ($ ssl ));
112- $ ssl ->expiry_notified_at = now ();
113-
114- return true ;
115- }
11650}
0 commit comments