def upload_scan(scanid, file_path_list): try: IrmaFTP = config.get_ftp_class() ftp_config = config.frontend_config['ftp_brain'] host = ftp_config.host port = ftp_config.port user = ftp_config.username pwd = ftp_config.password with IrmaFTP(host, port, user, pwd) as ftp: ftp.mkdir(scanid) for file_path in file_path_list: log.debug("scanid: %s uploading file %s", scanid, file_path) if not os.path.isfile(file_path): reason = "File does not exist" log.error(reason) raise IrmaFileSystemError(reason) # our ftp handler store file under its sha256 name hashname = ftp.upload_file(scanid, file_path) # and file are stored under their sha256 value sha256 = os.path.basename(file_path) if hashname != sha256: reason = "Ftp Error: integrity failure while uploading \ file {0} for scanid {1}".format(file_path, scanid) log.error(reason) raise IrmaFtpError(reason) return except Exception as e: log.exception(e) reason = "Ftp upload Error" raise IrmaFtpError(reason)
def upload_scan(scanid, file_path_list): try: IrmaFTP = config.get_ftp_class() ftp_config = config.frontend_config['ftp_brain'] host = ftp_config.host port = ftp_config.port user = ftp_config.username pwd = ftp_config.password with IrmaFTP(host, port, user, pwd) as ftp: ftp.mkdir(scanid) for file_path in file_path_list: log.debug("scanid: %s uploading file %s", scanid, file_path) if not os.path.isfile(file_path): reason = "File does not exist" log.error(reason) raise IrmaFileSystemError(reason) # our ftp handler store file under its sha256 name hashname = ftp.upload_file(scanid, file_path) # and file are stored under their sha256 value sha256 = os.path.basename(file_path) if hashname != sha256: reason = "Ftp Error: integrity failure while uploading \ file {0} for scanid {1}".format(file_path, scanid) log.error(reason) raise IrmaFtpError(reason) return except Exception as e: log.exception(e) reason = "Ftp upload Error" raise IrmaFtpError(reason)
def _get_ftp(): IrmaFTP = config.get_ftp_class() ftp_config = config.frontend_config['ftp_brain'] host = ftp_config.host port = ftp_config.port auth = ftp_config.auth key_path = ftp_config.key_path user = ftp_config.username pwd = ftp_config.password return IrmaFTP(host, port, auth, key_path, user, pwd)
def flush_dir(ftpuser, scanid): print("Flushing dir {0}".format(scanid)) IrmaFTP = config.get_ftp_class() conf_ftp = config.brain_config['ftp_brain'] with IrmaFTP(conf_ftp.host, conf_ftp.port, conf_ftp.username, conf_ftp.password, dst_user=ftpuser) as ftp: ftp.deletepath(scanid, deleteParent=True)
def _get_ftp(): IrmaFTP = config.get_ftp_class() ftp_config = config.frontend_config['ftp_brain'] host = ftp_config.host port = ftp_config.port auth = ftp_config.auth key_path = ftp_config.key_path user = ftp_config.username pwd = ftp_config.password return IrmaFTP(host, port, auth, key_path, user, pwd)
def download_file(frontend, path, srcname, dstname): IrmaFTP = config.get_ftp_class() ftp_config = config.probe_config['ftp_brain'] with IrmaFTP(ftp_config.host, ftp_config.port, ftp_config.username, ftp_config.password, dst_user=frontend) as ftp: log.debug("download %s/%s in %s frontend %s", path, srcname, dstname, frontend) ftp.download_file(path, srcname, dstname)
def download_file(frontend, path, srcname, dstname): IrmaFTP = config.get_ftp_class() ftp_config = config.probe_config['ftp_brain'] with IrmaFTP(ftp_config.host, ftp_config.port, ftp_config.username, ftp_config.password, dst_user=frontend) as ftp: log.debug("download %s/%s in %s frontend %s", path, srcname, dstname, frontend) ftp.download_file(path, srcname, dstname)
def flush(ftpuser, filename_list): IrmaFTP = config.get_ftp_class() conf_ftp = config.brain_config['ftp_brain'] with IrmaFTP(conf_ftp.host, conf_ftp.port, conf_ftp.auth, conf_ftp.key_path, conf_ftp.username, conf_ftp.password, dst_user=ftpuser) as ftp: for filename in filename_list: ftp.delete(".", filename)
def upload_files(frontend, path, srcname_list, scanid): IrmaFTP = config.get_ftp_class() uploaded_files = {} ftp_config = config.probe_config['ftp_brain'] with IrmaFTP(ftp_config.host, ftp_config.port, ftp_config.username, ftp_config.password, dst_user=frontend) as ftp: for srcname in srcname_list: full_path = os.path.join(path, srcname) if os.path.isdir(full_path): continue log.debug("upload %s in %s", full_path, scanid) hashname = ftp.upload_file(scanid, full_path) uploaded_files[srcname] = hashname return uploaded_files
def upload_files(frontend, path, srcname_list, scanid): IrmaFTP = config.get_ftp_class() uploaded_files = {} ftp_config = config.probe_config['ftp_brain'] with IrmaFTP(ftp_config.host, ftp_config.port, ftp_config.username, ftp_config.password, dst_user=frontend) as ftp: for srcname in srcname_list: full_path = os.path.join(path, srcname) if os.path.isdir(full_path): continue log.debug("upload %s in %s", full_path, scanid) hashname = ftp.upload_file(scanid, full_path) uploaded_files[srcname] = hashname return uploaded_files
def download_file_data(scanid, file_sha256): try: IrmaFTP = config.get_ftp_class() ftp_config = config.frontend_config['ftp_brain'] host = ftp_config.host port = ftp_config.port user = ftp_config.username pwd = ftp_config.password fobj = TemporaryFile() with IrmaFTP(host, port, user, pwd) as ftp: log.debug("scanid: %s downloading file %s", scanid, file_sha256) ftp.download_fobj(scanid, file_sha256, fobj) return fobj except Exception as e: log.exception(e) reason = "Ftp download Error" raise IrmaFtpError(reason)
def download_file_data(scanid, file_sha256): try: IrmaFTP = config.get_ftp_class() ftp_config = config.frontend_config['ftp_brain'] host = ftp_config.host port = ftp_config.port user = ftp_config.username pwd = ftp_config.password fobj = TemporaryFile() with IrmaFTP(host, port, user, pwd) as ftp: log.debug("scanid: %s downloading file %s", scanid, file_sha256) ftp.download_fobj(scanid, file_sha256, fobj) return fobj except Exception as e: log.exception(e) reason = "Ftp download Error" raise IrmaFtpError(reason)
def upload_files(frontend, path, srcname_list, parent_filename): IrmaFTP = config.get_ftp_class() uploaded_files = {} ftp_config = config.probe_config['ftp_brain'] with IrmaFTP(ftp_config.host, ftp_config.port, ftp_config.auth, ftp_config.key_path, ftp_config.username, ftp_config.password, dst_user=frontend) as ftp: index = 0 for srcname in srcname_list: full_path = os.path.join(path, srcname) if os.path.isdir(full_path): continue dstname = "{}_{}".format(parent_filename, index) index += 1 log.debug("upload %s in %s", full_path, dstname) ftp.upload_file(dstname, full_path) uploaded_files[srcname] = dstname return uploaded_files
def upload_files(frontend, path, srcname_list, parent_filename): IrmaFTP = config.get_ftp_class() uploaded_files = {} ftp_config = config.probe_config['ftp_brain'] with IrmaFTP(ftp_config.host, ftp_config.port, ftp_config.auth, ftp_config.key_path, ftp_config.username, ftp_config.password, dst_user=frontend) as ftp: index = 0 for srcname in srcname_list: full_path = os.path.join(path, srcname) if os.path.isdir(full_path): continue dstname = "{}_{}".format(parent_filename, index) index += 1 log.debug("upload %s in %s", full_path, dstname) ftp.upload_file(dstname, full_path) uploaded_files[srcname] = dstname return uploaded_files
def test_get_ftp_class_sftpv2_error(self, m_config): m_config.ftp.protocol = "sftpv2" m_config.ftp_brain.auth = "key" with self.assertRaises(module.IrmaConfigurationError): module.get_ftp_class()
def test_fget_tp_class_sftpv2(self, m_config): m_config.ftp_brain.auth = "password" m_config.ftp.protocol = "sftpv2" self.assertEqual(module.get_ftp_class(), module.IrmaSFTPv2)
def test_get_ftp_class_sftp_error(self, m_config, m_os): m_config.ftp.protocol = "sftp" m_config.ftp_brain.auth = "key" m_os.path.isfile.return_value = False with self.assertRaises(module.IrmaConfigurationError): module.get_ftp_class()
def test_get_ftp_class_ftps(self, m_config): m_config.ftp.protocol = "ftps" self.assertEqual(module.get_ftp_class(), module.IrmaFTPS)
def test_get_ftp_class_sftpv2_error(self, m_config): m_config.ftp.protocol = "sftpv2" m_config.ftp_brain.auth = "key" with self.assertRaises(module.IrmaConfigurationError): module.get_ftp_class()
def test_get_ftp_class_sftp_error(self, m_config, m_os): m_config.ftp.protocol = "sftp" m_config.ftp_brain.auth = "key" m_os.path.isfile.return_value = False with self.assertRaises(module.IrmaConfigurationError): module.get_ftp_class()
def test_get_ftp_class_sftp(self, m_config): m_config.ftp_brain.auth = "password" m_config.ftp.protocol = "sftp" self.assertEqual(module.get_ftp_class(), module.IrmaSFTP)
def test_get_ftp_class_ftps(self, m_config): m_config.ftp.protocol = "ftps" self.assertEqual(module.get_ftp_class(), module.IrmaFTPS)