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!
This commit is contained in:
parent
b56bb83558
commit
c32201fd05
3 changed files with 28 additions and 25 deletions
|
@ -124,18 +124,6 @@ class PappyConfig(object):
|
|||
|
||||
: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
|
||||
|
||||
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.crypt_dir = os.path.join(os.getcwd(), 'crypt')
|
||||
self.crypt_file = 'project.crypt'
|
||||
self.crypt_mode = None
|
||||
self.salt = os.urandom(16)
|
||||
self.salt_file = 'project.salt'
|
||||
|
||||
def get_default_config(self):
|
||||
|
@ -188,12 +174,14 @@ class PappyConfig(object):
|
|||
|
||||
def get_project_files(self):
|
||||
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)]
|
||||
|
||||
if self.salt_file in project_files:
|
||||
project_files.remove(self.salt_file)
|
||||
if self.crypt_file in project_files:
|
||||
project_files.remove(self.crypt_file)
|
||||
|
||||
return project_files
|
||||
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ import shutil
|
|||
import twisted
|
||||
|
||||
from . import compress
|
||||
from .util import PappyException
|
||||
from base64 import b64encode, b64decode
|
||||
from cryptography.fernet import Fernet
|
||||
from twisted.internet import reactor, defer
|
||||
|
@ -56,9 +57,10 @@ class Crypto(object):
|
|||
# Get the password and salt, then derive the key
|
||||
self.crypto_ramp_up()
|
||||
|
||||
# Create crypto working directory
|
||||
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):
|
||||
# Derive the key
|
||||
|
@ -113,7 +115,7 @@ class Crypto(object):
|
|||
def set_salt_from_file(self):
|
||||
try:
|
||||
salt_file = open(self.config.salt_file, 'rb')
|
||||
self.config.salt = salt_file.readline().strip()
|
||||
self.salt = salt_file.readline().strip()
|
||||
except:
|
||||
raise PappyException("Unable to read project.salt")
|
||||
|
||||
|
@ -121,7 +123,7 @@ class Crypto(object):
|
|||
if os.path.isfile(self.config.salt_file):
|
||||
self.set_salt_from_file()
|
||||
else:
|
||||
self.config.salt = os.urandom(16)
|
||||
self.salt = os.urandom(16)
|
||||
|
||||
def get_password(self):
|
||||
"""
|
||||
|
@ -166,6 +168,8 @@ class Crypto(object):
|
|||
|
||||
try:
|
||||
if not self.key:
|
||||
self.key = scrypt.hash(self.password, self.salt, bufflen=32)
|
||||
except e:
|
||||
raise PappyException("Error deriving the key: ", e)
|
||||
self.key = scrypt.hash(self.password, self.salt, buflen=32)
|
||||
except TypeError, 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()
|
||||
tmpfiles = ['cmdhistory', 'config.json', 'data.db']
|
||||
tmp_pass = 'fugyeahbruh'
|
||||
|
||||
def stub_files():
|
||||
enter_tmpdir()
|
||||
|
@ -40,12 +41,22 @@ def enter_tmpdir():
|
|||
|
||||
def test_decrypt_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'))
|
||||
|
||||
def test_decrypt_copy_files():
|
||||
enter_tmpdir()
|
||||
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:
|
||||
assert os.path.isfile(tf)
|
||||
assert os.path.isfile(os.path.join(os.getcwd(),tf))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue