def post(self, app_id): username = self.get_current_user().encode('utf-8') customer_name = (get_user_property(username, UserKeys.CurrentCustomer)) uri = self.request.uri method = self.request.method try: severity = self.arguments.get('severity').capitalize() if severity in CommonSeverityKeys.ValidRvSeverities: sev_data = {AppsKey.RvSeverity: severity} update_app_data_by_app_id(app_id, sev_data, AppCollections.vFenseApps) results = GenericResults(username, uri, method).object_updated( app_id, 'app severity', [sev_data]) self.set_status(results['http_status']) self.set_header('Content-Type', 'application/json') self.write(json.dumps(results, indent=4)) else: results = (PackageResults(username, uri, method).invalid_severity(severity)) self.set_status(results['http_status']) self.set_header('Content-Type', 'application/json') self.write(json.dumps(results, indent=4)) except Exception as e: results = (GenericResults(username, uri, method).something_broke( app_id, 'update_severity', e)) logger.exception(e) self.set_status(results['http_status']) self.set_header('Content-Type', 'application/json') self.write(json.dumps(results, indent=4))
def post(self, app_id): username = self.get_current_user().encode('utf-8') customer_name = ( get_user_property(username, UserKeys.CurrentCustomer) ) uri = self.request.uri method = self.request.method try: severity = self.arguments.get('severity').capitalize() if severity in CommonSeverityKeys.ValidRvSeverities: sev_data = ( { AppsKey.RvSeverity: severity } ) update_app_data_by_app_id( app_id, sev_data ) results = ( GenericResults( username, uri, method ).object_updated(app_id, 'app severity', [sev_data]) ) self.set_status(results['http_status']) self.set_header('Content-Type', 'application/json') self.write(json.dumps(results, indent=4)) else: results = ( PackageResults( username, uri, method ).invalid_severity(severity) ) self.set_status(results['http_status']) self.set_header('Content-Type', 'application/json') self.write(json.dumps(results, indent=4)) except Exception as e: results = ( GenericResults( username, uri, method ).something_broke(app_id, 'update_severity', e) ) logger.exception(e) self.set_status(results['http_status']) self.set_header('Content-Type', 'application/json') self.write(json.dumps(results, indent=4))
def application_updater(customer_name, app_data, os_string, collection=AppCollections.UniqueApplications): """Insert or update an existing application in the provided collection. Args: customer_name (str): The name of the customer, this application is a part of. app_data (dict): Dictionary of the application data. os_string (str): The name of the operating system... Ubuntu 12.04 Kwargs: collection (str): The collection where app_data should be inserted or updated Basic Usage: >>> from vFense.plugins.patching.patching import application_updater >>> customer_name = 'default' >>> app_data = { "kb": "", "vendor_name": "", "description": "Facebook plugin for Gwibber\n Gwibber is a social networking client for GNOME. It supports Facebook,\n Twitter, Identi.ca, StatusNet, FriendFeed, Qaiku, Flickr, and Digg.\n .", "release_date": 1394769600, "vendor_severity": "recommended", "app_id": "922bcb88f6bd75c1e40fcc0c571f603cd59cf7e05b4a192bd5d69c974acc1457", "reboot_required": "no", "os_code": "linux", "repo": "precise-updates/main", "support_url": "", "version": "3.4.2-0ubuntu2.4", "rv_severity": "Recommended", "uninstallable": "yes", "name": "gwibber-service-facebook" } >>> os_string = 'Ubuntu 12.04 ' >>> application_updater(customer_name, app_data, os_string[ ,'unique_applications']) Returns: Tuple (inserted_count, updated_count) """ updated_count = 0 inserted_count = 0 status = app_data.pop(DbCommonAppPerAgentKeys.Status, None) agent_id = app_data.pop(DbCommonAppPerAgentKeys.AgentId, None) app_data.pop(DbCommonAppPerAgentKeys.InstallDate, None) file_data = app_data.pop(DbCommonAppKeys.FileData) app_name = app_data.get(DbCommonAppKeys.Name, None) app_version = app_data.get(DbCommonAppKeys.Version, None) app_kb = app_data.get(DbCommonAppKeys.Kb, '') app_id = app_data.get(DbCommonAppKeys.AppId) exists = object_exist(app_id, collection) if exists: add_file_data(app_id, file_data, agent_id) update_customers_in_app_by_app_id(customer_name, app_id) vuln_data = get_vulnerability_info_for_app( os_string, app_name, app_version, app_kb ) data_updated = update_app_data_by_app_id( app_id, vuln_data, collection ) if data_updated[0] == DbCodes.Replaced: updated_count = data_updated[1] else: add_file_data(app_id, file_data, agent_id) app_data[AppsKey.Customers] = [customer_name] app_data[AppsKey.Hidden] = CommonKeys.NO if (len(file_data) > 0 and status == CommonAppKeys.AVAILABLE or len(file_data) > 0 and status == CommonAppKeys.INSTALLED): app_data[AppsKey.FilesDownloadStatus] = ( PackageCodes.FilePendingDownload ) elif len(file_data) == 0 and status == CommonAppKeys.AVAILABLE: app_data[AppsKey.FilesDownloadStatus] = PackageCodes.MissingUri elif len(file_data) == 0 and status == CommonAppKeys.INSTALLED: app_data[AppsKey.FilesDownloadStatus] = PackageCodes.FileNotRequired vuln_data = get_vulnerability_info_for_app( os_string, app_name, app_version, app_kb ) app_data = dict(app_data.items() + vuln_data.items()) data_inserted = insert_data_in_table(app_data, collection) if data_inserted[0] == DbCodes.Inserted: inserted_count = data_inserted[1] return(inserted_count, updated_count)
def download_all_files_in_app(app_id, os_code, os_string=None, file_data=None, throttle=0, collection=AppCollections.UniqueApplications): create_necessary_dirs() throttle *= 1024 if not file_data and check_if_redhat(os_string): download_status = { AppsKey.FilesDownloadStatus: \ PackageCodes.AgentWillDownloadFromVendor } update_app_data_by_app_id(app_id, download_status, collection) elif len(file_data) > 0: app_path = os.path.join(VFENSE_APP_PATH, str(app_id)) if not os.path.exists(app_path): os.mkdir(app_path) num_of_files_to_download = len(file_data) num_of_files_downloaded = 0 num_of_files_mismatch = 0 num_of_files_failed = 0 num_of_files_invalid_uri = 0 new_status = { AppsKey.FilesDownloadStatus: PackageCodes.FileIsDownloading } for file_info in file_data: uri = str(file_info[CommonFileKeys.PKG_URI]) lhash = str(file_info[CommonFileKeys.PKG_HASH]) fname = str(file_info[CommonFileKeys.PKG_NAME]) fsize = file_info[CommonFileKeys.PKG_SIZE] if os_code == 'linux': file_path = os.path.join(VFENSE_APP_DEP_PATH, fname) else: file_path = os.path.join(app_path, fname) symlink_path = os.path.join(app_path, fname) cmd = 'ln -s %s %s' % (file_path, symlink_path) try: if uri and not os.path.exists(file_path): download_file(uri, file_path, throttle) if os.path.exists(file_path): if lhash: hash_match = hash_verify( orig_hash=lhash, file_path=file_path ) if hash_match: num_of_files_downloaded += 1 if os_code == 'linux': if not os.path.islink(file_path): os.system(cmd) else: num_of_files_mismatch += 1 elif fsize and not lhash: if os.path.getsize(file_path) == fsize: num_of_files_downloaded += 1 else: num_of_files_mismatch += 1 else: num_of_files_failed += 1 elif os.path.exists(file_path) and os_code == 'linux': if not os.path.islink(symlink_path): os.system(cmd) num_of_files_downloaded += 1 elif os.path.exists(file_path) and os_code != 'linux': num_of_files_downloaded += 1 elif uri: num_of_files_invalid_uri += 1 except Exception as e: logger.exception(e) if num_of_files_downloaded == num_of_files_to_download: new_status[AppsKey.FilesDownloadStatus] = ( PackageCodes.FileCompletedDownload ) elif num_of_files_mismatch > 0: new_status[AppsKey.FilesDownloadStatus] = ( PackageCodes.FileSizeMisMatch ) elif num_of_files_failed > 0: new_status[AppsKey.FilesDownloadStatus] = ( PackageCodes.FileFailedDownload ) elif num_of_files_invalid_uri > 0: new_status[AppsKey.FilesDownloadStatus] = ( PackageCodes.InvalidUri ) db_update_response = update_app_data_by_app_id( app_id, new_status, collection ) logger.info( '%s, %s, %s, %s' % (collection, app_id, str(new_status), db_update_response) )