Skip to content

Commit

Permalink
Merge pull request #18 from level12/17-optional-keg-dependency
Browse files Browse the repository at this point in the history
Make keg/keg-elements optional dependencies
  • Loading branch information
bladams committed Oct 17, 2019
2 parents bd6c2c0 + 3e66d67 commit e33139a
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 9 deletions.
5 changes: 4 additions & 1 deletion keg_storage/__init__.py
@@ -1,2 +1,5 @@
from keg_storage.plugin import Storage # noqa
from importlib.util import find_spec as _find_spec

if _find_spec('keg'):
from keg_storage.plugin import Storage # noqa
from keg_storage.backends import * # noqa
24 changes: 21 additions & 3 deletions keg_storage/backends/__init__.py
@@ -1,4 +1,22 @@
from importlib.util import find_spec

from .base import StorageBackend, FileNotFoundInStorageError, FileMode # noqa
from .s3 import S3Storage # noqa
from .sftp import SFTPStorage # noqa
from .azure import AzureStorage # noqa

__all__ = [
'StorageBackend',
'FileNotFoundInStorageError',
'FileMode',
'base',
]

if find_spec('boto3') is not None:
from .s3 import S3Storage # noqa
__all__.extend(['s3', 'S3Storage'])

if find_spec('paramiko') is not None:
from .sftp import SFTPStorage # noqa
__all__.extend(['sftp', 'SFTPStorage'])

if find_spec('azure') is not None:
from .azure import AzureStorage # noqa
__all__.extend(['azure', 'AzureStorage'])
14 changes: 13 additions & 1 deletion keg_storage/tests/test_utils.py
Expand Up @@ -2,13 +2,18 @@

import pytest
from mock import mock
import keg_elements.crypto as ke_crypto

try:
import keg_elements.crypto as ke_crypto
except ImportError:
ke_crypto = None

from keg_storage import utils

DEFAULT_PLAINTEXT = b'data' * 1024 * 1024


@pytest.mark.skipif(ke_crypto is None, reason='Only run with keg elements installed')
class TestReencrypt:
def mock_storage(self, old_ciphertext, new_ciphertext):
m_storage = mock.MagicMock()
Expand Down Expand Up @@ -81,3 +86,10 @@ def test_with_bad_key(self):

with pytest.raises(utils.DecryptionException):
utils.reencrypt(store, 'file', k1, k3)


@pytest.mark.skipif(ke_crypto is not None, reason='Only run with keg elements not installed')
def test_reencrypt():
with pytest.raises(utils.MissingDependencyException) as exc:
utils.reencrypt(None, '', '', '')
assert str(exc.value) == 'Keg Elements is required for crypto operations'
13 changes: 12 additions & 1 deletion keg_storage/utils.py
Expand Up @@ -2,7 +2,11 @@
import tempfile

from blazeutils.helpers import ensure_list
import keg_elements.crypto as ke_crypto

try:
import keg_elements.crypto as ke_crypto
except ImportError:
ke_crypto = None

DEFAULT_KEY_SIZE = 32

Expand All @@ -17,11 +21,18 @@ class EncryptionKeyException(Exception):
pass


class MissingDependencyException(Exception):
pass


def verify_key_length(key, expected=DEFAULT_KEY_SIZE):
return len(key) == expected


def reencrypt(storage, path, old_key, new_key):
if ke_crypto is None:
raise MissingDependencyException('Keg Elements is required for crypto operations')

old_key = ensure_list(old_key)

# We append the new key just in case the operation was restarted and we have already encrypted
Expand Down
5 changes: 4 additions & 1 deletion setup.py
Expand Up @@ -30,9 +30,9 @@
zip_safe=True,
version=version['VERSION'],
install_requires=[
'arrow',
'boto3',
'botocore',
'kegelements',
'humanize',
'BlazeUtils',
],
Expand All @@ -43,6 +43,9 @@
'azure': [
'azure',
],
'keg': [
'kegelements',
],
'test': [
'azure',
'paramiko',
Expand Down
22 changes: 20 additions & 2 deletions tox.ini
@@ -1,12 +1,12 @@
[tox]
envlist = py36,py37,flake8
envlist = py36,py37,flake8,nokeg

[testenv]
whitelist_externals = *
usedevelop = true
recreate = true
commands =
pip install ".[test]"
pip install ".[test,keg]"
py.test \
--tb native \
--strict \
Expand All @@ -16,6 +16,24 @@ commands =
--no-cov-on-fail \
--junit-xml=.ci/test-reports/{envname}.pytests.xml

[nokeg]
whitelist_externals = *
usedevelop = true
recreate = true
commands =
pip install ".[test]"
py.test \
--tb native \
--strict \
--cov keg_storage \
--cov-config .coveragerc \
--cov-report xml \
--no-cov-on-fail \
--junit-xml=.ci/test-reports/{envname}.pytests.xml \
--ignore=keg_storage/tests/test_cli \
--ignore=keg_storage/tests/test_lib


[testenv:flake8]
skip_install = true
usedevelop = false
Expand Down

0 comments on commit e33139a

Please sign in to comment.