diff --git a/CHANGELOG.md b/CHANGELOG.md index 330ddf7a..3c421455 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,12 @@ 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, 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. - **`azure_velero_backups` fetcher.** Reads Velero backup storage locations in Azure Blob Storage, from Azure APIs only: each backup's phase, schedule, completion time and expiry, missed runs of configured expected schedules, schedules past the recovery point objective, each restore's result, and whether the backup diff --git a/fetchers/azure/flexible_server_backups/fetcher.py b/fetchers/azure/flexible_server_backups/fetcher.py new file mode 100644 index 00000000..04fa29fc --- /dev/null +++ b/fetchers/azure/flexible_server_backups/fetcher.py @@ -0,0 +1,262 @@ +#!/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_flexible_server_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_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 + + +# --- 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 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 {} + 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"), + # 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"), + "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"), + } + + +# 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}, +} + + +# 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_