-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add attachment integrity check (#1418)
* Add attachment integrity check * Update baselines
- Loading branch information
Showing
4 changed files
with
213 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
""" | ||
Every attachment in every collection has the right size and hash. | ||
The URLs of invalid attachments is returned along with the number of checked records. | ||
""" | ||
|
||
import hashlib | ||
|
||
import aiohttp | ||
|
||
from telescope.typings import CheckResult | ||
from telescope.utils import ClientSession, run_parallel | ||
|
||
from .utils import KintoClient | ||
|
||
|
||
async def test_attachment(session, attachment): | ||
url = attachment["location"] | ||
try: | ||
async with session.get(url) as response: | ||
binary = await response.read() | ||
except aiohttp.client_exceptions.ClientError as exc: | ||
return {"url": url, "error": str(exc)}, False | ||
|
||
if (bz := len(binary)) != (az := attachment["size"]): | ||
return {"url": url, "error": f"size differ ({bz}!={az})"}, False | ||
|
||
h = hashlib.sha256(binary) | ||
if (bh := h.hexdigest()) != (ah := attachment["hash"]): | ||
return {"url": url, "error": f"hash differ ({bh}!={ah})"}, False | ||
|
||
return {}, True | ||
|
||
|
||
async def run(server: str) -> CheckResult: | ||
client = KintoClient(server_url=server) | ||
|
||
info = await client.server_info() | ||
base_url = info["capabilities"]["attachments"]["base_url"] | ||
|
||
# Fetch collections records in parallel. | ||
entries = await client.get_monitor_changes() | ||
futures = [ | ||
client.get_records( | ||
bucket=entry["bucket"], | ||
collection=entry["collection"], | ||
_expected=entry["last_modified"], | ||
) | ||
for entry in entries | ||
if "preview" not in entry["bucket"] | ||
] | ||
results = await run_parallel(*futures) | ||
|
||
# For each record that has an attachment, check the attachment content. | ||
attachments = [] | ||
for entry, records in zip(entries, results): | ||
for record in records: | ||
if "attachment" not in record: | ||
continue | ||
attachment = record["attachment"] | ||
attachment["location"] = base_url + attachment["location"] | ||
attachments.append(attachment) | ||
|
||
async with ClientSession() as session: | ||
futures = [test_attachment(session, attachment) for attachment in attachments] | ||
results = await run_parallel(*futures) | ||
bad = [result for result, success in results if not success] | ||
return len(bad) == 0, {"bad": bad, "checked": len(attachments)} |
115 changes: 115 additions & 0 deletions
115
tests/checks/remotesettings/test_attachments_integrity.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
from checks.remotesettings.attachments_integrity import run | ||
|
||
|
||
RECORDS_URL = "/buckets/{}/collections/{}/records" | ||
|
||
|
||
async def test_positive(mock_responses, mock_aioresponses): | ||
server_url = "http://fake.local/v1" | ||
mock_responses.get( | ||
server_url + "/", | ||
payload={"capabilities": {"attachments": {"base_url": "http://cdn/"}}}, | ||
) | ||
changes_url = server_url + RECORDS_URL.format("monitor", "changes") | ||
mock_responses.get( | ||
changes_url, | ||
payload={ | ||
"data": [ | ||
{"id": "abc", "bucket": "bid", "collection": "cid", "last_modified": 42} | ||
] | ||
}, | ||
) | ||
records_url = server_url + RECORDS_URL.format("bid", "cid") + "?_expected=42" | ||
mock_responses.get( | ||
records_url, | ||
payload={ | ||
"data": [ | ||
{ | ||
"id": "abc", | ||
"attachment": { | ||
"size": 5, | ||
"hash": "ed968e840d10d2d313a870bc131a4e2c311d7ad09bdf32b3418147221f51a6e2", | ||
"location": "file1.jpg", | ||
}, | ||
}, | ||
{ | ||
"id": "efg", | ||
"attachment": { | ||
"size": 10, | ||
"hash": "bf2cb58a68f684d95a3b78ef8f661c9a4e5b09e82cc8f9cc88cce90528caeb27", | ||
"location": "file2.jpg", | ||
}, | ||
}, | ||
{"id": "ijk"}, | ||
] | ||
}, | ||
) | ||
mock_aioresponses.get("http://cdn/file1.jpg", body=b"a" * 5) | ||
mock_aioresponses.get("http://cdn/file2.jpg", body=b"a" * 10) | ||
|
||
status, data = await run(server_url) | ||
|
||
# assert status is True | ||
assert data == {"bad": [], "checked": 2} | ||
|
||
|
||
async def test_negative(mock_responses, mock_aioresponses): | ||
server_url = "http://fake.local/v1" | ||
mock_responses.get( | ||
server_url + "/", | ||
payload={"capabilities": {"attachments": {"base_url": "http://cdn/"}}}, | ||
) | ||
changes_url = server_url + RECORDS_URL.format("monitor", "changes") | ||
mock_responses.get( | ||
changes_url, | ||
payload={ | ||
"data": [ | ||
{"id": "abc", "bucket": "bid", "collection": "cid", "last_modified": 42} | ||
] | ||
}, | ||
) | ||
records_url = server_url + RECORDS_URL.format("bid", "cid") + "?_expected=42" | ||
mock_responses.get( | ||
records_url, | ||
payload={ | ||
"data": [ | ||
{ | ||
"id": "abc", | ||
"attachment": { | ||
"size": 7, | ||
"hash": "ed968e840d10d2d313a870bc131a4e2c311d7ad09bdf32b3418147221f51a6e2", | ||
"location": "file1.jpg", | ||
}, | ||
}, | ||
{"id": "efg", "attachment": {"location": "missing.jpg"}}, | ||
{ | ||
"id": "ijk", | ||
"attachment": {"size": 10, "hash": "foo", "location": "file2.jpg"}, | ||
}, | ||
{"id": "lmn"}, | ||
] | ||
}, | ||
) | ||
mock_aioresponses.get("http://cdn/file1.jpg", body=b"a" * 5) | ||
mock_aioresponses.get("http://cdn/file2.jpg", body=b"a" * 10) | ||
|
||
status, data = await run(server_url) | ||
|
||
assert status is False | ||
assert data == { | ||
"bad": [ | ||
{ | ||
"error": "size differ (5!=7)", | ||
"url": "http://cdn/file1.jpg", | ||
}, | ||
{ | ||
"error": "Connection refused: GET http://cdn/missing.jpg", | ||
"url": "http://cdn/missing.jpg", | ||
}, | ||
{ | ||
"error": "hash differ (bf2cb58a68f684d95a3b78ef8f661c9a4e5b09e82cc8f9cc88cce90528caeb27!=foo)", | ||
"url": "http://cdn/file2.jpg", | ||
}, | ||
], | ||
"checked": 3, | ||
} |