mirror of
https://github.com/lnbits/lnbits.git
synced 2026-08-13 12:42:47 +02:00
fix: get rid of url rewrites for extensions /upgrades/ (#4022)
This commit is contained in:
parent
0bdb5dc9a8
commit
0a7da6dc73
11 changed files with 73 additions and 90 deletions
|
|
@ -424,7 +424,6 @@ def register_custom_extensions_path():
|
|||
upgrades_dir = settings.lnbits_extensions_upgrade_path
|
||||
shutil.rmtree(upgrades_dir, True)
|
||||
Path(upgrades_dir).mkdir(parents=True, exist_ok=True)
|
||||
sys.path.append(str(upgrades_dir))
|
||||
|
||||
if settings.has_default_extension_path:
|
||||
return
|
||||
|
|
@ -494,10 +493,54 @@ def register_ext_tasks(ext: Extension) -> None:
|
|||
|
||||
def register_ext_routes(app: FastAPI, ext: Extension) -> None:
|
||||
"""Register FastAPI routes for extension."""
|
||||
ext_module = importlib.import_module(ext.module_name)
|
||||
module_name = ext.module_name
|
||||
# Clear all cached sub-modules so a fresh import picks up new files from ext_dir.
|
||||
# A simple reload() would reuse cached sub-modules (e.g. views_api) and serve
|
||||
# stale code even after the extension files have been replaced on disk.
|
||||
stale = [
|
||||
k for k in sys.modules if k == module_name or k.startswith(f"{module_name}.")
|
||||
]
|
||||
for k in stale:
|
||||
del sys.modules[k]
|
||||
if stale:
|
||||
# Pydantic v1 keeps a global _FUNCS set of validator qualnames to detect
|
||||
# duplicates. Clear the extension's entries so reimport doesn't raise
|
||||
# "duplicate validator" errors for validators with the same qualname.
|
||||
try:
|
||||
import pydantic.class_validators as _pydantic_cv
|
||||
|
||||
_pydantic_cv._FUNCS = {
|
||||
f for f in _pydantic_cv._FUNCS if not f.startswith(f"{module_name}.")
|
||||
}
|
||||
except (ImportError, AttributeError):
|
||||
pass
|
||||
ext_module = importlib.import_module(module_name)
|
||||
|
||||
ext_route = getattr(ext_module, f"{ext.code}_ext")
|
||||
|
||||
ext_redirects = (
|
||||
getattr(ext_module, f"{ext.code}_redirect_paths")
|
||||
if hasattr(ext_module, f"{ext.code}_redirect_paths")
|
||||
else []
|
||||
)
|
||||
|
||||
settings.activate_extension_paths(ext.code, ext_redirects)
|
||||
|
||||
# Remove existing routes for this extension before re-registering so that
|
||||
# an upgraded extension replaces the old one at the same paths (no prefix).
|
||||
ext_prefix = f"/{ext.code}"
|
||||
app.router.routes = [
|
||||
r
|
||||
for r in app.router.routes
|
||||
if not (
|
||||
getattr(r, "path", "") == ext_prefix
|
||||
or getattr(r, "path", "").startswith(f"{ext_prefix}/")
|
||||
)
|
||||
]
|
||||
# Invalidate FastAPI's cached OpenAPI schema so the next /openapi.json
|
||||
# request reflects the updated routes.
|
||||
app.openapi_schema = None
|
||||
|
||||
if hasattr(ext_module, f"{ext.code}_static_files"):
|
||||
ext_statics = getattr(ext_module, f"{ext.code}_static_files")
|
||||
for s in ext_statics:
|
||||
|
|
@ -506,17 +549,8 @@ def register_ext_routes(app: FastAPI, ext: Extension) -> None:
|
|||
)
|
||||
app.mount(s["path"], StaticFiles(directory=static_dir), s["name"])
|
||||
|
||||
ext_redirects = (
|
||||
getattr(ext_module, f"{ext.code}_redirect_paths")
|
||||
if hasattr(ext_module, f"{ext.code}_redirect_paths")
|
||||
else []
|
||||
)
|
||||
|
||||
settings.activate_extension_paths(ext.code, ext.upgrade_hash, ext_redirects)
|
||||
|
||||
logger.trace(f"Adding route for extension {ext_module}.")
|
||||
prefix = f"/upgrades/{ext.upgrade_hash}" if ext.upgrade_hash != "" else ""
|
||||
app.include_router(router=ext_route, prefix=prefix)
|
||||
app.include_router(router=ext_route)
|
||||
|
||||
|
||||
async def check_and_register_extensions(app: FastAPI) -> None:
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import asyncio
|
||||
import importlib
|
||||
import sys
|
||||
import time
|
||||
from functools import wraps
|
||||
from getpass import getpass
|
||||
|
|
@ -377,10 +376,6 @@ async def extensions_update( # noqa: C901
|
|||
if not await _can_run_operation(url):
|
||||
return
|
||||
|
||||
upgrades_dir = settings.lnbits_extensions_upgrade_path
|
||||
Path(upgrades_dir).mkdir(parents=True, exist_ok=True)
|
||||
sys.path.append(str(upgrades_dir))
|
||||
|
||||
if extension:
|
||||
await update_extension(extension, repo_index, source_repo, url, admin_user)
|
||||
return
|
||||
|
|
|
|||
|
|
@ -261,23 +261,13 @@ class Extension(BaseModel):
|
|||
name: str | None = None
|
||||
short_description: str | None = None
|
||||
tile: str | None = None
|
||||
upgrade_hash: str | None = ""
|
||||
|
||||
@property
|
||||
def module_name(self) -> str:
|
||||
if self.is_upgrade_extension:
|
||||
return f"{self.code}-{self.upgrade_hash}"
|
||||
|
||||
if settings.has_default_extension_path:
|
||||
return f"lnbits.extensions.{self.code}"
|
||||
return self.code
|
||||
|
||||
@property
|
||||
def is_upgrade_extension(self) -> bool:
|
||||
if self.is_wasm:
|
||||
return False
|
||||
return self.upgrade_hash != ""
|
||||
|
||||
@classmethod
|
||||
def from_installable_ext(cls, ext_info: InstallableExtension) -> Extension:
|
||||
return Extension(
|
||||
|
|
@ -287,7 +277,6 @@ class Extension(BaseModel):
|
|||
name=ext_info.name,
|
||||
short_description=ext_info.short_description,
|
||||
tile=_extension_tile(ext_info),
|
||||
upgrade_hash=ext_info.hash if ext_info.ext_upgrade_dir.is_dir() else "",
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -564,9 +553,6 @@ class InstallableExtension(BaseModel):
|
|||
|
||||
@property
|
||||
def module_name(self) -> str:
|
||||
if self.ext_upgrade_dir.is_dir():
|
||||
return f"{self.id}-{self.hash}"
|
||||
|
||||
if settings.has_default_extension_path:
|
||||
return f"lnbits.extensions.{self.id}"
|
||||
return self.id
|
||||
|
|
@ -720,6 +706,7 @@ class InstallableExtension(BaseModel):
|
|||
|
||||
shutil.rmtree(self.ext_dir, True)
|
||||
shutil.copytree(Path(self.ext_upgrade_dir), Path(self.ext_dir))
|
||||
shutil.rmtree(self.ext_upgrade_dir, True)
|
||||
logger.info(f"Extension {self.name} ({self.installed_version}) extracted.")
|
||||
|
||||
def extract_wasm_archive(self):
|
||||
|
|
|
|||
|
|
@ -233,15 +233,10 @@ async def install_extension(
|
|||
else:
|
||||
await update_installed_extension(ext_info)
|
||||
|
||||
extension = Extension.from_installable_ext(ext_info)
|
||||
if extension.is_upgrade_extension:
|
||||
# call stop while the old routes are still active
|
||||
if installed_ext:
|
||||
await stop_extension_background_work(ext_info.id)
|
||||
|
||||
if not extension.is_wasm:
|
||||
await start_extension_background_work(ext_info.id)
|
||||
|
||||
return extension
|
||||
return Extension.from_installable_ext(ext_info)
|
||||
|
||||
|
||||
async def check_extensions_limit(installed_ext: InstallableExtension | None = None):
|
||||
|
|
@ -683,16 +678,16 @@ async def stop_extension_background_work(ext_id: str) -> bool:
|
|||
if is_wasm_extension_id(ext_id):
|
||||
return True
|
||||
|
||||
upgrade_hash = settings.extension_upgrade_hash(ext_id)
|
||||
ext = Extension(code=ext_id, is_valid=True, upgrade_hash=upgrade_hash)
|
||||
ext = Extension(code=ext_id, is_valid=True)
|
||||
module_name = ext.module_name
|
||||
|
||||
try:
|
||||
logger.info(f"Stopping background work for extension '{ext.module_name}'.")
|
||||
old_module = importlib.import_module(ext.module_name)
|
||||
logger.info(f"Stopping background work for extension '{module_name}'.")
|
||||
old_module = importlib.import_module(module_name)
|
||||
|
||||
stop_fn_name = f"{ext_id}_stop"
|
||||
if not hasattr(old_module, stop_fn_name):
|
||||
raise ValueError(f"No stop function found for '{ext.module_name}'.")
|
||||
raise ValueError(f"No stop function found for '{module_name}'.")
|
||||
|
||||
stop_fn = getattr(old_module, stop_fn_name)
|
||||
if stop_fn:
|
||||
|
|
@ -700,9 +695,9 @@ async def stop_extension_background_work(ext_id: str) -> bool:
|
|||
await stop_fn()
|
||||
else:
|
||||
stop_fn()
|
||||
logger.info(f"Stopped background work for extension '{ext.module_name}'.")
|
||||
logger.info(f"Stopped background work for extension '{module_name}'.")
|
||||
except Exception as ex:
|
||||
logger.warning(f"Failed to stop background work for '{ext.module_name}'.")
|
||||
logger.warning(f"Failed to stop background work for '{module_name}'.")
|
||||
logger.warning(ex)
|
||||
return False
|
||||
|
||||
|
|
@ -718,12 +713,12 @@ async def start_extension_background_work(ext_id: str) -> bool:
|
|||
if is_wasm_extension_id(ext_id):
|
||||
return False
|
||||
|
||||
upgrade_hash = settings.extension_upgrade_hash(ext_id)
|
||||
ext = Extension(code=ext_id, is_valid=True, upgrade_hash=upgrade_hash)
|
||||
ext = Extension(code=ext_id, is_valid=True)
|
||||
module_name = ext.module_name
|
||||
|
||||
try:
|
||||
logger.info(f"Starting background work for extension '{ext.module_name}'.")
|
||||
new_module = importlib.import_module(ext.module_name)
|
||||
logger.info(f"Starting background work for extension '{module_name}'.")
|
||||
new_module = importlib.import_module(module_name)
|
||||
start_fn_name = f"{ext_id}_start"
|
||||
|
||||
# start function is optional, return False if not found
|
||||
|
|
@ -736,10 +731,10 @@ async def start_extension_background_work(ext_id: str) -> bool:
|
|||
await start_fn()
|
||||
else:
|
||||
start_fn()
|
||||
logger.info(f"Started background work for extension '{ext.module_name}'.")
|
||||
logger.info(f"Started background work for extension '{module_name}'.")
|
||||
return True
|
||||
except Exception as ex:
|
||||
logger.warning(f"Failed to start background work for '{ext.module_name}'.")
|
||||
logger.warning(f"Failed to start background work for '{module_name}'.")
|
||||
logger.warning(ex)
|
||||
return False
|
||||
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ def register_wasm_extension(app: FastAPI, ext_id: str) -> WasmExtension:
|
|||
|
||||
core_app_extra.wasm_extension_registry.register(loaded)
|
||||
|
||||
settings.activate_extension_paths(ext_id, "", [])
|
||||
settings.activate_extension_paths(ext_id, [])
|
||||
module_size = _format_wasm_extension_size(loaded.module_path.stat().st_size)
|
||||
load_seconds = perf_counter() - load_started_at
|
||||
logger.info(
|
||||
|
|
|
|||
|
|
@ -310,12 +310,7 @@ def get_api_routes(routes: list) -> dict[str, str]:
|
|||
|
||||
def path_segments(path: str) -> list[str]:
|
||||
path = path.strip("/")
|
||||
segments = path.split("/")
|
||||
if len(segments) < 2:
|
||||
return segments
|
||||
if segments[0] == "upgrades":
|
||||
return segments[2:]
|
||||
return segments[0:]
|
||||
return path.split("/")
|
||||
|
||||
|
||||
def normalize_path(path: str | None) -> str:
|
||||
|
|
|
|||
|
|
@ -51,14 +51,6 @@ class InstalledExtensionMiddleware:
|
|||
await self.app(scope, receive, send)
|
||||
return
|
||||
|
||||
# re-route all trafic if the extension has been upgraded
|
||||
if top_path in settings.lnbits_upgraded_extensions:
|
||||
upgrade_path = (
|
||||
f"""{settings.lnbits_upgraded_extensions[top_path]}/{top_path}"""
|
||||
)
|
||||
tail = "/".join(rest)
|
||||
scope["path"] = f"/upgrades/{upgrade_path}/{tail}"
|
||||
|
||||
await self.app(scope, receive, send)
|
||||
|
||||
def _response_by_accepted_type(
|
||||
|
|
|
|||
|
|
@ -199,8 +199,6 @@ class ExchangeRateProvider(BaseModel):
|
|||
class InstalledExtensionsSettings(LNbitsSettings):
|
||||
# installed extensions that have been deactivated
|
||||
lnbits_deactivated_extensions: set[str] = Field(default=set())
|
||||
# upgraded extensions that require API redirects
|
||||
lnbits_upgraded_extensions: dict[str, str] = Field(default={})
|
||||
# list of redirects that extensions want to perform
|
||||
lnbits_extensions_redirects: list[RedirectPath] = Field(default=[])
|
||||
|
||||
|
|
@ -223,18 +221,10 @@ class InstalledExtensionsSettings(LNbitsSettings):
|
|||
def activate_extension_paths(
|
||||
self,
|
||||
ext_id: str,
|
||||
upgrade_hash: str | None = None,
|
||||
ext_redirects: list[dict] | None = None,
|
||||
):
|
||||
self.lnbits_deactivated_extensions.discard(ext_id)
|
||||
|
||||
"""
|
||||
Update the list of upgraded extensions. The middleware will perform
|
||||
redirects based on this
|
||||
"""
|
||||
if upgrade_hash:
|
||||
self.lnbits_upgraded_extensions[ext_id] = upgrade_hash
|
||||
|
||||
if ext_redirects:
|
||||
self._activate_extension_redirects(ext_id, ext_redirects)
|
||||
|
||||
|
|
@ -244,9 +234,6 @@ class InstalledExtensionsSettings(LNbitsSettings):
|
|||
self.lnbits_deactivated_extensions.add(ext_id)
|
||||
self._remove_extension_redirects(ext_id)
|
||||
|
||||
def extension_upgrade_hash(self, ext_id: str) -> str:
|
||||
return settings.lnbits_upgraded_extensions.get(ext_id, "")
|
||||
|
||||
def _activate_extension_redirects(self, ext_id: str, ext_redirects: list[dict]):
|
||||
ext_redirect_paths = [
|
||||
RedirectPath(**{"ext_id": ext_id, **er}) for er in ext_redirects
|
||||
|
|
|
|||
|
|
@ -259,9 +259,7 @@ def test_get_api_routes_extracts_v1_paths():
|
|||
|
||||
def test_path_and_case_helpers():
|
||||
assert path_segments("/wallet/path") == ["wallet", "path"]
|
||||
assert path_segments("/upgrades/ext/assets/app.js") == ["assets", "app.js"]
|
||||
assert normalize_path(None) == "/"
|
||||
assert normalize_path("/upgrades/ext/assets/app.js") == "/assets/app.js"
|
||||
assert normalize_endpoint("example.com/") == "https://example.com"
|
||||
assert normalize_endpoint("ws://socket.example.com") == "ws://socket.example.com"
|
||||
assert (
|
||||
|
|
|
|||
|
|
@ -77,6 +77,9 @@ async def test_install_extension_creates_new_extension_and_starts_background_wor
|
|||
"lnbits.core.services.extensions.start_extension_background_work",
|
||||
mocker.AsyncMock(return_value=True),
|
||||
)
|
||||
mocker.patch(
|
||||
"lnbits.core.services.extensions.core_app_extra.register_new_ext_routes"
|
||||
)
|
||||
mocker.patch(
|
||||
"lnbits.core.services.extensions.get_db_version",
|
||||
mocker.AsyncMock(return_value=0),
|
||||
|
|
@ -91,6 +94,7 @@ async def test_install_extension_creates_new_extension_and_starts_background_wor
|
|||
settings.lnbits_extensions_path = str(tmp_path / "code")
|
||||
|
||||
extension = await install_extension(ext_info)
|
||||
await activate_extension(extension) # starts background task
|
||||
stored = await get_installed_extension(ext_id)
|
||||
finally:
|
||||
await delete_installed_extension(ext_id=ext_id)
|
||||
|
|
@ -126,6 +130,9 @@ async def test_install_extension_updates_existing_upgrade_and_preserves_payments
|
|||
"lnbits.core.services.extensions.stop_extension_background_work",
|
||||
mocker.AsyncMock(return_value=True),
|
||||
)
|
||||
mocker.patch(
|
||||
"lnbits.core.services.extensions.core_app_extra.register_new_ext_routes"
|
||||
)
|
||||
mocker.patch(
|
||||
"lnbits.core.services.extensions.get_db_version",
|
||||
mocker.AsyncMock(return_value=1),
|
||||
|
|
@ -139,9 +146,8 @@ async def test_install_extension_updates_existing_upgrade_and_preserves_payments
|
|||
settings.lnbits_data_folder = str(tmp_path / "data")
|
||||
settings.lnbits_extensions_path = str(tmp_path / "code")
|
||||
await create_installed_extension(existing_ext)
|
||||
updated_ext.ext_upgrade_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
extension = await install_extension(updated_ext, skip_download=True)
|
||||
await activate_extension(extension) # starts background task
|
||||
stored = await get_installed_extension(ext_id)
|
||||
finally:
|
||||
await delete_installed_extension(ext_id=ext_id)
|
||||
|
|
@ -149,7 +155,6 @@ async def test_install_extension_updates_existing_upgrade_and_preserves_payments
|
|||
settings.lnbits_extensions_path = original_extensions_path
|
||||
|
||||
assert extension.code == ext_id
|
||||
assert extension.is_upgrade_extension is True
|
||||
assert stored is not None
|
||||
assert stored.meta is not None
|
||||
assert stored.meta.payments == [existing_payment]
|
||||
|
|
|
|||
|
|
@ -277,16 +277,11 @@ def test_installed_extensions_settings_activate_and_deactivate_paths():
|
|||
}
|
||||
]
|
||||
|
||||
installed.activate_extension_paths(
|
||||
"lnurlp",
|
||||
upgrade_hash="hash123",
|
||||
ext_redirects=redirects,
|
||||
)
|
||||
installed.activate_extension_paths("lnurlp", ext_redirects=redirects)
|
||||
|
||||
redirect = installed.find_extension_redirect("/.well-known/lnurlp", [])
|
||||
assert redirect is not None
|
||||
assert redirect.ext_id == "lnurlp"
|
||||
assert installed.lnbits_upgraded_extensions["lnurlp"] == "hash123"
|
||||
assert "lnurlp" in installed.lnbits_installed_extensions_ids
|
||||
|
||||
installed.deactivate_extension_paths("lnurlp")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue