Debian Codegraph @

keystoneclient/tests/unit/auth/test_identity_v3.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import argparse
14 import copy
15 from unittest import mock
16 import uuid
17
18 from keystoneauth1 import fixture
19
20 from keystoneclient import access
21 from keystoneclient.auth.identity import v3
22 from keystoneclient.auth.identity.v3 import base as v3_base
23 from keystoneclient import client
24 from keystoneclient import exceptions
25 from keystoneclient import session
26 from keystoneclient.tests.unit import utils
27
28
29 class V3IdentityPlugin(utils.TestCase):
30
31 TEST_ROOT_URL = 'http://127.0.0.1:5000/'
32 TEST_URL = '%s%s' % (TEST_ROOT_URL, 'v3')
33 TEST_ROOT_ADMIN_URL = 'http://127.0.0.1:35357/'
34 TEST_ADMIN_URL = '%s%s' % (TEST_ROOT_ADMIN_URL, 'v3')
35
36 TEST_PASS = 'password'
37
38 TEST_SERVICE_CATALOG = [{
39 "endpoints": [{
40 "url": "http://cdn.admin-nets.local:8774/v1.0/",
41 "region": "RegionOne",
42 "interface": "public"
43 }, {
44 "url": "http://127.0.0.1:8774/v1.0",
45 "region": "RegionOne",
46 "interface": "internal"
47 }, {
48 "url": "http://cdn.admin-nets.local:8774/v1.0",
49 "region": "RegionOne",
50 "interface": "admin"
51 }],
52 "type": "nova_compat"
53 }, {
54 "endpoints": [{
55 "url": "http://nova/novapi/public",
56 "region": "RegionOne",
57 "interface": "public"
58 }, {
59 "url": "http://nova/novapi/internal",
60 "region": "RegionOne",
61 "interface": "internal"
62 }, {
63 "url": "http://nova/novapi/admin",
64 "region": "RegionOne",
65 "interface": "admin"
66 }],
67 "type": "compute",
68 "name": "nova",
69 }, {
70 "endpoints": [{
71 "url": "http://glance/glanceapi/public",
72 "region": "RegionOne",
73 "interface": "public"
74 }, {
75 "url": "http://glance/glanceapi/internal",
76 "region": "RegionOne",
77 "interface": "internal"
78 }, {
79 "url": "http://glance/glanceapi/admin",
80 "region": "RegionOne",
81 "interface": "admin"
82 }],
83 "type": "image",
84 "name": "glance"
85 }, {
86 "endpoints": [{
87 "url": "http://127.0.0.1:5000/v3",
88 "region": "RegionOne",
89 "interface": "public"
90 }, {
91 "url": "http://127.0.0.1:5000/v3",
92 "region": "RegionOne",
93 "interface": "internal"
94 }, {
95 "url": TEST_ADMIN_URL,
96 "region": "RegionOne",
97 "interface": "admin"
98 }],
99 "type": "identity"
100 }, {
101 "endpoints": [{
102 "url": "http://swift/swiftapi/public",
103 "region": "RegionOne",
104 "interface": "public"
105 }, {
106 "url": "http://swift/swiftapi/internal",
107 "region": "RegionOne",
108 "interface": "internal"
109 }, {
110 "url": "http://swift/swiftapi/admin",
111 "region": "RegionOne",
112 "interface": "admin"
113 }],
114 "type": "object-store"
115 }]
116
117 def setUp(self):
118 super(V3IdentityPlugin, self).setUp()
119
120 self.TEST_DISCOVERY_RESPONSE = {
121 'versions': {'values': [fixture.V3Discovery(self.TEST_URL)]}}
122
123 self.deprecations.expect_deprecations()
124
125 self.TEST_RESPONSE_DICT = {
126 "token": {
127 "methods": [
128 "token",
129 "password"
130 ],
131
132 "expires_at": "2999-01-01T00:00:10.000123Z",
133 "project": {
134 "domain": {
135 "id": self.TEST_DOMAIN_ID,
136 "name": self.TEST_DOMAIN_NAME
137 },
138 "id": self.TEST_TENANT_ID,
139 "name": self.TEST_TENANT_NAME
140 },
141 "user": {
142 "domain": {
143 "id": self.TEST_DOMAIN_ID,
144 "name": self.TEST_DOMAIN_NAME
145 },
146 "id": self.TEST_USER,
147 "name": self.TEST_USER
148 },
149 "issued_at": "2013-05-29T16:55:21.468960Z",
150 "catalog": self.TEST_SERVICE_CATALOG
151 },
152 }
153 self.TEST_PROJECTS_RESPONSE = {
154 "projects": [
155 {
156 "domain_id": "1789d1",
157 "enabled": "True",
158 "id": "263fd9",
159 "links": {
160 "self": "https://identity:5000/v3/projects/263fd9"
161 },
162 "name": "Dev Group A"
163 },
164 {
165 "domain_id": "1789d1",
166 "enabled": "True",
167 "id": "e56ad3",
168 "links": {
169 "self": "https://identity:5000/v3/projects/e56ad3"
170 },
171 "name": "Dev Group B"
172 }
173 ],
174 "links": {
175 "self": "https://identity:5000/v3/projects",
176 }
177 }
178
179 def stub_auth(self, subject_token=None, **kwargs):
180 if not subject_token:
181 subject_token = self.TEST_TOKEN
182
183 self.stub_url('POST', ['auth', 'tokens'],
184 headers={'X-Subject-Token': subject_token}, **kwargs)
185
186 def test_authenticate_with_username_password(self):
187 self.stub_auth(json=self.TEST_RESPONSE_DICT)
188 a = v3.Password(self.TEST_URL,
189 username=self.TEST_USER,
190 password=self.TEST_PASS)
191 s = session.Session(auth=a)
192
193 self.assertEqual({'X-Auth-Token': self.TEST_TOKEN},
194 s.get_auth_headers())
195
196 req = {'auth': {'identity':
197 {'methods': ['password'],
198 'password': {'user': {'name': self.TEST_USER,
199 'password': self.TEST_PASS}}}}}
200
201 self.assertRequestBodyIs(json=req)
202 self.assertRequestHeaderEqual('Content-Type', 'application/json')
203 self.assertRequestHeaderEqual('Accept', 'application/json')
204 self.assertEqual(s.auth.auth_ref.auth_token, self.TEST_TOKEN)
205
206 def test_authenticate_with_username_password_unscoped(self):
207 del self.TEST_RESPONSE_DICT['token']['catalog']
208 del self.TEST_RESPONSE_DICT['token']['project']
209
210 self.stub_auth(json=self.TEST_RESPONSE_DICT)
211 self.stub_url(method="GET", json=self.TEST_DISCOVERY_RESPONSE)
212 test_user_id = self.TEST_RESPONSE_DICT['token']['user']['id']
213 self.stub_url(method="GET",
214 json=self.TEST_PROJECTS_RESPONSE,
215 parts=['users', test_user_id, 'projects'])
216
217 a = v3.Password(self.TEST_URL,
218 username=self.TEST_USER,
219 password=self.TEST_PASS)
220 s = session.Session(auth=a)
221 cs = client.Client(session=s)
222
223 # As a sanity check on the auth_ref, make sure client has the
224 # proper user id, that it fetches the right project response
225 self.assertEqual(test_user_id, a.auth_ref.user_id)
226 t = cs.projects.list(user=a.auth_ref.user_id)
227 self.assertEqual(2, len(t))
228
229 def test_authenticate_with_username_password_domain_scoped(self):
230 self.stub_auth(json=self.TEST_RESPONSE_DICT)
231 a = v3.Password(self.TEST_URL, username=self.TEST_USER,
232 password=self.TEST_PASS, domain_id=self.TEST_DOMAIN_ID)
233 s = session.Session(a)
234
235 self.assertEqual({'X-Auth-Token': self.TEST_TOKEN},
236 s.get_auth_headers())
237
238 req = {'auth': {'identity':
239 {'methods': ['password'],
240 'password': {'user': {'name': self.TEST_USER,
241 'password': self.TEST_PASS}}},
242 'scope': {'domain': {'id': self.TEST_DOMAIN_ID}}}}
243 self.assertRequestBodyIs(json=req)
244 self.assertEqual(s.auth.auth_ref.auth_token, self.TEST_TOKEN)
245
246 def test_authenticate_with_username_password_project_scoped(self):
247 self.stub_auth(json=self.TEST_RESPONSE_DICT)
248 a = v3.Password(self.TEST_URL, username=self.TEST_USER,
249 password=self.TEST_PASS,
250 project_id=self.TEST_TENANT_ID)
251 s = session.Session(a)
252
253 self.assertEqual({'X-Auth-Token': self.TEST_TOKEN},
254 s.get_auth_headers())
255
256 req = {'auth': {'identity':
257 {'methods': ['password'],
258 'password': {'user': {'name': self.TEST_USER,
259 'password': self.TEST_PASS}}},
260 'scope': {'project': {'id': self.TEST_TENANT_ID}}}}
261 self.assertRequestBodyIs(json=req)
262 self.assertEqual(s.auth.auth_ref.auth_token, self.TEST_TOKEN)
263 self.assertEqual(s.auth.auth_ref.project_id, self.TEST_TENANT_ID)
264
265 def test_authenticate_with_token(self):
266 self.stub_auth(json=self.TEST_RESPONSE_DICT)
267 a = v3.Token(self.TEST_URL, self.TEST_TOKEN)
268 s = session.Session(auth=a)
269
270 self.assertEqual({'X-Auth-Token': self.TEST_TOKEN},
271 s.get_auth_headers())
272
273 req = {'auth': {'identity':
274 {'methods': ['token'],
275 'token': {'id': self.TEST_TOKEN}}}}
276
277 self.assertRequestBodyIs(json=req)
278
279 self.assertRequestHeaderEqual('Content-Type', 'application/json')
280 self.assertRequestHeaderEqual('Accept', 'application/json')
281 self.assertEqual(s.auth.auth_ref.auth_token, self.TEST_TOKEN)
282
283 def test_with_expired(self):
284 self.stub_auth(json=self.TEST_RESPONSE_DICT)
285
286 d = copy.deepcopy(self.TEST_RESPONSE_DICT)
287 d['token']['expires_at'] = '2000-01-01T00:00:10.000123Z'
288
289 a = v3.Password(self.TEST_URL, username='username',
290 password='password')
291 a.auth_ref = access.AccessInfo.factory(body=d)
292 s = session.Session(auth=a)
293
294 self.assertEqual({'X-Auth-Token': self.TEST_TOKEN},
295 s.get_auth_headers())
296
297 self.assertEqual(a.auth_ref['expires_at'],
298 self.TEST_RESPONSE_DICT['token']['expires_at'])
299
300 def test_with_domain_and_project_scoping(self):
301 a = v3.Password(self.TEST_URL, username='username',
302 password='password', project_id='project',
303 domain_id='domain')
304
305 self.assertRaises(exceptions.AuthorizationFailure,
306 a.get_token, None)
307 self.assertRaises(exceptions.AuthorizationFailure,
308 a.get_headers, None)
309
310 def test_with_trust_id(self):
311 self.stub_auth(json=self.TEST_RESPONSE_DICT)
312 a = v3.Password(self.TEST_URL, username=self.TEST_USER,
313 password=self.TEST_PASS, trust_id='trust')
314 s = session.Session(a)
315
316 self.assertEqual({'X-Auth-Token': self.TEST_TOKEN},
317 s.get_auth_headers())
318
319 req = {'auth': {'identity':
320 {'methods': ['password'],
321 'password': {'user': {'name': self.TEST_USER,
322 'password': self.TEST_PASS}}},
323 'scope': {'OS-TRUST:trust': {'id': 'trust'}}}}
324 self.assertRequestBodyIs(json=req)
325 self.assertEqual(s.auth.auth_ref.auth_token, self.TEST_TOKEN)
326
327 def test_with_multiple_mechanisms_factory(self):
328 self.stub_auth(json=self.TEST_RESPONSE_DICT)
329 p = v3.PasswordMethod(username=self.TEST_USER, password=self.TEST_PASS)
330 t = v3.TokenMethod(token='foo')
331 a = v3.Auth(self.TEST_URL, [p, t], trust_id='trust')
332 s = session.Session(a)
333
334 self.assertEqual({'X-Auth-Token': self.TEST_TOKEN},
335 s.get_auth_headers())
336
337 req = {'auth': {'identity':
338 {'methods': ['password', 'token'],
339 'password': {'user': {'name': self.TEST_USER,
340 'password': self.TEST_PASS}},
341 'token': {'id': 'foo'}},
342 'scope': {'OS-TRUST:trust': {'id': 'trust'}}}}
343 self.assertRequestBodyIs(json=req)
344 self.assertEqual(s.auth.auth_ref.auth_token, self.TEST_TOKEN)
345
346 def test_with_multiple_mechanisms(self):
347 self.stub_auth(json=self.TEST_RESPONSE_DICT)
348 p = v3.PasswordMethod(username=self.TEST_USER,
349 password=self.TEST_PASS)
350 t = v3.TokenMethod(token='foo')
351 a = v3.Auth(self.TEST_URL, [p, t], trust_id='trust')
352 s = session.Session(auth=a)
353
354 self.assertEqual({'X-Auth-Token': self.TEST_TOKEN},
355 s.get_auth_headers())
356
357 req = {'auth': {'identity':
358 {'methods': ['password', 'token'],
359 'password': {'user': {'name': self.TEST_USER,
360 'password': self.TEST_PASS}},
361 'token': {'id': 'foo'}},
362 'scope': {'OS-TRUST:trust': {'id': 'trust'}}}}
363 self.assertRequestBodyIs(json=req)
364 self.assertEqual(s.auth.auth_ref.auth_token, self.TEST_TOKEN)
365
366 def test_with_multiple_scopes(self):
367 s = session.Session()
368
369 a = v3.Password(self.TEST_URL,
370 username=self.TEST_USER, password=self.TEST_PASS,
371 domain_id='x', project_id='x')
372 self.assertRaises(exceptions.AuthorizationFailure, a.get_auth_ref, s)
373
374 a = v3.Password(self.TEST_URL,
375 username=self.TEST_USER, password=self.TEST_PASS,
376 domain_id='x', trust_id='x')
377 self.assertRaises(exceptions.AuthorizationFailure, a.get_auth_ref, s)
378
379 def _do_service_url_test(self, base_url, endpoint_filter):
380 self.stub_auth(json=self.TEST_RESPONSE_DICT)
381 self.stub_url('GET', ['path'],
382 base_url=base_url,
383 text='SUCCESS', status_code=200)
384
385 a = v3.Password(self.TEST_URL, username=self.TEST_USER,
386 password=self.TEST_PASS)
387 s = session.Session(auth=a)
388
389 resp = s.get('/path', endpoint_filter=endpoint_filter)
390
391 self.assertEqual(resp.status_code, 200)
392 self.assertEqual(self.requests_mock.last_request.url,
393 base_url + '/path')
394
395 def test_service_url(self):
396 endpoint_filter = {'service_type': 'compute',
397 'interface': 'admin',
398 'service_name': 'nova'}
399 self._do_service_url_test('http://nova/novapi/admin', endpoint_filter)
400
401 def test_service_url_defaults_to_public(self):
402 endpoint_filter = {'service_type': 'compute'}
403 self._do_service_url_test('http://nova/novapi/public', endpoint_filter)
404
405 def test_endpoint_filter_without_service_type_fails(self):
406 self.stub_auth(json=self.TEST_RESPONSE_DICT)
407
408 a = v3.Password(self.TEST_URL, username=self.TEST_USER,
409 password=self.TEST_PASS)
410 s = session.Session(auth=a)
411
412 self.assertRaises(exceptions.EndpointNotFound, s.get, '/path',
413 endpoint_filter={'interface': 'admin'})
414
415 def test_full_url_overrides_endpoint_filter(self):
416 self.stub_auth(json=self.TEST_RESPONSE_DICT)
417 self.stub_url('GET', [],
418 base_url='http://testurl/',
419 text='SUCCESS', status_code=200)
420
421 a = v3.Password(self.TEST_URL, username=self.TEST_USER,
422 password=self.TEST_PASS)
423 s = session.Session(auth=a)
424
425 resp = s.get('http://testurl/',
426 endpoint_filter={'service_type': 'compute'})
427 self.assertEqual(resp.status_code, 200)
428 self.assertEqual(resp.text, 'SUCCESS')
429
430 def test_invalid_auth_response_dict(self):
431 self.stub_auth(json={'hello': 'world'})
432
433 a = v3.Password(self.TEST_URL, username=self.TEST_USER,
434 password=self.TEST_PASS)
435 s = session.Session(auth=a)
436
437 self.assertRaises(exceptions.InvalidResponse, s.get, 'http://any',
438 authenticated=True)
439
440 def test_invalid_auth_response_type(self):
441 self.stub_url('POST', ['auth', 'tokens'], text='testdata')
442
443 a = v3.Password(self.TEST_URL, username=self.TEST_USER,
444 password=self.TEST_PASS)
445 s = session.Session(auth=a)
446
447 self.assertRaises(exceptions.InvalidResponse, s.get, 'http://any',
448 authenticated=True)
449
450 def test_invalidate_response(self):
451 auth_responses = [{'status_code': 200, 'json': self.TEST_RESPONSE_DICT,
452 'headers': {'X-Subject-Token': 'token1'}},
453 {'status_code': 200, 'json': self.TEST_RESPONSE_DICT,
454 'headers': {'X-Subject-Token': 'token2'}}]
455
456 self.requests_mock.post('%s/auth/tokens' % self.TEST_URL,
457 auth_responses)
458
459 a = v3.Password(self.TEST_URL, username=self.TEST_USER,
460 password=self.TEST_PASS)
461 s = session.Session(auth=a)
462
463 with self.deprecations.expect_deprecations_here():
464 self.assertEqual('token1', s.get_token())
465 self.assertEqual({'X-Auth-Token': 'token1'}, s.get_auth_headers())
466 a.invalidate()
467 with self.deprecations.expect_deprecations_here():
468 self.assertEqual('token2', s.get_token())
469 self.assertEqual({'X-Auth-Token': 'token2'}, s.get_auth_headers())
470
471 def test_doesnt_log_password(self):
472 self.stub_auth(json=self.TEST_RESPONSE_DICT)
473
474 password = uuid.uuid4().hex
475 a = v3.Password(self.TEST_URL, username=self.TEST_USER,
476 password=password)
477 s = session.Session(a)
478 with self.deprecations.expect_deprecations_here():
479 self.assertEqual(self.TEST_TOKEN, s.get_token())
480 self.assertEqual({'X-Auth-Token': self.TEST_TOKEN},
481 s.get_auth_headers())
482
483 self.assertNotIn(password, self.logger.output)
484
485 def test_sends_nocatalog(self):
486 del self.TEST_RESPONSE_DICT['token']['catalog']
487 self.stub_auth(json=self.TEST_RESPONSE_DICT)
488
489 a = v3.Password(self.TEST_URL,
490 username=self.TEST_USER,
491 password=self.TEST_PASS,
492 include_catalog=False)
493 s = session.Session(auth=a)
494
495 s.get_auth_headers()
496
497 auth_url = self.TEST_URL + '/auth/tokens'
498 self.assertEqual(auth_url, a.token_url)
499 self.assertEqual(auth_url + '?nocatalog',
500 self.requests_mock.last_request.url)
501
502 def test_symbols(self):
503 self.assertIs(v3.AuthMethod, v3_base.AuthMethod)
504 self.assertIs(v3.AuthConstructor, v3_base.AuthConstructor)
505 self.assertIs(v3.Auth, v3_base.Auth)
506
507 def test_unscoped_request(self):
508 token = fixture.V3Token()
509 self.stub_auth(json=token)
510 password = uuid.uuid4().hex
511
512 a = v3.Password(self.TEST_URL,
513 user_id=token.user_id,
514 password=password,
515 unscoped=True)
516 s = session.Session()
517
518 auth_ref = a.get_access(s)
519
520 with self.deprecations.expect_deprecations_here():
521 self.assertFalse(auth_ref.scoped)
522 body = self.requests_mock.last_request.json()
523
524 ident = body['auth']['identity']
525
526 self.assertEqual(['password'], ident['methods'])
527 self.assertEqual(token.user_id, ident['password']['user']['id'])
528 self.assertEqual(password, ident['password']['user']['password'])
529
530 self.assertEqual({}, body['auth']['scope']['unscoped'])
531
532 def test_unscoped_with_scope_data(self):
533 a = v3.Password(self.TEST_URL,
534 user_id=uuid.uuid4().hex,
535 password=uuid.uuid4().hex,
536 unscoped=True,
537 project_id=uuid.uuid4().hex)
538
539 s = session.Session()
540
541 self.assertRaises(exceptions.AuthorizationFailure, a.get_auth_ref, s)
542
543 @mock.patch('sys.stdin', autospec=True)
544 def test_prompt_password(self, mock_stdin):
545 parser = argparse.ArgumentParser()
546 v3.Password.register_argparse_arguments(parser)
547
548 username = uuid.uuid4().hex
549 user_domain_id = uuid.uuid4().hex
550 auth_url = uuid.uuid4().hex
551 project_id = uuid.uuid4().hex
552 password = uuid.uuid4().hex
553
554 opts = parser.parse_args(['--os-username', username,
555 '--os-auth-url', auth_url,
556 '--os-user-domain-id', user_domain_id,
557 '--os-project-id', project_id])
558
559 with mock.patch('getpass.getpass') as mock_getpass:
560 mock_getpass.return_value = password
561 mock_stdin.isatty = lambda: True
562
563 plugin = v3.Password.load_from_argparse_arguments(opts)
564
565 self.assertEqual(auth_url, plugin.auth_url)
566 self.assertEqual(username, plugin.auth_methods[0].username)
567 self.assertEqual(project_id, plugin.project_id)
568 self.assertEqual(user_domain_id,
569 plugin.auth_methods[0].user_domain_id)
570 self.assertEqual(password, plugin.auth_methods[0].password)