Skip to content

Commit

Permalink
Merge aa01ff8 into bbe9845
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvw committed Jan 21, 2019
2 parents bbe9845 + aa01ff8 commit e2b7c2e
Show file tree
Hide file tree
Showing 58 changed files with 1,251 additions and 1,006 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.pypirc
etc/*
.venv/*
venv/*

# Swap
[._]*.s[a-v][a-z]
Expand Down
12 changes: 2 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,18 @@ sudo: true
services:
- mysql

branches:
only:
- develop

before_install:
- sudo apt-get install python3-dev

install:
- pip install --upgrade pip
- pip install -r requirements.txt
- pip install -e .
- pip install flake8
- pip install coveralls
- pip install flake8 "tox>=1.8" coveralls tox-travis

before_script:
- flake8 django_crypto_fields
- mysql -e 'create database edc character set utf8;'

script:
- coverage run --source=django_crypto_fields manage.py test
- coverage run --source=django_crypto_fields setup.py test

after_success:
- coveralls
10 changes: 6 additions & 4 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
include AUTHORS CHANGES README.rst LICENSE VERSION
recursive-include docs *
recursive-exclude example/*
recursive-exclude etc/*
include AUTHORS CHANGES LICENSE VERSION
include MANIFEST.in
include *.rst
include *.txt
recursive-include docs *.rst
recursive-exclude etc *.*
50 changes: 50 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# all: init docs clean test
all: init clean test

clean: clean-build clean-pyc
rm -fr htmlcov/

clean-build:
rm -fr build/
rm -fr dist/
rm -fr *.egg-info

clean-pyc:
find . -name '*.pyc' -exec rm -f {} +
find . -name '*.pyo' -exec rm -f {} +
find . -name '*~' -exec rm -f {} +

init:
pip install -U pip
pip install "tox>=1.8" coverage Sphinx codecov black

test:
coverage erase
tox
coverage html

docs: documentation

documentation:
sphinx-build -b html -d docs/_build/doctrees docs docs/_build/html

dist: clean
pip install -U wheel
python setup.py sdist
python setup.py bdist_wheel
for file in dist/* ; do gpg --detach-sign -a "$$file" ; done
ls -l dist

test-release: dist
pip install -U twine
gpg --detach-sign -a dist/*
twine upload -r pypitest dist/*

release: dist
pip install -U twine
gpg --detach-sign -a dist/*
twine upload dist/*

format:
pip install -U black
black django_crypto_fields
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
|pypi| |travis| |coverage|
|pypi| |travis| |coverage| |downloads|

django-crypto-fields
--------------------
Expand Down Expand Up @@ -116,3 +116,6 @@ Contribute

.. |coverage| image:: https://coveralls.io/repos/github/erikvw/django-crypto-fields/badge.svg?branch=develop
:target: https://coveralls.io/github/erikvw/django-crypto-fields?branch=develop

.. |downloads| image:: https://pepy.tech/badge/django-crypto-fields
:target: https://pepy.tech/project/django-crypto-fields
8 changes: 4 additions & 4 deletions django_crypto_fields/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
@admin.register(Crypt, site=encryption_admin)
class CryptModelAdmin(admin.ModelAdmin):

date_hierarchy = 'modified'
date_hierarchy = "modified"

fields = sorted([field.name for field in Crypt._meta.fields])

readonly_fields = [field.name for field in Crypt._meta.fields]

list_display = ('algorithm', 'hash', 'modified', 'hostname_modified')
list_display = ("algorithm", "hash", "modified", "hostname_modified")

list_filter = ('algorithm', 'modified', 'hostname_modified')
list_filter = ("algorithm", "modified", "hostname_modified")

search_fields = ('hash', )
search_fields = ("hash",)
10 changes: 5 additions & 5 deletions django_crypto_fields/admin_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@


class CryptoFieldsAdminSite(AdminSite):
site_header = 'Data Encryption'
site_title = 'Data Encryption'
index_title = 'Data Encryption'
site_url = '/administration/'
site_header = "Data Encryption"
site_title = "Data Encryption"
index_title = "Data Encryption"
site_url = "/administration/"


encryption_admin = CryptoFieldsAdminSite(name='encryption_admin')
encryption_admin = CryptoFieldsAdminSite(name="encryption_admin")
70 changes: 39 additions & 31 deletions django_crypto_fields/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ class DjangoCryptoFieldsKeysDoNotExist(Exception):


class AppConfig(DjangoAppConfig):
name = 'django_crypto_fields'
name = "django_crypto_fields"
verbose_name = "Data Encryption"
_keys = None
_key_path_validated = None
app_label = 'django_crypto_fields'
last_key_path_filename = 'django_crypto_fields'
key_reference_model = 'django_crypto_fields.keyreference'
app_label = "django_crypto_fields"
last_key_path_filename = "django_crypto_fields"
key_reference_model = "django_crypto_fields.keyreference"
# change if using more than one database and not 'default'.
crypt_model_using = 'default'
crypt_model_using = "default"

def __init__(self, app_label, model_name):
"""Placed here instead of `ready()`. For models to
Expand All @@ -44,53 +44,61 @@ def __init__(self, app_label, model_name):
"""
self.temp_path = mkdtemp()
self._key_path = KeyPath(
path=self.temp_path if 'test' in sys.argv and 'raven' not in sys.argv else None)
path=self.temp_path
if "test" in sys.argv and "raven" not in sys.argv
else None
)
self.key_files = None
self.last_key_path = get_last_key_path(self.last_key_path_filename)

sys.stdout.write(f'Loading {self.verbose_name} (init)...\n')
sys.stdout.write(f"Loading {self.verbose_name} (init)...\n")

self.key_files = KeyFiles(key_path=self.key_path)
if not self._keys and not self.key_files.key_files_exist:
if self.auto_create_keys:
if not os.access(self.key_path.path, os.W_OK):
raise DjangoCryptoFieldsError(
'Cannot auto-create encryption keys. Folder is not writeable.'
f'Got {self.key_path}')
sys.stdout.write(style.SUCCESS(
f' * settings.AUTO_CREATE_KEYS={self.auto_create_keys}.\n'))
key_creator = KeyCreator(
key_files=self.key_files, verbose_mode=True)
"Cannot auto-create encryption keys. Folder is not writeable."
f"Got {self.key_path}"
)
sys.stdout.write(
style.SUCCESS(
f" * settings.AUTO_CREATE_KEYS={self.auto_create_keys}.\n"
)
)
key_creator = KeyCreator(key_files=self.key_files, verbose_mode=True)
key_creator.create_keys()
self._keys = Keys(key_path=self.key_path)
self._keys.load_keys()
else:
raise DjangoCryptoFieldsKeysDoNotExist(
f'Failed to find any encryption keys in path {self.key_path}. '
'If this is your first time loading '
'the project, set settings.AUTO_CREATE_KEYS=True and restart. '
'Make sure the folder is writeable.')

sys.stdout.write(style.WARNING(
f' * settings.AUTO_CREATE_KEYS={self.auto_create_keys}.\n'))
f"Failed to find any encryption keys in path {self.key_path}. "
"If this is your first time loading "
"the project, set settings.AUTO_CREATE_KEYS=True and restart. "
"Make sure the folder is writeable."
)

sys.stdout.write(
style.WARNING(
f" * settings.AUTO_CREATE_KEYS={self.auto_create_keys}.\n"
)
)
else:
self._keys = Keys(key_path=self.key_path)
self._keys.load_keys()

super().__init__(app_label, model_name)
sys.stdout.write(f' Done loading {self.verbose_name} (init)...\n')
sys.stdout.write(f" Done loading {self.verbose_name} (init)...\n")

def ready(self):
sys.stdout.write(f'Loading {self.verbose_name} ...\n')
if 'test' not in sys.argv:
register(key_path_check)(['django_crypto_fields'])
register(encryption_keys_check)(['django_crypto_fields'])
sys.stdout.write(f"Loading {self.verbose_name} ...\n")
if "test" not in sys.argv:
register(key_path_check)(["django_crypto_fields"])
register(encryption_keys_check)(["django_crypto_fields"])
register(aes_mode_check)
sys.stdout.write(
f' * found encryption keys in {self.key_path}.\n')
sys.stdout.write(
f' * using model {self.app_label}.crypt.\n')
sys.stdout.write(f' Done loading {self.verbose_name}.\n')
sys.stdout.write(f" * found encryption keys in {self.key_path}.\n")
sys.stdout.write(f" * using model {self.app_label}.crypt.\n")
sys.stdout.write(f" Done loading {self.verbose_name}.\n")

@property
def encryption_keys(self):
Expand All @@ -102,7 +110,7 @@ def auto_create_keys(self):
auto_create_keys = settings.AUTO_CREATE_KEYS
except AttributeError:
auto_create_keys = None
if 'test' in sys.argv:
if "test" in sys.argv:
if auto_create_keys is None:
auto_create_keys = True
return auto_create_keys
Expand Down
22 changes: 11 additions & 11 deletions django_crypto_fields/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

style = color_style()

AES = 'aes'
AES = "aes"
CIPHER_BUFFER_SIZE = 10
CIPHER_PREFIX = 'enc2:::'
ENCODING = 'utf-8'
HASH_ALGORITHM = 'sha256'
HASH_PREFIX = 'enc1:::'
CIPHER_PREFIX = "enc2:::"
ENCODING = "utf-8"
HASH_ALGORITHM = "sha256"
HASH_PREFIX = "enc1:::"
HASH_ROUNDS = 100000
LOCAL_MODE = 'local'
PRIVATE = 'private'
PUBLIC = 'public'
RSA = 'rsa'
LOCAL_MODE = "local"
PRIVATE = "private"
PUBLIC = "public"
RSA = "rsa"
RSA_KEY_SIZE = 2048
SALT = 'salt'
RESTRICTED_MODE = 'restricted'
SALT = "salt"
RESTRICTED_MODE = "restricted"

0 comments on commit e2b7c2e

Please sign in to comment.