| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import copy |
| 14 |
import datetime |
| 15 |
|
| 16 |
from oslo_serialization import jsonutils |
| 17 |
from oslo_utils import timeutils |
| 18 |
from testtools import testcase |
| 19 |
|
| 20 |
from keystoneclient import exceptions |
| 21 |
from keystoneclient.tests.unit.v2_0 import utils |
| 22 |
from keystoneclient.v2_0 import client |
| 23 |
|
| 24 |
|
| 25 |
class AuthenticateAgainstKeystoneTests(utils.TestCase): |
| 26 |
def setUp(self): |
| 27 |
super(AuthenticateAgainstKeystoneTests, self).setUp() |
| 28 |
self.TEST_RESPONSE_DICT = { |
| 29 |
"access": { |
| 30 |
"token": { |
| 31 |
"expires": "2999-01-01T00:00:10.000123Z", |
| 32 |
"id": self.TEST_TOKEN, |
| 33 |
"tenant": { |
| 34 |
"id": self.TEST_TENANT_ID |
| 35 |
}, |
| 36 |
}, |
| 37 |
"user": { |
| 38 |
"id": self.TEST_USER |
| 39 |
}, |
| 40 |
"serviceCatalog": self.TEST_SERVICE_CATALOG, |
| 41 |
}, |
| 42 |
} |
| 43 |
self.TEST_REQUEST_BODY = { |
| 44 |
"auth": { |
| 45 |
"passwordCredentials": { |
| 46 |
"username": self.TEST_USER, |
| 47 |
"password": self.TEST_TOKEN, |
| 48 |
}, |
| 49 |
"tenantId": self.TEST_TENANT_ID, |
| 50 |
}, |
| 51 |
} |
| 52 |
|
| 53 |
def test_authenticate_success_expired(self): |
| 54 |
resp_a = copy.deepcopy(self.TEST_RESPONSE_DICT) |
| 55 |
resp_b = copy.deepcopy(self.TEST_RESPONSE_DICT) |
| 56 |
headers = {'Content-Type': 'application/json'} |
| 57 |
|
| 58 |
|
| 59 |
resp_a['access']['token']['expires'] = ( |
| 60 |
(timeutils.utcnow() - datetime.timedelta(1)).isoformat()) |
| 61 |
|
| 62 |
|
| 63 |
TEST_TOKEN = "abcdef" |
| 64 |
resp_b['access']['token']['expires'] = '2999-01-01T00:00:10.000123Z' |
| 65 |
resp_b['access']['token']['id'] = TEST_TOKEN |
| 66 |
|
| 67 |
|
| 68 |
self.stub_auth(response_list=[{'json': resp_a, 'headers': headers}, |
| 69 |
{'json': resp_b, 'headers': headers}]) |
| 70 |
|
| 71 |
|
| 72 |
with self.deprecations.expect_deprecations_here(): |
| 73 |
cs = client.Client(project_id=self.TEST_TENANT_ID, |
| 74 |
auth_url=self.TEST_URL, |
| 75 |
username=self.TEST_USER, |
| 76 |
password=self.TEST_TOKEN) |
| 77 |
|
| 78 |
self.assertEqual(cs.management_url, |
| 79 |
self.TEST_RESPONSE_DICT["access"]["serviceCatalog"][3] |
| 80 |
['endpoints'][0]["adminURL"]) |
| 81 |
|
| 82 |
with self.deprecations.expect_deprecations_here(): |
| 83 |
self.assertEqual(cs.auth_token, TEST_TOKEN) |
| 84 |
self.assertRequestBodyIs(json=self.TEST_REQUEST_BODY) |
| 85 |
|
| 86 |
def test_authenticate_failure(self): |
| 87 |
_auth = 'auth' |
| 88 |
_cred = 'passwordCredentials' |
| 89 |
_pass = 'password' |
| 90 |
self.TEST_REQUEST_BODY[_auth][_cred][_pass] = 'bad_key' |
| 91 |
error = {"unauthorized": {"message": "Unauthorized", |
| 92 |
"code": "401"}} |
| 93 |
|
| 94 |
self.stub_auth(status_code=401, json=error) |
| 95 |
|
| 96 |
with testcase.ExpectedException(exceptions.Unauthorized): |
| 97 |
with self.deprecations.expect_deprecations_here(): |
| 98 |
client.Client(username=self.TEST_USER, |
| 99 |
password="bad_key", |
| 100 |
project_id=self.TEST_TENANT_ID, |
| 101 |
auth_url=self.TEST_URL) |
| 102 |
|
| 103 |
self.assertRequestBodyIs(json=self.TEST_REQUEST_BODY) |
| 104 |
|
| 105 |
def test_auth_redirect(self): |
| 106 |
self.stub_auth(status_code=305, text='Use Proxy', |
| 107 |
headers={'Location': self.TEST_ADMIN_URL + "/tokens"}) |
| 108 |
|
| 109 |
self.stub_auth(base_url=self.TEST_ADMIN_URL, |
| 110 |
json=self.TEST_RESPONSE_DICT) |
| 111 |
|
| 112 |
with self.deprecations.expect_deprecations_here(): |
| 113 |
cs = client.Client(username=self.TEST_USER, |
| 114 |
password=self.TEST_TOKEN, |
| 115 |
project_id=self.TEST_TENANT_ID, |
| 116 |
auth_url=self.TEST_URL) |
| 117 |
|
| 118 |
self.assertEqual(cs.management_url, |
| 119 |
self.TEST_RESPONSE_DICT["access"]["serviceCatalog"][3] |
| 120 |
['endpoints'][0]["adminURL"]) |
| 121 |
self.assertEqual(cs.auth_token, |
| 122 |
self.TEST_RESPONSE_DICT["access"]["token"]["id"]) |
| 123 |
self.assertRequestBodyIs(json=self.TEST_REQUEST_BODY) |
| 124 |
|
| 125 |
def test_authenticate_success_password_scoped(self): |
| 126 |
self.stub_auth(json=self.TEST_RESPONSE_DICT) |
| 127 |
|
| 128 |
with self.deprecations.expect_deprecations_here(): |
| 129 |
cs = client.Client(username=self.TEST_USER, |
| 130 |
password=self.TEST_TOKEN, |
| 131 |
project_id=self.TEST_TENANT_ID, |
| 132 |
auth_url=self.TEST_URL) |
| 133 |
self.assertEqual(cs.management_url, |
| 134 |
self.TEST_RESPONSE_DICT["access"]["serviceCatalog"][3] |
| 135 |
['endpoints'][0]["adminURL"]) |
| 136 |
self.assertEqual(cs.auth_token, |
| 137 |
self.TEST_RESPONSE_DICT["access"]["token"]["id"]) |
| 138 |
self.assertRequestBodyIs(json=self.TEST_REQUEST_BODY) |
| 139 |
|
| 140 |
def test_authenticate_success_password_unscoped(self): |
| 141 |
del self.TEST_RESPONSE_DICT['access']['serviceCatalog'] |
| 142 |
del self.TEST_REQUEST_BODY['auth']['tenantId'] |
| 143 |
|
| 144 |
self.stub_auth(json=self.TEST_RESPONSE_DICT) |
| 145 |
|
| 146 |
with self.deprecations.expect_deprecations_here(): |
| 147 |
cs = client.Client(username=self.TEST_USER, |
| 148 |
password=self.TEST_TOKEN, |
| 149 |
auth_url=self.TEST_URL) |
| 150 |
self.assertEqual(cs.auth_token, |
| 151 |
self.TEST_RESPONSE_DICT["access"]["token"]["id"]) |
| 152 |
self.assertNotIn('serviceCatalog', cs.service_catalog.catalog) |
| 153 |
self.assertRequestBodyIs(json=self.TEST_REQUEST_BODY) |
| 154 |
|
| 155 |
def test_auth_url_token_authentication(self): |
| 156 |
fake_token = 'fake_token' |
| 157 |
fake_url = '/fake-url' |
| 158 |
fake_resp = {'result': True} |
| 159 |
|
| 160 |
self.stub_auth(json=self.TEST_RESPONSE_DICT) |
| 161 |
self.stub_url('GET', [fake_url], json=fake_resp, |
| 162 |
base_url=self.TEST_ADMIN_IDENTITY_ENDPOINT) |
| 163 |
|
| 164 |
with self.deprecations.expect_deprecations_here(): |
| 165 |
cl = client.Client(auth_url=self.TEST_URL, |
| 166 |
token=fake_token) |
| 167 |
json_body = jsonutils.loads(self.requests_mock.last_request.body) |
| 168 |
self.assertEqual(json_body['auth']['token']['id'], fake_token) |
| 169 |
|
| 170 |
with self.deprecations.expect_deprecations_here(): |
| 171 |
resp, body = cl.get(fake_url) |
| 172 |
self.assertEqual(fake_resp, body) |
| 173 |
|
| 174 |
token = self.requests_mock.last_request.headers.get('X-Auth-Token') |
| 175 |
self.assertEqual(self.TEST_TOKEN, token) |
| 176 |
|
| 177 |
def test_authenticate_success_token_scoped(self): |
| 178 |
del self.TEST_REQUEST_BODY['auth']['passwordCredentials'] |
| 179 |
self.TEST_REQUEST_BODY['auth']['token'] = {'id': self.TEST_TOKEN} |
| 180 |
self.stub_auth(json=self.TEST_RESPONSE_DICT) |
| 181 |
|
| 182 |
with self.deprecations.expect_deprecations_here(): |
| 183 |
cs = client.Client(token=self.TEST_TOKEN, |
| 184 |
project_id=self.TEST_TENANT_ID, |
| 185 |
auth_url=self.TEST_URL) |
| 186 |
self.assertEqual(cs.management_url, |
| 187 |
self.TEST_RESPONSE_DICT["access"]["serviceCatalog"][3] |
| 188 |
['endpoints'][0]["adminURL"]) |
| 189 |
self.assertEqual(cs.auth_token, |
| 190 |
self.TEST_RESPONSE_DICT["access"]["token"]["id"]) |
| 191 |
self.assertRequestBodyIs(json=self.TEST_REQUEST_BODY) |
| 192 |
|
| 193 |
def test_authenticate_success_token_scoped_trust(self): |
| 194 |
del self.TEST_REQUEST_BODY['auth']['passwordCredentials'] |
| 195 |
self.TEST_REQUEST_BODY['auth']['token'] = {'id': self.TEST_TOKEN} |
| 196 |
self.TEST_REQUEST_BODY['auth']['trust_id'] = self.TEST_TRUST_ID |
| 197 |
response = self.TEST_RESPONSE_DICT.copy() |
| 198 |
response['access']['trust'] = {"trustee_user_id": self.TEST_USER, |
| 199 |
"id": self.TEST_TRUST_ID} |
| 200 |
self.stub_auth(json=response) |
| 201 |
|
| 202 |
with self.deprecations.expect_deprecations_here(): |
| 203 |
cs = client.Client(token=self.TEST_TOKEN, |
| 204 |
project_id=self.TEST_TENANT_ID, |
| 205 |
trust_id=self.TEST_TRUST_ID, |
| 206 |
auth_url=self.TEST_URL) |
| 207 |
self.assertTrue(cs.auth_ref.trust_scoped) |
| 208 |
self.assertEqual(cs.auth_ref.trust_id, self.TEST_TRUST_ID) |
| 209 |
self.assertEqual(cs.auth_ref.trustee_user_id, self.TEST_USER) |
| 210 |
self.assertRequestBodyIs(json=self.TEST_REQUEST_BODY) |
| 211 |
|
| 212 |
def test_authenticate_success_token_unscoped(self): |
| 213 |
del self.TEST_REQUEST_BODY['auth']['passwordCredentials'] |
| 214 |
del self.TEST_REQUEST_BODY['auth']['tenantId'] |
| 215 |
del self.TEST_RESPONSE_DICT['access']['serviceCatalog'] |
| 216 |
self.TEST_REQUEST_BODY['auth']['token'] = {'id': self.TEST_TOKEN} |
| 217 |
|
| 218 |
self.stub_auth(json=self.TEST_RESPONSE_DICT) |
| 219 |
|
| 220 |
with self.deprecations.expect_deprecations_here(): |
| 221 |
cs = client.Client(token=self.TEST_TOKEN, |
| 222 |
auth_url=self.TEST_URL) |
| 223 |
self.assertEqual(cs.auth_token, |
| 224 |
self.TEST_RESPONSE_DICT["access"]["token"]["id"]) |
| 225 |
self.assertNotIn('serviceCatalog', cs.service_catalog.catalog) |
| 226 |
self.assertRequestBodyIs(json=self.TEST_REQUEST_BODY) |
| 227 |
|
| 228 |
def test_allow_override_of_auth_token(self): |
| 229 |
fake_url = '/fake-url' |
| 230 |
fake_token = 'fake_token' |
| 231 |
fake_resp = {'result': True} |
| 232 |
|
| 233 |
self.stub_auth(json=self.TEST_RESPONSE_DICT) |
| 234 |
self.stub_url('GET', [fake_url], json=fake_resp, |
| 235 |
base_url=self.TEST_ADMIN_IDENTITY_ENDPOINT) |
| 236 |
|
| 237 |
with self.deprecations.expect_deprecations_here(): |
| 238 |
cl = client.Client(username='exampleuser', |
| 239 |
password='password', |
| 240 |
project_name='exampleproject', |
| 241 |
auth_url=self.TEST_URL) |
| 242 |
|
| 243 |
self.assertEqual(cl.auth_token, self.TEST_TOKEN) |
| 244 |
|
| 245 |
|
| 246 |
resp, body = cl._adapter.get(fake_url) |
| 247 |
self.assertEqual(fake_resp, body) |
| 248 |
|
| 249 |
token = self.requests_mock.last_request.headers.get('X-Auth-Token') |
| 250 |
self.assertEqual(self.TEST_TOKEN, token) |
| 251 |
|
| 252 |
|
| 253 |
cl.auth_token = fake_token |
| 254 |
|
| 255 |
resp, body = cl._adapter.get(fake_url) |
| 256 |
self.assertEqual(fake_resp, body) |
| 257 |
|
| 258 |
token = self.requests_mock.last_request.headers.get('X-Auth-Token') |
| 259 |
self.assertEqual(fake_token, token) |
| 260 |
|
| 261 |
|
| 262 |
del cl.auth_token |
| 263 |
|
| 264 |
resp, body = cl._adapter.get(fake_url) |
| 265 |
self.assertEqual(fake_resp, body) |
| 266 |
|
| 267 |
token = self.requests_mock.last_request.headers.get('X-Auth-Token') |
| 268 |
self.assertEqual(self.TEST_TOKEN, token) |