Permalink
Browse files

add asn lookup

  • Loading branch information...
iHile committed Jul 5, 2017
1 parent 08babfd commit 1f8a5d2dee93aa09b46baebe6060855a71ed82ad
Showing with 55 additions and 41 deletions.
  1. +2 −2 ansible/roles/common/vars/main.yml
  2. +1 −1 requirements.txt
  3. +47 −1 shared/IsolateCore.py
  4. +5 −33 shared/auth-manager.py
  5. +0 −4 shared/helper.py
@@ -56,5 +56,5 @@ common_utils_yum:
- pwgen
- zsh
- python-GeoIP
# - GeoIP-devel
# - GeoIP
- GeoIP-devel
- GeoIP
View
@@ -1,4 +1,4 @@
redis>=2.10.5
pymongo>=3.4.0
pyzabbix>=0.7.4
GeoIP>=1.2.8
View
@@ -1,9 +1,55 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import socket
import GeoIP
# Common snippets and funcs for use in other scripts (tiny lib)
__version__ = '0.100.502'
__version__ = '0.2.0'
def is_valid_ipv4_address(address):
try:
socket.inet_pton(socket.AF_INET, address)
except AttributeError:
try:
socket.inet_aton(address)
except socket.error:
return False
return True
except socket.error:
return False
return True
def is_valid_ipv6_address(address):
try:
socket.inet_pton(socket.AF_INET6, address)
except socket.error:
return False
return True
def is_valid_fqdn(hostname):
hostname = str(hostname).lower()
if len(hostname) > 255:
return False
if hostname[-1] == '.' or hostname[0] == '.':
return False
if re.match('^([a-z\d\-.]*)$', hostname) is None:
return False
return True
class IsolateGeoIP(object):
def __init__(self, **kwargs):
self.kwargs = kwargs
self.ASN_DB = os.getenv('ISOLATE_GEOIP_ASN', '/opt/auth/shared/geoip/GeoIPASNum.dat')
self.asn = GeoIP.open(self.ASN_DB, GeoIP.GEOIP_STANDARD)
class AuthStorage(object):
View
@@ -8,43 +8,12 @@
import argparse
from redis import Redis
import logging
import socket
from IsolateCore import IsolateGeoIP, is_valid_ipv6_address, is_valid_ipv4_address, is_valid_fqdn
LOGGER = logging.getLogger('auth-manager')
LOG_FORMAT = '[%(levelname)6s] %(name)s %(message)s'
def is_valid_ipv4_address(address):
try:
socket.inet_pton(socket.AF_INET, address)
except AttributeError:
try:
socket.inet_aton(address)
except socket.error:
return False
return True
except socket.error:
return False
return True
def is_valid_ipv6_address(address):
try:
socket.inet_pton(socket.AF_INET6, address)
except socket.error:
return False
return True
def is_valid_fqdn(hostname):
hostname = str(hostname).lower()
if len(hostname) > 255:
return False
if hostname[-1] == '.' or hostname[0] == '.':
return False
if re.match('^([a-z\d\-.]*)$', hostname) is None:
return False
return True
class AuthManager(object):
@@ -59,6 +28,7 @@ def __init__(self, params):
password=os.getenv('ISOLATE_REDIS_PASS', None),
db=int(os.getenv('ISOLATE_REDIS_DB', 0)))
self.validate_params()
self.geoip = IsolateGeoIP()
def process_args(self):
if self.action == 'add-host':
@@ -145,6 +115,8 @@ def add_host(self):
self.redis.set('offset_server_id', self.OFFSET_SERVER_ID)
self.params['server_id'] = self.redis.incr('offset_server_id')
self.params['geoip_asn'] = self.geoip.asn.name_by_addr(self.params['server_ip'])
redis_key = 'server_' + str(self.params['server_id'])
self.redis.set(redis_key, json.dumps(self.params))
LOGGER.info('Database updated: {0}'.format(self.params['server_id']))
View
@@ -14,7 +14,6 @@
from redis import Redis
from operator import itemgetter
from pyzabbix import ZabbixAPI
import GeoIP
# from IsolateCore import __version__
@@ -323,10 +322,7 @@ def start(self):
class AuthHelper(object):
ASN_DB = '/opt/auth/shared/geoip/GeoIPASNum.dat'
def __init__(self, args, unknown_args):
self.geoip = GeoIP.open(self.ASN_DB, GeoIP.GEOIP_STANDARD)
self.uuid = str(uuid4())
self.time_start = time()
self.args = args

0 comments on commit 1f8a5d2

Please sign in to comment.