Debian Codegraph @

keystoneclient/tests/unit/test_ec2utils.py

raw symbols SCIP
1 # Copyright 2012 OpenStack Foundation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 import testtools
16
17 from keystoneclient.contrib.ec2 import utils
18 from keystoneclient.tests.unit import client_fixtures
19
20
21 class Ec2SignerTest(testtools.TestCase):
22
23 def setUp(self):
24 super(Ec2SignerTest, self).setUp()
25 self.useFixture(client_fixtures.Deprecations())
26 self.access = '966afbde20b84200ae4e62e09acf46b2'
27 self.secret = '89cdf9e94e2643cab35b8b8ac5a51f83'
28 self.signer = utils.Ec2Signer(self.secret)
29
30 def test_v4_creds_header(self):
31 auth_str = 'AWS4-HMAC-SHA256 blah'
32 credentials = {'host': '127.0.0.1',
33 'verb': 'GET',
34 'path': '/v1/',
35 'params': {},
36 'headers': {'Authorization': auth_str}}
37 self.assertTrue(self.signer._v4_creds(credentials))
38
39 def test_v4_creds_param(self):
40 credentials = {'host': '127.0.0.1',
41 'verb': 'GET',
42 'path': '/v1/',
43 'params': {'X-Amz-Algorithm': 'AWS4-HMAC-SHA256'},
44 'headers': {}}
45 self.assertTrue(self.signer._v4_creds(credentials))
46
47 def test_v4_creds_false(self):
48 credentials = {'host': '127.0.0.1',
49 'verb': 'GET',
50 'path': '/v1/',
51 'params': {'SignatureVersion': '0',
52 'AWSAccessKeyId': self.access,
53 'Timestamp': '2012-11-27T11:47:02Z',
54 'Action': 'Foo'}}
55 self.assertFalse(self.signer._v4_creds(credentials))
56
57 def test_generate_0(self):
58 """Test generate function for v0 signature."""
59 credentials = {'host': '127.0.0.1',
60 'verb': 'GET',
61 'path': '/v1/',
62 'params': {'SignatureVersion': '0',
63 'AWSAccessKeyId': self.access,
64 'Timestamp': '2012-11-27T11:47:02Z',
65 'Action': 'Foo'}}
66 signature = self.signer.generate(credentials)
67 expected = 'SmXQEZAUdQw5glv5mX8mmixBtas='
68 self.assertEqual(signature, expected)
69
70 def test_generate_1(self):
71 """Test generate function for v1 signature."""
72 credentials = {'host': '127.0.0.1',
73 'verb': 'GET',
74 'path': '/v1/',
75 'params': {'SignatureVersion': '1',
76 'AWSAccessKeyId': self.access}}
77 signature = self.signer.generate(credentials)
78 expected = 'VRnoQH/EhVTTLhwRLfuL7jmFW9c='
79 self.assertEqual(signature, expected)
80
81 def test_generate_v2_SHA256(self):
82 """Test generate function for v2 signature, SHA256."""
83 credentials = {'host': '127.0.0.1',
84 'verb': 'GET',
85 'path': '/v1/',
86 'params': {'SignatureVersion': '2',
87 'AWSAccessKeyId': self.access}}
88 signature = self.signer.generate(credentials)
89 expected = 'odsGmT811GffUO0Eu13Pq+xTzKNIjJ6NhgZU74tYX/w='
90 self.assertEqual(signature, expected)
91
92 def test_generate_v2_SHA1(self):
93 """Test generate function for v2 signature, SHA1."""
94 credentials = {'host': '127.0.0.1',
95 'verb': 'GET',
96 'path': '/v1/',
97 'params': {'SignatureVersion': '2',
98 'AWSAccessKeyId': self.access}}
99 self.signer.hmac_256 = None
100 signature = self.signer.generate(credentials)
101 expected = 'ZqCxMI4ZtTXWI175743mJ0hy/Gc='
102 self.assertEqual(signature, expected)
103
104 def test_generate_v4(self):
105 """Test v4 generator with data from AWS docs example.
106
107 see:
108 http://docs.aws.amazon.com/general/latest/gr/
109 sigv4-create-canonical-request.html
110 and
111 http://docs.aws.amazon.com/general/latest/gr/
112 sigv4-signed-request-examples.html
113 """
114 # Create a new signer object with the AWS example key
115 secret = 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY'
116 signer = utils.Ec2Signer(secret)
117
118 body_hash = ('b6359072c78d70ebee1e81adcbab4f0'
119 '1bf2c23245fa365ef83fe8f1f955085e2')
120 auth_str = ('AWS4-HMAC-SHA256 '
121 'Credential=AKIAIOSFODNN7EXAMPLE/20110909/'
122 'us-east-1/iam/aws4_request,'
123 'SignedHeaders=content-type;host;x-amz-date,')
124 headers = {'Content-type':
125 'application/x-www-form-urlencoded; charset=utf-8',
126 'X-Amz-Date': '20110909T233600Z',
127 'Host': 'iam.amazonaws.com',
128 'Authorization': auth_str}
129 # Note the example in the AWS docs is inconsistent, previous
130 # examples specify no query string, but the final POST example
131 # does, apparently incorrectly since an empty parameter list
132 # aligns all steps and the final signature with the examples
133 params = {'Action': 'CreateUser',
134 'UserName': 'NewUser',
135 'Version': '2010-05-08',
136 'X-Amz-Algorithm': 'AWS4-HMAC-SHA256',
137 'X-Amz-Credential': 'AKIAEXAMPLE/20140611/'
138 'us-east-1/iam/aws4_request',
139 'X-Amz-Date': '20140611T231318Z',
140 'X-Amz-Expires': '30',
141 'X-Amz-SignedHeaders': 'host',
142 'X-Amz-Signature': 'ced6826de92d2bdeed8f846f0bf508e8'
143 '559e98e4b0199114b84c54174deb456c'}
144 credentials = {'host': 'iam.amazonaws.com',
145 'verb': 'POST',
146 'path': '/',
147 'params': params,
148 'headers': headers,
149 'body_hash': body_hash}
150 signature = signer.generate(credentials)
151 expected = ('ced6826de92d2bdeed8f846f0bf508e8'
152 '559e98e4b0199114b84c54174deb456c')
153 self.assertEqual(signature, expected)
154
155 def test_generate_v4_port(self):
156 """Test v4 generator with host:port format."""
157 # Create a new signer object with the AWS example key
158 secret = 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY'
159 signer = utils.Ec2Signer(secret)
160
161 body_hash = ('b6359072c78d70ebee1e81adcbab4f0'
162 '1bf2c23245fa365ef83fe8f1f955085e2')
163 auth_str = ('AWS4-HMAC-SHA256 '
164 'Credential=AKIAIOSFODNN7EXAMPLE/20110909/'
165 'us-east-1/iam/aws4_request,'
166 'SignedHeaders=content-type;host;x-amz-date,')
167 headers = {'Content-type':
168 'application/x-www-form-urlencoded; charset=utf-8',
169 'X-Amz-Date': '20110909T233600Z',
170 'Host': 'foo:8000',
171 'Authorization': auth_str}
172 # Note the example in the AWS docs is inconsistent, previous
173 # examples specify no query string, but the final POST example
174 # does, apparently incorrectly since an empty parameter list
175 # aligns all steps and the final signature with the examples
176 params = {}
177 credentials = {'host': 'foo:8000',
178 'verb': 'POST',
179 'path': '/',
180 'params': params,
181 'headers': headers,
182 'body_hash': body_hash}
183 signature = signer.generate(credentials)
184
185 expected = ('26dd92ea79aaa49f533d13b1055acdc'
186 'd7d7321460d64621f96cc79c4f4d4ab2b')
187 self.assertEqual(signature, expected)
188
189 def test_generate_v4_port_strip(self):
190 """Test v4 generator with host:port format for old boto version.
191
192 Validate for old (<2.9.3) version of boto, where the port should
193 be stripped to match boto behavior.
194 """
195 # Create a new signer object with the AWS example key
196 secret = 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY'
197 signer = utils.Ec2Signer(secret)
198
199 body_hash = ('b6359072c78d70ebee1e81adcbab4f0'
200 '1bf2c23245fa365ef83fe8f1f955085e2')
201 auth_str = ('AWS4-HMAC-SHA256 '
202 'Credential=AKIAIOSFODNN7EXAMPLE/20110909/'
203 'us-east-1/iam/aws4_request,'
204 'SignedHeaders=content-type;host;x-amz-date,')
205 headers = {'Content-type':
206 'application/x-www-form-urlencoded; charset=utf-8',
207 'X-Amz-Date': '20110909T233600Z',
208 'Host': 'foo:8000',
209 'Authorization': auth_str,
210 'User-Agent': 'Boto/2.9.2 (linux2)'}
211 # Note the example in the AWS docs is inconsistent, previous
212 # examples specify no query string, but the final POST example
213 # does, apparently incorrectly since an empty parameter list
214 # aligns all steps and the final signature with the examples
215 params = {}
216 credentials = {'host': 'foo:8000',
217 'verb': 'POST',
218 'path': '/',
219 'params': params,
220 'headers': headers,
221 'body_hash': body_hash}
222 signature = signer.generate(credentials)
223
224 expected = ('9a4b2276a5039ada3b90f72ea8ec1745'
225 '14b92b909fb106b22ad910c5d75a54f4')
226 self.assertEqual(expected, signature)
227
228 def test_generate_v4_port_nostrip(self):
229 """Test v4 generator with host:port format for new boto version.
230
231 Validate for new (>=2.9.3) version of boto, where the port should
232 not be stripped.
233 """
234 # Create a new signer object with the AWS example key
235 secret = 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY'
236 signer = utils.Ec2Signer(secret)
237
238 body_hash = ('b6359072c78d70ebee1e81adcbab4f0'
239 '1bf2c23245fa365ef83fe8f1f955085e2')
240 auth_str = ('AWS4-HMAC-SHA256 '
241 'Credential=AKIAIOSFODNN7EXAMPLE/20110909/'
242 'us-east-1/iam/aws4_request,'
243 'SignedHeaders=content-type;host;x-amz-date,')
244 headers = {'Content-type':
245 'application/x-www-form-urlencoded; charset=utf-8',
246 'X-Amz-Date': '20110909T233600Z',
247 'Host': 'foo:8000',
248 'Authorization': auth_str,
249 'User-Agent': 'Boto/2.9.3 (linux2)'}
250 # Note the example in the AWS docs is inconsistent, previous
251 # examples specify no query string, but the final POST example
252 # does, apparently incorrectly since an empty parameter list
253 # aligns all steps and the final signature with the examples
254 params = {}
255 credentials = {'host': 'foo:8000',
256 'verb': 'POST',
257 'path': '/',
258 'params': params,
259 'headers': headers,
260 'body_hash': body_hash}
261 signature = signer.generate(credentials)
262
263 expected = ('26dd92ea79aaa49f533d13b1055acdc'
264 'd7d7321460d64621f96cc79c4f4d4ab2b')
265 self.assertEqual(expected, signature)
266
267 def test_generate_v4_port_malformed_version(self):
268 """Test v4 generator with host:port format for malformed boto version.
269
270 Validate for malformed version of boto, where the port should
271 not be stripped.
272 """
273 # Create a new signer object with the AWS example key
274 secret = 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY'
275 signer = utils.Ec2Signer(secret)
276
277 body_hash = ('b6359072c78d70ebee1e81adcbab4f0'
278 '1bf2c23245fa365ef83fe8f1f955085e2')
279 auth_str = ('AWS4-HMAC-SHA256 '
280 'Credential=AKIAIOSFODNN7EXAMPLE/20110909/'
281 'us-east-1/iam/aws4_request,'
282 'SignedHeaders=content-type;host;x-amz-date,')
283 headers = {'Content-type':
284 'application/x-www-form-urlencoded; charset=utf-8',
285 'X-Amz-Date': '20110909T233600Z',
286 'Host': 'foo:8000',
287 'Authorization': auth_str,
288 'User-Agent': 'Boto/2.922 (linux2)'}
289 # Note the example in the AWS docs is inconsistent, previous
290 # examples specify no query string, but the final POST example
291 # does, apparently incorrectly since an empty parameter list
292 # aligns all steps and the final signature with the examples
293 params = {}
294 credentials = {'host': 'foo:8000',
295 'verb': 'POST',
296 'path': '/',
297 'params': params,
298 'headers': headers,
299 'body_hash': body_hash}
300 signature = signer.generate(credentials)
301
302 expected = ('26dd92ea79aaa49f533d13b1055acdc'
303 'd7d7321460d64621f96cc79c4f4d4ab2b')
304 self.assertEqual(expected, signature)