1+ # Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+ #
3+ # Licensed under the Apache License, Version 2.0 (the "License"). You
4+ # may not use this file except in compliance with the License. A copy of
5+ # the License is located at
6+ #
7+ # http://aws.amazon.com/apache2.0/
8+ #
9+ # or in the "license" file accompanying this file. This file is
10+ # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+ # ANY KIND, either express or implied. See the License for the specific
12+ # language governing permissions and limitations under the License.
113import datetime
214from datetime import timezone
315from unittest import mock
416
17+ import pytest
18+ from botocore .exceptions import ParamValidationError
19+
520import aiobotocore .credentials
621import aiobotocore .session
722import aiobotocore .signers
23+ from tests .boto_tests import assert_url_equal
24+
25+ DATE = datetime .datetime (2024 , 11 , 7 , 17 , 39 , 33 , tzinfo = timezone .utc )
826
927
1028async def test_signers_generate_db_auth_token (rds_client ):
@@ -31,3 +49,98 @@ async def test_signers_generate_db_auth_token(rds_client):
3149 assert result2 .startswith (
3250 'prod-instance.us-east-1.rds.amazonaws.com:3306/?AWSAccessKeyId=xxx&'
3351 )
52+
53+
54+ class TestDSQLGenerateDBAuthToken :
55+ @pytest .fixture (scope = "session" )
56+ def hostname (self ):
57+ return 'test.dsql.us-east-1.on.aws'
58+
59+ @pytest .fixture (scope = "session" )
60+ def action (self ):
61+ return 'DbConnect'
62+
63+ @pytest .fixture
64+ async def client (self , session ):
65+ async with session .create_client (
66+ 'dsql' ,
67+ region_name = 'us-east-1' ,
68+ aws_access_key_id = 'ACCESS_KEY' ,
69+ aws_secret_access_key = 'SECRET_KEY' ,
70+ aws_session_token = "SESSION_TOKEN" ,
71+ ) as client :
72+ yield client
73+
74+ async def test_dsql_generate_db_auth_token (
75+ self , client , hostname , action , time_machine
76+ ):
77+ time_machine .move_to (DATE , tick = False )
78+
79+ result = await aiobotocore .signers ._dsql_generate_db_auth_token (
80+ client , hostname , action
81+ )
82+
83+ expected_result = (
84+ 'test.dsql.us-east-1.on.aws/?Action=DbConnect'
85+ '&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential='
86+ 'ACCESS_KEY%2F20241107%2Fus-east-1%2Fdsql%2Faws4_request'
87+ '&X-Amz-Date=20241107T173933Z&X-Amz-Expires=900&X-Amz-SignedHeaders=host'
88+ '&X-Amz-Security-Token=SESSION_TOKEN&X-Amz-Signature='
89+ '57fe03e060348aaa21405c239bf02572bbc911076e94dcd65c12ae569dd8fcf4'
90+ )
91+
92+ # A scheme needs to be appended to the beginning or urlsplit may fail
93+ # on certain systems.
94+ assert_url_equal ('https://' + result , 'https://' + expected_result )
95+
96+ async def test_dsql_generate_db_connect_auth_token (
97+ self , client , hostname , time_machine
98+ ):
99+ time_machine .move_to (DATE , tick = False )
100+
101+ result = await aiobotocore .signers .dsql_generate_db_connect_auth_token (
102+ client , hostname
103+ )
104+
105+ expected_result = (
106+ 'test.dsql.us-east-1.on.aws/?Action=DbConnect'
107+ '&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential='
108+ 'ACCESS_KEY%2F20241107%2Fus-east-1%2Fdsql%2Faws4_request'
109+ '&X-Amz-Date=20241107T173933Z&X-Amz-Expires=900&X-Amz-SignedHeaders=host'
110+ '&X-Amz-Security-Token=SESSION_TOKEN&X-Amz-Signature='
111+ '57fe03e060348aaa21405c239bf02572bbc911076e94dcd65c12ae569dd8fcf4'
112+ )
113+
114+ # A scheme needs to be appended to the beginning or urlsplit may fail
115+ # on certain systems.
116+ assert_url_equal ('https://' + result , 'https://' + expected_result )
117+
118+ async def test_dsql_generate_db_connect_admin_auth_token (
119+ self , client , hostname , time_machine
120+ ):
121+ time_machine .move_to (DATE , tick = False )
122+
123+ result = await aiobotocore .signers .dsql_generate_db_connect_admin_auth_token (
124+ client , hostname
125+ )
126+
127+ expected_result = (
128+ 'test.dsql.us-east-1.on.aws/?Action=DbConnectAdmin'
129+ '&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential='
130+ 'ACCESS_KEY%2F20241107%2Fus-east-1%2Fdsql%2Faws4_request'
131+ '&X-Amz-Date=20241107T173933Z&X-Amz-Expires=900&X-Amz-SignedHeaders=host'
132+ '&X-Amz-Security-Token=SESSION_TOKEN&X-Amz-Signature='
133+ '5ac084bc7cabccc19a52a5d1b5c24b50d3ce143f43b659bd484c91aaf555e190'
134+ )
135+
136+ # A scheme needs to be appended to the beginning or urlsplit may fail
137+ # on certain systems.
138+ assert_url_equal ('https://' + result , 'https://' + expected_result )
139+
140+ async def test_dsql_generate_db_auth_token_invalid_action (
141+ self , client , hostname
142+ ):
143+ with pytest .raises (ParamValidationError ):
144+ await aiobotocore .signers ._dsql_generate_db_auth_token (
145+ client , hostname , "FooBar"
146+ )
0 commit comments