Added Basic authentication capability to Alertmanager Transport (#13867)

* Adding Basic authentication capability to Alertmanager's Transport

* Fix PHPStan linting error

* Fix StyleCI error

* Fix StyleCI error part 2
This commit is contained in:
geg347
2022-03-31 22:45:44 +02:00
committed by GitHub
parent ac11177beb
commit ef12e5f980
2 changed files with 30 additions and 4 deletions

View File

@@ -36,10 +36,13 @@ class Alertmanager extends Transport
$alertmanager_opts = $this->parseUserOptions($this->config['alertmanager-options']);
$alertmanager_opts['url'] = $this->config['alertmanager-url'];
return $this->contactAlertmanager($obj, $alertmanager_opts);
$alertmanager_username = $this->config['alertmanager-username'];
$alertmanager_password = $this->config['alertmanager-password'];
return $this->contactAlertmanager($obj, $alertmanager_opts, $alertmanager_username, $alertmanager_password);
}
public function contactAlertmanager($obj, $api)
public function contactAlertmanager($obj, $api, string $username, string $password)
{
if ($obj['state'] == AlertState::RECOVERED) {
$alertmanager_status = 'endsAt';
@@ -74,10 +77,10 @@ class Alertmanager extends Transport
}
}
return $this->postAlerts($url, $data);
return $this->postAlerts($url, $data, $username, $password);
}
public static function postAlerts($url, $data)
public static function postAlerts($url, $data, string $username, string $password)
{
$curl = curl_init();
Proxy::applyToCurl($curl);
@@ -89,6 +92,12 @@ class Alertmanager extends Transport
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT_MS, 5000);
curl_setopt($curl, CURLOPT_POST, true);
if ($username != '' && $password != '') {
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERNAME, $username);
curl_setopt($curl, CURLOPT_PASSWORD, $password);
}
$alert_message = json_encode($data);
curl_setopt($curl, CURLOPT_POSTFIELDS, $alert_message);
@@ -133,6 +142,18 @@ class Alertmanager extends Transport
'descr' => 'Alertmanager Webhook URL(s). Can contain comma-separated URLs',
'type' => 'text',
],
[
'title' => 'Alertmanager Username',
'name' => 'alertmanager-username',
'descr' => 'Alertmanager Basic Username to authenticate to Alertmanager',
'type' => 'text',
],
[
'title' => 'Alertmanager Password',
'name' => 'alertmanager-password',
'descr' => 'Alertmanager Basic Password to authenticate to Alertmanager',
'type' => 'password',
],
[
'title' => 'Alertmanager Options',
'name' => 'alertmanager-options',

View File

@@ -75,6 +75,9 @@ label's value will be the string you provided just as it was a fixed string.
Multiple Alertmanager URLs (comma separated) are supported. Each
URL will be tried and the search will stop at the first success.
Basic HTTP authentication with a username and a password is supported.
If you let those value blank, no authentication will be used.
[Alertmanager Docs](https://prometheus.io/docs/alerting/alertmanager/)
**Example:**
@@ -82,6 +85,8 @@ URL will be tried and the search will stop at the first success.
| Config | Example |
| ------ | ------- |
| Alertmanager URL(s) | http://alertmanager1.example.com,http://alertmanager2.example.com |
| Alertmanager Username | myUsername |
| Alertmanager Password | myPassword |
| Alertmanager Options: | source=librenms <br/> customlabel=value <br/> extra_dynamic_value=variable_name |
## API