Example #1
0
 def setUp(self):
     self.test_token = Token('89c38542-0c78-41f1-bcd2-5226189ccab9',
                             '89c38542-0c78-41f1-bcd2-5226189ddab1',
                             '2013-04-01T21:58:16.995031Z')
     self.test_tenant_bare = Tenant('1022', self.test_token)
     self.test_tenant = Tenant('1022', self.test_token, [], 'MDBid',
                               'TenantName')
Example #2
0
class WhenTestingTenantObject(unittest.TestCase):
    def setUp(self):
        self.test_token = Token('89c38542-0c78-41f1-bcd2-5226189ccab9',
                                '89c38542-0c78-41f1-bcd2-5226189ddab1',
                                '2013-04-01T21:58:16.995031Z')
        self.test_tenant_bare = Tenant('1022', self.test_token, [], [], [])
        self.test_tenant = Tenant('1022', self.test_token, [], [], [], 'MDBid')

    def test_tenant_get_id(self):
        self.assertEqual(self.test_tenant.get_id(), 'MDBid')

    def test_tenant_format(self):
        tenant_dict = self.test_tenant_bare.format()
        self.assertEqual(tenant_dict['tenant_id'], '1022')
        self.assertEqual(tenant_dict['hosts'], [])
        self.assertEqual(tenant_dict['profiles'], [])
        self.assertEqual(tenant_dict['event_producers'], [])
        self.assertTrue('token' in tenant_dict)

    def test_tenant_format_for_save(self):
        tenant_dict = self.test_tenant.format_for_save()
        self.assertEqual(tenant_dict['tenant_id'], '1022')
        self.assertEqual(tenant_dict['hosts'], [])
        self.assertEqual(tenant_dict['profiles'], [])
        self.assertEqual(tenant_dict['event_producers'], [])
        self.assertEqual(tenant_dict['_id'], 'MDBid')
Example #3
0
def find_tenant(ds_handler, tenant_id, create_on_missing=False):
    """
    Retrieves a dictionary describing a tenant object and its Hosts, Profiles,
    and eventProducers and maps them to a tenant object
    """
    # get the tenant dictionary form the data source
    tenant_dict = ds_handler.find_one('tenant', {'tenant_id': tenant_id})

    if tenant_dict:
        tenant = load_tenant_from_dict(tenant_dict)
        return tenant

    if create_on_missing:
        #create new token for the tenant
        new_token = Token()
        new_tenant = Tenant(tenant_id, new_token)

        ds_handler.put('tenant', new_tenant.format())
        ds_handler.create_sequence(new_tenant.tenant_id)

        tenant_dict = ds_handler.find_one('tenant', {'tenant_id': tenant_id})

        tenant = load_tenant_from_dict(tenant_dict)
        return tenant

    return None
Example #4
0
def find_tenant(ds_handler, tenant_id, create_on_missing=False):
    """
    Retrieves a dictionary describing a tenant object and its Hosts, Profiles,
    and eventProducers and maps them to a tenant object
    """
    # get the tenant dictionary form the data source
    tenant_dict = ds_handler.find_one('tenant', {'tenant_id': tenant_id})

    if tenant_dict:
        tenant = load_tenant_from_dict(tenant_dict)
        return tenant

    if create_on_missing:
        #create new token for the tenant
        new_token = Token()
        new_tenant = Tenant(tenant_id, new_token)

        ds_handler.put('tenant', new_tenant.format())
        ds_handler.create_sequence(new_tenant.tenant_id)

        tenant_dict = ds_handler.find_one('tenant', {'tenant_id': tenant_id})

        tenant = load_tenant_from_dict(tenant_dict)
        return tenant

    return None
