Skip to content

Commit 71ff18f

Browse files
authored
Fixes from demonstrations in January 2023 (#143)
* Pivots the World Clock API demo to use a GitHub API * Fixes support for binary evidence types * Add binary evidence signing test * Updates the demo configuration for remote lockers on main branches * Version 1.24.0
1 parent d030464 commit 71ff18f

11 files changed

Lines changed: 122 additions & 99 deletions

File tree

CHANGES.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
# [UNRELEASED](https://github.com/ComplianceAsCode/auditree-framework/releases/tag/UNRELEASED)
1+
# [1.24.0](https://github.com/ComplianceAsCode/auditree-framework/releases/tag/v1.24.0)
22

33
- [FIXED] Update pre-commit dependencies.
44
- [CHANGED] Use python 3.8 in GitHub Actions as newer flake8 does not support less than that.
55
- [CHANGED] Dot not update pre-commit hooks during "make develop"
66
- [ADDED] Add basic pre-commit hooks.
7+
- [FIXED] Support for agent signing of binary content
8+
- [FIXED] Demo fetcher/check for World Clock API replaced with GitHub API example
79

810
# [1.23.0](https://github.com/ComplianceAsCode/auditree-framework/releases/tag/v1.23.0)
911

compliance/__init__.py

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

16-
__version__ = '1.23.1'
16+
__version__ = '1.24.0'

compliance/evidence.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ def __init__(self, name, category, ttl=DAY, description='', **kwargs):
8080
self._evidence_dt = kwargs.get('evidence_dt')
8181
self._raw_content = None
8282
self._signature = None
83+
self.binary_content = kwargs.get('binary_content', False)
84+
self.filtered_content = kwargs.get('filtered_content', False)
8385

8486
@classmethod
8587
def from_evidence(cls, evidence):
@@ -223,8 +225,12 @@ def set_content(self, content, sign=True):
223225
if self.extension == 'json':
224226
self._content = format_json(json.loads(content))
225227
if sign:
228+
if self.binary_content:
229+
data_bytes = self._content
230+
else:
231+
data_bytes = self._content.encode()
226232
self._digest, self._signature = self.agent.hash_and_sign(
227-
self._content.encode()
233+
data_bytes
228234
)
229235

230236
def set_digest(self, digest):
@@ -291,8 +297,6 @@ def __init__(self, *args, **kwargs):
291297
)
292298
self.part_fields = partition.get('fields')
293299
self.part_root = partition.get('root')
294-
self.binary_content = kwargs.get('binary_content', False)
295-
self.filtered_content = kwargs.get('filtered_content', False)
296300

297301
@property
298302
def is_partitioned(self):

demo/auditree_demo.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"locker": {
3-
"url": "https://github.com/MY_ORG/MY_EVIDENCE_REPO"
3+
"default_branch": "main",
4+
"repo_url": "https://github.com/MY_ORG/MY_EVIDENCE_REPO"
45
},
56
"notify": {
67
"slack": {

demo/controls.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"arboretum.auditree.checks.test_python_packages.PythonPackageCheck": ["demo.arboretum.accred"],
3-
"demo_examples.checks.test_world_clock.WorldClockCheck": ["demo.custom.accred"],
3+
"demo_examples.checks.test_github_api_versions.GitHubAPIVersionsCheck": ["demo.custom.accred"],
44
"demo_examples.checks.test_image_content.ImageCheck": ["demo.custom.accred"]
55
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# -*- mode:python; coding:utf-8 -*-
2+
# Copyright (c) 2020 IBM Corp. All rights reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import json
17+
18+
from compliance.check import ComplianceCheck
19+
from compliance.evidence import with_raw_evidences
20+
21+
class GitHubAPIVersionsCheck(ComplianceCheck):
22+
"""Perform analysis on GitHub supported versions API response evidence."""
23+
24+
@property
25+
def title(self):
26+
"""
27+
Return the title of the checks.
28+
29+
:returns: the title of the checks
30+
"""
31+
return 'GitHub API Versions'
32+
33+
@with_raw_evidences('github/api_versions.json')
34+
def test_supported_versions(self, evidence):
35+
"""
36+
Check whether there are any supported versions.
37+
38+
Always warn about something, for demo purposes.
39+
"""
40+
version_list = json.loads(evidence.content)
41+
versions_str = ', '.join(version_list)
42+
if not version_list:
43+
self.add_failures(
44+
'Supported GitHub API Versions Violation',
45+
f'No API versions were indicated as supported by GitHub.'
46+
)
47+
elif len(version_list) == 1:
48+
self.add_warnings(
49+
'Supported GitHub API Versions Warning',
50+
f'There is only one supported version. Get with the program: {versions_str}'
51+
)
52+
elif len(version_list) > 1:
53+
self.add_warnings(
54+
'Supported GitHub API Versions Warning',
55+
f'There are more than one supported versions. Check the docs for the latest changes: {versions_str}'
56+
)
57+
58+
def get_reports(self):
59+
"""
60+
Provide the check report name.
61+
62+
:returns: the report(s) generated for this check
63+
"""
64+
return ['github/api_versions.md']
65+
66+
def msg_supported_versions(self):
67+
"""
68+
Supported GitHub API versions check notifier.
69+
70+
:returns: notification dictionary.
71+
"""
72+
return {'subtitle': 'Supported GitHub API Versions Violation', 'body': None}

demo/demo_examples/checks/test_world_clock.py

Lines changed: 0 additions & 76 deletions
This file was deleted.

demo/demo_examples/evidences/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
get_config().add_evidences(
1919
[
2020
RawEvidence(
21-
'world_clock_utc.json',
22-
'time',
21+
'api_versions.json',
22+
'github',
2323
DAY,
24-
'Coordinated Universal Time'
24+
'Supported GitHub API versions'
2525
),
2626
RawEvidence(
2727
'auditree_logo.png',
@@ -31,10 +31,10 @@
3131
binary_content=True
3232
),
3333
ReportEvidence(
34-
'world_clock.md',
35-
'time',
34+
'api_versions.md',
35+
'github',
3636
DAY,
37-
'World Clock Analysis report.'
37+
'Supported GitHub versions report.'
3838
),
3939
ReportEvidence(
4040
'image_check.md',

demo/demo_examples/fetchers/fetch_world_clock.py renamed to demo/demo_examples/fetchers/fetch_github_api_versions.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,22 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
import json
1415

1516
from compliance.evidence import store_raw_evidence
1617
from compliance.fetch import ComplianceFetcher
1718

18-
class WorldClockFetcher(ComplianceFetcher):
19-
"""Fetch the current coordinated universal time."""
19+
class GitHubAPIVersionsFetcher(ComplianceFetcher):
20+
"""Fetch the current supported GitHub API versions."""
2021

21-
@store_raw_evidence('time/world_clock_utc.json')
22-
def fetch_world_clock_utc(self):
23-
"""Fetch the universal time."""
24-
session = self.session('http://worldclockapi.com/api/json')
25-
clock = session.get('utc/now')
26-
clock.raise_for_status()
27-
return clock.text
22+
@store_raw_evidence('github/api_versions.json')
23+
def fetch_api_versions(self):
24+
"""Fetch the current supported GitHub API versions."""
25+
# This is where you might e.g. fetch your evidence
26+
# from a remote API
27+
session = self.session('https://api.github.com/')
28+
versions = session.get(
29+
'versions',
30+
headers={"Accept": "application/json"})
31+
versions.raise_for_status()
32+
return versions.text

demo/demo_examples/templates/reports/time/world_clock.md.tmpl renamed to demo/demo_examples/templates/reports/time/api_versions.md.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ limitations under the License.
1515
#}
1616
# {{ test.title }} Report: {{ now.strftime('%Y-%m-%d') }}
1717

18-
This report contains all world clock check violations.
18+
This report contains all GitHub API versions violations
1919

2020
{% if test.total_issues_count(results) == 0 %}
2121
**No violations found!**

0 commit comments

Comments
 (0)