|
| 1 | +<?php |
| 2 | + |
| 3 | +/* Icinga Web 2 X.509 Module | (c) 2023 Icinga GmbH | GPLv2 */ |
| 4 | + |
| 5 | +namespace Icinga\Module\X509\Clicommands; |
| 6 | + |
| 7 | +use DateTime; |
| 8 | +use Exception; |
| 9 | +use Icinga\Application\Logger; |
| 10 | +use Icinga\Module\X509\CertificateUtils; |
| 11 | +use Icinga\Module\X509\Command; |
| 12 | +use InvalidArgumentException; |
| 13 | +use Throwable; |
| 14 | + |
| 15 | +class CleanupCommand extends Command |
| 16 | +{ |
| 17 | + /** |
| 18 | + * Clean up certificates that aren't used by any host and targets whose last scan is older than a certain date/time |
| 19 | + * |
| 20 | + * This command purges by default all targets whose last scan is older than 1 month. The last_scan column is |
| 21 | + * always updated when scanning a target, regardless of whether a successful connection to the host has been |
| 22 | + * established or not. Additionally, it removes all untrusted certificates that aren't part of any chains. |
| 23 | + * |
| 24 | + * USAGE |
| 25 | + * |
| 26 | + * icingacli x509 cleanup [OPTIONS] |
| 27 | + * |
| 28 | + * OPTIONS |
| 29 | + * |
| 30 | + * --since-last-scan=<datetime> |
| 31 | + * Clean up targets whose last scan is older than the specified date/time, |
| 32 | + * which can also be an English textual datetime description like "2 days". |
| 33 | + * Default "1 month". |
| 34 | + * |
| 35 | + * EXAMPLES |
| 36 | + * |
| 37 | + * Remove all targets that haven't been scanned for 2 months and all unused untrusted certificates: |
| 38 | + * |
| 39 | + * icingacli x509 cleanup --since-last-scan="2 months" |
| 40 | + * |
| 41 | + */ |
| 42 | + public function indexAction() |
| 43 | + { |
| 44 | + $sinceLastScan = $this->params->get('since-last-scan', '-1 month'); |
| 45 | + $lastScan = $sinceLastScan; |
| 46 | + if ($lastScan[0] !== '-') { |
| 47 | + // When the user specified "2 days" as a threshold strtotime() will compute the |
| 48 | + // timestamp NOW() + 2 days, but it has to be NOW() + (-2 days) |
| 49 | + $lastScan = "-$lastScan"; |
| 50 | + } |
| 51 | + |
| 52 | + try { |
| 53 | + $sinceLastScan = new DateTime($lastScan); |
| 54 | + } catch (Exception $_) { |
| 55 | + throw new InvalidArgumentException(sprintf( |
| 56 | + 'The specified last scan time is in an unknown format: %s', |
| 57 | + $sinceLastScan |
| 58 | + )); |
| 59 | + } |
| 60 | + |
| 61 | + try { |
| 62 | + $conn = $this->getDb(); |
| 63 | + $query = $conn->delete( |
| 64 | + 'x509_target', |
| 65 | + ['last_scan < ?' => $sinceLastScan->format('Uv')] |
| 66 | + ); |
| 67 | + |
| 68 | + if ($query->rowCount() > 0) { |
| 69 | + Logger::info( |
| 70 | + 'Removed %d targets matching since last scan filter: %s', |
| 71 | + $query->rowCount(), |
| 72 | + $sinceLastScan->format(DateTime::ATOM) |
| 73 | + ); |
| 74 | + |
| 75 | + CertificateUtils::cleanupCertificateChains($conn); |
| 76 | + CertificateUtils::cleanupCertificates($conn); |
| 77 | + } |
| 78 | + } catch (Throwable $err) { |
| 79 | + Logger::error($err); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments