Fix add-on updates (#2632)

* Fix add-on updates

* Add to security role
This commit is contained in:
Joakim Sørensen 2021-02-25 23:29:39 +01:00 committed by GitHub
parent 31f5033dca
commit 9b569268ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 17 deletions

View File

@ -387,12 +387,6 @@ class APIAddons(CoreSysAttributes):
ATTR_BLK_WRITE: stats.blk_write, ATTR_BLK_WRITE: stats.blk_write,
} }
@api_process
def install(self, request: web.Request) -> Awaitable[None]:
"""Install add-on."""
addon = self._extract_addon(request)
return asyncio.shield(addon.install())
@api_process @api_process
def uninstall(self, request: web.Request) -> Awaitable[None]: def uninstall(self, request: web.Request) -> Awaitable[None]:
"""Uninstall add-on.""" """Uninstall add-on."""
@ -411,12 +405,6 @@ class APIAddons(CoreSysAttributes):
addon = self._extract_addon_installed(request) addon = self._extract_addon_installed(request)
return asyncio.shield(addon.stop()) return asyncio.shield(addon.stop())
@api_process
def update(self, request: web.Request) -> Awaitable[None]:
"""Update add-on."""
addon: Addon = self._extract_addon_installed(request)
return asyncio.shield(addon.update())
@api_process @api_process
def restart(self, request: web.Request) -> Awaitable[None]: def restart(self, request: web.Request) -> Awaitable[None]:
"""Restart add-on.""" """Restart add-on."""

View File

@ -99,6 +99,7 @@ ADDONS_ROLE_ACCESS = {
r"|/os/.+" r"|/os/.+"
r"|/resolution/.+" r"|/resolution/.+"
r"|/snapshots.*" r"|/snapshots.*"
r"|/store.*"
r"|/supervisor/.+" r"|/supervisor/.+"
r")$" r")$"
), ),

View File

@ -4,6 +4,7 @@ from typing import Any, Awaitable, Dict, List
from aiohttp import web from aiohttp import web
from ..addons import AnyAddon
from ..api.utils import api_process from ..api.utils import api_process
from ..const import ( from ..const import (
ATTR_ADDONS, ATTR_ADDONS,
@ -36,12 +37,18 @@ from ..store.repository import Repository
class APIStore(CoreSysAttributes): class APIStore(CoreSysAttributes):
"""Handle RESTful API for store functions.""" """Handle RESTful API for store functions."""
def _extract_addon(self, request: web.Request) -> AddonStore: def _extract_addon(self, request: web.Request, installed=False) -> AnyAddon:
"""Return add-on, throw an exception it it doesn't exist.""" """Return add-on, throw an exception it it doesn't exist."""
addon_slug: str = request.match_info.get("addon") addon_slug: str = request.match_info.get("addon")
addon_version: str = request.match_info.get("version", "latest") addon_version: str = request.match_info.get("version", "latest")
addon = self.sys_addons.store.get(addon_slug) if installed:
addon = self.sys_addons.local.get(addon_slug)
if addon is None or not addon.is_installed:
raise APIError(f"Addon {addon_slug} is not installed")
else:
addon = self.sys_addons.store.get(addon_slug)
if not addon: if not addon:
raise APIError( raise APIError(
f"Addon {addon_slug} with version {addon_version} does not exist in the store" f"Addon {addon_slug} with version {addon_version} does not exist in the store"
@ -128,9 +135,7 @@ class APIStore(CoreSysAttributes):
@api_process @api_process
def addons_addon_update(self, request: web.Request) -> Awaitable[None]: def addons_addon_update(self, request: web.Request) -> Awaitable[None]:
"""Update add-on.""" """Update add-on."""
addon = self._extract_addon(request) addon = self._extract_addon(request, installed=True)
if not addon.is_installed:
raise APIError(f"Addon {addon.slug} is not installed")
return asyncio.shield(addon.update()) return asyncio.shield(addon.update())
@api_process @api_process