-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add recent commits locker integrity checks (#17)
- Loading branch information
Showing
13 changed files
with
382 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
arboretum/auditree/checks/test_locker_commit_integrity.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
# -*- mode:python; coding:utf-8 -*- | ||
# Copyright (c) 2020 IBM Corp. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Evidence locker commit integrity checks.""" | ||
|
||
from urllib.parse import urlparse | ||
|
||
from arboretum.auditree.evidences.repo_branch_protection import ( | ||
RepoBranchProtectionEvidence | ||
) | ||
from arboretum.auditree.evidences.repo_commit import RepoCommitEvidence | ||
|
||
from compliance.check import ComplianceCheck | ||
from compliance.evidence import DAY, ReportEvidence, evidences | ||
|
||
|
||
class LockerCommitIntegrityCheck(ComplianceCheck): | ||
"""Monitor the integrity of evidence locker repository commits.""" | ||
|
||
@property | ||
def title(self): | ||
""" | ||
Return the title of the checks. | ||
:returns: the title of the checks | ||
""" | ||
return 'Locker Commit Integrity' | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
"""Initialize the check object with configuration settings.""" | ||
cls.config.add_evidences( | ||
[ | ||
ReportEvidence( | ||
'locker_commit_integrity.md', | ||
'auditree', | ||
DAY, | ||
'Evidence locker commit integrity report.' | ||
) | ||
] | ||
) | ||
return cls | ||
|
||
def test_recent_commit_integrity(self): | ||
"""Check that recent commits are signed.""" | ||
locker_branches = self.config.get( | ||
'org.auditree.locker_integrity.branches', | ||
self.config.get( | ||
'org.auditree.repo_integrity.branches', | ||
{self.config.get('locker.repo_url'): ['master']} | ||
) | ||
) | ||
for locker_url, branches in locker_branches.items(): | ||
parsed = urlparse(locker_url) | ||
service = 'gh' | ||
if 'gitlab' in parsed.hostname: | ||
service = 'gl' | ||
elif 'bitbucket' in parsed.hostname: | ||
service = 'bb' | ||
repo = parsed.path.strip('/') | ||
for branch in branches: | ||
filename = [ | ||
service, | ||
repo.lower().replace('/', '_').replace('-', '_'), | ||
branch.lower().replace('-', '_'), | ||
'recent_commits.json' | ||
] | ||
path = f'raw/auditree/{"_".join(filename)}' | ||
with evidences(self, path) as raw: | ||
evidence = RepoCommitEvidence.from_evidence(raw) | ||
for commit in evidence.commit_signed_status: | ||
if not commit['signed']: | ||
self.add_failures( | ||
'Locker Recent Commits - (Unsigned)', | ||
( | ||
f'[{commit["sha"][:8]}]({commit["url"]}) ' | ||
f'commit in `{locker_url}` ' | ||
f'`{branch}` branch.' | ||
) | ||
) | ||
|
||
def test_branch_protection_commit_integrity(self): | ||
"""Check that branch protection requires signed commits.""" | ||
locker_branches = self.config.get( | ||
'org.auditree.locker_integrity.branches', | ||
self.config.get( | ||
'org.auditree.repo_integrity.branches', | ||
{self.config.get('locker.repo_url'): ['master']} | ||
) | ||
) | ||
for locker_url, branches in locker_branches.items(): | ||
parsed = urlparse(locker_url) | ||
service = 'gh' | ||
if 'gitlab' in parsed.hostname: | ||
service = 'gl' | ||
elif 'bitbucket' in parsed.hostname: | ||
service = 'bb' | ||
repo = parsed.path.strip('/') | ||
for branch in branches: | ||
filename = [ | ||
service, | ||
repo.lower().replace('/', '_').replace('-', '_'), | ||
branch.lower().replace('-', '_'), | ||
'branch_protection.json' | ||
] | ||
path = f'raw/auditree/{"_".join(filename)}' | ||
with evidences(self, path) as raw: | ||
evidence = RepoBranchProtectionEvidence.from_evidence(raw) | ||
if not evidence.signed_commits_required: | ||
self.add_failures( | ||
( | ||
'Locker Branch Protection - ' | ||
'(Signed Commits Disabled)' | ||
), | ||
f'`{locker_url}` `{branch}` branch.' | ||
) | ||
|
||
def get_reports(self): | ||
""" | ||
Provide the check report name. | ||
:returns: the report(s) generated for this check | ||
""" | ||
return ['auditree/locker_commit_integrity.md'] | ||
|
||
def msg_recent_commit_integrity(self): | ||
""" | ||
Evidence locker recent commits integrity check notifier. | ||
:returns: notification dictionary | ||
""" | ||
return {'subtitle': 'Locker signed commits', 'body': None} | ||
|
||
def msg_branch_protection_commit_integrity(self): | ||
""" | ||
Evidence locker branch protection commit integrity check notifier. | ||
:returns: notification dictionary | ||
""" | ||
return {'subtitle': 'Locker require commit signatures', 'body': None} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# -*- mode:python; coding:utf-8 -*- | ||
# Copyright (c) 2020 IBM Corp. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Repository commit evidence.""" | ||
|
||
import json | ||
|
||
from compliance.evidence import RawEvidence | ||
|
||
|
||
class RepoCommitEvidence(RawEvidence): | ||
"""Repository commit raw evidence class.""" | ||
|
||
@property | ||
def commit_signed_status(self): | ||
"""Provide verified/signed status for each commit as a list.""" | ||
if self.content: | ||
rs_factory = { | ||
'gh': self._get_gh_commit_signed_status, | ||
'gl': self._get_gl_commit_signed_status, | ||
'bb': self._get_bb_commit_signed_status | ||
} | ||
if not hasattr(self, '_commit_signed_status'): | ||
self._commit_signed_status = rs_factory[self.name[:2]]() | ||
return self._commit_signed_status | ||
|
||
def _get_gh_commit_signed_status(self): | ||
commits = [] | ||
for commit in json.loads(self.content): | ||
commits.append( | ||
{ | ||
'sha': commit['sha'], | ||
'url': commit['html_url'], | ||
'signed': commit['commit']['verification']['verified'] | ||
} | ||
) | ||
return commits | ||
|
||
def _get_gl_commit_signed_status(self): | ||
raise NotImplementedError('Support for Gitlab coming soon...') | ||
|
||
def _get_bb_commit_signed_status(self): | ||
raise NotImplementedError('Support for Bitbucket coming soon...') |
Oops, something went wrong.