Files

404 lines
24 KiB
Python
Executable File

#!/usr/bin/env python3
"""Real local HTTPS, protocol ACL, identity and container lab7 acceptance.
Usage: python3 student-tests/lab7_security.py --ca-file secrets/lab7/pki/operator/current/ca.crt
Requires a bootstrapped running stand and python:3.14-slim available to Docker.
Operator-only credentials are mounted exclusively in one-shot probes. Evidence
contains classifications/status codes, never credentials or response bodies.
"""
import argparse
import base64
import datetime as dt
import hashlib
import json
import os
from pathlib import Path
import re
import struct
import sys
import time
import urllib.parse
import uuid
import zlib
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'tests'))
from client import Client, Failure
from lab7_helpers import Stand, ProbeFailure, command, require, http, temporary_directory
class Suite:
def __init__(self):
self.results = []
def run(self, name, fn):
started = time.monotonic()
try:
details = fn() or {}
result = {'name': name, 'passed': True, 'classification': 'passed', 'details': details}
except ProbeFailure as exc:
result = {'name': name, 'passed': False, 'classification': exc.classification, 'detail': exc.detail}
except Failure:
result = {'name': name, 'passed': False, 'classification': 'public_api_contract_failure'}
except Exception as exc:
result = {'name': name, 'passed': False, 'classification': 'probe_exception', 'type': type(exc).__name__}
result['seconds'] = round(time.monotonic() - started, 3)
self.results.append(result)
def public_tls(stand, client, ca):
client.call('GET', '/health/ready', schema='Health')
with temporary_directory(prefix='lab7-untrusted-') as temp:
key, cert = Path(temp) / 'key.pem', Path(temp) / 'cert.pem'
command(['openssl', 'req', '-x509', '-newkey', 'rsa:2048', '-nodes', '-keyout', str(key),
'-out', str(cert), '-days', '1', '-subj', '/CN=local-untrusted'], timeout=30)
observations = {}
for name, host, trust, expected in [('wrong_ca', 'localhost', cert, 'ca_rejected'),
('wrong_hostname', '127.0.0.1', ca, 'hostname_rejected')]:
try:
http(host, int(urllib.parse.urlsplit(stand.base_url).port or 8443),
'GET', '/health/ready', ca=str(trust))
raise ProbeFailure('unexpected_tls_acceptance', name)
except ProbeFailure as exc:
require(exc.classification == expected, 'incorrect_tls_failure_class', name + ':' + exc.classification)
observations[name] = exc.classification
return observations
def identity_acl(stand):
results = {}
for name, role, expected in [('lb', 'lb', 'http_response'), ('absent', None, 'tls_certificate_rejected'),
('wrong_uri', 'worker', 'tls_certificate_rejected')]:
result = stand.probe('tls', roles=('lb', 'worker', 'operator'),
config={'host': 'api-replica', 'port': 8080, 'role': role})
require(result['classification'] == expected, 'api_identity_mismatch', name + ':' + result['classification'])
if role == 'lb':
require(result['status'] == 200, 'api_lb_health_failed')
results[name] = result
with temporary_directory(prefix='lab7-untrusted-client-') as temp:
key, cert = Path(temp) / 'tls.key', Path(temp) / 'tls.crt'
command(['openssl', 'req', '-x509', '-newkey', 'rsa:2048', '-nodes', '-keyout', str(key),
'-out', str(cert), '-days', '1', '-subj', '/CN=lb'], timeout=30)
current = Path(temp) / 'current'
current.mkdir()
key.rename(current / 'tls.key')
cert.rename(current / 'tls.crt')
(current / 'ca.crt').write_bytes((stand.secrets / 'pki/operator/current/ca.crt').read_bytes())
for name, host, port, probe in (
('api', 'api-replica', 8080, 'tls'),
('collector', 'otel-collector', 4318, 'tls'),
('broker', 'broker', 5671, 'tls-channel')):
result = stand.probe(probe, roles=('operator',), extra_mounts=((temp, '/pki/untrusted'),),
config={'host': host, 'port': port, 'role': 'untrusted'})
require(result['classification'] == 'tls_certificate_rejected', name + '_wrong_ca_not_rejected')
results[name + '_wrong_ca_client'] = result
for role, expected in [('api', 200), ('lb', 403)]:
response = stand.probe('tls', roles=('api', 'lb'), config={'host': 'otel-collector', 'port': 4318,
'role': role, 'method': 'POST', 'path': '/v1/traces', 'body': '{}',
'headers': {'Content-Type': 'application/json'}})
require(response.get('status') == expected, 'collector_identity_acl_mismatch', role)
results['collector_' + role] = response
absent = stand.probe('tls', roles=('operator',), config={'host': 'otel-collector', 'port': 4318})
require(absent['classification'] == 'tls_certificate_rejected', 'collector_missing_cert_not_rejected')
results['collector_absent'] = absent
for host, port, role, method, path in [('otel-collector', 4318, 'api', 'GET', '/v1/traces'),
('prometheus', 9090, 'grafana', 'POST', '/-/reload'),
('loki', 3100, 'grafana', 'POST', '/loki/api/v1/push'),
('loki', 3100, 'collector', 'GET', '/loki/api/v1/query'),
('tempo', 3200, 'prometheus', 'GET', '/api/search')]:
result = stand.probe('tls', roles=(role,), config={'host': host, 'port': port, 'role': role, 'method': method, 'path': path}, network='observability')
require(result.get('status') == 403, 'telemetry_route_acl_mismatch', host + ':' + role)
results[host + '_' + role + '_route'] = result
return results
def broker_acl(stand):
results = {}
for role, operation, allowed in [('worker', 'consume', True), ('relay', 'publish', True),
('worker', 'publish', False), ('worker', 'topology', False),
('relay', 'consume', False), ('relay', 'topology', False)]:
result = stand.probe('amqp', roles=('worker', 'relay'), config={'role': role, 'operation': operation})
require(result['classification'] == ('amqp_allowed' if allowed else 'amqp_rejected'),
'broker_acl_mismatch', role + ':' + operation + ':' + result['classification'])
if not allowed:
require(result.get('code') == 403, 'broker_denial_not_access_refused')
results[role + '_' + operation] = result
absent = stand.probe('amqp', roles=('operator',), config={'role': None})
require(absent['classification'] == 'tls_certificate_rejected', 'broker_missing_cert_not_rejected')
wrong = stand.probe('amqp', roles=('api',), config={'role': 'api'})
require(wrong['classification'] == 'amqp_rejected' and wrong.get('code') == 403, 'broker_wrong_dns_principal_not_403')
offered = stand.probe('amqp', roles=('worker',), config={'role': 'worker', 'operation': 'mechanisms'})
require(offered['classification'] == 'amqp_mechanisms' and offered.get('mechanisms') == ['EXTERNAL'],
'broker_password_mechanism_negotiable')
results.update(absent=absent, wrong_dns=wrong, offered_mechanisms=offered)
return results
def seaweed_auxiliary_acl(stand):
results = {}
for role in (None, 'worker', 'api', 'operator', 's3'):
result = stand.probe('tls-channel', roles=('operator', 'worker', 'api', 's3'),
config={'host': 'seaweedfs', 'port': 18333, 'role': role})
expected = 'tls_channel_established' if role == 's3' else 'tls_certificate_rejected'
require(result['classification'] == expected, 'seaweed_auxiliary_mtls_mismatch', role or 'absent')
results[role or 'absent'] = result
return {'grpc_18333': results, 'limitations': ['Checks mutual-TLS HTTP/2 channel admission, not a Seaweed administrative gRPC method.']}
def database_acl(stand):
results = {}
for role, allowed in [('api', ('users', 'sessions', 'messages', 'attachments')),
('worker', ('attachments', 'attachment_jobs', 'outbox_messages')),
('relay', ('attachment_jobs', 'outbox_messages'))]:
for table in allowed:
stand.psql('BEGIN; SELECT 1 FROM ' + table + ' LIMIT 0; ROLLBACK;', role=role)
results[role + '_read_' + table] = 'allowed'
denied = ('users', 'sessions', 'messages') if role != 'api' else ()
for table in denied:
column = 'token_hash' if table == 'sessions' else 'id'
statements = {'select': 'SELECT * FROM ' + table + ' LIMIT 0',
'insert': 'INSERT INTO ' + table + ' DEFAULT VALUES',
'update': 'UPDATE ' + table + ' SET ' + column + '=' + column + ' WHERE false',
'delete': 'DELETE FROM ' + table + ' WHERE false'}
for method, sql in statements.items():
result = stand.psql('BEGIN; ' + sql + '; ROLLBACK;', role=role, check=False)
require(result.returncode != 0 and '42501' in result.stderr, 'database_denial_not_42501', role + ':' + table + ':' + method)
results[role + '_denied_' + table + '_' + method] = '42501'
result = stand.psql('BEGIN; CREATE TABLE public.lab7_denied_' + uuid.uuid4().hex + '(n int); ROLLBACK;', role=role, check=False)
require(result.returncode != 0 and '42501' in result.stderr, 'database_ddl_not_42501', role)
results[role + '_ddl'] = '42501'
return results
def http_security(stand, client, actors):
alice, bob, eve, chat = actors
path = '/api/v1/chats/' + chat['id'] + '/messages'
client.call('GET', path, 401, headers={'X-User-Id': alice['id']})
client.call('GET', path, 404, actor=eve, headers={'X-User-Id': alice['id']})
client.call('POST', path, 404, actor=eve, body={'text': 'denied'},
headers={'Idempotency-Key': uuid.uuid4().hex, 'X-User-Id': alice['id']})
disposable = client.user('logout')
client.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=disposable)
client.call('GET', path, 401, actor=disposable)
expired = client.user('expiry')
# Actual persisted session expiration, not a forged token or a schema check.
raw_token = base64.urlsafe_b64decode(expired['token'] + '=' * (-len(expired['token']) % 4))
digest = hashlib.sha256(raw_token).hexdigest()
stand.psql("UPDATE sessions SET expires_at=now()-interval '1 second' WHERE token_hash=decode('" + digest + "','hex');")
client.call('GET', '/api/v1/chats', 401, actor=expired)
upload = '/api/v1/chats/' + chat['id'] + '/attachments?filename=security.png'
client.call('POST', upload, 413, actor=alice, raw=b'x' * (10 * 1024 * 1024 + 1), headers={'Content-Type': 'image/png'})
client.call('POST', upload, 415, actor=alice, raw=b'not an image', headers={'Content-Type': 'image/png'})
def chunk(kind, content):
return struct.pack('>I', len(content)) + kind + content + struct.pack('>I', zlib.crc32(kind + content) & 0xffffffff)
huge = b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('>IIBBBBB', 100000, 100000, 8, 6, 0, 0, 0)) + chunk(b'IDAT', zlib.compress(b'\x00')) + chunk(b'IEND', b'')
client.call('POST', upload, (413, 415), actor=alice, raw=huge, headers={'Content-Type': 'image/png'})
return {'x_user_id': 'no_impersonation', 'foreign_chat': 404, 'logout': 401, 'persisted_expiration': 401,
'oversize_body': 413, 'invalid_image': 415, 'image_pixel_bound': 'rejected'}
def rate_limit(client, actor):
credential = base64.b64encode((actor['user']['username'] + ':wrong-local-test-password').encode()).decode()
instances, statuses, retries = set(), [], []
for _ in range(12):
result = client.call('POST', '/api/v1/auth/sessions', (401, 429), headers={'Authorization': 'Basic ' + credential})
instances.add(result['headers'].get('X-Instance-Id'))
statuses.append(result['status'])
if result['status'] == 429:
value = result['headers'].get('Retry-After', '')
require(value.isdigit() and int(value) > 0, 'rate_limit_retry_after_missing')
retries.append(int(value))
require(429 in statuses, 'shared_rate_limit_not_enforced')
require(len(instances) >= 2, 'shared_rate_limit_not_observed_across_replicas')
return {'instances': sorted(instances), 'statuses': statuses, 'retry_after': retries}
def workflow(client, actors):
alice, bob, _, chat = actors
attachment, content = client.attachment(alice, chat)
ready = client.wait_ready(alice, attachment, wait_seconds=90)
message = client.message(alice, chat, 'lab7-pipeline-sentinel', attachments=[ready['id']])['json']
downloaded = client.call('GET', '/api/v1/attachments/' + ready['id'] + '/content', actor=bob)['bytes']
require(downloaded == content, 'workflow_attachment_data_changed')
return {'attachment_id': ready['id'], 'message_id': message['id'], 'status': ready['status']}
def isolation(stand):
containers = stand.containers()
require(bool(containers), 'stand_containers_missing')
services = {}
pairs = {'collector-tls': 'otel-collector', 'prometheus-tls': 'prometheus', 'loki-tls': 'loki',
'tempo-tls': 'tempo', 'grafana-tls': 'grafana', 'broker-admin-tls': 'broker', 's3': 'seaweedfs'}
by_service = {}
for item in containers:
name = item['Config']['Labels'].get('com.docker.compose.service')
by_service.setdefault(name, []).append(item)
host = item['HostConfig']
require(not host['Privileged'] and host['NetworkMode'] != 'host', 'privileged_or_host_network', name)
require(not any('docker.sock' in m['Source'] for m in item['Mounts']), 'docker_socket_mounted', name)
for bindings in (host.get('PortBindings') or {}).values():
for binding in bindings or []:
require(binding['HostIp'] in ('127.0.0.1', '::1'), 'nonloopback_publish', name)
if name in ('api', 'worker', 'relay'):
require(item['State']['Running'], 'application_not_running', name)
require(host['ReadonlyRootfs'], 'application_writable_root', name)
require('ALL' in [x.upper() for x in host.get('CapDrop') or []] and not host.get('CapAdd'), 'application_capability_not_dropped', name)
require(any(x.startswith('no-new-privileges') for x in host.get('SecurityOpt') or []), 'application_new_privileges', name)
require(host.get('Memory', 0) > 0 and (host.get('NanoCpus', 0) > 0 or host.get('CpuQuota', 0) > 0), 'application_resource_limits_missing', name)
require(not host.get('PortBindings'), 'application_published_ports', name)
require('/tmp' in (host.get('Tmpfs') or {}), 'application_tmpfs_missing', name)
top = command(['docker', 'top', item['Id'], '-eo', 'uid,pid,comm']).stdout.splitlines()[1:]
require(bool(top) and all(row.split()[0] != '0' for row in top if row.split()), 'application_root_process', name)
identity = [m for m in item['Mounts'] if m['Destination'] == '/run/identity']
require(len(identity) == 1 and not identity[0]['RW'] and Path(identity[0]['Source']).name == name,
'application_identity_mount_not_isolated', name)
require(not any('credentials.json' in m['Source'] or Path(m['Source']).name in ('pki', 'backends', 'lab7') for m in item['Mounts']), 'application_operator_secrets_mounted', name)
services.setdefault(name, []).append({'id': item['Id'][:12], 'running_uid_nonzero': True, 'readonly': True})
require(len(by_service.get('api', [])) == 2, 'acceptance_requires_two_api_replicas')
for child, owner in pairs.items():
children = by_service.get(child, [])
require(len(children) == 1 and len(by_service.get(owner, [])) == 1, 'namespace_pair_missing', child)
require(children[0]['HostConfig']['NetworkMode'] == 'container:' + by_service[owner][0]['Id'], 'namespace_pair_not_current_owner', child)
# IDs alone cannot prove kernel namespace pairing after an owner restart.
namespace_ids = []
for container in (children[0], by_service[owner][0]):
observed_ns = command(['docker', 'run', '--rm', '--network', 'container:' + container['Id'],
'--user', '10001:10001', '--cap-drop=ALL',
'--security-opt=no-new-privileges', 'alpine:3.22',
'readlink', '/proc/self/ns/net'])
namespace_ids.append(observed_ns.stdout.strip())
require(namespace_ids[0] == namespace_ids[1] and namespace_ids[0].startswith('net:['),
'namespace_pair_kernel_mismatch', child)
target_groups = {
'default': [('otel-collector', 14318), ('otel-collector', 18888), ('otel-collector', 18889),
('otel-collector', 13133), ('otel-collector', 4317),
('broker', 5672), ('broker', 15672), ('broker', 15692),
('seaweedfs', 9333), ('seaweedfs', 19333), ('seaweedfs', 8888), ('seaweedfs', 18888),
('seaweedfs', 8080), ('seaweedfs', 18080), ('seaweedfs', 8181), ('seaweedfs', 9101),
('api-replica', 8084), ('worker', 8081), ('relay', 8082)],
'observability': [('loki', 13100), ('loki', 9096), ('loki', 7946),
('tempo', 13200), ('tempo', 14317), ('tempo', 9095), ('tempo', 7946),
('prometheus', 19090), ('grafana', 13000)],
}
private_ports = {}
for selector, targets in target_groups.items():
observed = stand.probe('tcp', config={'targets': targets}, network=selector)
require(all(v == 'connection_refused' for v in observed['targets'].values()),
'private_listener_network_reachable_or_timeout')
private_ports.update(observed['targets'])
return {'applications': services, 'namespace_pairs': pairs, 'private_ports': private_ports}
def plaintext_audit(stand):
checks = {}
for name, kind, config in (
('postgres', 'postgres-plaintext', {'host': 'postgres', 'port': 5432}),
('redis', 'redis-plaintext', {'host': 'redis', 'port': 6379}),
('s3', 's3-plaintext', {'host': 'seaweedfs', 'port': 8333})):
result = stand.probe(kind, config=config)
require(result.get('classification') == 'rejected', 'plaintext_channel_accepted', name)
checks[name] = result
redis_port = stand.probe('redis', roles=('operator',), credentials=True,
config={'role': 'operator', 'user': 'operator', 'check_port': True})
checks['redis_tls_port'] = redis_port
foreign = Path.home() / '.local/share/mtusi-lab7' / stand.project / 'root-foreign' / 'ca.crt'
require(foreign.is_file(), 'foreign_ca_missing')
result = stand.probe('tls', roles=('operator',),
extra_mounts=((foreign, '/pki/foreign-ca.crt'),),
config={'host': 'seaweedfs', 'port': 8333, 'role': 'operator',
'ca': '/pki/foreign-ca.crt'})
require(result.get('classification') == 'ca_rejected', 's3_foreign_ca_accepted')
checks['s3_foreign_ca'] = result
return checks
def leakage(stand, actors):
sensitive = []
for actor in actors[:3]:
sensitive += [actor['token'], actor['registration']['password'],
base64.b64encode((actor['registration']['username'] + ':' + actor['registration']['password']).encode()).decode()]
def collect(value):
if isinstance(value, dict):
for key, item in value.items():
if key in ('password', 'secret_key', 'access_key') and isinstance(item, str) and len(item) >= 8:
sensitive.append(item)
else:
collect(item)
elif isinstance(value, list):
for item in value:
collect(item)
collect(json.loads((stand.secrets / 'backends/credentials.json').read_text()))
checked = []
for item in stand.containers():
output = command(['docker', 'logs', '--since', '30m', item['Id']], check=False, timeout=30)
text = output.stdout + output.stderr
require(not any(value in text for value in sensitive), 'credential_leak_in_container_logs', item['Config']['Labels'].get('com.docker.compose.service'))
checked.append(item['Id'][:12])
with temporary_directory(prefix='lab7-secret-scan-') as directory:
secret_file = Path(directory) / 'secrets.json'
secret_file.write_text(json.dumps(list(dict.fromkeys(sensitive))))
secret_file.chmod(0o600)
result = stand.probe('loki-scan', roles=('grafana',),
extra_mounts=((secret_file, '/secret-scan.json'),),
config={'selector': '{service_name=~".+"}'}, network='observability')
require(result.get('classification') == 'passed', 'credential_leak_in_loki')
loki = [result]
return {'containers_scanned': checked, 'sensitive_values_scanned': len(sensitive),
'loki_scans': len(loki),
'limitations': ['Scans retained container logs and Loki streams with service_name; Tempo attributes and historic deleted logs are not text-scanned.']}
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--base-url', default='https://localhost:8443')
parser.add_argument('--ca-file', required=True)
parser.add_argument('--project', default=os.environ.get('COMPOSE_PROJECT_NAME', 'mtusi-messenger-lab6'))
parser.add_argument('--secrets-dir', default=os.environ.get('LAB7_SECRETS_DIR', 'secrets/lab7'))
parser.add_argument('--output', type=Path)
args = parser.parse_args()
stand = Stand(args.project, args.secrets_dir, args.base_url)
client = Client(args.base_url, args.ca_file, timeout=5)
suite = Suite()
actors = None
suite.run('public_ca_hostname', lambda: public_tls(stand, client, Path(args.ca_file)))
suite.run('mtls_and_telemetry_acl', lambda: identity_acl(stand))
suite.run('broker_external_acl', lambda: broker_acl(stand))
suite.run('bao_api_policy', lambda: stand.probe('bao', roles=('api',), config={'role': 'api'}))
suite.run('seaweed_auxiliary_mtls', lambda: seaweed_auxiliary_acl(stand))
suite.run('redis_tls_cache_acl', lambda: stand.probe('redis', roles=('api',), credentials=True, config={'role': 'api'}))
suite.run('database_privileges', lambda: database_acl(stand))
suite.run('s3_prefix_acl', lambda: stand.probe('s3', roles=('operator',), credentials=True))
suite.run('plaintext_refusal', lambda: plaintext_audit(stand))
try:
actors = client.bootstrap()
except Exception:
suite.results.append({'name': 'public_actors', 'passed': False, 'classification': 'actor_bootstrap_failed'})
if actors:
suite.run('http_acl_logout_expiry_bounds', lambda: http_security(stand, client, actors))
suite.run('real_async_pipeline', lambda: workflow(client, actors))
suite.run('shared_rate_limit', lambda: rate_limit(client, actors[2]))
suite.run('credential_log_sentinels', lambda: leakage(stand, actors))
suite.run('runtime_container_network_isolation', lambda: isolation(stand))
result = {'study': 'lab7-security', 'project': stand.project, 'timestamp': dt.datetime.now(dt.timezone.utc).isoformat(),
'passed': all(x['passed'] for x in suite.results), 'results': suite.results,
'limitations': ['Authorized local synthetic stand only; privilege tests use administrator SET ROLE over a local Unix socket.',
'Actual workflow confirms queue processing; standalone consume probe requeues unacknowledged deliveries.',
'Session expiry uses an operator expiry update, not a claim about the default configured TTL.']}
encoded = json.dumps(result, indent=2, sort_keys=True)
if args.output:
args.output.parent.mkdir(parents=True, exist_ok=True)
args.output.write_text(encoded + '\n')
print(encoded)
return 0 if result['passed'] else 1
if __name__ == '__main__':
try:
raise SystemExit(main())
except ProbeFailure as exc:
print(json.dumps({'passed': False, 'classification': exc.classification, 'detail': exc.detail}))
raise SystemExit(1)
except Exception as exc:
print(json.dumps({'passed': False, 'classification': 'suite_exception', 'type': type(exc).__name__}))
raise SystemExit(1)