Ejemplo n.º 1
0
    def on_post(self, req, resp, tenant_id, validated_body):
        """
        This method resets a token when an HTTP POST is received.  There is
        a minimum time limit that must be reached before resetting a token,
        unless the call is made  with the "invalidate_now: true" option.
        """

        body = validated_body['token']

        #verify the tenant exists
        tenant = tenant_util.find_tenant(tenant_id=tenant_id)

        if not tenant:
            _tenant_not_found()

        invalidate_now = body['invalidate_now']

        if invalidate_now:
            #immediately invalidate the token
            tenant.token.reset_token_now()

        else:
            self._validate_token_min_time_limit_reached(tenant.token)
            tenant.token.reset_token()

        #save the tenant document
        tenant_util.save_tenant(tenant)

        resp.status = falcon.HTTP_203
        resp.set_header('Location', '/v1/{0}/token'.format(tenant_id))
Ejemplo n.º 2
0
 def test_find_event_producer_by_name_returns_instance(self):
     with patch('meniscus.data.model.tenant_util._db_handler',
                self.ds_handler):
         tenant = tenant_util.find_tenant('12345')
         producer = tenant_util.find_event_producer(
             tenant, producer_name='system.auth')
         self.assertIsInstance(producer, EventProducer)
Ejemplo n.º 3
0
 def test_find_event_producer_by_name_returns_none(self):
     with patch('meniscus.data.model.tenant_util._db_handler',
                self.ds_handler):
         tenant = tenant_util.find_tenant('12345')
         producer = tenant_util.find_event_producer(
             tenant, producer_name='not_name')
         self.assertEquals(producer, None)
Ejemplo n.º 4
0
 def test_find_event_producer_by_name_returns_none(self):
     with patch('meniscus.data.model.tenant_util._db_handler',
                self.ds_handler):
         tenant = tenant_util.find_tenant('12345')
         producer = tenant_util.find_event_producer(
             tenant, producer_name='not_name')
         self.assertEquals(producer, None)
Ejemplo n.º 5
0
    def on_post(self, req, resp, tenant_id, validated_body):
        """
        This method resets a token when an HTTP POST is received.  There is
        a minimum time limit that must be reached before resetting a token,
        unless the call is made  with the "invalidate_now: true" option.
        """

        body = validated_body['token']

        #verify the tenant exists
        tenant = tenant_util.find_tenant(tenant_id=tenant_id)

        if not tenant:
            _tenant_not_found()

        invalidate_now = body['invalidate_now']

        if invalidate_now:
            #immediately invalidate the token
            tenant.token.reset_token_now()

        else:
            self._validate_token_min_time_limit_reached(tenant.token)
            tenant.token.reset_token()

        #save the tenant document
        tenant_util.save_tenant(tenant)

        resp.status = falcon.HTTP_203
        resp.set_header('Location', '/v1/{0}/token'.format(tenant_id))
Ejemplo n.º 6
0
 def test_find_event_producer_by_name_returns_instance(self):
     with patch('meniscus.data.model.tenant_util._db_handler',
                self.ds_handler):
         tenant = tenant_util.find_tenant('12345')
         producer = tenant_util.find_event_producer(
             tenant, producer_name='system.auth')
         self.assertIsInstance(producer, EventProducer)
Ejemplo n.º 7
0
 def test_find_tenant_creates_new_tenant_on_no_tenant_found(self):
     retrieve_tenant_call = MagicMock(side_effect=[None, self.tenant_obj])
     create_tenant_call = MagicMock()
     with patch('meniscus.data.model.tenant_util.retrieve_tenant',
                retrieve_tenant_call), patch(
                    'meniscus.data.model.tenant_util.create_tenant',
                    create_tenant_call):
         tenant = tenant_util.find_tenant('unknown_tenant_id',
                                          create_on_missing=True)
         self.assertIsInstance(tenant, Tenant)
         create_tenant_call.assert_called_once_with('unknown_tenant_id')
