github_backup.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. """API routes for GitHub profile backup."""
  2. import logging
  3. from fastapi import APIRouter, Depends, HTTPException, Query
  4. from sqlalchemy import delete, desc, select
  5. from sqlalchemy.ext.asyncio import AsyncSession
  6. from backend.app.core.auth import RequirePermissionIfAuthEnabled
  7. from backend.app.core.database import get_db
  8. from backend.app.core.permissions import Permission
  9. from backend.app.models.github_backup import GitHubBackupConfig, GitHubBackupLog
  10. from backend.app.models.user import User
  11. from backend.app.schemas.github_backup import (
  12. REF_PATTERN,
  13. CloudAccountCounts,
  14. GitHubBackupConfigCreate,
  15. GitHubBackupConfigResponse,
  16. GitHubBackupConfigUpdate,
  17. GitHubBackupLogResponse,
  18. GitHubBackupStatus,
  19. GitHubBackupTriggerResponse,
  20. GitHubCommitListResponse,
  21. GitHubRestorePreview,
  22. GitHubRestoreRequest,
  23. GitHubRestoreResponse,
  24. GitHubTestConnectionResponse,
  25. ProviderType,
  26. RestoreCategory,
  27. )
  28. from backend.app.services.github_backup import github_backup_service
  29. from backend.app.services.github_restore import github_restore_service
  30. logger = logging.getLogger(__name__)
  31. router = APIRouter(prefix="/github-backup", tags=["github-backup"])
  32. _PUBLIC_REPO_ERROR = (
  33. "Refusing to save: the target repository is not private. Bambuddy backups "
  34. "include MQTT credentials, Home Assistant tokens, Prometheus tokens, your "
  35. "Bambu Cloud email, the printer access codes via K-profiles, and other "
  36. "settings that must not be exposed publicly. Make the repository private "
  37. "in your provider's UI and try again."
  38. )
  39. _UNKNOWN_VISIBILITY_ERROR = (
  40. "Refusing to save: could not confirm the target repository is private. "
  41. "Bambuddy backups contain credentials and must never go to a public or "
  42. "internal-visibility repository. Verify the URL, the access token's scope, "
  43. "and that your provider exposes the 'private' / 'visibility' field on its "
  44. "repo API."
  45. )
  46. async def _enforce_private_repo(repo_url: str, token: str, provider: str) -> None:
  47. """Run a test_connection and refuse if the repo is not confirmed private.
  48. Used by POST and PATCH /config so a backup configuration can never be
  49. saved against a public repository.
  50. The URL is policy-checked first: the Gitea and Forgejo backends derive
  51. their API base from this value (``get_api_base``) and then request it with
  52. the supplied token, so an unchecked repository_url is an outbound fetch to
  53. an operator-supplied host. A self-hosted Gitea on the LAN is the normal
  54. case, so the LAN-service tier applies — this only rules out the targets
  55. that are wrong under any topology.
  56. """
  57. from backend.app.api.routes._url_safety import assert_safe_lan_service_url
  58. try:
  59. assert_safe_lan_service_url(repo_url, label="Repository URL")
  60. except ValueError as exc:
  61. raise HTTPException(status_code=422, detail=str(exc)) from exc
  62. result = await github_backup_service.test_connection(repo_url, token, provider=provider)
  63. if not result.get("success"):
  64. message = result.get("message") or "Connection test failed"
  65. raise HTTPException(status_code=400, detail=f"Cannot verify repository: {message}")
  66. is_private = result.get("is_private")
  67. if is_private is None:
  68. raise HTTPException(status_code=400, detail=_UNKNOWN_VISIBILITY_ERROR)
  69. if is_private is False:
  70. raise HTTPException(status_code=400, detail=_PUBLIC_REPO_ERROR)
  71. async def _count_cloud_accounts(db: AsyncSession) -> tuple[int, int]:
  72. """How many Bambu / Orca accounts a backup would collect from.
  73. Asks the collector itself rather than re-deriving the rule, so the number
  74. the UI gates on can't drift from the number the backup actually uses
  75. (#2717). Counts only — never who.
  76. """
  77. try:
  78. bambu, orca = await github_backup_service.cloud_accounts(db)
  79. return len(bambu), len(orca)
  80. except Exception:
  81. # A settings page must still render when a credential store is
  82. # unreadable; the toggle simply shows as unavailable.
  83. logger.warning("Failed to count connected cloud accounts", exc_info=True)
  84. return 0, 0
  85. @router.get("/cloud-accounts", response_model=CloudAccountCounts)
  86. async def get_cloud_accounts(
  87. db: AsyncSession = Depends(get_db),
  88. _: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_BACKUP),
  89. ):
  90. """How many cloud accounts the Cloud Profiles category would collect from.
  91. Its own endpoint rather than a field on ``/config``, because the settings
  92. form needs this before any config exists — ``/config`` answers ``null``
  93. until the first save, which would leave the toggle disabled during the
  94. very setup it's part of.
  95. """
  96. bambu, orca = await _count_cloud_accounts(db)
  97. return CloudAccountCounts(bambu=bambu, orca=orca)
  98. def _config_to_response(config: GitHubBackupConfig) -> dict:
  99. """Convert config model to response dict."""
  100. return {
  101. "id": config.id,
  102. "repository_url": config.repository_url,
  103. "has_token": bool(config.access_token),
  104. "branch": config.branch,
  105. "provider": config.provider,
  106. "allow_insecure_http": config.allow_insecure_http,
  107. "schedule_enabled": config.schedule_enabled,
  108. "schedule_type": config.schedule_type,
  109. "backup_kprofiles": config.backup_kprofiles,
  110. "backup_cloud_profiles": config.backup_cloud_profiles,
  111. "backup_settings": config.backup_settings,
  112. "backup_spools": config.backup_spools,
  113. "backup_archives": config.backup_archives,
  114. "enabled": config.enabled,
  115. "last_backup_at": config.last_backup_at,
  116. "last_backup_status": config.last_backup_status,
  117. "last_backup_message": config.last_backup_message,
  118. "last_backup_commit_sha": config.last_backup_commit_sha,
  119. "next_scheduled_run": config.next_scheduled_run,
  120. "created_at": config.created_at,
  121. "updated_at": config.updated_at,
  122. }
  123. @router.get("/config", response_model=GitHubBackupConfigResponse | None)
  124. async def get_config(
  125. db: AsyncSession = Depends(get_db),
  126. _: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_BACKUP),
  127. ):
  128. """Get the current GitHub backup configuration."""
  129. result = await db.execute(select(GitHubBackupConfig).limit(1))
  130. config = result.scalar_one_or_none()
  131. if not config:
  132. return None
  133. return _config_to_response(config)
  134. @router.post("/config", response_model=GitHubBackupConfigResponse)
  135. async def save_config(
  136. config_data: GitHubBackupConfigCreate,
  137. db: AsyncSession = Depends(get_db),
  138. _: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_BACKUP),
  139. ):
  140. """Create or update GitHub backup configuration.
  141. Only one configuration is supported. If one exists, it will be updated.
  142. The target repository must be private — Bambuddy backups carry MQTT
  143. credentials, HA/Prometheus tokens, the Bambu Cloud email, and printer
  144. access codes (via K-profiles), so a public repo is a hard reject.
  145. """
  146. await _enforce_private_repo(
  147. config_data.repository_url,
  148. config_data.access_token,
  149. config_data.provider.value,
  150. )
  151. # Check for existing config
  152. result = await db.execute(select(GitHubBackupConfig).limit(1))
  153. config = result.scalar_one_or_none()
  154. if config:
  155. # Update existing
  156. config.repository_url = config_data.repository_url
  157. config.access_token = config_data.access_token
  158. config.branch = config_data.branch
  159. config.provider = config_data.provider.value
  160. config.schedule_enabled = config_data.schedule_enabled
  161. config.schedule_type = config_data.schedule_type.value
  162. config.backup_kprofiles = config_data.backup_kprofiles
  163. config.backup_cloud_profiles = config_data.backup_cloud_profiles
  164. config.backup_settings = config_data.backup_settings
  165. config.backup_spools = config_data.backup_spools
  166. config.backup_archives = config_data.backup_archives
  167. config.allow_insecure_http = config_data.allow_insecure_http
  168. config.enabled = config_data.enabled
  169. # Calculate next scheduled run if enabled
  170. if config.schedule_enabled:
  171. config.next_scheduled_run = github_backup_service.calculate_next_run(config.schedule_type)
  172. else:
  173. config.next_scheduled_run = None
  174. logger.info("Updated GitHub backup config: %s", config.repository_url)
  175. else:
  176. # Create new
  177. config = GitHubBackupConfig(
  178. repository_url=config_data.repository_url,
  179. access_token=config_data.access_token,
  180. branch=config_data.branch,
  181. provider=config_data.provider.value,
  182. schedule_enabled=config_data.schedule_enabled,
  183. schedule_type=config_data.schedule_type.value,
  184. backup_kprofiles=config_data.backup_kprofiles,
  185. backup_cloud_profiles=config_data.backup_cloud_profiles,
  186. backup_settings=config_data.backup_settings,
  187. backup_spools=config_data.backup_spools,
  188. backup_archives=config_data.backup_archives,
  189. allow_insecure_http=config_data.allow_insecure_http,
  190. enabled=config_data.enabled,
  191. )
  192. if config.schedule_enabled:
  193. config.next_scheduled_run = github_backup_service.calculate_next_run(config.schedule_type)
  194. db.add(config)
  195. logger.info("Created GitHub backup config: %s", config.repository_url)
  196. await db.commit()
  197. await db.refresh(config)
  198. return _config_to_response(config)
  199. @router.patch("/config", response_model=GitHubBackupConfigResponse)
  200. async def update_config(
  201. update_data: GitHubBackupConfigUpdate,
  202. db: AsyncSession = Depends(get_db),
  203. _: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_BACKUP),
  204. ):
  205. """Partially update GitHub backup configuration."""
  206. result = await db.execute(select(GitHubBackupConfig).limit(1))
  207. config = result.scalar_one_or_none()
  208. if not config:
  209. raise HTTPException(status_code=404, detail="No configuration found")
  210. update_dict = update_data.model_dump(exclude_unset=True)
  211. # Validate HTTP URL restriction when the URL policy is being changed. This avoids blocking unrelated autosaves
  212. # for legacy configs that already contain an HTTP URL.
  213. if "repository_url" in update_dict or "allow_insecure_http" in update_dict:
  214. url_to_check = update_dict.get("repository_url", config.repository_url)
  215. effective_allow_http = update_dict.get("allow_insecure_http", config.allow_insecure_http)
  216. if url_to_check and url_to_check.startswith("http://") and not effective_allow_http:
  217. raise HTTPException(
  218. status_code=422,
  219. detail="This URL uses HTTP instead of HTTPS. Enable 'Allow insecure HTTP' if your instance does not use TLS.",
  220. )
  221. # Re-verify the repo is private whenever the target changes — new URL,
  222. # new token, or new provider. We DON'T re-test on every unrelated PATCH
  223. # (e.g. toggling backup_archives) so flipping schedule settings doesn't
  224. # round-trip a live API call.
  225. target_changed = "repository_url" in update_dict or "access_token" in update_dict or "provider" in update_dict
  226. if target_changed:
  227. provider_value = update_dict.get("provider", config.provider)
  228. if hasattr(provider_value, "value"):
  229. provider_value = provider_value.value
  230. await _enforce_private_repo(
  231. update_dict.get("repository_url", config.repository_url),
  232. update_dict.get("access_token", config.access_token),
  233. provider_value,
  234. )
  235. for key, value in update_dict.items():
  236. if key in ("schedule_type", "provider") and value is not None:
  237. setattr(config, key, value.value)
  238. else:
  239. setattr(config, key, value)
  240. # Recalculate next scheduled run if schedule settings changed
  241. if "schedule_enabled" in update_dict or "schedule_type" in update_dict:
  242. if config.schedule_enabled:
  243. config.next_scheduled_run = github_backup_service.calculate_next_run(config.schedule_type)
  244. else:
  245. config.next_scheduled_run = None
  246. await db.commit()
  247. await db.refresh(config)
  248. logger.info("Updated GitHub backup config: %s", config.repository_url)
  249. return _config_to_response(config)
  250. @router.delete("/config")
  251. async def delete_config(
  252. db: AsyncSession = Depends(get_db),
  253. _: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_BACKUP),
  254. ):
  255. """Delete the GitHub backup configuration and all logs."""
  256. result = await db.execute(select(GitHubBackupConfig).limit(1))
  257. config = result.scalar_one_or_none()
  258. if not config:
  259. raise HTTPException(status_code=404, detail="No configuration found")
  260. await db.delete(config)
  261. await db.commit()
  262. logger.info("Deleted GitHub backup config")
  263. return {"message": "Configuration deleted"}
  264. @router.post("/test", response_model=GitHubTestConnectionResponse)
  265. async def test_connection(
  266. repo_url: str = Query(..., description="Repository URL"),
  267. token: str = Query(..., description="Personal Access Token"),
  268. provider: ProviderType = Query(default=ProviderType.GITHUB, description="Git provider key"),
  269. _: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_BACKUP),
  270. ):
  271. """Test Git provider connection with provided credentials."""
  272. result = await github_backup_service.test_connection(repo_url, token, provider=provider)
  273. return GitHubTestConnectionResponse(**result)
  274. @router.post("/test-stored", response_model=GitHubTestConnectionResponse)
  275. async def test_stored_connection(
  276. db: AsyncSession = Depends(get_db),
  277. _: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_BACKUP),
  278. ):
  279. """Test GitHub connection using stored configuration."""
  280. result = await db.execute(select(GitHubBackupConfig).limit(1))
  281. config = result.scalar_one_or_none()
  282. if not config:
  283. raise HTTPException(status_code=404, detail="No configuration found")
  284. if not config.access_token:
  285. raise HTTPException(status_code=400, detail="No access token configured")
  286. test_result = await github_backup_service.test_connection(
  287. config.repository_url,
  288. config.access_token,
  289. provider=config.provider,
  290. )
  291. return GitHubTestConnectionResponse(**test_result)
  292. @router.post("/run", response_model=GitHubBackupTriggerResponse)
  293. async def trigger_backup(
  294. db: AsyncSession = Depends(get_db),
  295. _: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_BACKUP),
  296. ):
  297. """Manually trigger a backup."""
  298. result = await db.execute(select(GitHubBackupConfig).limit(1))
  299. config = result.scalar_one_or_none()
  300. if not config:
  301. raise HTTPException(status_code=404, detail="No configuration found. Configure backup first.")
  302. if not config.enabled:
  303. raise HTTPException(status_code=400, detail="Backup is disabled")
  304. backup_result = await github_backup_service.run_backup(config.id, trigger="manual")
  305. return GitHubBackupTriggerResponse(**backup_result)
  306. @router.get("/status", response_model=GitHubBackupStatus)
  307. async def get_status(
  308. db: AsyncSession = Depends(get_db),
  309. _: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_BACKUP),
  310. ):
  311. """Get current backup status."""
  312. result = await db.execute(select(GitHubBackupConfig).limit(1))
  313. config = result.scalar_one_or_none()
  314. if not config:
  315. return GitHubBackupStatus(
  316. configured=False,
  317. enabled=False,
  318. is_running=False,
  319. progress=None,
  320. last_backup_at=None,
  321. last_backup_status=None,
  322. next_scheduled_run=None,
  323. )
  324. return GitHubBackupStatus(
  325. configured=True,
  326. enabled=config.enabled,
  327. is_running=github_backup_service.is_running,
  328. restore_running=github_restore_service.is_running,
  329. progress=github_backup_service.progress or github_restore_service.progress,
  330. last_backup_at=config.last_backup_at,
  331. last_backup_status=config.last_backup_status,
  332. next_scheduled_run=config.next_scheduled_run,
  333. )
  334. @router.get("/commits", response_model=GitHubCommitListResponse)
  335. async def list_commits(
  336. limit: int = Query(default=20, ge=1, le=100),
  337. db: AsyncSession = Depends(get_db),
  338. _: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_RESTORE),
  339. ):
  340. """List recent backup commits so the user can pick one to restore from."""
  341. result = await db.execute(select(GitHubBackupConfig).limit(1))
  342. config = result.scalar_one_or_none()
  343. if not config:
  344. raise HTTPException(status_code=404, detail="No configuration found. Configure backup first.")
  345. commit_result = await github_restore_service.list_commits(config, limit=limit)
  346. return GitHubCommitListResponse(**commit_result)
  347. @router.get("/restore/preview", response_model=GitHubRestorePreview)
  348. async def preview_restore(
  349. ref: str = Query(default="HEAD", pattern=REF_PATTERN),
  350. db: AsyncSession = Depends(get_db),
  351. _: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_RESTORE),
  352. ):
  353. """Report which categories a given backup commit contains."""
  354. result = await db.execute(select(GitHubBackupConfig).limit(1))
  355. config = result.scalar_one_or_none()
  356. if not config:
  357. raise HTTPException(status_code=404, detail="No configuration found. Configure backup first.")
  358. preview = await github_restore_service.preview(db, config, ref=ref)
  359. return GitHubRestorePreview(**preview)
  360. @router.post("/restore", response_model=GitHubRestoreResponse)
  361. async def restore_backup(
  362. request: GitHubRestoreRequest,
  363. db: AsyncSession = Depends(get_db),
  364. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_RESTORE),
  365. ):
  366. """Restore selected categories from one backup commit.
  367. Note there is no private-repo gate here, unlike the config endpoints: that
  368. check exists to stop credentials leaving the instance, and this path only
  369. reads. A config can only be saved against a private repo anyway.
  370. The settings category needs ``settings:update`` as well — see the check
  371. below.
  372. """
  373. if RestoreCategory.SETTINGS in request.categories and current_user is not None:
  374. # The settings category rewrites arbitrary non-auth Settings rows, which
  375. # is what PUT /api/v1/settings/ gates on settings:update. Backup and
  376. # Settings are separate permission groups, so a role holding only Backup
  377. # could otherwise change settings it cannot change through the endpoint
  378. # that owns them. This module already makes that argument — it is why
  379. # the four protected auth keys are refused outright — so the gap was an
  380. # inconsistency in ours, not a new policy.
  381. #
  382. # current_user is None only when auth is disabled: github:restore is in
  383. # _APIKEY_DENIED_PERMISSIONS, so an API key never gets past the
  384. # dependency to reach this line.
  385. if not current_user.has_all_permissions(Permission.SETTINGS_UPDATE.value):
  386. raise HTTPException(
  387. status_code=403,
  388. detail=f"Missing required permissions: {Permission.SETTINGS_UPDATE.value}",
  389. )
  390. result = await db.execute(select(GitHubBackupConfig).limit(1))
  391. config = result.scalar_one_or_none()
  392. if not config:
  393. raise HTTPException(status_code=404, detail="No configuration found. Configure backup first.")
  394. restore_result = await github_restore_service.run_restore(
  395. config.id,
  396. ref=request.ref,
  397. categories=request.categories,
  398. overwrite_existing=request.overwrite_existing,
  399. )
  400. return GitHubRestoreResponse(**restore_result)
  401. @router.get("/logs", response_model=list[GitHubBackupLogResponse])
  402. async def get_logs(
  403. limit: int = Query(default=50, ge=1, le=200),
  404. offset: int = Query(default=0, ge=0),
  405. db: AsyncSession = Depends(get_db),
  406. _: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_BACKUP),
  407. ):
  408. """Get backup logs."""
  409. result = await db.execute(select(GitHubBackupConfig).limit(1))
  410. config = result.scalar_one_or_none()
  411. if not config:
  412. return []
  413. logs_result = await db.execute(
  414. select(GitHubBackupLog)
  415. .where(GitHubBackupLog.config_id == config.id)
  416. .order_by(desc(GitHubBackupLog.started_at))
  417. .offset(offset)
  418. .limit(limit)
  419. )
  420. logs = logs_result.scalars().all()
  421. return [
  422. GitHubBackupLogResponse(
  423. id=log.id,
  424. config_id=log.config_id,
  425. started_at=log.started_at,
  426. completed_at=log.completed_at,
  427. status=log.status,
  428. trigger=log.trigger,
  429. commit_sha=log.commit_sha,
  430. files_changed=log.files_changed,
  431. error_message=log.error_message,
  432. )
  433. for log in logs
  434. ]
  435. @router.delete("/logs")
  436. async def clear_logs(
  437. keep_last: int = Query(default=10, ge=0, le=100, description="Number of recent logs to keep"),
  438. db: AsyncSession = Depends(get_db),
  439. _: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_BACKUP),
  440. ):
  441. """Clear backup logs, optionally keeping the most recent entries."""
  442. result = await db.execute(select(GitHubBackupConfig).limit(1))
  443. config = result.scalar_one_or_none()
  444. if not config:
  445. return {"deleted": 0, "message": "No configuration found"}
  446. if keep_last > 0:
  447. # Get IDs to keep
  448. keep_result = await db.execute(
  449. select(GitHubBackupLog.id)
  450. .where(GitHubBackupLog.config_id == config.id)
  451. .order_by(desc(GitHubBackupLog.started_at))
  452. .limit(keep_last)
  453. )
  454. keep_ids = [row[0] for row in keep_result.fetchall()]
  455. if keep_ids:
  456. delete_result = await db.execute(
  457. delete(GitHubBackupLog).where(
  458. GitHubBackupLog.config_id == config.id, GitHubBackupLog.id.not_in(keep_ids)
  459. )
  460. )
  461. else:
  462. delete_result = await db.execute(delete(GitHubBackupLog).where(GitHubBackupLog.config_id == config.id))
  463. else:
  464. delete_result = await db.execute(delete(GitHubBackupLog).where(GitHubBackupLog.config_id == config.id))
  465. await db.commit()
  466. deleted_count = delete_result.rowcount
  467. logger.info("Deleted %s GitHub backup logs (kept %s)", deleted_count, keep_last)
  468. return {"deleted": deleted_count, "message": f"Deleted {deleted_count} logs"}