From 5bb5143bb1d5644091054750d5e440c730b8c735 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Fri, 27 Oct 2023 14:01:23 +0200 Subject: [PATCH] Use `since_last_scan` flag properly This flag is used to scan new and rescan targets that were scanned an `X` time ago, but if it gets just initialized to a `Datetime` object at daemon startup this becomes a fixed time and the daemon will never be able to rescan known targets ever again. --- library/X509/Common/JobOptions.php | 13 ++++++++++--- library/X509/Job.php | 7 ++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/library/X509/Common/JobOptions.php b/library/X509/Common/JobOptions.php index bae3712f..51122725 100644 --- a/library/X509/Common/JobOptions.php +++ b/library/X509/Common/JobOptions.php @@ -20,7 +20,7 @@ trait JobOptions /** @var bool Whether this job should perform a full scan */ protected $fullScan; - /** @var ?DateTime Since last scan threshold used to filter out scan targets */ + /** @var ?string Since last scan threshold used to filter out scan targets */ protected $sinceLastScan; /** @var int Used to control how many targets can be scanned in parallel */ @@ -97,7 +97,10 @@ public function setLastScan(?string $time): self } try { - $this->sinceLastScan = new DateTime($sinceLastScan); + // Ensure it's a valid date time string representation. + new DateTime($sinceLastScan); + + $this->sinceLastScan = $sinceLastScan; } catch (Exception $_) { throw new InvalidArgumentException(sprintf( 'The specified last scan time is in an unknown format: %s', @@ -116,7 +119,11 @@ public function setLastScan(?string $time): self */ public function getSinceLastScan(): ?DateTime { - return $this->sinceLastScan; + if (! $this->sinceLastScan) { + return null; + } + + return new DateTime($this->sinceLastScan); } /** diff --git a/library/X509/Job.php b/library/X509/Job.php index 575e3ae8..1a0f0f4a 100644 --- a/library/X509/Job.php +++ b/library/X509/Job.php @@ -349,10 +349,11 @@ protected function getScanTargets(): Generator yield from $this->generateTargets(); } - if ((! $this->fullScan && $this->sinceLastScan !== null) || $this->isRescan()) { + $sinceLastScan = $this->getSinceLastScan(); + if ((! $this->fullScan && $sinceLastScan !== null) || $this->isRescan()) { $targets = X509Target::on($this->db)->columns(['id', 'ip', 'hostname', 'port']); - if (! $this->fullScan && $this->sinceLastScan) { - $targets->filter(Filter::lessThan('last_scan', $this->sinceLastScan)); + if (! $this->fullScan && $sinceLastScan) { + $targets->filter(Filter::lessThan('last_scan', $sinceLastScan)); } foreach ($targets as $target) {