Implement /tlsrootcacollection/*

refs #3016
This commit is contained in:
Alexander A. Klimov 2017-11-17 13:51:35 +01:00
parent 2aed948896
commit 1b30682adc
5 changed files with 250 additions and 2 deletions

View File

@ -207,7 +207,7 @@ class ConfigController extends Controller
$rootCaCollections = array(); $rootCaCollections = array();
foreach (LocalFileStorage::common('tls/rootcacollections') as $rootCaCollection) { foreach (LocalFileStorage::common('tls/rootcacollections') as $rootCaCollection) {
$matches = array(); $matches = array();
if (preg_match('~\A([0-9a-f]{2}+)\.pem\z~i', $rootCaCollection, $matches)) { if (preg_match('~\A((?:[0-9a-f]{2})+)\.pem\z~', $rootCaCollection, $matches)) {
$rootCaCollections[hex2bin($matches[1])] = null; $rootCaCollections[hex2bin($matches[1])] = null;
} }
} }
@ -218,7 +218,7 @@ class ConfigController extends Controller
$clientIdentities = array(); $clientIdentities = array();
foreach (LocalFileStorage::common('tls/clientidentities') as $clientIdentity) { foreach (LocalFileStorage::common('tls/clientidentities') as $clientIdentity) {
$matches = array(); $matches = array();
if (preg_match('~\A([0-9a-f]{2}+)\.pem\z~i', $clientIdentity, $matches)) { if (preg_match('~\A((?:[0-9a-f]{2})+)\.pem\z~', $clientIdentity, $matches)) {
$clientIdentities[hex2bin($matches[1])] = null; $clientIdentities[hex2bin($matches[1])] = null;
} }
} }

View File

@ -0,0 +1,110 @@
<?php
/* Icinga Web 2 | (c) 2017 Icinga Development Team | GPLv2+ */
namespace Icinga\Controllers;
use Exception;
use Icinga\Application\Icinga;
use Icinga\File\Storage\LocalFileStorage;
use Icinga\Forms\Config\Tls\RootCaCollection\CreateForm;
use Icinga\Forms\Config\Tls\RootCaCollection\EditForm;
use Icinga\Forms\ConfirmRemovalForm;
use Icinga\Web\Controller;
use Icinga\Web\Notification;
/**
* Manage TLS root CA certificate collections
*/
class TlsrootcacollectionController extends Controller
{
public function init()
{
$this->assertPermission('config/application/tlscert');
parent::init();
}
public function createAction()
{
$this->view->form = $form = new CreateForm();
$form->setRedirectUrl('tlsrootcacollection/edit')
->handleRequest();
$this->addTitleTab(
$this->translate('New Certificate Collection'),
$this->translate('Create A New TLS Root CA Certificate Collection')
);
$this->render('form');
}
public function editAction()
{
$this->view->form = $form = new EditForm();
$name = $this->params->getRequired('name');
$form->setOldName($name)
->setRedirectUrl('tlsrootcacollection/edit')
->handleRequest();
$this->addTitleTab(
$this->translate('Edit Certificate Collection'),
sprintf($this->translate('Edit TLS Root CA Certificate Collection "%s"'), $name)
);
$this->render('form');
}
public function removeAction()
{
$rootCaCollections = LocalFileStorage::common('tls/rootcacollections');
$name = $this->params->getRequired('name');
$fileName = bin2hex($name) . '.pem';
$rootCaCollections->resolvePath($fileName, true);
$this->view->form = $form = new ConfirmRemovalForm();
$form->setOnSuccess(function (ConfirmRemovalForm $form) use ($name, $fileName, $rootCaCollections) {
try {
$rootCaCollections->delete($fileName);
} catch (Exception $e) {
$form->error($e->getMessage());
return false;
}
Notification::success(
sprintf(t('TLS root CA certificate collection "%s" successfully removed'), $name)
);
return true;
})
->setRedirectUrl('config/tls')
->handleRequest();
$this->addTitleTab(
$this->translate('Remove Certificate Collection'),
sprintf($this->translate('Remove TLS Root CA Certificate Collection "%s"'), $name)
);
$this->render('form');
}
/**
* Add primary tab with the given label and title
*
* @param string $label
* @param string $title
*/
protected function addTitleTab($label, $title)
{
$url = clone $this->getRequest()->getUrl();
$this->getTabs()->add(
preg_replace('~\A.*/~', '', $url->getPath()),
array(
'active' => true,
'label' => $label,
'title' => $title,
'url' => $url
)
);
}
}

View File

@ -0,0 +1,47 @@
<?php
/* Icinga Web 2 | (c) 2017 Icinga Development Team | GPLv2+ */
namespace Icinga\Forms\Config\Tls\RootCaCollection;
use Exception;
use Icinga\File\Storage\LocalFileStorage;
use Icinga\Web\Form;
/**
* Configuration form for creating TLS root CA certificate collections
*/
class CreateForm extends Form
{
public function init()
{
$this->setName('form_config_tlsrootcacollection');
$this->setSubmitLabel($this->translate('Create'));
}
public function createElements(array $formData)
{
$this->addElement(
'text',
'name',
array(
'label' => $this->translate('Name'),
'description' => $this->translate('The new TLS root CA certificate collection\'s name'),
'required' => true
)
);
}
public function onSuccess()
{
$name = $this->getElement('name')->getValue();
try {
LocalFileStorage::common('tls/rootcacollections')->create(bin2hex($name) . '.pem', '');
} catch (Exception $e) {
$this->error($e->getMessage());
return false;
}
$this->getRedirectUrl()->setParam('name', $name);
}
}

View File

@ -0,0 +1,85 @@
<?php
/* Icinga Web 2 | (c) 2017 Icinga Development Team | GPLv2+ */
namespace Icinga\Forms\Config\Tls\RootCaCollection;
use Exception;
use Icinga\File\Storage\LocalFileStorage;
use Icinga\Web\Form;
/**
* Configuration form for editing TLS root CA certificate collections
*/
class EditForm extends Form
{
/**
* The TLS root CA certificate collection's old name
*
* @var string
*/
protected $oldName;
public function init()
{
$this->setName('form_config_tlsrootcacollection');
$this->setSubmitLabel($this->translate('Save Changes'));
}
public function createElements(array $formData)
{
$this->addElement(
'text',
'name',
array(
'label' => $this->translate('Name'),
'description' => $this->translate('The TLS root CA certificate collection\'s name'),
'required' => true,
'value' => $this->oldName
)
);
$this->addElement(
'hidden',
'old_name',
array(
'required' => true,
'disabled' => true,
'value' => $this->oldName
)
);
}
public function onSuccess()
{
$name = $this->getElement('name')->getValue();
if ($name !== $this->oldName) {
try {
$rootCaCollections = LocalFileStorage::common('tls/rootcacollections');
$oldFileName = bin2hex($this->oldName) . '.pem';
$rootCaCollections->create(bin2hex($name) . '.pem', $rootCaCollections->read($oldFileName));
$rootCaCollections->delete($oldFileName);
} catch (Exception $e) {
$this->error($e->getMessage());
return false;
}
}
$this->getRedirectUrl()->setParam('name', $name);
}
/**
* Set the TLS root CA certificate collection's old name
*
* @param string $oldName
*
* @return $this
*/
public function setOldName($oldName)
{
$this->oldName = $oldName;
return $this;
}
}

View File

@ -0,0 +1,6 @@
<div class="controls">
<?= /** @var \Icinga\Web\Widget\Tabs $tabs */ $tabs ?>
</div>
<div class="content">
<?= /** @var Icinga\Web\Form $form */ $form ?>
</div>