Skip to content

Commit 922f298

Browse files
committed
Optimize imports & cleanup some codes
1 parent d2b5622 commit 922f298

File tree

10 files changed

+76
-69
lines changed

10 files changed

+76
-69
lines changed

application/clicommands/CheckCommand.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
use Icinga\Application\Logger;
88
use Icinga\Module\X509\Command;
9-
use Icinga\Module\X509\Job;
109
use Icinga\Module\X509\Model\X509Certificate;
10+
use Icinga\Module\X509\Model\X509CertificateChain;
1111
use Icinga\Module\X509\Model\X509Target;
1212
use ipl\Sql\Expression;
1313
use ipl\Stdlib\Filter;
@@ -89,36 +89,28 @@ public function hostAction()
8989
]);
9090

9191
// Sub queries for (valid_from, valid_to) columns
92-
$validFrom = X509Certificate::on($conn)
93-
->with(['chain', 'issuer_certificate'])
94-
->columns([
95-
new Expression('MAX(GREATEST(%s, %s))', ['valid_from', 'issuer_certificate.valid_from'])
96-
]);
97-
98-
$validFrom->getResolver()->setAliasPrefix('sub_');
92+
$validFrom = $targets->createSubQuery(new X509Certificate(), 'chain.certificate');
9993
$validFrom
94+
->columns([new Expression('MAX(GREATEST(%s, %s))', ['valid_from', 'issuer_certificate.valid_from'])])
10095
->getSelectBase()
101-
->where(new Expression(
102-
'sub_certificate_link.certificate_chain_id = target_chain.id'
103-
));
96+
->resetWhere()
97+
->where(new Expression('sub_certificate_link.certificate_chain_id = target_chain.id'));
10498

10599
$validTo = clone $validFrom;
106-
$validTo->columns([
107-
new Expression('MIN(LEAST(%s, %s))', ['valid_to', 'issuer_certificate.valid_to'])
108-
]);
100+
$validTo->columns([new Expression('MIN(LEAST(%s, %s))', ['valid_to', 'issuer_certificate.valid_to'])]);
109101

110-
list($validFromSelect, $validFromValues) = $validFrom->dump();
111-
list($validToSelect, $validToValues) = $validTo->dump();
102+
list($validFromSelect, $_) = $validFrom->dump();
103+
list($validToSelect, $_) = $validTo->dump();
112104
$targets
113105
->withColumns([
114-
'valid_from' => new Expression("$validFromSelect", null, ...$validFromValues),
115-
'valid_to' => new Expression("$validToSelect", null, ...$validToValues)
106+
'valid_from' => new Expression($validFromSelect),
107+
'valid_to' => new Expression($validToSelect)
116108
])
117109
->getSelectBase()
118110
->where(new Expression('target_chain_link.order = 0'));
119111