Ejemplo n.º 8
0
 def test_find_tenant_creates_new_tenant_on_no_tenant_found(self):
     retrieve_tenant_call = MagicMock(side_effect=[None, self.tenant_obj])
     create_tenant_call = MagicMock()
     with patch('meniscus.data.model.tenant_util.retrieve_tenant',
                retrieve_tenant_call), patch(
             'meniscus.data.model.tenant_util.create_tenant',
             create_tenant_call):
         tenant = tenant_util.find_tenant(
             'unknown_tenant_id', create_on_missing=True)
         self.assertIsInstance(tenant, Tenant)
         create_tenant_call.assert_called_once_with('unknown_tenant_id')
Ejemplo n.º 9
0
    def on_get(self, req, resp, tenant_id):
        """
        Retrieve a specified tenant when a HTTP GET is received
        """
        tenant = tenant_util.find_tenant(tenant_id=tenant_id,
                                         create_on_missing=True)

        if not tenant:
            _tenant_not_found()

        resp.status = falcon.HTTP_200
        resp.body = api.format_response_body({'tenant': tenant.format()})
Ejemplo n.º 10
0
    def on_get(self, req, resp, tenant_id):
        """
        Retrieve a specified tenant when a HTTP GET is received
        """
        tenant = tenant_util.find_tenant(
            tenant_id=tenant_id, create_on_missing=True)

        if not tenant:
            _tenant_not_found()

        resp.status = falcon.HTTP_200
        resp.body = api.format_response_body({'tenant': tenant.format()})
Ejemplo n.º 11
0
    def on_get(self, req, resp, tenant_id):
        """
        Retrieve a list of all Event Producers for a specified Tenant
        """
        tenant = tenant_util.find_tenant(tenant_id=tenant_id)

        if not tenant:
            _tenant_not_found()

        resp.status = falcon.HTTP_200
        resp.body = api.format_response_body(
            {'event_producers': [p.format() for p in tenant.event_producers]})
Ejemplo n.º 12
0
    def on_get(self, req, resp, tenant_id):
        """
        Retrieve a list of all Event Producers for a specified Tenant
        """
        tenant = tenant_util.find_tenant(tenant_id=tenant_id)

        if not tenant:
            _tenant_not_found()

        resp.status = falcon.HTTP_200
        resp.body = api.format_response_body(
            {'event_producers': [p.format() for p in tenant.event_producers]})
Ejemplo n.º 13
0
    def on_post(self, req, resp, tenant_id, validated_body):
        """
        Create a a new event Producer for a specified Tenant
        when an HTTP Post is received
        """
        body = validated_body['event_producer']

        tenant = tenant_util.find_tenant(tenant_id=tenant_id)

        if not tenant:
            _tenant_not_found()

        event_producer_name = body['name']
        event_producer_pattern = body['pattern']

        #if durable or encrypted aren't specified, set to False
        if 'durable' in body.keys():
            event_producer_durable = body['durable']
        else:
            event_producer_durable = False

        if 'encrypted' in body.keys():
            event_producer_encrypted = body['encrypted']
        else:
            event_producer_encrypted = False

        if 'sinks' in body.keys():
            event_producer_sinks = body['sinks']
        else:
            event_producer_sinks = None

        # Check if the tenant already has an event producer with this name
        producer = tenant_util.find_event_producer(
            tenant, producer_name=event_producer_name)
        if producer:
            api.abort(
                falcon.HTTP_400,
                'Event producer with name {0} already exists with id={1}.'
                .format(producer.name, producer.get_id()))

        # Create the new profile for the host
        producer_id = tenant_util.create_event_producer(
            tenant,
            event_producer_name,
            event_producer_pattern,
            event_producer_durable,
            event_producer_encrypted,
            event_producer_sinks)

        resp.status = falcon.HTTP_201
        resp.set_header('Location',
                        '/v1/{0}/producers/{1}'
                        .format(tenant_id, producer_id))
