Skip to content

Commit 77453a0

Browse files
authored
PagerDuty notifier is configurable by check within an accreditation (#59)
Signed-off-by: xinivanchen <xin.ivan.chen@ibm.com>
1 parent bf2ed5a commit 77453a0

File tree

6 files changed

+65
-6
lines changed

6 files changed

+65
-6
lines changed

CHANGES.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# UNRELEASED
2-
1+
# 1.4.0
2+
3+
- [IMPROVED] PagerDuty notifier can send alerts for a subset of the accreditation checks based on the config.
34
- [NEW] Added warning for possible sensitive information contained within notifications.
45

56
# 1.3.0

compliance/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
# limitations under the License.
1515
"""Compliance automation package."""
1616

17-
__version__ = '1.3.0'
17+
__version__ = '1.4.0'

compliance/notify.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,16 @@ def notify(self):
892892
for accreditation, desc in messages:
893893
if accreditation not in self._config or not desc:
894894
continue
895+
# determine if we should page for all failing checks in this
896+
# accreditation, or only a subset defined in the config.
897+
# The config can either be a string of the PD service ID,
898+
# or a dictionary containing additional config details, including
899+
# the optional list of checks.
900+
all_checks, pd_checks = True, []
901+
if (isinstance(self._config[accreditation], dict)
902+
and 'checks' in self._config[accreditation]):
903+
all_checks = False
904+
pd_checks = self._config[accreditation]['checks'] or []
895905

896906
# get all current PD alerts
897907
alerts = self._get_alerts(accreditation)
@@ -900,6 +910,10 @@ def notify(self):
900910
passed, failed, warned, errored = self._split_by_status(desc)
901911

902912
for test_id, test_desc, msg in (failed + errored):
913+
# is the check in scope?
914+
if not all_checks and test_id not in pd_checks:
915+
continue
916+
903917
summary, body = self._get_summary_and_body(
904918
test_desc,
905919
msg,
@@ -935,7 +949,8 @@ def notify(self):
935949
self._resolve_alert(test_id, test_desc, msg, accreditation)
936950

937951
def _get_alerts(self, accreditation):
938-
pd_service = self._config[accreditation]
952+
conf = self._config[accreditation]
953+
pd_service = conf['service_id'] if isinstance(conf, dict) else conf
939954

940955
# Get all triggered alerts
941956
# NOTE: statuses[]=triggered is supposed to work, but doesn't

compliance/utils/services/pagerduty.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"""Compliance Pagerduty service helper."""
1616

1717
import json
18-
from urllib.parse import urlparse
18+
from urllib.parse import urljoin
1919

2020
from compliance.utils.credentials import Config
2121

@@ -36,7 +36,7 @@ def _init_request(path, params, headers, creds):
3636
if headers:
3737
hdrs.update(headers)
3838
params = params or {}
39-
url = urlparse.urljoin(PAGERDUTY_API_URL, path)
39+
url = urljoin(PAGERDUTY_API_URL, path)
4040
return url, params, hdrs
4141

4242

doc-source/credentials-example.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ token=XXX
88
[slack]
99
webhook=XXX
1010
# token=XXX # can be used instead of webhook
11+
12+
# Token for PagerDuty notifications
13+
[pagerduty]
14+
api_key=XXX
15+
events_integration_key=XXX

doc-source/notifiers.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,44 @@ Channel ID ``11223344`` can be obtained quickly from the URL to a
151151
message of the target private channel. Of course, the Slack App needs
152152
to be part of the private channel.
153153

154+
PagerDuty
155+
---------
156+
157+
This configurable notifier will send alerts to PagerDuty.
158+
The following is an example configuration for this notifier
159+
to be added to a configuration file and used with the ``pagerduty``
160+
option when executing your compliance checks.
161+
162+
Note that you have two options to configure the PagerDuty notifier:
163+
164+
* Provide a list of checks by class path within an accreditation. This allows you
165+
to define which checks within the accreditation will trigger PageDuty notifications::
166+
167+
{
168+
"pagerduty": {
169+
"my.accred1": {
170+
"service_id": "SERVICE_ID",
171+
"checks": [
172+
"package.category.checks.test_module_one.CheckClassOne",
173+
"package.category.checks.test_module_two.CheckClassTwo"
174+
]
175+
}
176+
}
177+
}
178+
179+
* Provide accreditations only and the notifier will send alerts for all checks with those
180+
accreditations::
181+
182+
{
183+
"pagerduty": {
184+
"my.accred1": "SERVICE_ID"
185+
}
186+
}
187+
188+
Note that the ``service_id`` field is the service id from PagerDuty, e.g. ``PABC123``.
189+
The PagerDuty notifier loads the active incidents to determine if
190+
it needs to create a new incident or update an existing one by using the ``service_id``.
191+
154192
GitHub Issue
155193
------------
156194

0 commit comments

Comments
 (0)