Exemple #1
0
 def __init__(self):
     engine_options = {
         "pool_timeout": config.get("DB_POOL_TIMEOUT", 30),
         "pool_recycle": config.get("DB_POOL_RECYCLE", -1),
         "pool_size": config.get("DB_POOL_SIZE", 5),
         "max_overflow": config.get("DB_MAX_OVERFLOW", 10),
         "pool_pre_ping": config.get("DB_POOL_PRE_PING", False),
     }
     super().__init__(declarative_base=Base, engine_options=engine_options)
Exemple #2
0
    def setup(self):
        host = config.get("GRPC_BIND_HOST", "0.0.0.0")
        port = config.get("GRPC_BIND_PORT", 50051)
        ssl = SslConfig(config.get("GRPC_SSL"))

        def spawn_thread(target, args=(), kwargs=None, name=None):
            self.container.spawn_managed_thread(
                lambda: target(*args, **kwargs or {}), identifier=name)

        self.channel = ServerChannel(host, port, ssl, spawn_thread,
                                     self.handle_request)
    def __init__(self, exception):

        self.exception = exception

        if config.get('MAX_RETRY_TIME') is not None:
            self.max_retry_time = config.get('MAX_RETRY_TIME')
        else:
            self.max_retry_time = 1

        if config.get('MAX_RETRY') is not None:
            self.max_retries = config.get('MAX_RETRY')
        else:
            self.max_retries = 1
    def setup(self):

        self.elasticsearch = Elasticsearch()
        self.index = config.get('ELASTIC_INDEX')

        host = config.get('ELASTIC_HOST') 
        port = config.get('ELASTIC_PORT') 
        username = config.get('ELASTIC_USER') 
        password = config.get('ELASTIC_PASS')

        self.elasticsearch = Elasticsearch([f"{host}:{port}"],
                http_auth=(username, password),        
                sniff_on_start=True,
                sniff_on_connection_fail=True,
                sniffer_timeout=60)
Exemple #5
0
    def auth_user(self, email, password):
        is_correct_password = self.storage.users.is_correct_password(
            email, password)

        if not is_correct_password:
            raise UserNotAuthorised("user not authorised for this request")

        # not the most ideal code but i want to keep the
        # requests here to storage quite easy to test
        # and maintain
        user_details = self.storage.users.get_from_email(email)

        if not user_details["verified"]:
            raise UserNotVerified("user is not verified")

        jwt_result = {
            "JWT":
            jwt.encode(
                {
                    "user_id": user_details["id"],
                    "email": user_details["email"],
                    # exp is number of seconds since epoch. 1 day expiry
                    "exp":
                    datetime.datetime.utcnow() + datetime.timedelta(days=1),
                },
                config.get("JWT_SECRET"),
                algorithm="HS256",
            ).decode("utf-8")
        }

        return jwt_result
    def __init__(self, method, url, expected_exceptions=(), **kwargs):
        super().__init__(method, url, expected_exceptions=expected_exceptions)

        cors = config.get("DEFAULT_CORS", "*").split(",")

        self.allowed_origin = kwargs.get("origin", cors)
        self.allowed_methods = kwargs.get("methods", ["*"])
        self.allow_credentials = kwargs.get("credentials", True)

        self.standard_mapped_errors_tuple = tuple(
            self.standard_mapped_errors.keys())

        self.rate_limit = kwargs.get("rate_limit")
        self.private_rate_limit = kwargs.get("private_rate_limit")
        self.auth_required = kwargs.get("auth_required", False)

        if self.rate_limit is not None and not self.auth_required:
            raise ValueError(
                "if public rate limit is defined then auth_required must be true"
            )

        if self.rate_limit and self.private_rate_limit:
            raise ValueError(
                "cant define an entrypoint with a public and private rate limit"
            )

        if self.rate_limit or self.private_rate_limit:
            store_redis_rate_limit_for_url(
                self.url, self.rate_limit or self.private_rate_limit)
    def setup(self):
        self.redis_client = walrus.Database().from_url(config.get("REDIS_URL"))
        self.consumer_group = self.redis_client.consumer_group(
            self.consumer_group, self.streams)

        self.consumer_group.create()

        # todo: investigate what this does!
        self.consumer_group.set_id(id="$")
