Skip to content

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Dec 5, 2022
1 parent 7e6a846 commit dc1903d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 40 deletions.
9 changes: 4 additions & 5 deletions application/clicommands/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Icinga\Application\Logger;
use Icinga\Module\X509\Command;
use Icinga\Module\X509\Model\X509Certificate;
use Icinga\Module\X509\Model\X509CertificateChain;
use Icinga\Module\X509\Model\X509Target;
use ipl\Sql\Expression;
use ipl\Stdlib\Filter;
Expand Down Expand Up @@ -88,13 +89,11 @@ public function hostAction()
]);

// Sub queries for (valid_from, valid_to) columns
$validFrom = X509Certificate::on($conn)
->with(['chain', 'issuer_certificate'])
->columns([new Expression('MAX(GREATEST(%s, %s))', ['valid_from', 'issuer_certificate.valid_from'])]);

$validFrom->getResolver()->setAliasPrefix('sub_');
$validFrom = $targets->createSubQuery(new X509Certificate(), 'chain.certificate');
$validFrom
->columns([new Expression('MAX(GREATEST(%s, %s))', ['valid_from', 'issuer_certificate.valid_from'])])
->getSelectBase()
->resetWhere()
->where(new Expression('sub_certificate_link.certificate_chain_id = target_chain.id'));

$validTo = clone $validFrom;
Expand Down
20 changes: 7 additions & 13 deletions application/controllers/CertificatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,7 @@ public function indexAction()
$this->addControl($limitControl);
$this->addControl($searchBar);

// List of allowed columns to be exported
$exportable = array_flip([
'id', 'subject', 'issuer', 'version', 'self_signed', 'ca', 'trusted',
'pubkey_algo', 'pubkey_bits', 'signature_algo', 'signature_hash_algo',
'valid_from', 'valid_to'
]);

$this->handleFormatRequest($certificates, function (Query $certificates) use ($exportable) {
$this->handleFormatRequest($certificates, function (Query $certificates) {
/** @var X509Certificate $cert */
foreach ($certificates as $cert) {
$cert['valid_from'] = (new \DateTime())
Expand All @@ -94,7 +87,7 @@ public function indexAction()
->setTimestamp($cert['valid_to'])
->format('l F jS, Y H:i:s e');

yield array_intersect_key(iterator_to_array($cert), $exportable);
yield array_intersect_key(iterator_to_array($cert), array_flip($cert->getExportableColumns()));
}
});

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

public function completeAction()
{
$suggestions = new ObjectSuggestions();
$suggestions->setModel(X509Certificate::class);
$suggestions->forRequest($this->getServerRequest());
$this->getDocument()->add($suggestions);
$this->getDocument()->add(
(new ObjectSuggestions())
->setModel(X509Certificate::class)
->forRequest($this->getServerRequest())
);
}

public function searchEditorAction()
Expand Down
23 changes: 11 additions & 12 deletions application/controllers/UsageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function indexAction()
} else {
$this->addControl($searchBar);
$this->sendMultipartUpdate();

return;
}
} else {
Expand All @@ -93,13 +94,7 @@ public function indexAction()
$this->addControl($limitControl);
$this->addControl($searchBar);

$exportable = array_flip([
'valid', 'hostname', 'ip', 'port', 'subject', 'issuer', 'version',
'self_signed', 'ca', 'trusted', 'pubkey_algo', 'pubkey_bits',
'signature_algo', 'signature_hash_algo', 'valid_from', 'valid_to'
]);

$this->handleFormatRequest($targets, function (Query $targets) use ($conn, $exportable) {
$this->handleFormatRequest($targets, function (Query $targets) {
foreach ($targets as $usage) {
$usage['valid_from'] = (new \DateTime())
->setTimestamp($usage['valid_from'])
Expand All @@ -113,7 +108,10 @@ public function indexAction()
$usage->port = $usage->chain->target->port;
$usage->valid = $usage->chain->valid;

yield array_intersect_key(iterator_to_array($usage), $exportable);
yield array_intersect_key(
iterator_to_array($usage),
array_flip(array_merge(['valid', 'hostname', 'ip', 'port'], $usage->getExportableColumns()))
);
}
});

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

public function completeAction()
{
$suggestions = new ObjectSuggestions();
$suggestions->setModel(X509Certificate::class);
$suggestions->forRequest($this->getServerRequest());
$this->getDocument()->add($suggestions);
$this->getDocument()->add(
(new ObjectSuggestions())
->setModel(X509Certificate::class)
->forRequest($this->getServerRequest())
);
}

public function searchEditorAction()
Expand Down
2 changes: 1 addition & 1 deletion library/X509/DataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected function renderRow($row)
$cells = [];

foreach ($this->columns as $key => $column) {
if (! is_int($key) && isset($row->$key)) {
if (! is_int($key) && property_exists($row, $key)) {
$data = $row[$key];
} else {
$data = null;
Expand Down
19 changes: 10 additions & 9 deletions library/X509/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,10 @@ protected function processChain($target, $chain)
$this->db->transaction(function () use ($target, $chain) {
$row = X509Target::on($this->db)->columns(['id']);

$filter = Filter::all();
$filter->add(Filter::equal('ip', $target->ip));
$filter->add(Filter::equal('port', $target->port));
$filter->add(Filter::equal('hostname', $target->hostname));
$filter = Filter::all()
->add(Filter::equal('ip', $target->ip))
->add(Filter::equal('port', $target->port))
->add(Filter::equal('hostname', $target->hostname));

$row->filter($filter);

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

$chainUptodate = false;

$lastChain = X509CertificateChain::on($this->db)->columns(['id']);
$lastChain
$lastChain = X509CertificateChain::on($this->db)
->columns(['id'])
->filter(Filter::equal('target_id', $targetId))
->orderBy('id', SORT_DESC)
->limit(1);
->limit(1)
->first();

if (($lastChain = $lastChain->first())) {
if ($lastChain) {
$lastFingerprints = X509Certificate::on($this->db)->utilize('chain');
$lastFingerprints
->columns(['fingerprint'])
Expand All @@ -495,7 +496,7 @@ protected function processChain($target, $chain)
}

if ($chainUptodate) {
$chainId = (int) $lastChain->id;
$chainId = $lastChain->id;
} else {
// TODO: https://github.com/Icinga/ipl-orm/pull/78
$this->db->insert(
Expand Down
24 changes: 24 additions & 0 deletions library/X509/Model/X509Certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,30 @@ public function getSearchColumns()
return ['subject', 'issuer'];
}

/**
* Get list of allowed columns to be exported
*
* @return string[]
*/
public function getExportableColumns(): array
{
return [
'id',
'subject',
'issuer',
'version',
'self_signed',
'ca',
'trusted',
'pubkey_algo',
'pubkey_bits',
'signature_algo',
'signature_hash_algo',
'valid_from',
'valid_to'
];
}

public function createBehaviors(Behaviors $behaviors)
{
$behaviors->add(new Binary([
Expand Down

0 comments on commit dc1903d

Please sign in to comment.