Skip to content

Commit 7e4f81a

Browse files
committed
refactor: use importlib instead of pkg_resources
1 parent 61fc565 commit 7e4f81a

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

redshift_connector/iam_helper.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import logging
44
import typing
55

6-
import pkg_resources
76
from dateutil.tz import tzutc
87
from packaging.version import Version
98

@@ -58,7 +57,7 @@ def can_support_v2(provider_type: "IamHelper.IAMAuthenticationType") -> bool:
5857
IamHelper.IAMAuthenticationType.IAM_KEYS,
5958
IamHelper.IAMAuthenticationType.IAM_KEYS_WITH_SESSION,
6059
)
61-
) and Version(pkg_resources.get_distribution("boto3").version) >= Version("1.24.5")
60+
) and IdpAuthHelper.get_pkg_version("boto3") >= Version("1.24.5")
6261

6362
credentials_cache: typing.Dict[str, dict] = {}
6463

@@ -94,8 +93,8 @@ def set_iam_properties(info: RedshiftProperty) -> RedshiftProperty:
9493
IamHelper.set_auth_properties(info)
9594

9695
if info._is_serverless and info.iam:
97-
if Version(pkg_resources.get_distribution("boto3").version) < Version("1.24.11"):
98-
raise pkg_resources.VersionConflict(
96+
if IdpAuthHelper.get_pkg_version("boto3") < Version("1.24.11"):
97+
raise ModuleNotFoundError(
9998
"boto3 >= 1.24.11 required for authentication with Amazon Redshift serverless. "
10099
"Please upgrade the installed version of boto3 to use this functionality."
101100
)

redshift_connector/idp_auth_helper.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import typing
33
from enum import Enum
44

5+
from packaging.version import Version
6+
57
from redshift_connector.error import InterfaceError, ProgrammingError
68
from redshift_connector.plugin.i_plugin import IPlugin
79
from redshift_connector.redshift_property import RedshiftProperty
@@ -32,14 +34,26 @@ class IdpAuthHelper:
3234
SAML_PLUGIN: int = 1
3335
JWT_PLUGIN: int = 2
3436

37+
@staticmethod
38+
def get_pkg_version(module_name: str) -> Version:
39+
"""
40+
Returns a Version object pertaining to the module name provided.
41+
"""
42+
try:
43+
from importlib.metadata import version as version
44+
except ModuleNotFoundError: # if importlib is not present, fallback to pkg_resources
45+
import pkg_resources
46+
47+
return Version(pkg_resources.get_distribution(module_name).version)
48+
49+
return Version(version(module_name))
50+
3551
@staticmethod
3652
def set_auth_properties(info: RedshiftProperty):
3753
"""
3854
Helper function to handle IAM and Native Auth connection properties and ensure required parameters are specified.
3955
Parameters
4056
"""
41-
import pkg_resources
42-
from packaging.version import Version
4357

4458
if info is None:
4559
raise InterfaceError("Invalid connection property setting. info must be specified")
@@ -65,13 +79,13 @@ def set_auth_properties(info: RedshiftProperty):
6579
# "AWS credentials, Amazon Redshift authentication profile, or AWS profile"
6680
# )
6781
if info.iam is True:
68-
_logger.debug("boto3 version: {}".format(Version(pkg_resources.get_distribution("boto3").version)))
69-
_logger.debug("botocore version: {}".format(Version(pkg_resources.get_distribution("botocore").version)))
82+
_logger.debug("boto3 version: {}".format(IdpAuthHelper.get_pkg_version("boto3")))
83+
_logger.debug("botocore version: {}".format(IdpAuthHelper.get_pkg_version("botocore")))
7084

7185
# Check for IAM keys and AuthProfile first
7286
if info.auth_profile is not None:
73-
if Version(pkg_resources.get_distribution("boto3").version) < Version("1.17.111"):
74-
raise pkg_resources.VersionConflict(
87+
if IdpAuthHelper.get_pkg_version("boto3") < Version("1.17.111"):
88+
raise ModuleNotFoundError(
7589
"boto3 >= 1.17.111 required for authentication via Amazon Redshift authentication profile. "
7690
"Please upgrade the installed version of boto3 to use this functionality."
7791
)

test/manual/auth/test_aws_credentials.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_use_aws_credentials_default_profile():
3838
0) Generate credentials using instructions: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/getting-your-credentials.html
3939
1) In the connect method below, specify the connection parameters
4040
3) Specify the AWS IAM credentials in the variables above
41-
4) Update iam_helper.py to include correct min version. line `Version(pkg_resources.get_distribution("boto3").version) > Version("9.99.9999"):`
41+
4) Update iam_helper.py to include correct min version.
4242
5) Manually execute this test
4343
"""
4444

test/unit/test_iam_helper.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -781,13 +781,13 @@ def test_get_cluster_credentials_api_type_will_use_correct_api(conn_params, prov
781781
),
782782
)
783783
def test_set_iam_properties_raises_exception_when_insufficient_boto3_version(mocker, boto3_version):
784-
mock_boto3_dist_obj = MagicMock()
785-
mock_boto3_dist_obj.version = boto3_version
784+
from packaging.version import Version
786785

787-
mocker.patch("pkg_resources.get_distribution", return_value=mock_boto3_dist_obj)
788-
import pkg_resources
786+
mock_boto3_dist_obj = Version(boto3_version)
789787

790-
with pytest.raises(pkg_resources.VersionConflict) as excinfo:
788+
mocker.patch("redshift_connector.idp_auth_helper.IdpAuthHelper.get_pkg_version", return_value=mock_boto3_dist_obj)
789+
790+
with pytest.raises(ModuleNotFoundError) as excinfo:
791791
IamHelper.set_iam_properties(
792792
make_basic_redshift_property(
793793
**{"iam": True, "ssl": True, "auth_profile": "SomeTestProfile", "cluster_identifier": "my_cluster"}

0 commit comments

Comments
 (0)