ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 1 | # Copyright 2015 NEC Corporation. All rights reserved. |
| 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 json |
Andrea Frittoli | 9001235 | 2015-02-25 21:58:02 +0000 | [diff] [blame] | 16 | from tempest_lib.common import rest_client |
Masayuki Igawa | 29d554f | 2015-01-20 12:36:42 +0900 | [diff] [blame] | 17 | from tempest_lib import exceptions as lib_exc |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 18 | |
| 19 | from tempest.common import service_client |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 20 | from tempest import exceptions |
| 21 | |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 22 | |
Andrea Frittoli | 9001235 | 2015-02-25 21:58:02 +0000 | [diff] [blame] | 23 | class V3TokenClientJSON(rest_client.RestClient): |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 24 | |
Andrea Frittoli | 9001235 | 2015-02-25 21:58:02 +0000 | [diff] [blame] | 25 | def __init__(self, auth_url, disable_ssl_certificate_validation=None, |
| 26 | ca_certs=None, trace_requests=None): |
| 27 | dscv = disable_ssl_certificate_validation |
| 28 | super(V3TokenClientJSON, self).__init__( |
| 29 | None, None, None, disable_ssl_certificate_validation=dscv, |
| 30 | ca_certs=ca_certs, trace_requests=trace_requests) |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 31 | if not auth_url: |
| 32 | raise exceptions.InvalidConfiguration('you must specify a v3 uri ' |
| 33 | 'if using the v3 identity ' |
| 34 | 'api') |
| 35 | if 'auth/tokens' not in auth_url: |
| 36 | auth_url = auth_url.rstrip('/') + '/auth/tokens' |
| 37 | |
| 38 | self.auth_url = auth_url |
| 39 | |
Jamie Lennox | e5a95d4 | 2015-02-11 07:19:57 +0000 | [diff] [blame] | 40 | def auth(self, user=None, password=None, project=None, user_type='id', |
| 41 | user_domain=None, project_domain=None, token=None): |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 42 | """ |
| 43 | :param user: user id or name, as specified in user_type |
Jamie Lennox | e5a95d4 | 2015-02-11 07:19:57 +0000 | [diff] [blame] | 44 | :param user_domain: the user domain |
| 45 | :param project_domain: the project domain |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 46 | :param token: a token to re-scope. |
| 47 | |
| 48 | Accepts different combinations of credentials. Restrictions: |
Jamie Lennox | e5a95d4 | 2015-02-11 07:19:57 +0000 | [diff] [blame] | 49 | - project and domain are only name (no id) |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 50 | Sample sample valid combinations: |
| 51 | - token |
Jamie Lennox | e5a95d4 | 2015-02-11 07:19:57 +0000 | [diff] [blame] | 52 | - token, project, project_domain |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 53 | - user_id, password |
Jamie Lennox | e5a95d4 | 2015-02-11 07:19:57 +0000 | [diff] [blame] | 54 | - username, password, user_domain |
| 55 | - username, password, project, user_domain, project_domain |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 56 | Validation is left to the server side. |
| 57 | """ |
| 58 | creds = { |
| 59 | 'auth': { |
| 60 | 'identity': { |
| 61 | 'methods': [], |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | id_obj = creds['auth']['identity'] |
| 66 | if token: |
| 67 | id_obj['methods'].append('token') |
| 68 | id_obj['token'] = { |
| 69 | 'id': token |
| 70 | } |
| 71 | if user and password: |
| 72 | id_obj['methods'].append('password') |
| 73 | id_obj['password'] = { |
| 74 | 'user': { |
| 75 | 'password': password, |
| 76 | } |
| 77 | } |
| 78 | if user_type == 'id': |
| 79 | id_obj['password']['user']['id'] = user |
| 80 | else: |
| 81 | id_obj['password']['user']['name'] = user |
Jamie Lennox | e5a95d4 | 2015-02-11 07:19:57 +0000 | [diff] [blame] | 82 | if user_domain is not None: |
| 83 | _domain = dict(name=user_domain) |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 84 | id_obj['password']['user']['domain'] = _domain |
Jamie Lennox | e5a95d4 | 2015-02-11 07:19:57 +0000 | [diff] [blame] | 85 | if project is not None: |
| 86 | _domain = dict(name=project_domain) |
| 87 | _project = dict(name=project, domain=_domain) |
| 88 | scope = dict(project=_project) |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 89 | creds['auth']['scope'] = scope |
| 90 | |
| 91 | body = json.dumps(creds) |
| 92 | resp, body = self.post(self.auth_url, body=body) |
| 93 | self.expected_success(201, resp.status) |
| 94 | return service_client.ResponseBody(resp, body) |
| 95 | |
| 96 | def request(self, method, url, extra_headers=False, headers=None, |
| 97 | body=None): |
| 98 | """A simple HTTP request interface.""" |
| 99 | if headers is None: |
| 100 | # Always accept 'json', for xml token client too. |
| 101 | # Because XML response is not easily |
| 102 | # converted to the corresponding JSON one |
| 103 | headers = self.get_headers(accept_type="json") |
| 104 | elif extra_headers: |
| 105 | try: |
| 106 | headers.update(self.get_headers(accept_type="json")) |
| 107 | except (ValueError, TypeError): |
| 108 | headers = self.get_headers(accept_type="json") |
| 109 | |
| 110 | resp, resp_body = self.raw_request(url, method, |
| 111 | headers=headers, body=body) |
| 112 | self._log_request(method, url, resp) |
| 113 | |
| 114 | if resp.status in [401, 403]: |
| 115 | resp_body = json.loads(resp_body) |
Masayuki Igawa | 29d554f | 2015-01-20 12:36:42 +0900 | [diff] [blame] | 116 | raise lib_exc.Unauthorized(resp_body['error']['message']) |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 117 | elif resp.status not in [200, 201, 204]: |
| 118 | raise exceptions.IdentityError( |
| 119 | 'Unexpected status code {0}'.format(resp.status)) |
| 120 | |
| 121 | return resp, json.loads(resp_body) |
| 122 | |
Jamie Lennox | e5a95d4 | 2015-02-11 07:19:57 +0000 | [diff] [blame] | 123 | def get_token(self, user, password, project=None, project_domain='Default', |
| 124 | user_domain='Default', auth_data=False): |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 125 | """ |
| 126 | :param user: username |
| 127 | Returns (token id, token data) for supplied credentials |
| 128 | """ |
Jamie Lennox | e5a95d4 | 2015-02-11 07:19:57 +0000 | [diff] [blame] | 129 | body = self.auth(user, password, project, user_type='name', |
| 130 | user_domain=user_domain, |
| 131 | project_domain=project_domain) |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 132 | |
| 133 | token = body.response.get('x-subject-token') |
| 134 | if auth_data: |
| 135 | return token, body['token'] |
| 136 | else: |
| 137 | return token |