Example #5
0
 def setUp(self):
     self.test_token = Token('89c38542-0c78-41f1-bcd2-5226189ccab9',
                             '89c38542-0c78-41f1-bcd2-5226189ddab1',
                             '2013-04-01T21:58:16.995031Z')
     self.test_tenant_bare = Tenant('1022', self.test_token)
     self.test_tenant = Tenant('1022', self.test_token, [], 'MDBid',
                               'TenantName')
 def setUp(self):
     self.cache_clear = MagicMock()
     self.cache_true = MagicMock(return_value=True)
     self.cache_false = MagicMock(return_value=False)
     self.cache_update = MagicMock()
     self.cache_set = MagicMock()
     self.cache_del = MagicMock()
     self.tenant_id = '101'
     self.tenant = Tenant(tenant_id=self.tenant_id, token=Token())
     self.tenant_json = jsonutils.dumps(self.tenant.format())
     self.cache_get_tenant = MagicMock(return_value=self.tenant_json)
Example #7
0
    def setUp(self):

        self.timestamp = "2013-03-19T18:16:48.411029Z"
        self.producers = [
            EventProducer(432, 'producer1', 'syslog', durable=True),
            EventProducer(433, 'producer2', 'syslog', durable=False)
        ]
        self.token = Token('ffe7104e-8d93-47dc-a49a-8fb0d39e5192',
                           'bbd6302e-8d93-47dc-a49a-8fb0d39e5192',
                           "2013-03-19T18:16:48.411029Z")
        self.tenant_id = '1234'
        self.tenant = Tenant(self.tenant_id,
                             self.token,
                             event_producers=self.producers)
        self.tenant_found = MagicMock(return_value=self.tenant)

        self.cache = MagicMock()
        self.valid_message_token = 'ffe7104e-8d93-47dc-a49a-8fb0d39e5192'
        self.invalid_message_token = 'yyy7104e-8d93-47dc-a49a-8fb0d39e5192'
        self.get_token = MagicMock(return_value=self.token)
        self.get_tenant = MagicMock(return_value=self.tenant)
        self.get_none = MagicMock(return_value=None)
        self.config = WorkerConfiguration(
            personality='correlation',
            personality_module='meniscus.personas.worker.correlation.app',
            worker_id='fgc7104e-8d93-47dc-a49a-8fb0d39e5192',
            worker_token='bbd6307f-8d93-47dc-a49a-8fb0d39e5192',
            coordinator_uri='http://192.168.1.2/v1')
        self.get_config = MagicMock(return_value=self.config)
Example #8
0
def create_tenant(tenant_id, tenant_name=None):
    """
    Creates a new tenant and and persists to the datastore
    """
    #create new token for the tenant
    new_token = Token()
    new_tenant = Tenant(tenant_id, new_token, tenant_name=tenant_name)

    #save the new tenant to the datastore
    _db_handler.put('tenant', new_tenant.format())
    #create a new sequence for the tenant for creation of IDs on child objects
    _db_handler.create_sequence(new_tenant.tenant_id)

    #create an index for the tenant in the default sink
    # and enables time to live for the default doc_type
    ttl_tasks.create_index.delay(tenant_id)
Example #9
0
def create_tenant(tenant_id, tenant_name=None):
    """
    Creates a new tenant and and persists to the datastore
    """
    #create new token for the tenant
    new_token = Token()
    new_tenant = Tenant(tenant_id, new_token, tenant_name=tenant_name)

    #save the new tenant to the datastore
    _db_handler.put('tenant', new_tenant.format())
    #create a new sequence for the tenant for creation of IDs on child objects
    _db_handler.create_sequence(new_tenant.tenant_id)

    #create an index for the tenant in the default sink
    # and enables time to live for the default doc_type
    mapping_tasks.create_index.delay(tenant_id)
