Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b666ea5a3f
|
No files matched your search
@@ -56,6 +56,8 @@ jobs:
|
||||
python3 tests/test_qbit_feature_profile.py
|
||||
python3 tests/test_ci_maintenance.py
|
||||
python3 tests/test_rollout_version.py
|
||||
python3 tests/test_enforce_orb_checks.py
|
||||
python3 tests/test_static_analysis_exit.py
|
||||
python3 tests/test_validate_standard.py
|
||||
python3 tests/test_voyage_profile.py
|
||||
python3 tests/test_next_native.py
|
||||
|
||||
@@ -61,6 +61,8 @@ test-pipelines: pack
|
||||
python3 tests/test_qbit_hotfix_profile.py
|
||||
python3 tests/test_ci_maintenance.py
|
||||
python3 tests/test_rollout_version.py
|
||||
python3 tests/test_enforce_orb_checks.py
|
||||
python3 tests/test_static_analysis_exit.py
|
||||
python3 tests/test_validate_standard.py
|
||||
python3 tests/test_voyage_profile.py
|
||||
python3 tests/test_next_native.py
|
||||
|
||||
@@ -130,4 +130,9 @@ The organization mode has no fixed repository-count ceiling. Supply
|
||||
mode preserves its default of 68. Reports identify the inventory source and
|
||||
credential visibility limit: inaccessible repositories cannot be certified.
|
||||
This is discovery and audit, not automatic enrollment or branch-rule mutation.
|
||||
|
||||
For a qualified batch of canonical orb branches, use the shared
|
||||
[branch-check enforcement command](branch-enforcement.md). It adds the observed
|
||||
CircleCI setup/verify gates while preserving other protections. Source enrollment,
|
||||
scanner transitions and scheduled reconciliation remain separate requirements.
|
||||
Scheduling and authenticated onboarding still need to be configured centrally.
|
||||
@@ -0,0 +1,49 @@
|
||||
# Shared orb check enforcement
|
||||
|
||||
`tools/enforce_orb_checks.py` replaces repository-specific scripts for the shared
|
||||
CircleCI `setup` and `verify` branch gates. One invocation processes an explicit
|
||||
batch of current `main` and `develop` branches using existing `gh` authentication.
|
||||
|
||||
Create a targets file:
|
||||
|
||||
```json
|
||||
[
|
||||
{"repository": "QRun-IO/qbit-example", "branch": "main"},
|
||||
{"repository": "QRun-IO/qbit-example", "branch": "develop"}
|
||||
]
|
||||
```
|
||||
|
||||
Preview, then apply the same batch:
|
||||
|
||||
```sh
|
||||
python tools/enforce_orb_checks.py --targets targets.json --orb-version 0.8 --output enforcement-plan.json
|
||||
python tools/enforce_orb_checks.py --targets targets.json --orb-version 0.8 --output enforcement-result.json --apply
|
||||
```
|
||||
|
||||
Every branch must already contain the exact canonical caller for its manifest and
|
||||
have successful `setup` and `verify` checks from CircleCI app 18001 at its current
|
||||
commit. Supported profiles are QBit, Core, frontend-core and frontend-material.
|
||||
The complete batch is inspected before any mutation. Source/head/protection drift,
|
||||
unavailable APIs, unknown profiles, and ruleset-managed branches fail closed.
|
||||
|
||||
The tool appends the app-bound checks, enables strict checks and administrator
|
||||
enforcement, and reads the result back. Existing required checks remain required.
|
||||
For protected branches it uses the narrow status-check/admin endpoints, preserving
|
||||
reviews, signatures, linear history, restrictions and other settings. For a
|
||||
previously unprotected branch it creates protection without adding human reviews.
|
||||
It never changes scanner configuration, repository visibility, billing, or source.
|
||||
|
||||
Output records the planned head and original protection, then verification as each
|
||||
branch completes. A batch is not transactional: if an API call fails, inspect the
|
||||
output and rerun; successful protections are retained rather than rolled back.
|
||||
An already-correct branch needs no mutation.
|
||||
|
||||
This command covers the shared orb gate pair only. Continue to run the estate
|
||||
audit for scanner/native-worker requirements and other policy. Automatic repository
|
||||
discovery, source enrollment and scheduled reconciliation remain separate work.
|
||||
|
||||
GitHub API reference: [protected branches](https://docs.github.com/en/rest/branches/branch-protection).
|
||||
|
||||
```sh
|
||||
python -m unittest discover -s tests -p test_enforce_orb_checks.py -v
|
||||
```
|
||||
@@ -1,5 +1,5 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
set -eo pipefail
|
||||
|
||||
echo "Running PMD static analysis..."
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
set -eo pipefail
|
||||
|
||||
echo "Running SpotBugs static analysis..."
|
||||
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
"""Shared enforcement only adds proven gates and preserves unrelated policy."""
|
||||
import copy
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
import unittest
|
||||
|
||||
spec = importlib.util.spec_from_file_location('enforcement', Path(__file__).resolve().parents[1] / 'tools/enforce_orb_checks.py')
|
||||
enforcement = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(enforcement)
|
||||
|
||||
|
||||
def runs():
|
||||
return [{'id': index, 'name': name, 'app': {'id': 18001}, 'conclusion': 'success'}
|
||||
for index, name in enumerate(('setup', 'verify'), 1)]
|
||||
|
||||
|
||||
class EnforcementTests(unittest.TestCase):
|
||||
def test_preserves_security_gate_and_binds_orb_checks(self):
|
||||
before = {'required_status_checks': {'strict': False, 'contexts': ['security', 'setup'],
|
||||
'checks': [{'context': 'security', 'app_id': 15368}, {'context': 'setup', 'app_id': -1}]}}
|
||||
original = copy.deepcopy(before)
|
||||
result = enforcement.required_checks(before, runs())
|
||||
self.assertEqual(original, before)
|
||||
self.assertEqual(result, {'strict': True, 'checks': [{'context': 'security', 'app_id': 15368}, *enforcement.DESIRED]})
|
||||
|
||||
def test_wrong_app_failed_latest_and_missing_checks_reject(self):
|
||||
cases = [[], [runs()[0]], [dict(run, app={'id': 999}) for run in runs()],
|
||||
runs() + [dict(runs()[1], id=3, conclusion='failure')]]
|
||||
for candidate in cases:
|
||||
with self.subTest(candidate=candidate), self.assertRaises(ValueError):
|
||||
enforcement.required_checks(None, candidate)
|
||||
with self.assertRaises(ValueError):
|
||||
enforcement.required_checks({'required_status_checks': {'checks': [{'context': 'verify', 'app_id': 999}]}}, runs())
|
||||
|
||||
def test_existing_unrestricted_contexts_are_retained(self):
|
||||
result = enforcement.required_checks({'required_status_checks': {'contexts': ['legacy-build']}}, runs())
|
||||
self.assertIn({'context': 'legacy-build', 'app_id': -1}, result['checks'])
|
||||
|
||||
def fixture(self, before):
|
||||
state = {'before': copy.deepcopy(before), 'head': 'a' * 40, 'writes': []}
|
||||
def request(path, method='GET', payload=None):
|
||||
if method != 'GET':
|
||||
state['writes'].append((method, path, payload))
|
||||
if method == 'PUT':
|
||||
state['before'] = {**payload, 'enforce_admins': {'enabled': True}}
|
||||
elif method == 'PATCH':
|
||||
state['before']['required_status_checks'] = copy.deepcopy(payload)
|
||||
elif method == 'POST':
|
||||
state['before']['enforce_admins'] = {'enabled': True}
|
||||
return None
|
||||
if '/rules/' in path:
|
||||
return []
|
||||
if path.endswith('/protection'):
|
||||
return copy.deepcopy(state['before'])
|
||||
return {'commit': {'sha': state['head']}}
|
||||
plan = {'repository': 'QRun-IO/example', 'branch': 'develop', 'head': 'a' * 40,
|
||||
'before': copy.deepcopy(before), 'required_status_checks': enforcement.required_checks(before, runs()), 'changed': True}
|
||||
return plan, request, state
|
||||
|
||||
def test_narrow_updates_preserve_reviews_signatures_and_restrictions(self):
|
||||
before = {'required_status_checks': {'strict': False, 'checks': []}, 'enforce_admins': {'enabled': False},
|
||||
'required_signatures': {'enabled': True}, 'required_linear_history': {'enabled': True},
|
||||
'required_pull_request_reviews': {'required_approving_review_count': 2},
|
||||
'restrictions': {'users': [{'login': 'owner'}]}}
|
||||
plan, request, state = self.fixture(before)
|
||||
enforcement.enforce(plan, request)
|
||||
self.assertEqual(['PATCH', 'POST'], [write[0] for write in state['writes']])
|
||||
for key in ('required_signatures', 'required_linear_history', 'required_pull_request_reviews', 'restrictions'):
|
||||
self.assertEqual(before[key], state['before'][key])
|
||||
|
||||
def test_new_protection_and_idempotent_noop(self):
|
||||
plan, request, state = self.fixture(None)
|
||||
enforcement.enforce(plan, request)
|
||||
self.assertEqual('PUT', state['writes'][0][0])
|
||||
self.assertIsNone(state['before']['required_pull_request_reviews'])
|
||||
plan['before'] = copy.deepcopy(state['before'])
|
||||
plan['changed'] = False
|
||||
state['writes'] = []
|
||||
enforcement.enforce(plan, request)
|
||||
self.assertEqual([], state['writes'])
|
||||
|
||||
def test_head_or_protection_drift_never_mutates(self):
|
||||
for field, value in [('head', 'b' * 40), ('before', {'unexpected': True})]:
|
||||
plan, request, state = self.fixture(None)
|
||||
state[field] = value
|
||||
with self.assertRaises(ValueError):
|
||||
enforcement.enforce(plan, request)
|
||||
self.assertEqual([], state['writes'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,51 @@
|
||||
"""Maven failures must survive tee without overriding project analysis policy."""
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
SCRIPTS = [('mvn_spotbugs.sh', 'SPOTBUGS_FAIL_ON_ERROR', 'spotbugs.failOnError'),
|
||||
('mvn_pmd.sh', 'PMD_FAIL_ON_VIOLATION', 'pmd.failOnViolation')]
|
||||
|
||||
|
||||
class StaticAnalysisExitTests(unittest.TestCase):
|
||||
def invoke(self, script, variable, setting, status):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
binary = root / 'mvn'
|
||||
binary.write_text('#!/bin/sh\nprintf "%s\\n" "$@" > "$ARGUMENT_FILE"\necho analysis-output\nexit "$FAKE_MAVEN_EXIT"\n')
|
||||
binary.chmod(0o755)
|
||||
env = dict(os.environ, PATH=str(root) + os.pathsep + os.environ['PATH'],
|
||||
ARGUMENT_FILE=str(root / 'arguments'), FAKE_MAVEN_EXIT=str(status))
|
||||
env.pop(variable, None)
|
||||
if setting is not None:
|
||||
env[variable] = setting
|
||||
result = subprocess.run(['bash', str(ROOT / 'src/scripts' / script)], cwd=root,
|
||||
env=env, text=True, capture_output=True, timeout=15)
|
||||
return result, (root / 'arguments').read_text().splitlines()
|
||||
|
||||
def test_maven_failure_is_never_hidden_by_successful_tee(self):
|
||||
for script, variable, _ in SCRIPTS:
|
||||
for setting in ('true', 'false'):
|
||||
with self.subTest(script=script, setting=setting):
|
||||
result, _ = self.invoke(script, variable, setting, 17)
|
||||
self.assertEqual(17, result.returncode)
|
||||
self.assertIn('analysis-output', result.stdout)
|
||||
|
||||
def test_success_preserves_explicit_flag_and_project_defaults(self):
|
||||
for script, variable, property_name in SCRIPTS:
|
||||
for setting in ('true', 'false', None):
|
||||
with self.subTest(script=script, setting=setting):
|
||||
result, arguments = self.invoke(script, variable, setting, 0)
|
||||
self.assertEqual(0, result.returncode, result.stderr)
|
||||
if setting == 'true':
|
||||
self.assertIn('-D' + property_name + '=true', arguments)
|
||||
else:
|
||||
self.assertFalse(any(arg.startswith('-D' + property_name + '=') for arg in arguments))
|
||||
self.assertIn('-DskipTests', arguments)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,151 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Batch-enforce observed orb checks; dry-run unless --apply. Never changes source."""
|
||||
import argparse
|
||||
import base64
|
||||
import importlib.util
|
||||
import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
from urllib.parse import quote
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
spec = importlib.util.spec_from_file_location('standard', ROOT / 'src/scripts/validate_standard.py')
|
||||
standard = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(standard)
|
||||
DESIRED = [{'context': name, 'app_id': 18001} for name in ('setup', 'verify')]
|
||||
PROFILES = {'qbit', 'core', 'frontend-core', 'frontend-material'}
|
||||
|
||||
|
||||
def api(path, method='GET', payload=None):
|
||||
"""Use existing gh authentication; never expose response bodies on failure."""
|
||||
args = ['gh', 'api', '--method', method, path]
|
||||
if payload is not None:
|
||||
args += ['--input', '-']
|
||||
result = subprocess.run(args, input=None if payload is None else json.dumps(payload),
|
||||
text=True, encoding='utf-8', capture_output=True, timeout=60)
|
||||
if result.returncode:
|
||||
try:
|
||||
error = json.loads(result.stdout)
|
||||
except ValueError:
|
||||
error = {}
|
||||
if method == 'GET' and path.endswith('/protection') and error.get('status') == '404' and error.get('message') == 'Branch not protected':
|
||||
return None
|
||||
raise ValueError('GitHub operation unavailable: ' + method + ' ' + path.split('?')[0])
|
||||
return json.loads(result.stdout) if result.stdout.strip() else None
|
||||
|
||||
|
||||
def required_checks(before, runs):
|
||||
"""Append app-bound gates only after both succeeded; preserve all other gates."""
|
||||
for desired in DESIRED:
|
||||
matches = [run for run in runs if run['name'] == desired['context'] and run['app']['id'] == desired['app_id']]
|
||||
if not matches or max(matches, key=lambda run: run['id'])['conclusion'] != 'success':
|
||||
raise ValueError('Current branch head has no successful CircleCI ' + desired['context'])
|
||||
prior = (before or {}).get('required_status_checks') or {}
|
||||
checks = [dict(check) for check in prior.get('checks', [])]
|
||||
represented = {check['context'] for check in checks}
|
||||
checks += [{'context': name, 'app_id': -1} for name in prior.get('contexts', []) if name not in represented]
|
||||
for desired in DESIRED:
|
||||
matches = [check for check in checks if check['context'] == desired['context']]
|
||||
if any(check.get('app_id') not in (None, -1, desired['app_id']) for check in matches):
|
||||
raise ValueError('Existing gate uses a different app: ' + desired['context'])
|
||||
checks = [check for check in checks if check['context'] != desired['context']]
|
||||
checks.append(dict(desired))
|
||||
return {'strict': True, 'checks': checks}
|
||||
|
||||
|
||||
def inspect(repo, branch, version, request=api):
|
||||
"""Read immutable canonical source, latest check runs, and current protections."""
|
||||
if not re.fullmatch(r'[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+', repo) or branch not in ('main', 'develop'):
|
||||
raise ValueError('Targets must name an exact repository and main/develop branch')
|
||||
root = 'repos/' + repo
|
||||
info = request(root)
|
||||
if info['archived']:
|
||||
raise ValueError('Archived repository')
|
||||
prefix = root + '/branches/' + branch
|
||||
head = request(prefix)['commit']['sha']
|
||||
source = []
|
||||
for path in ('.circleci/config.yml', '.qqq-ci.yml'):
|
||||
item = request(root + '/contents/' + quote(path, safe='/') + '?ref=' + head)
|
||||
if item.get('encoding') != 'base64' or item.get('type') != 'file':
|
||||
raise ValueError('Canonical declarations must be regular files')
|
||||
source.append(base64.b64decode(item['content'], validate=False).decode('utf-8'))
|
||||
standard.validate(*source, version)
|
||||
if standard.load_yaml(source[1])['profile'] not in PROFILES:
|
||||
raise ValueError('Profile does not use the shared setup/verify gate pair')
|
||||
rules = request(root + '/rules/branches/' + branch)
|
||||
if rules:
|
||||
raise ValueError('Ruleset-managed branch requires ruleset reconciliation')
|
||||
before = request(prefix + '/protection')
|
||||
runs = []
|
||||
page = 1
|
||||
while True:
|
||||
batch = request(root + '/commits/' + head + '/check-runs?filter=latest&per_page=100&page=' + str(page))['check_runs']
|
||||
runs += batch
|
||||
if len(batch) < 100:
|
||||
break
|
||||
page += 1
|
||||
desired = required_checks(before, runs)
|
||||
current = (before or {}).get('required_status_checks') or {}
|
||||
ready = (current.get('strict') is True and current.get('checks') == desired['checks']
|
||||
and (before or {}).get('enforce_admins', {}).get('enabled') is True)
|
||||
return {'repository': repo, 'branch': branch, 'head': head, 'before': before,
|
||||
'required_status_checks': desired, 'changed': not ready}
|
||||
|
||||
|
||||
def enforce(plan, request=api):
|
||||
"""Recheck drift, use narrow updates, and verify preservation of other settings."""
|
||||
root = 'repos/' + plan['repository']
|
||||
prefix = root + '/branches/' + plan['branch']
|
||||
if request(prefix)['commit']['sha'] != plan['head'] or request(prefix + '/protection') != plan['before']:
|
||||
raise ValueError('Branch or protection changed after preflight')
|
||||
if request(root + '/rules/branches/' + plan['branch']):
|
||||
raise ValueError('Ruleset changed after preflight')
|
||||
if not plan['changed']:
|
||||
return
|
||||
before = plan['before']
|
||||
if before is None:
|
||||
request(prefix + '/protection', 'PUT', {
|
||||
'required_status_checks': plan['required_status_checks'], 'enforce_admins': True,
|
||||
'required_pull_request_reviews': None, 'restrictions': None})
|
||||
else:
|
||||
request(prefix + '/protection/required_status_checks', 'PATCH', plan['required_status_checks'])
|
||||
if not before['enforce_admins']['enabled']:
|
||||
request(prefix + '/protection/enforce_admins', 'POST')
|
||||
after = request(prefix + '/protection')
|
||||
actual = after['required_status_checks']
|
||||
if not actual['strict'] or actual['checks'] != plan['required_status_checks']['checks'] or not after['enforce_admins']['enabled']:
|
||||
raise ValueError('Protection readback did not match requested gates')
|
||||
if before:
|
||||
for key, value in before.items():
|
||||
if key not in ('required_status_checks', 'enforce_admins') and after.get(key) != value:
|
||||
raise ValueError('Unrelated protection changed: ' + key)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--targets', type=Path, required=True, help='JSON list of {repository, branch}')
|
||||
parser.add_argument('--orb-version', required=True)
|
||||
parser.add_argument('--apply', action='store_true')
|
||||
parser.add_argument('--output', type=Path, required=True)
|
||||
args = parser.parse_args()
|
||||
targets = json.loads(args.targets.read_text(encoding='utf-8'))
|
||||
if not isinstance(targets, list) or not targets:
|
||||
raise ValueError('A nonempty explicit target list is required')
|
||||
if len({(row['repository'], row['branch']) for row in targets}) != len(targets):
|
||||
raise ValueError('Duplicate branch target')
|
||||
# Resolve the entire batch before any mutations. Failures never trigger rollback
|
||||
# that would remove successful protections; rerunning converges idempotently.
|
||||
plans = [inspect(row['repository'], row['branch'], args.orb_version) for row in targets]
|
||||
args.output.write_text(json.dumps({'state': 'planned', 'plans': plans}, indent=2), encoding='utf-8')
|
||||
if args.apply:
|
||||
for plan in plans:
|
||||
enforce(plan)
|
||||
plan['verified'] = True
|
||||
args.output.write_text(json.dumps({'state': 'applying', 'plans': plans}, indent=2), encoding='utf-8')
|
||||
args.output.write_text(json.dumps({'state': 'verified', 'plans': plans}, indent=2), encoding='utf-8')
|
||||
print(json.dumps({'targets': len(plans), 'changed': sum(plan['changed'] for plan in plans), 'applied': args.apply}))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in new issue
Block a user