def test_get_tenant_from_coordinator_exception_for_no_tenant_found(self): tenant_identify = correlator.TenantIdentification( self.tenant_id, self.valid_message_token) response = MagicMock() response.status_code = httplib.NOT_FOUND http_request = MagicMock(return_value=response) with patch.object( correlator.ConfigCache, 'get_config', self.get_config),\ patch('meniscus.api.correlation.' 'correlator.http_request', http_request): with self.assertRaises(exception.ResourceNotFoundError): tenant_identify._get_tenant_from_coordinator()
def test_validate_token_with_coordinator_returns_true(self): tenant_identify = correlator.TenantIdentification( self.tenant_id, self.valid_message_token) response = MagicMock() response.status_code = httplib.OK http_request = MagicMock(return_value=response) with patch.object( correlator.ConfigCache, 'get_config', self.get_config), \ patch('meniscus.api.correlation.' 'correlator.http_request', http_request): result = tenant_identify._validate_token_with_coordinator() self.assertTrue(result)
def test_validate_token_with_coordinator_throws_auth_error(self): tenant_identify = correlator.TenantIdentification( self.tenant_id, self.invalid_message_token) response = MagicMock() response.status_code = httplib.NOT_FOUND http_request = MagicMock(return_value=response) with patch.object( correlator.ConfigCache, 'get_config', self.get_config), \ patch('meniscus.api.correlation.' 'correlator.http_request', http_request): with self.assertRaises(exception.MessageAuthenticationError): tenant_identify._validate_token_with_coordinator()
def test_get_coord_validated_tenant_from_coordinator_returns_tenant(self): tenant_identify = correlator.TenantIdentification( self.tenant_id, self.valid_message_token) with patch.object(correlator.TokenCache, 'get_token', self.get_none), \ patch.object(correlator.TenantIdentification, '_validate_token_with_coordinator', MagicMock()), \ patch.object( correlator.TenantIdentification, '_get_tenant_from_coordinator', self.get_tenant): tenant = tenant_identify.get_validated_tenant() self.assertIsInstance(tenant, Tenant)
def test_get_tenant_from_coordinator_returns_tenant(self): tenant_identify = correlator.TenantIdentification( self.tenant_id, self.valid_message_token) response = MagicMock() response.status_code = httplib.OK http_request = MagicMock(return_value=response) with patch.object( correlator.ConfigCache, 'get_config', self.get_config), \ patch('meniscus.api.correlation.' 'correlator.http_request', http_request), \ patch('meniscus.api.correlation.' 'correlator.load_tenant_from_dict', self.tenant_found): tenant = tenant_identify._get_tenant_from_coordinator() self.assertIsInstance(tenant, Tenant)
def on_post(self, req, resp, tenant_id, validated_body): """ This method is passed log event data by a tenant. The request will have a message token and a tenant id which must be validated either by the local cache or by a call to this workers coordinator. """ #read message token from header message_token = req.get_header(MESSAGE_TOKEN, required=True) #Validate the tenant's JSON event log data as valid JSON. message = validated_body['log_message'] tenant_identification = correlator.TenantIdentification( tenant_id, message_token) try: tenant = tenant_identification.get_validated_tenant() message = correlator.add_correlation_info_to_message( tenant, message) except errors.MessageAuthenticationError as ex: abort(falcon.HTTP_401, ex.message) except errors.ResourceNotFoundError as ex: abort(falcon.HTTP_404, ex.message) except errors.CoordinatorCommunicationError: abort(falcon.HTTP_500) dispatch.persist_message(message) #if message is durable, return durable job info if message['meniscus']['correlation']['durable']: durable_job_id = message['meniscus']['correlation']['job_id'] job_status_uri = "http://{0}/v1/job/{1}/status" \ .format("meniscus_uri", durable_job_id) resp.status = falcon.HTTP_202 resp.body = format_response_body({ "job_id": durable_job_id, "job_status_uri": job_status_uri }) else: resp.status = falcon.HTTP_204
def _correlate_syslog_message(syslog_message): #remove meniscus tenant id and message token # from the syslog structured data try: tenant_data = syslog_message['sd'].pop('meniscus') tenant_id = tenant_data['tenant'] message_token = tenant_data['token'] #if there is a key error then the syslog message did #not contain necessary credential information except KeyError: message = 'tenant_id or message token not provided' _LOG.debug('Message validation failed: {0}'.format(message)) raise errors.MessageValidationError(message) #validate the tenant and the message token tenant_identification = correlator.TenantIdentification( tenant_id, message_token) tenant = tenant_identification.get_validated_tenant() cee_message = _convert_message_cee(syslog_message) correlator.add_correlation_info_to_message(tenant, cee_message) return cee_message