| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import argparse |
| 14 |
import copy |
| 15 |
from unittest import mock |
| 16 |
import uuid |
| 17 |
|
| 18 |
|
| 19 |
from keystoneclient.auth.identity import v2 |
| 20 |
from keystoneclient import exceptions |
| 21 |
from keystoneclient import session |
| 22 |
from keystoneclient.tests.unit import utils |
| 23 |
|
| 24 |
|
| 25 |
class V2IdentityPlugin(utils.TestCase): |
| 26 |
|
| 27 |
TEST_ROOT_URL = 'http://127.0.0.1:5000/' |
| 28 |
TEST_URL = '%s%s' % (TEST_ROOT_URL, 'v2.0') |
| 29 |
TEST_ROOT_ADMIN_URL = 'http://127.0.0.1:35357/' |
| 30 |
TEST_ADMIN_URL = '%s%s' % (TEST_ROOT_ADMIN_URL, 'v2.0') |
| 31 |
|
| 32 |
TEST_PASS = 'password' |
| 33 |
|
| 34 |
TEST_SERVICE_CATALOG = [{ |
| 35 |
"endpoints": [{ |
| 36 |
"adminURL": "http://cdn.admin-nets.local:8774/v1.0", |
| 37 |
"region": "RegionOne", |
| 38 |
"internalURL": "http://127.0.0.1:8774/v1.0", |
| 39 |
"publicURL": "http://cdn.admin-nets.local:8774/v1.0/" |
| 40 |
}], |
| 41 |
"type": "nova_compat", |
| 42 |
"name": "nova_compat" |
| 43 |
}, { |
| 44 |
"endpoints": [{ |
| 45 |
"adminURL": "http://nova/novapi/admin", |
| 46 |
"region": "RegionOne", |
| 47 |
"internalURL": "http://nova/novapi/internal", |
| 48 |
"publicURL": "http://nova/novapi/public" |
| 49 |
}], |
| 50 |
"type": "compute", |
| 51 |
"name": "nova" |
| 52 |
}, { |
| 53 |
"endpoints": [{ |
| 54 |
"adminURL": "http://glance/glanceapi/admin", |
| 55 |
"region": "RegionOne", |
| 56 |
"internalURL": "http://glance/glanceapi/internal", |
| 57 |
"publicURL": "http://glance/glanceapi/public" |
| 58 |
}], |
| 59 |
"type": "image", |
| 60 |
"name": "glance" |
| 61 |
}, { |
| 62 |
"endpoints": [{ |
| 63 |
"adminURL": TEST_ADMIN_URL, |
| 64 |
"region": "RegionOne", |
| 65 |
"internalURL": "http://127.0.0.1:5000/v2.0", |
| 66 |
"publicURL": "http://127.0.0.1:5000/v2.0" |
| 67 |
}], |
| 68 |
"type": "identity", |
| 69 |
"name": "keystone" |
| 70 |
}, { |
| 71 |
"endpoints": [{ |
| 72 |
"adminURL": "http://swift/swiftapi/admin", |
| 73 |
"region": "RegionOne", |
| 74 |
"internalURL": "http://swift/swiftapi/internal", |
| 75 |
"publicURL": "http://swift/swiftapi/public" |
| 76 |
}], |
| 77 |
"type": "object-store", |
| 78 |
"name": "swift" |
| 79 |
}] |
| 80 |
|
| 81 |
def setUp(self): |
| 82 |
super(V2IdentityPlugin, self).setUp() |
| 83 |
self.deprecations.expect_deprecations() |
| 84 |
self.TEST_RESPONSE_DICT = { |
| 85 |
"access": { |
| 86 |
"token": { |
| 87 |
"expires": "2999-01-01T00:00:10.000123Z", |
| 88 |
"id": self.TEST_TOKEN, |
| 89 |
"tenant": { |
| 90 |
"id": self.TEST_TENANT_ID |
| 91 |
}, |
| 92 |
}, |
| 93 |
"user": { |
| 94 |
"id": self.TEST_USER |
| 95 |
}, |
| 96 |
"serviceCatalog": self.TEST_SERVICE_CATALOG, |
| 97 |
}, |
| 98 |
} |
| 99 |
|
| 100 |
def stub_auth(self, **kwargs): |
| 101 |
self.stub_url('POST', ['tokens'], **kwargs) |
| 102 |
|
| 103 |
def test_authenticate_with_username_password(self): |
| 104 |
self.stub_auth(json=self.TEST_RESPONSE_DICT) |
| 105 |
a = v2.Password(self.TEST_URL, username=self.TEST_USER, |
| 106 |
password=self.TEST_PASS) |
| 107 |
self.assertIsNone(a.user_id) |
| 108 |
s = session.Session(a) |
| 109 |
self.assertEqual({'X-Auth-Token': self.TEST_TOKEN}, |
| 110 |
s.get_auth_headers()) |
| 111 |
|
| 112 |
req = {'auth': {'passwordCredentials': {'username': self.TEST_USER, |
| 113 |
'password': self.TEST_PASS}}} |
| 114 |
self.assertRequestBodyIs(json=req) |
| 115 |
self.assertRequestHeaderEqual('Content-Type', 'application/json') |
| 116 |
self.assertRequestHeaderEqual('Accept', 'application/json') |
| 117 |
self.assertEqual(s.auth.auth_ref.auth_token, self.TEST_TOKEN) |
| 118 |
|
| 119 |
def test_authenticate_with_user_id_password(self): |
| 120 |
self.stub_auth(json=self.TEST_RESPONSE_DICT) |
| 121 |
a = v2.Password(self.TEST_URL, user_id=self.TEST_USER, |
| 122 |
password=self.TEST_PASS) |
| 123 |
self.assertIsNone(a.username) |
| 124 |
s = session.Session(a) |
| 125 |
self.assertEqual({'X-Auth-Token': self.TEST_TOKEN}, |
| 126 |
s.get_auth_headers()) |
| 127 |
|
| 128 |
req = {'auth': {'passwordCredentials': {'userId': self.TEST_USER, |
| 129 |
'password': self.TEST_PASS}}} |
| 130 |
self.assertRequestBodyIs(json=req) |
| 131 |
self.assertRequestHeaderEqual('Content-Type', 'application/json') |
| 132 |
self.assertRequestHeaderEqual('Accept', 'application/json') |
| 133 |
self.assertEqual(s.auth.auth_ref.auth_token, self.TEST_TOKEN) |
| 134 |
|
| 135 |
def test_authenticate_with_username_password_scoped(self): |
| 136 |
self.stub_auth(json=self.TEST_RESPONSE_DICT) |
| 137 |
a = v2.Password(self.TEST_URL, username=self.TEST_USER, |
| 138 |
password=self.TEST_PASS, tenant_id=self.TEST_TENANT_ID) |
| 139 |
self.assertIsNone(a.user_id) |
| 140 |
s = session.Session(a) |
| 141 |
self.assertEqual({'X-Auth-Token': self.TEST_TOKEN}, |
| 142 |
s.get_auth_headers()) |
| 143 |
|
| 144 |
req = {'auth': {'passwordCredentials': {'username': self.TEST_USER, |
| 145 |
'password': self.TEST_PASS}, |
| 146 |
'tenantId': self.TEST_TENANT_ID}} |
| 147 |
self.assertRequestBodyIs(json=req) |
| 148 |
self.assertEqual(s.auth.auth_ref.auth_token, self.TEST_TOKEN) |
| 149 |
|
| 150 |
def test_authenticate_with_user_id_password_scoped(self): |
| 151 |
self.stub_auth(json=self.TEST_RESPONSE_DICT) |
| 152 |
a = v2.Password(self.TEST_URL, user_id=self.TEST_USER, |
| 153 |
password=self.TEST_PASS, tenant_id=self.TEST_TENANT_ID) |
| 154 |
self.assertIsNone(a.username) |
| 155 |
s = session.Session(a) |
| 156 |
self.assertEqual({'X-Auth-Token': self.TEST_TOKEN}, |
| 157 |
s.get_auth_headers()) |
| 158 |
|
| 159 |
req = {'auth': {'passwordCredentials': {'userId': self.TEST_USER, |
| 160 |
'password': self.TEST_PASS}, |
| 161 |
'tenantId': self.TEST_TENANT_ID}} |
| 162 |
self.assertRequestBodyIs(json=req) |
| 163 |
self.assertEqual(s.auth.auth_ref.auth_token, self.TEST_TOKEN) |
| 164 |
|
| 165 |
def test_authenticate_with_token(self): |
| 166 |
self.stub_auth(json=self.TEST_RESPONSE_DICT) |
| 167 |
a = v2.Token(self.TEST_URL, 'foo') |
| 168 |
s = session.Session(a) |
| 169 |
self.assertEqual({'X-Auth-Token': self.TEST_TOKEN}, |
| 170 |
s.get_auth_headers()) |
| 171 |
|
| 172 |
req = {'auth': {'token': {'id': 'foo'}}} |
| 173 |
self.assertRequestBodyIs(json=req) |
| 174 |
self.assertRequestHeaderEqual('x-Auth-Token', 'foo') |
| 175 |
self.assertRequestHeaderEqual('Content-Type', 'application/json') |
| 176 |
self.assertRequestHeaderEqual('Accept', 'application/json') |
| 177 |
self.assertEqual(s.auth.auth_ref.auth_token, self.TEST_TOKEN) |
| 178 |
|
| 179 |
def test_with_trust_id(self): |
| 180 |
self.stub_auth(json=self.TEST_RESPONSE_DICT) |
| 181 |
a = v2.Password(self.TEST_URL, username=self.TEST_USER, |
| 182 |
password=self.TEST_PASS, trust_id='trust') |
| 183 |
s = session.Session(a) |
| 184 |
self.assertEqual({'X-Auth-Token': self.TEST_TOKEN}, |
| 185 |
s.get_auth_headers()) |
| 186 |
|
| 187 |
req = {'auth': {'passwordCredentials': {'username': self.TEST_USER, |
| 188 |
'password': self.TEST_PASS}, |
| 189 |
'trust_id': 'trust'}} |
| 190 |
|
| 191 |
self.assertRequestBodyIs(json=req) |
| 192 |
self.assertEqual(s.auth.auth_ref.auth_token, self.TEST_TOKEN) |
| 193 |
|
| 194 |
def _do_service_url_test(self, base_url, endpoint_filter): |
| 195 |
self.stub_auth(json=self.TEST_RESPONSE_DICT) |
| 196 |
self.stub_url('GET', ['path'], |
| 197 |
base_url=base_url, |
| 198 |
text='SUCCESS', status_code=200) |
| 199 |
|
| 200 |
a = v2.Password(self.TEST_URL, username=self.TEST_USER, |
| 201 |
password=self.TEST_PASS) |
| 202 |
s = session.Session(auth=a) |
| 203 |
|
| 204 |
resp = s.get('/path', endpoint_filter=endpoint_filter) |
| 205 |
|
| 206 |
self.assertEqual(resp.status_code, 200) |
| 207 |
self.assertEqual(self.requests_mock.last_request.url, |
| 208 |
base_url + '/path') |
| 209 |
|
| 210 |
def test_service_url(self): |
| 211 |
endpoint_filter = {'service_type': 'compute', |
| 212 |
'interface': 'admin', |
| 213 |
'service_name': 'nova'} |
| 214 |
self._do_service_url_test('http://nova/novapi/admin', endpoint_filter) |
| 215 |
|
| 216 |
def test_service_url_defaults_to_public(self): |
| 217 |
endpoint_filter = {'service_type': 'compute'} |
| 218 |
self._do_service_url_test('http://nova/novapi/public', endpoint_filter) |
| 219 |
|
| 220 |
def test_endpoint_filter_without_service_type_fails(self): |
| 221 |
self.stub_auth(json=self.TEST_RESPONSE_DICT) |
| 222 |
|
| 223 |
a = v2.Password(self.TEST_URL, username=self.TEST_USER, |
| 224 |
password=self.TEST_PASS) |
| 225 |
s = session.Session(auth=a) |
| 226 |
|
| 227 |
self.assertRaises(exceptions.EndpointNotFound, s.get, '/path', |
| 228 |
endpoint_filter={'interface': 'admin'}) |
| 229 |
|
| 230 |
def test_full_url_overrides_endpoint_filter(self): |
| 231 |
self.stub_auth(json=self.TEST_RESPONSE_DICT) |
| 232 |
self.stub_url('GET', [], |
| 233 |
base_url='http://testurl/', |
| 234 |
text='SUCCESS', status_code=200) |
| 235 |
|
| 236 |
a = v2.Password(self.TEST_URL, username=self.TEST_USER, |
| 237 |
password=self.TEST_PASS) |
| 238 |
s = session.Session(auth=a) |
| 239 |
|
| 240 |
resp = s.get('http://testurl/', |
| 241 |
endpoint_filter={'service_type': 'compute'}) |
| 242 |
self.assertEqual(resp.status_code, 200) |
| 243 |
self.assertEqual(resp.text, 'SUCCESS') |
| 244 |
|
| 245 |
def test_invalid_auth_response_dict(self): |
| 246 |
self.stub_auth(json={'hello': 'world'}) |
| 247 |
|
| 248 |
a = v2.Password(self.TEST_URL, username=self.TEST_USER, |
| 249 |
password=self.TEST_PASS) |
| 250 |
s = session.Session(auth=a) |
| 251 |
|
| 252 |
self.assertRaises(exceptions.InvalidResponse, s.get, 'http://any', |
| 253 |
authenticated=True) |
| 254 |
|
| 255 |
def test_invalid_auth_response_type(self): |
| 256 |
self.stub_url('POST', ['tokens'], text='testdata') |
| 257 |
|
| 258 |
a = v2.Password(self.TEST_URL, username=self.TEST_USER, |
| 259 |
password=self.TEST_PASS) |
| 260 |
s = session.Session(auth=a) |
| 261 |
|
| 262 |
self.assertRaises(exceptions.InvalidResponse, s.get, 'http://any', |
| 263 |
authenticated=True) |
| 264 |
|
| 265 |
def test_invalidate_response(self): |
| 266 |
resp_data1 = copy.deepcopy(self.TEST_RESPONSE_DICT) |
| 267 |
resp_data2 = copy.deepcopy(self.TEST_RESPONSE_DICT) |
| 268 |
|
| 269 |
resp_data1['access']['token']['id'] = 'token1' |
| 270 |
resp_data2['access']['token']['id'] = 'token2' |
| 271 |
|
| 272 |
auth_responses = [{'json': resp_data1}, {'json': resp_data2}] |
| 273 |
self.stub_auth(response_list=auth_responses) |
| 274 |
|
| 275 |
a = v2.Password(self.TEST_URL, username=self.TEST_USER, |
| 276 |
password=self.TEST_PASS) |
| 277 |
s = session.Session(auth=a) |
| 278 |
|
| 279 |
with self.deprecations.expect_deprecations_here(): |
| 280 |
self.assertEqual('token1', s.get_token()) |
| 281 |
self.assertEqual({'X-Auth-Token': 'token1'}, s.get_auth_headers()) |
| 282 |
|
| 283 |
a.invalidate() |
| 284 |
with self.deprecations.expect_deprecations_here(): |
| 285 |
self.assertEqual('token2', s.get_token()) |
| 286 |
self.assertEqual({'X-Auth-Token': 'token2'}, s.get_auth_headers()) |
| 287 |
|
| 288 |
def test_doesnt_log_password(self): |
| 289 |
self.stub_auth(json=self.TEST_RESPONSE_DICT) |
| 290 |
password = uuid.uuid4().hex |
| 291 |
|
| 292 |
a = v2.Password(self.TEST_URL, username=self.TEST_USER, |
| 293 |
password=password) |
| 294 |
s = session.Session(auth=a) |
| 295 |
with self.deprecations.expect_deprecations_here(): |
| 296 |
self.assertEqual(self.TEST_TOKEN, s.get_token()) |
| 297 |
self.assertEqual({'X-Auth-Token': self.TEST_TOKEN}, |
| 298 |
s.get_auth_headers()) |
| 299 |
self.assertNotIn(password, self.logger.output) |
| 300 |
|
| 301 |
def test_password_with_no_user_id_or_name(self): |
| 302 |
self.assertRaises(TypeError, |
| 303 |
v2.Password, self.TEST_URL, password=self.TEST_PASS) |
| 304 |
|
| 305 |
@mock.patch('sys.stdin', autospec=True) |
| 306 |
def test_prompt_password(self, mock_stdin): |
| 307 |
parser = argparse.ArgumentParser() |
| 308 |
v2.Password.register_argparse_arguments(parser) |
| 309 |
|
| 310 |
username = uuid.uuid4().hex |
| 311 |
auth_url = uuid.uuid4().hex |
| 312 |
tenant_id = uuid.uuid4().hex |
| 313 |
password = uuid.uuid4().hex |
| 314 |
|
| 315 |
opts = parser.parse_args(['--os-username', username, |
| 316 |
'--os-auth-url', auth_url, |
| 317 |
'--os-tenant-id', tenant_id]) |
| 318 |
|
| 319 |
with mock.patch('getpass.getpass') as mock_getpass: |
| 320 |
mock_getpass.return_value = password |
| 321 |
mock_stdin.isatty = lambda: True |
| 322 |
|
| 323 |
plugin = v2.Password.load_from_argparse_arguments(opts) |
| 324 |
|
| 325 |
self.assertEqual(auth_url, plugin.auth_url) |
| 326 |
self.assertEqual(username, plugin.username) |
| 327 |
self.assertEqual(tenant_id, plugin.tenant_id) |
| 328 |
self.assertEqual(password, plugin.password) |