mirror of https://github.com/caronc/apprise
more cryptography availability handling
parent
6dc34fd23a
commit
d577fb81d1
|
@ -268,6 +268,11 @@ class ApprisePEMController:
|
||||||
Generates a set of keys based on name configured.
|
Generates a set of keys based on name configured.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if not PEM_SUPPORT:
|
||||||
|
msg = 'PEM Support unavailable; install cryptography library'
|
||||||
|
logger.warning(msg)
|
||||||
|
raise ApprisePEMException(msg)
|
||||||
|
|
||||||
if self._pub_keyfile or not self.path:
|
if self._pub_keyfile or not self.path:
|
||||||
logger.trace(
|
logger.trace(
|
||||||
'PEM keygen disabled, reason=%s',
|
'PEM keygen disabled, reason=%s',
|
||||||
|
@ -514,6 +519,11 @@ class ApprisePEMController:
|
||||||
if isinstance(message, str):
|
if isinstance(message, str):
|
||||||
message = message.encode('utf-8')
|
message = message.encode('utf-8')
|
||||||
|
|
||||||
|
if not PEM_SUPPORT:
|
||||||
|
msg = 'PEM Support unavailable; install cryptography library'
|
||||||
|
logger.warning(msg)
|
||||||
|
raise ApprisePEMException(msg)
|
||||||
|
|
||||||
# 1. Generate ephemeral EC private/Public key
|
# 1. Generate ephemeral EC private/Public key
|
||||||
ephemeral_private_key = \
|
ephemeral_private_key = \
|
||||||
ec.generate_private_key(ec.SECP256R1(), default_backend())
|
ec.generate_private_key(ec.SECP256R1(), default_backend())
|
||||||
|
@ -584,6 +594,11 @@ class ApprisePEMController:
|
||||||
key if none provided). Message can be str or bytes.
|
key if none provided). Message can be str or bytes.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if not PEM_SUPPORT:
|
||||||
|
msg = 'PEM Support unavailable; install cryptography library'
|
||||||
|
logger.warning(msg)
|
||||||
|
raise ApprisePEMException(msg)
|
||||||
|
|
||||||
# 1. Handle string vs bytes input
|
# 1. Handle string vs bytes input
|
||||||
if isinstance(message, str):
|
if isinstance(message, str):
|
||||||
message = message.encode('utf-8')
|
message = message.encode('utf-8')
|
||||||
|
@ -651,6 +666,11 @@ class ApprisePEMController:
|
||||||
Payload is the base64-encoded JSON from encrypt().
|
Payload is the base64-encoded JSON from encrypt().
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if not PEM_SUPPORT:
|
||||||
|
msg = 'PEM Support unavailable; install cryptography library'
|
||||||
|
logger.warning(msg)
|
||||||
|
raise ApprisePEMException(msg)
|
||||||
|
|
||||||
# 1. Parse input
|
# 1. Parse input
|
||||||
if isinstance(encrypted_payload, str):
|
if isinstance(encrypted_payload, str):
|
||||||
payload_bytes = base64.b64decode(encrypted_payload.encode('utf-8'))
|
payload_bytes = base64.b64decode(encrypted_payload.encode('utf-8'))
|
||||||
|
|
|
@ -28,6 +28,8 @@
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
|
import pytest
|
||||||
|
|
||||||
from apprise import AppriseAsset
|
from apprise import AppriseAsset
|
||||||
from apprise import PersistentStoreMode
|
from apprise import PersistentStoreMode
|
||||||
|
@ -40,6 +42,8 @@ logging.disable(logging.CRITICAL)
|
||||||
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), 'var')
|
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), 'var')
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(
|
||||||
|
'cryptography' not in sys.modules, reason="Requires cryptography")
|
||||||
def test_utils_pem_general(tmpdir):
|
def test_utils_pem_general(tmpdir):
|
||||||
"""
|
"""
|
||||||
Utils:PEM
|
Utils:PEM
|
||||||
|
@ -97,3 +101,89 @@ def test_utils_pem_general(tmpdir):
|
||||||
content = pem_c.encrypt("message")
|
content = pem_c.encrypt("message")
|
||||||
assert isinstance(content, str)
|
assert isinstance(content, str)
|
||||||
assert pem_c.decrypt(content) == "message"
|
assert pem_c.decrypt(content) == "message"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(
|
||||||
|
'cryptography' in sys.modules,
|
||||||
|
reason="Requires that cryptography NOT be installed")
|
||||||
|
def test_utils_pem_general_without_c(tmpdir):
|
||||||
|
"""
|
||||||
|
Utils:PEM Without cryptography
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
tmpdir0 = tmpdir.mkdir('tmp00')
|
||||||
|
|
||||||
|
# Currently no files here
|
||||||
|
assert os.listdir(str(tmpdir0)) == []
|
||||||
|
|
||||||
|
asset = AppriseAsset(
|
||||||
|
storage_mode=PersistentStoreMode.MEMORY,
|
||||||
|
storage_path=str(tmpdir0),
|
||||||
|
pem_autogen=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Create a PEM Controller
|
||||||
|
pem_c = utils.pem.ApprisePEMController(path=None, asset=asset)
|
||||||
|
|
||||||
|
# cryptography library missing poses issues with library useage
|
||||||
|
with pytest.raises(utils.pem.ApprisePEMException):
|
||||||
|
pem_c.public_keyfile()
|
||||||
|
|
||||||
|
with pytest.raises(utils.pem.ApprisePEMException):
|
||||||
|
pem_c.public_key()
|
||||||
|
|
||||||
|
with pytest.raises(utils.pem.ApprisePEMException):
|
||||||
|
pem_c.x962_str
|
||||||
|
|
||||||
|
with pytest.raises(utils.pem.ApprisePEMException):
|
||||||
|
pem_c.encrypt("message")
|
||||||
|
|
||||||
|
with pytest.raises(utils.pem.ApprisePEMException):
|
||||||
|
pem_c.keygen()
|
||||||
|
|
||||||
|
asset = AppriseAsset(
|
||||||
|
storage_mode=PersistentStoreMode.FLUSH,
|
||||||
|
storage_path=str(tmpdir0),
|
||||||
|
pem_autogen=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
# No new files
|
||||||
|
assert os.listdir(str(tmpdir0)) == []
|
||||||
|
|
||||||
|
# Our asset is now write mode, so we will be able to generate a key
|
||||||
|
pem_c = utils.pem.ApprisePEMController(path=str(tmpdir0), asset=asset)
|
||||||
|
# Nothing to lookup
|
||||||
|
with pytest.raises(utils.pem.ApprisePEMException):
|
||||||
|
pem_c.public_keyfile()
|
||||||
|
|
||||||
|
with pytest.raises(utils.pem.ApprisePEMException):
|
||||||
|
pem_c.public_key()
|
||||||
|
|
||||||
|
with pytest.raises(utils.pem.ApprisePEMException):
|
||||||
|
pem_c.x962_str
|
||||||
|
|
||||||
|
with pytest.raises(utils.pem.ApprisePEMException):
|
||||||
|
pem_c.encrypt("message")
|
||||||
|
|
||||||
|
# Keys can not be generated in memory mode
|
||||||
|
with pytest.raises(utils.pem.ApprisePEMException):
|
||||||
|
pem_c.keygen()
|
||||||
|
|
||||||
|
# No files loaded
|
||||||
|
assert os.listdir(str(tmpdir0)) == []
|
||||||
|
|
||||||
|
with pytest.raises(utils.pem.ApprisePEMException):
|
||||||
|
pem_c.public_keyfile()
|
||||||
|
|
||||||
|
with pytest.raises(utils.pem.ApprisePEMException):
|
||||||
|
pem_c.public_key()
|
||||||
|
|
||||||
|
with pytest.raises(utils.pem.ApprisePEMException):
|
||||||
|
pem_c.x962_str
|
||||||
|
|
||||||
|
with pytest.raises(utils.pem.ApprisePEMException):
|
||||||
|
pem_c.encrypt("message")
|
||||||
|
|
||||||
|
with pytest.raises(utils.pem.ApprisePEMException):
|
||||||
|
pem_c.decrypt("abcd==")
|
||||||
|
|
Loading…
Reference in New Issue