more cryptography availability handling

pull/1323/head
Chris Caron 2025-05-24 22:11:18 -04:00
parent 6dc34fd23a
commit d577fb81d1
2 changed files with 110 additions and 0 deletions

View File

@ -268,6 +268,11 @@ class ApprisePEMController:
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:
logger.trace(
'PEM keygen disabled, reason=%s',
@ -514,6 +519,11 @@ class ApprisePEMController:
if isinstance(message, str):
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
ephemeral_private_key = \
ec.generate_private_key(ec.SECP256R1(), default_backend())
@ -584,6 +594,11 @@ class ApprisePEMController:
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
if isinstance(message, str):
message = message.encode('utf-8')
@ -651,6 +666,11 @@ class ApprisePEMController:
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
if isinstance(encrypted_payload, str):
payload_bytes = base64.b64decode(encrypted_payload.encode('utf-8'))

View File

@ -28,6 +28,8 @@
import logging
import os
import sys
import pytest
from apprise import AppriseAsset
from apprise import PersistentStoreMode
@ -40,6 +42,8 @@ logging.disable(logging.CRITICAL)
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):
"""
Utils:PEM
@ -97,3 +101,89 @@ def test_utils_pem_general(tmpdir):
content = pem_c.encrypt("message")
assert isinstance(content, str)
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==")