22import typing
33from enum import Enum
44
5+ from packaging .version import Version
6+
57from redshift_connector .error import InterfaceError , ProgrammingError
68from redshift_connector .plugin .i_plugin import IPlugin
79from 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 )
0 commit comments