File size: 27,794 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
import datetime
import enum
import logging
import typing

from dateutil.tz import tzutc
from packaging.version import Version

from redshift_connector.auth.aws_credentials_provider import AWSCredentialsProvider
from redshift_connector.credentials_holder import (
    ABCAWSCredentialsHolder,
    AWSDirectCredentialsHolder,
    AWSProfileCredentialsHolder,
    CredentialsHolder,
)
from redshift_connector.error import InterfaceError, ProgrammingError
from redshift_connector.idp_auth_helper import IdpAuthHelper
from redshift_connector.native_plugin_helper import NativeAuthPluginHelper
from redshift_connector.plugin.i_native_plugin import INativePlugin
from redshift_connector.plugin.i_plugin import IPlugin
from redshift_connector.plugin.saml_credentials_provider import SamlCredentialsProvider
from redshift_connector.redshift_property import RedshiftProperty

_logger: logging.Logger = logging.getLogger(__name__)


class IamHelper(IdpAuthHelper):
    class IAMAuthenticationType(enum.Enum):
        """
        Defines authentication types supported by redshift-connector
        """

        NONE = enum.auto()
        PROFILE = enum.auto()
        IAM_KEYS_WITH_SESSION = enum.auto()
        IAM_KEYS = enum.auto()
        PLUGIN = enum.auto()

    class GetClusterCredentialsAPIType(enum.Enum):
        """
        Defines supported Python SDK methods used for Redshift credential retrieval
        """

        # https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/redshift-serverless/client/get_credentials.html#
        SERVERLESS_V1 = "get_credentials()"
        # https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/redshift/client/get_cluster_credentials.html
        IAM_V1 = "get_cluster_credentials()"
        # https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/redshift/client/get_cluster_credentials_with_iam.html#
        IAM_V2 = "get_cluster_credentials_with_iam()"

        @staticmethod
        def can_support_v2(provider_type: "IamHelper.IAMAuthenticationType") -> bool:
            """
            Determines if user provided connection options and boto3 version support group federation.
            """
            return (
                provider_type
                in (
                    IamHelper.IAMAuthenticationType.PROFILE,
                    IamHelper.IAMAuthenticationType.IAM_KEYS,
                    IamHelper.IAMAuthenticationType.IAM_KEYS_WITH_SESSION,
                    IamHelper.IAMAuthenticationType.PLUGIN,
                )
            ) and IdpAuthHelper.get_pkg_version("boto3") >= Version("1.24.5")

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

    @staticmethod
    def get_cluster_credentials_api_type(
        info: RedshiftProperty, provider_type: "IamHelper.IAMAuthenticationType"
    ) -> GetClusterCredentialsAPIType:
        """
        Returns an enum representing the Python SDK method to use for getting temporary IAM credentials.
        """
        _logger.debug("Determining which Redshift API to use for retrieving temporary Redshift instance credentials")
        FAILED_TO_USE_V2_API_ERROR_MSG: str = (
            "Environment does not meet requirements to use {} API. "
            "This could be due to the connection properties provided or the version of boto3 in use. "
            "Please try updating the boto3 version or consider setting group_federation connection parameter to False."
        )

        if not info._is_serverless:
            _logger.debug("Redshift provisioned")
            if not info.group_federation:
                _logger.debug("Provisioned cluster GetClusterCredentialsAPIType.IAM_V1")
                return IamHelper.GetClusterCredentialsAPIType.IAM_V1
            elif IamHelper.GetClusterCredentialsAPIType.can_support_v2(provider_type):
                _logger.debug("Provisioned cluster GetClusterCredentialsAPIType.IAM_V2")
                return IamHelper.GetClusterCredentialsAPIType.IAM_V2
            else:
                raise InterfaceError(FAILED_TO_USE_V2_API_ERROR_MSG.format("GetClusterCredentials V2 API"))
        elif not info.group_federation:
            _logger.debug("Serverless cluster GetClusterCredentialsAPIType.SERVERLESS_V1")
            return IamHelper.GetClusterCredentialsAPIType.SERVERLESS_V1
        elif IamHelper.GetClusterCredentialsAPIType.can_support_v2(provider_type):
            if info.is_cname:
                raise InterfaceError("Custom cluster names are not supported for Redshift Serverless")
            else:
                _logger.debug("Serverless cluster GetClusterCredentialsAPIType.IAM_V2")
                return IamHelper.GetClusterCredentialsAPIType.IAM_V2
        else:
            raise InterfaceError(FAILED_TO_USE_V2_API_ERROR_MSG.format("GetClusterCredentials V2 API"))

    @staticmethod
    def set_iam_properties(info: RedshiftProperty) -> RedshiftProperty:
        """
        Helper function to handle connection properties and ensure required parameters are specified.
        Parameters
        """
        _logger.debug("IamHelper.set_iam_properties")
        provider_type: IamHelper.IAMAuthenticationType = IamHelper.IAMAuthenticationType.NONE
        info.set_is_cname()
        # set properties present for both IAM, Native authentication
        IamHelper.set_auth_properties(info)

        if info._is_serverless and info.iam:
            if IdpAuthHelper.get_pkg_version("boto3") < Version("1.24.11"):
                raise ModuleNotFoundError(
                    "boto3 >= 1.24.11 required for authentication with Amazon Redshift serverless. "
                    "Please upgrade the installed version of boto3 to use this functionality."
                )

        if info.iam and info.is_cname:
            if IdpAuthHelper.get_pkg_version("boto3") < Version("1.26.157"):
                _logger.debug(
                    "boto3 >= 1.26.157 required for authentication with Amazon Redshift using custom domain name. "
                    "Please upgrade the installed version of boto3 to use this functionality."
                )

        # consider overridden connection parameters
        if info.is_serverless_host:
            _logger.debug("Redshift Serverless host detected")
            if not info.serverless_acct_id:
                info.set_serverless_acct_id()
            if not info.serverless_work_group:
                info.set_serverless_work_group_from_host()
        if not info.region:
            info.set_region_from_host()

        if info.iam is True:
            if info.region is None:
                _logger.debug("Setting region via DNS lookup as region was not provided in connection parameters")
                info.set_region_from_endpoint_lookup()

            if info.cluster_identifier is None and not info._is_serverless and not info.is_cname:
                raise InterfaceError(
                    "Invalid connection property setting. cluster_identifier must be provided when IAM is enabled"
                )
            IamHelper.set_iam_credentials(info)
        # Check for Browser based OAuth Native authentication
        NativeAuthPluginHelper.set_native_auth_plugin_properties(info)
        return info

    @staticmethod
    def set_iam_credentials(info: RedshiftProperty) -> None:
        """
        Helper function to create the appropriate credential providers.
        """
        _logger.debug("IamHelper.set_iam_credentials")
        klass: typing.Optional[IPlugin] = None
        provider: typing.Union[IPlugin, AWSCredentialsProvider]

        if info.credentials_provider is not None:
            _logger.debug("IdP plugin will be used for authentication")
            provider = IdpAuthHelper.load_credentials_provider(info)

        else:  # indicates AWS Credentials will be used
            _logger.debug("AWS Credentials provider will be used for authentication")
            provider = AWSCredentialsProvider()
            provider.add_parameter(info)

        if isinstance(provider, SamlCredentialsProvider):
            _logger.debug("SAML based credential provider identified")
            credentials: CredentialsHolder = provider.get_credentials()
            metadata: CredentialsHolder.IamMetadata = credentials.get_metadata()
            if metadata is not None:
                _logger.debug("Using SAML metadata to set connection properties")
                auto_create: bool = metadata.get_auto_create()
                db_user: typing.Optional[str] = metadata.get_db_user()
                saml_db_user: typing.Optional[str] = metadata.get_saml_db_user()
                profile_db_user: typing.Optional[str] = metadata.get_profile_db_user()
                db_groups: typing.List[str] = metadata.get_db_groups()
                force_lowercase: bool = metadata.get_force_lowercase()
                allow_db_user_override: bool = metadata.get_allow_db_user_override()
                if auto_create is True:
                    _logger.debug("setting auto_create %s", auto_create)
                    info.put("auto_create", auto_create)

                if force_lowercase is True:
                    _logger.debug("setting force_lowercase %s", force_lowercase)
                    info.put("force_lowercase", force_lowercase)

                if allow_db_user_override is True:
                    _logger.debug("allow_db_user_override enabled")
                    if saml_db_user is not None:
                        _logger.debug("setting db_user to saml_db_user %s", saml_db_user)
                        info.put("db_user", saml_db_user)
                    elif db_user is not None:
                        _logger.debug("setting db_user to db_user %s", db_user)
                        info.put("db_user", db_user)
                    elif profile_db_user is not None:
                        _logger.debug("setting db_user to profile_db_user %s", profile_db_user)
                        info.put("db_user", profile_db_user)
                else:
                    if db_user is not None:
                        _logger.debug("setting db_user to db_user %s", db_user)
                        info.put("db_user", db_user)
                    elif profile_db_user is not None:
                        _logger.debug("setting db_user to profile_db_user %s", profile_db_user)
                        info.put("db_user", profile_db_user)
                    elif saml_db_user is not None:
                        _logger.debug("setting db_user to saml_db_user %s", saml_db_user)
                        info.put("db_user", saml_db_user)

                if (len(info.db_groups) == 0) and (len(db_groups) > 0):
                    if force_lowercase:
                        _logger.debug("setting db_groups after cast to lowercase")
                        info.db_groups = [group.lower() for group in db_groups]
                    else:
                        _logger.debug("setting db_groups")
                        info.db_groups = db_groups

        if not isinstance(provider, INativePlugin):
            # If the Redshift instance has been identified as using a custom domain name, the hostname must
            # be determined using the redshift client from boto3 API
            if info.is_cname is True and not info.is_serverless:
                IamHelper.set_cluster_identifier(provider, info)

            # Redshift database credentials  will be determined using the redshift client from boto3 API
            IamHelper.set_cluster_credentials(provider, info)

            # Redshift instance host and port must be retrieved
            IamHelper.set_cluster_host_and_port(provider, info)

    @staticmethod
    def get_credentials_cache_key(info: RedshiftProperty, cred_provider: typing.Union[IPlugin, AWSCredentialsProvider]):
        db_groups: str = ""

        if len(info.db_groups) > 0:
            info.put("db_groups", sorted(info.db_groups))
            db_groups = ",".join(info.db_groups)

        cred_key: str = ""

        if cred_provider:
            cred_key = str(cred_provider.get_cache_key())

        return ";".join(
            filter(
                None,
                (
                    cred_key,
                    typing.cast(str, info.db_user if info.db_user else info.user_name),
                    info.db_name,
                    db_groups,
                    typing.cast(str, info.serverless_acct_id if info._is_serverless else info.cluster_identifier),
                    typing.cast(
                        str, info.serverless_work_group if info._is_serverless and info.serverless_work_group else ""
                    ),
                    str(info.auto_create),
                    str(info.duration),
                    # v2 api parameters
                    info.preferred_role,
                    info.web_identity_token,
                    info.role_arn,
                    info.role_session_name,
                    # providers
                    info.profile,
                    info.access_key_id,
                    info.secret_access_key,
                    info.session_token,
                ),
            )
        )

    @staticmethod
    def get_authentication_type(
        provider: typing.Union[IPlugin, AWSCredentialsProvider]
    ) -> "IamHelper.IAMAuthenticationType":
        """
        Returns an enum representing the type of authentication the user is requesting based on connection parameters.
        """
        _logger.debug("IamHelper.get_authentication_type")
        provider_type: IamHelper.IAMAuthenticationType = IamHelper.IAMAuthenticationType.NONE
        if isinstance(provider, IPlugin):
            provider_type = IamHelper.IAMAuthenticationType.PLUGIN
        elif isinstance(provider, AWSCredentialsProvider):
            if provider.profile is not None:
                provider_type = IamHelper.IAMAuthenticationType.PROFILE
            elif provider.session_token is not None:
                provider_type = IamHelper.IAMAuthenticationType.IAM_KEYS_WITH_SESSION
            else:
                provider_type = IamHelper.IAMAuthenticationType.IAM_KEYS
        _logger.debug("Inferred authentication type %s from connection parameters", provider_type)

        return provider_type

    @staticmethod
    def get_boto3_redshift_client(cred_provider: typing.Union[IPlugin, AWSCredentialsProvider], info: RedshiftProperty):
        """
        Returns a boto3 client configured for Amazon Redshift using AWS credentials provided by user, system, or IdP.
        """
        _logger.debug("IamHelper.set_cluster_credentials")
        import boto3  # type: ignore
        import botocore  # type: ignore

        session_args: typing.Dict[str, str] = {
            "service_name": "redshift-serverless" if info._is_serverless else "redshift"
        }
        for opt_key, opt_val in (("region_name", info.region), ("endpoint_url", info.endpoint_url)):
            if opt_val is not None:
                session_args[opt_key] = opt_val

        try:
            credentials_holder: typing.Union[
                CredentialsHolder, ABCAWSCredentialsHolder
            ] = cred_provider.get_credentials()  # type: ignore
            session_credentials: typing.Dict[str, str] = credentials_holder.get_session_credentials()

            _logger.debug("boto3.client(service_name=%s) being used for IAM auth", session_args["service_name"])

            # if AWS credentials were used to create a boto3.Session object, use it
            if credentials_holder.has_associated_session:
                _logger.debug("Using cached boto3 session")
                cached_session: boto3.Session = typing.cast(
                    ABCAWSCredentialsHolder, credentials_holder
                ).get_boto_session()

                client = cached_session.client(**session_args)

            else:
                client = boto3.client(**{**session_credentials, **session_args})
            return client
        except botocore.exceptions.ClientError as e:
            _logger.debug("ClientError when establishing boto3 client: %s", e)
            raise e
        except Exception as e:
            _logger.debug("Other Exception when establishing boto3 client: %s", e)
            raise e

    @staticmethod
    def set_cluster_identifier(
        cred_provider: typing.Union[IPlugin, AWSCredentialsProvider], info: RedshiftProperty
    ) -> None:
        """
        Retrieves the hostname of a Redshift instance using custom domain name using boto3 API
        """
        import boto3  # type: ignore
        import botocore  # type: ignore

        client = IamHelper.get_boto3_redshift_client(cred_provider, info)

        try:
            _logger.debug("Redshift custom domain name in use. Determining cluster identifier.")
            response = client.describe_custom_domain_associations(CustomDomainName=info.host)
            cluster_identifier: str = response["Associations"][0]["CertificateAssociations"][0]["ClusterIdentifier"]
            _logger.debug("Retrieved cluster_identifier=%s", cluster_identifier)
            info.put(key="cluster_identifier", value=cluster_identifier)
        except Exception as e:
            if info.cluster_identifier is None or info.cluster_identifier == "":
                _logger.debug(
                    "Other Exception when requesting cluster identifier for Redshift with custom domain: %s", e
                )
                raise e
            else:
                _logger.debug(
                    "User provided cluster_identifier. Assuming cluster is using NLB/custom domain name. Using cluster_identifier"
                )

    @staticmethod
    def set_cluster_host_and_port(
        cred_provider: typing.Union[IPlugin, AWSCredentialsProvider], info: RedshiftProperty
    ) -> None:
        """
        Sets RedshiftProperty attributes for host and port using user configured connection properties and AWS SDK API calls.
        """
        import boto3  # type: ignore
        import botocore  # type: ignore

        try:
            # we must fetch the Redshift instance host and port name if either are unspecified by the user
            if info.host is None or info.host == "" or info.port is None or info.port == "":
                _logger.debug("retrieving Redshift instance host and port from boto3 redshift client")
                response: dict
                client = IamHelper.get_boto3_redshift_client(cred_provider, info)

                if info._is_serverless:
                    if not info.serverless_work_group:
                        raise InterfaceError("Serverless workgroup is not set.")
                    response = client.get_workgroup(workgroupName=info.serverless_work_group)
                    info.put("host", response["workgroup"]["endpoint"]["address"])
                    info.put("port", response["workgroup"]["endpoint"]["port"])
                else:
                    response = client.describe_clusters(ClusterIdentifier=info.cluster_identifier)
                    info.put("host", response["Clusters"][0]["Endpoint"]["Address"])
                    info.put("port", response["Clusters"][0]["Endpoint"]["Port"])
            _logger.debug("host=%s port=%s", info.host, info.port)
        except botocore.exceptions.ClientError as e:
            _logger.debug("ClientError when requesting cluster identifier for Redshift with custom domain: %s", e)
            raise e
        except Exception as e:
            _logger.debug("Other Exception when requesting cluster identifier for Redshift with custom domain: %s", e)
            raise e

    @staticmethod
    def set_cluster_credentials(
        cred_provider: typing.Union[IPlugin, AWSCredentialsProvider], info: RedshiftProperty
    ) -> None:
        """
        Calls the AWS SDK methods to return temporary credentials.
        The expiration date is returned as the local time set by the client machines OS.
        """
        import boto3  # type: ignore
        import botocore  # type: ignore
        from botocore.exceptions import ClientError

        client = IamHelper.get_boto3_redshift_client(cred_provider, info)
        cred: typing.Optional[typing.Dict[str, typing.Union[str, datetime.datetime]]] = None

        if info.iam_disable_cache is False:
            _logger.debug("iam_disable_cache=False")
            # temporary credentials are cached by redshift_connector and will be used if they have not expired
            cache_key: str = IamHelper.get_credentials_cache_key(info, cred_provider)
            cred = IamHelper.credentials_cache.get(cache_key, None)

            _logger.debug(
                "Searching credential cache for temporary AWS credentials. Found: %s Expiration: %s",
                bool(cache_key in IamHelper.credentials_cache),
                cred["Expiration"] if cred is not None else "N/A",
            )

        if cred is None or typing.cast(datetime.datetime, cred["Expiration"]) < datetime.datetime.now(tz=tzutc()):
            # retries will occur by default ref:
            # https://boto3.amazonaws.com/v1/documentation/api/latest/guide/retries.html#legacy-retry-mode
            _logger.debug("Credentials expired or not found...requesting from boto")
            provider_type: IamHelper.IAMAuthenticationType = IamHelper.get_authentication_type(cred_provider)
            get_creds_api_version: IamHelper.GetClusterCredentialsAPIType = IamHelper.get_cluster_credentials_api_type(
                info, provider_type
            )
            _logger.debug("boto3 get_credentials api version: %s will be used", get_creds_api_version.value)

            if get_creds_api_version == IamHelper.GetClusterCredentialsAPIType.SERVERLESS_V1:
                # https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/redshift-serverless/client/get_credentials.html#
                get_cred_args: typing.Dict[str, str] = {"dbName": info.db_name}
                # if a connection parameter for serverless workgroup is provided it will
                # be preferred over providing the CustomDomainName. The reason for this
                # is backwards compatibility with the following cases:
                # 0/ Serverless with NLB
                # 1/ Serverless with Custom Domain Name
                # Providing the CustomDomainName parameter to getCredentials will lead to
                # failure if the custom domain name is not registered with Redshift. Hence,
                # the ordering of these conditions is important.
                if info.serverless_work_group:
                    get_cred_args["workgroupName"] = info.serverless_work_group
                elif info.is_cname:
                    get_cred_args["customDomainName"] = info.host
                _logger.debug("Calling get_credentials with parameters %s", get_cred_args)
                cred = typing.cast(
                    typing.Dict[str, typing.Union[str, datetime.datetime]],
                    client.get_credentials(**get_cred_args),
                )
                # re-map expiration for compatibility with redshift credential response
                cred["Expiration"] = cred["expiration"]
                del cred["expiration"]
            elif get_creds_api_version == IamHelper.GetClusterCredentialsAPIType.IAM_V2:
                # https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/redshift/client/get_cluster_credentials_with_iam.html#
                request_params = {
                    "DbName": info.db_name,
                    "DurationSeconds": info.duration,
                }

                if info.is_cname:
                    request_params["CustomDomainName"] = info.host
                else:
                    request_params["ClusterIdentifier"] = info.cluster_identifier
                _logger.debug("Calling get_cluster_credentials_with_iam with parameters %s", request_params)

                try:
                    cred = typing.cast(
                        typing.Dict[str, typing.Union[str, datetime.datetime]],
                        client.get_cluster_credentials_with_iam(**request_params),
                    )
                except Exception as e:
                    if info.is_cname:
                        _logger.debug(
                            "Failed to get_cluster_credentials_with_iam. Assuming cluster incorrectly classified as cname, retrying..."
                        )
                        del request_params["CustomDomainName"]
                        request_params["ClusterIdentifier"] = info.cluster_identifier

                        _logger.debug(
                            "Retrying calling get_cluster_credentials_with_iam with parameters %s", request_params
                        )

                        cred = typing.cast(
                            typing.Dict[str, typing.Union[str, datetime.datetime]],
                            client.get_cluster_credentials_with_iam(**request_params),
                        )
                    else:
                        raise e

            else:
                if info.db_user is None or info.db_user == "":
                    raise InterfaceError("Connection parameter db_user must be specified when using IAM authentication")
                # https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/redshift/client/get_cluster_credentials.html
                request_params = {
                    "DbUser": info.db_user,
                    "DbName": info.db_name,
                    "DbGroups": info.db_groups,
                    "AutoCreate": info.auto_create,
                }

                if info.is_cname:
                    request_params["CustomDomainName"] = info.host
                else:
                    request_params["ClusterIdentifier"] = info.cluster_identifier

                _logger.debug("Calling get_cluster_credentials with parameters %s", request_params)

                try:
                    cred = typing.cast(
                        typing.Dict[str, typing.Union[str, datetime.datetime]],
                        client.get_cluster_credentials(**request_params),
                    )
                except Exception as e:
                    if info.is_cname:
                        _logger.debug(
                            "Failed to get_cluster_credentials. Assuming cluster incorrectly classified as cname, retrying..."
                        )
                        del request_params["CustomDomainName"]
                        request_params["ClusterIdentifier"] = info.cluster_identifier

                        _logger.debug("Retrying calling get_cluster_credentials with parameters %s", request_params)

                        cred = typing.cast(
                            typing.Dict[str, typing.Union[str, datetime.datetime]],
                            client.get_cluster_credentials(**request_params),
                        )

                    else:
                        raise e

            if info.iam_disable_cache is False:
                IamHelper.credentials_cache[cache_key] = typing.cast(
                    typing.Dict[str, typing.Union[str, datetime.datetime]], cred
                )
        # redshift-serverless api json response payload slightly differs
        if info._is_serverless:
            info.put("user_name", typing.cast(str, cred["dbUser"]))
            info.put("password", typing.cast(str, cred["dbPassword"]))
        else:
            info.put("user_name", typing.cast(str, cred["DbUser"]))
            info.put("password", typing.cast(str, cred["DbPassword"]))

        _logger.debug("Using temporary aws credentials with expiration: %s", cred.get("Expiration"))