Exemple #8
0
    def __init__(self, method, url, expected_exceptions=(), **kwargs):
        super().__init__(method, url, expected_exceptions=expected_exceptions)

        cors = config.get("DEFAULT_CORS", "*").split(",")

        self.allowed_origin = kwargs.get("origin", cors)
        self.allowed_methods = kwargs.get("methods", ["*"])
        self.allow_credentials = kwargs.get("credentials", True)

        self.standard_mapped_errors_tuple = tuple(self.standard_mapped_errors.keys())

        self.auth_required = kwargs.get("auth_required", False)
    def setup(self):
        self.connection = rabbitpy.Connection(config.get(AMQP_URI_KEY))
        self.channel = self.connection.channel()
        self.exchange = rabbitpy.Exchange(self.channel, 'exch_pi')
        self.exchange.declare()
        queue = rabbitpy.Queue(
            self.channel,
            'q_pi_buzzer',
            arguments={'x-message-ttl': 3600000})  # 1h

        queue.declare()
        queue.bind(self.exchange, ROUTING_KEY)
Exemple #10
0
    def __init__(self):
        # setup logging
        logging.basicConfig(
            stream=sys.stdout,
            # filename=config.get('DEFAULT', 'log_file'),
            level=logging.DEBUG,
            format='%(asctime)s - %(levelname)s - %(message)s',
            datefmt='%m/%d/%Y %I:%M:%S %p')

        self.db_engine = create_engine(config.get('db_connection'),
                                       connect_args={'connect_timeout': 60})
        DbHelper(self.db_engine).init_db()

        self.lcms = []
        self.s_orchs = []
def test_jwt_required_works_correctly_on_class(config):

    valid_token = jwt.encode({"test": "123"},
                             nameko_config.get("JWT_SECRET"),
                             algorithm="HS256")

    mock_request = Mock()

    mock_request.headers = {"Authorization": valid_token.decode("utf-8")}

    service = FakeService()

    result = service.fake_function(mock_request)

    assert result == {"test": "123"}
Exemple #12
0
    def page(self, last):
        limit = config.get(PAGINATION_KEY)
        if last is None:
            chunk = self.collection.find().limit(limit)
        else:
            chunk = self.collection.find({
                '_id': {
                    '$gt': ObjectId(last)
                }
            }).limit(limit)

        data = [x for x in chunk]

        if not data:
            return None, None

        last_id = str(data[-1]['_id'])
        return data, last_id
Exemple #13
0
    def __init__(self, exchange=None, declare=None, **publisher_options):
        super(SchedulerPublisher, self).__init__(exchange=exchange,
                                                 declare=declare,
                                                 **publisher_options)
        default_ssl = config.get(AMQP_SSL_CONFIG_KEY)
        ssl = self.publisher_options.pop("ssl", default_ssl)

        with get_connection(self.amqp_uri, ssl) as conn:
            for entity in self.declare:
                maybe_declare(entity, conn.channel())

        serializer = self.publisher_options.pop("serializer", "json")
        self.publisher = self.publisher_cls(self.amqp_uri,
                                            ssl=ssl,
                                            serializer=serializer,
                                            exchange=self.exchange,
                                            declare=self.declare,
                                            **self.publisher_options)
def test_jwt_required_raises_error_if_jwt_expired(config):
    mock_request = Mock()

    # exp is the expiry time epoch.
    # (1577208307 ~ 2019 - 12 - 24 @ 5:30pm UTC Christmas Eve)
    jwt.encode(
        {
            "test": "123",
            "exp": 1577208307
        },
        nameko_config.get("JWT_SECRET"),
        algorithm="HS256",
    )

    mock_request.headers = {"Authorization": "random token!"}

    service = FakeService()

    with pytest.raises(UserNotAuthorised):
        service.fake_function(mock_request)