Ejemplo n.º 14
0
    def on_get(self, req, resp, tenant_id):
        """
        Retrieves Token information for a specified Tenant
        when an HTTP GET call is received
        """

        #verify the tenant exists
        tenant = tenant_util.find_tenant(tenant_id=tenant_id)

        if not tenant:
            _tenant_not_found()

        resp.status = falcon.HTTP_200
        resp.body = api.format_response_body({'token': tenant.token.format()})
Ejemplo n.º 15
0
    def on_get(self, req, resp, tenant_id):
        """
        Retrieves Token information for a specified Tenant
        when an HTTP GET call is received
        """

        #verify the tenant exists
        tenant = tenant_util.find_tenant(tenant_id=tenant_id)

        if not tenant:
            _tenant_not_found()

        resp.status = falcon.HTTP_200
        resp.body = api.format_response_body({'token': tenant.token.format()})
Ejemplo n.º 16
0
    def on_put(self, req, resp, tenant_id, event_producer_id, validated_body):
        """
        Make an update to a specified Event Producer's configuration
        when an HTTP PUT is received
        """

        body = validated_body['event_producer']

        #verify the tenant exists
        tenant = tenant_util.find_tenant(tenant_id=tenant_id)

        if not tenant:
            _tenant_not_found()

        #verify the event_producer exists and belongs to the tenant
        event_producer = tenant_util.find_event_producer(
            tenant, producer_id=event_producer_id)
        if not event_producer:
            _producer_not_found()

        #if a key is present, update the event_producer with the value
        if 'name' in body.keys() and event_producer.name != body['name']:
            #if the tenant already has a profile with this name then abort
            duplicate_producer = tenant_util.find_event_producer(
                tenant,  producer_name=body['name'])
            if duplicate_producer:
                api.abort(
                    falcon.HTTP_400,
                    'EventProducer with name {0} already exists with id={1}.'
                    .format(duplicate_producer.name,
                            duplicate_producer.get_id()))
            event_producer.name = body['name']

        if 'pattern' in body:
            event_producer.pattern = str(body['pattern'])

        if 'durable' in body:
            event_producer.durable = body['durable']

        if 'encrypted' in body:
            event_producer.encrypted = body['encrypted']

        if 'sinks' in body:
            event_producer.sinks = body['sinks']

        #save the tenant document
        tenant_util.save_tenant(tenant)

        resp.status = falcon.HTTP_200
Ejemplo n.º 17
0
    def on_put(self, req, resp, tenant_id, event_producer_id, validated_body):
        """
        Make an update to a specified Event Producer's configuration
        when an HTTP PUT is received
        """

        body = validated_body['event_producer']

        #verify the tenant exists
        tenant = tenant_util.find_tenant(tenant_id=tenant_id)

        if not tenant:
            _tenant_not_found()

        #verify the event_producer exists and belongs to the tenant
        event_producer = tenant_util.find_event_producer(
            tenant, producer_id=event_producer_id)
        if not event_producer:
            _producer_not_found()

        #if a key is present, update the event_producer with the value
        if 'name' in body.keys() and event_producer.name != body['name']:
            #if the tenant already has a profile with this name then abort
            duplicate_producer = tenant_util.find_event_producer(
                tenant, producer_name=body['name'])
            if duplicate_producer:
                api.abort(
                    falcon.HTTP_400,
                    'EventProducer with name {0} already exists with id={1}.'.
                    format(duplicate_producer.name,
                           duplicate_producer.get_id()))
            event_producer.name = body['name']

        if 'pattern' in body:
            event_producer.pattern = str(body['pattern'])

        if 'durable' in body:
            event_producer.durable = body['durable']

        if 'encrypted' in body:
            event_producer.encrypted = body['encrypted']

        if 'sinks' in body:
            event_producer.sinks = body['sinks']

        #save the tenant document
        tenant_util.save_tenant(tenant)

        resp.status = falcon.HTTP_200
