Source code for ourobori.services.rules

"""
This module contains the base rules to be implemented as minimum in the Rules
of a service using the ourobori.
So the class ``Rules`` in your ``rules.py`` should inherit from the
:class:BaseRules`.
This module also contains the class :class:`CustomBasicAuth` which is used
when restricting the access to the service by as BasicAuth.
This authentication has to be defined in your ``Rules``-class in the attribute
``auths`` like the following:

.. code:: python

    class Rules(BaseRules):
        auths: CustomBasicAuth = CustomBasicAuth(
            valids=[('example_user', 'example_password')])

If you start your service with the flag ``--auth`` the BasicAuth-method
defined there will be used.
"""


from pathlib import Path
from typing import List
from typing import Tuple

import attr
import aiohttp
from aiohttp_basicauth import BasicAuth
from aiohttp_basicauth import BasicAuthMiddleware

from ourobori.docs.swagger import Contact
from ourobori.docs.swagger import License
from ourobori.services.errors import DatabaseNotAliveException
from ourobori.services.errors import InvalidInputException
from ourobori.services.errors import InvalidOutputException
from ourobori.services.errors import ServiceCallException
from ourobori.services.errors import ServiceNotAliveException
from ourobori.services.errors import DatabaseTransactionException
from ourobori.services.errors import HostCmdException
from ourobori.services.errors import HostConnectionException
from ourobori.services.errors import HostSCPFromException
from ourobori.services.errors import HostSCPToException


[docs]@attr.s(auto_attribs=True, slots=True, frozen=True) class AuthenticatedUser: """ Class representing an authenticated user to gain access to the service via http-basic-authentication. """ login: str password: str
[docs]@attr.s(auto_attribs=True) class CustomBasicAuth(BasicAuthMiddleware): """ Class to be used as middleware for basic-http-authentication to the service. """ valids: List[Tuple[str]] username: str = None password: str = None force: bool = True realm: str = ''
[docs] async def check_credentials(self, username: str, password: str) -> bool: """ Checks if the user-password-combination is allowed to access the service. Parameters: username: The username to check in combination with the password password: The password to check in combination with the username Returns: valid: Check if the username-password-combination is allowed to access the service. """ valid = (username, password) in self.valids return valid
[docs]@attr.s(auto_attribs=True, slots=True, frozen=True) class BaseRules: """ These rules are the minimal rules to implement in the Rules of a service using the ourobori. Attributes: servicename: The name of the service port: The default port for the service to listen. Can be overwritten by the ``--port`` argument on service-startup. version: The version of the service. description: The description for the service. contact: The contact-person for the service. logfile: Path where to store the logfiles of the service. Can be overwritten by the ``--logfile`` argument on service-startup. hostname: The hostname of the host this service is run. license: The license for this service. auths: The allowed users to access the service. logstash_conn: The connection to logstash as ``<HOSTNANE>:<PORT>``. apidoc_location: The location of the built apidoc. client_apidoc_location: The location of the built client-apidoc. handle_exceptions: The exceptions to handle in the error-middleware at :func:`ourobori.apps.middlewares.error_middleware`. The defined exceptions have to inherit from :class:`ourobori.services.errors.BaseError`. """ servicename: str port: int version: str description: str contact: Contact logfile: Path hostname: str license: License = License(name='', url='') auths: BasicAuth = None logstash_conn: str = None apidoc_location: Path = Path('.') / 'doc/ext/build/index.html' client_apidoc_location: Path = Path('.') / 'doc/int/build/index.html' handle_exceptions: tuple = ( DatabaseNotAliveException, DatabaseTransactionException, HostCmdException, HostConnectionException, HostSCPFromException, HostSCPToException, InvalidInputException, InvalidOutputException, ServiceCallException, ServiceNotAliveException, )
__all__ = ['AuthenticatedUser', 'BaseRules', 'CustomBasicAuth']