Add resolved IP information to track_and_log
This commit is contained in:
parent
010dda2c52
commit
52927de7f6
2 changed files with 29 additions and 8 deletions
|
@ -1,5 +1,6 @@
|
|||
import logging
|
||||
import json
|
||||
import requests
|
||||
|
||||
from cachetools import ttl_cache, lru_cache
|
||||
from collections import namedtuple, defaultdict
|
||||
|
@ -12,6 +13,21 @@ ResolvedLocation = namedtuple('ResolvedLocation', ['provider', 'region', 'servic
|
|||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_DATA_FILES = {'aws-ip-ranges.json': 'https://ip-ranges.amazonaws.com/ip-ranges.json'}
|
||||
|
||||
def update_resolver_datafiles():
|
||||
""" Performs an update of the data file(s) used by the IP Resolver. """
|
||||
for filename, url in _DATA_FILES.iteritems():
|
||||
logger.debug('Updating IP resolver data file "%s" from URL "%s"', filename, url)
|
||||
with open('util/ipresolver/%s' % filename, 'w') as f:
|
||||
response = requests.get(url)
|
||||
logger.debug('Got %s response for URL %s', response.status_code, url)
|
||||
if response.status_code / 2 != 100:
|
||||
raise Exception('Got non-2XX status code for URL %s: %s' % (url, response.status_code))
|
||||
|
||||
f.write(response.text)
|
||||
logger.debug('Successfully wrote %s', filename)
|
||||
|
||||
class IPResolver(object):
|
||||
def __init__(self, app, *args, **kwargs):
|
||||
self.app = app
|
||||
|
@ -30,20 +46,20 @@ class IPResolver(object):
|
|||
@ttl_cache(maxsize=1, ttl=600)
|
||||
def _get_location_function(self):
|
||||
try:
|
||||
with open('util/ipresolver/ip-ranges.json', 'r') as f:
|
||||
ip_range_json = json.loads(f.read())
|
||||
with open('util/ipresolver/aws-ip-ranges.json', 'r') as f:
|
||||
aws_ip_range_json = json.loads(f.read())
|
||||
except IOError:
|
||||
logger.exception('Could not load IP Ranges')
|
||||
logger.exception('Could not load AWS IP Ranges')
|
||||
return None
|
||||
except ValueError:
|
||||
logger.exception('Could not load IP Ranges')
|
||||
logger.exception('Could not load AWS IP Ranges')
|
||||
return None
|
||||
except TypeError:
|
||||
logger.exception('Could not load IP Ranges')
|
||||
logger.exception('Could not load AWS IP Ranges')
|
||||
return None
|
||||
|
||||
sync_token = ip_range_json['syncToken']
|
||||
all_amazon, regions, services = IPResolver._parse_amazon_ranges(ip_range_json)
|
||||
sync_token = aws_ip_range_json['syncToken']
|
||||
all_amazon, regions, services = IPResolver._parse_amazon_ranges(aws_ip_range_json)
|
||||
return IPResolver._build_location_function(sync_token, all_amazon, regions, services, self.geoip_db)
|
||||
|
||||
@staticmethod
|
||||
|
|
Reference in a new issue