Ejemplo n.º 18
0
    def on_post(self, req, resp, tenant_id, validated_body):
        """
        Create a a new event Producer for a specified Tenant
        when an HTTP Post is received
        """
        body = validated_body['event_producer']

        tenant = tenant_util.find_tenant(tenant_id=tenant_id)

        if not tenant:
            _tenant_not_found()

        event_producer_name = body['name']
        event_producer_pattern = body['pattern']

        #if durable or encrypted aren't specified, set to False
        if 'durable' in body.keys():
            event_producer_durable = body['durable']
        else:
            event_producer_durable = False

        if 'encrypted' in body.keys():
            event_producer_encrypted = body['encrypted']
        else:
            event_producer_encrypted = False

        if 'sinks' in body.keys():
            event_producer_sinks = body['sinks']
        else:
            event_producer_sinks = None

        # Check if the tenant already has an event producer with this name
        producer = tenant_util.find_event_producer(
            tenant, producer_name=event_producer_name)
        if producer:
            api.abort(
                falcon.HTTP_400,
                'Event producer with name {0} already exists with id={1}.'.
                format(producer.name, producer.get_id()))

        # Create the new profile for the host
        producer_id = tenant_util.create_event_producer(
            tenant, event_producer_name, event_producer_pattern,
            event_producer_durable, event_producer_encrypted,
            event_producer_sinks)

        resp.status = falcon.HTTP_201
        resp.set_header('Location',
                        '/v1/{0}/producers/{1}'.format(tenant_id, producer_id))
Ejemplo n.º 19
0
    def on_head(self, req, resp, tenant_id):
        """
        Validates a token for a specified tenant
        when an HTTP HEAD call is received
        """

        #get message token, or abort if token is not in header
        message_token = req.get_header(MESSAGE_TOKEN, required=True)

        #verify the tenant exists
        tenant = tenant_util.find_tenant(tenant_id=tenant_id)

        if not tenant:
            _tenant_not_found()

        if not tenant.token.validate_token(message_token):
            _message_token_is_invalid()

        resp.status = falcon.HTTP_200
Ejemplo n.º 20
0
    def on_head(self, req, resp, tenant_id):
        """
        Validates a token for a specified tenant
        when an HTTP HEAD call is received
        """

        #get message token, or abort if token is not in header
        message_token = req.get_header(MESSAGE_TOKEN, required=True)

        #verify the tenant exists
        tenant = tenant_util.find_tenant(tenant_id=tenant_id)

        if not tenant:
            _tenant_not_found()

        if not tenant.token.validate_token(message_token):
            _message_token_is_invalid()

        resp.status = falcon.HTTP_200
Ejemplo n.º 21
0
    def on_delete(self, req, resp, tenant_id, event_producer_id):
        """
        Delete a specified Event Producer from a Tenant's configuration
        when an HTTP DELETE is received
        """
        #verify the tenant exists
        tenant = tenant_util.find_tenant(tenant_id=tenant_id)

        if not tenant:
            _tenant_not_found()

        #verify the event_producer exists and belongs to the tenant
        event_producer = tenant_util.find_event_producer(
            tenant, producer_id=event_producer_id)
        if not event_producer:
            _producer_not_found()

        tenant_util.delete_event_producer(tenant, event_producer)

        resp.status = falcon.HTTP_200
Ejemplo n.º 22
0
    def on_get(self, req, resp, tenant_id, event_producer_id):
        """
        Retrieve a specified Event Producer from a Tenant
        when an HTTP GET is received
        """
        #verify the tenant exists
        tenant = tenant_util.find_tenant(tenant_id=tenant_id)

        if not tenant:
            _tenant_not_found()

        #verify the event_producer exists and belongs to the tenant
        event_producer = tenant_util.find_event_producer(
            tenant, producer_id=event_producer_id)
        if not event_producer:
            _producer_not_found()

        resp.status = falcon.HTTP_200
        resp.body = api.format_response_body(
            {'event_producer': event_producer.format()})
