def execute(self, *args): super().execute() files = super().get_target_files(self._src_dir, self._src_pattern) if len(files) == 0: self._logger.info("No files are found. Nothing to do.") return if self._key_dir and self._key_pattern: key_files = super().get_target_files(self._key_dir, self._key_pattern) self._logger.info("Keys found %s" % key_files) self.key_import(key_files, self._trust_level) gpg = Gpg(self._gnupghome) for file in files: root, ext = os.path.splitext(file) if ext == ".gpg": dest_path = (os.path.join(self._dest_dir, os.path.basename(root)) if self._dest_dir is not None else os.path.join( self._src_dir, os.path.basename(root))) gpg.decrypt( file, dest_path, passphrase=self._passphrase, always_trust=self._always_trust, ) else: self._logger.warning("Extention was not gpg. %s" % file)
def execute(self, *args): super().execute() valid = EssentialParameters(self.__class__.__name__, [self._recipients]) valid() files = super().get_target_files(self._src_dir, self._src_pattern) if len(files) == 0: self._logger.info("No files are found. Nothing to do.") return gpg = Gpg(self._gnupghome) if self._key_dir and self._key_pattern: key_files = super().get_target_files(self._key_dir, self._key_pattern) self._logger.info("Keys found %s" % key_files) self.key_import(gpg, key_files, self._trust_level) for file in files: dest_path = (os.path.join(self._dest_dir, os.path.basename(file)) if self._dest_dir is not None else os.path.join( self._src_dir, os.path.basename(file))) gpg.encrypt( file, dest_path, recipients=self._recipients, passphrase=self._passphrase, always_trust=self._always_trust, )
def test_encrypt_decrypt_ok(self): gpg = Gpg(self._gpg_dir) gpg.generate_key(dest_dir=self._data_dir, name_email="*****@*****.**", passphrase="password") # Encryption instance = GpgEncrypt() Helper.set_property(instance, "logger", LisboaLog.get_logger(__name__)) Helper.set_property(instance, "gnupghome", self._gpg_dir) Helper.set_property(instance, "src_dir", self._data_dir) Helper.set_property(instance, "src_pattern", r"test\.txt") Helper.set_property(instance, "dest_dir", self._result_dir) Helper.set_property(instance, "recipients", ["*****@*****.**"]) instance.execute() # Decryption instance = GpgDecrypt() Helper.set_property(instance, "logger", LisboaLog.get_logger(__name__)) Helper.set_property(instance, "gnupghome", self._gpg_dir) Helper.set_property(instance, "src_dir", self._result_dir) Helper.set_property(instance, "src_pattern", r"test\.txt\.gpg") Helper.set_property(instance, "passphrase", "password") instance.execute() with open(os.path.join(self._result_dir, self._file_name), mode="r", encoding="utf-8") as f: txt = f.read() assert txt == "This is test"
def test_generate_key_and_encrypt_decrypt_ok(self): """ Generate rsa key, encryption, decryption, everything is ok. """ _gpg = Gpg(self._gpg_dir) _gpg.generate_key( dest_dir=self._data_dir, name_email="*****@*****.**", passphrase="password" ) _gpg.encrypt( src_path=self._file_path, dest_path=os.path.join(self._result_dir, self._file_name), recipients="*****@*****.**", ) _gpg.decrypt( src_path=os.path.join(self._result_dir, self._encrypt_name), dest_path=os.path.join(self._result_dir, self._file_name), passphrase="password", ) with open( os.path.join(self._result_dir, self._file_name), mode="r", encoding="utf-8" ) as f: txt = f.read() assert txt == "This is test"
def execute(self, *args): super().execute() valid = EssentialParameters(self.__class__.__name__, [self._dest_dir, self._name_email]) valid() Gpg(self._gnupghome).generate_key( self._dest_dir, name_real=self._name_real, name_email=self._name_email, passphrase=self._passphrase, )
def test_generate_key_and_decrypt_ng(self): """ Decryption fails due to wrong password. """ _gpg = Gpg(self._gpg_dir) _gpg.generate_key( dest_dir=self._data_dir, name_email="*****@*****.**", passphrase="password" ) _gpg.encrypt( src_path=self._file_path, dest_path=os.path.join(self._result_dir, self._file_name), recipients="*****@*****.**", ) with pytest.raises(CliboaException) as execinfo: _gpg.decrypt( src_path=os.path.join(self._result_dir, self._encrypt_name), dest_path=os.path.join(self._result_dir, self._file_name), passphrase="password2", ) assert "Failed to decrypt gpg." in str(execinfo.value)
def test_import_key_and_encrypt_decrypt_ok(self): """ Check if public and private keys work for encryption and decryption. """ _gpg = Gpg(self._gpg_dir) _gpg.generate_key( dest_dir=self._data_dir, name_email="*****@*****.**", passphrase="password" ) # Remove gpg home and re-create other one. # This will clear all trusted data store. shutil.rmtree(self._gpg_dir) _gpg = Gpg(self._gpg_dir) # Encrypt with no keys. Expect an error. with pytest.raises(CliboaException) as execinfo: _gpg.encrypt( src_path=self._file_path, dest_path=os.path.join(self._result_dir, self._file_name), recipients="*****@*****.**", always_trust=True, ) assert "Failed to encrypt gpg." in str(execinfo.value) # Import public key and encrypt. _gpg.import_key(os.path.join(self._data_dir, "public")) _gpg.encrypt( src_path=self._file_path, dest_path=os.path.join(self._result_dir, self._file_name), recipients="*****@*****.**", always_trust=True, ) # Decrypt with no keys(Only public key is imported). Expect an error. with pytest.raises(CliboaException) as execinfo: _gpg.decrypt( src_path=os.path.join(self._result_dir, self._encrypt_name), dest_path=os.path.join(self._result_dir, self._file_name), passphrase="password", always_trust=True, ) assert "Failed to decrypt gpg." in str(execinfo.value) # Import private key and decrypt. _gpg.import_key(os.path.join(self._data_dir, "private")) _gpg.decrypt( src_path=os.path.join(self._result_dir, self._encrypt_name), dest_path=os.path.join(self._result_dir, self._file_name), passphrase="password", always_trust=True, ) with open( os.path.join(self._result_dir, self._file_name), mode="r", encoding="utf-8" ) as f: txt = f.read() assert txt == "This is test"