44
55namespace Icinga \Module \X509 ;
66
7+ use DateTime ;
78use Icinga \Application \Config ;
89use Icinga \Application \Logger ;
910use Icinga \Data \ConfigObject ;
@@ -41,6 +42,7 @@ class Job
4142 private $ snimap ;
4243 private $ parallel ;
4344 private $ name ;
45+ private $ currentDateTime ;
4446
4547 public function __construct ($ name , ConfigObject $ jobDescription , array $ snimap , $ parallel )
4648 {
@@ -50,6 +52,7 @@ public function __construct($name, ConfigObject $jobDescription, array $snimap,
5052 $ this ->snimap = $ snimap ;
5153 $ this ->parallel = $ parallel ;
5254 $ this ->name = $ name ;
55+ $ this ->currentDateTime = (new DateTime ())->getTimestamp ();
5356 }
5457
5558 private function getConnector ($ peerName )
@@ -85,7 +88,7 @@ private static function numberToAddr($num, $ipv6 = true)
8588 }
8689 }
8790
88- private function generateTargets (ConfigObject $ jobDescription , array $ hostnamesConfig )
91+ private static function generateTargets (ConfigObject $ jobDescription , array $ hostnamesConfig )
8992 {
9093 foreach (StringHelper::trimSplit ($ jobDescription ->get ('cidrs ' )) as $ cidr ) {
9194 $ pieces = explode ('/ ' , $ cidr );
@@ -124,17 +127,6 @@ private function generateTargets(ConfigObject $jobDescription, array $hostnamesC
124127 foreach (range ($ start_port , $ end_port ) as $ port ) {
125128 $ hostnames = isset ($ hostnamesConfig [$ ip ]) ? $ hostnamesConfig [$ ip ] : [];
126129
127- if (! empty ($ hostnames )) {
128- $ this ->db ->delete (
129- 'x509_target ' ,
130- [
131- 'ip = ? ' => $ this ->dbTool ->marshalBinary (static ::binary ($ ip )),
132- 'port = ? ' => $ port ,
133- 'hostname NOT IN (?) ' => array_values ($ hostnames )
134- ]
135- );
136- }
137-
138130 if (empty ($ hostnames )) {
139131 $ hostnames [] = null ;
140132 }
@@ -144,8 +136,6 @@ private function generateTargets(ConfigObject $jobDescription, array $hostnamesC
144136 $ target ->ip = $ ip ;
145137 $ target ->port = $ port ;
146138 $ target ->hostname = $ hostname ;
147-
148- $ this ->updateLastScan ($ target );
149139 yield $ target ;
150140 }
151141 }
@@ -216,6 +206,16 @@ function (ConnectionInterface $conn) use ($target, $streamCapture) {
216206 // Close connection in order to capture stream context options
217207 $ conn ->close ();
218208
209+ $ this ->db ->update (
210+ 'x509_target ' ,
211+ ['last_seen ' => $ this ->currentDateTime ],
212+ [
213+ 'ip = ? ' => $ this ->dbTool ->marshalBinary (static ::binary ($ target ->ip )),
214+ 'port = ? ' => $ target ->port ,
215+ 'hostname = ? ' => $ target ->hostname
216+ ]
217+ );
218+
219219 $ capturedStreamOptions = $ streamCapture ->getCapturedStreamOptions ();
220220
221221 $ this ->processChain ($ target , $ capturedStreamOptions ['ssl ' ]['peer_certificate_chain ' ]);
@@ -253,25 +253,19 @@ function (\Exception $exception) use ($target, $streamCapture) {
253253 )->otherwise (function (\Exception $ e ) {
254254 echo $ e ->getMessage () . PHP_EOL ;
255255 echo $ e ->getTraceAsString () . PHP_EOL ;
256+ })->always (function () use ($ target ) {
257+ $ this ->db ->update (
258+ 'x509_target ' ,
259+ ['last_scan ' => $ this ->currentDateTime ],
260+ [
261+ 'ip = ? ' => $ this ->dbTool ->marshalBinary (static ::binary ($ target ->ip )),
262+ 'port = ? ' => $ target ->port ,
263+ 'hostname = ? ' => $ target ->hostname
264+ ]
265+ );
256266 });
257267 }
258268
259- protected function updateLastScan ($ target )
260- {
261- if (isset ($ target ->id )) {
262- $ filter = ['id = ? ' => $ target ->id ];
263- } else {
264- $ filter = [
265- 'ip = ? ' => $ this ->dbTool ->marshalBinary (static ::binary ($ target ->ip )),
266- 'port = ? ' => $ target ->port ,
267- 'hostname = ? ' => $ target ->hostname
268- ];
269- }
270-
271- $ now = new Expression ('NOW() ' );
272- $ this ->db ->update ('x509_target ' , ['last_scan ' => $ now ], $ filter );
273- }
274-
275269 public function getJobId ()
276270 {
277271 return $ this ->jobId ;
@@ -281,13 +275,13 @@ public function run()
281275 {
282276 $ this ->loop = Factory::create ();
283277
284- $ this ->totalTargets = iterator_count ($ this -> generateTargets ($ this ->jobDescription , $ this ->snimap ));
278+ $ this ->totalTargets = iterator_count (static :: generateTargets ($ this ->jobDescription , $ this ->snimap ));
285279
286280 if ($ this ->totalTargets == 0 ) {
287281 return null ;
288282 }
289283
290- $ this ->targets = $ this -> generateTargets ($ this ->jobDescription , $ this ->snimap );
284+ $ this ->targets = static :: generateTargets ($ this ->jobDescription , $ this ->snimap );
291285
292286 $ this ->db ->insert (
293287 'x509_job_run ' ,
@@ -339,7 +333,8 @@ protected function processChain($target, $chain)
339333 'ip ' => $ this ->dbTool ->marshalBinary (static ::binary ($ target ->ip )),
340334 'port ' => $ target ->port ,
341335 'hostname ' => $ target ->hostname ,
342- 'last_scan ' => new Expression ('NOW() ' )
336+ 'last_scan ' => $ this ->currentDateTime ,
337+ 'last_seen ' => $ this ->currentDateTime
343338 ]
344339 );
345340 $ targetId = $ this ->db ->lastInsertId ();
@@ -401,7 +396,7 @@ protected function processChain($target, $chain)
401396
402397 $ this ->db ->update (
403398 'x509_certificate ' ,
404- ['last_seen ' => new Expression ( ' NOW() ' ) ],
399+ ['last_seen ' => $ this -> currentDateTime ],
405400 ['id = ? ' => $ certId ]
406401 );
407402
@@ -419,9 +414,7 @@ protected function processChain($target, $chain)
419414 $ this ->db ->update (
420415 'x509_target ' ,
421416 [
422- 'latest_certificate_chain_id ' => $ chainId ,
423- 'last_seen ' => new Expression ('NOW() ' )
424- ],
417+ 'latest_certificate_chain_id ' => $ chainId ],
425418 ['id = ? ' => $ targetId ]
426419 );
427420 });
0 commit comments