Exemple #15
0
def default_error_from_exception(exc_info, code=None, message=None):
    """ Create a new GrpcError instance representing an underlying exception.

    If the `GRPC_DEBUG` key is set in the Nameko config, the `status` message will
    capture the underyling traceback in a `google.rpc.error_details.DebugInfo` message.
    """
    exc_type, exc, tb = exc_info

    code = code or StatusCode.UNKNOWN
    message = message or str(exc)

    status = Status(code=STATUS_CODE_ENUM_TO_INT_MAP[code], message=message)

    if config.get("GRPC_DEBUG"):
        debug_info = Any()
        debug_info.Pack(
            DebugInfo(
                stack_entries=traceback.format_exception(*exc_info), detail=str(exc),
            )
        )
        status.details.append(debug_info)

    return GrpcError(code=code, message=message, status=status)
        def decorator(*args, **kwargs):
            request = args[1]

            # https://security.stackexchange.com/a/205701
            jwt_header = get_jwt_header(request)

            if not jwt_header:
                raise UserNotAuthorised()
            try:
                jwt_data = jwt.decode(jwt_header,
                                      config.get("JWT_SECRET"),
                                      algorithms=["HS256"])
                request.jwt_data = jwt_data
                args = list(args)
                args[1] = request
                # todo: inject into request here!
            except ExpiredSignatureError:
                # todo: mainly here incase in the future we want to handle this
                # better for the flow for a user
                raise UserNotAuthorised()
            except InvalidTokenError:
                raise UserNotAuthorised()

            return fn(*args, **kwargs)
Exemple #17
0
 def setup(self):
     self.stripe = stripe
     self.stripe.api_key = config.get("STRIPE").get("API_KEY")
Exemple #18
0
 def __init__(self, *args, **kwargs):
     ssl = kwargs.pop("ssl", config.get("GRPC_SSL"))
     super().__init__(*args, ssl=ssl, **kwargs)
Exemple #19
0
def redis_client(test_config):
    client = redis.StrictRedis.from_url(config.get(REDIS_URI_KEY))
    yield client
    client.flushdb()
Exemple #20
0
 def setup(self):
     self.key = config.get("SENDGRID_KEY")
     self.web_app_address = config.get("WEB_APP_ADDRESS")
 def setup(self):
     self.client = redis.StrictRedis.from_url(config.get(REDIS_URI_KEY))
Exemple #22
0
 def setup(self):
     self.redis_uri = config.get("REDIS_URL", "redis://127.0.0.1:6379/0")
Exemple #23
0
def get_redis_connection():
    redis_url = config.get("REDIS_URL", "redis://127.0.0.1:6379/0")

    return walrus.Database().from_url(redis_url)
Exemple #24
0
 def setup(self):
     self.api_key = config.get("STRIPE").get("API_KEY")
Exemple #25
0
 def __init__(self):
     self.retries = config.get('nr_retries')
     self.retires_interval = config.get('retires_interval')
Exemple #26
0
import os

from apscheduler.schedulers.background import BackgroundScheduler
from kombu.common import maybe_declare
from nameko import config
from nameko.amqp.publish import get_connection
from nameko.constants import AMQP_SSL_CONFIG_KEY
from nameko.extensions import DependencyProvider
from nameko.messaging import encode_to_headers, Publisher
from nameko.standalone.events import get_event_exchange

from nameko_apscheduler.schema import SchedulerSchema
from nameko_apscheduler.utils import get, delete

EXCHANGE_NAME = config.get("APSCHDULER", {}).get("exchange_name",
                                                 "nameko-apscheduler")


class SchedulerPublisher(Publisher):
    def __init__(self, exchange=None, declare=None, **publisher_options):
        super(SchedulerPublisher, self).__init__(exchange=exchange,
                                                 declare=declare,
                                                 **publisher_options)
        default_ssl = config.get(AMQP_SSL_CONFIG_KEY)
        ssl = self.publisher_options.pop("ssl", default_ssl)

        with get_connection(self.amqp_uri, ssl) as conn:
            for entity in self.declare:
                maybe_declare(entity, conn.channel())

        serializer = self.publisher_options.pop("serializer", "json")
