settings.py 65 KB

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