Example #10
0
    def before(self):
        self.db_handler = MagicMock()
        self.req = MagicMock()
        self.req.content_type = 'application/json'

        self.resp = MagicMock()
        self.producer_id = 432
        self.producer_name = 'producer1'
        self.producer_id_2 = 432
        self.producer_name_2 = 'producer2'
        self.not_valid_producer_id = 777
        self.producers = [
            EventProducer(self.producer_id, self.producer_name, 'syslog'),
            EventProducer(self.producer_id_2, self.producer_name_2, 'syslog')
        ]
        self.token_original = 'ffe7104e-8d93-47dc-a49a-8fb0d39e5192'
        self.token_previous = 'bbd6302e-8d93-47dc-a49a-8fb0d39e5192'
        self.token_invalid = 'xxxyyy33-8d93-47dc-a49a-8fb0d39e5192'
        self.timestamp_original = "2013-03-19T18:16:48.411029Z"
        self.token = Token(self.token_original, self.token_previous,
                           self.timestamp_original)
        self.tenant_id = '1234'
        self.tenant_name = 'TenantName'
        self.tenant = Tenant(self.tenant_id,
                             self.token,
                             event_producers=self.producers)
        self.tenant_not_found = MagicMock(return_value=None)
        self.tenant_found = MagicMock(return_value=self.tenant)

        self._set_resource()
Example #11
0
    def on_post(self, req, resp, validated_body):

        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 = find_tenant(self.db, tenant_id=tenant_id)
        if tenant:
            abort(falcon.HTTP_400, 'Tenant with tenant_id {0} '
                  'already exists'.format(tenant_id))

        #create new token for the tenant
        new_token = Token()
        new_tenant = Tenant(tenant_id, new_token, tenant_name=tenant_name)

        self.db.put('tenant', new_tenant.format())
        self.db.create_sequence(new_tenant.tenant_id)
        resp.status = falcon.HTTP_201
        resp.set_header('Location', '/v1/{0}'.format(tenant_id))
Example #12
0
    def on_post(self, req, resp, validated_body):

        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 = find_tenant(self.db, tenant_id=tenant_id)
        if tenant:
            abort(
                falcon.HTTP_400, 'Tenant with tenant_id {0} '
                'already exists'.format(tenant_id))

        #create new token for the tenant
        new_token = Token()
        new_tenant = Tenant(tenant_id, new_token, tenant_name=tenant_name)

        self.db.put('tenant', new_tenant.format())
        self.db.create_sequence(new_tenant.tenant_id)
        resp.status = falcon.HTTP_201
        resp.set_header('Location', '/v1/{0}'.format(tenant_id))
 def setUp(self):
     self.cache_clear = MagicMock()
     self.cache_true = MagicMock(return_value=True)
     self.cache_false = MagicMock(return_value=False)
     self.cache_update = MagicMock()
     self.cache_set = MagicMock()
     self.cache_del = MagicMock()
     self.tenant_id = '101'
     self.tenant = Tenant(
         tenant_id=self.tenant_id,
         token=Token()
     )
     self.tenant_json = jsonutils.dumps(self.tenant.format())
     self.cache_get_tenant = MagicMock(return_value=self.tenant_json)
Example #14
0
class WhenTestingTenantObject(unittest.TestCase):
    def setUp(self):
        self.test_token = Token('89c38542-0c78-41f1-bcd2-5226189ccab9',
                                '89c38542-0c78-41f1-bcd2-5226189ddab1',
                                '2013-04-01T21:58:16.995031Z')
        self.test_tenant_bare = Tenant('1022', self.test_token)
        self.test_tenant = Tenant('1022', self.test_token, [], 'MDBid',
                                  'TenantName')

    def test_tenant_get_id(self):
        self.assertEqual(self.test_tenant.get_id(), 'MDBid')

    def test_tenant_format(self):
        tenant_dict = self.test_tenant_bare.format()
        self.assertEqual(tenant_dict['tenant_id'], '1022')
        self.assertEqual(tenant_dict['event_producers'], [])
        self.assertTrue('token' in tenant_dict)

    def test_tenant_format_for_save(self):
        tenant_dict = self.test_tenant.format_for_save()
        self.assertEqual(tenant_dict['tenant_id'], '1022')
        self.assertEqual(tenant_dict['tenant_name'], 'TenantName')
        self.assertEqual(tenant_dict['event_producers'], [])
        self.assertEqual(tenant_dict['_id'], 'MDBid')
