forked from mtusi2026/mtusi2026lab7
lab7: исправления по независимому ревью
This commit is contained in:
+25
-2
@@ -5,6 +5,7 @@ from __future__ import annotations
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import secrets
|
||||
import ssl
|
||||
import time
|
||||
@@ -156,7 +157,7 @@ subjectAltName={san}
|
||||
restarted_listener = False
|
||||
for role in ('operator', 'openbao'):
|
||||
certificate = self.env.identity(role) / 'tls.crt'
|
||||
valid = self.env.run(['openssl', 'x509', '-checkend', '30', '-noout',
|
||||
valid = self.env.run(['openssl', 'x509', '-checkend', '300', '-noout',
|
||||
'-in', str(certificate)], check=False).returncode == 0
|
||||
if valid:
|
||||
continue
|
||||
@@ -331,6 +332,19 @@ authorityKeyIdentifier=keyid,issuer
|
||||
next_renewal = time.time() + max(1, (expiry - time.time()) / 2)
|
||||
time.sleep(interval)
|
||||
|
||||
def crl_contains_serial(self, bundle: str, normalized: str) -> bool:
|
||||
wanted = normalized.lstrip('0').lower()
|
||||
for block in re.findall(r'-----BEGIN X509 CRL-----.*?-----END X509 CRL-----', bundle, re.S):
|
||||
output = self.env.run(['openssl', 'crl', '-noout', '-text'], data=block,
|
||||
check=False).stdout
|
||||
for line in output.splitlines():
|
||||
if 'Serial Number:' not in line:
|
||||
continue
|
||||
value = line.split(':', 1)[1].strip().replace(':', '').lower().lstrip('0')
|
||||
if value == wanted:
|
||||
return True
|
||||
return False
|
||||
|
||||
def revoke(self, role: str, serial: str) -> dict:
|
||||
normalized = serial.replace(':', '').replace('-', '').lower()
|
||||
if not normalized or any(ch not in '0123456789abcdef' for ch in normalized):
|
||||
@@ -344,7 +358,10 @@ authorityKeyIdentifier=keyid,issuer
|
||||
# exposes rotation as GET /pki/crl/rotate (POST is 405 here).
|
||||
client.request('GET', 'pki/crl/rotate', token=token)
|
||||
self.sync_crls(client, token)
|
||||
return {'role': role, 'revoked_serial': normalized, 'crls_updated': True, 'crl_rotated': True}
|
||||
if not self.crl_contains_serial(self.cached_crls(), normalized):
|
||||
raise OperationError('published CRL does not contain the revoked serial')
|
||||
return {'role': role, 'revoked_serial': normalized, 'crls_updated': True,
|
||||
'crl_rotated': True, 'crl_contains_serial': True}
|
||||
|
||||
def prepare_rollover(self) -> dict:
|
||||
if self.state.get('next_root'):
|
||||
@@ -405,6 +422,12 @@ authorityKeyIdentifier=keyid,issuer
|
||||
continue
|
||||
if self.certificate_name(current / 'tls.crt', 'issuer') not in active_issuers:
|
||||
raise OperationError('retire blocked: leaf is not from the active issuer: ' + role)
|
||||
verified = self.env.run(['openssl', 'verify',
|
||||
'-CAfile', str(root_dir / 'ca.crt'),
|
||||
'-untrusted', str(root_dir / 'intermediate.crt'),
|
||||
str(current / 'tls.crt')], check=False)
|
||||
if verified.returncode:
|
||||
raise OperationError('retire blocked: leaf chain does not verify: ' + role)
|
||||
client = BaoClient(self.env)
|
||||
token = client.login()
|
||||
self.state['trust_roots'] = [self.state['active_root']]
|
||||
|
||||
+16
-5
@@ -234,7 +234,7 @@ user operator on >{operator_password} ~* &* +@all
|
||||
|
||||
def unseal(self) -> dict:
|
||||
deadline = time.monotonic() + 60
|
||||
client = BaoClient(self.env, None)
|
||||
client = BaoClient(self.env)
|
||||
while True:
|
||||
try:
|
||||
status = client.request('GET', 'sys/seal-status')
|
||||
@@ -311,7 +311,7 @@ user operator on >{operator_password} ~* &* +@all
|
||||
root_token = material.get('root_token')
|
||||
if root_token:
|
||||
self.env.redactions.add(root_token)
|
||||
client = BaoClient(self.env, None)
|
||||
client = BaoClient(self.env)
|
||||
token = root_token
|
||||
else:
|
||||
client = BaoClient(self.env)
|
||||
@@ -437,12 +437,20 @@ END $$;
|
||||
certificate = self.env.identity(role) / 'tls.crt'
|
||||
if not certificate.exists():
|
||||
continue
|
||||
valid = self.env.run(['openssl', 'x509', '-checkend', '120', '-noout',
|
||||
valid = self.env.run(['openssl', 'x509', '-checkend', '300', '-noout',
|
||||
'-in', str(certificate)], check=False).returncode == 0
|
||||
if not valid:
|
||||
self.pki.issue(role)
|
||||
renewed.append(role)
|
||||
return {'renewed': renewed}
|
||||
reloaded, skipped = [], []
|
||||
for role in renewed:
|
||||
try:
|
||||
self.reload_identity(role)
|
||||
reloaded.append(role)
|
||||
except OperationError:
|
||||
# A stopped service will load the reissued leaf when compose starts it.
|
||||
skipped.append(role)
|
||||
return {'renewed': renewed, 'reloaded': reloaded, 'reload_skipped': skipped}
|
||||
|
||||
def bootstrap(self) -> dict:
|
||||
self.prepare()
|
||||
@@ -470,6 +478,7 @@ END $$;
|
||||
self.retire_broker_users()
|
||||
return {'bootstrapped': True, 'project': self.env.project,
|
||||
'expired_identities_renewed': recovered.get('renewed', []),
|
||||
'expired_identities_reloaded': recovered.get('reloaded', []),
|
||||
'root_token_revoked': self.env.read_json(self.init_path).get('root_token_revoked', False)}
|
||||
|
||||
def wait_stores(self) -> None:
|
||||
@@ -728,7 +737,9 @@ END $$;
|
||||
gateways = ['lb', *[child for children in GATEWAY_PAIRS.values()
|
||||
for child in children if child != 's3']]
|
||||
self.env.compose('restart', *gateways, timeout=180)
|
||||
return {**result, 'native_gateway_crls_reloaded': True, 'old_connections_drained': True}
|
||||
return {**result, 'native_gateway_crls_reloaded': True,
|
||||
'native_gateway_restarts': True,
|
||||
'existing_application_pools_drained': False}
|
||||
|
||||
def recover_identity(self, role: str) -> dict:
|
||||
transport_replaced = self.pki.recover_transport()
|
||||
|
||||
Reference in New Issue
Block a user