Пример #1
0
 def _decrypt(self, filepath: str, db_object: Union[File, Message, Reply],
              session: Session) -> None:
     """
     Decrypt the file located at the given filepath and mark it as decrypted.
     """
     try:
         original_filename = self.call_decrypt(filepath, session)
         db_object.download_error = None
         mark_as_decrypted(type(db_object),
                           db_object.uuid,
                           session,
                           original_filename=original_filename)
         logger.info(f"File decrypted to {os.path.dirname(filepath)}")
     except CryptoError as e:
         mark_as_decrypted(type(db_object),
                           db_object.uuid,
                           session,
                           is_decrypted=False)
         download_error = (session.query(DownloadError).filter_by(
             name=DownloadErrorCodes.DECRYPTION_ERROR.name).one())
         db_object.download_error = download_error
         session.commit()
         raise DownloadDecryptionException(
             f"Failed to decrypt file: {os.path.basename(filepath)}",
             type(db_object),
             db_object.uuid,
         ) from e
Пример #2
0
 def _decrypt(self, filepath: str, db_object: Union[File, Message, Reply],
              session: Session) -> None:
     '''
     Decrypt the file located at the given filepath and mark it as decrypted.
     '''
     try:
         original_filename = self.call_decrypt(filepath, session)
         db_object.download_error = None
         mark_as_decrypted(type(db_object),
                           db_object.uuid,
                           session,
                           original_filename=original_filename)
         logger.info("File decrypted: {} (decrypted file in: {})".format(
             os.path.basename(filepath), os.path.dirname(filepath)))
     except CryptoError as e:
         mark_as_decrypted(type(db_object),
                           db_object.uuid,
                           session,
                           is_decrypted=False)
         download_error = session.query(DownloadError).filter_by(
             name=DownloadErrorCodes.DECRYPTION_ERROR.name).one()
         db_object.download_error = download_error
         session.commit()
         logger.debug("Failed to decrypt file: {}".format(
             os.path.basename(filepath)))
         raise DownloadDecryptionException(
             "Downloaded file could not be decrypted.", type(db_object),
             db_object.uuid) from e
Пример #3
0
def test_set_file_decryption_status_with_content_null_to_false(
        mocker, session):
    file = factory.File(source=factory.Source(), is_decrypted=None)
    session.add(file)
    session.commit()

    mark_as_decrypted(type(file), file.uuid, session, False)

    assert file.is_decrypted is False
Пример #4
0
def test_set_file_decryption_status_with_content_false_to_true(
        mocker, session):
    file = factory.File(source=factory.Source(),
                        is_downloaded=True,
                        is_decrypted=False)
    session.add(file)
    session.commit()

    mark_as_decrypted(type(file), file.uuid, session)

    assert file.is_decrypted is True
Пример #5
0
 def _decrypt(self,
              filepath: str,
              db_object: Union[File, Message, Reply],
              session: Session) -> None:
     '''
     Decrypt the file located at the given filepath and mark it as decrypted.
     '''
     try:
         original_filename = self.call_decrypt(filepath, session)
         mark_as_decrypted(
             type(db_object), db_object.uuid, session, original_filename=original_filename
         )
         logger.info("File decrypted: {}".format(os.path.basename(filepath)))
     except CryptoError as e:
         mark_as_decrypted(type(db_object), db_object.uuid, session, is_decrypted=False)
         logger.debug("Failed to decrypt file: {}".format(os.path.basename(filepath)))
         raise e
Пример #6
0
def test_set_message_decryption_status_with_content_with_content(
        session, source):
    """
    It should be possible to set the decryption status of an object in the database to `True`.
    Additionally, if `content` is passed in, the `content` column of the DB should take that
    value. This is to ensure that we have a way to decrypt something without violating the
    condition: if is_decrypted then content is not none.
    """
    message = factory.Message(source=source["source"],
                              is_downloaded=True,
                              is_decrypted=None,
                              content=None)
    session.add(message)
    session.commit()

    set_message_or_reply_content(type(message), message.uuid, "mock_content",
                                 session)
    mark_as_decrypted(type(message), message.uuid, session)

    # requery to ensure new object
    message = session.query(db.Message).get(message.id)
    assert message.is_decrypted is True
    assert message.content == "mock_content"