From 69aa9292ffc3f2f2a87b506c19cb82734d8201a5 Mon Sep 17 00:00:00 2001 From: Bruce Weygandt Date: Tue, 17 Nov 2026 10:42:15 -0700 Subject: [PATCH 1/5] feat(azure): add azure_database_backups fetcher --- CHANGELOG.md | 4 + fetchers/azure/database_backups/fetcher.py | 216 +++++++++++++++++ fetchers/azure/database_backups/fetcher.yaml | 125 ++++++++++ tests/test_azure_database_backups.py | 241 +++++++++++++++++++ 4 files changed, 586 insertions(+) create mode 100644 fetchers/azure/database_backups/fetcher.py create mode 100644 fetchers/azure/database_backups/fetcher.yaml create mode 100644 tests/test_azure_database_backups.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 56a905d2..93de5fa5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ schemas and the `paramify` CLI — not the internal code. ### Added +- **`azure_database_backups` fetcher.** Reads Azure Database for PostgreSQL and MySQL flexible servers: each + server's newest completed automatic backup against a window (manual, on-demand backups reported apart), backup retention against an optional authorized + period, geo-redundant backup, and servers created by point-in-time restore within an optional restore-test interval. Read replicas are counted, not + judged. Unread backup lists leave verdicts null rather than 0. - **`wiz_inventory`**, the Wiz cloud resource inventory, as a pipeline fetcher (`kind: inventory`, `id_scheme: wiz-resource-id`). One record per resource from `cloudResourcesV2` under `payload.data`, keyed by the Wiz resource id, with its cloud ID (`external_id`), account, region, type, IPs, OS, image, environment and owner, and internet diff --git a/fetchers/azure/database_backups/fetcher.py b/fetchers/azure/database_backups/fetcher.py new file mode 100644 index 00000000..c8c7b249 --- /dev/null +++ b/fetchers/azure/database_backups/fetcher.py @@ -0,0 +1,216 @@ +#!/usr/bin/env python3 +"""Azure Database for PostgreSQL and MySQL flexible servers: whether each server's automated backups run, how long they are kept, whether they are geo-redundant, and point-in-time restores.""" + +import logging +import os +import sys +from datetime import datetime, timedelta, timezone +from pathlib import Path +from typing import Any, Dict, List, Optional + +SCRIPT_DIR = Path(__file__).resolve().parent +sys.path.insert(0, str(SCRIPT_DIR.parent / "_shared")) +from azure_common import NOT_REGISTERED, REGISTERED, REGISTRATION_UNKNOWN, Collector, arm_id_key, resource_group_from_id # noqa: E402 +from azure_rest import int_config, parse_time, run_subscription # noqa: E402 +from resource_exclusions import ResourceExclusions # noqa: E402 + +NAME = "azure_database_backups" +logger = logging.getLogger(NAME) + +PROVIDERS_API = "2021-04-01" +# Newest stable versions this projection was written against; a cloud that does not list one (Azure Government +# lags) gets the newest stable version it does list below it. +ENGINES = { + "postgresql": {"namespace": "Microsoft.DBforPostgreSQL", "preferred": "2024-08-01"}, + "mysql": {"namespace": "Microsoft.DBforMySQL", "preferred": "2023-12-30"}, +} +REPLICA_ROLES = frozenset({"replica", "asyncreplica", "geoasyncreplica", "secondary"}) + +WINDOW_ENV = "AZURE_DATABASE_BACKUP_WINDOW_HOURS" +RETENTION_ENV = "AZURE_DATABASE_BACKUP_RETENTION_DAYS" +LOOKBACK_ENV = "AZURE_DATABASE_RESTORE_LOOKBACK_DAYS" +DEFAULT_WINDOW_HOURS = 26 +MAX_LOOKBACK_DAYS = 3650 + + +# --- pure transforms --- + +def choose_api_version(listed: List[str], preferred: str) -> str: + stable = sorted(v for v in listed if "preview" not in v.lower() and v <= preferred) + return stable[-1] if stable else preferred + + +def server_record(engine: str, raw: Dict[str, Any]) -> Dict[str, Any]: + props = raw.get("properties") or {} + backup = props.get("backup") or {} + role = (props.get("replicationRole") or "").lower() + source = props.get("sourceServerResourceId") + return { + "id": raw.get("id"), + "name": raw.get("name"), + "engine": engine, + "resource_group": resource_group_from_id(raw.get("id")), + "location": raw.get("location"), + "state": props.get("state"), + "replica": role in REPLICA_ROLES, + "restored_from": source if source and role not in REPLICA_ROLES else None, + "created": (raw.get("systemData") or {}).get("createdAt"), + "backup_retention_days": backup.get("backupRetentionDays"), + "geo_redundant_backup": (backup.get("geoRedundantBackup") or "").lower() == "enabled" if backup.get("geoRedundantBackup") else None, + "earliest_restore_date": backup.get("earliestRestoreDate"), + } + + +def is_manual(backup: Dict[str, Any]) -> bool: + """On-demand backups name themselves in backupType (PostgreSQL: Customer_On_Demand) or source (MySQL: On-Demand).""" + props = backup.get("properties") or {} + text = f"{props.get('backupType') or ''} {props.get('source') or ''}".lower().replace("-", "").replace("_", "") + return any(word in text for word in ("ondemand", "customer", "manual", "user")) + + +def _newest(backups: List[Dict[str, Any]]) -> Optional[datetime]: + times = [t for t in (parse_time((b.get("properties") or {}).get("completedTime")) for b in backups) if t] + return max(times) if times else None + + +def _iso(dt: Optional[datetime]) -> Optional[str]: + return dt.strftime("%Y-%m-%dT%H:%M:%SZ") if dt else None + + +def with_backups(server: Dict[str, Any], backups: Optional[List[Dict[str, Any]]], settings: Dict[str, Any], now: datetime) -> Dict[str, Any]: + """Automatic and manual backups apart; only the newest automatic backup is judged against the window. Null when unread.""" + if backups is None: + return {**server, "backups_read": False, "automatic_backups": None, "manual_backups": None, "latest_automatic_backup": None, + "latest_manual_backup": None, "hours_since_latest_automatic_backup": None, "backup_within_window": None, + "manual_backup_within_window": None} + manual = [b for b in backups if is_manual(b)] + automatic = [b for b in backups if not is_manual(b)] + auto, man = _newest(automatic), _newest(manual) + hours = round((now - auto).total_seconds() / 3600, 1) if auto else None + return {**server, "backups_read": True, "automatic_backups": len(automatic), "manual_backups": len(manual), + "latest_automatic_backup": _iso(auto), "latest_manual_backup": _iso(man), "hours_since_latest_automatic_backup": hours, + "backup_within_window": hours is not None and hours <= settings["window_hours"], + "manual_backup_within_window": bool(man) and (now - man).total_seconds() / 3600 <= settings["window_hours"]} + + +def summarize(servers: List[Dict[str, Any]], settings: Dict[str, Any], now: datetime, listed: bool = True) -> Dict[str, Any]: + primaries = [s for s in servers if not s["replica"]] + read = [s for s in primaries if s["backups_read"]] + complete = listed and len(read) == len(primaries) + lookback = settings["restore_lookback_days"] + required = settings["required_retention_days"] + + def known(value): + return value if complete else None + + retention = [s["backup_retention_days"] for s in primaries if s["backup_retention_days"] is not None] + return { + "database_servers_postgresql": sum(1 for s in servers if s["engine"] == "postgresql") if listed else None, + "database_servers_mysql": sum(1 for s in servers if s["engine"] == "mysql") if listed else None, + "replica_servers": sum(1 for s in servers if s["replica"]) if listed else None, + "backup_servers": len(primaries) if listed else None, + "backup_servers_read": len(read) if listed else None, + "servers_with_automatic_backup_within_window": known(sum(1 for s in primaries if s["backup_within_window"])), + "servers_without_automatic_backup_within_window": known(sorted(s["name"] for s in primaries if not s["backup_within_window"])), + "servers_relying_on_manual_backup": known(sum(1 for s in primaries if not s["backup_within_window"] and s["manual_backup_within_window"])), + "servers_with_manual_backups": known(sum(1 for s in primaries if s["manual_backups"])), + "max_hours_since_automatic_backup": known(max((s["hours_since_latest_automatic_backup"] for s in primaries), default=None) + if all(s["hours_since_latest_automatic_backup"] is not None for s in primaries) else None), + "servers_geo_redundant_backup": sum(1 for s in primaries if s["geo_redundant_backup"]) if listed else None, + "min_backup_retention_days": min(retention) if listed and retention else None, + "max_backup_retention_days": max(retention) if listed and retention else None, + "servers_retention_not_required": (sum(1 for s in primaries if s["backup_retention_days"] != required) + if listed and required is not None else None), + "required_retention_days": required, + "restored_servers_within_lookback": sum(1 for s in servers if s["restored_from"] and parse_time(s["created"]) + and parse_time(s["created"]) >= now - timedelta(days=lookback)) if listed and lookback else None, + "restored_servers_date_unknown": sum(1 for s in servers if s["restored_from"] and not parse_time(s["created"])) if listed else None, + "backup_window_hours": settings["window_hours"], + "restore_lookback_days": settings["restore_lookback_days"], + } + + +def settings_from_env(collector: Collector) -> Dict[str, Any]: + raw = (os.environ.get(RETENTION_ENV) or "").strip() + required = int(raw) if raw.isdigit() and int(raw) > 0 else None + if raw and required is None: + collector.record(f"config {RETENTION_ENV}", ValueError(f"{RETENTION_ENV}={raw!r} is not a whole number above 0")) + raw_lookback = (os.environ.get(LOOKBACK_ENV) or "").strip() + lookback = min(int(raw_lookback), MAX_LOOKBACK_DAYS) if raw_lookback.isdigit() and int(raw_lookback) > 0 else None # unset until adopted + if raw_lookback and lookback is None: + collector.record(f"config {LOOKBACK_ENV}", ValueError(f"{LOOKBACK_ENV}={raw_lookback!r} is not a whole number above 0; left unset")) + return {"window_hours": int_config(WINDOW_ENV, DEFAULT_WINDOW_HOURS), "required_retention_days": required, "restore_lookback_days": lookback} + + +# --- collection --- + +def read_provider(client, subscription_id: str, engine: str, collector: Collector) -> Dict[str, Any]: + spec = ENGINES[engine] + body = collector.guard(f"providers.get({spec['namespace']})", lambda: client.get( + f"/subscriptions/{subscription_id}/providers/{spec['namespace']}", PROVIDERS_API), default=None) + if body is None: + return {"registration": REGISTRATION_UNKNOWN, "api_version": spec["preferred"]} + versions = next((list(rt.get("apiVersions") or []) for rt in body.get("resourceTypes") or [] + if (rt.get("resourceType") or "").lower() == "flexibleservers"), []) + state = (body.get("registrationState") or "").lower() + return {"registration": REGISTERED if state == "registered" else NOT_REGISTERED, + "api_version": choose_api_version(versions, spec["preferred"])} + + +def collect(client, subscription_id: str, collector: Collector, exclusions: Optional[ResourceExclusions] = None, + now: Optional[datetime] = None) -> tuple: + exclusions = exclusions if exclusions is not None else ResourceExclusions() + now = now or datetime.now(timezone.utc) + settings = settings_from_env(collector) + servers: List[Dict[str, Any]] = [] + excluded: List[dict] = [] + listed = True + providers = {} + for engine, spec in ENGINES.items(): + provider = read_provider(client, subscription_id, engine, collector) + providers[engine] = provider + if provider["registration"] == NOT_REGISTERED: + continue # an unregistered provider has no servers, which is known + api = provider["api_version"] + raw = collector.guard(f"{engine}.flexibleServers.list", lambda: client.list( + f"/subscriptions/{subscription_id}/providers/{spec['namespace']}/flexibleServers", api), default=None) + if raw is None: + listed = False + continue + found = sorted((server_record(engine, s) for s in raw), key=lambda s: arm_id_key(s["id"])) + found, out = exclusions.split(found, "database_server") + excluded += out + for s in found: + if s["replica"]: + # Read replicas take no backups of their own; they are counted, not judged. + servers.append({**s, "backups_read": False, "automatic_backups": None, "manual_backups": None, + "latest_automatic_backup": None, "latest_manual_backup": None, + "hours_since_latest_automatic_backup": None, "backup_within_window": None, + "manual_backup_within_window": None}) + continue + backups = collector.guard(f"{engine}.backups.list ({s['name']})", + lambda s=s: client.list(f"{s['id']}/backups", api), default=None) + servers.append(with_backups(s, backups, settings, now)) + exclusions.record_invalid(collector) + if not listed: + exclusions.mark_unread() + exclusions.warn_unmatched(logger) + results = {"database_servers": servers, "evaluated_at": now.strftime("%Y-%m-%dT%H:%M:%SZ"), + "api_versions": {e: p["api_version"] for e, p in providers.items()}, + "excluded": sorted(excluded, key=lambda e: arm_id_key(e.get("id")))} + summary = {**summarize(servers, settings, now, listed), + **{f"{e}_provider_registration_status": p["registration"] for e, p in providers.items()}, + **exclusions.summary(excluded, ("database_server",), {"database_servers": len(servers) if listed else None})} + return results, summary + + +def collect_subscription(client, subscription_id: str, collector: Collector) -> tuple: + return collect(client, subscription_id, collector, ResourceExclusions.from_env()) + + +def main() -> int: + return run_subscription(fetcher=NAME, logger=logger, collect=collect_subscription) + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/fetchers/azure/database_backups/fetcher.yaml b/fetchers/azure/database_backups/fetcher.yaml new file mode 100644 index 00000000..06dff8bd --- /dev/null +++ b/fetchers/azure/database_backups/fetcher.yaml @@ -0,0 +1,125 @@ +name: azure_database_backups +version: 0.1.0 +description: >- + Shows whether each Azure Database for PostgreSQL and MySQL flexible server completed an automatic backup recently, apart from manual ones, how long backups are kept, whether they are geo-redundant, and recent point-in-time restores. +category: azure + +supports_targets: true + +runtime: + type: python + entry: fetcher.py + +output: + type: json + path: azure_database_backups.json + aggregation: per_target + +config_schema: + backup_window_hours: + type: integer + required: false + default: 26 + env: AZURE_DATABASE_BACKUP_WINDOW_HOURS + description: > + How recent a server's newest completed backup must be to count as a backup within the window. Flexible servers + take a full snapshot daily, so the default allows a day plus slack. + required_retention_days: + type: integer + required: false + env: AZURE_DATABASE_BACKUP_RETENTION_DAYS + description: > + The authorized backup retention, in days. When set, each server whose retention differs is counted in + servers_retention_not_required. Leave it unset and that count is null. A value that is not a whole number above 0 + fails the run as bad_config. + restore_lookback_days: + type: integer + required: false + env: AZURE_DATABASE_RESTORE_LOOKBACK_DAYS + description: > + The adopted restore-test interval in days: how far back a server created by point-in-time restore counts toward + restored_servers_within_lookback, up to 3650. There is no default: while it is unset, that count is null rather + than judged against an interval nobody adopted. A value that is not a whole number above 0 fails the run as + bad_config and leaves it unset. + resource_exclusions: + type: patterns + prefixes: [rg, location, id] + bare: false + required: false + env: AZURE_RESOURCE_EXCLUSIONS + description: > + Patterns that take database servers out of scope, as for the other Azure fetchers. + +target_schema: + subscription_id: + type: string + required: false + env: AZURE_SUBSCRIPTION_ID + description: > + Azure subscription id to collect from. Optional — omit to use the first + enabled subscription the ambient credential can see; set one target per + subscription to fan out. + environment: + type: string + required: false + env: AZURE_ENVIRONMENT + description: Deployment environment label (e.g. prod) written into the evidence metadata. + +secrets: [] # DefaultAzureCredential only — see _categories/azure.yaml + +ksis: + - KSI-RPL-ABO # each server's newest completed backup against the window + - KSI-RPL-ARP # geo-redundant backup, which keeps a copy in the paired region + - KSI-RPL-TRC # servers created by point-in-time restore + - KSI-SVC-RUD # backup retention against the authorized period + +evidence_set: + reference_id: EVD-AZURE-DATABASE-BACKUPS + name: Azure Database Backups + instructions: | + Script: azure/database_backups/fetcher.py + + Access: Reader on the subscription. + + Commands Executed: + - subscriptions.list() — picks a subscription when none is set + - GET providers/Microsoft.DBforPostgreSQL and providers/Microsoft.DBforMySQL — whether each provider is registered, and which API version this cloud supports + - GET Microsoft.DBforPostgreSQL/flexibleServers and Microsoft.DBforMySQL/flexibleServers — every flexible server with its backup retention, geo-redundant backup, earliest restore point, replication role and restore source + - GET flexibleServers/{server}/backups (per server that is not a read replica) — the server's completed backups, told apart as automatic or manual (on-demand) by their type and source + + Output: azure_database_backups_.json — each PostgreSQL and MySQL flexible server with its backup settings, its newest completed backup judged against the window, and whether it was created by a point-in-time restore. + + Summary fields: + + What was checked? + - database_servers_postgresql — Number of PostgreSQL flexible servers in scope; null if a server list could not be read + - database_servers_mysql — Number of MySQL flexible servers in scope + - replica_servers — Number of read replicas, which take no backups of their own and are not judged + - backup_servers — Number of servers that are not read replicas (backup servers) + - backup_servers_read — Number of backup servers whose backup list could be read + - postgresql_provider_registration_status — Whether Microsoft.DBforPostgreSQL is registered: registered, not_registered or unknown + - mysql_provider_registration_status — Whether Microsoft.DBforMySQL is registered: registered, not_registered or unknown + - resource_exclusion_patterns — List of the resource_exclusions patterns as given + - resource_exclusion_patterns_unmatched — List of the patterns that matched nothing, such as a mistyped name; null when a server list could not be read + - excluded_database_servers — Number of database servers a pattern took out of scope + - in_scope_database_servers — Number of database servers assessed, read replicas included + + Do backups run? + - servers_with_automatic_backup_within_window — Number of backup servers whose newest completed automatic backup finished within backup_window_hours (an automatic backup within the window); manual, on-demand backups never count; null unless every backup server's backup list was read + - servers_without_automatic_backup_within_window — List of backup server names without an automatic backup within the window + - servers_relying_on_manual_backup — Number of backup servers with no automatic backup within the window but a manual one within it, where a manual backup would otherwise hide broken automation + - servers_with_manual_backups — Number of backup servers with at least one manual, on-demand backup + - max_hours_since_automatic_backup — Hours since the newest completed automatic backup of the backup server that has gone longest without one; null if any backup server has no completed automatic backup or could not be read + - backup_window_hours — Hours a server's newest completed backup may be old and still count as a backup within the window + + Are backups kept for the authorized period and copied off-site? + - min_backup_retention_days — Days of backup retention on the backup server that keeps backups the shortest + - max_backup_retention_days — Days of backup retention on the backup server that keeps backups the longest + - servers_retention_not_required — Number of backup servers whose retention differs from required_retention_days; null when required_retention_days is not set + - required_retention_days — The authorized backup retention in days, as configured; null when not set + - servers_geo_redundant_backup — Number of backup servers with geo-redundant backup enabled, which keeps a copy of backups in the paired region + + Are restores exercised? + - restored_servers_within_lookback — Number of servers created by a point-in-time restore (a restore source and not a read replica) within restore_lookback_days; a server with no creation date never counts; null while restore_lookback_days is not set + - restored_servers_date_unknown — Number of servers created by a point-in-time restore whose creation date is missing, reported as unknown rather than recent + - restore_lookback_days — Days back that a server created by point-in-time restore counts, as configured; null when not set diff --git a/tests/test_azure_database_backups.py b/tests/test_azure_database_backups.py new file mode 100644 index 00000000..14e18110 --- /dev/null +++ b/tests/test_azure_database_backups.py @@ -0,0 +1,241 @@ +"""azure_database_backups: server projection, backup recency against the window, replica and restore handling, retention against the authorized period, null-not-zero summaries, and a full run against synthetic ARM responses. No SDK or network.""" + +from __future__ import annotations + +import importlib.util +import json +import sys +from collections import namedtuple +from datetime import datetime, timedelta, timezone +from pathlib import Path +from urllib.parse import parse_qs, urlparse + +import pytest + +REPO_ROOT = Path(__file__).resolve().parent.parent +AZURE = REPO_ROOT / "fetchers" / "azure" +sys.path.insert(0, str(REPO_ROOT / "fetchers" / "_lib")) +sys.path.insert(0, str(AZURE / "_shared")) + +_spec = importlib.util.spec_from_file_location("azure_database_backups_under_test", AZURE / "database_backups" / "fetcher.py") +db = importlib.util.module_from_spec(_spec) +_spec.loader.exec_module(db) +azure_rest = sys.modules["azure_rest"] + +NOW = datetime(2026, 11, 17, 12, 0, tzinfo=timezone.utc) +SETTINGS = {"window_hours": 26, "required_retention_days": None, "restore_lookback_days": 365} +SUB = "/subscriptions/s1" + + +def iso(dt): + return dt.strftime("%Y-%m-%dT%H:%M:%SZ") + + +def raw_server(name, engine="postgresql", role="Primary", source=None, retention=7, geo="Enabled", created=None): + ns = db.ENGINES[engine]["namespace"] + props = {"state": "Ready", "replicationRole": role, + "backup": {"backupRetentionDays": retention, "geoRedundantBackup": geo, "earliestRestoreDate": "2026-11-10T00:00:00Z"}} + if source: + props["sourceServerResourceId"] = source + return {"id": f"{SUB}/resourceGroups/rg1/providers/{ns}/flexibleServers/{name}", "name": name, "location": "eastus", + "properties": props, "systemData": {"createdAt": created or "2026-01-01T00:00:00Z"}} + + +def backup(hours_ago, now=NOW): + return {"name": f"b{hours_ago}", "properties": {"backupType": "FULL", "completedTime": iso(now - timedelta(hours=hours_ago))}} + + +def test_server_record_reads_backup_settings_and_tells_replicas_from_restores(): + s = db.server_record("postgresql", raw_server("pg1")) + assert (s["backup_retention_days"], s["geo_redundant_backup"], s["replica"], s["restored_from"]) == (7, True, False, None) + rep = db.server_record("mysql", raw_server("r1", "mysql", role="Replica", source="/src")) + assert (rep["replica"], rep["restored_from"]) == (True, None) + restored = db.server_record("postgresql", raw_server("pitr", source="/src", role="None")) + assert (restored["replica"], restored["restored_from"]) == (False, "/src") + assert db.server_record("postgresql", raw_server("x", geo=None))["geo_redundant_backup"] is None + + +def test_newest_completed_backup_is_judged_against_the_window(): + s = db.server_record("postgresql", raw_server("pg1")) + fresh = db.with_backups(s, [backup(30), backup(5)], SETTINGS, NOW) + assert (fresh["latest_automatic_backup"], fresh["hours_since_latest_automatic_backup"], fresh["backup_within_window"]) == (iso(NOW - timedelta(hours=5)), 5.0, True) + stale = db.with_backups(s, [backup(40)], SETTINGS, NOW) + assert stale["backup_within_window"] is False + assert db.with_backups(s, [], SETTINGS, NOW)["backup_within_window"] is False + assert db.with_backups(s, None, SETTINGS, NOW)["backup_within_window"] is None + + +def _servers(): + a = db.with_backups(db.server_record("postgresql", raw_server("pg1", retention=35)), [backup(5)], SETTINGS, NOW) + b = db.with_backups(db.server_record("mysql", raw_server("my1", "mysql", retention=7, geo="Disabled")), [backup(50)], SETTINGS, NOW) + r = {**db.server_record("mysql", raw_server("r1", "mysql", role="Replica", source="/x")), "backups_read": False, + "automatic_backups": None, "manual_backups": None, "latest_automatic_backup": None, "latest_manual_backup": None, + "hours_since_latest_automatic_backup": None, "backup_within_window": None, "manual_backup_within_window": None} + p = db.with_backups(db.server_record("postgresql", raw_server("pitr", source="/src", role="None", created=iso(NOW - timedelta(days=20)))), + [backup(3)], SETTINGS, NOW) + return [a, b, r, p] + + +def test_summary_judges_backup_servers_and_skips_replicas(): + s = db.summarize(_servers(), SETTINGS, NOW) + assert (s["database_servers_postgresql"], s["database_servers_mysql"], s["replica_servers"], s["backup_servers"], s["backup_servers_read"]) == (2, 2, 1, 3, 3) + assert (s["servers_with_automatic_backup_within_window"], s["servers_without_automatic_backup_within_window"], s["max_hours_since_automatic_backup"]) == (2, ["my1"], 50.0) + assert (s["servers_geo_redundant_backup"], s["min_backup_retention_days"], s["max_backup_retention_days"]) == (2, 7, 35) + assert (s["servers_retention_not_required"], s["restored_servers_within_lookback"]) == (None, 1) + + +def test_retention_is_counted_only_against_a_configured_authorized_period(): + s = db.summarize(_servers(), {**SETTINGS, "required_retention_days": 35}, NOW) + assert (s["servers_retention_not_required"], s["required_retention_days"]) == (2, 35) + + +def test_an_unread_backup_list_or_server_list_leaves_verdicts_null(): + servers = _servers() + servers[0] = db.with_backups(db.server_record("postgresql", raw_server("pg1")), None, SETTINGS, NOW) + s = db.summarize(servers, SETTINGS, NOW) + assert (s["backup_servers_read"], s["servers_with_automatic_backup_within_window"], s["max_hours_since_automatic_backup"]) == (2, None, None) + unlisted = db.summarize([], SETTINGS, NOW, listed=False) + assert (unlisted["backup_servers"], unlisted["servers_geo_redundant_backup"], unlisted["restored_servers_within_lookback"]) == (None, None, None) + + +def test_api_version_is_the_newest_stable_at_or_below_the_preferred(): + assert db.choose_api_version(["2022-12-01", "2024-08-01", "2025-01-01-preview", "2026-01-01"], "2024-08-01") == "2024-08-01" + assert db.choose_api_version(["2021-05-01", "2023-06-01-preview"], "2023-12-30") == "2021-05-01" + assert db.choose_api_version([], "2024-08-01") == "2024-08-01" + + +def test_summary_matches_the_instructions(assert_summary_documented): + s = db.summarize(_servers(), SETTINGS, NOW) + s.update({"postgresql_provider_registration_status": "registered", "mysql_provider_registration_status": "registered"}) + s.update(db.ResourceExclusions().summary([], ("database_server",), {"database_servers": 4})) + assert_summary_documented("azure/database_backups", s) + + +# --- full run against synthetic ARM responses --- + +class _Resp: + def __init__(self, status, body): + self.status_code, self._body, self.reason, self.text, self.headers = status, body, "", json.dumps(body), {} + + def json(self): + return self._body + + +class _Arm: + def __init__(self, mysql_registered=True, refused_backups=()): + self.mysql_registered, self.refused = mysql_registered, set(refused_backups) + self.urls = [] + + def get(self, url, params=None, headers=None, timeout=None, allow_redirects=True): + path = urlparse(url).path + query = {**{k: v[0] for k, v in parse_qs(urlparse(url).query).items()}, **(params or {})} + self.urls.append((path, query.get("api-version"))) + now = datetime.now(timezone.utc) + for engine, spec in db.ENGINES.items(): + ns = spec["namespace"] + if path.endswith(f"/providers/{ns}"): + registered = engine == "postgresql" or self.mysql_registered + return _Resp(200, {"registrationState": "Registered" if registered else "NotRegistered", + "resourceTypes": [{"resourceType": "flexibleServers", "apiVersions": ["2021-05-01", "2022-12-01", spec["preferred"]]}]}) + if path.endswith(f"/providers/{ns}/flexibleServers"): + return _Resp(200, {"value": [raw_server("pg1"), raw_server("pg-replica", role="AsyncReplica", source="/src")]} if engine == "postgresql" + else {"value": [raw_server("my1", "mysql", geo="Disabled")]}) + if path.endswith("/backups"): + name = path.split("/")[-2] + if name in self.refused: + return _Resp(403, {"error": {"code": "AuthorizationFailed", "message": "no"}}) + return _Resp(200, {"value": [backup(4, now), backup(28, now)]}) + return _Resp(404, {"error": {"code": "NotFound", "message": path}}) + + +class _Cred: + def get_token(self, scope): + return namedtuple("T", "token expires_on")("tok", 9_999_999_999) + + +@pytest.fixture +def arm(monkeypatch, tmp_path): + def serve(**kwargs): + session = _Arm(**kwargs) + monkeypatch.setattr("requests.Session", lambda: session) + return session + + monkeypatch.setattr(azure_rest, "pinned_credential", lambda sub: _Cred()) + monkeypatch.setattr(azure_rest, "REPO_ROOT", tmp_path / "repo") + monkeypatch.chdir(tmp_path) + monkeypatch.setenv("AZURE_SUBSCRIPTION_ID", "s1") + monkeypatch.setenv("EVIDENCE_DIR", str(tmp_path)) + monkeypatch.setenv("FETCHER_STATUS_FILE", str(tmp_path / "status.json")) + for env in ("AZURE_AUTHORITY_HOST", "AZURE_TENANT_ID", "AZURE_ENVIRONMENT", "AZURE_RESOURCE_EXCLUSIONS", "AZURE_TARGET_SUBSCRIPTION_ONLY", + db.WINDOW_ENV, db.RETENTION_ENV, db.LOOKBACK_ENV): + monkeypatch.delenv(env, raising=False) + return serve + + +def _evidence(tmp_path): + [path] = tmp_path.glob("azure_database_backups_s1.json") + return json.loads(path.read_text()) + + +def test_main_reads_both_engines_and_skips_replica_backups(arm, tmp_path): + session = arm() + assert db.main() == 0 + s = _evidence(tmp_path)["summary"] + assert (s["backup_servers"], s["replica_servers"], s["servers_with_automatic_backup_within_window"], s["servers_geo_redundant_backup"]) == (2, 1, 2, 1) + assert (s["postgresql_provider_registration_status"], s["mysql_provider_registration_status"]) == ("registered", "registered") + backup_calls = [p for p, _ in session.urls if p.endswith("/backups")] + assert len(backup_calls) == 2 and not any("pg-replica" in p for p in backup_calls) + assert {v for p, v in session.urls if "DBforPostgreSQL/flexibleServers" in p} == {"2024-08-01"} + + +def test_an_unreadable_backup_list_fails_the_run_and_leaves_verdicts_null(arm, tmp_path): + arm(refused_backups=("my1",)) + assert db.main() != 0 + s = _evidence(tmp_path)["summary"] + assert (s["backup_servers_read"], s["servers_with_automatic_backup_within_window"], s["max_hours_since_automatic_backup"]) == (1, None, None) + + +def test_an_unregistered_provider_has_no_servers_and_succeeds(arm, tmp_path): + arm(mysql_registered=False) + assert db.main() == 0 + s = _evidence(tmp_path)["summary"] + assert (s["database_servers_mysql"], s["mysql_provider_registration_status"], s["backup_servers"]) == (0, "not_registered", 1) + + +def test_an_invalid_retention_setting_fails_the_run(arm, tmp_path, monkeypatch): + monkeypatch.setenv(db.RETENTION_ENV, "a week") + arm() + assert db.main() != 0 + assert _evidence(tmp_path)["summary"]["servers_retention_not_required"] is None + + +def test_restored_servers_are_counted_only_once_an_interval_is_adopted(): + unset = db.summarize(_servers(), {**SETTINGS, "restore_lookback_days": None}, NOW) + assert (unset["restored_servers_within_lookback"], unset["restore_lookback_days"]) == (None, None) + assert db.summarize(_servers(), {**SETTINGS, "restore_lookback_days": 10}, NOW)["restored_servers_within_lookback"] == 0 + + +def manual_backup(hours_ago, engine="postgresql", now=NOW): + props = {"completedTime": iso(now - timedelta(hours=hours_ago))} + props.update({"backupType": "Customer_On_Demand"} if engine == "postgresql" else {"backupType": "FULL", "source": "On-Demand"}) + return {"name": f"m{hours_ago}", "properties": props} + + +def test_manual_backups_are_told_apart_by_type_or_source(): + assert db.is_manual(manual_backup(1)) and db.is_manual(manual_backup(1, "mysql")) + assert not db.is_manual(backup(1)) and not db.is_manual({"properties": {"backupType": "FULL", "source": "Automatic"}}) + + +def test_a_recent_manual_backup_does_not_hide_broken_automation(): + s = db.with_backups(db.server_record("postgresql", raw_server("pg1")), [backup(80), manual_backup(2)], SETTINGS, NOW) + assert (s["automatic_backups"], s["manual_backups"], s["backup_within_window"], s["manual_backup_within_window"]) == (1, 1, False, True) + summary = db.summarize([s], SETTINGS, NOW) + assert (summary["servers_with_automatic_backup_within_window"], summary["servers_relying_on_manual_backup"], summary["servers_with_manual_backups"]) == (0, 1, 1) + assert summary["max_hours_since_automatic_backup"] == 80.0 + + +def test_a_restored_server_without_a_creation_date_is_unknown_not_recent(): + undated = db.server_record("postgresql", raw_server("pitr", source="/src", role="None")) + undated["created"] = None + s = db.summarize([db.with_backups(undated, [backup(3)], SETTINGS, NOW)], {**SETTINGS, "restore_lookback_days": 365}, NOW) + assert (s["restored_servers_within_lookback"], s["restored_servers_date_unknown"]) == (0, 1) From 1ee44ac5604fb7191168cedf0d5588b6da89c8cc Mon Sep 17 00:00:00 2001 From: Bruce Weygandt Date: Tue, 17 Nov 2026 13:42:53 -0700 Subject: [PATCH 2/5] refactor(azure): rename azure_database_backups to azure_flexible_server_backups; retention minimum, stopped servers, shared API version --- CHANGELOG.md | 9 ++-- .../fetcher.py | 31 ++++++++++---- .../fetcher.yaml | 32 +++++++------- ... => test_azure_flexible_server_backups.py} | 42 ++++++++++++++----- tests/test_contracts.py | 1 + 5 files changed, 78 insertions(+), 37 deletions(-) rename fetchers/azure/{database_backups => flexible_server_backups}/fetcher.py (88%) rename fetchers/azure/{database_backups => flexible_server_backups}/fetcher.yaml (74%) rename tests/{test_azure_database_backups.py => test_azure_flexible_server_backups.py} (82%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93de5fa5..f445bb31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,10 +12,11 @@ schemas and the `paramify` CLI — not the internal code. ### Added -- **`azure_database_backups` fetcher.** Reads Azure Database for PostgreSQL and MySQL flexible servers: each - server's newest completed automatic backup against a window (manual, on-demand backups reported apart), backup retention against an optional authorized - period, geo-redundant backup, and servers created by point-in-time restore within an optional restore-test interval. Read replicas are counted, not - judged. Unread backup lists leave verdicts null rather than 0. +- **`azure_flexible_server_backups` fetcher.** Reads Azure Database for PostgreSQL and MySQL flexible servers: each + server's newest completed automatic backup against a window (manual, on-demand backups reported apart), backup retention against an optional minimum + authorized period (unknown retention counted apart), geo-redundant backup, stopped servers, and servers created by point-in-time restore within an + optional restore-test interval. One API version that serves both the server and backup lists is used. Read replicas are counted, not judged. Unread + backup lists leave verdicts null rather than 0. - **`wiz_inventory`**, the Wiz cloud resource inventory, as a pipeline fetcher (`kind: inventory`, `id_scheme: wiz-resource-id`). One record per resource from `cloudResourcesV2` under `payload.data`, keyed by the Wiz resource id, with its cloud ID (`external_id`), account, region, type, IPs, OS, image, environment and owner, and internet diff --git a/fetchers/azure/database_backups/fetcher.py b/fetchers/azure/flexible_server_backups/fetcher.py similarity index 88% rename from fetchers/azure/database_backups/fetcher.py rename to fetchers/azure/flexible_server_backups/fetcher.py index c8c7b249..14d2772c 100644 --- a/fetchers/azure/database_backups/fetcher.py +++ b/fetchers/azure/flexible_server_backups/fetcher.py @@ -14,7 +14,7 @@ from azure_rest import int_config, parse_time, run_subscription # noqa: E402 from resource_exclusions import ResourceExclusions # noqa: E402 -NAME = "azure_database_backups" +NAME = "azure_flexible_server_backups" logger = logging.getLogger(NAME) PROVIDERS_API = "2021-04-01" @@ -26,9 +26,9 @@ } REPLICA_ROLES = frozenset({"replica", "asyncreplica", "geoasyncreplica", "secondary"}) -WINDOW_ENV = "AZURE_DATABASE_BACKUP_WINDOW_HOURS" -RETENTION_ENV = "AZURE_DATABASE_BACKUP_RETENTION_DAYS" -LOOKBACK_ENV = "AZURE_DATABASE_RESTORE_LOOKBACK_DAYS" +WINDOW_ENV = "AZURE_FLEXIBLE_SERVER_BACKUP_WINDOW_HOURS" +RETENTION_ENV = "AZURE_FLEXIBLE_SERVER_BACKUP_RETENTION_DAYS" +LOOKBACK_ENV = "AZURE_FLEXIBLE_SERVER_RESTORE_LOOKBACK_DAYS" DEFAULT_WINDOW_HOURS = 26 MAX_LOOKBACK_DAYS = 3650 @@ -40,6 +40,16 @@ def choose_api_version(listed: List[str], preferred: str) -> str: return stable[-1] if stable else preferred +def shared_versions(resource_types: List[Dict[str, Any]]) -> List[str]: + """API versions listed for both flexibleServers and flexibleServers/backups, since one version serves both calls. + Falls back to the flexibleServers list when the provider does not list the backups type.""" + def listed(name): + return next((list(rt.get("apiVersions") or []) for rt in resource_types + if (rt.get("resourceType") or "").lower() == name), None) + servers, backups = listed("flexibleservers") or [], listed("flexibleservers/backups") + return [v for v in servers if v in set(backups)] if backups is not None else servers + + def server_record(engine: str, raw: Dict[str, Any]) -> Dict[str, Any]: props = raw.get("properties") or {} backup = props.get("backup") or {} @@ -52,6 +62,8 @@ def server_record(engine: str, raw: Dict[str, Any]) -> Dict[str, Any]: "resource_group": resource_group_from_id(raw.get("id")), "location": raw.get("location"), "state": props.get("state"), + # A stopped server takes no backups, so it shows without a recent one; stopped_servers names the reason. + "stopped": (props.get("state") or "").lower() == "stopped", "replica": role in REPLICA_ROLES, "restored_from": source if source and role not in REPLICA_ROLES else None, "created": (raw.get("systemData") or {}).get("createdAt"), @@ -104,6 +116,7 @@ def known(value): return value if complete else None retention = [s["backup_retention_days"] for s in primaries if s["backup_retention_days"] is not None] + unknown_retention = sum(1 for s in primaries if s["backup_retention_days"] is None) return { "database_servers_postgresql": sum(1 for s in servers if s["engine"] == "postgresql") if listed else None, "database_servers_mysql": sum(1 for s in servers if s["engine"] == "mysql") if listed else None, @@ -119,8 +132,11 @@ def known(value): "servers_geo_redundant_backup": sum(1 for s in primaries if s["geo_redundant_backup"]) if listed else None, "min_backup_retention_days": min(retention) if listed and retention else None, "max_backup_retention_days": max(retention) if listed and retention else None, - "servers_retention_not_required": (sum(1 for s in primaries if s["backup_retention_days"] != required) - if listed and required is not None else None), + "servers_retention_below_required": (sum(1 for s in primaries if s["backup_retention_days"] is not None + and s["backup_retention_days"] < required) + if listed and required is not None else None), + "servers_retention_unknown": unknown_retention if listed else None, + "stopped_servers": sum(1 for s in primaries if s["stopped"]) if listed else None, "required_retention_days": required, "restored_servers_within_lookback": sum(1 for s in servers if s["restored_from"] and parse_time(s["created"]) and parse_time(s["created"]) >= now - timedelta(days=lookback)) if listed and lookback else None, @@ -150,8 +166,7 @@ def read_provider(client, subscription_id: str, engine: str, collector: Collecto f"/subscriptions/{subscription_id}/providers/{spec['namespace']}", PROVIDERS_API), default=None) if body is None: return {"registration": REGISTRATION_UNKNOWN, "api_version": spec["preferred"]} - versions = next((list(rt.get("apiVersions") or []) for rt in body.get("resourceTypes") or [] - if (rt.get("resourceType") or "").lower() == "flexibleservers"), []) + versions = shared_versions(body.get("resourceTypes") or []) state = (body.get("registrationState") or "").lower() return {"registration": REGISTERED if state == "registered" else NOT_REGISTERED, "api_version": choose_api_version(versions, spec["preferred"])} diff --git a/fetchers/azure/database_backups/fetcher.yaml b/fetchers/azure/flexible_server_backups/fetcher.yaml similarity index 74% rename from fetchers/azure/database_backups/fetcher.yaml rename to fetchers/azure/flexible_server_backups/fetcher.yaml index 06dff8bd..16061b3b 100644 --- a/fetchers/azure/database_backups/fetcher.yaml +++ b/fetchers/azure/flexible_server_backups/fetcher.yaml @@ -1,7 +1,7 @@ -name: azure_database_backups +name: azure_flexible_server_backups version: 0.1.0 description: >- - Shows whether each Azure Database for PostgreSQL and MySQL flexible server completed an automatic backup recently, apart from manual ones, how long backups are kept, whether they are geo-redundant, and recent point-in-time restores. + Shows whether each PostgreSQL and MySQL flexible server completed a recent automatic backup, how long backups are kept, whether they are geo-redundant, and recent point-in-time restores. category: azure supports_targets: true @@ -12,7 +12,7 @@ runtime: output: type: json - path: azure_database_backups.json + path: azure_flexible_server_backups.json aggregation: per_target config_schema: @@ -20,22 +20,22 @@ config_schema: type: integer required: false default: 26 - env: AZURE_DATABASE_BACKUP_WINDOW_HOURS + env: AZURE_FLEXIBLE_SERVER_BACKUP_WINDOW_HOURS description: > How recent a server's newest completed backup must be to count as a backup within the window. Flexible servers take a full snapshot daily, so the default allows a day plus slack. required_retention_days: type: integer required: false - env: AZURE_DATABASE_BACKUP_RETENTION_DAYS + env: AZURE_FLEXIBLE_SERVER_BACKUP_RETENTION_DAYS description: > - The authorized backup retention, in days. When set, each server whose retention differs is counted in - servers_retention_not_required. Leave it unset and that count is null. A value that is not a whole number above 0 + The minimum authorized backup retention, in days. When set, each server that keeps backups for fewer days is + counted in servers_retention_below_required; keeping them longer meets it. Leave it unset and that count is null. A value that is not a whole number above 0 fails the run as bad_config. restore_lookback_days: type: integer required: false - env: AZURE_DATABASE_RESTORE_LOOKBACK_DAYS + env: AZURE_FLEXIBLE_SERVER_RESTORE_LOOKBACK_DAYS description: > The adopted restore-test interval in days: how far back a server created by point-in-time restore counts toward restored_servers_within_lookback, up to 3650. There is no default: while it is unset, that count is null rather @@ -74,20 +74,20 @@ ksis: - KSI-SVC-RUD # backup retention against the authorized period evidence_set: - reference_id: EVD-AZURE-DATABASE-BACKUPS - name: Azure Database Backups + reference_id: EVD-AZURE-FLEXIBLE-SERVER-BACKUPS + name: Azure PostgreSQL and MySQL Backups instructions: | - Script: azure/database_backups/fetcher.py + Script: azure/flexible_server_backups/fetcher.py Access: Reader on the subscription. Commands Executed: - subscriptions.list() — picks a subscription when none is set - - GET providers/Microsoft.DBforPostgreSQL and providers/Microsoft.DBforMySQL — whether each provider is registered, and which API version this cloud supports + - GET providers/Microsoft.DBforPostgreSQL and providers/Microsoft.DBforMySQL — whether each provider is registered, and the newest API version this cloud lists for both flexibleServers and flexibleServers/backups, since one version serves both calls - GET Microsoft.DBforPostgreSQL/flexibleServers and Microsoft.DBforMySQL/flexibleServers — every flexible server with its backup retention, geo-redundant backup, earliest restore point, replication role and restore source - GET flexibleServers/{server}/backups (per server that is not a read replica) — the server's completed backups, told apart as automatic or manual (on-demand) by their type and source - Output: azure_database_backups_.json — each PostgreSQL and MySQL flexible server with its backup settings, its newest completed backup judged against the window, and whether it was created by a point-in-time restore. + Output: azure_flexible_server_backups_.json — each PostgreSQL and MySQL flexible server with its backup settings, its newest completed backup judged against the window, and whether it was created by a point-in-time restore. Summary fields: @@ -97,6 +97,7 @@ evidence_set: - replica_servers — Number of read replicas, which take no backups of their own and are not judged - backup_servers — Number of servers that are not read replicas (backup servers) - backup_servers_read — Number of backup servers whose backup list could be read + - stopped_servers — Number of backup servers that are stopped; a stopped server takes no backups, so it also appears without an automatic backup within the window - postgresql_provider_registration_status — Whether Microsoft.DBforPostgreSQL is registered: registered, not_registered or unknown - mysql_provider_registration_status — Whether Microsoft.DBforMySQL is registered: registered, not_registered or unknown - resource_exclusion_patterns — List of the resource_exclusions patterns as given @@ -115,11 +116,12 @@ evidence_set: Are backups kept for the authorized period and copied off-site? - min_backup_retention_days — Days of backup retention on the backup server that keeps backups the shortest - max_backup_retention_days — Days of backup retention on the backup server that keeps backups the longest - - servers_retention_not_required — Number of backup servers whose retention differs from required_retention_days; null when required_retention_days is not set + - servers_retention_below_required — Number of backup servers that keep backups for fewer days than required_retention_days; a server that keeps them longer meets it, and one with no retention returned is counted in servers_retention_unknown instead; null when required_retention_days is not set + - servers_retention_unknown — Number of backup servers for which Azure returned no backup retention - required_retention_days — The authorized backup retention in days, as configured; null when not set - servers_geo_redundant_backup — Number of backup servers with geo-redundant backup enabled, which keeps a copy of backups in the paired region Are restores exercised? - - restored_servers_within_lookback — Number of servers created by a point-in-time restore (a restore source and not a read replica) within restore_lookback_days; a server with no creation date never counts; null while restore_lookback_days is not set + - restored_servers_within_lookback — Number of servers created by a point-in-time restore (a restore source and not a read replica) within restore_lookback_days; only servers that still exist are seen, so a restore test that deleted its server when it finished does not count; a server with no creation date never counts; null while restore_lookback_days is not set - restored_servers_date_unknown — Number of servers created by a point-in-time restore whose creation date is missing, reported as unknown rather than recent - restore_lookback_days — Days back that a server created by point-in-time restore counts, as configured; null when not set diff --git a/tests/test_azure_database_backups.py b/tests/test_azure_flexible_server_backups.py similarity index 82% rename from tests/test_azure_database_backups.py rename to tests/test_azure_flexible_server_backups.py index 14e18110..e7840791 100644 --- a/tests/test_azure_database_backups.py +++ b/tests/test_azure_flexible_server_backups.py @@ -1,4 +1,4 @@ -"""azure_database_backups: server projection, backup recency against the window, replica and restore handling, retention against the authorized period, null-not-zero summaries, and a full run against synthetic ARM responses. No SDK or network.""" +"""azure_flexible_server_backups: server projection, backup recency against the window, replica and restore handling, retention against the authorized period, null-not-zero summaries, and a full run against synthetic ARM responses. No SDK or network.""" from __future__ import annotations @@ -17,7 +17,7 @@ sys.path.insert(0, str(REPO_ROOT / "fetchers" / "_lib")) sys.path.insert(0, str(AZURE / "_shared")) -_spec = importlib.util.spec_from_file_location("azure_database_backups_under_test", AZURE / "database_backups" / "fetcher.py") +_spec = importlib.util.spec_from_file_location("azure_flexible_server_backups_under_test", AZURE / "flexible_server_backups" / "fetcher.py") db = importlib.util.module_from_spec(_spec) _spec.loader.exec_module(db) azure_rest = sys.modules["azure_rest"] @@ -31,9 +31,9 @@ def iso(dt): return dt.strftime("%Y-%m-%dT%H:%M:%SZ") -def raw_server(name, engine="postgresql", role="Primary", source=None, retention=7, geo="Enabled", created=None): +def raw_server(name, engine="postgresql", role="Primary", source=None, retention=7, geo="Enabled", created=None, state="Ready"): ns = db.ENGINES[engine]["namespace"] - props = {"state": "Ready", "replicationRole": role, + props = {"state": state, "replicationRole": role, "backup": {"backupRetentionDays": retention, "geoRedundantBackup": geo, "earliestRestoreDate": "2026-11-10T00:00:00Z"}} if source: props["sourceServerResourceId"] = source @@ -81,12 +81,33 @@ def test_summary_judges_backup_servers_and_skips_replicas(): assert (s["database_servers_postgresql"], s["database_servers_mysql"], s["replica_servers"], s["backup_servers"], s["backup_servers_read"]) == (2, 2, 1, 3, 3) assert (s["servers_with_automatic_backup_within_window"], s["servers_without_automatic_backup_within_window"], s["max_hours_since_automatic_backup"]) == (2, ["my1"], 50.0) assert (s["servers_geo_redundant_backup"], s["min_backup_retention_days"], s["max_backup_retention_days"]) == (2, 7, 35) - assert (s["servers_retention_not_required"], s["restored_servers_within_lookback"]) == (None, 1) + assert (s["servers_retention_below_required"], s["restored_servers_within_lookback"]) == (None, 1) def test_retention_is_counted_only_against_a_configured_authorized_period(): s = db.summarize(_servers(), {**SETTINGS, "required_retention_days": 35}, NOW) - assert (s["servers_retention_not_required"], s["required_retention_days"]) == (2, 35) + assert (s["servers_retention_below_required"], s["required_retention_days"]) == (2, 35) + + +def test_longer_retention_meets_the_requirement_and_unknown_retention_is_counted_apart(): + longer = db.with_backups(db.server_record("postgresql", raw_server("long", retention=35)), [backup(5)], SETTINGS, NOW) + short = db.with_backups(db.server_record("postgresql", raw_server("short", retention=7)), [backup(5)], SETTINGS, NOW) + unknown = db.with_backups(db.server_record("mysql", raw_server("unk", "mysql", retention=None)), [backup(5)], SETTINGS, NOW) + s = db.summarize([longer, short, unknown], {**SETTINGS, "required_retention_days": 14}, NOW) + assert (s["servers_retention_below_required"], s["servers_retention_unknown"]) == (1, 1) + + +def test_a_stopped_server_is_counted_so_its_missing_backup_has_a_reason(): + stopped = db.with_backups(db.server_record("postgresql", raw_server("off", state="Stopped")), [backup(80)], SETTINGS, NOW) + s = db.summarize([stopped], SETTINGS, NOW) + assert (s["stopped_servers"], s["servers_without_automatic_backup_within_window"]) == (1, ["off"]) + + +def test_one_api_version_serves_both_the_server_list_and_the_backups_list(): + types = [{"resourceType": "flexibleServers", "apiVersions": ["2022-12-01", "2023-06-01", "2024-08-01"]}, + {"resourceType": "flexibleServers/backups", "apiVersions": ["2022-12-01", "2023-06-01"]}] + assert db.choose_api_version(db.shared_versions(types), "2024-08-01") == "2023-06-01" + assert db.shared_versions(types[:1]) == ["2022-12-01", "2023-06-01", "2024-08-01"] # backups type not listed def test_an_unread_backup_list_or_server_list_leaves_verdicts_null(): @@ -108,7 +129,7 @@ def test_summary_matches_the_instructions(assert_summary_documented): s = db.summarize(_servers(), SETTINGS, NOW) s.update({"postgresql_provider_registration_status": "registered", "mysql_provider_registration_status": "registered"}) s.update(db.ResourceExclusions().summary([], ("database_server",), {"database_servers": 4})) - assert_summary_documented("azure/database_backups", s) + assert_summary_documented("azure/flexible_server_backups", s) # --- full run against synthetic ARM responses --- @@ -136,7 +157,8 @@ def get(self, url, params=None, headers=None, timeout=None, allow_redirects=True if path.endswith(f"/providers/{ns}"): registered = engine == "postgresql" or self.mysql_registered return _Resp(200, {"registrationState": "Registered" if registered else "NotRegistered", - "resourceTypes": [{"resourceType": "flexibleServers", "apiVersions": ["2021-05-01", "2022-12-01", spec["preferred"]]}]}) + "resourceTypes": [{"resourceType": "flexibleServers", "apiVersions": ["2021-05-01", "2022-12-01", spec["preferred"]]}, + {"resourceType": "flexibleServers/backups", "apiVersions": ["2021-05-01", "2022-12-01", spec["preferred"]]}]}) if path.endswith(f"/providers/{ns}/flexibleServers"): return _Resp(200, {"value": [raw_server("pg1"), raw_server("pg-replica", role="AsyncReplica", source="/src")]} if engine == "postgresql" else {"value": [raw_server("my1", "mysql", geo="Disabled")]}) @@ -173,7 +195,7 @@ def serve(**kwargs): def _evidence(tmp_path): - [path] = tmp_path.glob("azure_database_backups_s1.json") + [path] = tmp_path.glob("azure_flexible_server_backups_s1.json") return json.loads(path.read_text()) @@ -206,7 +228,7 @@ def test_an_invalid_retention_setting_fails_the_run(arm, tmp_path, monkeypatch): monkeypatch.setenv(db.RETENTION_ENV, "a week") arm() assert db.main() != 0 - assert _evidence(tmp_path)["summary"]["servers_retention_not_required"] is None + assert _evidence(tmp_path)["summary"]["servers_retention_below_required"] is None def test_restored_servers_are_counted_only_once_an_interval_is_adopted(): diff --git a/tests/test_contracts.py b/tests/test_contracts.py index 181753c9..6e14fa6c 100644 --- a/tests/test_contracts.py +++ b/tests/test_contracts.py @@ -275,6 +275,7 @@ def test_instructions_open_with_the_entry_path() -> None: "azure/entra_service_principals", "azure/entra_sign_ins", "azure/firewall_configuration", + "azure/flexible_server_backups", "azure/front_door_waf_policies", "azure/function_app_configuration", "azure/key_vault_configuration", From 2cf87e6e2f27688c0088209ee3122ebe1726e3e6 Mon Sep 17 00:00:00 2001 From: Bruce Weygandt Date: Tue, 17 Nov 2026 14:06:11 -0700 Subject: [PATCH 3/5] fix(azure_flexible_server_backups): classify backups by exact label per engine; unknown labels never count as automatic --- CHANGELOG.md | 3 +- .../azure/flexible_server_backups/fetcher.py | 36 ++++-- .../flexible_server_backups/fetcher.yaml | 3 +- .../flexible_server_backups/README.md | 12 ++ .../mysql-backups-2023-12-30.json | 114 ++++++++++++++++++ .../postgresql-backups-2024-08-01.json | 24 ++++ tests/test_azure_flexible_server_backups.py | 48 ++++++-- 7 files changed, 222 insertions(+), 18 deletions(-) create mode 100644 tests/fixtures/flexible_server_backups/README.md create mode 100644 tests/fixtures/flexible_server_backups/mysql-backups-2023-12-30.json create mode 100644 tests/fixtures/flexible_server_backups/postgresql-backups-2024-08-01.json diff --git a/CHANGELOG.md b/CHANGELOG.md index f445bb31..2905201b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,8 @@ schemas and the `paramify` CLI — not the internal code. ### Added - **`azure_flexible_server_backups` fetcher.** Reads Azure Database for PostgreSQL and MySQL flexible servers: each - server's newest completed automatic backup against a window (manual, on-demand backups reported apart), backup retention against an optional minimum + server's newest completed automatic backup against a window (manual, on-demand backups reported apart, and backups + whose labels match no known value counted as unknown, never automatic), backup retention against an optional minimum authorized period (unknown retention counted apart), geo-redundant backup, stopped servers, and servers created by point-in-time restore within an optional restore-test interval. One API version that serves both the server and backup lists is used. Read replicas are counted, not judged. Unread backup lists leave verdicts null rather than 0. diff --git a/fetchers/azure/flexible_server_backups/fetcher.py b/fetchers/azure/flexible_server_backups/fetcher.py index 14d2772c..9a1171ab 100644 --- a/fetchers/azure/flexible_server_backups/fetcher.py +++ b/fetchers/azure/flexible_server_backups/fetcher.py @@ -73,11 +73,26 @@ def server_record(engine: str, raw: Dict[str, Any]) -> Dict[str, Any]: } -def is_manual(backup: Dict[str, Any]) -> bool: - """On-demand backups name themselves in backupType (PostgreSQL: Customer_On_Demand) or source (MySQL: On-Demand).""" +# How each engine labels a backup in GET flexibleServers/{server}/backups (properties.backupType, properties.source), +# compared exactly and without regard to case. PostgreSQL's backupType is a defined enum: Full is a scheduled +# automatic backup and "Customer On-Demand" (source "Customer Initiated") one a person took. MySQL's backupType and +# source are free text: only FULL from Automatic is confirmed as automatic, and no on-demand value is confirmed yet. +# Anything else is unknown, never automatic, so an unrecognized backup can never stand in for broken automation. +AUTOMATIC, MANUAL, UNKNOWN = "automatic", "manual", "unknown" +BACKUP_KINDS = { + "postgresql": {("full", None): AUTOMATIC, ("full", "automatic"): AUTOMATIC, + ("customer on-demand", None): MANUAL, ("customer on-demand", "customer initiated"): MANUAL}, + "mysql": {("full", "automatic"): AUTOMATIC}, +} + + +def backup_kind(engine: str, backup: Dict[str, Any]) -> str: + """automatic, manual or unknown, from the exact backupType and source the engine reports.""" props = backup.get("properties") or {} - text = f"{props.get('backupType') or ''} {props.get('source') or ''}".lower().replace("-", "").replace("_", "") - return any(word in text for word in ("ondemand", "customer", "manual", "user")) + + def norm(value): + return str(value).strip().lower() if value not in (None, "") else None + return BACKUP_KINDS.get(engine, {}).get((norm(props.get("backupType")), norm(props.get("source"))), UNKNOWN) def _newest(backups: List[Dict[str, Any]]) -> Optional[datetime]: @@ -92,14 +107,19 @@ def _iso(dt: Optional[datetime]) -> Optional[str]: def with_backups(server: Dict[str, Any], backups: Optional[List[Dict[str, Any]]], settings: Dict[str, Any], now: datetime) -> Dict[str, Any]: """Automatic and manual backups apart; only the newest automatic backup is judged against the window. Null when unread.""" if backups is None: - return {**server, "backups_read": False, "automatic_backups": None, "manual_backups": None, "latest_automatic_backup": None, + return {**server, "backups_read": False, "automatic_backups": None, "manual_backups": None, "unknown_type_backups": None, + "unknown_backup_labels": None, "latest_automatic_backup": None, "latest_manual_backup": None, "hours_since_latest_automatic_backup": None, "backup_within_window": None, "manual_backup_within_window": None} - manual = [b for b in backups if is_manual(b)] - automatic = [b for b in backups if not is_manual(b)] + kinds = [(b, backup_kind(server["engine"], b)) for b in backups] + automatic = [b for b, k in kinds if k == AUTOMATIC] + manual = [b for b, k in kinds if k == MANUAL] + unknown = [b for b, k in kinds if k == UNKNOWN] + labels = sorted({f"{(b.get('properties') or {}).get('backupType')} / {(b.get('properties') or {}).get('source')}" for b in unknown}) auto, man = _newest(automatic), _newest(manual) hours = round((now - auto).total_seconds() / 3600, 1) if auto else None return {**server, "backups_read": True, "automatic_backups": len(automatic), "manual_backups": len(manual), + "unknown_type_backups": len(unknown), "unknown_backup_labels": labels, "latest_automatic_backup": _iso(auto), "latest_manual_backup": _iso(man), "hours_since_latest_automatic_backup": hours, "backup_within_window": hours is not None and hours <= settings["window_hours"], "manual_backup_within_window": bool(man) and (now - man).total_seconds() / 3600 <= settings["window_hours"]} @@ -127,6 +147,7 @@ def known(value): "servers_without_automatic_backup_within_window": known(sorted(s["name"] for s in primaries if not s["backup_within_window"])), "servers_relying_on_manual_backup": known(sum(1 for s in primaries if not s["backup_within_window"] and s["manual_backup_within_window"])), "servers_with_manual_backups": known(sum(1 for s in primaries if s["manual_backups"])), + "servers_with_unknown_backup_types": known(sum(1 for s in primaries if s["unknown_type_backups"])), "max_hours_since_automatic_backup": known(max((s["hours_since_latest_automatic_backup"] for s in primaries), default=None) if all(s["hours_since_latest_automatic_backup"] is not None for s in primaries) else None), "servers_geo_redundant_backup": sum(1 for s in primaries if s["geo_redundant_backup"]) if listed else None, @@ -199,6 +220,7 @@ def collect(client, subscription_id: str, collector: Collector, exclusions: Opti if s["replica"]: # Read replicas take no backups of their own; they are counted, not judged. servers.append({**s, "backups_read": False, "automatic_backups": None, "manual_backups": None, + "unknown_type_backups": None, "unknown_backup_labels": None, "latest_automatic_backup": None, "latest_manual_backup": None, "hours_since_latest_automatic_backup": None, "backup_within_window": None, "manual_backup_within_window": None}) diff --git a/fetchers/azure/flexible_server_backups/fetcher.yaml b/fetchers/azure/flexible_server_backups/fetcher.yaml index 16061b3b..b33d1bf3 100644 --- a/fetchers/azure/flexible_server_backups/fetcher.yaml +++ b/fetchers/azure/flexible_server_backups/fetcher.yaml @@ -85,7 +85,7 @@ evidence_set: - subscriptions.list() — picks a subscription when none is set - GET providers/Microsoft.DBforPostgreSQL and providers/Microsoft.DBforMySQL — whether each provider is registered, and the newest API version this cloud lists for both flexibleServers and flexibleServers/backups, since one version serves both calls - GET Microsoft.DBforPostgreSQL/flexibleServers and Microsoft.DBforMySQL/flexibleServers — every flexible server with its backup retention, geo-redundant backup, earliest restore point, replication role and restore source - - GET flexibleServers/{server}/backups (per server that is not a read replica) — the server's completed backups, told apart as automatic or manual (on-demand) by their type and source + - GET flexibleServers/{server}/backups (per server that is not a read replica) — the server's completed backups, each classed as automatic, manual (on-demand) or unknown by the exact backupType and source the engine reports: PostgreSQL Full is automatic and Customer On-Demand manual; MySQL FULL from Automatic is automatic; any other label is unknown, never automatic Output: azure_flexible_server_backups_.json — each PostgreSQL and MySQL flexible server with its backup settings, its newest completed backup judged against the window, and whether it was created by a point-in-time restore. @@ -110,6 +110,7 @@ evidence_set: - servers_without_automatic_backup_within_window — List of backup server names without an automatic backup within the window - servers_relying_on_manual_backup — Number of backup servers with no automatic backup within the window but a manual one within it, where a manual backup would otherwise hide broken automation - servers_with_manual_backups — Number of backup servers with at least one manual, on-demand backup + - servers_with_unknown_backup_types — Number of backup servers with at least one backup whose backupType and source match no known label; such a backup is never counted as automatic, so it cannot hide broken automation (each server lists the labels under unknown_backup_labels) - max_hours_since_automatic_backup — Hours since the newest completed automatic backup of the backup server that has gone longest without one; null if any backup server has no completed automatic backup or could not be read - backup_window_hours — Hours a server's newest completed backup may be old and still count as a backup within the window diff --git a/tests/fixtures/flexible_server_backups/README.md b/tests/fixtures/flexible_server_backups/README.md new file mode 100644 index 00000000..93c01b8e --- /dev/null +++ b/tests/fixtures/flexible_server_backups/README.md @@ -0,0 +1,12 @@ +# flexible_server_backups fixtures + +Response bodies of `GET flexibleServers/{server}/backups`, copied from Microsoft's published examples in +[Azure/azure-rest-api-specs](https://github.com/Azure/azure-rest-api-specs): + +- `postgresql-backups-2024-08-01.json`: `Microsoft.DBforPostgreSQL/PostgreSql/stable/2024-08-01/examples/BackupListByServer.json` + (one automatic and one on-demand backup). +- `mysql-backups-2023-12-30.json`: `Microsoft.DBforMySQL/FlexibleServers/stable/2023-12-30/examples/BackupsListByServer.json` + (automatic backups only). + +They hold no tenant data. The MySQL list has no on-demand backup, and the published MySQL on-demand example reports +`source: Automatic`, so how a real MySQL on-demand backup is labelled is still to be confirmed against a live response. diff --git a/tests/fixtures/flexible_server_backups/mysql-backups-2023-12-30.json b/tests/fixtures/flexible_server_backups/mysql-backups-2023-12-30.json new file mode 100644 index 00000000..e77c4cad --- /dev/null +++ b/tests/fixtures/flexible_server_backups/mysql-backups-2023-12-30.json @@ -0,0 +1,114 @@ +{ + "value": [ + { + "properties": { + "backupType": "FULL", + "source": "Automatic", + "completedTime": "2021-06-15T16:05:19.902522+00:00" + }, + "id": "/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/TestGroup/providers/Microsoft.DBforMySQL/flexibleServers/mysqltestserver/backups/daily_20210615T160516", + "name": "daily_20210615T160516", + "type": "Microsoft.DBforMySQL/flexibleServers/backups" + }, + { + "properties": { + "backupType": "FULL", + "source": "Automatic", + "completedTime": "2021-06-16T16:05:23.9243453+00:00" + }, + "id": "/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/TestGroup/providers/Microsoft.DBforMySQL/flexibleServers/mysqltestserver/backups/daily_20210616T160520", + "name": "daily_20210616T160520", + "type": "Microsoft.DBforMySQL/flexibleServers/backups" + }, + { + "properties": { + "backupType": "FULL", + "source": "Automatic", + "completedTime": "2021-06-17T16:05:28.1247488+00:00" + }, + "id": "/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/TestGroup/providers/Microsoft.DBforMySQL/flexibleServers/mysqltestserver/backups/daily_20210617T160525", + "name": "daily_20210617T160525", + "type": "Microsoft.DBforMySQL/flexibleServers/backups" + }, + { + "properties": { + "backupType": "FULL", + "source": "Automatic", + "completedTime": "2021-06-18T16:05:32.2736701+00:00" + }, + "id": "/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/TestGroup/providers/Microsoft.DBforMySQL/flexibleServers/mysqltestserver/backups/daily_20210618T160529", + "name": "daily_20210618T160529", + "type": "Microsoft.DBforMySQL/flexibleServers/backups" + }, + { + "properties": { + "backupType": "FULL", + "source": "Automatic", + "completedTime": "2021-06-19T16:05:36.8603354+00:00" + }, + "id": "/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/TestGroup/providers/Microsoft.DBforMySQL/flexibleServers/mysqltestserver/backups/daily_20210619T160533", + "name": "daily_20210619T160533", + "type": "Microsoft.DBforMySQL/flexibleServers/backups" + }, + { + "properties": { + "backupType": "FULL", + "source": "Automatic", + "completedTime": "2021-06-20T16:05:41.9200138+00:00" + }, + "id": "/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/TestGroup/providers/Microsoft.DBforMySQL/flexibleServers/mysqltestserver/backups/daily_20210620T160538", + "name": "daily_20210620T160538", + "type": "Microsoft.DBforMySQL/flexibleServers/backups" + }, + { + "properties": { + "backupType": "FULL", + "source": "Automatic", + "completedTime": "2021-06-21T16:05:48.8528447+00:00" + }, + "id": "/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/TestGroup/providers/Microsoft.DBforMySQL/flexibleServers/mysqltestserver/backups/daily_20210621T160543", + "name": "daily_20210621T160543", + "type": "Microsoft.DBforMySQL/flexibleServers/backups" + }, + { + "properties": { + "backupType": "FULL", + "source": "Automatic", + "completedTime": "2021-06-22T16:08:06.3121688+00:00" + }, + "id": "/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/TestGroup/providers/Microsoft.DBforMySQL/flexibleServers/mysqltestserver/backups/daily_20210622T160803", + "name": "daily_20210622T160803", + "type": "Microsoft.DBforMySQL/flexibleServers/backups" + }, + { + "properties": { + "backupType": "FULL", + "source": "Automatic", + "completedTime": "2021-06-22T21:08:10.5057354+00:00" + }, + "id": "/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/TestGroup/providers/Microsoft.DBforMySQL/flexibleServers/mysqltestserver/backups/daily_20210622T210807", + "name": "daily_20210622T210807", + "type": "Microsoft.DBforMySQL/flexibleServers/backups" + }, + { + "properties": { + "backupType": "FULL", + "source": "Automatic", + "completedTime": "2021-06-23T21:24:16.9401531+00:00" + }, + "id": "/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/TestGroup/providers/Microsoft.DBforMySQL/flexibleServers/mysqltestserver/backups/daily_20210623T212413", + "name": "daily_20210623T212413", + "type": "Microsoft.DBforMySQL/flexibleServers/backups" + }, + { + "properties": { + "backupType": "FULL", + "source": "Automatic", + "completedTime": "2021-06-24T06:13:31.4962137+00:00" + }, + "id": "/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/TestGroup/providers/Microsoft.DBforMySQL/flexibleServers/mysqltestserver/backups/daily_20210624T061328", + "name": "daily_20210624T061328", + "type": "Microsoft.DBforMySQL/flexibleServers/backups" + } + ] +} diff --git a/tests/fixtures/flexible_server_backups/postgresql-backups-2024-08-01.json b/tests/fixtures/flexible_server_backups/postgresql-backups-2024-08-01.json new file mode 100644 index 00000000..2ce60aad --- /dev/null +++ b/tests/fixtures/flexible_server_backups/postgresql-backups-2024-08-01.json @@ -0,0 +1,24 @@ +{ + "value": [ + { + "properties": { + "backupType": "Full", + "source": "Automatic", + "completedTime": "2024-08-01T14:30:22.123456+00:00" + }, + "id": "/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.DBforPostgreSQL/flexibleServers/exampleserver/backups/backup_638830782181266873", + "name": "backup_638830782181266873", + "type": "Microsoft.DBforPostgreSQL/flexibleServers/backups" + }, + { + "properties": { + "backupType": "Customer On-Demand", + "source": "Customer Initiated", + "completedTime": "2024-08-01T18:30:22.123456+00:00" + }, + "id": "/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.DBforPostgreSQL/flexibleServers/exampleserver/backups/ondemandbackup-20240801T183022", + "name": "ondemandbackup-20240801T183022", + "type": "Microsoft.DBforPostgreSQL/flexibleServers/backups" + } + ] +} diff --git a/tests/test_azure_flexible_server_backups.py b/tests/test_azure_flexible_server_backups.py index e7840791..c29f5f27 100644 --- a/tests/test_azure_flexible_server_backups.py +++ b/tests/test_azure_flexible_server_backups.py @@ -42,7 +42,7 @@ def raw_server(name, engine="postgresql", role="Primary", source=None, retention def backup(hours_ago, now=NOW): - return {"name": f"b{hours_ago}", "properties": {"backupType": "FULL", "completedTime": iso(now - timedelta(hours=hours_ago))}} + return {"name": f"b{hours_ago}", "properties": {"backupType": "FULL", "source": "Automatic", "completedTime": iso(now - timedelta(hours=hours_ago))}} def test_server_record_reads_backup_settings_and_tells_replicas_from_restores(): @@ -69,7 +69,7 @@ def _servers(): a = db.with_backups(db.server_record("postgresql", raw_server("pg1", retention=35)), [backup(5)], SETTINGS, NOW) b = db.with_backups(db.server_record("mysql", raw_server("my1", "mysql", retention=7, geo="Disabled")), [backup(50)], SETTINGS, NOW) r = {**db.server_record("mysql", raw_server("r1", "mysql", role="Replica", source="/x")), "backups_read": False, - "automatic_backups": None, "manual_backups": None, "latest_automatic_backup": None, "latest_manual_backup": None, + "automatic_backups": None, "manual_backups": None, "unknown_type_backups": None, "unknown_backup_labels": None, "latest_automatic_backup": None, "latest_manual_backup": None, "hours_since_latest_automatic_backup": None, "backup_within_window": None, "manual_backup_within_window": None} p = db.with_backups(db.server_record("postgresql", raw_server("pitr", source="/src", role="None", created=iso(NOW - timedelta(days=20)))), [backup(3)], SETTINGS, NOW) @@ -237,15 +237,45 @@ def test_restored_servers_are_counted_only_once_an_interval_is_adopted(): assert db.summarize(_servers(), {**SETTINGS, "restore_lookback_days": 10}, NOW)["restored_servers_within_lookback"] == 0 -def manual_backup(hours_ago, engine="postgresql", now=NOW): - props = {"completedTime": iso(now - timedelta(hours=hours_ago))} - props.update({"backupType": "Customer_On_Demand"} if engine == "postgresql" else {"backupType": "FULL", "source": "On-Demand"}) - return {"name": f"m{hours_ago}", "properties": props} +FIXTURES = REPO_ROOT / "tests" / "fixtures" / "flexible_server_backups" -def test_manual_backups_are_told_apart_by_type_or_source(): - assert db.is_manual(manual_backup(1)) and db.is_manual(manual_backup(1, "mysql")) - assert not db.is_manual(backup(1)) and not db.is_manual({"properties": {"backupType": "FULL", "source": "Automatic"}}) +def manual_backup(hours_ago, now=NOW): + """A PostgreSQL on-demand backup, labelled as Microsoft's published example labels it.""" + return {"name": f"m{hours_ago}", "properties": {"backupType": "Customer On-Demand", "source": "Customer Initiated", + "completedTime": iso(now - timedelta(hours=hours_ago))}} + + +def _kinds(engine, fixture): + return [db.backup_kind(engine, b) for b in json.loads((FIXTURES / fixture).read_text())["value"]] + + +def test_postgresql_backups_are_classified_from_microsofts_published_list(): + assert _kinds("postgresql", "postgresql-backups-2024-08-01.json") == [db.AUTOMATIC, db.MANUAL] + + +def test_mysql_automatic_backups_are_classified_from_microsofts_published_list(): + assert set(_kinds("mysql", "mysql-backups-2023-12-30.json")) == {db.AUTOMATIC} + + +def test_labels_are_matched_exactly_and_anything_else_is_unknown_never_automatic(): + def kind(engine, backup_type, source=None): + return db.backup_kind(engine, {"properties": {"backupType": backup_type, "source": source}}) + assert kind("postgresql", "FULL", "automatic") == db.AUTOMATIC # case does not matter + assert kind("postgresql", "Full") == db.AUTOMATIC # source absent on older API versions + # The old substring rules would have read these as automatic or manual; now they are unknown. + for engine, backup_type, source in (("postgresql", "Customer_On_Demand", None), ("postgresql", "Incremental", "Automatic"), + ("mysql", "FULL", "On-Demand"), ("mysql", "FULL", None), ("mysql", None, "Automatic"), + ("mysql", "FULL", "Customer Initiated")): + assert kind(engine, backup_type, source) == db.UNKNOWN, (engine, backup_type, source) + + +def test_an_unrecognized_backup_cannot_stand_in_for_broken_automation(): + odd = {"name": "odd", "properties": {"backupType": "FULL", "source": "On-Demand", "completedTime": iso(NOW - timedelta(hours=1))}} + s = db.with_backups(db.server_record("mysql", raw_server("my1", "mysql")), [backup(80), odd], SETTINGS, NOW) + assert (s["automatic_backups"], s["unknown_type_backups"], s["unknown_backup_labels"], s["backup_within_window"]) == (1, 1, ["FULL / On-Demand"], False) + summary = db.summarize([s], SETTINGS, NOW) + assert (summary["servers_with_automatic_backup_within_window"], summary["servers_with_unknown_backup_types"]) == (0, 1) def test_a_recent_manual_backup_does_not_hide_broken_automation(): From 8d666c7aff8cf0bc9d595dfddbbdea3c64aaafbb Mon Sep 17 00:00:00 2001 From: Bruce Weygandt Date: Tue, 17 Nov 2026 14:53:43 -0700 Subject: [PATCH 4/5] fix(azure_flexible_server_backups): a MySQL customer_ backup is manual whatever its labels --- .../azure/flexible_server_backups/fetcher.py | 10 +++++++++- .../azure/flexible_server_backups/fetcher.yaml | 2 +- .../fixtures/flexible_server_backups/README.md | 8 ++++++-- .../mysql-backup-on-demand-2023-12-30.json | 10 ++++++++++ tests/test_azure_flexible_server_backups.py | 18 ++++++++++++++++++ 5 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 tests/fixtures/flexible_server_backups/mysql-backup-on-demand-2023-12-30.json diff --git a/fetchers/azure/flexible_server_backups/fetcher.py b/fetchers/azure/flexible_server_backups/fetcher.py index 9a1171ab..ddf3b67a 100644 --- a/fetchers/azure/flexible_server_backups/fetcher.py +++ b/fetchers/azure/flexible_server_backups/fetcher.py @@ -86,9 +86,17 @@ def server_record(engine: str, raw: Dict[str, Any]) -> Dict[str, Any]: } +# MySQL labels an on-demand backup FULL / Automatic, the same as a scheduled one (Microsoft's BackupPut.json example, +# 2023-12-30); only the name, customer_