Skip to content

Commit bef16ec

Browse files
authored
Handle deprecated libs, PD docs, bandit (#119)
- Replace imp with importlib - Replace ibm_security_advisor_findings_api_sdk with ibm_cloud_security_advisor - Add PagerDuty notifier clarifying doc content - Add bandit to the pre-commit workflow
1 parent 7e57766 commit bef16ec

13 files changed

Lines changed: 58 additions & 26 deletions

File tree

.github/workflows/python-test.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: format | lint | test
1+
name: format | lint | security | test
22
on: [push, pull_request]
33
jobs:
44
lint_unit_tests_coverage:
@@ -21,6 +21,9 @@ jobs:
2121
- name: Run linter
2222
run: |
2323
make code-lint
24+
- name: Run security check
25+
run: |
26+
make code-security
2427
- name: Run unit tests with coverage
2528
run: |
2629
git config --global user.email "you@example.com"

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,10 @@ repos:
3434
]
3535
files: "^(compliance|test)"
3636
stages: [commit]
37+
- repo: https://github.com/PyCQA/bandit
38+
rev: 1.7.0
39+
hooks:
40+
- id: bandit
41+
args: [--recursive]
42+
files: "^(compliance|test)"
43+
stages: [commit]

CHANGES.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# [1.19.0](https://github.com/ComplianceAsCode/auditree-framework/releases/tag/v1.19.0)
2+
3+
- [ADDED] Pre-commit hook for running `bandit` as part of CI/CD was added.
4+
- [CHANGED] Replaced the deprecated `imp` library with `importlib`.
5+
- [CHANGED] Replaced the deprecated `ibm_security_advisor_findings_api_sdk` library with `ibm_cloud_security_advisor`.
6+
- [FIXED] Added clarifying PagerDuty notifier documentation content.
7+
- [FIXED] Addressed `bandit` (minor) security issue findings.
8+
19
# [1.18.0](https://github.com/ComplianceAsCode/auditree-framework/releases/tag/v1.18.0)
210

311
- [CHANGED] Now using `pathlib` exclusively for operating system filepath and file functionality.

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ code-format:
3434
code-lint:
3535
pre-commit run flake8 --all-files
3636

37+
code-security:
38+
pre-commit run bandit --all-files
39+
3740
test::
3841
pytest --cov compliance test -v
3942

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[![OS Compatibility][platform-badge]](#prerequisites)
22
[![Python Compatibility][python-badge]][python]
33
[![pre-commit][pre-commit-badge]][pre-commit]
4-
[![Code validation](https://github.com/ComplianceAsCode/auditree-framework/workflows/format%20%7C%20lint%20%7C%20test/badge.svg)][lint-test]
4+
[![Code validation](https://github.com/ComplianceAsCode/auditree-framework/workflows/format%20%7C%20lint%20%7C%20security%20%7C%20test/badge.svg)][lint-test]
55
[![Upload Python Package](https://github.com/ComplianceAsCode/auditree-framework/workflows/PyPI%20upload/badge.svg)][pypi-upload]
66

77
# auditree-framework

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.18.0'
17+
__version__ = '1.19.0'

compliance/notify.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
from compliance.utils.services.github import Github
2929
from compliance.utils.test import parse_test_id
3030

31+
from ibm_cloud_sdk_core.api_exception import ApiException
3132
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
3233

33-
from ibm_security_advisor_findings_api_sdk import ApiException, FindingsApiV1
34+
from ibm_cloud_security_advisor import FindingsApiV1
3435

3536
import requests
3637

compliance/utils/path.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
# limitations under the License.
1515
"""Compliance automation path formatting utilities module."""
1616

17-
import imp
17+
import importlib
1818
import sys
1919
from pathlib import Path
2020

2121
from compliance.config import get_config
22+
from compliance.utils.data_parse import get_sha256_hash
2223

2324
FETCH_PREFIX = 'fetch_'
2425
CHECK_PREFIX = 'test_'
@@ -49,20 +50,22 @@ def load_evidences_modules(path):
4950
"""
5051
Load all evidences modules found within the ``path`` directory structure.
5152
52-
This function prevents double loading.
53-
5453
:param path: absolute path to a top level directory.
5554
"""
56-
subdirs = [p.parent for p in Path(path).rglob('evidences') if p.is_dir()]
57-
for subdir in subdirs:
55+
for ev_mod in [p for p in Path(path).rglob('evidences') if p.is_dir()]:
56+
module_name = f'evidences.{get_sha256_hash(ev_mod.parts, size=10)}'
57+
spec = None
5858
try:
59-
mod_data = imp.find_module('evidences', [str(subdir)])
59+
spec = importlib.util.spec_from_file_location(
60+
module_name, str(Path(ev_mod, '__init__.py'))
61+
)
6062
except ImportError:
6163
continue
62-
module_name = f'{subdir.name}.evidences'
63-
if module_name in sys.modules:
64+
if spec is None or module_name in sys.modules:
6465
continue
65-
imp.load_module(module_name, *mod_data)
66+
module = importlib.util.module_from_spec(spec)
67+
sys.modules[module_name] = module
68+
spec.loader.exec_module(module)
6669

6770

6871
def substitute_config(path_tmpl):

compliance/utils/services/github.py

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

1717
import json
18-
import random
18+
import secrets
1919
from collections import OrderedDict
2020
from urllib.parse import parse_qs, urlparse
2121

@@ -332,9 +332,9 @@ def creates_for_project(self, url, data, org=False):
332332
def rand_color(self):
333333
"""Generate a random color for labels."""
334334
return (
335-
f'{random.randint(0, 255):02X}'
336-
f'{random.randint(0, 255):02X}'
337-
f'{random.randint(0, 255):02X}'
335+
f'{secrets.randbelow(255):02X}'
336+
f'{secrets.randbelow(255):02X}'
337+
f'{secrets.randbelow(255):02X}'
338338
)
339339

340340
def create_label(self, repo, name, org=False):

doc-source/notifiers.rst

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ channel monitoring rotation management::
124124

125125

126126
This notifier also needs to know the credentials for sending message
127-
to your Slack organisation. Include the following in your credentials
127+
to your Slack organization. Include the following in your credentials
128128
file::
129129

130130
[slack]
@@ -161,7 +161,7 @@ option when executing your compliance checks.
161161

162162
Note that you have two options to configure the PagerDuty notifier:
163163

164-
* Provide a list of checks by class path within an accreditation. This allows you
164+
* Provide a list of checks by class path within an accreditation. This allows you
165165
to define which checks within the accreditation will trigger PageDuty notifications::
166166

167167
{
@@ -181,13 +181,22 @@ accreditations::
181181

182182
{
183183
"pagerduty": {
184-
"my.accred1": "SERVICE_ID"
184+
"my.accred1": "SERVICE_ID"
185185
}
186186
}
187187

188188
Note that the ``service_id`` field is the service id from PagerDuty, e.g. ``PABC123``.
189189
The PagerDuty notifier loads the active incidents to determine if
190190
it needs to create a new incident or update an existing one by using the ``service_id``.
191+
To get your service ID, go to your service in the PagerDuty dashboard and the
192+
service ID will be the last path element (7 characters) of the URL. For example
193+
for ``https://my-service/PABC123``, the service ID is ``PABC123``.
194+
195+
This notifier also needs to know the credentials for sending message to PagerDuty.
196+
Include the following in your credentials file::
197+
198+
[pagerduty]
199+
events_integration_key=XXX
191200

192201
GitHub Issue
193202
------------

0 commit comments

Comments
 (0)