Exemple #27
0
 def setup(self):
     secret = config.get("JWT_SECRET")
     if not secret:
         raise ConfigurationError("Not found `JWT_SECRET`.")
     self.secret = secret
    def setup(self):

        self.host = config.get('REDIS_HOST')
        self.port = config.get('REDIS_PORT')
        self.db = config.get('REDIS_DB')
        self.password = config.get('REDIS_PASS')
Exemple #29
0
    def create_appointment(self, request):
        """
        Saves a new Appointment
        :param request: new Appointment that should be saved
        :return: 201 Detailed information for newly created Appointment
        :return: 404 No Treatment for given TreatmentID 
        :return: 409 Appointment Details do not match the requirements
        :return: 500 Connection Error  
        """

        appointment_detail = json.loads(request.get_data())

        if appointment_detail['start_time'] >= appointment_detail['end_time']:
            return 409, "Start time has to be before end time.\n"
        elif appointment_detail['start_time'] < 8 or appointment_detail[
                'start_time'] > 17 or appointment_detail[
                    'end_time'] < 9 or appointment_detail['end_time'] > 18:
            return 409, "Appointments are only available from 8 - 18. Please choose another timeslot.\n"

        try:
            URL = "http://" + config.get(
                'TREATMENTS_SERVICE') + "/treatments/" + str(
                    appointment_detail['treatment_id'])
            t_response = requests.get(url=URL)
            t_response.encoding = 'utf-8'
            treatment_data = literal_eval(t_response.text)
        except SyntaxError:
            return 404, "No Treatment exists for given TreatmentID.\n"
        except requests.exceptions.ConnectionError:
            return 500, "Could not read Treatments.\n"

        if treatment_data['minduration'] > (appointment_detail['end_time'] -
                                            appointment_detail['start_time']):
            return 409, "An appointment for " + treatment_data[
                'name'] + " takes at least " + str(
                    treatment_data['minduration']
                ) + " hour(s). Please choose another timeslot.\n"
        elif treatment_data['maxduration'] < (
                appointment_detail['end_time'] -
                appointment_detail['start_time']):
            return 409, "An appointment for " + treatment_data[
                'name'] + " takes maximum " + str(
                    treatment_data['maxduration']
                ) + " hour(s). Please choose another timeslot.\n"

        try:
            conflicts = 0
            appointments = self.db.session.query(Appointment).all()
            for appointment in appointments:
                if treatment_data['name'] == appointment.treatment_name:
                    if appointment_detail['date'] == appointment.date.strftime(
                            "%Y-%m-%d"):
                        if not (appointment_detail['start_time'] <=
                                appointment.start_time
                                and appointment_detail['end_time'] <=
                                appointment.start_time) and not (
                                    appointment.end_time <=
                                    appointment_detail['end_time']
                                    and appointment.end_time <=
                                    appointment_detail['start_time']):
                            conflicts += 1

            if conflicts == 0:
                newappointment = Appointment(
                    treatment_id=appointment_detail['treatment_id'],
                    treatment_name=treatment_data['name'],
                    customer_name=appointment_detail['customer_name'],
                    date=appointment_detail['date'],
                    start_time=appointment_detail['start_time'],
                    end_time=appointment_detail['end_time'],
                    duration=appointment_detail['end_time'] -
                    appointment_detail['start_time'])

                with self.db.get_session() as session:
                    session.add(newappointment)

                appointment_detail['treatment_name'] = treatment_data['name']
                self.dispatch("booked_appointment", appointment_detail)

                return 201, u"\nAppointment: {}".format(appointment_detail)
            else:
                return 409, "There were " + str(
                    conflicts
                ) + " conflicts with other appointments. Please choose a free timeslot.\n"

        except exc.SQLAlchemyError:
            return 500, "Could not save Appointment.\n"
Exemple #30
0
 def setup(self):
     self.client = MongoClient(config.get(MONGODB_URI_KEY))
     self.db = self.client.streams