Skip to content

Commit 163061f

Browse files
author
Daniel Zohm
committed
Add possibility to disable token validation
1 parent 8833d6b commit 163061f

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

src/Controller/Index/Index.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,18 @@ public function __construct(
3131

3232
public function execute(): ResultInterface
3333
{
34-
$token = sprintf('Bearer %s', $this->config->getToken());
35-
$authorizationHeader = $this->getRequest()->getHeader('Authorization');
36-
37-
if ($token !== $authorizationHeader) {
38-
/** @var \Magento\Framework\Controller\Result\Raw $result */
39-
$result = $this->resultFactory->create(ResultFactory::TYPE_RAW);
40-
$result->setHttpResponseCode(Http::STATUS_CODE_401);
41-
$result->setContents('You are not allowed to see these metrics.');
42-
43-
return $result;
34+
if ($this->config->getTokenValidationEnabled()) {
35+
$token = sprintf('Bearer %s', $this->config->getToken());
36+
$authorizationHeader = $this->getRequest()->getHeader('Authorization');
37+
38+
if ($token !== $authorizationHeader) {
39+
/** @var \Magento\Framework\Controller\Result\Raw $result */
40+
$result = $this->resultFactory->create(ResultFactory::TYPE_RAW);
41+
$result->setHttpResponseCode(Http::STATUS_CODE_401);
42+
$result->setContents('You are not allowed to see these metrics.');
43+
44+
return $result;
45+
}
4446
}
4547

4648
return $this->prometheusResultFactory->create();

src/Data/Config.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Config
1212
{
1313
private const CONFIG_PATH_METRICS_ENABLED = 'metric_configuration/metric/metric_status';
1414
private const CONFIG_PATH_AUTH_TOKEN = 'metric_configuration/security/token';
15+
private const CONFIG_PATH_TOKEN_VALIDATION_ENABLED = 'metric_configuration/security/enable_token';
1516

1617
private $config;
1718
private $metricsSource;
@@ -38,6 +39,11 @@ public function getDefaultMetrics(): array
3839
return array_column($this->metricsSource->toOptionArray(), 'value');
3940
}
4041

42+
public function getTokenValidationEnabled(?string $scopeCode = null): bool
43+
{
44+
return $this->config->isSetFlag(self::CONFIG_PATH_TOKEN_VALIDATION_ENABLED, ScopeInterface::SCOPE_STORE, $scopeCode);
45+
}
46+
4147
public function getToken(?string $scopeCode = null): string
4248
{
4349
return $this->config->getValue(self::CONFIG_PATH_AUTH_TOKEN, ScopeInterface::SCOPE_STORE, $scopeCode) ?? '';

src/etc/adminhtml/system.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,25 @@
2525
<label>Security Settings for the Prometheus Scrape Config</label>
2626
<comment>This section contains security related configurations. We recommend using the Bearer Token in your Prometheus Scrape Config.</comment>
2727

28+
<field id="enable_token" showInWebsite="1" showInStore="1" showInDefault="1" type="select">
29+
<label>Enable token authorization</label>
30+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
31+
</field>
2832
<field id="token" showInWebsite="1" showInStore="1" showInDefault="1" type="text">
2933
<label>Token</label>
3034
<frontend_model>RunAsRoot\PrometheusExporter\Block\Adminhtml\System\Config\DisabledText</frontend_model>
35+
<depends>
36+
<field id="metric_configuration/security/enable_token">1</field>
37+
</depends>
3138
</field>
3239

3340
<field id="generate_auth_token" translate="button_label" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
3441
<button_label>Generate</button_label>
3542
<comment>Click 'Generate' to generate a random auth token, that you can use for your scrape config.</comment>
3643
<frontend_model>RunAsRoot\PrometheusExporter\Block\Adminhtml\System\Config\TokenGenerator</frontend_model>
44+
<depends>
45+
<field id="metric_configuration/security/enable_token">1</field>
46+
</depends>
3747
</field>
3848
</group>
3949
</section>

0 commit comments

Comments
 (0)