lab7: безопасность, mTLS, OpenBao и PKI lifecycle

This commit is contained in:
Vladislav Kunetskiy
2026-11-10 10:58:07 +03:00
parent 3d216fbd50
commit 3441f2269a
183 changed files with 216667 additions and 3165 deletions
+12 -10
View File
@@ -5,7 +5,7 @@ The mandatory proxy endpoint stays available; this script exercises the
optional extension: presign -> direct PUT to S3 -> complete -> message.
Run against a live stand:
python3 student-tests/presign_flow.py --base-url http://localhost:8080
python3 student-tests/presign_flow.py --base-url https://localhost:8443
Set PRESIGN_EXPIRY_WAIT=seconds to also verify that an expired URL is rejected
(the stand must run with a matching S3_PRESIGN_TTL).
@@ -21,8 +21,10 @@ import uuid
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
from client import Client, Failure, arguments, check, run # noqa: E402
from lab7_http import BASE_URL, CA_FILE, open_url # noqa: E402
FIXTURE = Path(__file__).resolve().parents[1] / 'tests/fixtures/sample.png'
@@ -41,12 +43,12 @@ def presign(client, actor, chat, content, filename='presigned.png', content_type
return json.loads(response['bytes'])
def put(url, content, content_type='image/png'):
def put(url, content, content_type='image/png', ca_file=CA_FILE):
request = urllib.request.Request(
url, data=content, method='PUT', headers={'Content-Type': content_type},
)
try:
with urllib.request.urlopen(request, timeout=15) as response:
with open_url(request, timeout=15, ca_file=ca_file) as response:
return response.status
except urllib.error.HTTPError as error:
return error.code
@@ -61,7 +63,7 @@ def complete(client, actor, attachment_id, digest, expected=202):
def main():
args = arguments('Presigned upload flow (lab4 track 1)')
client = Client(args.base_url, args.ca_file)
client = Client(args.base_url or BASE_URL, args.ca_file or CA_FILE)
alice, bob, eve, chat = client.bootstrap()
content = FIXTURE.read_bytes()
digest = hashlib.sha256(content).hexdigest()
@@ -70,7 +72,7 @@ def main():
presigned = presign(client, alice, chat, content)
check(presigned['method'] == 'PUT', 'presign method must be PUT')
check('content-length' in presigned['upload_url'], 'content-length must be signed into the URL')
check(put(presigned['upload_url'], content) == 200, 'direct PUT failed')
check(put(presigned['upload_url'], content, ca_file=args.ca_file or CA_FILE) == 200, 'direct PUT failed')
attachment = complete(client, alice, presigned['attachment_id'], digest)['json']
check(attachment['sha256'] == digest and attachment['size_bytes'] == len(content), 'completed metadata mismatch')
attachment = client.wait_ready(alice, attachment)
@@ -99,14 +101,14 @@ def main():
# Wrong checksum: the object is rejected and removed.
mismatch = presign(client, alice, chat, content)
check(put(mismatch['upload_url'], content) == 200, 'direct PUT failed')
check(put(mismatch['upload_url'], content, ca_file=args.ca_file or CA_FILE) == 200, 'direct PUT failed')
complete(client, alice, mismatch['attachment_id'], '0' * 64, expected=409)
print('wrong completion checksum: 409')
# Content-Length is signed, so S3 rejects a different number of bytes at
# write time instead of storing an oversized staging object.
oversized = presign(client, alice, chat, content)
check(put(oversized['upload_url'], content + b'x') == 403, 'oversized PUT must be rejected by S3')
check(put(oversized['upload_url'], content + b'x', ca_file=args.ca_file or CA_FILE) == 403, 'oversized PUT must be rejected by S3')
complete(client, alice, oversized['attachment_id'], digest, expected=409)
print('oversized direct PUT: 403 from S3; complete: 409 (nothing stored)')
@@ -115,8 +117,8 @@ def main():
overwrite = presign(client, alice, chat, content)
corrupt = bytearray(content)
corrupt[0] ^= 0xFF
check(put(overwrite['upload_url'], bytes(corrupt)) == 200, 'first PUT failed')
check(put(overwrite['upload_url'], content) == 200, 'second PUT failed')
check(put(overwrite['upload_url'], bytes(corrupt), ca_file=args.ca_file or CA_FILE) == 200, 'first PUT failed')
check(put(overwrite['upload_url'], content, ca_file=args.ca_file or CA_FILE) == 200, 'second PUT failed')
overwritten = complete(client, alice, overwrite['attachment_id'], digest)['json']
check(overwritten['sha256'] == digest, 'last PUT was not the completed object')
print('same-size overwrite: last PUT wins, checksum verified')
@@ -139,7 +141,7 @@ def main():
if wait > 0:
expired = presign(client, alice, chat, content)
time.sleep(wait)
status = put(expired['upload_url'], content)
status = put(expired['upload_url'], content, ca_file=args.ca_file or CA_FILE)
check(status == 403, 'expired presigned URL must be 403, got ' + str(status))
print('EXPIRED: presigned URL rejected after', wait, 'seconds')