def test_transform_from_api(self): api = Api(endpoint="") api_dict = [{ "url": "http://127.0.0.1/api/v1/software/4/", "name": "Test Software", "vendor": "Paychex", "version": "70", "install_date": "2018-12-14T00:00:00Z", "removal_date": None, }] initial_transform = api.transform_from_api(api_dict) self.assertEqual(initial_transform[0]['package_name'], 'Test Software')
def test_transform_to_api(self): api_dict = [{ 'package_name': 'Test Software', 'software_vendor': 'Paychex', 'versions': [{ 'name': '70', 'install_date': '2018-12-14T00:00:00Z' }] }] api = Api( endpoint="http://127.0.0.1/api/v1/api/") transform = api.transform_to_api(api_dict) self.assertEqual(transform[0]['name'], 'Test Software')
def test_updating_software_with_no_version_or_vendor(self): add(GET, 'http://127.0.0.1/api/v1/virtualmachine?name=test', json={'count': 1, 'results': [{ "url": "https:///127.0.0.1/api/v1/virtualmachine/4/" }]}, status=200) add(GET, 'http://127.0.0.1/api/v1/software?vm=4', json={'count': 0, 'results': []}, status=200) add(GET, 'http://127.0.0.1/api/v1/software?name=Test%20Software&version=&vm=4&vendor=', json={'count': 0, 'results': []}, status=200) def request_callback(request): from json import dumps, loads return (200, {}, request.body) add_callback( POST, 'http://127.0.0.1/api/v1/software/', callback=request_callback, content_type='application/json', ) api = Api(endpoint="http://127.0.0.1/api/v1", host_dict={ "name": "test", "software": [ { "package_name": "Test Software", "versions": [ { "install_date": "2018-12-30T00:00:00Z", "name": None, } ] } ] }).upsert() self.assertFalse(api[0]['version'])
def test_transform_from_api_multiple_software(self): api = Api(endpoint="") api_dict = [{ "url": "http://127.0.0.1/api/v1/api/software/4/", "name": "Test Software", "vendor": "Paychex", "version": "70", "install_date": "2018-12-14T00:00:00Z", "removal_date": None, }, { "url": "http://127.0.0.1/api/v1/software/4/", "name": "Another Software", "vendor": "Paychex", "version": "71", "install_date": "2018-12-14T00:00:00Z", "removal_date": None, }] transform = api.transform_from_api(api_dict) self.assertEqual(len(transform), 2)
def test_updating_software(self): add(GET, 'http://127.0.0.1/api/v1/virtualmachine?name=test', json={'count': 1, 'results': [{ "url": "https:///127.0.0.1/api/v1/virtualmachine/4/" }]}, status=200) add(GET, 'http://127.0.0.1/api/v1/software?vm=4', json={'count': 1, 'results': [{ "url": "http://127.0.0.1/api/v1/software/4/", "name": "Test Software", "vendor": "Paychex", "version": "70", "install_date": "2018-12-14T00:00:00Z", "removal_date": None, }]}, status=200) add(GET, 'http://127.0.0.1/api/v1/software?name=Test%20Software&version=70&vm=4&vendor=Paychex', json={'count': 1, 'results': [{ "url": "http://127.0.0.1/api/v1/software/4/", "name": "Test Software", "vendor": "Paychex", "version": "70", "install_date": "2018-12-14T00:00:00Z", "removal_date": None, }]}, status=200) def request_callback(request): from json import dumps, loads return (200, {}, request.body) add_callback( PUT, 'http://127.0.0.1/api/v1/software/4/', callback=request_callback, content_type='application/json', ) api = Api(endpoint="http://127.0.0.1/api/v1", host_dict={ "name": "test", "software": [ { "package_name": "Test Software", "software_vendor": "Paychex", "versions": [ { "install_date": "2018-12-30T00:00:00Z", "name": "70", } ] } ] }).upsert() self.assertEqual(api[0]['install_date'], '2018-12-30T00:00:00+00:00')
def test_no_change(self): add(GET, 'http://127.0.0.1/api/v1/virtualmachine?name=test', json={'count': 1, 'results': [{ "url": "https:///127.0.0.1/api/v1/virtualmachine/4/" }]}, status=200) add(GET, 'http://127.0.0.1/api/v1/software?vm=4', json={'count': 1, 'results': [{ "url": "http://127.0.0.1/api/v1//software/4/", "name": "Test Software", "vendor": "Paychex", "version": "70", "install_date": "2018-12-14T00:00:00Z", "removal_date": None, }]}, status=200) add(GET, 'http://127.0.0.1/api/v1/software?name=Test%20Software&version=70&vm=4&vendor=Paychex', json={'count': 1, 'results': [{ "url": "http://127.0.0.1/api/v1//software/4/", "name": "Test Software", "vendor": "Paychex", "version": "70", "install_date": "2018-12-14T00:00:00Z", "removal_date": None, }]}, status=200) api = Api(endpoint="http://127.0.0.1/api/v1", host_dict={ "name": "test", "software": [ { "package_name": "Test Software", "software_vendor": "Paychex", "versions": [ { "install_date": "2018-12-14T00:00:00Z", "name": "70" } ] } ] }).upsert() self.assertEqual(len(api), 0)
def test_add_when_host_has_no_software(self): add(GET, 'http://127.0.0.1/api/v1/virtualmachine?name=test', json={'count': 1, 'results': [{ "url": "https:///127.0.0.1/api/v1/virtualmachine/4/" }]}, status=200) add(GET, 'http://127.0.0.1/api/v1/software?vm=4', json={'count': 1, 'results': [{ "url": "http://127.0.0.1/api/v1/software/4/", "name": "Test Software", "vendor": "Paychex", "version": "70", "install_date": "2018-12-14T00:00:00Z", "removal_date": None, }]}, status=200) add(GET, "http://127.0.0.1/api/v1/software?name=Test%20Software&version=70&vm=4&vendor=Paychex", json={'count': 1, 'results': [{ "url": "http://127.0.0.1/api/v1//software/4/", "name": "Test Software", "vendor": "Paychex", "version": "70", "install_date": "2018-12-14T00:00:00Z", "removal_date": None, }]}, status=200) add(GET, "http://127.0.0.1/api/v1/software?name=Another%20Software&version=70&vm=4&vendor=Paychex", json={'count': 0, 'results': []}, status=200) def request_callback(request): return (200, {}, request.body) add_callback( POST, 'http://127.0.0.1/api/v1/software/', callback=request_callback, content_type='application/json', ) api = Api(endpoint="http://127.0.0.1/api/v1", host_dict={ "name": "test", "software": [ { "package_name": "Test Software", "software_vendor": "Paychex", "versions": [ { "install_date": "2018-12-14T00:00:00Z", "name": "70", } ] }, { "package_name": "Another Software", "software_vendor": "Paychex", "versions": [ { "install_date": "2018-12-14T00:00:00Z", "name": "70", } ] } ] }).upsert() self.assertEqual(api[0]['name'], 'Another Software')
def main(results: any): """ The main function to take in all arguments, analyze, and post to mongo """ if results.debug: logging.basicConfig( stream=stdout, format=' %(levelname)s: %(asctime)s %(message)s', level=logging.NOTSET) else: logging.basicConfig( stream=stdout, format='%(levelname)s: %(asctime)s %(message)s', level=logging.INFO) if results.sleep: logging.debug("Taking a light nap as requested") time.sleep(randint(0, 60)) logging.debug("Starting Execution") start = time.time() try: with open(results.packages, 'r') as file_resource: packages = file_resource.read() try: host = Host(results.server) except BaseException: raise ValueError( "X-RAY: Packages were provided without hostname. Please use the -s flag and provide a hostname") except ValueError as error: raise error except BaseException: # Assume if they are running locally they dont provide packages host = Host(socket.getfqdn()) packages = get_local_packages() try: with open(results.mounts, 'r') as file_resource: mounts = file_resource.read() except BaseException: # Assume if they are running locally they dont provide mounts mounts = get_local_mounts() if results.runtime: host.last_update_duration = float(results.runtime) host.software = analyze_packages(packages) if mounts is not None: host.mounts = analyze_mounts(mounts) if not results.test: try: if environ.get('XRAY_CONNECTION_STRING'): mongo = Mongo(environ['CONNECTION_STRING']) mongo.post(host.__dict__, start) elif environ.get('XRAY_ENDPOINT'): api = Api(endpoint=environ['XRAY_ENDPOINT'], host_dict=host.__dict__, start = start) results = api.upsert() logging.info("Total changes upserted %s", len(results)) else: raise ValueError("Must specify XRAY_ENDPOINT or XRAY_CONNECTION_STRING in envioronment variable") exit(0) except ValueError as error: raise error except Exception as error: logging.error(f"X-RAY:Error with db: {error}") exit(1) else: logging.info(host.__dict__)