File tree Expand file tree Collapse file tree 3 files changed +28
-10
lines changed Expand file tree Collapse file tree 3 files changed +28
-10
lines changed Original file line number Diff line number Diff line change @@ -31,16 +31,18 @@ public function __construct(
31
31
32
32
public function execute (): ResultInterface
33
33
{
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
+ }
44
46
}
45
47
46
48
return $ this ->prometheusResultFactory ->create ();
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ class Config
12
12
{
13
13
private const CONFIG_PATH_METRICS_ENABLED = 'metric_configuration/metric/metric_status ' ;
14
14
private const CONFIG_PATH_AUTH_TOKEN = 'metric_configuration/security/token ' ;
15
+ private const CONFIG_PATH_TOKEN_VALIDATION_ENABLED = 'metric_configuration/security/enable_token ' ;
15
16
16
17
private $ config ;
17
18
private $ metricsSource ;
@@ -38,6 +39,11 @@ public function getDefaultMetrics(): array
38
39
return array_column ($ this ->metricsSource ->toOptionArray (), 'value ' );
39
40
}
40
41
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
+
41
47
public function getToken (?string $ scopeCode = null ): string
42
48
{
43
49
return $ this ->config ->getValue (self ::CONFIG_PATH_AUTH_TOKEN , ScopeInterface::SCOPE_STORE , $ scopeCode ) ?? '' ;
Original file line number Diff line number Diff line change 25
25
<label >Security Settings for the Prometheus Scrape Config</label >
26
26
<comment >This section contains security related configurations. We recommend using the Bearer Token in your Prometheus Scrape Config.</comment >
27
27
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 >
28
32
<field id =" token" showInWebsite =" 1" showInStore =" 1" showInDefault =" 1" type =" text" >
29
33
<label >Token</label >
30
34
<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 >
31
38
</field >
32
39
33
40
<field id =" generate_auth_token" translate =" button_label" sortOrder =" 20" showInDefault =" 1" showInWebsite =" 1" showInStore =" 1" >
34
41
<button_label >Generate</button_label >
35
42
<comment >Click 'Generate' to generate a random auth token, that you can use for your scrape config.</comment >
36
43
<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 >
37
47
</field >
38
48
</group >
39
49
</section >
You can’t perform that action at this time.
0 commit comments