github_backup.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. GitHubBackupConfigCreate,
  13. GitHubBackupConfigResponse,
  14. GitHubBackupConfigUpdate,
  15. GitHubBackupLogResponse,
  16. GitHubBackupStatus,
  17. GitHubBackupTriggerResponse,
  18. GitHubTestConnectionResponse,
  19. ProviderType,
  20. )
  21. from backend.app.services.github_backup import github_backup_service
  22. logger = logging.getLogger(__name__)
  23. router = APIRouter(prefix="/github-backup", tags=["github-backup"])
  24. _PUBLIC_REPO_ERROR = (
  25. "Refusing to save: the target repository is not private. Bambuddy backups "
  26. "include MQTT credentials, Home Assistant tokens, Prometheus tokens, your "
  27. "Bambu Cloud email, the printer access codes via K-profiles, and other "
  28. "settings that must not be exposed publicly. Make the repository private "
  29. "in your provider's UI and try again."
  30. )
  31. _UNKNOWN_VISIBILITY_ERROR = (
  32. "Refusing to save: could not confirm the target repository is private. "
  33. "Bambuddy backups contain credentials and must never go to a public or "
  34. "internal-visibility repository. Verify the URL, the access token's scope, "
  35. "and that your provider exposes the 'private' / 'visibility' field on its "
  36. "repo API."
  37. )
  38. async def _enforce_private_repo(repo_url: str, token: str, provider: str) -> None:
  39. """Run a test_connection and refuse if the repo is not confirmed private.
  40. Used by POST and PATCH /config so a backup configuration can never be
  41. saved against a public repository.
  42. """
  43. result = await github_backup_service.test_connection(repo_url, token, provider=provider)
  44. if not result.get("success"):
  45. message = result.get("message") or "Connection test failed"
  46. raise HTTPException(status_code=400, detail=f"Cannot verify repository: {message}")
  47. is_private = result.get("is_private")
  48. if is_private is None:
  49. raise HTTPException(status_code=400, detail=_UNKNOWN_VISIBILITY_ERROR)
  50. if is_private is False:
  51. raise HTTPException(status_code=400, detail=_PUBLIC_REPO_ERROR)
  52. def _config_to_response(config: GitHubBackupConfig) -> dict:
  53. """Convert config model to response dict."""
  54. return {
  55. "id": config.id,
  56. "repository_url": config.repository_url,
  57. "has_token": bool(config.access_token),
  58. "branch": config.branch,
  59. "provider": config.provider,
  60. "allow_insecure_http": config.allow_insecure_http,
  61. "schedule_enabled": config.schedule_enabled,
  62. "schedule_type": config.schedule_type,
  63. "backup_kprofiles": config.backup_kprofiles,
  64. "backup_cloud_profiles": config.backup_cloud_profiles,
  65. "backup_settings": config.backup_settings,
  66. "backup_spools": config.backup_spools,
  67. "backup_archives": config.backup_archives,
  68. "enabled": config.enabled,
  69. "last_backup_at": config.last_backup_at,
  70. "last_backup_status": config.last_backup_status,
  71. "last_backup_message": config.last_backup_message,
  72. "last_backup_commit_sha": config.last_backup_commit_sha,
  73. "next_scheduled_run": config.next_scheduled_run,
  74. "created_at": config.created_at,
  75. "updated_at": config.updated_at,
  76. }
  77. @router.get("/config", response_model=GitHubBackupConfigResponse | None)
  78. async def get_config(
  79. db: AsyncSession = Depends(get_db),
  80. _: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_BACKUP),
  81. ):
  82. """Get the current GitHub backup configuration."""
  83. result = await db.execute(select(GitHubBackupConfig).limit(1))
  84. config = result.scalar_one_or_none()
  85. if not config:
  86. return None
  87. return _config_to_response(config)
  88. @router.post("/config", response_model=GitHubBackupConfigResponse)
  89. async def save_config(
  90. config_data: GitHubBackupConfigCreate,
  91. db: AsyncSession = Depends(get_db),
  92. _: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_BACKUP),
  93. ):
  94. """Create or update GitHub backup configuration.
  95. Only one configuration is supported. If one exists, it will be updated.
  96. The target repository must be private — Bambuddy backups carry MQTT
  97. credentials, HA/Prometheus tokens, the Bambu Cloud email, and printer
  98. access codes (via K-profiles), so a public repo is a hard reject.
  99. """
  100. await _enforce_private_repo(
  101. config_data.repository_url,
  102. config_data.access_token,
  103. config_data.provider.value,
  104. )
  105. # Check for existing config
  106. result = await db.execute(select(GitHubBackupConfig).limit(1))
  107. config = result.scalar_one_or_none()
  108. if config:
  109. # Update existing
  110. config.repository_url = config_data.repository_url
  111. config.access_token = config_data.access_token
  112. config.branch = config_data.branch
  113. config.provider = config_data.provider.value
  114. config.schedule_enabled = config_data.schedule_enabled
  115. config.schedule_type = config_data.schedule_type.value
  116. config.backup_kprofiles = config_data.backup_kprofiles
  117. config.backup_cloud_profiles = config_data.backup_cloud_profiles
  118. config.backup_settings = config_data.backup_settings
  119. config.backup_spools = config_data.backup_spools
  120. config.backup_archives = config_data.backup_archives
  121. config.allow_insecure_http = config_data.allow_insecure_http
  122. config.enabled = config_data.enabled
  123. # Calculate next scheduled run if enabled
  124. if config.schedule_enabled:
  125. config.next_scheduled_run = github_backup_service.calculate_next_run(config.schedule_type)
  126. else:
  127. config.next_scheduled_run = None
  128. logger.info("Updated GitHub backup config: %s", config.repository_url)
  129. else:
  130. # Create new
  131. config = GitHubBackupConfig(
  132. repository_url=config_data.repository_url,
  133. access_token=config_data.access_token,
  134. branch=config_data.branch,
  135. provider=config_data.provider.value,
  136. schedule_enabled=config_data.schedule_enabled,
  137. schedule_type=config_data.schedule_type.value,
  138. backup_kprofiles=config_data.backup_kprofiles,
  139. backup_cloud_profiles=config_data.backup_cloud_profiles,
  140. backup_settings=config_data.backup_settings,
  141. backup_spools=config_data.backup_spools,
  142. backup_archives=config_data.backup_archives,
  143. allow_insecure_http=config_data.allow_insecure_http,
  144. enabled=config_data.enabled,
  145. )
  146. if config.schedule_enabled:
  147. config.next_scheduled_run = github_backup_service.calculate_next_run(config.schedule_type)
  148. db.add(config)
  149. logger.info("Created GitHub backup config: %s", config.repository_url)
  150. await db.commit()
  151. await db.refresh(config)
  152. return _config_to_response(config)
  153. @router.patch("/config", response_model=GitHubBackupConfigResponse)
  154. async def update_config(
  155. update_data: GitHubBackupConfigUpdate,
  156. db: AsyncSession = Depends(get_db),
  157. _: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_BACKUP),
  158. ):
  159. """Partially update GitHub backup configuration."""
  160. result = await db.execute(select(GitHubBackupConfig).limit(1))
  161. config = result.scalar_one_or_none()
  162. if not config:
  163. raise HTTPException(status_code=404, detail="No configuration found")
  164. update_dict = update_data.model_dump(exclude_unset=True)
  165. # Validate HTTP URL restriction when the URL policy is being changed. This avoids blocking unrelated autosaves
  166. # for legacy configs that already contain an HTTP URL.
  167. if "repository_url" in update_dict or "allow_insecure_http" in update_dict:
  168. url_to_check = update_dict.get("repository_url", config.repository_url)
  169. effective_allow_http = update_dict.get("allow_insecure_http", config.allow_insecure_http)
  170. if url_to_check and url_to_check.startswith("http://") and not effective_allow_http:
  171. raise HTTPException(
  172. status_code=422,
  173. detail="This URL uses HTTP instead of HTTPS. Enable 'Allow insecure HTTP' if your instance does not use TLS.",
  174. )
  175. # Re-verify the repo is private whenever the target changes — new URL,
  176. # new token, or new provider. We DON'T re-test on every unrelated PATCH
  177. # (e.g. toggling backup_archives) so flipping schedule settings doesn't
  178. # round-trip a live API call.
  179. target_changed = "repository_url" in update_dict or "access_token" in update_dict or "provider" in update_dict
  180. if target_changed:
  181. provider_value = update_dict.get("provider", config.provider)
  182. if hasattr(provider_value, "value"):
  183. provider_value = provider_value.value
  184. await _enforce_private_repo(
  185. update_dict.get("repository_url", config.repository_url),
  186. update_dict.get("access_token", config.access_token),
  187. provider_value,
  188. )
  189. for key, value in update_dict.items():
  190. if key in ("schedule_type", "provider") and value is not None:
  191. setattr(config, key, value.value)
  192. else:
  193. setattr(config, key, value)
  194. # Recalculate next scheduled run if schedule settings changed
  195. if "schedule_enabled" in update_dict or "schedule_type" in update_dict:
  196. if config.schedule_enabled:
  197. config.next_scheduled_run = github_backup_service.calculate_next_run(config.schedule_type)
  198. else:
  199. config.next_scheduled_run = None
  200. await db.commit()
  201. await db.refresh(config)
  202. logger.info("Updated GitHub backup config: %s", config.repository_url)
  203. return _config_to_response(config)
  204. @router.delete("/config")
  205. async def delete_config(
  206. db: AsyncSession = Depends(get_db),
  207. _: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_BACKUP),
  208. ):
  209. """Delete the GitHub backup configuration and all logs."""
  210. result = await db.execute(select(GitHubBackupConfig).limit(1))
  211. config = result.scalar_one_or_none()
  212. if not config:
  213. raise HTTPException(status_code=404, detail="No configuration found")
  214. await db.delete(config)
  215. await db.commit()
  216. logger.info("Deleted GitHub backup config")
  217. return {"message": "Configuration deleted"}
  218. @router.post("/test", response_model=GitHubTestConnectionResponse)
  219. async def test_connection(
  220. repo_url: str = Query(..., description="Repository URL"),
  221. token: str = Query(..., description="Personal Access Token"),
  222. provider: ProviderType = Query(default=ProviderType.GITHUB, description="Git provider key"),
  223. _: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_BACKUP),
  224. ):
  225. """Test Git provider connection with provided credentials."""
  226. result = await github_backup_service.test_connection(repo_url, token, provider=provider)
  227. return GitHubTestConnectionResponse(**result)
  228. @router.post("/test-stored", response_model=GitHubTestConnectionResponse)
  229. async def test_stored_connection(
  230. db: AsyncSession = Depends(get_db),
  231. _: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_BACKUP),
  232. ):
  233. """Test GitHub connection using stored configuration."""
  234. result = await db.execute(select(GitHubBackupConfig).limit(1))
  235. config = result.scalar_one_or_none()
  236. if not config:
  237. raise HTTPException(status_code=404, detail="No configuration found")
  238. if not config.access_token:
  239. raise HTTPException(status_code=400, detail="No access token configured")
  240. test_result = await github_backup_service.test_connection(
  241. config.repository_url,
  242. config.access_token,
  243. provider=config.provider,
  244. )
  245. return GitHubTestConnectionResponse(**test_result)
  246. @router.post("/run", response_model=GitHubBackupTriggerResponse)
  247. async def trigger_backup(
  248. db: AsyncSession = Depends(get_db),
  249. _: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_BACKUP),
  250. ):
  251. """Manually trigger a backup."""
  252. result = await db.execute(select(GitHubBackupConfig).limit(1))
  253. config = result.scalar_one_or_none()
  254. if not config:
  255. raise HTTPException(status_code=404, detail="No configuration found. Configure backup first.")
  256. if not config.enabled:
  257. raise HTTPException(status_code=400, detail="Backup is disabled")
  258. backup_result = await github_backup_service.run_backup(config.id, trigger="manual")
  259. return GitHubBackupTriggerResponse(**backup_result)
  260. @router.get("/status", response_model=GitHubBackupStatus)
  261. async def get_status(
  262. db: AsyncSession = Depends(get_db),
  263. _: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_BACKUP),
  264. ):
  265. """Get current backup status."""
  266. result = await db.execute(select(GitHubBackupConfig).limit(1))
  267. config = result.scalar_one_or_none()
  268. if not config:
  269. return GitHubBackupStatus(
  270. configured=False,
  271. enabled=False,
  272. is_running=False,
  273. progress=None,
  274. last_backup_at=None,
  275. last_backup_status=None,
  276. next_scheduled_run=None,
  277. )
  278. return GitHubBackupStatus(
  279. configured=True,
  280. enabled=config.enabled,
  281. is_running=github_backup_service.is_running,
  282. progress=github_backup_service.progress,
  283. last_backup_at=config.last_backup_at,
  284. last_backup_status=config.last_backup_status,
  285. next_scheduled_run=config.next_scheduled_run,
  286. )
  287. @router.get("/logs", response_model=list[GitHubBackupLogResponse])
  288. async def get_logs(
  289. limit: int = Query(default=50, ge=1, le=200),
  290. offset: int = Query(default=0, ge=0),
  291. db: AsyncSession = Depends(get_db),
  292. _: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_BACKUP),
  293. ):
  294. """Get backup logs."""
  295. result = await db.execute(select(GitHubBackupConfig).limit(1))
  296. config = result.scalar_one_or_none()
  297. if not config:
  298. return []
  299. logs_result = await db.execute(
  300. select(GitHubBackupLog)
  301. .where(GitHubBackupLog.config_id == config.id)
  302. .order_by(desc(GitHubBackupLog.started_at))
  303. .offset(offset)
  304. .limit(limit)
  305. )
  306. logs = logs_result.scalars().all()
  307. return [
  308. GitHubBackupLogResponse(
  309. id=log.id,
  310. config_id=log.config_id,
  311. started_at=log.started_at,
  312. completed_at=log.completed_at,
  313. status=log.status,
  314. trigger=log.trigger,
  315. commit_sha=log.commit_sha,
  316. files_changed=log.files_changed,
  317. error_message=log.error_message,
  318. )
  319. for log in logs
  320. ]
  321. @router.delete("/logs")
  322. async def clear_logs(
  323. keep_last: int = Query(default=10, ge=0, le=100, description="Number of recent logs to keep"),
  324. db: AsyncSession = Depends(get_db),
  325. _: User | None = RequirePermissionIfAuthEnabled(Permission.GITHUB_BACKUP),
  326. ):
  327. """Clear backup logs, optionally keeping the most recent entries."""
  328. result = await db.execute(select(GitHubBackupConfig).limit(1))
  329. config = result.scalar_one_or_none()
  330. if not config:
  331. return {"deleted": 0, "message": "No configuration found"}
  332. if keep_last > 0:
  333. # Get IDs to keep
  334. keep_result = await db.execute(
  335. select(GitHubBackupLog.id)
  336. .where(GitHubBackupLog.config_id == config.id)
  337. .order_by(desc(GitHubBackupLog.started_at))
  338. .limit(keep_last)
  339. )
  340. keep_ids = [row[0] for row in keep_result.fetchall()]
  341. if keep_ids:
  342. delete_result = await db.execute(
  343. delete(GitHubBackupLog).where(
  344. GitHubBackupLog.config_id == config.id, GitHubBackupLog.id.not_in(keep_ids)
  345. )
  346. )
  347. else:
  348. delete_result = await db.execute(delete(GitHubBackupLog).where(GitHubBackupLog.config_id == config.id))
  349. else:
  350. delete_result = await db.execute(delete(GitHubBackupLog).where(GitHubBackupLog.config_id == config.id))
  351. await db.commit()
  352. deleted_count = delete_result.rowcount
  353. logger.info("Deleted %s GitHub backup logs (kept %s)", deleted_count, keep_last)
  354. return {"deleted": deleted_count, "message": f"Deleted {deleted_count} logs"}