Ejemplo n.º 23
0
    def on_get(self, req, resp, tenant_id, event_producer_id):
        """
        Retrieve a specified Event Producer from a Tenant
        when an HTTP GET is received
        """
        #verify the tenant exists
        tenant = tenant_util.find_tenant(tenant_id=tenant_id)

        if not tenant:
            _tenant_not_found()

        #verify the event_producer exists and belongs to the tenant
        event_producer = tenant_util.find_event_producer(
            tenant, producer_id=event_producer_id)
        if not event_producer:
            _producer_not_found()

        resp.status = falcon.HTTP_200
        resp.body = api.format_response_body(
            {'event_producer': event_producer.format()})
Ejemplo n.º 24
0
    def on_post(self, req, resp, validated_body):
        """
        Create a new tenant when a HTTP POST is received
        """

        body = validated_body['tenant']
        tenant_id = str(body['tenant_id'])

        tenant_name = body.get('tenant_name', tenant_id)

        #validate that tenant does not already exists
        tenant = tenant_util.find_tenant(tenant_id=tenant_id)
        if tenant:
            api.abort(falcon.HTTP_400, 'Tenant with tenant_id {0} '
                      'already exists'.format(tenant_id))

        tenant_util.create_tenant(tenant_id=tenant_id, tenant_name=tenant_name)

        resp.status = falcon.HTTP_201
        resp.set_header('Location', '/v1/{0}'.format(tenant_id))
Ejemplo n.º 25
0
    def on_delete(self, req, resp, tenant_id, event_producer_id):
        """
        Delete a specified Event Producer from a Tenant's configuration
        when an HTTP DELETE is received
        """
        #verify the tenant exists
        tenant = tenant_util.find_tenant(tenant_id=tenant_id)

        if not tenant:
            _tenant_not_found()

        #verify the event_producer exists and belongs to the tenant
        event_producer = tenant_util.find_event_producer(
            tenant, producer_id=event_producer_id)
        if not event_producer:
            _producer_not_found()

        tenant_util.delete_event_producer(tenant, event_producer)

        resp.status = falcon.HTTP_200
Ejemplo n.º 26
0
    def on_post(self, req, resp, validated_body):
        """
        Create a new tenant when a HTTP POST is received
        """

        body = validated_body['tenant']
        tenant_id = str(body['tenant_id'])

        tenant_name = body.get('tenant_name', tenant_id)

        #validate that tenant does not already exists
        tenant = tenant_util.find_tenant(tenant_id=tenant_id)
        if tenant:
            api.abort(
                falcon.HTTP_400, 'Tenant with tenant_id {0} '
                'already exists'.format(tenant_id))

        tenant_util.create_tenant(tenant_id=tenant_id, tenant_name=tenant_name)

        resp.status = falcon.HTTP_201
        resp.set_header('Location', '/v1/{0}'.format(tenant_id))
Ejemplo n.º 27
0
 def test_find_tenant_returns_instance(self):
     retrieve_tenant_call = MagicMock(return_value=self.tenant_obj)
     with patch('meniscus.data.model.tenant_util.retrieve_tenant',
                retrieve_tenant_call):
         tenant = tenant_util.find_tenant('12345')
         self.assertIsInstance(tenant, Tenant)
Ejemplo n.º 28
0
 def test_find_tenant_returns_none(self):
     retrieve_tenant_call = MagicMock(return_value=None)
     with patch('meniscus.data.model.tenant_util.retrieve_tenant',
                retrieve_tenant_call):
         tenant = tenant_util.find_tenant('12345')
         self.assertIsNone(tenant)
Ejemplo n.º 29
0
 def test_find_tenant_returns_instance(self):
     retrieve_tenant_call = MagicMock(return_value=self.tenant_obj)
     with patch('meniscus.data.model.tenant_util.retrieve_tenant',
                retrieve_tenant_call):
         tenant = tenant_util.find_tenant('12345')
         self.assertIsInstance(tenant, Tenant)
Ejemplo n.º 30
0
 def test_find_tenant_returns_none(self):
     retrieve_tenant_call = MagicMock(return_value=None)
     with patch('meniscus.data.model.tenant_util.retrieve_tenant',
                retrieve_tenant_call):
         tenant = tenant_util.find_tenant('12345')
         self.assertIsNone(tenant)