Example #15
0
    def setUp(self):

        self.producers = [
            EventProducer(432,
                          'producer1',
                          'syslog',
                          durable=True,
                          sinks=VALID_SINKS),
            EventProducer(433, 'producer2', 'syslog', durable=False)
        ]
        self.token = Token('ffe7104e-8d93-47dc-a49a-8fb0d39e5192',
                           'bbd6302e-8d93-47dc-a49a-8fb0d39e5192',
                           "2013-03-19T18:16:48.411029Z")
        self.tenant_id = '1234'
        self.tenant_name = 'TenantName'
        self.tenant = Tenant(self.tenant_id,
                             self.token,
                             event_producers=self.producers,
                             tenant_name=self.tenant_name)
        self.destination = {'transaction_id': None, 'transaction_time': None}
Example #16
0
def load_tenant_from_dict(tenant_dict):
    #Create a list of EventProducer objects from the dictionary
    event_producers = [
        EventProducer(e['id'], e['name'], e['pattern'], e['durable'],
                      e['encrypted'], e['sinks'])
        for e in tenant_dict['event_producers']
    ]

    token = load_token_from_dict(tenant_dict['token'])

    _id = None
    if "_id" in tenant_dict.keys():
        _id = tenant_dict['_id']

    #Create the parent tenant object
    tenant = Tenant(tenant_dict['tenant_id'],
                    token,
                    event_producers=event_producers,
                    _id=_id,
                    tenant_name=tenant_dict['tenant_name'])

    #Return tenant object
    return tenant
Example #17
0
 def setUp(self):
     self.tenant_id = '5164b8f4-16fb-4376-9d29-8a6cbaa02fa9'
     self.message_token = 'ffe7104e-8d93-47dc-a49a-8fb0d39e5192'
     self.producers = [
         EventProducer(432, 'producer1', 'syslog', durable=True),
         EventProducer(433, 'producer2', 'syslog', durable=False)
     ]
     self.invalid_message_token = 'yyy7104e-8d93-47dc-a49a-8fb0d39e5192'
     self.token = Token('ffe7104e-8d93-47dc-a49a-8fb0d39e5192',
                        'bbd6302e-8d93-47dc-a49a-8fb0d39e5192',
                        '2013-03-19T18:16:48.411029Z')
     self.tenant = Tenant(self.tenant_id,
                          self.token,
                          event_producers=self.producers)
     self.get_token = MagicMock(return_value=self.token)
     self.get_tenant = MagicMock(return_value=self.tenant)
     self.get_none = MagicMock(return_value=None)
     self.src_msg = {
         'HOST':
         'tohru',
         '_SDATA': {
             'meniscus': {
                 'token': self.message_token,
                 'tenant': self.tenant_id
             }
         },
         'PRIORITY':
         'info',
         'MESSAGE':
         '127.0.0.1 - - [12/Jul/2013:19:40:58 +0000] '
         '\'GET /test.html HTTP/1.1\' 404 466 \'-\' '
         '\'curl/7.29.0\'',
         'FACILITY':
         'local1',
         'MSGID':
         '345',
         'ISODATE':
         '2013-07-12T14:17:00+00:00',
         'PROGRAM':
         'apache',
         'DATE':
         '2013-07-12T14:17:00.134+00:00',
         'PID':
         '234'
     }
     self.malformed_sys_msg = {
         'HOST':
         'tohru',
         '_SDATA': {
             'meniscus': {
                 'token': '',
                 'tenant': ''
             }
         },
         'PRIORITY':
         'info',
         'MESSAGE':
         '127.0.0.1 - - [12/Jul/2013:19:40:58 +0000] '
         '\'GET /test.html HTTP/1.1\' 404 466 \'-\' '
         '\'curl/7.29.0\'',
         'FACILITY':
         'local1',
         'MSGID':
         '345',
         'ISODATE':
         '2013-07-12T14:17:00+00:00',
         'PROGRAM':
         'apache',
         'DATE':
         '2013-07-12T14:17:00.134+00:00',
         'PID':
         '234'
     }
     self.cee_msg = {
         'host':
         'tohru',
         'pri':
         'info',
         'msg':
         '127.0.0.1 - - [12/Jul/2013:19:40:58 +0000] '
         '\'GET /test.html HTTP/1.1\' 404 466 \'-\' '
         '\'curl/7.29.0\'',
         'msgid':
         '345',
         'time':
         '2013-07-12T14:17:00+00:00',
         'pname':
         'apache',
         'pid':
         '234',
         'ver':
         '1',
         'native': {
             'meniscus': {
                 'token': 'ffe7104e-8d93-47dc-a49a-8fb0d39e5192',
                 'tenant': '5164b8f4-16fb-4376-9d29-8a6cbaa02fa9'
             }
         }
     }
     self.config = WorkerConfiguration(
         personality='worker',
         hostname='worker01',
         coordinator_uri='http://192.168.1.2/v1')
     self.get_config = MagicMock(return_value=self.config)
     self.tenant_found = MagicMock(return_value=self.tenant)
