Tested and fixed file copying from `Crypto.decrypt_project`

In the function for grabbing project files (`Config.get_project_files`)
I was overcomplicating getting the current working directory.
Simplified the process and removed the bug!
master
onizenso 9 years ago
parent b56bb83558
commit c32201fd05
  1. 18
      pappyproxy/config.py
  2. 18
      pappyproxy/crypto.py
  3. 17
      pappyproxy/tests/test_crypto.py

@ -124,18 +124,6 @@ class PappyConfig(object):
:Default: 'project.crypt' :Default: 'project.crypt'
.. data: crypt_mode
Boolean value to determine whether project is being decrypted or encrypted, during
start-up and tear-down respectively.
.. data: salt
Nonce value used for key derivation. Generated by reading 16 bytes
from /dev/urandom.
:Default: ``os.urandom(16)``
.. data: salt_file .. data: salt_file
Clear-text file containing the salt generated for key derivation. A new salt Clear-text file containing the salt generated for key derivation. A new salt
@ -175,8 +163,6 @@ class PappyConfig(object):
self.archive = 'project.archive' self.archive = 'project.archive'
self.crypt_dir = os.path.join(os.getcwd(), 'crypt') self.crypt_dir = os.path.join(os.getcwd(), 'crypt')
self.crypt_file = 'project.crypt' self.crypt_file = 'project.crypt'
self.crypt_mode = None
self.salt = os.urandom(16)
self.salt_file = 'project.salt' self.salt_file = 'project.salt'
def get_default_config(self): def get_default_config(self):
@ -188,12 +174,14 @@ class PappyConfig(object):
def get_project_files(self): def get_project_files(self):
file_glob = glob.glob('*') file_glob = glob.glob('*')
pp = os.path.join(os.getcwd()) pp = os.getcwd() + os.sep
project_files = [pp+f for f in file_glob if os.path.isfile(pp+f)] project_files = [pp+f for f in file_glob if os.path.isfile(pp+f)]
if self.salt_file in project_files: if self.salt_file in project_files:
project_files.remove(self.salt_file) project_files.remove(self.salt_file)
if self.crypt_file in project_files: if self.crypt_file in project_files:
project_files.remove(self.crypt_file) project_files.remove(self.crypt_file)
return project_files return project_files

@ -9,6 +9,7 @@ import shutil
import twisted import twisted
from . import compress from . import compress
from .util import PappyException
from base64 import b64encode, b64decode from base64 import b64encode, b64decode
from cryptography.fernet import Fernet from cryptography.fernet import Fernet
from twisted.internet import reactor, defer from twisted.internet import reactor, defer
@ -56,9 +57,10 @@ 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()
# Create crypto working directory
crypto_path = os.path.join(os.getcwd(), self.config.crypt_dir) crypto_path = os.path.join(os.getcwd(), self.config.crypt_dir)
os.mkdir(crypto_path)
if not os.path.isdir(crypto_path):
os.mkdir(crypto_path)
if os.path.isfile(self.config.crypt_file): if os.path.isfile(self.config.crypt_file):
# Derive the key # Derive the key
@ -113,7 +115,7 @@ class Crypto(object):
def set_salt_from_file(self): def set_salt_from_file(self):
try: try:
salt_file = open(self.config.salt_file, 'rb') salt_file = open(self.config.salt_file, 'rb')
self.config.salt = salt_file.readline().strip() self.salt = salt_file.readline().strip()
except: except:
raise PappyException("Unable to read project.salt") raise PappyException("Unable to read project.salt")
@ -121,7 +123,7 @@ class Crypto(object):
if os.path.isfile(self.config.salt_file): if os.path.isfile(self.config.salt_file):
self.set_salt_from_file() self.set_salt_from_file()
else: else:
self.config.salt = os.urandom(16) self.salt = os.urandom(16)
def get_password(self): def get_password(self):
""" """
@ -166,6 +168,8 @@ class Crypto(object):
try: try:
if not self.key: if not self.key:
self.key = scrypt.hash(self.password, self.salt, bufflen=32) self.key = scrypt.hash(self.password, self.salt, buflen=32)
except e: except TypeError, e:
raise PappyException("Error deriving the key: ", e) raise PappyException("Scrypt failed with type error: ", e)
except scrypt.error, e:
raise PappyException("Scrypt failed with internal error: ", e)

@ -26,6 +26,7 @@ def tmpname():
tmpdir = '/tmp/test_crypto'+tmpname() tmpdir = '/tmp/test_crypto'+tmpname()
tmpfiles = ['cmdhistory', 'config.json', 'data.db'] tmpfiles = ['cmdhistory', 'config.json', 'data.db']
tmp_pass = 'fugyeahbruh'
def stub_files(): def stub_files():
enter_tmpdir() enter_tmpdir()
@ -40,12 +41,22 @@ def enter_tmpdir():
def test_decrypt_tmpdir(): def test_decrypt_tmpdir():
enter_tmpdir() enter_tmpdir()
crypt().decrypt_project() c = crypt()
# Stub out the password, working with stdout is a pain with pytest
c.password = tmp_pass
c.decrypt_project()
assert os.path.isdir(os.path.join(os.getcwd(), '../crypt')) assert os.path.isdir(os.path.join(os.getcwd(), '../crypt'))
def test_decrypt_copy_files(): def test_decrypt_copy_files():
enter_tmpdir() enter_tmpdir()
stub_files() stub_files()
crypt().decrypt_project() c = crypt()
# Stub out the password, working with stdout is a pain with pytest
c.password = tmp_pass
c.decrypt_project()
for tf in tmpfiles: for tf in tmpfiles:
assert os.path.isfile(tf) assert os.path.isfile(os.path.join(os.getcwd(),tf))

Loading…
Cancel
Save