Skip to content

Commit

Permalink
Merge pull request #1418 from kexin-zhai/add_pagerduty_ca_certs_option
Browse files Browse the repository at this point in the history
Add pagerduty_ca_certs option
  • Loading branch information
jertel authored Apr 4, 2024
2 parents f8397a6 + 62e437e commit d687419
Show file tree
Hide file tree
Showing 16 changed files with 150 additions and 78 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

## Other changes
- [workwechat] add workwechat msgtype - [#1369](https://github.com/jertel/elastalert2/pull/1369) - @bitqiu
- [Pager Duty] Add options: pagerduty_ca_certs, pagerduty_ignore_ssl_errors - [#1418](https://github.com/jertel/elastalert2/pull/1418) - @kexin-zhai

# 2.17.0

Expand Down
4 changes: 4 additions & 0 deletions docs/source/alerts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,10 @@ If there's no open (i.e. unresolved) incident with this key, a new one will be c

``pagerduty_proxy``: By default ElastAlert 2 will not use a network proxy to send notifications to PagerDuty. Set this option using ``hostname:port`` if you need to use a proxy. only supports https.

``pagerduty_ca_certs``: Set this option to ``True`` or a path to a CA cert bundle or directory (eg: ``/etc/ssl/certs/ca-certificates.crt``) to validate the SSL certificate.

``pagerduty_ignore_ssl_errors``: By default ElastAlert 2 will verify SSL certificate. Set this option to ``True`` if you want to ignore SSL errors.

V2 API Options (Optional):

These options are specific to the PagerDuty V2 API
Expand Down
12 changes: 11 additions & 1 deletion elastalert/alerters/pagerduty.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def __init__(self, rule):
self.pagerduty_incident_key_args = self.rule.get('pagerduty_incident_key_args', None)
self.pagerduty_event_type = self.rule.get('pagerduty_event_type', 'trigger')
self.pagerduty_proxy = self.rule.get('pagerduty_proxy', None)
self.pagerduty_ca_certs = self.rule.get('pagerduty_ca_certs')
self.pagerduty_ignore_ssl_errors = self.rule.get('pagerduty_ignore_ssl_errors', False)

self.pagerduty_api_version = self.rule.get('pagerduty_api_version', 'v1')
self.pagerduty_v2_payload_class = self.rule.get('pagerduty_v2_payload_class', '')
Expand Down Expand Up @@ -90,12 +92,20 @@ def alert(self, matches):

# set https proxy, if it was provided
proxies = {'https': self.pagerduty_proxy} if self.pagerduty_proxy else None
if self.pagerduty_ca_certs:
verify = self.pagerduty_ca_certs
else:
verify = not self.pagerduty_ignore_ssl_errors

if self.pagerduty_ignore_ssl_errors:
requests.packages.urllib3.disable_warnings()
try:
response = requests.post(
self.url,
data=json.dumps(payload, cls=DateTimeEncoder, ensure_ascii=False).encode("utf-8"),
headers=headers,
proxies=proxies
proxies=proxies,
verify=verify
)
response.raise_for_status()
except RequestException as e:
Expand Down
2 changes: 2 additions & 0 deletions elastalert/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,8 @@ properties:
pagerduty_incident_key: {type: string}
pagerduty_incident_key_args: {type: array, items: {type: string}}
pagerduty_proxy: {type: string}
pagerduty_ca_certs: {type: [boolean, string]}
pagerduty_ignore_ssl_errors: {type: boolean}
pagerduty_api_version: {type: string, enum: ['v1', 'v2']}
pagerduty_v2_payload_class: {type: string}
pagerduty_v2_payload_class_args: {type: array, items: {type: string}}
Expand Down
6 changes: 3 additions & 3 deletions tests/alerters/alertmanager_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def test_alertmanager_timeout():
assert expected_data == json.loads(mock_post_request.call_args_list[0][1]['data'])


@pytest.mark.parametrize('ca_certs, ignore_ssl_errors, excpet_verify', [
@pytest.mark.parametrize('ca_certs, ignore_ssl_errors, expect_verify', [
('', '', True),
('', True, False),
('', False, True),
Expand All @@ -199,7 +199,7 @@ def test_alertmanager_timeout():
(False, True, False),
(False, False, True)
])
def test_alertmanager_ca_certs(ca_certs, ignore_ssl_errors, excpet_verify):
def test_alertmanager_ca_certs(ca_certs, ignore_ssl_errors, expect_verify):
rule = {
'name': 'Test Alertmanager Rule',
'type': 'any',
Expand Down Expand Up @@ -255,7 +255,7 @@ def test_alertmanager_ca_certs(ca_certs, ignore_ssl_errors, excpet_verify):
data=mock.ANY,
headers={'content-type': 'application/json'},
proxies=None,
verify=excpet_verify,
verify=expect_verify,
timeout=10,
auth=None
)
Expand Down
20 changes: 10 additions & 10 deletions tests/alerters/debug_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ def test_debug_alerter(caplog):
}
alert.alert([match])

excepted1 = 'Alert for Test Debug Event Alerter at None:'
assert ('elastalert', logging.INFO, excepted1) == caplog.record_tuples[0]
expected1 = 'Alert for Test Debug Event Alerter at None:'
assert ('elastalert', logging.INFO, expected1) == caplog.record_tuples[0]

excepted2 = 'Test Debug Event Alerter\n\n@timestamp: 2021-01-01T00:00:00\n'
excepted2 += 'name: debug-test-name\n'
assert ('elastalert', logging.INFO, excepted2) == caplog.record_tuples[1]
expected2 = 'Test Debug Event Alerter\n\n@timestamp: 2021-01-01T00:00:00\n'
expected2 += 'name: debug-test-name\n'
assert ('elastalert', logging.INFO, expected2) == caplog.record_tuples[1]


def test_debug_alerter_querykey(caplog):
Expand All @@ -66,9 +66,9 @@ def test_debug_alerter_querykey(caplog):
}
alert.alert([match])

excepted1 = 'Alert for Test Debug Event Alerter, aProbe at None:'
assert ('elastalert', logging.INFO, excepted1) == caplog.record_tuples[0]
expected1 = 'Alert for Test Debug Event Alerter, aProbe at None:'
assert ('elastalert', logging.INFO, expected1) == caplog.record_tuples[0]

excepted2 = 'Test Debug Event Alerter\n\n@timestamp: 2021-01-01T00:00:00\n'
excepted2 += 'hostname: aProbe\nname: debug-test-name\n'
assert ('elastalert', logging.INFO, excepted2) == caplog.record_tuples[1]
expected2 = 'Test Debug Event Alerter\n\n@timestamp: 2021-01-01T00:00:00\n'
expected2 += 'hostname: aProbe\nname: debug-test-name\n'
assert ('elastalert', logging.INFO, expected2) == caplog.record_tuples[1]
6 changes: 3 additions & 3 deletions tests/alerters/httppost2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1886,7 +1886,7 @@ def test_http_alerter_headers():
assert expected_data == json.loads(mock_post_request.call_args_list[0][1]['data'])


@pytest.mark.parametrize('ca_certs, ignore_ssl_errors, excpet_verify', [
@pytest.mark.parametrize('ca_certs, ignore_ssl_errors, expect_verify', [
('', '', True),
('', True, False),
('', False, True),
Expand All @@ -1897,7 +1897,7 @@ def test_http_alerter_headers():
(False, True, False),
(False, False, True)
])
def test_http_alerter_post_ca_certs(ca_certs, ignore_ssl_errors, excpet_verify):
def test_http_alerter_post_ca_certs(ca_certs, ignore_ssl_errors, expect_verify):
rule = {
'name': 'Test HTTP Post Alerter Without Payload',
'type': 'any',
Expand Down Expand Up @@ -1929,7 +1929,7 @@ def test_http_alerter_post_ca_certs(ca_certs, ignore_ssl_errors, excpet_verify):
headers={'Content-Type': 'application/json', 'Accept': 'application/json;charset=utf-8'},
proxies=None,
timeout=10,
verify=excpet_verify
verify=expect_verify
)
assert expected_data == json.loads(mock_post_request.call_args_list[0][1]['data'])

Expand Down
6 changes: 3 additions & 3 deletions tests/alerters/httppost_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def test_http_alerter_headers():
assert expected_data == json.loads(mock_post_request.call_args_list[0][1]['data'])


@pytest.mark.parametrize('ca_certs, ignore_ssl_errors, excpet_verify', [
@pytest.mark.parametrize('ca_certs, ignore_ssl_errors, expect_verify', [
('', '', True),
('', True, False),
('', False, True),
Expand All @@ -228,7 +228,7 @@ def test_http_alerter_headers():
(False, True, False),
(False, False, True)
])
def test_http_alerter_post_ca_certs(ca_certs, ignore_ssl_errors, excpet_verify):
def test_http_alerter_post_ca_certs(ca_certs, ignore_ssl_errors, expect_verify):
rule = {
'name': 'Test HTTP Post Alerter Without Payload',
'type': 'any',
Expand Down Expand Up @@ -262,7 +262,7 @@ def test_http_alerter_post_ca_certs(ca_certs, ignore_ssl_errors, excpet_verify):
headers={'Content-Type': 'application/json', 'Accept': 'application/json;charset=utf-8'},
proxies=None,
timeout=10,
verify=excpet_verify
verify=expect_verify
)
assert expected_data == json.loads(mock_post_request.call_args_list[0][1]['data'])

Expand Down
16 changes: 8 additions & 8 deletions tests/alerters/opsgenie_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,8 +1036,8 @@ def test_opsgenie_parse_responders(caplog):
match,
rule['opsgenie_default_teams']
)
excepted = ['Test']
assert excepted == actual
expected = ['Test']
assert expected == actual
user, level, message = caplog.record_tuples[0]
assert logging.WARNING == level
assert "Cannot create responder for OpsGenie Alert. Key not foud: 'RECEIPIENT_PREFIX'." in message
Expand Down Expand Up @@ -1081,8 +1081,8 @@ def test_opsgenie_create_custom_title():
]
alert = OpsGenieAlerter(rule)
actual = alert.create_custom_title(match)
excepted = 'abc Testing 2014-10-31T00:00:00'
assert excepted == actual
expected = 'abc Testing 2014-10-31T00:00:00'
assert expected == actual


def test_opsgenie_create_custom_description():
Expand Down Expand Up @@ -1159,8 +1159,8 @@ def test_opsgenie_get_details():
]
alert = OpsGenieAlerter(rule)
actual = alert.get_details(match)
excepted = {'Message': 'Testing', 'abc': 'test'}
assert excepted == actual
expected = {'Message': 'Testing', 'abc': 'test'}
assert expected == actual


def test_opsgenie_get_details2():
Expand All @@ -1185,5 +1185,5 @@ def test_opsgenie_get_details2():
]
alert = OpsGenieAlerter(rule)
actual = alert.get_details(match)
excepted = {}
assert excepted == actual
expected = {}
assert expected == actual
Loading

0 comments on commit d687419

Please sign in to comment.