droboticflow-new/scripts/lib/release_checks.py

106 lines
5.8 KiB
Python

"""Private, bounded local-check evidence. Never log kubectl payloads here."""
import hashlib
import json
import os
from pathlib import Path
import re
import subprocess
import time
import uuid
SENSITIVE = '[A-Za-z0-9_-]{0,96}(?:password|passwd|secret|token|authorization|cookie|credential|access[_-]?key|secret[_-]?key|phone)[A-Za-z0-9_-]{0,96}|(?:BOS|BCE|AWS)[_-](?:AK|SK)|AK|SK'
def redact(text):
"""Remove credential-bearing fields and URLs before persisting bounded local test output."""
text = '\n'.join(line if len(line) <= 65536 else '<oversized-line-redacted>' for line in text.splitlines())
text = re.sub('(?is)(?<![A-Za-z0-9_-])([\\"\\\']?(?:' + SENSITIVE + ')[\\"\\\']?\\s*[:=]\\s*)(\\"[^\\"]*\\"|\\\'[^\\\']*\\\'|[^\\s,}]+)', '\\1<redacted>', text)
text = re.sub('(?i)\\bBearer\\s+\\S+', 'Bearer <redacted>', text)
text = re.sub('(?i)\\b(?:https?|mongodb(?:\\+srv)?)://[^\\s\\"\\\'<>]+', '<redacted-url>', text)
lines = ['<redacted-sensitive-line>' if re.search('(?i)\\b(?:' + SENSITIVE + ')\\b\\s*:', line) else line for line in text.splitlines()]
text = '\n'.join(lines)
limit = 2 * 1024 * 1024
if len(text) > limit:
text = text[:limit // 2] + '\n<output-truncated>\n' + text[-limit // 2:]
return text
def write_log(folder, argv, output, result):
folder = Path(folder)
folder.mkdir(parents=True, exist_ok=True, mode=448)
path = folder / ('check-' + uuid.uuid4().hex + '.log')
data = redact(json.dumps({'argv': argv, 'result': result}) + '\n' + output) + '\n'
with os.fdopen(os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 384), 'w') as f:
f.write(data)
return {'log': str(path.resolve()), 'logSHA256': hashlib.sha256(path.read_bytes()).hexdigest()}
def check_environment():
"""Local synthetic checks inherit no company credentials or application/test switches."""
return {k: v for k, v in os.environ.items() if not re.search('(?i)(password|secret|token|credential|access_key|secret_key|^BOS_|^BCE_|^AWS_|(^|_)MONGO(_|$)|(_URI|_URL)$|^KUBECONFIG$|^ANNOTATION_|^DROBOTICFLOW_|^DEPLOY_ENV$|^UPDATE_|^REGENERATE_|_TEST$|_TEST_|^TEST_.*MONGO)', k)}
def source_fingerprint(root):
"""Hash the complete non-ignored source tree; unavailable Git identity is never reusable."""
root = Path(root)
h = hashlib.sha256()
try:
names = subprocess.check_output(['git', 'ls-files', '-z', '--cached', '--others', '--exclude-standard'], cwd=root, stderr=subprocess.DEVNULL).decode().split('\x00')
except (OSError, subprocess.SubprocessError):
return None
for name in sorted(set(filter(None, names))):
path = root / name
h.update(name.encode())
if path.is_symlink():
h.update(os.readlink(path).encode())
elif path.is_file():
h.update(path.read_bytes())
else:
h.update(b'<missing>')
return h.hexdigest()
def identity(root, commit, policy_hash):
"""Bind source, installed dependency contents, tool versions and a hash of the sanitized environment. Values and dependency contents are not persisted."""
root = Path(root)
h = hashlib.sha256()
dependencies = root / 'node_modules'
if dependencies.is_dir():
visited = set()
for base, dirs, files in os.walk(dependencies, followlinks=True):
stat = Path(base).stat()
key = (stat.st_dev, stat.st_ino)
if key in visited:
dirs[:] = []
continue
visited.add(key)
h.update(str(Path(base).relative_to(dependencies)).encode())
if Path(base).is_symlink():
h.update(os.readlink(base).encode())
dirs[:] = sorted((d for d in dirs if d not in ('.cache', '.vite', '.vite-temp')))
for name in sorted(files):
p = Path(base) / name
h.update(str(p.relative_to(dependencies)).encode())
if p.is_symlink():
h.update(os.readlink(p).encode())
else:
with p.open('rb') as f:
for block in iter(lambda: f.read(1024 * 1024), b''):
h.update(block)
else:
h.update(b'no-installed-node-dependencies')
versions = {}
for name, args in [('python', ['python3', '--version']), ('node', ['node', '--version']), ('go', ['go', 'version']), ('ffmpeg', ['ffmpeg', '-version'])]:
try:
versions[name] = subprocess.check_output(args, cwd=root, stderr=subprocess.STDOUT, timeout=10).decode().splitlines()[0]
except (OSError, subprocess.SubprocessError):
versions[name] = 'unavailable'
env_hash = hashlib.sha256(json.dumps(check_environment(), sort_keys=True).encode()).hexdigest()
source = source_fingerprint(root)
complete = bool(source) and dependencies.is_dir() and all((v != 'unavailable' for v in versions.values()))
return {'complete': complete, 'sourceTree': source, 'commit': commit, 'policyHash': policy_hash, 'dependencies': h.hexdigest(), 'tools': versions, 'environment': env_hash}
def reusable(entry, expected, argv):
"""Only explicitly listed pure checks may reuse unchanged, unexpired and intact success evidence. Browser, Mongo and timing checks always rerun."""
pure = argv[:3] == ['npm', 'run', 'lint:web'] or argv[:3] == ['npm', 'run', 'test:annotation:persistence'] or argv[:3] == ['npm', 'run', 'test:review:decisions'] or (argv[:3] == ['npm', 'run', 'test:review:drafts']) or (argv[:3] == ['python3', '-m', 'unittest'])
if not expected.get('complete') or not pure or entry.get('identity') != expected or (entry.get('result') != 'passed'):
return False
if not 0 <= time.time() - entry.get('finishedUnix', 0) <= 3600:
return False
path = Path(entry.get('log', ''))
return path.is_file() and hashlib.sha256(path.read_bytes()).hexdigest() == entry.get('logSHA256')