81 lines
3.0 KiB
Python
81 lines
3.0 KiB
Python
import hashlib
|
|
import json
|
|
import zipfile
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
from fastapi import APIRouter, FastAPI
|
|
from fastapi.testclient import TestClient
|
|
|
|
from k1link.fleet.netbird import backups
|
|
from k1link.web.netbird_backup_api import register
|
|
|
|
|
|
def archive(path, *, extra=None):
|
|
files = {name: b"synthetic-private-state" for name in backups.REQUIRED}
|
|
if extra:
|
|
files.update(extra)
|
|
manifest = {
|
|
"schema": "missioncore.netbird-transfer/v1",
|
|
"domain": "network.example",
|
|
"management_url": "https://network.example:8443",
|
|
"identity_sha256": "a" * 64,
|
|
"files": {
|
|
name: {"bytes": len(data), "sha256": hashlib.sha256(data).hexdigest()}
|
|
for name, data in files.items()
|
|
},
|
|
}
|
|
with zipfile.ZipFile(path, "w") as output:
|
|
output.writestr("manifest.json", json.dumps(manifest))
|
|
for name, data in files.items():
|
|
output.writestr(name, data)
|
|
path.chmod(0o600)
|
|
return path
|
|
|
|
|
|
def test_private_backup_import_deduplicates_and_export_checks_integrity(tmp_path):
|
|
root = tmp_path / "fleet"
|
|
source = archive(tmp_path / "input.zip")
|
|
original = source.read_bytes()
|
|
row = backups.admit(root, source)
|
|
assert not source.exists()
|
|
assert "synthetic-private-state" not in json.dumps(row)
|
|
saved = backups.download(root, row["id"])
|
|
assert saved.read_bytes() == original
|
|
assert saved.stat().st_mode & 0o077 == 0
|
|
source.write_bytes(original)
|
|
assert backups.admit(root, source) == row
|
|
assert len(backups.catalog(root)["items"]) == 1
|
|
saved.write_bytes(b"x" * len(original))
|
|
with pytest.raises(zipfile.BadZipFile):
|
|
backups.download(root, row["id"])
|
|
|
|
|
|
def test_invalid_archive_cannot_write_paths_or_replace_previous_backup(tmp_path):
|
|
root = tmp_path / "fleet"
|
|
safe = backups.admit(root, archive(tmp_path / "safe.zip"))
|
|
unsafe = archive(tmp_path / "unsafe.zip", extra={"../../outside": b"no"})
|
|
with pytest.raises(ValueError):
|
|
backups.admit(root, unsafe)
|
|
assert not (tmp_path / "outside").exists()
|
|
assert backups.catalog(root)["items"] == [safe]
|
|
|
|
|
|
def test_api_import_export_is_binary_and_does_not_return_secrets(tmp_path):
|
|
app = FastAPI()
|
|
router = APIRouter()
|
|
register(router, lambda: SimpleNamespace(root=tmp_path / "fleet"))
|
|
app.include_router(router)
|
|
raw = archive(tmp_path / "source.zip").read_bytes()
|
|
with TestClient(app) as client:
|
|
response = client.post("/netbird/backups", content=raw)
|
|
assert response.status_code == 200
|
|
row = response.json()
|
|
assert "synthetic-private-state" not in response.text
|
|
assert client.get("/netbird/backups").json()["items"] == [row]
|
|
downloaded = client.get(f"/netbird/backups/{row['id']}/download")
|
|
assert downloaded.content == raw
|
|
assert downloaded.headers["cache-control"] == "no-store"
|
|
assert client.post("/netbird/backups", content=b"not an archive").status_code == 409
|
|
assert not list((tmp_path / "fleet/networks/netbird-backups").glob(".upload-*"))
|