def test_file_not_exists(self): """Test parsing non-existant config file raises and InvalidConfigError. """ cp = ConfigParser() with self.assertRaises(InvalidConfigError) as ctx: cp.parse_file('this_file_doesnt_exist') self.assertEqual('Unable to read config file: this_file_doesnt_exist', str(ctx.exception))
def test_no_vpns(self): cp = ConfigParser() cp.parse_file('data/configs/no_vpns.conf') self.assertEqual(cp.settings['name'], 'No VPNs') self.assertIsNone(cp.settings['geoip_data']) self.assertEqual(cp.settings['datetime_format'], '%d/%m/%Y %H:%M:%S') self.assertEqual(len(cp.vpns), 0)
def __init__(self): super(MongoDbDriver, self).__init__() self.__config = ConfigParser() self.client = pymongo.MongoClient('mongodb://' + self.__config.get_mongodb_host() + ':' + self.__config.get_mongodb_port() + '/') self.db = self.client.vuln_database
def test_load_defaults(self): """Test loaded defaults are as expected. """ cp = ConfigParser() cp.load_defaults() self.assertEqual(cp.settings, { 'name': None, 'geoip_data': None, 'datetime_format': '%d/%m/%Y %H:%M:%S', }) self.assertEqual(1, len(cp.vpns)) vpn = cp.vpns[0] self.assertEqual(vpn.name, 'Default VPN') self.assertEqual(vpn.mgmt_address, 'localhost:5555') self.assertEqual(vpn.allow_disconnect, True)
def test_many_vpns(self): cp = ConfigParser.from_file('data/configs/many_vpns.conf') self.assertEqual(cp.settings['name'], 'Many VPNs') self.assertEqual(cp.settings['geoip_data'], '/asd/asd/a_data_file.whatever') self.assertEqual(cp.settings['datetime_format'], '%Y/%d/%m %S:%H:%M') self.assertEqual(len(cp.vpns), 4) self.assertEqual( sorted(['A VPN', 'Another VPN', 'All the VPNs', 'So many !"£$%^&*()\'']), sorted([v.name for v in cp.vpns])) # A VPN vpn = [v for v in cp.vpns if v.name == 'A VPN'][0] self.assertEqual(vpn.mgmt_address, '/asd/asd/asd') self.assertEqual(vpn.allow_disconnect, False) # Another VPN vpn = [v for v in cp.vpns if v.name == 'Another VPN'][0] self.assertEqual(vpn.mgmt_address, '1.2.3.4:5678') self.assertEqual(vpn.allow_disconnect, True) # All the VPNs vpn = [v for v in cp.vpns if v.name == 'All the VPNs'][0] self.assertEqual(vpn.mgmt_address, '/asd/asd.sock') self.assertEqual(vpn.allow_disconnect, True) # So many !"£$%^&*()' vpn = [v for v in cp.vpns if v.name == 'So many !"£$%^&*()\''][0] self.assertEqual(vpn.mgmt_address, 'localhost:1234') self.assertEqual(vpn.allow_disconnect, False)
class MongoDbDriver: # -- Public methods # MongoDbDriver Constructor def __init__(self): super(MongoDbDriver, self).__init__() self.__config = ConfigParser() self.client = pymongo.MongoClient('mongodb://' + self.__config.get_mongodb_host() + ':' + self.__config.get_mongodb_port() + '/') self.db = self.client.vuln_database # -- Inserting and bulk inserting methods # Bulk insert the cve list with the next format: <CVE-ID>#<product>#<version> def bulk_insert_cves(self, cve_list): products = [] for product in cve_list: splitted_product = product.split("#") data = {} data['cve_id'] = splitted_product[0] data['product'] = splitted_product[1] data['version'] = splitted_product[2] products.append(data) # Bulk insert self.db.cve.create_index([('product', pymongo.DESCENDING)]) self.db.cve.insert_many(products) # Bulk insert the bid list with the next format: <BID-ID>#<product>#<version> def bulk_insert_bids(self, bid_list): products = [] for product in bid_list: splitted_product = product.split("#") data = {} data['bugtraq_id'] = splitted_product[0] data['product'] = splitted_product[1] data['version'] = splitted_product[2] products.append(data) # Bulk insert self.db.bid.create_index([('product', 'text')], default_language='english') self.db.bid.insert_many(products) # Bulk insert the exploit_db list with the next format: <EXPLOIT_DB-ID>#<product>#<version> def bulk_insert_exploit_db_ids(self, exploit_db_list): products = [] for product in exploit_db_list: splitted_product = product.split("#") data = {} data['exploit_db_id'] = splitted_product[0] data['product'] = splitted_product[1] data['version'] = splitted_product[2] products.append(data) # Bulk insert self.db.exploit_db.create_index([('product', 'text')], default_language='english') self.db.exploit_db.insert_many(products) # Inserts the docker image scan result to history def insert_docker_image_scan_result_to_history(self, scan_result): if self.db.image_history.count() == 0: self.db.image_history.create_index([('image_name', pymongo.DESCENDING)]) self.db.image_history.insert(scan_result) # -- Removing methods # Removes cve collection def delete_cve_collection(self): self.db.cve.remove() # Removes bid collection def delete_bid_collection(self): self.db.bid.remove() # Removes exploit_db collection def delete_exploit_db_collection(self): self.db.exploit_db.remove() # -- Querying methods # Checks if the product has CVEs or BIDs def has_vulnerabilities(self, product, version=None): if not version: return (self.db.cve.count({'product': product}) + self.db.bid.count({'$text': { '$search': product }}) + self.db.exploit_db.count({'$text': { '$search': product }})) > 0 else: return (self.db.cve.count({ 'product': product, 'version': version }) + self.db.bid.count({ '$text': { '$search': product }, 'version': version }) + self.db.exploit_db.count({ '$text': { '$search': product }, 'version': version })) > 0 # Gets the product vulnerabilities def get_vulnerabilities(self, product, version=None): if not version: cve_cursor = self.db.cve.find({'product': product}, { 'product': 0, 'version': 0, '_id': 0 }) bid_cursor = self.db.bid.find({'$text': { '$search': product }}, { 'product': 0, 'version': 0, '_id': 0 }) exploit_db_cursor = self.db.exploit_db.find( {'$text': { '$search': product }}, { 'product': 0, 'version': 0, '_id': 0 }) else: cve_cursor = self.db.cve.find( { 'product': product, 'version': version }, { 'product': 0, 'version': 0, '_id': 0 }) bid_cursor = self.db.bid.find( { '$text': { '$search': product }, 'version': version }, { 'product': 0, 'version': 0, '_id': 0 }) exploit_db_cursor = self.db.exploit_db.find( { '$text': { '$search': product }, 'version': version }, { 'product': 0, 'version': 0, '_id': 0 }) # Prepare output output = [] for cve in cve_cursor: if cve is not None: output.append(cve['cve_id']) for bid in bid_cursor: if bid is not None: output.append('BID-' + bid['bugtraq_id']) for exploit_db in exploit_db_cursor: if exploit_db is not None: output.append('EXPLOIT_DB_ID-' + exploit_db['exploit_db_id']) # Return return output # Gets products from CVE def get_products_from_CVE(self, cve): cursor = self.db.cve.find({'cve_id': cve}, {'cve_id': 0, '_id': 0}) # Prepare output output = [] for product in cursor: if product is not None: output.append(product) # Return return output # Gets products from BID def get_products_from_BID(self, bid): cursor = self.db.bid.find({'bugtraq_id': str(bid)}, { 'bugtraq_id': 0, '_id': 0 }) # Prepare output output = [] for product in cursor: if product is not None: output.append(product) # Return return output # Gets products from Exploit_db id def get_products_from_exploit_db_id(self, exploit_db_id): cursor = self.db.exploit_db.find({'exploit_db_id': str(exploit_db_id)}, { 'exploit_db_id': 0, '_id': 0 }) # Prepare output output = [] for product in cursor: if product is not None: output.append(product) # Return return output # Gets docker image history def get_docker_image_history(self, image_name): cursor = self.db.image_history.find({ 'image_name': image_name }, { '_id': 0 }).sort("timestamp", pymongo.DESCENDING) # Prepare output output = [] for scan in cursor: if scan is not None: scan['timestamp'] = str( datetime.datetime.utcfromtimestamp(scan['timestamp'])) output.append(scan) # Return return output
def test_empty_host_port(self): cp = ConfigParser() with self.assertRaises(InvalidConfigError) as ctx: cp.parse_file('data/configs/empty_host_port.conf') self.assertEqual('Must specify either socket or host and port', str(ctx.exception))
def test_lowercase_monitor(self): cp = ConfigParser() with self.assertRaises(InvalidConfigError) as ctx: cp.parse_file('data/configs/lowercase_monitor.conf') self.assertEqual("'Monitor' section required, but not found in config", str(ctx.exception))
def test_parse_etc_no_file(self): """Test parsing a file from /etc returns False when no expect files are present. """ cp = ConfigParser() self.assertFalse(cp.parse_etc_file())
import sys from util.config_parser import ConfigParser from util.input_parser import InputParser from auction_factory import AuctionFactory from bidder_factory import BidderFactory from site_factory import SiteFactory from managers.auction_manager import AuctionManager if __name__ == '__main__': config_path, input_path = sys.argv[1:3] config_data = ConfigParser.parse(config_path) input_data = InputParser.parse(input_path) bidder_object_dict = \ BidderFactory.create_bidder_dict(config_data.bidders) site_object_dict = SiteFactory.create_site_dict(config_data.sites, bidder_object_dict) auction_manager = AuctionManager( AuctionFactory.create_auctions(input_data.auctions, site_object_dict, bidder_object_dict), site_object_dict) auction_results = auction_manager.generate_auction_results() pass
class MongoDbDriver: # -- Public methods # MongoDbDriver Constructor def __init__(self): super(MongoDbDriver, self).__init__() self.__config = ConfigParser() self.client = pymongo.MongoClient('mongodb://' + self.__config.get_mongodb_host() + ':' + self.__config.get_mongodb_port() + '/') self.db = self.client.cve_database # Bulk insert the cve list with the next format: <CVE-ID>#<product>#<version> def bulk_insert(self, cve_list): products = [] for product in cve_list: splitted_product = product.split("#") data = {} data['cve_id'] = splitted_product[0] data['product'] = splitted_product[1] data['version'] = splitted_product[2] products.append(data) # Bulk insert self.db.cve.create_index([('product', pymongo.DESCENDING)]) self.db.cve.insert_many(products) # Remove cve collection def delete_cve_collection(self): self.db.cve.remove() # Checks if the product has CVEs def has_cves(self, product, version=None): if not version: return self.db.cve.count({'product': product}) > 0 else: return self.db.cve.count({ 'product': product, 'version': version }) > 0 # Gets the product CVEs def get_cves(self, product, version=None): if not version: cursor = self.db.cve.find({'product': product}, { 'product': 0, 'version': 0, '_id': 0 }) else: cursor = self.db.cve.find({ 'product': product, 'version': version }, { 'product': 0, 'version': 0, '_id': 0 }) # Prepare output output = [] for cve in cursor: if cve is not None: output.append(cve['cve_id']) # Return return output # Gets products from CVE def get_products(self, cve): cursor = self.db.cve.find({'cve_id': cve}, {'cve_id': 0, '_id': 0}) # Prepare output output = [] for product in cursor: if product is not None: output.append(product) # Return return output # Insert the docker image scan result to history def insert_docker_image_scan_result_to_history(self, scan_result): if self.db.image_history.count() == 0: self.db.image_history.create_index([('image_name', pymongo.DESCENDING)]) self.db.image_history.insert(scan_result) # Gets docker image history def get_docker_image_history(self, image_name): cursor = self.db.image_history.find({ 'image_name': image_name }, { '_id': 0 }).sort("timestamp", pymongo.DESCENDING) # Prepare output output = [] for scan in cursor: if scan is not None: scan['timestamp'] = str( datetime.datetime.utcfromtimestamp(scan['timestamp'])) output.append(scan) # Return return output
def test_config_parser_parse(self): """Test config parser -> parse method""" result = ConfigParser.parse(self.test_config_path) expected = ConfigData(*self.operator.mock_config_parser_parse()) self.assertEqual(result, expected, 'Invalid Config Data')