def test_Class_encryption_file(self): # setup the GPG key before GLSetting.gpgroot = GPGROOT tempsource = os.path.join(os.getcwd(), "temp_source.txt") with file(tempsource, 'w+') as f: f.write("\n\nDecrypt the Cat!\n\nhttp://tobtu.com/decryptocat.php\n\n") f.seek(0) fake_receiver_desc = { 'gpg_key_armor': unicode(DeveloperKey.__doc__), 'gpg_key_status': Receiver._gpg_types[1], 'gpg_key_fingerprint': u"C1ED5C8FDB6A1C74A807569591EC9BB8D9A950DE", 'username': u'*****@*****.**', } # these are the same lines used in delivery_sched.py gpoj = GLBGPG(fake_receiver_desc) gpoj.validate_key(DeveloperKey.__doc__) encrypted_file_path, encrypted_file_size = gpoj.encrypt_file(tempsource, f, "/tmp") gpoj.destroy_environment() with file(encrypted_file_path, "r") as f: first_line = f.readline() self.assertSubstring('-----BEGIN PGP MESSAGE-----', first_line) with file(encrypted_file_path, "r") as f: whole = f.read() self.assertEqual(encrypted_file_size, len(whole))
def fsops_gpg_encrypt(fpath, recipient_gpg): """ return path of encrypted file, length of the encrypted file this function is used to encrypt a file for a specific recipient. commonly 'receiver_desc' is expected as second argument; anyhow a simpler dict can be used. required keys are checked on top """ gpoj = GLBGPG() try: gpoj.load_key(recipient_gpg['gpg_key_armor']) filepath = os.path.join(GLSetting.submission_path, fpath) with GLSecureFile(filepath) as f: encrypted_file_path, encrypted_file_size = \ gpoj.encrypt_file(recipient_gpg['gpg_key_fingerprint'], filepath, f, GLSetting.submission_path) except: raise finally: # the finally statement is always called also if # except contains a return or a raise gpoj.destroy_environment() return encrypted_file_path, encrypted_file_size
def test_Class_encryption_file(self): # setup the GPG key before GLSetting.gpgroot = GPGROOT tempsource = os.path.join(os.getcwd(), "temp_source.txt") with file(tempsource, 'w+') as f1: f1.write("\n\nDecrypt the Cat!\n\nhttp://tobtu.com/decryptocat.php\n\n") f1.seek(0) fake_receiver_desc = { 'gpg_key_armor': unicode(VALID_PGP_KEY), 'gpg_key_status': Receiver._gpg_types[1], 'gpg_key_fingerprint': u"CF4A22020873A76D1DCB68D32B25551568E49345", 'username': u'*****@*****.**', } # these are the same lines used in delivery_sched.py gpoj = GLBGPG(fake_receiver_desc) gpoj.validate_key(VALID_PGP_KEY) encrypted_file_path, encrypted_file_size = gpoj.encrypt_file(tempsource, f1, "/tmp") gpoj.destroy_environment() with file(encrypted_file_path, "r") as f2: first_line = f2.readline() self.assertSubstring('-----BEGIN PGP MESSAGE-----', first_line) with file(encrypted_file_path, "r") as f2: whole = f2.read() self.assertEqual(encrypted_file_size, len(whole))
def test_Class_encryption_file(self): # setup the GPG key before GLSetting.gpgroot = GPGROOT tempsource = os.path.join(os.getcwd(), "temp_source.txt") with file(tempsource, 'w+') as f1: f1.write( "\n\nDecrypt the Cat!\n\nhttp://tobtu.com/decryptocat.php\n\n") f1.seek(0) fake_receiver_desc = { 'gpg_key_armor': unicode(VALID_PGP_KEY), 'gpg_key_status': Receiver._gpg_types[1], 'gpg_key_fingerprint': u"CF4A22020873A76D1DCB68D32B25551568E49345", 'username': u'*****@*****.**', } # these are the same lines used in delivery_sched.py gpoj = GLBGPG(fake_receiver_desc) gpoj.validate_key(VALID_PGP_KEY) encrypted_file_path, encrypted_file_size = gpoj.encrypt_file( tempsource, f1, "/tmp") gpoj.destroy_environment() with file(encrypted_file_path, "r") as f2: first_line = f2.readline() self.assertSubstring('-----BEGIN PGP MESSAGE-----', first_line) with file(encrypted_file_path, "r") as f2: whole = f2.read() self.assertEqual(encrypted_file_size, len(whole))
def fsops_gpg_encrypt(fpath, recipient_gpg): """ return path of encrypted file, length of the encrypted file this function is used to encrypt a file for a specific recipient. commonly 'receiver_desc' is expected as second argument; anyhow a simpler dict can be used. required keys are checked on top """ assert isinstance(recipient_gpg, dict), "invalid recipient" assert recipient_gpg.has_key('gpg_key_armor'), "missing key" assert recipient_gpg.has_key('gpg_key_status'), "missing status" assert recipient_gpg['gpg_key_status'] == u'Enabled', "GPG not enabled" assert recipient_gpg.has_key('name'), "missing recipient Name" try: gpoj = GLBGPG(recipient_gpg) if not gpoj.validate_key(recipient_gpg['gpg_key_armor']): raise Exception("Unable to validate key") ifile_abs = os.path.join(GLSetting.submission_path, fpath) with file(ifile_abs, "r") as f: encrypted_file_path, encrypted_file_size = \ gpoj.encrypt_file(ifile_abs, f, GLSetting.submission_path) gpoj.destroy_environment() assert encrypted_file_size > 1 assert os.path.isfile(encrypted_file_path) return encrypted_file_path, encrypted_file_size except Exception as excep: # Yea, please, don't mention protocol downgrade. log.err("Error in encrypting %s for %s: fallback in plaintext (%s)" % ( fpath, recipient_gpg['name'], excep ) ) raise excep
def fsops_gpg_encrypt(fpath, recipient_gpg): """ return path of encrypted file, length of the encrypted file this function is used to encrypt a file for a specific recipient. commonly 'receiver_desc' is expected as second argument; anyhow a simpler dict can be used. required keys are checked on top """ assert isinstance(recipient_gpg, dict), "invalid recipient" assert recipient_gpg.has_key('gpg_key_armor'), "missing key" assert recipient_gpg.has_key('gpg_key_status'), "missing status" assert recipient_gpg['gpg_key_status'] == u'Enabled', "GPG not enabled" assert recipient_gpg.has_key('name'), "missing recipient Name" gpoj = GLBGPG(recipient_gpg) if not gpoj.validate_key(recipient_gpg['gpg_key_armor']): raise Exception("Unable to validate key") filepath = os.path.join(GLSetting.submission_path, fpath) with GLSecureFile(filepath) as f: encrypted_file_path, encrypted_file_size = \ gpoj.encrypt_file(filepath, f, GLSetting.submission_path) gpoj.destroy_environment() assert (encrypted_file_size > 1), "File generated is empty or size is 0" assert os.path.isfile( encrypted_file_path), "Output generated is not a file!" return encrypted_file_path, encrypted_file_size
def fsops_gpg_encrypt(fpath, recipient_gpg): """ return path of encrypted file, length of the encrypted file this function is used to encrypt a file for a specific recipient. commonly 'receiver_desc' is expected as second argument; anyhow a simpler dict can be used. required keys are checked on top """ assert isinstance(recipient_gpg, dict), "invalid recipient" assert recipient_gpg.has_key('gpg_key_armor'), "missing key" assert recipient_gpg.has_key('gpg_key_status'), "missing status" assert recipient_gpg['gpg_key_status'] == u'Enabled', "GPG not enabled" assert recipient_gpg.has_key('name'), "missing recipient Name" gpoj = GLBGPG(recipient_gpg) if not gpoj.validate_key(recipient_gpg['gpg_key_armor']): raise Exception("Unable to validate key") filepath = os.path.join(GLSetting.submission_path, fpath) with GLSecureFile(filepath) as f: encrypted_file_path, encrypted_file_size = \ gpoj.encrypt_file(filepath, f, GLSetting.submission_path) gpoj.destroy_environment() assert (encrypted_file_size > 1), "File generated is empty or size is 0" assert os.path.isfile(encrypted_file_path), "Output generated is not a file!" return encrypted_file_path, encrypted_file_size