class WhenTestingTenantCache(unittest.TestCase):
    def setUp(self):
        self.cache_clear = MagicMock()
        self.cache_true = MagicMock(return_value=True)
        self.cache_false = MagicMock(return_value=False)
        self.cache_update = MagicMock()
        self.cache_set = MagicMock()
        self.cache_del = MagicMock()
        self.tenant_id = '101'
        self.tenant = Tenant(
            tenant_id=self.tenant_id,
            token=Token()
        )
        self.tenant_json = jsonutils.dumps(self.tenant.format())
        self.cache_get_tenant = MagicMock(return_value=self.tenant_json)

    def test_clear_calls_cache_clear(self):
        with patch.object(NativeProxy, 'cache_clear', self.cache_clear):
            tenant_cache = TenantCache()
            tenant_cache.clear()
        self.cache_clear.assert_called_once_with(CACHE_TENANT)

    def test_set_tenant_calls_cache_update(self):
        with patch.object(
                NativeProxy, 'cache_exists', self.cache_true
        ), patch.object(NativeProxy, 'cache_update', self.cache_update):
            tenant_cache = TenantCache()
            tenant_cache.set_tenant(self.tenant)

        self.cache_update.assert_called_once_with(
            self.tenant_id, jsonutils.dumps(self.tenant.format()),
            DEFAULT_EXPIRES, CACHE_TENANT)

    def test_set_tenant_calls_cache_set(self):
        with patch.object(
                NativeProxy, 'cache_exists', self.cache_false
        ), patch.object(NativeProxy, 'cache_set', self.cache_set):
            tenant_cache = TenantCache()
            tenant_cache.set_tenant(self.tenant)

        self.cache_set.assert_called_once_with(
            self.tenant_id, jsonutils.dumps(self.tenant.format()),
            DEFAULT_EXPIRES, CACHE_TENANT)

    def test_get_tenant_calls_returns_tenant(self):
        with patch.object(
                NativeProxy, 'cache_exists', self.cache_true
        ), patch.object(NativeProxy, 'cache_get',  self.cache_get_tenant):
            tenant_cache = TenantCache()
            tenant = tenant_cache.get_tenant(self.tenant_id)

        self.cache_get_tenant.assert_called_once_with(
            self.tenant_id, CACHE_TENANT)
        self.assertIsInstance(tenant, Tenant)

    def test_get_tenant_calls_returns_none(self):
        with patch.object(
                NativeProxy, 'cache_exists', self.cache_false):
            tenant_cache = TenantCache()
            tenant = tenant_cache.get_tenant(self.tenant_id)

        self.assertIs(tenant, None)

    def test_delete_tenant_calls_cache_del(self):
        with patch.object(
                NativeProxy, 'cache_exists', self.cache_true
        ), patch.object(NativeProxy, 'cache_del', self.cache_del):
            tenant_cache = TenantCache()
            tenant_cache.delete_tenant(self.tenant_id)

        self.cache_del.assert_called_once_with(
            self.tenant_id, CACHE_TENANT)

    def test_delete_tenant_does_not_call_cache_del(self):
        with patch.object(
                NativeProxy, 'cache_exists', self.cache_false
        ), patch.object(NativeProxy, 'cache_del', self.cache_del):
            tenant_cache = TenantCache()
            tenant_cache.delete_tenant(self.tenant_id)

        with self.assertRaises(AssertionError):
            self.cache_del.assert_called_once_with(
                self.tenant_id, CACHE_TENANT)
