Skip to content

Commit

Permalink
Make Prometheus credentials configurable via web
Browse files Browse the repository at this point in the history
  • Loading branch information
jrauh01 committed Nov 20, 2024
1 parent ec925c3 commit 55253ff
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 12 deletions.
73 changes: 63 additions & 10 deletions application/controllers/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class ConfigController extends Controller
{
protected bool $disableDefaultAutoRefresh = true;

public const PROMETHEUS_URL = 'prometheus.url';

public const PROMETHEUS_USERNAME = 'prometheus.username';

public const PROMETHEUS_PASSWORD = 'prometheus.password';

public function init()
{
$this->assertPermission('config/modules');
Expand Down Expand Up @@ -181,34 +187,81 @@ function (NotificationsConfigForm $form) use ($kconfig, $sourceForm) {
public function prometheusAction()
{
$db = Database::connection();
$dbConfig = KConfig::on($db)->filter(Filter::equal('key', 'prometheus.url'))->first();
$dbConfig = KConfig::on($db)->filter(
Filter::any(
Filter::equal('key', self::PROMETHEUS_URL),
Filter::equal('key', self::PROMETHEUS_USERNAME),
Filter::equal('key', self::PROMETHEUS_PASSWORD)
)
);

$data = [];

foreach ($dbConfig as $pair) {
switch ($pair->key) {
case self::PROMETHEUS_URL:
$data['prometheus_url'] = $pair->value;
break;
case self::PROMETHEUS_USERNAME:
$data['prometheus_username'] = $pair->value;
break;
case self::PROMETHEUS_PASSWORD:
$data['prometheus_password'] = $pair->value;
break;
}
}

// $config = Config::module('kubernetes');
$form = (new PrometheusConfigForm())
->populate(['prometheus_url' => $dbConfig->value])
->on(PrometheusConfigForm::ON_SUCCESS, function ($form) use ($db, $dbConfig) {
->populate($data)
->on(PrometheusConfigForm::ON_SUCCESS, function ($form) use ($db, $data) {
if ($form->isLocked()) {
Notification::error($this->translate('Prometheus configuration is locked'));
return;
}

// $config->setSection('prometheus', $form->getValues());
// $config->saveIni();

if ($dbConfig) {
if (isset($data['prometheus_url'])) {
$db->update('config',
['value' => $form->getValue('prometheus_url')],
[$db->quoteIdentifier('key') . ' = ?' => 'prometheus.url']
[$db->quoteIdentifier('key') . ' = ?' => self::PROMETHEUS_URL]
);
} else {
$db->insert('config',
[
$db->quoteIdentifier('key') => 'prometheus.url',
$db->quoteIdentifier('key') => self::PROMETHEUS_URL,
'value' => $form->getValue('prometheus_url')
]
);
}

if (isset($data['prometheus_username'])) {
$db->update('config',
['value' => $form->getValue('prometheus_username')],
[$db->quoteIdentifier('key') . ' = ?' => self::PROMETHEUS_USERNAME]
);
} else {
$db->insert('config',
[
$db->quoteIdentifier('key') => self::PROMETHEUS_USERNAME,
'value' => $form->getValue('prometheus_username')
]
);
}

if (isset($data['prometheus_password'])) {
$db->update('config',
['value' => $form->getValue('prometheus_password')],
[$db->quoteIdentifier('key') . ' = ?' => self::PROMETHEUS_PASSWORD]
);
} else {
$db->insert('config',
[
$db->quoteIdentifier('key') => self::PROMETHEUS_PASSWORD,
'value' => $form->getValue('prometheus_password')
]
);
}

$this->redirectNow('__REFRESH__');

Notification::success($this->translate('New configuration has successfully been stored'));
})->handleRequest($this->getServerRequest());
Expand Down
27 changes: 25 additions & 2 deletions application/forms/PrometheusConfigForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Icinga\Module\Kubernetes\Forms;

use Icinga\Data\ResourceFactory;
use Icinga\Module\Kubernetes\Controllers\ConfigController;
use ipl\Html\Attributes;
use ipl\Html\Html;
use ipl\Html\HtmlElement;
Expand Down Expand Up @@ -44,6 +45,26 @@ protected function assemble(): void
]
);

$this->addElement(
'text',
'prometheus_username',
[
'label' => $this->translate('Username'),
'disabled' => $this->isLocked(),
'value' => ''
]
);

$this->addElement(
'password',
'prometheus_password',
[
'label' => $this->translate('Password'),
'disabled' => $this->isLocked(),
'value' => ''
]
);

$this->addElement(
'submit',
'submit',
Expand All @@ -57,9 +78,11 @@ protected function assemble(): void
public function isLocked(): bool
{
$config = Config::on(Database::connection());
$config->filter(Filter::equal('key', 'prometheus.locked'));
$config->filter(Filter::equal('key', ConfigController::PROMETHEUS_URL));

$temp = $config->first();

if (isset($config->first()->value) && $config->first()->value === 'true') {
if (isset($config->first()->locked) && $config->first()->locked === 'y') {
return true;
}

Expand Down

0 comments on commit 55253ff

Please sign in to comment.