def _mark_as_user_data(self, path_spec): """ path_spec can be a file or a directory. We want to match a regex like: path_spec + r'|' + pathspec + r'/.*' """ # db query to find matches and populate list of files to mark system: System = State.get_system() session: Session = State.get_db_session() base: List[FileDetail] = session.query(FileDetail).filter( (FileDetail.system_id == system.system_id) & (FileDetail.file_location.like(path_spec))).all() dirs: List[FileDetail] = session.query(FileDetail).filter( (FileDetail.system_id == system.system_id) & (FileDetail.file_location.like(path_spec + '/%'))).all() files = base + dirs for f in files: # update file_origin for each file. # ...update file_origin for each file. update_origin = update(FileDetail).values( origin=FileOrigin.UserData.name, ).where( FileDetail.file_detail_id == f.file_detail_id) result = session.execute(update_origin) session.flush() session.commit() self._log('Marked as UserData ' + f.file_location)
def system_OS(distro: OSDistro) -> bool: system: System = State.get_system() if system.os_distro == 'centos': return OSDistro.CentOS else: return OSDistro.Other
def _mark_as_content(self, path_spec): """ Link file to package and mark origin as package content. path_spec can be a file or a directory. We want to match a regex like: path_spec + r'|' + pathspec + r'/.*' """ # db query to find matches and populate list of files to mark system: System = State.get_system() session: Session = State.get_db_session() base: List[FileDetail] = session.query(FileDetail).filter( (FileDetail.system_id == system.system_id) & (FileDetail.file_location.like(path_spec))).all() dirs: List[FileDetail] = session.query(FileDetail).filter( (FileDetail.system_id == system.system_id) & (FileDetail.file_location.like(path_spec + '/%'))).all() files = base + dirs for f in files: # ...update file_origin for each file. update_origin = update(FileDetail).values( origin=FileOrigin.PackageContent.name, rpm_info_id=self._rpm_info.rpm_info_id).where( FileDetail.file_detail_id == f.file_detail_id) result = session.execute(update_origin) session.flush() session.commit() self._log('Linked to package ' + self._package_name + ': ' + f.file_location) self._log('Marked as PackageContent ' + f.file_location)
def __init__(self, **kwargs): name = kwargs.get("name") gather = kwargs.get("gather", False) self.system: System = State.get_system(name=name, gather=gather) if not gather: self.system_id: int = self.system.system_id
def unknown_count() -> int: """ Count the number of files where FileOrigin is UnknownSource for the current system. """ system: System = State.get_system() session: Session = State.get_db_session() return session.query(FileDetail).filter( (FileDetail.system_id == system.system_id) & (FileDetail.origin == FileOrigin.UnknownSource.name)).count()
def run_rules(self, p): system_name = State.get_system().name self._log('Start firing rules for system ' + system_name + ' Pass ' + p.name) self._log('Unmarked files: %d' % hutils.unknown_count()) for r in self._rules[p]: r.fire() self._log('Finished firing rules for system ' + system_name + ' Pass ' + p.name) self._log('Unmarked files: %d' % hutils.unknown_count())
def path_exists(path_spec: str) -> bool: """ Return boolean if the path exists in the source file system. """ system: System = State.get_system() session: Session = State.get_db_session() file_detail: FileDetail = session.query(FileDetail).filter( (FileDetail.system_id == system.system_id) & (FileDetail.file_location == path_spec)).one_or_none() return file_detail is not None
def origin_unknown(path_spec: str) -> bool: """ Return True if FileOrigin is UnknownSource, else False. """ system: System = State.get_system() session: Session = State.get_db_session() file_detail: FileDetail = session.query(FileDetail).filter( (FileDetail.system_id == system.system_id) & (FileDetail.file_location == path_spec) & (FileDetail.origin == FileOrigin.UnknownSource.name)).one_or_none() return file_detail is not None
def fetch_file_detail(rpm_detail: RpmDetail) -> FileDetail: db_session = State.get_db_session() system_id = State.get_system().system_id query = db_session.query(FileDetail).join(RpmFileDetailLink).join( RpmDetail).filter((RpmDetail.system_id == system_id) & ( RpmDetail.rpm_detail_id == rpm_detail.rpm_detail_id)) file_details = query.all() if len(file_details) > 0: return file_details[0] else: raise ValueError("FileDetail cannot be loaded.")
def package_installed(package_name: str) -> List[RpmInfo]: """ Check if package is installed on the source system. Mangle package name as needed for rpm or scl naming conventions. Check the rpm_details table for a match. Return None for no match, or an rpm_details object if found. """ system: System = State.get_system() session: Session = State.get_db_session() rpm_info: List[RpmInfo] = session.query(RpmInfo).filter( (RpmInfo.system_id == system.system_id) & (RpmInfo.name == package_name)).all() return rpm_info
def get_directory_contents(path_spec: str) -> List[FileDetail]: system: System = State.get_system() session: Session = State.get_db_session() lookup = path_spec if not lookup.endswith(os.path.sep): lookup += os.path.sep lookup += "%" file_details: List[FileDetail] = session.query(FileDetail).filter( (FileDetail.system_id == system.system_id) & (FileDetail.file_location.like(lookup)) & (func.strpos( func.substr(FileDetail.file_location, len(lookup)), os.path.sep, ) == 0)).all() return file_details
def path_modified(path_spec: str) -> bool: """ Check if a source system file was modified after package installation. path_spec is a full pathname on the source file system. Look it up in file_details table. If origin is PKG_MODIFIED, return true. """ system: System = State.get_system() session: Session = State.get_db_session() rpm_detail: RpmDetail = session.query(RpmDetail).filter( (RpmDetail.system_id == system.system_id) & (RpmDetail.file_location == path_spec)).one_or_none() if not rpm_detail: raise RpmFileNotFound("Unable to locate file in RpmDetails") return rpm_detail.file_changed
def main(): sys = utils.os.GetArguments().parse().name print('Path to analyze:') path = input() print('OK. Examining ' + path + ' on ' + sys) system: System = State.get_system(name=sys) session: Session = State.get_db_session() lookup = path if lookup.endswith(os.path.sep): lookup += "%" file_details: List[FileDetail] = session.query( FileDetail ).filter( (FileDetail.system_id == system.system_id) & (FileDetail.file_location.like(lookup)) ).all() for f in file_details: print(f.file_location + ' is ' + f.origin)
fetch_files.process_modified_packages() # All of the pattern matching, etc. has been pushed down into the rules run_rules.main() fetch_files.process_user_content() if __name__ == '__main__': try: arguments = utils.os.GetMinimumSshArguments().parse() LogConfig.initialize(path="logs/run_analysis.log", level=arguments.log_level) system: System = State.get_system(name=arguments.name) State.startup( action=main, action_kwargs={"args": arguments}, ssh_kwargs={ "hostname": system.hostname, "port": system.port, "username": system.username, "key_file": system.key_file, }, ) except ApiExceptionBase as e: log.fatal(f"Error accessing API: {e}")