class WhenTestingTenantCache(unittest.TestCase):
    def setUp(self):
        self.cache_clear = MagicMock()
        self.cache_true = MagicMock(return_value=True)
        self.cache_false = MagicMock(return_value=False)
        self.cache_update = MagicMock()
        self.cache_set = MagicMock()
        self.cache_del = MagicMock()
        self.tenant_id = '101'
        self.tenant = Tenant(tenant_id=self.tenant_id, token=Token())
        self.tenant_json = jsonutils.dumps(self.tenant.format())
        self.cache_get_tenant = MagicMock(return_value=self.tenant_json)

    def test_clear_calls_cache_clear(self):
        with patch.object(NativeProxy, 'cache_clear', self.cache_clear):
            tenant_cache = TenantCache()
            tenant_cache.clear()
        self.cache_clear.assert_called_once_with(CACHE_TENANT)

    def test_set_tenant_calls_cache_update(self):
        with patch.object(NativeProxy, 'cache_exists',
                          self.cache_true), patch.object(
                              NativeProxy, 'cache_update', self.cache_update):
            tenant_cache = TenantCache()
            tenant_cache.set_tenant(self.tenant)

        self.cache_update.assert_called_once_with(
            self.tenant_id, jsonutils.dumps(self.tenant.format()),
            DEFAULT_EXPIRES, CACHE_TENANT)

    def test_set_tenant_calls_cache_set(self):
        with patch.object(NativeProxy, 'cache_exists',
                          self.cache_false), patch.object(
                              NativeProxy, 'cache_set', self.cache_set):
            tenant_cache = TenantCache()
            tenant_cache.set_tenant(self.tenant)

        self.cache_set.assert_called_once_with(
            self.tenant_id, jsonutils.dumps(self.tenant.format()),
            DEFAULT_EXPIRES, CACHE_TENANT)

    def test_get_tenant_calls_returns_tenant(self):
        with patch.object(NativeProxy, 'cache_exists',
                          self.cache_true), patch.object(
                              NativeProxy, 'cache_get', self.cache_get_tenant):
            tenant_cache = TenantCache()
            tenant = tenant_cache.get_tenant(self.tenant_id)

        self.cache_get_tenant.assert_called_once_with(self.tenant_id,
                                                      CACHE_TENANT)
        self.assertIsInstance(tenant, Tenant)

    def test_get_tenant_calls_returns_none(self):
        with patch.object(NativeProxy, 'cache_exists', self.cache_false):
            tenant_cache = TenantCache()
            tenant = tenant_cache.get_tenant(self.tenant_id)

        self.assertIs(tenant, None)

    def test_delete_tenant_calls_cache_del(self):
        with patch.object(NativeProxy, 'cache_exists',
                          self.cache_true), patch.object(
                              NativeProxy, 'cache_del', self.cache_del):
            tenant_cache = TenantCache()
            tenant_cache.delete_tenant(self.tenant_id)

        self.cache_del.assert_called_once_with(self.tenant_id, CACHE_TENANT)

    def test_delete_tenant_does_not_call_cache_del(self):
        with patch.object(NativeProxy, 'cache_exists',
                          self.cache_false), patch.object(
                              NativeProxy, 'cache_del', self.cache_del):
            tenant_cache = TenantCache()
            tenant_cache.delete_tenant(self.tenant_id)

        with self.assertRaises(AssertionError):
            self.cache_del.assert_called_once_with(self.tenant_id,
                                                   CACHE_TENANT)