blob: 58e2ab8c6103d9db9d5b5303d7abb29a4e0e8f78 [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
Doug Hellmann583ce2c2015-03-11 14:55:46 +000016from oslo_utils import timeutils
Masayuki Igawabfa07602015-01-20 18:47:17 +090017
Steven Hardybf70c5c2013-10-30 21:55:16 +000018from tempest.api.identity import base
19from tempest import clients
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010020from tempest.common import credentials_factory as common_creds
Fei Long Wangd39431f2015-05-14 11:30:48 +120021from tempest.common.utils import data_utils
Matthew Treinishd5021a72014-01-09 18:42:51 +000022from tempest import config
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050023from tempest.lib import exceptions as lib_exc
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
Steven Hardybf70c5c2013-10-30 21:55:16 +000037 self.trust_id = None
Steven Hardy776f4572013-12-23 21:42:48 +000038
39 def tearDown(self):
40 if self.trust_id:
41 # Do the delete in tearDown not addCleanup - we want the test to
42 # fail in the event there is a bug which causes undeletable trusts
43 self.delete_trust()
44 super(BaseTrustsV3Test, self).tearDown()
Steven Hardybf70c5c2013-10-30 21:55:16 +000045
46 def create_trustor_and_roles(self):
Jamie Lennoxf8507b42015-02-23 06:17:57 +000047 # create a project that trusts will be granted on
Ken'ichi Ohmichi80369a92015-04-06 23:41:14 +000048 self.trustor_project_name = data_utils.rand_name(name='project')
Yaroslav Lobankov47a93ab2016-02-07 16:32:49 -060049 project = self.projects_client.create_project(
50 self.trustor_project_name, domain_id='default')['project']
Jamie Lennoxf8507b42015-02-23 06:17:57 +000051 self.trustor_project_id = project['id']
Steven Hardybf70c5c2013-10-30 21:55:16 +000052 self.assertIsNotNone(self.trustor_project_id)
53
54 # Create a trustor User
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000055 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'
Zack Feldsteind8c5f7a2015-12-14 10:44:07 -060058 self.trustor_password = data_utils.rand_password()
Daniel Mellado7aea5342016-02-09 09:10:12 +000059 user = self.users_client.create_user(
ghanshyam7f817db2016-08-01 18:37:13 +090060 name=self.trustor_username,
Steven Hardybf70c5c2013-10-30 21:55:16 +000061 description=u_desc,
62 password=self.trustor_password,
63 email=u_email,
Jamie Lennoxf8507b42015-02-23 06:17:57 +000064 project_id=self.trustor_project_id,
John Warren56317e02015-08-12 20:48:32 +000065 domain_id='default')['user']
Steven Hardybf70c5c2013-10-30 21:55:16 +000066 self.trustor_user_id = user['id']
67
68 # And two roles, one we'll delegate and one we won't
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000069 self.delegated_role = data_utils.rand_name('DelegatedRole')
70 self.not_delegated_role = data_utils.rand_name('NotDelegatedRole')
Steven Hardybf70c5c2013-10-30 21:55:16 +000071
Arx Cruz24bcb882016-02-10 15:20:16 +010072 role = self.roles_client.create_role(name=self.delegated_role)['role']
Steven Hardybf70c5c2013-10-30 21:55:16 +000073 self.delegated_role_id = role['id']
74
Arx Cruz24bcb882016-02-10 15:20:16 +010075 role = self.roles_client.create_role(
76 name=self.not_delegated_role)['role']
Steven Hardybf70c5c2013-10-30 21:55:16 +000077 self.not_delegated_role_id = role['id']
78
79 # Assign roles to trustor
Arx Cruz24bcb882016-02-10 15:20:16 +010080 self.roles_client.assign_user_role_on_project(
81 self.trustor_project_id,
82 self.trustor_user_id,
83 self.delegated_role_id)
84 self.roles_client.assign_user_role_on_project(
85 self.trustor_project_id,
86 self.trustor_user_id,
87 self.not_delegated_role_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +000088
89 # Get trustee user ID, use the demo user
Matthew Treinishdb2c5972014-01-31 22:18:59 +000090 trustee_username = self.non_admin_client.user
Steven Hardybf70c5c2013-10-30 21:55:16 +000091 self.trustee_user_id = self.get_user_by_name(trustee_username)['id']
92 self.assertIsNotNone(self.trustee_user_id)
93
94 # Initialize a new client with the trustor credentials
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010095 creds = common_creds.get_credentials(
Jamie Lennoxf8507b42015-02-23 06:17:57 +000096 identity_version='v3',
Andrea Frittoli422fbdf2014-03-20 10:05:18 +000097 username=self.trustor_username,
98 password=self.trustor_password,
Jamie Lennoxf8507b42015-02-23 06:17:57 +000099 user_domain_id='default',
100 tenant_name=self.trustor_project_name,
Andrea Frittoli (andreaf)100d18d2016-05-05 23:34:52 +0100101 project_domain_id='default',
102 domain_id='default')
Andrea Frittolic0978352015-02-06 15:57:40 +0000103 os = clients.Manager(credentials=creds)
Daniel Mellado76405392016-02-11 12:47:12 +0000104 self.trustor_client = os.trusts_client
Steven Hardybf70c5c2013-10-30 21:55:16 +0000105
Steven Hardy776f4572013-12-23 21:42:48 +0000106 def cleanup_user_and_roles(self):
Steven Hardybf70c5c2013-10-30 21:55:16 +0000107 if self.trustor_user_id:
Daniel Mellado7aea5342016-02-09 09:10:12 +0000108 self.users_client.delete_user(self.trustor_user_id)
Jamie Lennoxf8507b42015-02-23 06:17:57 +0000109 if self.trustor_project_id:
Yaroslav Lobankov47a93ab2016-02-07 16:32:49 -0600110 self.projects_client.delete_project(self.trustor_project_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000111 if self.delegated_role_id:
Arx Cruz24bcb882016-02-10 15:20:16 +0100112 self.roles_client.delete_role(self.delegated_role_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000113 if self.not_delegated_role_id:
Arx Cruz24bcb882016-02-10 15:20:16 +0100114 self.roles_client.delete_role(self.not_delegated_role_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000115
116 def create_trust(self, impersonate=True, expires=None):
117
David Kranzd8ccb792014-12-29 11:32:05 -0500118 trust_create = self.trustor_client.create_trust(
Steven Hardybf70c5c2013-10-30 21:55:16 +0000119 trustor_user_id=self.trustor_user_id,
120 trustee_user_id=self.trustee_user_id,
121 project_id=self.trustor_project_id,
piyush1107864d7553a2015-12-10 10:57:40 +0530122 roles=[{'name': self.delegated_role}],
Steven Hardybf70c5c2013-10-30 21:55:16 +0000123 impersonation=impersonate,
John Warren56317e02015-08-12 20:48:32 +0000124 expires_at=expires)['trust']
Steven Hardybf70c5c2013-10-30 21:55:16 +0000125 self.trust_id = trust_create['id']
126 return trust_create
127
128 def validate_trust(self, trust, impersonate=True, expires=None,
129 summary=False):
130 self.assertIsNotNone(trust['id'])
131 self.assertEqual(impersonate, trust['impersonation'])
Steven Hardyc234ada2013-12-10 17:00:41 +0000132 if expires is not None:
Yaroslav Lobankov8801baa2015-02-25 11:23:36 +0300133 # Omit microseconds component of the expiry time
Yaroslav Lobankov94340b52015-02-17 22:15:37 +0300134 trust_expires_at = re.sub(r'\.([0-9]){6}', '', trust['expires_at'])
135 self.assertEqual(expires, trust_expires_at)
Steven Hardyc234ada2013-12-10 17:00:41 +0000136 else:
137 self.assertIsNone(trust['expires_at'])
Steven Hardybf70c5c2013-10-30 21:55:16 +0000138 self.assertEqual(self.trustor_user_id, trust['trustor_user_id'])
139 self.assertEqual(self.trustee_user_id, trust['trustee_user_id'])
140 self.assertIn('v3/OS-TRUST/trusts', trust['links']['self'])
141 self.assertEqual(self.trustor_project_id, trust['project_id'])
142 if not summary:
143 self.assertEqual(self.delegated_role, trust['roles'][0]['name'])
144 self.assertEqual(1, len(trust['roles']))
145
Ken'ichi Ohmichi2c4cb0c2016-02-03 07:16:21 +0000146 def show_trust(self):
147 trust_get = self.trustor_client.show_trust(self.trust_id)['trust']
Steven Hardybf70c5c2013-10-30 21:55:16 +0000148 return trust_get
149
150 def validate_role(self, role):
151 self.assertEqual(self.delegated_role_id, role['id'])
152 self.assertEqual(self.delegated_role, role['name'])
153 self.assertIn('v3/roles/%s' % self.delegated_role_id,
154 role['links']['self'])
155 self.assertNotEqual(self.not_delegated_role_id, role['id'])
156 self.assertNotEqual(self.not_delegated_role, role['name'])
157 self.assertNotIn('v3/roles/%s' % self.not_delegated_role_id,
158 role['links']['self'])
159
160 def check_trust_roles(self):
161 # Check we find the delegated role
Ken'ichi Ohmichi2c4cb0c2016-02-03 07:16:21 +0000162 roles_get = self.trustor_client.list_trust_roles(
John Warren56317e02015-08-12 20:48:32 +0000163 self.trust_id)['roles']
Steven Hardybf70c5c2013-10-30 21:55:16 +0000164 self.assertEqual(1, len(roles_get))
165 self.validate_role(roles_get[0])
166
Ken'ichi Ohmichi2c4cb0c2016-02-03 07:16:21 +0000167 role_get = self.trustor_client.show_trust_role(
John Warren56317e02015-08-12 20:48:32 +0000168 self.trust_id, self.delegated_role_id)['role']
Steven Hardybf70c5c2013-10-30 21:55:16 +0000169 self.validate_role(role_get)
170
David Kranzd8ccb792014-12-29 11:32:05 -0500171 role_get = self.trustor_client.check_trust_role(
Steven Hardybf70c5c2013-10-30 21:55:16 +0000172 self.trust_id, self.delegated_role_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000173
174 # And that we don't find not_delegated_role
Masayuki Igawabfa07602015-01-20 18:47:17 +0900175 self.assertRaises(lib_exc.NotFound,
Ken'ichi Ohmichi2c4cb0c2016-02-03 07:16:21 +0000176 self.trustor_client.show_trust_role,
Steven Hardybf70c5c2013-10-30 21:55:16 +0000177 self.trust_id,
178 self.not_delegated_role_id)
179
Masayuki Igawabfa07602015-01-20 18:47:17 +0900180 self.assertRaises(lib_exc.NotFound,
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000181 self.trustor_client.check_trust_role,
Steven Hardybf70c5c2013-10-30 21:55:16 +0000182 self.trust_id,
183 self.not_delegated_role_id)
184
185 def delete_trust(self):
David Kranze9d2f422014-07-02 13:57:41 -0400186 self.trustor_client.delete_trust(self.trust_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900187 self.assertRaises(lib_exc.NotFound,
Ken'ichi Ohmichi2c4cb0c2016-02-03 07:16:21 +0000188 self.trustor_client.show_trust,
Steven Hardybf70c5c2013-10-30 21:55:16 +0000189 self.trust_id)
190 self.trust_id = None
191
192
193class TrustsV3TestJSON(BaseTrustsV3Test):
Steven Hardybf70c5c2013-10-30 21:55:16 +0000194
195 def setUp(self):
196 super(TrustsV3TestJSON, self).setUp()
197 self.create_trustor_and_roles()
Steven Hardy68f95282014-01-10 17:40:31 +0000198 self.addCleanup(self.cleanup_user_and_roles)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000199
Chris Hoge7579c1a2015-02-26 14:12:15 -0800200 @test.idempotent_id('5a0a91a4-baef-4a14-baba-59bf4d7fcace')
Steven Hardybf70c5c2013-10-30 21:55:16 +0000201 def test_trust_impersonate(self):
202 # Test case to check we can create, get and delete a trust
203 # updates are not supported for trusts
204 trust = self.create_trust()
205 self.validate_trust(trust)
206
Ken'ichi Ohmichi2c4cb0c2016-02-03 07:16:21 +0000207 trust_get = self.show_trust()
Steven Hardybf70c5c2013-10-30 21:55:16 +0000208 self.validate_trust(trust_get)
209
210 self.check_trust_roles()
211
Chris Hoge7579c1a2015-02-26 14:12:15 -0800212 @test.idempotent_id('ed2a8779-a7ac-49dc-afd7-30f32f936ed2')
Steven Hardybf70c5c2013-10-30 21:55:16 +0000213 def test_trust_noimpersonate(self):
214 # Test case to check we can create, get and delete a trust
215 # with impersonation=False
216 trust = self.create_trust(impersonate=False)
217 self.validate_trust(trust, impersonate=False)
218
Ken'ichi Ohmichi2c4cb0c2016-02-03 07:16:21 +0000219 trust_get = self.show_trust()
Steven Hardybf70c5c2013-10-30 21:55:16 +0000220 self.validate_trust(trust_get, impersonate=False)
221
222 self.check_trust_roles()
223
Chris Hoge7579c1a2015-02-26 14:12:15 -0800224 @test.idempotent_id('0ed14b66-cefd-4b5c-a964-65759453e292')
Steven Hardyc234ada2013-12-10 17:00:41 +0000225 def test_trust_expire(self):
226 # Test case to check we can create, get and delete a trust
227 # with an expiry specified
228 expires_at = timeutils.utcnow() + datetime.timedelta(hours=1)
Yaroslav Lobankov94340b52015-02-17 22:15:37 +0300229 # NOTE(ylobankov) In some cases the expiry time may be rounded up
Yaroslav Lobankov8801baa2015-02-25 11:23:36 +0300230 # because of microseconds. In fact, it depends on database and its
231 # version. At least MySQL 5.6.16 does this.
232 # For example, when creating a trust, we will set the expiry time of
233 # the trust to 2015-02-17T17:34:01.907051Z. However, if we make a GET
234 # request on the trust, the response will contain the time rounded up
235 # to 2015-02-17T17:34:02.000000Z. That is why we shouldn't set flag
236 # "subsecond" to True when we invoke timeutils.isotime(...) to avoid
237 # problems with rounding.
Yaroslav Lobankov94340b52015-02-17 22:15:37 +0300238 expires_str = timeutils.isotime(at=expires_at)
Steven Hardyc234ada2013-12-10 17:00:41 +0000239
240 trust = self.create_trust(expires=expires_str)
241 self.validate_trust(trust, expires=expires_str)
242
Ken'ichi Ohmichi2c4cb0c2016-02-03 07:16:21 +0000243 trust_get = self.show_trust()
Steven Hardyc234ada2013-12-10 17:00:41 +0000244
245 self.validate_trust(trust_get, expires=expires_str)
246
247 self.check_trust_roles()
248
Chris Hoge7579c1a2015-02-26 14:12:15 -0800249 @test.idempotent_id('3e48f95d-e660-4fa9-85e0-5a3d85594384')
Steven Hardybf70c5c2013-10-30 21:55:16 +0000250 def test_trust_expire_invalid(self):
nayna-patel1dfbedb2015-08-04 11:07:56 +0000251 # Test case to check we can check an invalid expiry time
Steven Hardybf70c5c2013-10-30 21:55:16 +0000252 # is rejected with the correct error
253 # with an expiry specified
254 expires_str = 'bad.123Z'
Masayuki Igawa4b29e472015-02-16 10:41:54 +0900255 self.assertRaises(lib_exc.BadRequest,
Steven Hardybf70c5c2013-10-30 21:55:16 +0000256 self.create_trust,
257 expires=expires_str)
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000258
Chris Hoge7579c1a2015-02-26 14:12:15 -0800259 @test.idempotent_id('6268b345-87ca-47c0-9ce3-37792b43403a')
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000260 def test_get_trusts_query(self):
261 self.create_trust()
Ken'ichi Ohmichi2c4cb0c2016-02-03 07:16:21 +0000262 trusts_get = self.trustor_client.list_trusts(
John Warren56317e02015-08-12 20:48:32 +0000263 trustor_user_id=self.trustor_user_id)['trusts']
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000264 self.assertEqual(1, len(trusts_get))
265 self.validate_trust(trusts_get[0], summary=True)
266
Masayuki Igawaba7bcf62014-02-17 14:56:41 +0900267 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800268 @test.idempotent_id('4773ebd5-ecbf-4255-b8d8-b63e6f72b65d')
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000269 def test_get_trusts_all(self):
Andrea Frittoli (andreaf)100d18d2016-05-05 23:34:52 +0100270
271 # Simple function that can be used for cleanup
272 def set_scope(auth_provider, scope):
273 auth_provider.scope = scope
274
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000275 self.create_trust()
Andrea Frittoli (andreaf)100d18d2016-05-05 23:34:52 +0100276 # Listing trusts can be done by trustor, by trustee, or without
277 # any filter if scoped to a project, so we must ensure token scope is
278 # project for this test.
279 original_scope = self.os_adm.auth_provider.scope
280 set_scope(self.os_adm.auth_provider, 'project')
281 self.addCleanup(set_scope, self.os_adm.auth_provider, original_scope)
Daniel Mellado76405392016-02-11 12:47:12 +0000282 trusts_get = self.trusts_client.list_trusts()['trusts']
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000283 trusts = [t for t in trusts_get
284 if t['id'] == self.trust_id]
285 self.assertEqual(1, len(trusts))
286 self.validate_trust(trusts[0], summary=True)