120112
if ($ip !== null) {
121-
$targets->filter(Filter::equal('ip', Job::binary($ip)));
113+
$targets->filter(Filter::equal('ip', $ip));
122114
}
123115
if ($hostname !== null) {
124116
$targets->filter(Filter::equal('hostname', $hostname));

application/controllers/CertificateController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Icinga\Module\X509\CertificateDetails;
99
use Icinga\Module\X509\Controller;
1010
use Icinga\Module\X509\Model\X509Certificate;
11-
use ipl\Sql;
1211
use ipl\Stdlib\Filter;
1312

1413
class CertificateController extends Controller

application/controllers/CertificatesController.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,7 @@ public function indexAction()
7777
$this->addControl($limitControl);
7878
$this->addControl($searchBar);
7979

80-
// List of allowed columns to be exported
81-
$exportable = array_flip([
82-
'id', 'subject', 'issuer', 'version', 'self_signed', 'ca', 'trusted',
83-
'pubkey_algo', 'pubkey_bits', 'signature_algo', 'signature_hash_algo',
84-
'valid_from', 'valid_to'
85-
]);
86-
87-
$this->handleFormatRequest($certificates, function (Query $certificates) use ($exportable) {
80+
$this->handleFormatRequest($certificates, function (Query $certificates) {
8881
/** @var X509Certificate $cert */
8982
foreach ($certificates as $cert) {
9083
$cert['valid_from'] = (new \DateTime())
@@ -94,7 +87,7 @@ public function indexAction()
9487
->setTimestamp($cert['valid_to'])
9588
->format('l F jS, Y H:i:s e');
9689

97-
yield array_intersect_key(iterator_to_array($cert), $exportable);
90+
yield array_intersect_key(iterator_to_array($cert), array_flip($cert->getExportableColumns()));
9891
}
9992
});
10093

@@ -107,10 +100,11 @@ public function indexAction()
107100

108101
public function completeAction()
109102
{
110-
$suggestions = new ObjectSuggestions();
111-
$suggestions->setModel(X509Certificate::class);
112-
$suggestions->forRequest($this->getServerRequest());
113-
$this->getDocument()->add($suggestions);
103+
$this->getDocument()->add(
104+
(new ObjectSuggestions())
105+
->setModel(X509Certificate::class)
106+
->forRequest($this->getServerRequest())
107+
);
114108
}
115109

116110
public function searchEditorAction()

application/controllers/UsageController.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public function indexAction()
7878
} else {
7979
$this->addControl($searchBar);
8080
$this->sendMultipartUpdate();
81+
8182
return;
8283
}
8384
} else {
@@ -93,13 +94,7 @@ public function indexAction()
9394
$this->addControl($limitControl);
9495
$this->addControl($searchBar);
9596

96-
$exportable = array_flip([
97-
'valid', 'hostname', 'ip', 'port', 'subject', 'issuer', 'version',
98-
'self_signed', 'ca', 'trusted', 'pubkey_algo', 'pubkey_bits',
99-
'signature_algo', 'signature_hash_algo', 'valid_from', 'valid_to'
100-
]);
101-
102-
$this->handleFormatRequest($targets, function (Query $targets) use ($conn, $exportable) {
97+
$this->handleFormatRequest($targets, function (Query $targets) {
10398
foreach ($targets as $usage) {
10499
$usage['valid_from'] = (new \DateTime())
105100
->setTimestamp($usage['valid_from'])
@@ -113,7 +108,10 @@ public function indexAction()
113108
$usage->port = $usage->chain->target->port;
114109
$usage->valid = $usage->chain->valid;
115110

116-
yield array_intersect_key(iterator_to_array($usage), $exportable);
111+
yield array_intersect_key(
112+
iterator_to_array($usage),
113+
array_flip(array_merge(['valid', 'hostname', 'ip', 'port'], $usage->getExportableColumns()))
114+
);
117115
}
118116
});
119117

@@ -126,10 +124,11 @@ public function indexAction()
126124

127125
public function completeAction()
128126
{
129-
$suggestions = new ObjectSuggestions();
130-
$suggestions->setModel(X509Certificate::class);
131-
$suggestions->forRequest($this->getServerRequest());
132-
$this->getDocument()->add($suggestions);
127+
$this->getDocument()->add(
128+
(new ObjectSuggestions())
129+
->setModel(X509Certificate::class)
130+
->forRequest($this->getServerRequest())
131+
);
133132
}
134133

135134
public function searchEditorAction()

library/X509/CertificateDetails.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
namespace Icinga\Module\X509;
66

77
use DateTime;
8+
use Icinga\Module\X509\Model\X509Certificate;
89
use ipl\Html\BaseHtmlElement;
910
use ipl\Html\Html;
10-
use ipl\Orm\Model;
1111

1212
/**
1313
* Widget to display X.509 certificate details
@@ -23,7 +23,7 @@ class CertificateDetails extends BaseHtmlElement
2323
*/
2424
protected $cert;
2525

26-
public function setCert(Model $cert)
26+
public function setCert(X509Certificate $cert)
2727
{
2828
$this->cert = $cert;
2929

library/X509/DataTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ protected function renderRow($row)
8484
$cells = [];
8585

8686
foreach ($this->columns as $key => $column) {
87-
if (! is_int($key) && isset($row->$key)) {
87+
if (! is_int($key) && property_exists($row, $key)) {
8888
$data = $row[$key];
8989
} else {
9090
$data = null;

library/X509/Job.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -438,10 +438,10 @@ protected function processChain($target, $chain)
438438
$this->db->transaction(function () use ($target, $chain) {
439439
$row = X509Target::on($this->db)->columns(['id']);
440440

441-
$filter = Filter::all();
442-
$filter->add(Filter::equal('ip', static::binary($target->ip)));
443-
$filter->add(Filter::equal('port', $target->port));
444-
$filter->add(Filter::equal('hostname', $target->hostname));
441+
$filter = Filter::all()
442+
->add(Filter::equal('ip', $target->ip))
443+
->add(Filter::equal('port', $target->port))
444+
->add(Filter::equal('hostname', $target->hostname));
445445

446446
$row->filter($filter);
447447

@@ -463,13 +463,14 @@ protected function processChain($target, $chain)
463463

464464
$chainUptodate = false;
465465

466-
$lastChain = X509CertificateChain::on($this->db)->columns(['id']);
467-
$lastChain
466+
$lastChain = X509CertificateChain::on($this->db)
467+
->columns(['id'])
468468
->filter(Filter::equal('target_id', $targetId))
469469
->orderBy('id', SORT_DESC)
470-
->limit(1);
470+
->limit(1)
471+
->first();
471472

472-
if (($lastChain = $lastChain->first())) {
473+
if ($lastChain) {
473474
$lastFingerprints = X509Certificate::on($this->db)->utilize('chain');
474475
$lastFingerprints
475476
->columns(['fingerprint'])
@@ -495,7 +496,7 @@ protected function processChain($target, $chain)
495496
}
496497

497498
if ($chainUptodate) {
498-
$chainId = (int) $lastChain->id;
499+
$chainId = $lastChain->id;
499500
} else {
500501
// TODO: https://github.com/Icinga/ipl-orm/pull/78
501502
$this->db->insert(

library/X509/Model/X509Certificate.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,30 @@ public function getSearchColumns()
8686
return ['subject', 'issuer'];
8787
}
8888

89+
/**
90+
* Get list of allowed columns to be exported
91+
*
92+
* @return string[]
93+
*/
94+
public function getExportableColumns(): array
95+
{
96+
return [
97+
'id',
98+
'subject',
99+
'issuer',
100+
'version',
101+
'self_signed',
102+
'ca',
103+
'trusted',
104+
'pubkey_algo',
105+
'pubkey_bits',
106+
'signature_algo',
107+
'signature_hash_algo',
108+
'valid_from',
109+
'valid_to'
110+
];
111+
}
112+
89113
public function createBehaviors(Behaviors $behaviors)
90114
{
91115
$behaviors->add(new Binary([

library/X509/ProvidedHook/HostsImportSource.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44

55
namespace Icinga\Module\X509\ProvidedHook;
66

7-
use Icinga\Module\X509\DbTool;
87
use Icinga\Module\X509\Job;
98
use Icinga\Module\X509\Model\X509Target;
109
use ipl\Sql;
11-
use ipl\Stdlib\Filter;
1210

1311
class HostsImportSource extends X509ImportSource
1412
{
@@ -60,12 +58,12 @@ public function fetchData()
6058
$target->host_name_or_ip = $target->host_ip;
6159
}
6260

63-
unset($target->ip); // Isn't needed any more!!
64-
unset($target->chain); // We don't need any relation properties anymore
61+
// Target ip is now obsolete and must not be included in the results.
62+
// The relation is only used to utilize the query and must not be in the result set as well.
63+
unset($target->ip);
64+
unset($target->chain);
6565

66-
$properties = iterator_to_array($target);
67-
68-
$results[$target->host_name_or_ip] = (object) $properties;
66+
$results[$target->host_name_or_ip] = (object) iterator_to_array($target);
6967
}
7068

7169
return $results;

library/X509/ProvidedHook/ServicesImportSource.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ public function fetchData()
101101
$target->host_port
102102
);
103103

104-
unset($target->ip); // Isn't needed any more!!
105-
unset($target->chain); // We don't need any relation properties anymore
104+
// Target ip is now obsolete and must not be included in the results.
105+
// The relation is only used to utilize the query and must not be in the result set as well.
106+
unset($target->ip);
107+
unset($target->chain);
106108

107-
$properties = iterator_to_array($target);
108-
109-
$results[$target->host_name_ip_and_port] = (object) $properties;
109+
$results[$target->host_name_ip_and_port] = (object) iterator_to_array($target);
110110
}
111111

112112
return $results;

0 commit comments

Comments
 (0)