forked from mtusi2026/mtusi2026lab7
lab7: безопасность, mTLS, OpenBao и PKI lifecycle
This commit is contained in:
@@ -3,49 +3,57 @@
|
||||
Собственные тесты лежат в student-tests/ (CONTRIBUTING.md). X-Instance-Id равен
|
||||
hostname контейнера, поэтому по нему можно найти реально обслуживший контейнер.
|
||||
"""
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'scripts'))
|
||||
|
||||
from lab7_environment import Environment
|
||||
from securityctl import GATEWAY_PAIRS
|
||||
|
||||
|
||||
def compose(*args, check=True, env=None):
|
||||
command = [*os.environ.get('COMPOSE_BIN', 'docker compose').split(), '--env-file', '.env', '-f', 'compose.yaml', *args]
|
||||
environment = {**os.environ, **(env or {})}
|
||||
|
||||
return subprocess.run(command, cwd=ROOT, capture_output=True, text=True, check=check, env=environment)
|
||||
environment = Environment()
|
||||
environment.process_env.update(env or {})
|
||||
owners = [value for value in args[1:] if value in GATEWAY_PAIRS] if args else []
|
||||
children = [child for owner in owners for child in GATEWAY_PAIRS[owner]]
|
||||
operation = args[0] if args else ''
|
||||
if operation == 'stop':
|
||||
return environment.compose(*args, *(child for child in children if child not in args), check=check)
|
||||
result = environment.compose(*args, check=check)
|
||||
if result.returncode == 0 and operation in ('up', 'start', 'restart') and children:
|
||||
environment.compose('up', '-d', '--no-build', '--force-recreate', '--no-deps', *children)
|
||||
return result
|
||||
|
||||
|
||||
def jobctl(*args, check=True):
|
||||
"""Run the operator CLI inside the worker image."""
|
||||
return compose('run', '--rm', '--entrypoint', '/usr/local/bin/jobctl', 'worker', *args, check=check)
|
||||
"""Run the dedicated operator CLI, never the worker's application credentials."""
|
||||
return operator_command('jobctl', *args, check=check)
|
||||
|
||||
|
||||
def psql(sql):
|
||||
"""Read a scalar/list from the stand database (operator evidence only)."""
|
||||
project = os.environ.get('COMPOSE_PROJECT_NAME', 'mtusi-messenger-lab6')
|
||||
result = subprocess.run(
|
||||
['docker', 'exec', project + '-postgres-1', 'psql', '-U', 'messenger', '-d', 'messenger', '-Atc', sql],
|
||||
cwd=ROOT, capture_output=True, text=True, check=True,
|
||||
"""Read a scalar/list through the dedicated TLS PostgreSQL operator."""
|
||||
return operator_command('psql', data=sql).stdout.strip()
|
||||
|
||||
|
||||
def operator_command(command, *args, data=None, check=True):
|
||||
environment = Environment()
|
||||
return environment.run(
|
||||
[sys.executable, str(ROOT / 'scripts/securityctl.py'), command, *map(str, args)],
|
||||
data=data, check=check,
|
||||
)
|
||||
|
||||
return result.stdout.strip()
|
||||
|
||||
def redis(*args):
|
||||
"""Run authenticated Redis TLS operator probes without exposing credentials."""
|
||||
return operator_command('redis', *args).stdout.strip()
|
||||
|
||||
|
||||
def containers_by_hostname(service='api'):
|
||||
"""{hostname: {id, name, status}} для контейнеров сервиса."""
|
||||
project = os.environ.get('COMPOSE_PROJECT_NAME', 'mtusi-messenger-lab6')
|
||||
listed = subprocess.run(
|
||||
[
|
||||
'docker', 'ps', '-q',
|
||||
'--filter', 'label=com.docker.compose.project=' + project,
|
||||
'--filter', 'label=com.docker.compose.service=' + service,
|
||||
],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True,
|
||||
).stdout.split()
|
||||
listed = compose('ps', '-q', service).stdout.split()
|
||||
|
||||
result = {}
|
||||
for container in listed:
|
||||
@@ -63,3 +71,19 @@ def containers_by_hostname(service='api'):
|
||||
|
||||
def messages_path(chat):
|
||||
return '/api/v1/chats/' + chat['id'] + '/messages'
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) < 2:
|
||||
raise SystemExit('expected compose, psql, redis or jobctl command')
|
||||
command, *args = sys.argv[1:]
|
||||
if command == 'compose':
|
||||
result = compose(*args, check=False)
|
||||
elif command in ('psql', 'redis', 'jobctl'):
|
||||
result = operator_command(command, *args, data=sys.stdin.read() if command == 'psql' else None,
|
||||
check=False)
|
||||
else:
|
||||
raise SystemExit('unsupported operator helper command')
|
||||
sys.stdout.write(result.stdout)
|
||||
sys.stderr.write(result.stderr)
|
||||
raise SystemExit(result.returncode)
|
||||
|
||||
Reference in New Issue
Block a user