def get_defendant_from_item(item):
"""
Get defendant/service for given item
"""
customer_id = None
item['rawItem'] = _get_deobfuscate_item(item['rawItem'])
try:
ip_addr, hostname, url = _get_item_ip_hostname_url(item)
if not ip_addr and not hostname:
raise BadRequest('Unable to get infos for this item')
except ValidationError:
raise BadRequest('Invalid item')
for param in {'urls': [url]}, {'ips': [ip_addr], 'fqdn': [hostname]}:
try:
services = ImplementationFactory.instance.get_singleton_of(
'CustomerDaoBase'
).get_services_from_items(**param)
schema.valid_adapter_response('CustomerDaoBase', 'get_services_from_items', services)
if services:
break
except (CustomerDaoException, schema.InvalidFormatError, schema.SchemaNotFound):
raise InternalServerError('Unknown exception while identifying defendant')
if services:
try:
customer_id = services[0]['defendant']['customerId']
service = services[0]['service']
except (IndexError, KeyError):
raise InternalServerError('Unable to parse CustomerDaoBase response')
if not customer_id:
raise NotFound('No defendant found for this item')
return {'customerId': customer_id, 'service': service}
评论列表
文章目录