Fixed bugs with crypto.py, need to work on cleanup

Project is now properly encrypting the archive,
and now I just need to ensure proper decryption
is happening. Also need to work on cleaning up
clear text versions of the crypt project files.
Need to write tests for flushing out edge cases.
master
Nich 9 years ago
parent c32201fd05
commit a3cb5f13ed
  1. 38
      pappyproxy/compress.py
  2. 6
      pappyproxy/crypto.py
  3. 9
      pappyproxy/pappy.py

@ -23,19 +23,19 @@ class Compress(object):
self.zip_archive = sessconfig.archive self.zip_archive = sessconfig.archive
self.bz2_archive = sessconfig.archive self.bz2_archive = sessconfig.archive
def compress_project(): def compress_project(self):
if bz2: if bz2:
tar_project() self.tar_project()
else: else:
zip_project() self.zip_project()
def decompress_project(): def decompress_project(self):
if bz2: if bz2:
untar_project() self.untar_project()
else: else:
unzip_project() self.unzip_project()
def zip_project(): def zip_project(self):
""" """
Zip project files Zip project files
@ -52,7 +52,7 @@ class Compress(object):
raise PappyException("Error creating the zipfile", e) raise PappyException("Error creating the zipfile", e)
pass pass
def unzip_project(): def unzip_project(self):
""" """
Extract project files from decrypted zip archive. Extract project files from decrypted zip archive.
Initially checks the zip archive's magic number and Initially checks the zip archive's magic number and
@ -71,24 +71,20 @@ class Compress(object):
zf.extractall() zf.extractall()
def tar_project(): def tar_project(self):
if tarfile.is_tarfile(self.bz2_archive): archive = tarfile.open(self.bz2_archive, 'w:bz2')
archive = tarfile.open(self.bz2_archive, 'w:bz2') project_files = self.config.get_project_files()
project_files = self.config.get_project_files()
# Read files line by line to accomodate larger files, e.g. the project database
# Read files line by line to accomodate larger files, e.g. the project database for pf in project_files:
for pf in project_files: archive.add(pf)
archive.add(pf)
archive.close() archive.close()
def untar_project(): def untar_project(self):
if tarfile.is_tarfile(self.bz2_archive): if tarfile.is_tarfile(self.bz2_archive):
# Attempt to read the first 16 bytes of the archive
# Raise exception if there is a failure # Raise exception if there is a failure
project_files = self.config.get_project_files()
try: try:
with tarfile.open(self.bz2_archive, "r:bz2") as archive: with tarfile.open(self.bz2_archive, "r:bz2") as archive:
for pf in project_files: archive.extractall()
archive.add(pf)
except e: except e:
raise PappyException("Project archive contents corrupted. Error: ", e) raise PappyException("Project archive contents corrupted. Error: ", e)

@ -36,7 +36,7 @@ class Crypto(object):
# Create project archive and crypto archive # Create project archive and crypto archive
self.compressor.compress_project() self.compressor.compress_project()
archive_file = open(self.archive, 'rb') archive_file = open(self.archive, 'rb').read()
archive_crypt = open(self.config.crypt_file, 'wb') archive_crypt = open(self.config.crypt_file, 'wb')
# Encrypt the archive read as a bytestring # Encrypt the archive read as a bytestring
@ -57,7 +57,7 @@ class Crypto(object):
# Get the password and salt, then derive the key # Get the password and salt, then derive the key
self.crypto_ramp_up() self.crypto_ramp_up()
crypto_path = os.path.join(os.getcwd(), self.config.crypt_dir) crypto_path = self.config.crypt_dir
if not os.path.isdir(crypto_path): if not os.path.isdir(crypto_path):
os.mkdir(crypto_path) os.mkdir(crypto_path)
@ -168,7 +168,7 @@ class Crypto(object):
try: try:
if not self.key: if not self.key:
self.key = scrypt.hash(self.password, self.salt, buflen=32) self.key = b64encode(scrypt.hash(self.password, self.salt, buflen=32))
except TypeError, e: except TypeError, e:
raise PappyException("Scrypt failed with type error: ", e) raise PappyException("Scrypt failed with type error: ", e)
except scrypt.error, e: except scrypt.error, e:

@ -145,11 +145,11 @@ class PappySession(object):
@defer.inlineCallbacks @defer.inlineCallbacks
def encrypt(self): def encrypt(self):
self.crypto.encrypt_project(self.password) yield self.crypto.encrypt_project()
@defer.inlineCallbacks @defer.inlineCallbacks
def decrypt(self): def decrypt(self):
self.crypto.decrypt_project() yield self.crypto.decrypt_project()
@defer.inlineCallbacks @defer.inlineCallbacks
def cleanup(self, ignored=None): def cleanup(self, ignored=None):
@ -159,6 +159,11 @@ class PappySession(object):
if self.delete_data_on_quit: if self.delete_data_on_quit:
print 'Deleting temporary datafile' print 'Deleting temporary datafile'
os.remove(self.config.datafile) os.remove(self.config.datafile)
# If currently in the crypt directory,
# encrypt the project, delete clear files
if os.getcwd() == self.config.crypt_dir:
self.encrypt()
def parse_args(): def parse_args():
# parses sys.argv and returns a settings dictionary # parses sys.argv and returns a settings dictionary

Loading…
Cancel
Save