Withhold addresses after the database is recreated without a backup
When ripe.db is corrupted and no valid backup exists, a new empty database is created with a db_recreated.json marker; /addresses and /addresses/diff answer 503 until the collector gathers data again, /health reports db_recreated. Tests now isolate all state files via conftest. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
46d56654d8
commit
0155fd3f38
13 files changed
+226
-32
No files matched your search
@@ -21,6 +21,9 @@ def env(tmp_path, monkeypatch):
|
||||
monkeypatch.setattr(cc, "DATA_FILE", str(tmp_path / "data.json"))
|
||||
monkeypatch.setattr(cc, "FQDN_DATA_FILE", str(tmp_path / "fqdn_data.json"))
|
||||
monkeypatch.setattr(cc, "DB_FILE", str(tmp_path / "ripe.db"))
|
||||
monkeypatch.setattr(cc, "RECREATED_FILE", str(tmp_path / "db_recreated.json"))
|
||||
monkeypatch.setattr(cc, "RESTORE_FILE", str(tmp_path / "last_restore.json"))
|
||||
monkeypatch.setattr(cc, "BACKUP_DIR", str(tmp_path / "backups"))
|
||||
monkeypatch.setenv("RIPE_API_TOKEN", "secret")
|
||||
save_json_atomic(cc.CONFIG_FILE, {"asns": [], "fqdns": [], "ttl_days": 90})
|
||||
return TestClient(api_server.app)
|
||||
@@ -106,3 +109,26 @@ def test_input_hardening(env):
|
||||
assert env.get("/addresses/diff", params={"since": bad}).status_code == 400, bad
|
||||
# Корректный, но выходящий за журнал курсор - 410, а не 400
|
||||
assert env.get("/addresses/diff", params={"since": "9" * 30}).status_code == 410
|
||||
|
||||
|
||||
def test_recreated_database_is_withheld(env):
|
||||
cc.add_to_config_list("asns", 62041)
|
||||
with open(cc.DB_FILE, "wb") as f: # порча базы, копий нет
|
||||
f.write(b"garbage" * 1000)
|
||||
|
||||
# Пока данные не собраны заново, пустой список не отдаётся (503 + Retry-After)
|
||||
for url in ("/addresses", "/addresses/diff?since=0"):
|
||||
response = env.get(url)
|
||||
assert response.status_code == 503 and response.headers["Retry-After"], url
|
||||
assert env.get("/addresses", params={"type": "fqdn"}).json() == [] # тип без источников не блокируется
|
||||
health = env.get("/health").json()
|
||||
assert health["status"] == "degraded" and health["db_recreated"]["pending"] is True
|
||||
assert "quarantine" not in health["db_recreated"] # пути наружу не отдаются
|
||||
|
||||
# Сборщик собрал данные: выдача возобновляется, метка снята
|
||||
with db.session() as conn:
|
||||
with db.transaction(conn):
|
||||
db.merge_source(conn, "asn", "62041", {"1.0.0.0/24"}, datetime.datetime.now(), 90)
|
||||
db.settle_recreated(conn)
|
||||
assert env.get("/addresses").json() == ["1.0.0.0/24"]
|
||||
assert env.get("/health").json()["db_recreated"] is None
|
||||
Reference in new issue
Block a user