forked from mtusi2026/mtusi2026lab3
37 lines
2.1 KiB
Python
37 lines
2.1 KiB
Python
"""Проверка русских событий авторизации и отсутствия секретов в логах Compose."""
|
|
import subprocess
|
|
import base64
|
|
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
|
|
from client import Client, arguments, check, run
|
|
|
|
|
|
def main():
|
|
args = arguments('Проверка логов авторизации')
|
|
started = datetime.now(timezone.utc).isoformat().replace('+00:00', 'Z')
|
|
client = Client(args.base_url, args.ca_file)
|
|
actor = client.user('auth_logs')
|
|
username = actor['registration']['username']
|
|
wrong_password = 'wrong-password-for-logging'
|
|
client.call('POST', '/api/v1/auth/sessions', 401, headers={'Authorization': 'Basic ' + base64.b64encode((username + ':' + wrong_password).encode()).decode()})
|
|
missing_username = 'missing_' + actor['id'].replace('-', '')[:12]
|
|
client.call('POST', '/api/v1/auth/sessions', 401, headers={'Authorization': 'Basic ' + base64.b64encode((missing_username + ':' + wrong_password).encode()).decode()})
|
|
client.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=actor)
|
|
client.call('GET', '/api/v1/users/me', 401, actor=actor)
|
|
result = subprocess.run(
|
|
['docker', 'compose', 'logs', '--no-color', '--since', started, 'api'],
|
|
cwd=Path(__file__).resolve().parents[1], check=True, capture_output=True, text=True,
|
|
)
|
|
logs = result.stdout + result.stderr
|
|
check('src.service.auth' in logs, 'Не используется обычный логгер модуля')
|
|
for message in ('Сессия создана', 'Вход отклонён', 'Сессия завершена', 'Доступ отклонён'):
|
|
check(message in logs, 'В логах отсутствует событие авторизации')
|
|
for secret in (actor['token'], actor['registration']['password'], wrong_password):
|
|
check(secret not in logs, 'В логах обнаружены credentials')
|
|
print('PASS: русские события входа/отказа/logout; пароли и токены не попали в логи')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run(main)
|