Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/functional/rds/test_generate_db_auth_token.py
1567 views
1
# Copyright 2016 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.
13
import datetime
14
15
from dateutil.tz import tzutc
16
from botocore.compat import urlparse, parse_qs
17
18
from awscli.testutils import mock, BaseAWSCommandParamsTest
19
20
21
class TestGenerateDBAuthToken(BaseAWSCommandParamsTest):
22
23
prefix = 'rds generate-db-auth-token'
24
25
def _urlparse(self, url):
26
if isinstance(url, bytes):
27
# Not really necessary, but it helps to reduce noise on Python 2.x
28
url = url.decode('utf8')
29
return urlparse(url)
30
31
def assert_url_equal(self, url1, url2):
32
parts1 = self._urlparse(url1)
33
parts2 = self._urlparse(url2)
34
35
# Because the query string ordering isn't relevant, we have to parse
36
# every single part manually and then handle the query string.
37
self.assertEqual(parts1.scheme, parts2.scheme)
38
self.assertEqual(parts1.netloc, parts2.netloc)
39
self.assertEqual(parts1.path, parts2.path)
40
self.assertEqual(parts1.params, parts2.params)
41
self.assertEqual(parts1.fragment, parts2.fragment)
42
self.assertEqual(parts1.username, parts2.username)
43
self.assertEqual(parts1.password, parts2.password)
44
self.assertEqual(parts1.hostname, parts2.hostname)
45
self.assertEqual(parts1.port, parts2.port)
46
self.assertEqual(parse_qs(parts1.query), parse_qs(parts2.query))
47
48
def test_generate_simple_token(self):
49
command = self.prefix + ' --hostname host.us-east-1.amazonaws.com'
50
command += ' --port 3306 --username mySQLUser'
51
clock = datetime.datetime(2016, 11, 7, 17, 39, 33, tzinfo=tzutc())
52
53
with mock.patch('datetime.datetime') as dt:
54
dt.now.return_value = clock
55
stdout, _, _ = self.run_cmd(command, expected_rc=0)
56
57
expected = (
58
'host.us-east-1.amazonaws.com:3306/?DBUser=mySQLUser&'
59
'Action=connect&X-Amz-Credential=access_key%2F20161107%2Fus-east-1'
60
'%2Frds-db%2Faws4_request&X-Amz-Expires=900&X-Amz-Date=20161107T173'
61
'933Z&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-SignedHeaders=host&'
62
'X-Amz-Signature=87ab58107ef49f1c311a412f98b7f976b0b5152ffb559f0d'
63
'36c6c9a0c5e0e362'
64
)
65
66
self.assert_url_equal(
67
'https://' + stdout.strip('\n'), 'https://' + expected)
68
69