def __init__(self, opts):
        Generator.__init__(self, opts)
        paths = opts.get(
            'path',
            ['/var/lib/GeoIP', '/usr/share/GeoIP', '/usr/local/share/GeoIP'])
        if not isinstance(paths, list):
            paths = [paths]
        filename = opts.get('filename', 'GeoLite2-Country.mmdb')
        db = opts.get('db', None)

        if db is None:
            for path in paths:
                db = '%s/%s' % (path, filename)
                if os.path.isfile(db) and os.access(db, os.R_OK):
                    break
                db = None
        if db is None:
            raise Exception(
                'Please specify valid Maxmind database with path=,filename= or db='
            )

        logging.info('Using %s' % db)
        self.reader = maxminddb.open_database(db)

        if opts.get('nonstrict', False):
            self.nonstrict = True
 def __init__(self, opts):
     Generator.__init__(self, opts)
     self.auth = {}
     csvs = opts.get('csv', None)
     urlv4 = opts.get(
         'urlv4',
         'https://www.iana.org/assignments/ipv4-address-space/ipv4-address-space.csv'
     )
     urlv6 = opts.get(
         'urlv6',
         'https://www.iana.org/assignments/ipv6-unicast-address-assignments/ipv6-unicast-address-assignments.csv'
     )
     if csvs:
         if not isinstance(csvs, list):
             csvs = [csvs]
         for file in csvs:
             with open(file, newline='') as csvfile:
                 self._read(csvfile)
     elif opts.get('fetch', 'no').lower() == 'yes':
         urls = opts.get('url', [urlv4, urlv6])
         if urls and not isinstance(urls, list):
             urls = [urls]
         logging.info('bootstrapping client subnet authority using URLs')
         for url in urls:
             logging.info('fetching %s' % url)
             self._read(
                 StringIO(urlopen(Request(url)).read().decode('utf-8')))
     else:
         raise Exception(
             'No authorities bootstrapped, please specify csv= or fetch=yes'
         )
Beispiel #3
0
def test_generator():
    o = Generator({})
    with pytest.raises(Exception):
        o.process([])

    class Generator1(Generator):
        def process(self, file):
            pass
    with pytest.raises(Exception):
        class Generator1(Generator):
            def process(self, file):
                pass
                try:
                    c = self.reader.get(subnet)
                except Exception as e:
                    if not self.nonstrict:
                        raise e
                    continue
                if c:
                    iso_code = c.get('country', {}).get('iso_code', '??')
                    if iso_code in cc:
                        cc[iso_code] += subnets[subnet]
                    else:
                        cc[iso_code] = subnets[subnet]

            if cc:
                ccd = Dataset()
                ccd.name = 'client_subnet_country'
                ccd.start_time = dataset.start_time
                ccd.stop_time = dataset.stop_time
                gen_datasets.append(ccd)

                ccd1 = Dimension('ClientCountry')
                ccd1.values = cc
                ccd.dimensions.append(ccd1)

        return gen_datasets


import sys
if sys.version_info[0] == 3 and sys.version_info[1] == 5:  # pragma: no cover
    Generator.__init_subclass__(client_subnet_country)
                if not idx.network_address in self.auth:
                    idx = '??'
                else:
                    for entry in self.auth[idx.network_address]:
                        if entry['net'].overlaps(ip):
                            idx = entry['auth']
                            break

                if idx in auth:
                    auth[idx] += subnets[subnet]
                else:
                    auth[idx] = subnets[subnet]

            if auth:
                authd = Dataset()
                authd.name = 'client_subnet_authority'
                authd.start_time = dataset.start_time
                authd.stop_time = dataset.stop_time
                gen_datasets.append(authd)

                authd1 = Dimension('ClientAuthority')
                authd1.values = auth
                authd.dimensions.append(authd1)

        return gen_datasets


import sys
if sys.version_info[0] == 3 and sys.version_info[1] == 5:  # pragma: no cover
    Generator.__init_subclass__(client_subnet_authority)