Skip to content

Commit

Permalink
Merge pull request #218 from Icinga/fix-since-last-scan
Browse files Browse the repository at this point in the history
Use `since_last_scan` flag properly
  • Loading branch information
yhabteab authored Oct 27, 2023
2 parents 069a76b + 5bb5143 commit 714b795
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
13 changes: 10 additions & 3 deletions library/X509/Common/JobOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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',
Expand All @@ -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);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions library/X509/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 714b795

Please sign in to comment.