lab7: безопасность, mTLS, OpenBao и PKI lifecycle
This commit is contained in:
Executable
+108
@@ -0,0 +1,108 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Pinned source/image vulnerability scans with no Docker socket in scanners."""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
from lab7_environment import Environment, ROOT
|
||||
|
||||
TRIVY = 'aquasec/trivy:0.76.2@sha256:8f6bb947dbc528902e11f2f5be52f7b519f6a39d5d148f41166f2040499768fb'
|
||||
GOVULNCHECK = 'golang.org/x/vuln/cmd/govulncheck@v1.9.0'
|
||||
|
||||
|
||||
def source_messages(text: str) -> list[dict]:
|
||||
"""govulncheck emits consecutive pretty-printed JSON objects, not JSONL."""
|
||||
decoder = json.JSONDecoder()
|
||||
messages, offset = [], 0
|
||||
while offset < len(text):
|
||||
while offset < len(text) and text[offset].isspace():
|
||||
offset += 1
|
||||
if offset == len(text):
|
||||
break
|
||||
message, offset = decoder.raw_decode(text, offset)
|
||||
messages.append(message)
|
||||
return messages
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--source-only', action='store_true')
|
||||
parser.add_argument('--output', type=Path, default=ROOT / 'evidence/lab7/scans')
|
||||
args = parser.parse_args()
|
||||
output = args.output.resolve()
|
||||
output.mkdir(parents=True, exist_ok=True)
|
||||
cache = ROOT / '.cache/lab7-scans'
|
||||
cache.mkdir(parents=True, exist_ok=True)
|
||||
tool = ROOT / 'bin/govulncheck'
|
||||
if not tool.exists():
|
||||
subprocess.run(['go', 'install', GOVULNCHECK], cwd=ROOT,
|
||||
env={**os.environ, 'GOBIN': str(tool.parent)}, check=True)
|
||||
source = subprocess.run([str(tool), '-json', './...'], cwd=ROOT,
|
||||
capture_output=True, text=True, timeout=300)
|
||||
messages = source_messages(source.stdout)
|
||||
(output / 'govulncheck.json').write_text(json.dumps(messages, indent=2) + '\n')
|
||||
(output / 'govulncheck-status.txt').write_text('exit=' + str(source.returncode) + '\n' + source.stderr)
|
||||
advisories = {scope: set() for scope in ('symbol', 'package', 'module')}
|
||||
for message in messages:
|
||||
finding = message.get('finding')
|
||||
if finding:
|
||||
for scope, field in (('symbol', 'function'), ('package', 'package'), ('module', 'module')):
|
||||
if any(frame.get(field) for frame in finding.get('trace', [])):
|
||||
advisories[scope].add(finding['osv'])
|
||||
summary = {'govulncheck': {
|
||||
'exit': source.returncode, 'tool': GOVULNCHECK,
|
||||
'advisories': {'symbol': sorted(advisories['symbol']),
|
||||
'package_only': sorted(advisories['package'] - advisories['symbol']),
|
||||
'module_only': sorted(advisories['module'] - advisories['package'])},
|
||||
}, 'images': []}
|
||||
environment = Environment()
|
||||
if not args.source_only:
|
||||
images = sorted(set(environment.compose('config', '--images').stdout.split()))
|
||||
for index, image in enumerate(images):
|
||||
archive = cache / ('image-' + uuid.uuid4().hex + '.tar')
|
||||
subprocess.run(['docker', 'image', 'save', '--output', str(archive), image],
|
||||
cwd=ROOT, check=True, timeout=300)
|
||||
filename = f'image-{index:02d}.json'
|
||||
(output / filename).unlink(missing_ok=True)
|
||||
try:
|
||||
scan = subprocess.run([
|
||||
'docker', 'run', '--rm', '--cap-drop', 'ALL',
|
||||
'--security-opt', 'no-new-privileges:true',
|
||||
'--mount', f'type=bind,source={cache},target=/cache',
|
||||
'--mount', f'type=bind,source={output},target=/reports',
|
||||
TRIVY, 'image', '--input', '/cache/' + archive.name, '--cache-dir', '/cache/trivy',
|
||||
'--scanners', 'vuln', '--format', 'json', '--output', '/reports/' + filename,
|
||||
'--timeout', '5m',
|
||||
], cwd=ROOT, capture_output=True, text=True, timeout=330)
|
||||
(output / f'image-{index:02d}-status.txt').write_text(
|
||||
'exit=' + str(scan.returncode) + '\n' + scan.stdout + scan.stderr)
|
||||
counts = {}
|
||||
if scan.returncode == 0 and (output / filename).exists():
|
||||
report = json.loads((output / filename).read_text())
|
||||
for result in report.get('Results', []):
|
||||
for finding in result.get('Vulnerabilities') or []:
|
||||
severity = finding.get('Severity', 'UNKNOWN')
|
||||
counts[severity] = counts.get(severity, 0) + 1
|
||||
summary['images'].append({'image': image, 'exit': scan.returncode,
|
||||
'file': filename, 'counts': counts})
|
||||
print(json.dumps(summary['images'][-1]), flush=True)
|
||||
finally:
|
||||
archive.unlink(missing_ok=True)
|
||||
summary['trivy'] = TRIVY
|
||||
(output / 'summary.json').write_text(json.dumps(summary, indent=2) + '\n')
|
||||
print(json.dumps(summary), flush=True)
|
||||
return 0 if source.returncode in (0, 3) and all(item['exit'] == 0 for item in summary['images']) else 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
raise SystemExit(main())
|
||||
except (OSError, subprocess.SubprocessError, json.JSONDecodeError) as error:
|
||||
print('SCAN FAILED: ' + str(error), file=sys.stderr)
|
||||
raise SystemExit(1)
|
||||
Reference in New Issue
Block a user