Skip to content

Commit

Permalink
Made cryptography an optional dependency for pure-python deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveMcGrath committed Sep 24, 2024
1 parent f8ad3d1 commit 51f62e7
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 9 deletions.
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,16 @@ dependencies = [
"typing-extensions>=4.0.1",
"dataclasses>=0.8;python_version=='3.6'",
"requests-toolbelt>=1.0.0",
"cryptography>=43.0.1",
"gql>=3.5.0",
"graphql-core>=3.2.3",
]


[project.optional-dependencies]
pkcs12 = ["cryptography>=43.0.1"]
all = ["pytenable[pkcs12]"]


[project.urls]
Homepage = "https://pytenable.readthedocs.io"
Repository = "https://github.com/tenable/pytenable"
Expand Down Expand Up @@ -113,6 +117,7 @@ docstring-code-line-length = "dynamic"
[tool.uv]
dev-dependencies = [
"bpython>=0.24",
"mock>=5.1.0",
"pytest-cov>=4.1.0",
"pytest-datafiles>=3.0.0",
"pytest-vcr>=1.0.2",
Expand Down
17 changes: 15 additions & 2 deletions tenable/sc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@
from typing import Optional
from semver import VersionInfo
import tempfile
import cryptography.hazmat.primitives.serialization.pkcs12
from cryptography.hazmat.primitives import serialization
from tenable.errors import APIError, ConnectionError
from tenable.base.platform import APIPlatform
from .accept_risks import AcceptRiskAPI
Expand Down Expand Up @@ -78,6 +76,12 @@
from .system import SystemAPI
from .users import UserAPI

try:
import cryptography.hazmat.primitives.serialization.pkcs12
from cryptography.hazmat.primitives import serialization
except ImportError:
serialization = None


class TenableSC(APIPlatform): # noqa PLR0904
'''TenableSC API Wrapper
Expand Down Expand Up @@ -292,6 +296,15 @@ def _p12_auth(self, p12_cert, password):
"""
PKCS12 Certificate Authentication
"""
if not serialization:
raise ModuleNotFoundError(
(
'Cryptyography library is required for PKCS12 certificate usage. '
'You can either install it with manually or install pytenable with '
'"pytenable[pkcs12]" to install the optional dependencies.'
),
name='cryptography',
)
with open(p12_cert, 'rb') as fobj:
key, cert, _ = serialization.pkcs12.load_key_and_certificates(
fobj.read(), password.encode()
Expand Down
13 changes: 13 additions & 0 deletions tenable/sc/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ def _constructor(self, **kwargs):
kwargs['value'] = self._check('filter:value', kwargs['filter'][2], str)
del kwargs['filter']

#filters = kwargs.pop('filters', [])
#kwargs['filters'] = []
#for filter in filters:
# if isinstance(filter, tuple):
# kwargs['filters'].append({
# 'filterField': filter[0],
# 'filterOperator': filter[1],
# 'filterString': filter[2],
# })
# elif isinstance(filter, dict):
# kwargs['filters'].append(filter)


if 'sort_field' in kwargs:
# convert the snake_cased variant of the parameter to the camelCased
# variant that the API expects to see.
Expand Down
2 changes: 1 addition & 1 deletion tenable/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = '1.5.2'
version = '1.5.3'
version_info = tuple(int(d) for d in version.split("-")[0].split("."))
12 changes: 10 additions & 2 deletions tests/sc/test___init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
test file to test various scenarios in init.py
'''
import os

import sys
import pytest
from requests.models import Response

from requests.exceptions import ConnectionError as RequestsConnectionError
from tenable.errors import ConnectionError
from tenable.sc import TenableSC
Expand Down Expand Up @@ -84,3 +83,12 @@ def test_log_in(vcr):
with vcr.use_cassette('sc_login_5_20_0'):
tsc.login(access_key='access_key', secret_key='secret_key')
assert tsc._auth_mech == 'keys'


def test_pkcs12_import_error():
import tenable.sc
tenable.sc.serialization = None
with pytest.raises(ModuleNotFoundError):
sc = TenableSC(
url='http://something', p12_cert='something', password='something'
)
25 changes: 22 additions & 3 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 51f62e7

Please sign in to comment.