settings.py 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  1. import io
  2. import logging
  3. import os
  4. import zipfile
  5. from datetime import datetime
  6. from pathlib import Path
  7. from fastapi import APIRouter, Depends, File, UploadFile
  8. from fastapi.responses import FileResponse, JSONResponse
  9. from pydantic import BaseModel, Field
  10. from sqlalchemy import delete, select
  11. from sqlalchemy.ext.asyncio import AsyncSession
  12. from backend.app.core.auth import RequirePermissionIfAuthEnabled, caller_is_api_key, require_energy_cost_update
  13. from backend.app.core.config import settings as app_settings
  14. from backend.app.core.database import get_db
  15. from backend.app.core.permissions import Permission
  16. from backend.app.models.settings import Settings
  17. from backend.app.models.user import User
  18. from backend.app.schemas.settings import AppSettings, AppSettingsUpdate
  19. logger = logging.getLogger(__name__)
  20. router = APIRouter(prefix="/settings", tags=["settings"])
  21. DEFAULT_SETTINGS = AppSettings()
  22. # Sensitive credential fields blanked for API-key callers
  23. _SENSITIVE_FIELDS_FOR_API_KEY = (
  24. "mqtt_password",
  25. "ha_token",
  26. "prometheus_token",
  27. "virtual_printer_access_code",
  28. "ldap_bind_password",
  29. )
  30. def _sqlalchemy_type_to_sqlite_type(type_repr: str) -> str:
  31. """Map a SQLAlchemy column type's ``str()`` to a SQLite-native column type.
  32. Used by ``create_backup_zip`` to reconstruct a portable SQLite database
  33. file from PostgreSQL data. Falling through to TEXT for binary columns
  34. corrupts non-UTF8 bytes — the BLOB branch is the #1333 regression guard
  35. for OIDC icon BLOBs.
  36. Extracted as a pure helper so it can be unit-tested without spinning up
  37. the full FastAPI app + backup pipeline.
  38. """
  39. type_str = type_repr.upper()
  40. if "INT" in type_str:
  41. return "INTEGER"
  42. if "FLOAT" in type_str or "REAL" in type_str or "NUMERIC" in type_str:
  43. return "REAL"
  44. if "BOOL" in type_str:
  45. return "BOOLEAN"
  46. if "BLOB" in type_str or "BYTEA" in type_str or "BINARY" in type_str:
  47. # OIDC icon BLOB column (#1333) — without this branch the column
  48. # was created as TEXT and non-UTF8 bytes were corrupted during the
  49. # PG→SQLite-ZIP backup round trip.
  50. return "BLOB"
  51. return "TEXT"
  52. async def get_setting(db: AsyncSession, key: str) -> str | None:
  53. """Get a single setting value by key."""
  54. result = await db.execute(select(Settings).where(Settings.key == key))
  55. setting = result.scalar_one_or_none()
  56. return setting.value if setting else None
  57. async def get_external_login_url(db: AsyncSession) -> str:
  58. """Get the external URL for the login page.
  59. Uses external_url from settings if available, otherwise falls back to APP_URL env var.
  60. Args:
  61. db: Database session
  62. Returns:
  63. Full URL to the login page
  64. """
  65. import os
  66. external_url = await get_setting(db, "external_url")
  67. if external_url:
  68. external_url = external_url.rstrip("/")
  69. else:
  70. external_url = os.environ.get("APP_URL", "http://localhost:5173")
  71. return external_url + "/login"
  72. async def set_setting(db: AsyncSession, key: str, value: str) -> None:
  73. """Set a single setting value."""
  74. from backend.app.core.db_dialect import upsert_setting
  75. await upsert_setting(db, Settings, key, value)
  76. async def _build_settings_response(db: AsyncSession, is_api_key: bool = False) -> AppSettings:
  77. """Build the full settings response, scrubbing secrets for API-key callers."""
  78. settings_dict = DEFAULT_SETTINGS.model_dump()
  79. result = await db.execute(select(Settings))
  80. for setting in result.scalars().all():
  81. if setting.key not in settings_dict:
  82. continue
  83. if setting.key in [
  84. "auto_archive",
  85. "save_thumbnails",
  86. "capture_finish_photo",
  87. "spoolman_enabled",
  88. "spoolman_disable_weight_sync",
  89. "spoolman_report_partial_usage",
  90. "disable_filament_warnings",
  91. "prefer_lowest_filament",
  92. "check_updates",
  93. "check_printer_firmware",
  94. "include_beta_updates",
  95. "virtual_printer_enabled",
  96. "ftp_retry_enabled",
  97. "mqtt_enabled",
  98. "mqtt_use_tls",
  99. "ha_enabled",
  100. "per_printer_mapping_expanded",
  101. "prometheus_enabled",
  102. "user_notifications_enabled",
  103. "queue_drying_enabled",
  104. "queue_drying_block",
  105. "ambient_drying_enabled",
  106. "require_plate_clear",
  107. "queue_shortest_first",
  108. "default_bed_levelling",
  109. "default_flow_cali",
  110. "default_vibration_cali",
  111. "default_layer_inspect",
  112. "default_timelapse",
  113. "ldap_enabled",
  114. "ldap_auto_provision",
  115. ]:
  116. settings_dict[setting.key] = setting.value.lower() == "true"
  117. elif setting.key in [
  118. "default_filament_cost",
  119. "energy_cost_per_kwh",
  120. "ams_temp_good",
  121. "ams_temp_fair",
  122. "library_disk_warning_gb",
  123. "low_stock_threshold",
  124. ]:
  125. settings_dict[setting.key] = float(setting.value)
  126. elif setting.key in [
  127. "ams_humidity_good",
  128. "ams_humidity_fair",
  129. "ams_history_retention_days",
  130. "ftp_retry_count",
  131. "ftp_retry_delay",
  132. "ftp_timeout",
  133. "mqtt_port",
  134. "stagger_group_size",
  135. "stagger_interval_minutes",
  136. "forecast_global_lead_time_days",
  137. ]:
  138. settings_dict[setting.key] = int(setting.value)
  139. elif setting.key == "default_printer_id":
  140. settings_dict[setting.key] = int(setting.value) if setting.value and setting.value != "None" else None
  141. else:
  142. settings_dict[setting.key] = setting.value
  143. ha_settings = await get_homeassistant_settings(db)
  144. settings_dict.update(ha_settings)
  145. # ldap_bind_password is never returned to any caller
  146. settings_dict["ldap_bind_password"] = ""
  147. if is_api_key:
  148. for field in _SENSITIVE_FIELDS_FOR_API_KEY:
  149. if field in settings_dict:
  150. settings_dict[field] = ""
  151. return AppSettings(**settings_dict)
  152. @router.get("", response_model=AppSettings)
  153. @router.get("/", response_model=AppSettings)
  154. async def get_settings(
  155. db: AsyncSession = Depends(get_db),
  156. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  157. _is_api_key: bool = Depends(caller_is_api_key),
  158. ):
  159. """Get all application settings."""
  160. return await _build_settings_response(db, is_api_key=_is_api_key)
  161. @router.put("/", response_model=AppSettings)
  162. async def update_settings(
  163. settings_update: AppSettingsUpdate,
  164. db: AsyncSession = Depends(get_db),
  165. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  166. ):
  167. """Update application settings."""
  168. update_data = settings_update.model_dump(exclude_unset=True)
  169. # Check if any MQTT settings are being updated
  170. mqtt_keys = {
  171. "mqtt_enabled",
  172. "mqtt_broker",
  173. "mqtt_port",
  174. "mqtt_username",
  175. "mqtt_password",
  176. "mqtt_topic_prefix",
  177. "mqtt_use_tls",
  178. }
  179. mqtt_updated = bool(mqtt_keys & set(update_data.keys()))
  180. for key, value in update_data.items():
  181. # Convert value to string for storage
  182. if isinstance(value, bool):
  183. str_value = "true" if value else "false"
  184. elif value is None:
  185. str_value = "None"
  186. else:
  187. str_value = str(value)
  188. await set_setting(db, key, str_value)
  189. await db.commit()
  190. # Expire all objects to ensure fresh reads after commit
  191. db.expire_all()
  192. # Reconfigure MQTT relay if any MQTT settings changed
  193. if mqtt_updated:
  194. try:
  195. from backend.app.services.mqtt_relay import mqtt_relay
  196. mqtt_settings = {
  197. "mqtt_enabled": (await get_setting(db, "mqtt_enabled") or "false") == "true",
  198. "mqtt_broker": await get_setting(db, "mqtt_broker") or "",
  199. "mqtt_port": int(await get_setting(db, "mqtt_port") or "1883"),
  200. "mqtt_username": await get_setting(db, "mqtt_username") or "",
  201. "mqtt_password": await get_setting(db, "mqtt_password") or "",
  202. "mqtt_topic_prefix": await get_setting(db, "mqtt_topic_prefix") or "bambuddy",
  203. "mqtt_use_tls": (await get_setting(db, "mqtt_use_tls") or "false") == "true",
  204. }
  205. await mqtt_relay.configure(mqtt_settings)
  206. except Exception:
  207. pass # Don't fail the settings update if MQTT reconfiguration fails
  208. # Return updated settings (never scrub secrets on PUT — caller has SETTINGS_UPDATE permission)
  209. return await _build_settings_response(db, is_api_key=False)
  210. @router.patch("/", response_model=AppSettings)
  211. @router.patch("", response_model=AppSettings)
  212. async def patch_settings(
  213. settings_update: AppSettingsUpdate,
  214. db: AsyncSession = Depends(get_db),
  215. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  216. ):
  217. """Partially update application settings (same as PUT, for REST compatibility)."""
  218. return await update_settings(settings_update, db, _)
  219. class ElectricityPriceUpdate(BaseModel):
  220. """Payload for ``POST /settings/electricity-price`` (#1356).
  221. Mirrors the field name documented in ``wiki/features/energy.md`` so the
  222. Home Assistant ``rest_command`` example needs only a URL change, not a
  223. payload change. Plain non-negative float; tariffs can go as low as 0.0 in
  224. some markets (e.g. free hours).
  225. """
  226. energy_cost_per_kwh: float = Field(ge=0)
  227. @router.post("/electricity-price", response_model=AppSettings)
  228. async def update_electricity_price(
  229. payload: ElectricityPriceUpdate,
  230. db: AsyncSession = Depends(get_db),
  231. _: User | None = Depends(require_energy_cost_update()),
  232. _is_api_key: bool = Depends(caller_is_api_key),
  233. ):
  234. """Update the per-kWh electricity cost used by the energy-tracking pipeline.
  235. This is the only settings field writable via API key, gated by the
  236. ``can_update_energy_cost`` toggle on the key. JWT users still need the
  237. standard ``SETTINGS_UPDATE`` permission. See #1356 for the rationale —
  238. the general ``PATCH /settings`` route remains denied for API keys because
  239. it can rewrite SMTP/LDAP/MQTT credentials, which is a much wider surface
  240. than the documented dynamic-tariff use case requires.
  241. """
  242. await set_setting(db, "energy_cost_per_kwh", str(payload.energy_cost_per_kwh))
  243. await db.commit()
  244. db.expire_all()
  245. return await _build_settings_response(db, is_api_key=_is_api_key)
  246. @router.post("/reset", response_model=AppSettings)
  247. async def reset_settings(
  248. db: AsyncSession = Depends(get_db),
  249. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  250. ):
  251. """Reset all settings to defaults."""
  252. # Delete all settings
  253. result = await db.execute(select(Settings))
  254. for setting in result.scalars().all():
  255. await db.delete(setting)
  256. await db.commit()
  257. return DEFAULT_SETTINGS
  258. @router.get("/default-sidebar-order")
  259. async def get_default_sidebar_order(
  260. db: AsyncSession = Depends(get_db),
  261. ):
  262. """Get the admin-set default sidebar order.
  263. Intentionally unauthenticated: non-admin users need to read this value to apply
  264. the default sidebar order, but may lack SETTINGS_READ permission.
  265. The value is non-sensitive (sidebar item IDs only).
  266. """
  267. value = await get_setting(db, "default_sidebar_order")
  268. return {"default_sidebar_order": value or ""}
  269. # Fields exposed via /ui-preferences without SETTINGS_READ. Each entry MUST be
  270. # non-sensitive (no credentials, no PII, no secret tokens) — granting SETTINGS_READ
  271. # also grants visibility of SMTP/LDAP/MQTT passwords and similar, so the goal of
  272. # this endpoint is exactly to NOT require that permission for UI rendering hints.
  273. # When adding a field here, confirm it doesn't carry anything sensitive.
  274. _UI_PREFERENCE_FIELDS: tuple[str, ...] = (
  275. "require_plate_clear",
  276. "check_printer_firmware",
  277. "camera_view_mode",
  278. "time_format",
  279. "date_format",
  280. "drying_presets",
  281. "ams_humidity_good",
  282. "ams_humidity_fair",
  283. "ams_temp_good",
  284. "ams_temp_fair",
  285. "bed_cooled_threshold",
  286. )
  287. @router.get("/ui-preferences")
  288. async def get_ui_preferences(db: AsyncSession = Depends(get_db)):
  289. """Get the curated subset of settings that any page needs to render correctly.
  290. Intentionally not gated on SETTINGS_READ — every authenticated user (and
  291. every page that loads for them) needs these fields, but granting SETTINGS_READ
  292. would also grant visibility of secrets (SMTP/LDAP/MQTT credentials, etc.).
  293. Same pattern as /default-sidebar-order (#1293).
  294. Reuses _build_settings_response so the typed values match what /settings
  295. returns for fields with the same name — bool/int/float/str types stay in
  296. sync without a separate type-coercion path.
  297. """
  298. full = await _build_settings_response(db, is_api_key=False)
  299. dumped = full.model_dump()
  300. return {key: dumped[key] for key in _UI_PREFERENCE_FIELDS if key in dumped}
  301. @router.get("/check-ffmpeg")
  302. async def check_ffmpeg(
  303. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  304. ):
  305. """Check if ffmpeg is installed and available.
  306. Gated on ``SETTINGS_READ`` (audit finding I4 — the binary path was
  307. leaking the host filesystem layout to unauthenticated callers).
  308. ``require_permission_if_auth_enabled`` returns ``None`` only when
  309. auth is disabled (in which case there's no privacy boundary to
  310. enforce); otherwise it raises 401/403 before we get here.
  311. """
  312. from backend.app.services.camera import get_ffmpeg_path
  313. ffmpeg_path = get_ffmpeg_path()
  314. return {
  315. "installed": ffmpeg_path is not None,
  316. "path": ffmpeg_path,
  317. }
  318. @router.get("/spoolman")
  319. async def get_spoolman_settings(
  320. db: AsyncSession = Depends(get_db),
  321. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  322. ):
  323. """Get Spoolman integration settings."""
  324. spoolman_enabled = await get_setting(db, "spoolman_enabled") or "false"
  325. spoolman_url = await get_setting(db, "spoolman_url") or ""
  326. spoolman_sync_mode = await get_setting(db, "spoolman_sync_mode") or "auto"
  327. spoolman_disable_weight_sync = await get_setting(db, "spoolman_disable_weight_sync") or "false"
  328. spoolman_report_partial_usage = await get_setting(db, "spoolman_report_partial_usage") or "true"
  329. return {
  330. "spoolman_enabled": spoolman_enabled,
  331. "spoolman_url": spoolman_url,
  332. "spoolman_sync_mode": spoolman_sync_mode,
  333. "spoolman_disable_weight_sync": spoolman_disable_weight_sync,
  334. "spoolman_report_partial_usage": spoolman_report_partial_usage,
  335. }
  336. @router.put("/spoolman")
  337. async def update_spoolman_settings(
  338. settings: dict,
  339. db: AsyncSession = Depends(get_db),
  340. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  341. ):
  342. """Update Spoolman integration settings."""
  343. if "spoolman_enabled" in settings:
  344. old_val = await get_setting(db, "spoolman_enabled") or "false"
  345. new_val = settings["spoolman_enabled"]
  346. await set_setting(db, "spoolman_enabled", new_val)
  347. # Switching to Spoolman: clear built-in inventory slot assignments
  348. if old_val.lower() != "true" and new_val.lower() == "true":
  349. from backend.app.models.spool_assignment import SpoolAssignment
  350. result = await db.execute(delete(SpoolAssignment))
  351. logger.info("Cleared %d spool assignments on switch to Spoolman mode", result.rowcount)
  352. # Switching back to internal mode: clear Spoolman slot assignments — the
  353. # symmetric counterpart of the clear above. Without this, stale
  354. # spoolman_slot_assignments rows linger and would wrongly count as
  355. # "assigned" in any mode-agnostic check (e.g. the missing-spool-
  356. # assignment notification, which unions both tables — #1473).
  357. elif old_val.lower() == "true" and new_val.lower() != "true":
  358. from backend.app.models.spoolman_slot_assignment import SpoolmanSlotAssignment
  359. result = await db.execute(delete(SpoolmanSlotAssignment))
  360. logger.info("Cleared %d Spoolman slot assignments on switch to internal mode", result.rowcount)
  361. if "spoolman_url" in settings:
  362. await set_setting(db, "spoolman_url", settings["spoolman_url"])
  363. if "spoolman_sync_mode" in settings:
  364. await set_setting(db, "spoolman_sync_mode", settings["spoolman_sync_mode"])
  365. if "spoolman_disable_weight_sync" in settings:
  366. await set_setting(db, "spoolman_disable_weight_sync", settings["spoolman_disable_weight_sync"])
  367. if "spoolman_report_partial_usage" in settings:
  368. await set_setting(db, "spoolman_report_partial_usage", settings["spoolman_report_partial_usage"])
  369. await db.commit()
  370. db.expire_all()
  371. # Return updated settings
  372. return await get_spoolman_settings(db)
  373. async def get_homeassistant_settings(db: AsyncSession) -> dict:
  374. """
  375. Get Home Assistant integration settings.
  376. Environment variables (HA_URL, HA_TOKEN) take precedence over database settings.
  377. """
  378. import os
  379. # Check environment variables first
  380. ha_url_env = os.environ.get("HA_URL")
  381. ha_token_env = os.environ.get("HA_TOKEN")
  382. # Fall back to database values
  383. ha_url = ha_url_env or await get_setting(db, "ha_url") or ""
  384. ha_token = ha_token_env or await get_setting(db, "ha_token") or ""
  385. ha_enabled_db = await get_setting(db, "ha_enabled") or "false"
  386. # Track which settings come from environment
  387. ha_url_from_env = bool(ha_url_env)
  388. ha_token_from_env = bool(ha_token_env)
  389. ha_env_managed = ha_url_from_env and ha_token_from_env
  390. # Auto-enable when both env vars are set, otherwise use database value
  391. if ha_url_env and ha_token_env:
  392. ha_enabled = True
  393. else:
  394. ha_enabled = ha_enabled_db.lower() == "true"
  395. return {
  396. "ha_enabled": ha_enabled,
  397. "ha_url": ha_url,
  398. "ha_token": ha_token,
  399. "ha_url_from_env": ha_url_from_env,
  400. "ha_token_from_env": ha_token_from_env,
  401. "ha_env_managed": ha_env_managed,
  402. }
  403. async def create_backup_zip(output_path: Path | None = None) -> tuple[Path, str]:
  404. """Create a complete backup ZIP (database + all data directories).
  405. If output_path is given, the ZIP is written there.
  406. Otherwise a temporary file is created (caller must clean up).
  407. Returns (zip_path, filename).
  408. """
  409. import shutil
  410. import tempfile
  411. from backend.app.core.db_dialect import is_sqlite
  412. base_dir = app_settings.base_dir
  413. filename = f"bambuddy-backup-{datetime.now().strftime('%Y%m%d-%H%M%S')}.zip"
  414. with tempfile.TemporaryDirectory() as temp_dir:
  415. temp_path = Path(temp_dir)
  416. if is_sqlite():
  417. from sqlalchemy import text
  418. from backend.app.core.database import engine
  419. db_path = Path(app_settings.database_url.replace("sqlite+aiosqlite:///", ""))
  420. # Checkpoint WAL to ensure all data is in main db file
  421. async with engine.begin() as conn:
  422. await conn.execute(text("PRAGMA wal_checkpoint(TRUNCATE)"))
  423. # Copy database file
  424. shutil.copy2(db_path, temp_path / "bambuddy.db")
  425. else:
  426. # PostgreSQL: export to a portable SQLite file via SQLAlchemy.
  427. # This makes backups restorable on both SQLite and Postgres installs.
  428. import json
  429. import sqlite3
  430. from backend.app.core.database import Base, engine
  431. backup_db_path = temp_path / "bambuddy.db"
  432. dst = sqlite3.connect(str(backup_db_path))
  433. metadata = Base.metadata
  434. # Create tables in SQLite backup (simplified — just column names and types)
  435. for table in metadata.sorted_tables:
  436. cols = []
  437. pk_cols = [col.name for col in table.columns if col.primary_key]
  438. for col in table.columns:
  439. col_type = _sqlalchemy_type_to_sqlite_type(str(col.type))
  440. # Only inline PRIMARY KEY for single-column PKs
  441. pk = " PRIMARY KEY" if col.primary_key and len(pk_cols) == 1 else ""
  442. cols.append(f"{col.name} {col_type}{pk}")
  443. # Add composite primary key constraint if needed
  444. if len(pk_cols) > 1:
  445. cols.append(f"PRIMARY KEY ({', '.join(pk_cols)})")
  446. dst.execute(f"CREATE TABLE IF NOT EXISTS {table.name} ({', '.join(cols)})") # noqa: S608
  447. # Export data from Postgres to SQLite
  448. async with engine.connect() as conn:
  449. for table in metadata.sorted_tables:
  450. result = await conn.execute(table.select())
  451. rows = result.fetchall()
  452. if not rows:
  453. continue
  454. columns = list(result.keys())
  455. placeholders = ", ".join(["?"] * len(columns))
  456. col_list = ", ".join(columns)
  457. insert_sql = f"INSERT INTO {table.name} ({col_list}) VALUES ({placeholders})" # noqa: S608 # nosec B608 — table/column names from ORM metadata, not user input
  458. def _serialize_row(row):
  459. return tuple(json.dumps(v) if isinstance(v, (list, dict)) else v for v in row)
  460. dst.executemany(insert_sql, [_serialize_row(row) for row in rows])
  461. dst.commit()
  462. dst.close()
  463. logger.info("PostgreSQL backup exported to portable SQLite format")
  464. # Copy data directories (if they exist)
  465. dirs_to_backup = [
  466. ("archive", base_dir / "archive"),
  467. ("virtual_printer", base_dir / "virtual_printer"),
  468. ("plate_calibration", app_settings.plate_calibration_dir),
  469. ("icons", base_dir / "icons"),
  470. ("projects", base_dir / "projects"),
  471. ]
  472. for name, src_dir in dirs_to_backup:
  473. if src_dir.exists() and any(src_dir.iterdir()):
  474. try:
  475. shutil.copytree(
  476. src_dir, temp_path / name
  477. ) # SEC-PATH-OK: name iterates the dirs_to_backup tuple of constant strings ("archive", "virtual_printer", ...)
  478. except shutil.Error as e:
  479. logger.warning("Some files in %s could not be copied: %s", name, e)
  480. except PermissionError as e:
  481. logger.warning("Permission denied copying %s: %s", name, e)
  482. # Include the MFA encryption key as a ZIP top-level entry alongside
  483. # bambuddy.db. Without it, encrypted client_secret / TOTP secret rows
  484. # would be unrecoverable after restore on a host without MFA_ENCRYPTION_KEY set.
  485. from backend.app.core.paths import resolve_data_dir
  486. mfa_key_src = resolve_data_dir() / ".mfa_encryption_key"
  487. if mfa_key_src.exists() and mfa_key_src.is_file():
  488. try:
  489. shutil.copy2(mfa_key_src, temp_path / ".mfa_encryption_key")
  490. except OSError as exc:
  491. logger.error(
  492. "Could not include MFA encryption key in backup (%s). "
  493. "The backup ZIP will not contain the key — restore on a "
  494. "keyless host will fail for encrypted secrets.",
  495. exc,
  496. )
  497. raise
  498. # Create ZIP
  499. if output_path is not None:
  500. zip_file = (
  501. output_path / filename
  502. ) # SEC-PATH-OK: filename = f"bambuddy-backup-{datetime.now()...}.zip" generated in create_backup_zip itself
  503. else:
  504. fd, tmp = tempfile.mkstemp(suffix=".zip")
  505. os.close(fd)
  506. zip_file = Path(tmp)
  507. with zipfile.ZipFile(zip_file, "w", zipfile.ZIP_DEFLATED) as zf:
  508. for file_path in temp_path.rglob("*"):
  509. if file_path.is_file():
  510. arcname = file_path.relative_to(temp_path)
  511. zf.write(file_path, arcname)
  512. return zip_file, filename
  513. @router.get("/backup")
  514. async def create_backup(
  515. db: AsyncSession = Depends(get_db),
  516. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_BACKUP),
  517. ):
  518. """Create a complete backup (database + all files) as a ZIP download."""
  519. from starlette.background import BackgroundTask
  520. try:
  521. zip_file, filename = await create_backup_zip()
  522. return FileResponse(
  523. path=zip_file,
  524. filename=filename,
  525. media_type="application/zip",
  526. background=BackgroundTask(lambda: zip_file.unlink(missing_ok=True)),
  527. )
  528. except Exception as e:
  529. logger.error("Backup failed: %s", e, exc_info=True)
  530. return JSONResponse(
  531. status_code=500,
  532. content={"success": False, "message": "Backup failed. Check server logs for details."},
  533. )
  534. async def _import_sqlite_to_postgres(sqlite_path: Path, postgres_url: str):
  535. """Import data from a SQLite database file into the current PostgreSQL database.
  536. Used for cross-database restore (SQLite backup → PostgreSQL).
  537. Reads all tables from the SQLite file and bulk-inserts into Postgres.
  538. """
  539. import sqlite3
  540. from sqlalchemy import text
  541. from backend.app.core.database import Base, _create_engine
  542. # Create a temporary engine for the import (current engine was disposed)
  543. pg_engine = _create_engine()
  544. try:
  545. # Open SQLite file directly (sync — it's a local file read)
  546. src = sqlite3.connect(str(sqlite_path))
  547. src.row_factory = sqlite3.Row
  548. # Get list of tables from SQLite (skip internal/FTS tables)
  549. cursor = src.execute(
  550. "SELECT name FROM sqlite_master WHERE type='table' "
  551. "AND name NOT LIKE 'sqlite_%' AND name NOT LIKE 'archive_fts%'"
  552. )
  553. src_tables = {row["name"] for row in cursor.fetchall()}
  554. # Get Postgres tables from our ORM models
  555. metadata = Base.metadata
  556. pg_tables = set(metadata.tables.keys())
  557. # Only import tables that exist in both source and destination
  558. tables_to_import = src_tables & pg_tables
  559. sorted_tables = [t.name for t in metadata.sorted_tables if t.name in tables_to_import]
  560. # Phase 1: Drop all tables and recreate WITHOUT foreign keys.
  561. # This avoids all FK ordering/orphan issues during import.
  562. saved_fks = {}
  563. for table in metadata.sorted_tables:
  564. fks = list(table.foreign_key_constraints)
  565. if fks:
  566. saved_fks[table.name] = fks
  567. for fk in fks:
  568. table.constraints.discard(fk)
  569. async with pg_engine.begin() as conn:
  570. # Drop every existing table in the public schema with CASCADE
  571. # rather than `metadata.drop_all`. Two reasons:
  572. # 1. The user's live DB may carry orphan tables from removed
  573. # features (e.g. the legacy `spoolman_slot_assignments`,
  574. # `spoolman_k_profile`) that hold FK constraints back to
  575. # ORM tables. `drop_all` doesn't know they exist and emits
  576. # `DROP TABLE printers` without CASCADE — Postgres refuses
  577. # and the whole restore aborts (#XXXX).
  578. # 2. Even within the metadata, `drop_all` is FK-ordered and
  579. # breaks if a future schema rename leaves old constraints
  580. # around. CASCADE is the right tool for a destructive
  581. # restore: the user is intentionally wiping state.
  582. await conn.execute(
  583. text(
  584. "DO $$ DECLARE r RECORD; BEGIN "
  585. "FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = 'public') LOOP "
  586. "EXECUTE 'DROP TABLE IF EXISTS public.' || quote_ident(r.tablename) || ' CASCADE'; "
  587. "END LOOP; END $$;"
  588. )
  589. )
  590. await conn.run_sync(metadata.create_all)
  591. # Restore FK definitions in metadata (needed for re-adding later)
  592. for table_name, fks in saved_fks.items():
  593. table_obj = metadata.tables[table_name]
  594. for fk in fks:
  595. table_obj.constraints.add(fk)
  596. # Phase 2: Import data (no FKs to worry about)
  597. async with pg_engine.begin() as conn:
  598. # Import each table in dependency order (parents before children)
  599. for table_name in sorted_tables:
  600. rows = src.execute(f"SELECT * FROM {table_name}").fetchall() # noqa: S608 # nosec B608
  601. if not rows:
  602. continue
  603. # Filter to columns that exist in the Postgres table
  604. src_columns = rows[0].keys()
  605. pg_table = metadata.tables.get(table_name)
  606. pg_columns = {c.name for c in pg_table.columns} if pg_table is not None else set()
  607. columns = [c for c in src_columns if c in pg_columns]
  608. if not columns:
  609. continue
  610. col_list = ", ".join(columns)
  611. param_list = ", ".join(f":{c}" for c in columns)
  612. # ON CONFLICT DO NOTHING handles duplicate rows from SQLite (which doesn't enforce unique constraints)
  613. insert_sql = text(f"INSERT INTO {table_name} ({col_list}) VALUES ({param_list}) ON CONFLICT DO NOTHING") # noqa: S608 # nosec B608
  614. # Identify columns that need type conversion (SQLite stores booleans
  615. # as int and datetimes as str — asyncpg requires native Python types)
  616. from datetime import datetime as dt
  617. bool_columns = set()
  618. datetime_columns = set()
  619. not_null_defaults = {} # col_name -> default value for NOT NULL columns
  620. if pg_table is not None:
  621. for col in pg_table.columns:
  622. if col.name not in columns:
  623. continue
  624. col_type = str(col.type)
  625. if col_type == "BOOLEAN":
  626. bool_columns.add(col.name)
  627. elif col_type in ("DATETIME", "TIMESTAMP WITHOUT TIME ZONE", "TIMESTAMP WITH TIME ZONE"):
  628. datetime_columns.add(col.name)
  629. # Track NOT NULL columns with defaults — older backups may have NULL
  630. # for columns added after the backup was created
  631. if not col.nullable:
  632. if col.default is not None:
  633. default = col.default.arg
  634. if callable(default):
  635. default = default(None)
  636. not_null_defaults[col.name] = default
  637. elif col.server_default is not None:
  638. # server_default=func.now() → use current timestamp
  639. if col.name in datetime_columns:
  640. not_null_defaults[col.name] = "__now__"
  641. else:
  642. # Try to extract literal server default
  643. sd = str(col.server_default.arg) if hasattr(col.server_default, "arg") else None
  644. if sd is not None:
  645. not_null_defaults[col.name] = sd
  646. now = dt.now()
  647. def _convert_row(
  648. row, cols=columns, bools=bool_columns, dts=datetime_columns, nn_defaults=not_null_defaults, _now=now
  649. ):
  650. result = {}
  651. for c in cols:
  652. val = row[c]
  653. if val is None and c in nn_defaults:
  654. val = _now if nn_defaults[c] == "__now__" else nn_defaults[c]
  655. if val is not None:
  656. if c in bools:
  657. val = bool(val)
  658. elif c in dts and isinstance(val, str):
  659. try:
  660. val = dt.fromisoformat(val)
  661. except ValueError:
  662. pass
  663. result[c] = val
  664. return result
  665. batch = [_convert_row(row) for row in rows]
  666. await conn.execute(insert_sql, batch)
  667. logger.info("Imported %d rows into %s", len(batch), table_name)
  668. # Reset sequences to max(id) + 1 for each table with an id column
  669. for table_name in sorted_tables:
  670. try:
  671. async with conn.begin_nested():
  672. result = await conn.execute(text(f"SELECT MAX(id) FROM {table_name}")) # noqa: S608 # nosec B608
  673. max_id = result.scalar()
  674. if max_id is not None:
  675. seq_name = f"{table_name}_id_seq"
  676. await conn.execute(text(f"SELECT setval('{seq_name}', {max_id})")) # noqa: S608
  677. except Exception:
  678. pass # Table may not have an id column or sequence
  679. src.close()
  680. logger.info("Cross-database import complete: %d tables imported", len(tables_to_import))
  681. # Recreate FK constraints from ORM metadata (not from saved definitions).
  682. # Use individual transactions so orphaned SQLite data doesn't block valid FKs.
  683. from sqlalchemy.schema import AddConstraint
  684. failed_fks = []
  685. for table in metadata.sorted_tables:
  686. for fk in table.foreign_key_constraints:
  687. try:
  688. async with pg_engine.begin() as fk_conn:
  689. await fk_conn.execute(AddConstraint(fk))
  690. except Exception:
  691. failed_fks.append(f"{table.name}.{fk.name}")
  692. if failed_fks:
  693. logger.warning(
  694. "Could not restore %d FK constraints (orphaned data in SQLite): %s",
  695. len(failed_fks),
  696. ", ".join(failed_fks),
  697. )
  698. finally:
  699. await pg_engine.dispose()
  700. @router.post("/restore")
  701. async def restore_backup(
  702. file: UploadFile = File(...),
  703. db: AsyncSession = Depends(get_db),
  704. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_RESTORE),
  705. ):
  706. """Restore from a complete backup ZIP.
  707. Replaces the database and all data directories from the backup ZIP.
  708. Requires a restart after restore.
  709. """
  710. import shutil
  711. import tempfile
  712. from fastapi import HTTPException
  713. from backend.app.core.database import close_all_connections, init_db, reinitialize_database
  714. from backend.app.core.db_dialect import is_sqlite
  715. from backend.app.services.virtual_printer import virtual_printer_manager
  716. base_dir = app_settings.base_dir
  717. with tempfile.TemporaryDirectory() as temp_dir:
  718. temp_path = Path(temp_dir)
  719. # 1. Read and extract ZIP
  720. content = await file.read()
  721. # Check if it's a valid ZIP
  722. if not file.filename or not file.filename.endswith(".zip"):
  723. raise HTTPException(400, "Invalid backup file: must be a .zip file")
  724. try:
  725. with zipfile.ZipFile(io.BytesIO(content), "r") as zf:
  726. for name in zf.namelist():
  727. # Reject path-traversal payloads: any entry whose resolved
  728. # path escapes temp_path would allow writing arbitrary files
  729. # on the host (ZipSlip / CVE-2006-5456).
  730. dest = (
  731. temp_path / name
  732. ).resolve() # SEC-PATH-OK: is_relative_to containment check below before extractall
  733. # is_relative_to (Python 3.9+) covers both relative
  734. # path-traversal (../etc/passwd) and absolute-path overrides
  735. # (/etc/passwd) — str.startswith was vulnerable to
  736. # prefix-collision attacks (e.g. /tmp/abc_evil/file passing
  737. # a /tmp/abc prefix check).
  738. if not dest.is_relative_to(temp_path.resolve()):
  739. raise HTTPException(400, f"Invalid backup: unsafe path in ZIP: {name!r}")
  740. zf.extractall(temp_path)
  741. except zipfile.BadZipFile:
  742. raise HTTPException(400, "Invalid backup file: not a valid ZIP")
  743. # 2. Validate backup
  744. backup_db = temp_path / "bambuddy.db"
  745. if not backup_db.exists():
  746. raise HTTPException(400, "Invalid backup: missing bambuddy.db")
  747. try:
  748. import asyncio
  749. # 3. Stop virtual printer if running (releases file locks)
  750. try:
  751. if virtual_printer_manager.is_enabled:
  752. logger.info("Stopping virtual printer for restore...")
  753. await virtual_printer_manager.configure(enabled=False)
  754. await asyncio.sleep(1)
  755. except Exception as e:
  756. logger.warning("Failed to stop virtual printer: %s", e)
  757. # 4. Close current database connections
  758. logger.info("Closing database connections...")
  759. await close_all_connections()
  760. # B1: Restore the MFA encryption key file BEFORE the database swap.
  761. # If the key write fails (OSError, RO disk, full disk, EACCES) we
  762. # can still abort while the live DB is intact. Doing this AFTER the
  763. # DB swap would leave the database with rows encrypted under the
  764. # backup's key but the running install holding only the old key —
  765. # every encrypted secret becomes unrecoverable.
  766. from backend.app.core.paths import resolve_data_dir
  767. mfa_key_src = temp_path / ".mfa_encryption_key"
  768. if mfa_key_src.exists() and mfa_key_src.is_file():
  769. dst_key = resolve_data_dir() / ".mfa_encryption_key"
  770. tmp_key = dst_key.parent / ".mfa_encryption_key.restore-tmp"
  771. try:
  772. dst_key.parent.mkdir(parents=True, exist_ok=True)
  773. # S1: atomic write with restrictive mode from creation.
  774. # O_TRUNC because a stale tmp may exist from a prior
  775. # failed restore attempt — we want to overwrite it.
  776. fd = os.open(str(tmp_key), os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
  777. try:
  778. os.write(fd, mfa_key_src.read_bytes())
  779. finally:
  780. os.close(fd)
  781. # POSIX rename(2) — atomic when source/dest are on the
  782. # same filesystem (we're staying inside dst_key.parent).
  783. os.replace(str(tmp_key), str(dst_key))
  784. # S9: warn if the FS doesn't enforce 0o600
  785. actual_mode = dst_key.stat().st_mode & 0o777
  786. if actual_mode != 0o600:
  787. logger.warning(
  788. "Restored MFA key file %s: filesystem did not enforce 0o600 "
  789. "(actual: 0o%o). Key may be world-readable on Windows / SMB / FUSE.",
  790. dst_key,
  791. actual_mode,
  792. )
  793. logger.info("Restored .mfa_encryption_key from backup")
  794. except OSError as e:
  795. logger.error(
  796. "Could not write restored MFA key file to %s: %s — "
  797. "aborting BEFORE database swap (DB unchanged).",
  798. dst_key,
  799. e,
  800. exc_info=True,
  801. )
  802. raise HTTPException(
  803. status_code=500,
  804. detail=("Restore aborted: MFA key write failed. Database is unchanged. Check server logs."),
  805. ) from e
  806. # 5. Replace database
  807. logger.info("Restoring database from backup...")
  808. if is_sqlite():
  809. db_path = Path(app_settings.database_url.replace("sqlite+aiosqlite:///", ""))
  810. # Use SQLite's online backup API instead of shutil.copy2.
  811. # The pragma at database.py:19 runs the live DB in WAL mode,
  812. # which means a naive file copy is unsafe: anything written
  813. # to the live DB before this call that hasn't been
  814. # checkpointed yet (seed_default_groups + init_db on first
  815. # start, plus whatever background heartbeats wrote during
  816. # the request window) sits in bambuddy.db-wal with valid
  817. # checksums. The route handler's own `db: Depends(get_db)`
  818. # session also keeps a connection checked out across
  819. # engine.dispose(), holding fds to the WAL inode. With
  820. # `shutil.copy2` SQLite finds the stale WAL on the next
  821. # open and silently re-applies those page-level writes on
  822. # top of the restored DB, partially clobbering it with
  823. # fresh-install state — the user sees a "successful"
  824. # restore where most rows and settings have reverted to
  825. # defaults (#1211 / #668). The page-by-page backup API
  826. # opens both DBs as real SQLite connections, takes the
  827. # right locks, and routes new pages through the live DB's
  828. # own WAL — so concurrent open sessions see their own
  829. # snapshot until they close (transaction isolation) but
  830. # can't corrupt the restored state.
  831. import sqlite3
  832. src_conn = sqlite3.connect(str(backup_db))
  833. try:
  834. dst_conn = sqlite3.connect(str(db_path))
  835. try:
  836. src_conn.backup(dst_conn)
  837. finally:
  838. dst_conn.close()
  839. finally:
  840. src_conn.close()
  841. else:
  842. # Import SQLite backup into PostgreSQL
  843. logger.info("Importing SQLite backup into PostgreSQL...")
  844. await _import_sqlite_to_postgres(backup_db, app_settings.database_url)
  845. # 6. Replace data directories
  846. # For Docker compatibility: clear contents then copy (don't delete mount points)
  847. dirs_to_restore = [
  848. ("archive", base_dir / "archive"),
  849. ("virtual_printer", base_dir / "virtual_printer"),
  850. ("plate_calibration", app_settings.plate_calibration_dir),
  851. ("icons", base_dir / "icons"),
  852. ("projects", base_dir / "projects"),
  853. ]
  854. skipped_dirs = []
  855. for name, dest_dir in dirs_to_restore:
  856. src_dir = (
  857. temp_path / name
  858. ) # SEC-PATH-OK: name iterates the dirs_to_restore tuple of constant strings ("archive", "virtual_printer", ...)
  859. if src_dir.exists():
  860. logger.info("Restoring %s directory...", name)
  861. try:
  862. # Clear destination contents (not the dir itself - may be Docker mount)
  863. if dest_dir.exists():
  864. for item in dest_dir.iterdir():
  865. try:
  866. if item.is_dir():
  867. shutil.rmtree(item)
  868. else:
  869. item.unlink()
  870. except OSError as e:
  871. logger.warning("Could not delete %s: %s", item, e)
  872. else:
  873. dest_dir.mkdir(parents=True, exist_ok=True)
  874. # Copy contents from backup
  875. for item in src_dir.iterdir():
  876. dest_item = dest_dir / item.name
  877. if item.is_dir():
  878. shutil.copytree(item, dest_item)
  879. else:
  880. shutil.copy2(item, dest_item)
  881. except OSError as e:
  882. logger.warning("Could not restore %s directory: %s", name, e)
  883. skipped_dirs.append(name)
  884. # 7. Reset the encryption singleton so the migration that runs
  885. # inside init_db() picks up the restored key file (if a new one
  886. # was written above). Without this reset, _get_fernet would
  887. # return the cached Fernet instance built from the previous key.
  888. import backend.app.core.encryption as _enc_mod
  889. _enc_mod._fernet_instance = None
  890. _enc_mod._key_source = None
  891. _enc_mod._warn_shown = False
  892. # 8. Reinitialize the database engine and apply schema migrations so that
  893. # tables added after the backup was created (e.g. ams_labels) exist
  894. # immediately, without requiring a manual restart.
  895. await reinitialize_database()
  896. await init_db()
  897. logger.info("Restore complete - restart required")
  898. message = "Backup restored successfully. Please restart Bambuddy for changes to take effect."
  899. if skipped_dirs:
  900. message += f" Note: Some directories could not be restored ({', '.join(skipped_dirs)})."
  901. return {
  902. "success": True,
  903. "message": message,
  904. }
  905. except HTTPException:
  906. # Preserve specific HTTP error responses raised inside the restore
  907. # body (e.g. the key-write OSError → 500). The blanket
  908. # except Exception below would otherwise swallow them and replace
  909. # the operator-facing detail with a generic message.
  910. raise
  911. except Exception as e:
  912. logger.error("Restore failed: %s", e, exc_info=True)
  913. return JSONResponse(
  914. status_code=500,
  915. content={"success": False, "message": "Restore failed. Check server logs for details."},
  916. )
  917. @router.get("/network-interfaces")
  918. async def get_network_interfaces(
  919. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  920. ):
  921. """Get available network interfaces with all IPs (primary + aliases)."""
  922. from backend.app.services.network_utils import get_all_interface_ips
  923. interfaces = get_all_interface_ips()
  924. return {"interfaces": interfaces}
  925. @router.get("/virtual-printer/models")
  926. async def get_virtual_printer_models(
  927. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  928. ):
  929. """Get available virtual printer models."""
  930. from backend.app.services.virtual_printer import (
  931. DEFAULT_VIRTUAL_PRINTER_MODEL,
  932. VIRTUAL_PRINTER_MODELS,
  933. )
  934. return {
  935. "models": VIRTUAL_PRINTER_MODELS,
  936. "default": DEFAULT_VIRTUAL_PRINTER_MODEL,
  937. }
  938. @router.get("/virtual-printer")
  939. async def get_virtual_printer_settings(
  940. db: AsyncSession = Depends(get_db),
  941. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  942. ):
  943. """Get virtual printer settings and status."""
  944. from backend.app.services.virtual_printer import (
  945. DEFAULT_VIRTUAL_PRINTER_MODEL,
  946. virtual_printer_manager,
  947. )
  948. enabled = await get_setting(db, "virtual_printer_enabled")
  949. access_code = await get_setting(db, "virtual_printer_access_code")
  950. mode = await get_setting(db, "virtual_printer_mode")
  951. model = await get_setting(db, "virtual_printer_model")
  952. target_printer_id = await get_setting(db, "virtual_printer_target_printer_id")
  953. remote_interface_ip = await get_setting(db, "virtual_printer_remote_interface_ip")
  954. tailscale_disabled_raw = await get_setting(db, "virtual_printer_tailscale_disabled")
  955. archive_name_source = await get_setting(db, "virtual_printer_archive_name_source")
  956. from backend.app.models.virtual_printer import VP_MODE_ARCHIVE, normalize_vp_mode
  957. return {
  958. "enabled": enabled == "true" if enabled else False,
  959. "access_code_set": bool(access_code),
  960. # Normalize on read so older settings rows (with `immediate` /
  961. # `print_queue`) come out as `archive` / `queue` for the frontend.
  962. "mode": normalize_vp_mode(mode) or VP_MODE_ARCHIVE,
  963. "model": model or DEFAULT_VIRTUAL_PRINTER_MODEL,
  964. "target_printer_id": int(target_printer_id) if target_printer_id else None,
  965. "remote_interface_ip": remote_interface_ip or "",
  966. "tailscale_disabled": tailscale_disabled_raw == "true" if tailscale_disabled_raw else True,
  967. "archive_name_source": archive_name_source if archive_name_source in ("metadata", "filename") else "metadata",
  968. "status": virtual_printer_manager.get_status(),
  969. }
  970. @router.put("/virtual-printer")
  971. async def update_virtual_printer_settings(
  972. enabled: bool = None,
  973. access_code: str = None,
  974. mode: str = None,
  975. model: str = None,
  976. target_printer_id: int = None,
  977. remote_interface_ip: str = None,
  978. tailscale_disabled: bool = None,
  979. archive_name_source: str = None,
  980. db: AsyncSession = Depends(get_db),
  981. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  982. ):
  983. """Update virtual printer settings and restart services if needed.
  984. For proxy mode with SSDP proxy (dual-homed setup):
  985. - remote_interface_ip: IP of interface on slicer's network (LAN B)
  986. - Local interface is auto-detected based on target printer IP
  987. """
  988. from sqlalchemy import select
  989. from backend.app.models.printer import Printer
  990. from backend.app.services.virtual_printer import (
  991. DEFAULT_VIRTUAL_PRINTER_MODEL,
  992. VIRTUAL_PRINTER_MODELS,
  993. virtual_printer_manager,
  994. )
  995. # Get current values
  996. current_enabled = await get_setting(db, "virtual_printer_enabled") == "true"
  997. current_access_code = await get_setting(db, "virtual_printer_access_code") or ""
  998. # Default to `archive` (the canonical name) but tolerate legacy `immediate`
  999. # in the stored value — normalized later before validation.
  1000. current_mode = await get_setting(db, "virtual_printer_mode") or "archive"
  1001. current_model = await get_setting(db, "virtual_printer_model") or DEFAULT_VIRTUAL_PRINTER_MODEL
  1002. current_target_id_str = await get_setting(db, "virtual_printer_target_printer_id")
  1003. current_target_id = int(current_target_id_str) if current_target_id_str else None
  1004. current_remote_iface = await get_setting(db, "virtual_printer_remote_interface_ip") or ""
  1005. current_ts_disabled_raw = await get_setting(db, "virtual_printer_tailscale_disabled")
  1006. # Default True (opt-in) when the setting has never been saved — matches the model default.
  1007. current_ts_disabled = current_ts_disabled_raw == "true" if current_ts_disabled_raw else True
  1008. # Apply updates
  1009. new_enabled = enabled if enabled is not None else current_enabled
  1010. new_access_code = access_code if access_code is not None else current_access_code
  1011. new_mode = mode if mode is not None else current_mode
  1012. new_model = model if model is not None else current_model
  1013. new_target_id = target_printer_id if target_printer_id is not None else current_target_id
  1014. new_remote_iface = remote_interface_ip if remote_interface_ip is not None else current_remote_iface
  1015. new_ts_disabled = tailscale_disabled if tailscale_disabled is not None else current_ts_disabled
  1016. # Validate mode. Canonical wire values are `archive` / `review` / `queue`
  1017. # / `proxy`; legacy `immediate` and `print_queue` are accepted as aliases
  1018. # and translated before storage so support bundles stop showing the old
  1019. # confusing pair (#1429 mode-label discrepancy).
  1020. from backend.app.models.virtual_printer import VP_MODE_VALUES, normalize_vp_mode
  1021. canonical_mode = normalize_vp_mode(new_mode)
  1022. if canonical_mode not in VP_MODE_VALUES:
  1023. return JSONResponse(
  1024. status_code=400,
  1025. content={
  1026. "detail": f"Mode must be one of: {', '.join(VP_MODE_VALUES)}",
  1027. },
  1028. )
  1029. new_mode = canonical_mode
  1030. # Validate archive_name_source
  1031. if archive_name_source is not None and archive_name_source not in ("metadata", "filename"):
  1032. return JSONResponse(
  1033. status_code=400,
  1034. content={"detail": "archive_name_source must be 'metadata' or 'filename'"},
  1035. )
  1036. # Validate model
  1037. if model is not None and model not in VIRTUAL_PRINTER_MODELS:
  1038. return JSONResponse(
  1039. status_code=400,
  1040. content={"detail": f"Invalid model. Must be one of: {', '.join(VIRTUAL_PRINTER_MODELS.keys())}"},
  1041. )
  1042. # Mode-specific validation and printer lookup
  1043. target_printer_ip = ""
  1044. target_printer_serial = ""
  1045. if new_mode == "proxy":
  1046. # Proxy mode requires target printer when enabling
  1047. if new_enabled and not new_target_id:
  1048. # If just switching to proxy mode (not explicitly enabling), auto-disable
  1049. if enabled is None:
  1050. new_enabled = False
  1051. else:
  1052. return JSONResponse(
  1053. status_code=400,
  1054. content={"detail": "Target printer is required for proxy mode"},
  1055. )
  1056. # Look up printer IP and serial if we have a target
  1057. if new_target_id:
  1058. result = await db.execute(select(Printer).where(Printer.id == new_target_id))
  1059. printer = result.scalar_one_or_none()
  1060. if not printer:
  1061. return JSONResponse(
  1062. status_code=400,
  1063. content={"detail": f"Printer with ID {new_target_id} not found"},
  1064. )
  1065. target_printer_ip = printer.ip_address
  1066. target_printer_serial = printer.serial_number
  1067. # Access code not required for proxy mode
  1068. else:
  1069. # Non-proxy modes require access code when enabling
  1070. if new_enabled and not new_access_code:
  1071. # If just switching modes (not explicitly enabling), auto-disable
  1072. if enabled is None:
  1073. new_enabled = False
  1074. else:
  1075. return JSONResponse(
  1076. status_code=400,
  1077. content={"detail": "Access code is required when enabling virtual printer"},
  1078. )
  1079. # Validate access code length (Bambu Studio requires exactly 8 characters)
  1080. if access_code is not None and access_code and len(access_code) != 8:
  1081. return JSONResponse(
  1082. status_code=400,
  1083. content={"detail": "Access code must be exactly 8 characters"},
  1084. )
  1085. # Save settings
  1086. await set_setting(db, "virtual_printer_enabled", "true" if new_enabled else "false")
  1087. if access_code is not None:
  1088. await set_setting(db, "virtual_printer_access_code", access_code)
  1089. await set_setting(db, "virtual_printer_mode", new_mode)
  1090. if model is not None:
  1091. await set_setting(db, "virtual_printer_model", model)
  1092. if target_printer_id is not None:
  1093. await set_setting(db, "virtual_printer_target_printer_id", str(target_printer_id))
  1094. if remote_interface_ip is not None:
  1095. await set_setting(db, "virtual_printer_remote_interface_ip", remote_interface_ip)
  1096. if tailscale_disabled is not None:
  1097. await set_setting(db, "virtual_printer_tailscale_disabled", "true" if tailscale_disabled else "false")
  1098. if archive_name_source is not None:
  1099. await set_setting(db, "virtual_printer_archive_name_source", archive_name_source)
  1100. # Propagate tailscale_disabled to the first VirtualPrinter row so sync_from_db() picks it up
  1101. if tailscale_disabled is not None:
  1102. from backend.app.models.virtual_printer import VirtualPrinter as VPModel
  1103. vp_result = await db.execute(select(VPModel).order_by(VPModel.position).limit(1))
  1104. first_vp = vp_result.scalar_one_or_none()
  1105. if first_vp is not None:
  1106. first_vp.tailscale_disabled = new_ts_disabled
  1107. await db.commit()
  1108. db.expire_all()
  1109. # Reconfigure virtual printer
  1110. try:
  1111. await virtual_printer_manager.configure(
  1112. enabled=new_enabled,
  1113. access_code=new_access_code,
  1114. mode=new_mode,
  1115. model=new_model,
  1116. target_printer_ip=target_printer_ip,
  1117. target_printer_serial=target_printer_serial,
  1118. remote_interface_ip=new_remote_iface,
  1119. )
  1120. except ValueError as e:
  1121. logger.warning("Virtual printer configuration validation error: %s", e)
  1122. return JSONResponse(
  1123. status_code=400,
  1124. content={"detail": "Invalid virtual printer configuration. Check the provided values."},
  1125. )
  1126. except Exception as e:
  1127. logger.error("Failed to configure virtual printer: %s", e, exc_info=True)
  1128. return JSONResponse(
  1129. status_code=500,
  1130. content={"detail": "Failed to configure virtual printer. Check server logs for details."},
  1131. )
  1132. return await get_virtual_printer_settings(db)
  1133. # =============================================================================
  1134. # MQTT Relay Settings
  1135. # =============================================================================
  1136. @router.get("/mqtt/status")
  1137. async def get_mqtt_status(
  1138. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  1139. ):
  1140. """Get MQTT relay connection status."""
  1141. from backend.app.services.mqtt_relay import mqtt_relay
  1142. return mqtt_relay.get_status()