forked from mtusi2026/mtusi2026lab7
74 lines
2.5 KiB
Python
74 lines
2.5 KiB
Python
"""Verify the worker identity against PostgreSQL, S3 and the broker."""
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
CHECK = """
|
|
import json
|
|
import os
|
|
import uuid
|
|
|
|
import boto3
|
|
from botocore.config import Config
|
|
from botocore.exceptions import ClientError
|
|
import psycopg
|
|
from psycopg.errors import InsufficientPrivilege
|
|
import redis
|
|
|
|
with psycopg.connect(os.environ['DATABASE_URL'], autocommit=True) as db:
|
|
db.execute('SELECT id FROM attachments LIMIT 0')
|
|
assert db.execute('SELECT ssl FROM pg_stat_ssl WHERE pid = pg_backend_pid()').fetchone()[0]
|
|
try:
|
|
db.execute('SELECT id FROM users LIMIT 0')
|
|
except InsufficientPrivilege:
|
|
pass
|
|
else:
|
|
raise AssertionError('Worker can read the users table')
|
|
|
|
s3 = boto3.client('s3', endpoint_url=os.environ['S3_ENDPOINT'],
|
|
aws_access_key_id=os.environ['S3_ACCESS_KEY'],
|
|
aws_secret_access_key=os.environ['S3_SECRET_KEY'],
|
|
verify='/ca/ca.crt', config=Config(s3={'addressing_style': 'path'}))
|
|
bucket = os.environ['S3_BUCKET']
|
|
key = 'original/permission-test-' + uuid.uuid4().hex
|
|
s3.list_objects_v2(Bucket=bucket, Prefix='staging/', MaxKeys=1)
|
|
try:
|
|
s3.head_object(Bucket=bucket, Key=key)
|
|
except ClientError as error:
|
|
assert error.response['ResponseMetadata']['HTTPStatusCode'] == 403
|
|
else:
|
|
raise AssertionError('Worker can read from the original prefix')
|
|
|
|
broker = redis.Redis.from_url(os.environ['BROKER_URL'], ssl_ca_certs='/ca/ca.crt',
|
|
ssl_certfile='/identity/tls.crt', ssl_keyfile='/identity/tls.key',
|
|
ssl_check_hostname=True, socket_timeout=5)
|
|
assert broker.ping()
|
|
try:
|
|
broker.xlen('attachment:jobs')
|
|
except redis.exceptions.NoPermissionError:
|
|
pass
|
|
else:
|
|
raise AssertionError('Worker was granted a broker command outside its role')
|
|
try:
|
|
broker.xreadgroup('workers', 'permission-check', {'permission-test-' + uuid.uuid4().hex: '>'})
|
|
except redis.exceptions.NoPermissionError:
|
|
pass
|
|
else:
|
|
raise AssertionError('Worker can access an unrelated stream')
|
|
|
|
print(json.dumps({'postgres_tls_and_table_acl': True, 's3_tls_and_prefix_acl': True,
|
|
'broker_mtls_and_command_acl': True}))
|
|
"""
|
|
|
|
output = subprocess.check_output(
|
|
['docker', 'compose', 'exec', '-T', 'worker', 'python', 'secret_entry.py', 'python', '-c', CHECK],
|
|
cwd=ROOT, text=True,
|
|
)
|
|
results = json.loads(output)
|
|
(ROOT / 'evidence/worker-permissions.json').write_text(
|
|
json.dumps(results, indent=2) + '\n', encoding='utf-8',
|
|
)
|
|
print('PASS: worker TLS and scopes in PostgreSQL, S3 and the broker')
|