Skip to content

Commit e39d2b8

Browse files
committed
chore: add type verification for custom handlers
1 parent 27150f1 commit e39d2b8

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

aws_advanced_python_wrapper/aws_credentials_manager.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from typing import TYPE_CHECKING, Any, Callable, Optional
1919

2020
from boto3 import Session
21+
22+
from aws_advanced_python_wrapper.utils.messages import Messages
2123
from aws_advanced_python_wrapper.utils.properties import WrapperProperties
2224
if TYPE_CHECKING:
2325
from aws_advanced_python_wrapper.hostinfo import HostInfo
@@ -32,6 +34,8 @@ class AwsCredentialsManager:
3234

3335
@staticmethod
3436
def set_custom_handler(custom_handler: Callable[[HostInfo, Properties], Optional[Session]]) -> None:
37+
if not callable(custom_handler):
38+
raise TypeError("custom_handler must be callable")
3539
with AwsCredentialsManager._lock:
3640
AwsCredentialsManager._handler = custom_handler
3741

@@ -52,7 +56,10 @@ def get_session(host_info: HostInfo, props: Properties, region: str) -> Session:
5256

5357
# Initialize session outside of lock.
5458
session = handler(host_info, props) if handler else None
55-
59+
60+
if session is not None and not isinstance(session, Session):
61+
raise TypeError(Messages.get_formatted("AwsCredentialsManager.InvalidHandler", type(session).__name__))
62+
5663
if session is None:
5764
profile_name = WrapperProperties.AWS_PROFILE.get(props)
5865
session = Session(profile_name=profile_name, region_name=region) if profile_name else Session(region_name=region)

aws_advanced_python_wrapper/resources/aws_advanced_python_wrapper_messages.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ AdfsCredentialsProviderFactory.SignOnPagePostActionUrl=[AdfsCredentialsProviderF
2626
AdfsCredentialsProviderFactory.SignOnPagePostActionRequestFailed=[AdfsCredentialsProviderFactory] ADFS SignOn Page POST action failed with HTTP status '{}', reason phrase '{}', and response '{}'
2727
AdfsCredentialsProviderFactory.SignOnPageUrl=[AdfsCredentialsProviderFactory] ADFS SignOn URL: '{}'
2828

29+
AwsCredentialsManager.InvalidHandler=[AwsCredentialsManager] Custom credentials provider set via AwsCredentialsManager.set_custom_handler must return a boto3.Session or None, got '{}'.
30+
2931
AwsSdk.UnsupportedRegion=[AwsSdk] Unsupported AWS region {}. For supported regions please read https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html
3032

3133
AwsSecretsManagerPlugin.ConnectException=[AwsSecretsManagerPlugin] Error occurred while opening a connection: {}

0 commit comments

Comments
 (0)