def add_supported_app_to_agents(username, customer_name, uri, method, agent_id=None): if agent_id: agent_info = get_agent_info(agent_id) apps_info = ( fetch_apps_data_by_os_code( agent_info[AgentKey.OsCode], customer_name, collection=AppCollections.SupportedApps, ) ) if len(apps_info) > 0: for app_info in apps_info: app_id = app_info.get(SupportedAppsKey.AppId) file_data = fetch_file_data(app_id) add_file_data( app_id, file_data, agent_id ) agent_info_to_insert = ( { SupportedAppsPerAgentKey.AgentId: agent_id, SupportedAppsPerAgentKey.AppId: app_id, SupportedAppsPerAgentKey.Status: AVAILABLE, SupportedAppsPerAgentKey.CustomerName: customer_name, SupportedAppsPerAgentKey.InstallDate: r.epoch_time(0.0) } ) insert_app_data( agent_info_to_insert, collection=AppCollections.SupportedAppsPerAgent )
def add_supported_app_to_agents(username, customer_name, uri, method, agent_id=None): if agent_id: agent_info = get_agent_info(agent_id) apps_info = (fetch_apps_data_by_os_code( agent_info[AgentKey.OsCode], customer_name, collection=AppCollections.SupportedApps, )) if len(apps_info) > 0: for app_info in apps_info: app_id = app_info.get(SupportedAppsKey.AppId) file_data = fetch_file_data(app_id) add_file_data(app_id, file_data, agent_id) agent_info_to_insert = ({ SupportedAppsPerAgentKey.AgentId: agent_id, SupportedAppsPerAgentKey.AppId: app_id, SupportedAppsPerAgentKey.Status: AVAILABLE, SupportedAppsPerAgentKey.CustomerName: customer_name, SupportedAppsPerAgentKey.InstallDate: r.epoch_time(0.0) }) insert_app_data( agent_info_to_insert, collection=AppCollections.SupportedAppsPerAgent)
def update_supported_apps(json_data): try: rv_q = Queue("downloader", connection=RQ_PKG_POOL) conn = db_connect() inserted_count = 0 all_customers = list(r.table(Collection.Customers).map(lambda x: x[CustomerKey.CustomerName]).run(conn)) for i in range(len(json_data)): json_data[i][SupportedAppsKey.Customers] = all_customers json_data[i][SupportedAppsKey.ReleaseDate] = r.epoch_time(json_data[i][SupportedAppsKey.ReleaseDate]) json_data[i][SupportedAppsKey.FilesDownloadStatus] = PackageCodes.FilePendingDownload json_data[i][SupportedAppsKey.Hidden] = "no" json_data[i][SupportedAppsKey.VulnerabilityId] = "" insert_app_data(json_data[i], DownloadCollections.LatestDownloadedSupported) file_data = json_data[i].get(SupportedAppsKey.FileData) add_file_data(json_data[i][SupportedAppsKey.AppId], file_data) data_to_update = {SupportedAppsKey.Customers: all_customers} exists = r.table(AppCollections.SupportedApps).get(json_data[i][SupportedAppsKey.AppId]).run(conn) if exists: updated = ( r.table(AppCollections.SupportedApps) .get(json_data[i][SupportedAppsKey.AppId]) .update(data_to_update) .run(conn) ) else: updated = r.table(AppCollections.SupportedApps).insert(json_data[i]).run(conn) rv_q.enqueue_call( func=download_all_files_in_app, args=( json_data[i][SupportedAppsKey.AppId], json_data[i][SupportedAppsKey.OsCode], None, file_data, 0, AppCollections.SupportedApps, ), timeout=86400, ) inserted_count += updated["inserted"] conn.close() update_apps = IncomingSupportedApps() update_apps.sync_supported_updates_to_all_agents(json_data) except Exception as e: logger.exception(e)
def add_custom_app_to_agents(username, customer_name, uri, method, file_data, agent_id=None, app_id=None): if app_id and not agent_id: app_info = (fetch_app_data(app_id, collection=AppCollections.CustomApps)) agent_ids = get_all_agent_ids(customer_name, agent_os=app_info[AgentKey.OsCode]) if len(agent_ids) > 0: for agentid in agent_ids: add_file_data(app_id, file_data, agent_id) agent_info_to_insert = ({ CustomAppsPerAgentKey.AgentId: agentid, CustomAppsPerAgentKey.AppId: app_id, CustomAppsPerAgentKey.Status: CommonAppKeys.AVAILABLE, CustomAppsPerAgentKey.CustomerName: customer_name, CustomAppsPerAgentKey.InstallDate: r.epoch_time(0.0) }) insert_app_data(agent_info_to_insert, collection=AppCollections.CustomAppsPerAgent) if agent_id and not app_id: agent_info = get_agent_info(agent_id) apps_info = fetch_apps_data_by_os_code( agent_info[AgentKey.OsCode], customer_name, collection=AppCollections.CustomApps) for app_info in apps_info: app_id = app_info.get(CustomAppsKey.AppId) file_data = fetch_file_data(app_id) add_file_data(app_id, file_data, agent_id) agent_info_to_insert = { CustomAppsPerAgentKey.AgentId: agent_id, CustomAppsPerAgentKey.AppId: app_id, CustomAppsPerAgentKey.Status: CommonAppKeys.AVAILABLE, CustomAppsPerAgentKey.CustomerName: customer_name, CustomAppsPerAgentKey.InstallDate: r.epoch_time(0.0) } insert_app_data(agent_info_to_insert, collection=AppCollections.CustomAppsPerAgent)
def update_supported_apps(json_data): try: rv_q = Queue('downloader', connection=RQ_PKG_POOL) conn = db_connect() inserted_count = 0 all_customers = list( r.table(Collection.Customers).map( lambda x: x[CustomerKey.CustomerName]).run(conn)) for i in range(len(json_data)): json_data[i][SupportedAppsKey.Customers] = all_customers json_data[i][SupportedAppsKey.ReleaseDate] = \ r.epoch_time(json_data[i][SupportedAppsKey.ReleaseDate]) json_data[i][SupportedAppsKey.FilesDownloadStatus] = \ PackageCodes.FilePendingDownload json_data[i][SupportedAppsKey.Hidden] = 'no' json_data[i][SupportedAppsKey.VulnerabilityId] = '' insert_app_data(json_data[i], DownloadCollections.LatestDownloadedSupported) file_data = json_data[i].get(SupportedAppsKey.FileData) add_file_data(json_data[i][SupportedAppsKey.AppId], file_data) data_to_update = {SupportedAppsKey.Customers: all_customers} exists = (r.table(AppCollections.SupportedApps).get( json_data[i][SupportedAppsKey.AppId]).run(conn)) if exists: updated = (r.table(AppCollections.SupportedApps).get( json_data[i][SupportedAppsKey.AppId]).update( data_to_update).run(conn)) else: updated = (r.table(AppCollections.SupportedApps).insert( json_data[i]).run(conn)) rv_q.enqueue_call(func=download_all_files_in_app, args=(json_data[i][SupportedAppsKey.AppId], json_data[i][SupportedAppsKey.OsCode], None, file_data, 0, AppCollections.SupportedApps), timeout=86400) inserted_count += updated['inserted'] conn.close() update_apps = IncomingSupportedApps() update_apps.sync_supported_updates_to_all_agents(json_data) except Exception as e: logger.exception(e)
def add_custom_app_to_agents(username, customer_name, uri, method, file_data, agent_id=None, app_id=None): if app_id and not agent_id: app_info = ( fetch_app_data( app_id, collection=AppCollections.CustomApps ) ) agent_ids = get_all_agent_ids(customer_name, agent_os=app_info[AgentKey.OsCode]) if len(agent_ids) > 0: for agentid in agent_ids: add_file_data(app_id, file_data, agent_id) agent_info_to_insert = ( { CustomAppsPerAgentKey.AgentId: agentid, CustomAppsPerAgentKey.AppId: app_id, CustomAppsPerAgentKey.Status: CommonAppKeys.AVAILABLE, CustomAppsPerAgentKey.CustomerName: customer_name, CustomAppsPerAgentKey.InstallDate: r.epoch_time(0.0) } ) insert_app_data( agent_info_to_insert, collection=AppCollections.CustomAppsPerAgent ) if agent_id and not app_id: agent_info = get_agent_info(agent_id) apps_info = fetch_apps_data_by_os_code( agent_info[AgentKey.OsCode], customer_name, collection=AppCollections.CustomApps ) for app_info in apps_info: app_id = app_info.get(CustomAppsKey.AppId) file_data = fetch_file_data(app_id) add_file_data(app_id, file_data, agent_id) agent_info_to_insert = { CustomAppsPerAgentKey.AgentId: agent_id, CustomAppsPerAgentKey.AppId: app_id, CustomAppsPerAgentKey.Status: CommonAppKeys.AVAILABLE, CustomAppsPerAgentKey.CustomerName: customer_name, CustomAppsPerAgentKey.InstallDate: r.epoch_time(0.0) } insert_app_data( agent_info_to_insert, collection=AppCollections.CustomAppsPerAgent )
def update_agents_with_supported(self, apps, agents=None): if agents is None: agents = [] try: conn = db_connect() for agent in agents: self._delete_old_supported_apps_from_agent( agent[SupportedAppsPerAgentKey.AgentId], conn) for app in apps: if not agents: agents = self._get_list_of_agents_by_os_code( app[AgentKey.OsCode]) app_id = app.get(SupportedAppsKey.AppId) file_data = app.get(SupportedAppsKey.FileData) for agent in agents: if agent[AgentKey.OsCode] == app[SupportedAppsKey.OsCode]: agent_id = agent[SupportedAppsPerAgentKey.AgentId] add_file_data(app_id, file_data, agent_id) app_per_agent_props = \ self._set_app_per_agent_properties(agent, app_id) agent_has_app = self.check_if_agent_has_app( agent_id, app_id) if not agent_has_app: self.insert_app(app_per_agent_props) elif agent_has_app: app_per_agent_props[ SupportedAppsPerAgentKey. Status] = CommonAppKeys.INSTALLED self.insert_app(app_per_agent_props) conn.close() except Exception as e: logger.exception(e)
def update_agents_with_supported(self, apps, agents=None): if agents is None: agents = [] try: conn = db_connect() for agent in agents: self._delete_old_supported_apps_from_agent(agent[SupportedAppsPerAgentKey.AgentId], conn) for app in apps: if not agents: agents = self._get_list_of_agents_by_os_code(app[AgentKey.OsCode]) app_id = app.get(SupportedAppsKey.AppId) file_data = app.get(SupportedAppsKey.FileData) for agent in agents: if agent[AgentKey.OsCode] == app[SupportedAppsKey.OsCode]: agent_id = agent[SupportedAppsPerAgentKey.AgentId] add_file_data(app_id, file_data, agent_id) app_per_agent_props = self._set_app_per_agent_properties(agent, app_id) agent_has_app = self.check_if_agent_has_app(agent_id, app_id) if not agent_has_app: self.insert_app(app_per_agent_props) elif agent_has_app: app_per_agent_props[SupportedAppsPerAgentKey.Status] = CommonAppKeys.INSTALLED self.insert_app(app_per_agent_props) conn.close() except Exception as e: logger.exception(e)
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)