litellm/tests/integration/providers/test_s3_wire.py
Nate Armstrong 8e92894ff2
feat(s3-log): replace the python s3_v2 logger with a rust callback (#51539)
The s3_v2 S3Logger is ported to a Rust callback, callbacks-s3-log, behind
the CustomLoggerV2 contract, with the bridge constructor NativeS3Log and the
Python glue in litellm/integrations/s3_v3.py. It is now the only
implementation: the Python logger is deleted, s3_v2.py re-exports the
Rust-backed S3Logger, and LITELLM_NATIVE_S3_V2 is removed. The callback keeps
its name, list position and settings, so no config changes.

The original s3_v2 tests are replicated against the Rust callback as
<original>_rust.py, and Python's outputs for the differential suite are
recorded in tests/unit/integrations/s3_v2_golden/python_s3_v2.json.
2026-11-14 23:03:58 +00:00

120 lines
7.6 KiB
Python

import asyncio
import base64
import hashlib
import hmac
import json
from datetime import datetime
from typing import Final
import httpx
import pytest
from integration._support.sigv4 import encoded_path, signature
from integration._support.wire import Reply, Request, wire_server
ACCESS: Final = "AKIAIOSFODNN7EXAMPLE"
SECRET: Final = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
@pytest.mark.covers("other.provider_wire.s3.verifier_known_answer_and_negative_controls")
def test_sigv4_verifier_matches_published_put_and_rejects_corruption() -> None:
# Public AWS example credentials and PUT vector, not an active account:
# https://docs.aws.amazon.com/AmazonS3/latest/developerguide/sig-v4-header-based-auth.html
headers: Final = {
"date": "Fri, 24 May 2013 00:00:00 GMT", "host": "examplebucket.s3.amazonaws.com",
"x-amz-content-sha256": "44ce7dd67c959e0d3524ffac1771dfbba87d2b6b4b4e99e42034a8b803f8b072",
"x-amz-date": "20130524T000000Z", "x-amz-storage-class": "REDUCED_REDUNDANCY",
}
signed: Final = "date;host;x-amz-content-sha256;x-amz-date;x-amz-storage-class"
expected: Final = (
"9e0e90d9c76de8fa5b200d8c849cd5b8dc7a3be3951ddb7f6a76b4158342019d",
"98ad721746da40c64f1a55b78f14c238d841ea1380cd77a1b5971af0ece108bd",
)
actual: Final = signature("PUT", "/test%24file.text", headers, signed, b"Welcome to Amazon S3.", SECRET, "20130524/us-east-1/s3/aws4_request")
assert actual == expected
assert signature("PUT", "/test$file.text", headers, signed, b"Welcome to Amazon S3.", SECRET, "20130524/us-east-1/s3/aws4_request") != expected
assert encoded_path("/bucket/a=b+c/d e/雪.json") == "/bucket/a%3Db%2Bc/d%20e/%E9%9B%AA.json"
@pytest.mark.covers("other.provider_wire.s3.sync_async_reserved_keys_are_signed_and_accepted")
async def test_s3_sync_and_async_uploads_pass_independent_wire_verification() -> None:
from litellm.integrations.s3_v2 import S3Logger
from litellm.types.utils import StandardBatchLineItem, StandardBatchLineItemMetadata, StandardBatchLineItemsPayload
metadata: Final = StandardBatchLineItemMetadata(
user_api_key_hash=None, user_api_key_alias=None, user_api_key_team_id=None,
user_api_key_team_alias=None, user_api_key_user_id=None, user_api_key_org_id=None,
)
lines: Final = (
StandardBatchLineItem(
batch_id="synthetic-batch", custom_id="synthetic-event", status="success",
request={"content": "synthetic snow 雪"}, response=None, usage=None, response_cost=0.0,
model="synthetic-model", custom_llm_provider=None, metadata=metadata,
),
)
expected_path = ""
def scope_of(request: Request) -> str:
return f"{request.headers.get('x-amz-date', '')[:8]}/us-east-1/s3/aws4_request"
def verify(request: Request) -> Reply:
if request.method != "PUT" or request.target != expected_path:
return Reply(status=403)
try:
authorization: Final = request.headers.get("authorization", "")
assert authorization.startswith("AWS4-HMAC-SHA256 ")
fields: Final = dict(part.split("=", 1) for part in authorization.removeprefix("AWS4-HMAC-SHA256 ").split(", "))
access, scope = fields["Credential"].split("/", 1)
assert access == ACCESS and scope == scope_of(request)
assert datetime.strptime(request.headers["x-amz-date"], "%Y%m%dT%H%M%SZ")
signed: Final = fields["SignedHeaders"].split(";")
assert signed == sorted(set(signed))
assert {"host", "content-md5", "x-amz-date"}.issubset(signed)
assert {name for name in request.headers if name.startswith("x-amz-") and name != "x-amz-content-sha256"}.issubset(signed)
assert request.headers["content-md5"] == base64.b64encode(hashlib.md5(request.body, usedforsecurity=False).digest()).decode()
assert request.headers["x-amz-content-sha256"] == hashlib.sha256(request.body).hexdigest()
expected: Final = signature("PUT", request.target, request.headers, fields["SignedHeaders"], request.body, SECRET, scope)[1]
return Reply(status=200 if hmac.compare_digest(expected, fields["Signature"]) else 403)
except (AssertionError, KeyError, ValueError):
return Reply(status=403)
with wire_server(verify) as wire:
prior: Final = asyncio.all_tasks()
logger: Final = S3Logger(s3_bucket_name="integration-bucket", s3_region_name="us-east-1", s3_endpoint_url=wire.url,
s3_aws_access_key_id=ACCESS, s3_aws_secret_access_key=SECRET, s3_callback_params_override={},
s3_path="a=b+c/d e/雪")
owned: Final = asyncio.all_tasks() - prior
assert owned == {logger.periodic_flush_task}
try:
for batch_id in ("plain", "a=b+c d e", "percent%2Fplus+"):
expected_path = encoded_path(f"/integration-bucket/a=b+c/d e/雪/batch_line_items/2026-09-14/{batch_id}/part-00001.jsonl")
await logger.async_log_batch_line_items(StandardBatchLineItemsPayload(batch_id=batch_id, finished_at=1789344000, part=1, lines=lines))
requests: Final = wire.drain()
assert len(requests) == 1, "Upload must be accepted on its first actual PUT"
request: Final = requests[0]
assert request.target == expected_path
assert tuple(json.loads(line) for line in request.body.decode().splitlines()) == lines
assert verify(request).status == 200
day: Final = scope_of(request).split("/", 1)[0]
with httpx.Client(timeout=5, trust_env=False) as client:
corrupt: Final = {**request.headers, "authorization": request.headers["authorization"][:-1] + ("0" if request.headers["authorization"][-1] != "0" else "1")}
assert client.put(wire.url + expected_path, content=request.body, headers=corrupt).status_code == 403
assert client.put(wire.url + expected_path + "-wrong", content=request.body, headers=request.headers).status_code == 403
assert client.put(wire.url + expected_path, content=request.body + b" ", headers={name: value for name, value in request.headers.items() if name != "content-length"}).status_code == 403
fields: Final = dict(part.split("=", 1) for part in request.headers["authorization"].removeprefix("AWS4-HMAC-SHA256 ").split(", "))
for signed, scope, md5 in (
(fields["SignedHeaders"].replace("host;", ""), f"{day}/us-east-1/s3/aws4_request", request.headers["content-md5"]),
(fields["SignedHeaders"], f"{day}/us-west-2/s3/aws4_request", request.headers["content-md5"]),
(fields["SignedHeaders"], f"{day}/us-east-1/s3/aws4_request", "AAAAAAAAAAAAAAAAAAAAAA=="),
):
candidate_headers: Final = {**request.headers, "content-md5": md5}
digest: Final = signature("PUT", request.target, candidate_headers, signed, request.body, SECRET, scope)[1]
candidate_headers["authorization"] = f"AWS4-HMAC-SHA256 Credential={ACCESS}/{scope}, SignedHeaders={signed}, Signature={digest}"
assert client.put(wire.url + expected_path, content=request.body, headers=candidate_headers).status_code == 403
assert len(wire.drain()) == 6
finally:
for task in owned:
task.cancel()
await asyncio.gather(*owned, return_exceptions=True)
assert all(task.done() for task in owned)