blob: e73ad1d241c5603be64ea206a8e8ee6c682ac8be [file] [log] [blame]
Steven Hardybf70c5c2013-10-30 21:55:16 +00001# 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
Steven Hardyc234ada2013-12-10 17:00:41 +000013import datetime
14import re
Matthew Treinish96e9e882014-06-09 18:37:19 -040015
Masayuki Igawabfa07602015-01-20 18:47:17 +090016from tempest_lib import exceptions as lib_exc
17
Steven Hardybf70c5c2013-10-30 21:55:16 +000018from tempest.api.identity import base
19from tempest import clients
Andrea Frittoli878d5ab2015-01-30 13:22:50 +000020from tempest.common import cred_provider
Masayuki Igawaba7bcf62014-02-17 14:56:41 +090021from tempest.common.utils import data_utils
Matthew Treinishd5021a72014-01-09 18:42:51 +000022from tempest import config
Steven Hardyc234ada2013-12-10 17:00:41 +000023from tempest.openstack.common import timeutils
Masayuki Igawaba7bcf62014-02-17 14:56:41 +090024from tempest import test
Steven Hardybf70c5c2013-10-30 21:55:16 +000025
Matthew Treinishd5021a72014-01-09 18:42:51 +000026CONF = config.CONF
27
Steven Hardybf70c5c2013-10-30 21:55:16 +000028
Matthew Treinishdb2c5972014-01-31 22:18:59 +000029class BaseTrustsV3Test(base.BaseIdentityV3AdminTest):
Steven Hardybf70c5c2013-10-30 21:55:16 +000030
31 def setUp(self):
32 super(BaseTrustsV3Test, self).setUp()
33 # Use alt_username as the trustee
Matthew Treinishd5021a72014-01-09 18:42:51 +000034 if not CONF.identity_feature_enabled.trust:
35 raise self.skipException("Trusts aren't enabled")
36
Matthew Treinish5ba84e32014-01-29 16:52:57 +000037 self.trustee_username = CONF.identity.alt_username
Steven Hardybf70c5c2013-10-30 21:55:16 +000038 self.trust_id = None
Steven Hardy776f4572013-12-23 21:42:48 +000039
40 def tearDown(self):
41 if self.trust_id:
42 # Do the delete in tearDown not addCleanup - we want the test to
43 # fail in the event there is a bug which causes undeletable trusts
44 self.delete_trust()
45 super(BaseTrustsV3Test, self).tearDown()
Steven Hardybf70c5c2013-10-30 21:55:16 +000046
47 def create_trustor_and_roles(self):
48 # Get trustor project ID, use the admin project
Matthew Treinishdb2c5972014-01-31 22:18:59 +000049 self.trustor_project_name = self.client.tenant_name
Steven Hardybf70c5c2013-10-30 21:55:16 +000050 self.trustor_project_id = self.get_tenant_by_name(
51 self.trustor_project_name)['id']
52 self.assertIsNotNone(self.trustor_project_id)
53
54 # Create a trustor User
Masayuki Igawaba7bcf62014-02-17 14:56:41 +090055 self.trustor_username = data_utils.rand_name('user-')
Steven Hardybf70c5c2013-10-30 21:55:16 +000056 u_desc = self.trustor_username + 'description'
Steven Hardy68f95282014-01-10 17:40:31 +000057 u_email = self.trustor_username + '@testmail.xx'
Masayuki Igawaba7bcf62014-02-17 14:56:41 +090058 self.trustor_password = data_utils.rand_name('pass-')
David Kranzd8ccb792014-12-29 11:32:05 -050059 user = self.client.create_user(
Steven Hardybf70c5c2013-10-30 21:55:16 +000060 self.trustor_username,
61 description=u_desc,
62 password=self.trustor_password,
63 email=u_email,
64 project_id=self.trustor_project_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +000065 self.trustor_user_id = user['id']
66
67 # And two roles, one we'll delegate and one we won't
Masayuki Igawaba7bcf62014-02-17 14:56:41 +090068 self.delegated_role = data_utils.rand_name('DelegatedRole-')
69 self.not_delegated_role = data_utils.rand_name('NotDelegatedRole-')
Steven Hardybf70c5c2013-10-30 21:55:16 +000070
David Kranzd8ccb792014-12-29 11:32:05 -050071 role = self.client.create_role(self.delegated_role)
Steven Hardybf70c5c2013-10-30 21:55:16 +000072 self.delegated_role_id = role['id']
73
David Kranzd8ccb792014-12-29 11:32:05 -050074 role = self.client.create_role(self.not_delegated_role)
Steven Hardybf70c5c2013-10-30 21:55:16 +000075 self.not_delegated_role_id = role['id']
76
77 # Assign roles to trustor
Matthew Treinishdb2c5972014-01-31 22:18:59 +000078 self.client.assign_user_role(self.trustor_project_id,
79 self.trustor_user_id,
80 self.delegated_role_id)
81 self.client.assign_user_role(self.trustor_project_id,
82 self.trustor_user_id,
83 self.not_delegated_role_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +000084
85 # Get trustee user ID, use the demo user
Matthew Treinishdb2c5972014-01-31 22:18:59 +000086 trustee_username = self.non_admin_client.user
Steven Hardybf70c5c2013-10-30 21:55:16 +000087 self.trustee_user_id = self.get_user_by_name(trustee_username)['id']
88 self.assertIsNotNone(self.trustee_user_id)
89
90 # Initialize a new client with the trustor credentials
Andrea Frittoli878d5ab2015-01-30 13:22:50 +000091 creds = cred_provider.get_credentials(
Andrea Frittoli422fbdf2014-03-20 10:05:18 +000092 username=self.trustor_username,
93 password=self.trustor_password,
94 tenant_name=self.trustor_project_name)
Andrea Frittolic0978352015-02-06 15:57:40 +000095 os = clients.Manager(credentials=creds)
Matthew Treinishdb2c5972014-01-31 22:18:59 +000096 self.trustor_client = os.identity_v3_client
Steven Hardybf70c5c2013-10-30 21:55:16 +000097
Steven Hardy776f4572013-12-23 21:42:48 +000098 def cleanup_user_and_roles(self):
Steven Hardybf70c5c2013-10-30 21:55:16 +000099 if self.trustor_user_id:
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000100 self.client.delete_user(self.trustor_user_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000101 if self.delegated_role_id:
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000102 self.client.delete_role(self.delegated_role_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000103 if self.not_delegated_role_id:
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000104 self.client.delete_role(self.not_delegated_role_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000105
106 def create_trust(self, impersonate=True, expires=None):
107
David Kranzd8ccb792014-12-29 11:32:05 -0500108 trust_create = self.trustor_client.create_trust(
Steven Hardybf70c5c2013-10-30 21:55:16 +0000109 trustor_user_id=self.trustor_user_id,
110 trustee_user_id=self.trustee_user_id,
111 project_id=self.trustor_project_id,
112 role_names=[self.delegated_role],
113 impersonation=impersonate,
114 expires_at=expires)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000115 self.trust_id = trust_create['id']
116 return trust_create
117
118 def validate_trust(self, trust, impersonate=True, expires=None,
119 summary=False):
120 self.assertIsNotNone(trust['id'])
121 self.assertEqual(impersonate, trust['impersonation'])
Steven Hardyc234ada2013-12-10 17:00:41 +0000122 if expires is not None:
Yaroslav Lobankov94340b52015-02-17 22:15:37 +0300123 # Omit microseconds of the expiry time
124 trust_expires_at = re.sub(r'\.([0-9]){6}', '', trust['expires_at'])
125 self.assertEqual(expires, trust_expires_at)
Steven Hardyc234ada2013-12-10 17:00:41 +0000126 else:
127 self.assertIsNone(trust['expires_at'])
Steven Hardybf70c5c2013-10-30 21:55:16 +0000128 self.assertEqual(self.trustor_user_id, trust['trustor_user_id'])
129 self.assertEqual(self.trustee_user_id, trust['trustee_user_id'])
130 self.assertIn('v3/OS-TRUST/trusts', trust['links']['self'])
131 self.assertEqual(self.trustor_project_id, trust['project_id'])
132 if not summary:
133 self.assertEqual(self.delegated_role, trust['roles'][0]['name'])
134 self.assertEqual(1, len(trust['roles']))
135
136 def get_trust(self):
David Kranzd8ccb792014-12-29 11:32:05 -0500137 trust_get = self.trustor_client.get_trust(self.trust_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000138 return trust_get
139
140 def validate_role(self, role):
141 self.assertEqual(self.delegated_role_id, role['id'])
142 self.assertEqual(self.delegated_role, role['name'])
143 self.assertIn('v3/roles/%s' % self.delegated_role_id,
144 role['links']['self'])
145 self.assertNotEqual(self.not_delegated_role_id, role['id'])
146 self.assertNotEqual(self.not_delegated_role, role['name'])
147 self.assertNotIn('v3/roles/%s' % self.not_delegated_role_id,
148 role['links']['self'])
149
150 def check_trust_roles(self):
151 # Check we find the delegated role
David Kranzd8ccb792014-12-29 11:32:05 -0500152 roles_get = self.trustor_client.get_trust_roles(
Steven Hardybf70c5c2013-10-30 21:55:16 +0000153 self.trust_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000154 self.assertEqual(1, len(roles_get))
155 self.validate_role(roles_get[0])
156
David Kranzd8ccb792014-12-29 11:32:05 -0500157 role_get = self.trustor_client.get_trust_role(
Steven Hardybf70c5c2013-10-30 21:55:16 +0000158 self.trust_id, self.delegated_role_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000159 self.validate_role(role_get)
160
David Kranzd8ccb792014-12-29 11:32:05 -0500161 role_get = self.trustor_client.check_trust_role(
Steven Hardybf70c5c2013-10-30 21:55:16 +0000162 self.trust_id, self.delegated_role_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000163
164 # And that we don't find not_delegated_role
Masayuki Igawabfa07602015-01-20 18:47:17 +0900165 self.assertRaises(lib_exc.NotFound,
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000166 self.trustor_client.get_trust_role,
Steven Hardybf70c5c2013-10-30 21:55:16 +0000167 self.trust_id,
168 self.not_delegated_role_id)
169
Masayuki Igawabfa07602015-01-20 18:47:17 +0900170 self.assertRaises(lib_exc.NotFound,
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000171 self.trustor_client.check_trust_role,
Steven Hardybf70c5c2013-10-30 21:55:16 +0000172 self.trust_id,
173 self.not_delegated_role_id)
174
175 def delete_trust(self):
David Kranze9d2f422014-07-02 13:57:41 -0400176 self.trustor_client.delete_trust(self.trust_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900177 self.assertRaises(lib_exc.NotFound,
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000178 self.trustor_client.get_trust,
Steven Hardybf70c5c2013-10-30 21:55:16 +0000179 self.trust_id)
180 self.trust_id = None
181
182
183class TrustsV3TestJSON(BaseTrustsV3Test):
Steven Hardybf70c5c2013-10-30 21:55:16 +0000184
185 def setUp(self):
186 super(TrustsV3TestJSON, self).setUp()
187 self.create_trustor_and_roles()
Steven Hardy68f95282014-01-10 17:40:31 +0000188 self.addCleanup(self.cleanup_user_and_roles)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000189
Masayuki Igawaba7bcf62014-02-17 14:56:41 +0900190 @test.attr(type='smoke')
Steven Hardybf70c5c2013-10-30 21:55:16 +0000191 def test_trust_impersonate(self):
192 # Test case to check we can create, get and delete a trust
193 # updates are not supported for trusts
194 trust = self.create_trust()
195 self.validate_trust(trust)
196
197 trust_get = self.get_trust()
198 self.validate_trust(trust_get)
199
200 self.check_trust_roles()
201
Masayuki Igawaba7bcf62014-02-17 14:56:41 +0900202 @test.attr(type='smoke')
Steven Hardybf70c5c2013-10-30 21:55:16 +0000203 def test_trust_noimpersonate(self):
204 # Test case to check we can create, get and delete a trust
205 # with impersonation=False
206 trust = self.create_trust(impersonate=False)
207 self.validate_trust(trust, impersonate=False)
208
209 trust_get = self.get_trust()
210 self.validate_trust(trust_get, impersonate=False)
211
212 self.check_trust_roles()
213
Masayuki Igawaba7bcf62014-02-17 14:56:41 +0900214 @test.attr(type='smoke')
Steven Hardyc234ada2013-12-10 17:00:41 +0000215 def test_trust_expire(self):
216 # Test case to check we can create, get and delete a trust
217 # with an expiry specified
218 expires_at = timeutils.utcnow() + datetime.timedelta(hours=1)
Yaroslav Lobankov94340b52015-02-17 22:15:37 +0300219 # NOTE(ylobankov) In some cases the expiry time may be rounded up
220 # because of microseconds. For example, we have the following expiry
221 # time for a trust: 2015-02-17T17:34:01.907051Z. However, if we make
222 # a GET request on the trust, the response may contain the time
223 # rounded up to 2015-02-17T17:34:02.000000Z. That is why we should
224 # omit microseconds when creating a trust.
225 expires_str = timeutils.isotime(at=expires_at)
Steven Hardyc234ada2013-12-10 17:00:41 +0000226
227 trust = self.create_trust(expires=expires_str)
228 self.validate_trust(trust, expires=expires_str)
229
230 trust_get = self.get_trust()
231
232 self.validate_trust(trust_get, expires=expires_str)
233
234 self.check_trust_roles()
235
Masayuki Igawaba7bcf62014-02-17 14:56:41 +0900236 @test.attr(type='smoke')
Steven Hardybf70c5c2013-10-30 21:55:16 +0000237 def test_trust_expire_invalid(self):
238 # Test case to check we can check an invlaid expiry time
239 # is rejected with the correct error
240 # with an expiry specified
241 expires_str = 'bad.123Z'
Masayuki Igawa4b29e472015-02-16 10:41:54 +0900242 self.assertRaises(lib_exc.BadRequest,
Steven Hardybf70c5c2013-10-30 21:55:16 +0000243 self.create_trust,
244 expires=expires_str)
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000245
Masayuki Igawaba7bcf62014-02-17 14:56:41 +0900246 @test.attr(type='smoke')
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000247 def test_get_trusts_query(self):
248 self.create_trust()
David Kranzd8ccb792014-12-29 11:32:05 -0500249 trusts_get = self.trustor_client.get_trusts(
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000250 trustor_user_id=self.trustor_user_id)
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000251 self.assertEqual(1, len(trusts_get))
252 self.validate_trust(trusts_get[0], summary=True)
253
Masayuki Igawaba7bcf62014-02-17 14:56:41 +0900254 @test.attr(type='smoke')
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000255 def test_get_trusts_all(self):
256 self.create_trust()
David Kranzd8ccb792014-12-29 11:32:05 -0500257 trusts_get = self.client.get_trusts()
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000258 trusts = [t for t in trusts_get
259 if t['id'] == self.trust_id]
260 self.assertEqual(1, len(trusts))
261 self.validate_trust(trusts[0], summary=True)