Update aioHttp3 (#403)

* Update aioHttp3

* fix line ending

* fix close session
This commit is contained in:
Pascal Vizeli 2018-03-12 23:40:06 +01:00 committed by GitHub
parent 073166190f
commit 524d875516
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 97 additions and 92 deletions

View File

@ -13,7 +13,7 @@ from .proxy import APIProxy
from .supervisor import APISupervisor from .supervisor import APISupervisor
from .snapshots import APISnapshots from .snapshots import APISnapshots
from .services import APIServices from .services import APIServices
from .security import security_layer from .security import SecurityMiddleware
from ..coresys import CoreSysAttributes from ..coresys import CoreSysAttributes
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -25,16 +25,14 @@ class RestAPI(CoreSysAttributes):
def __init__(self, coresys): def __init__(self, coresys):
"""Initialize docker base wrapper.""" """Initialize docker base wrapper."""
self.coresys = coresys self.coresys = coresys
self.security = SecurityMiddleware(coresys)
self.webapp = web.Application( self.webapp = web.Application(
middlewares=[security_layer], loop=self._loop) middlewares=[self.security.token_validation], loop=self._loop)
# service stuff # service stuff
self._handler = None self._handler = None
self.server = None self.server = None
# middleware
self.webapp['coresys'] = coresys
async def load(self): async def load(self):
"""Register REST API Calls.""" """Register REST API Calls."""
self._register_supervisor() self._register_supervisor()

View File

@ -83,7 +83,7 @@ class APIProxy(CoreSysAttributes):
if not data: if not data:
await response.write_eof() await response.write_eof()
break break
response.write(data) await response.write(data)
except aiohttp.ClientError: except aiohttp.ClientError:
await response.write_eof() await response.write_eof()

View File

@ -6,6 +6,7 @@ from aiohttp.web import middleware
from aiohttp.web_exceptions import HTTPUnauthorized from aiohttp.web_exceptions import HTTPUnauthorized
from ..const import HEADER_TOKEN, REQUEST_FROM from ..const import HEADER_TOKEN, REQUEST_FROM
from ..coresys import CoreSysAttributes
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -16,10 +17,16 @@ NO_SECURITY_CHECK = set((
)) ))
class SecurityMiddleware(CoreSysAttributes):
"""Security middleware functions."""
def __init__(self, coresys):
"""Initialize security middleware."""
self.coresys = coresys
@middleware @middleware
async def security_layer(request, handler): async def token_validation(self, request, handler):
"""Check security access of this layer.""" """Check security access of this layer."""
coresys = request.app['coresys']
hassio_token = request.headers.get(HEADER_TOKEN) hassio_token = request.headers.get(HEADER_TOKEN)
# Ignore security check # Ignore security check
@ -35,13 +42,13 @@ async def security_layer(request, handler):
return await handler(request) return await handler(request)
# Home-Assistant # Home-Assistant
if hassio_token == coresys.homeassistant.uuid: if hassio_token == self._homeassistant.uuid:
_LOGGER.debug("%s access from Home-Assistant", request.path) _LOGGER.debug("%s access from Home-Assistant", request.path)
request[REQUEST_FROM] = 'homeassistant' request[REQUEST_FROM] = 'homeassistant'
return await handler(request) return await handler(request)
# Add-on # Add-on
addon = coresys.addons.from_uuid(hassio_token) addon = self._addons.from_uuid(hassio_token)
if addon: if addon:
_LOGGER.info("%s access from %s", request.path, addon.slug) _LOGGER.info("%s access from %s", request.path, addon.slug)
request[REQUEST_FROM] = addon.slug request[REQUEST_FROM] = addon.slug

View File

@ -108,10 +108,10 @@ class HassIO(CoreSysAttributes):
# don't process scheduler anymore # don't process scheduler anymore
self._scheduler.suspend = True self._scheduler.suspend = True
# process stop tasks
self._websession.close()
self._websession_ssl.close()
# process async stop tasks # process async stop tasks
await asyncio.wait( await asyncio.wait([
[self._api.stop(), self._dns.stop()], loop=self._loop) self._api.stop(),
self._dns.stop(),
self._websession.close(),
self._websession_ssl.close()
], loop=self._loop)

View File

@ -41,8 +41,8 @@ setup(
include_package_data=True, include_package_data=True,
install_requires=[ install_requires=[
'async_timeout==2.0.0', 'async_timeout==2.0.0',
'aiohttp==2.3.10', 'aiohttp==3.0.7',
'docker==3.1.0', 'docker==3.1.1',
'colorlog==3.1.2', 'colorlog==3.1.2',
'voluptuous==0.11.1', 'voluptuous==0.11.1',
'gitpython==2.1.8', 'gitpython==2.1.8',