Jane Zadorozhna | 9c938c6 | 2015-07-01 17:06:16 +0300 | [diff] [blame] | 1 | # Copyright 2015 OpenStack Foundation |
| 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
Lance Bragstad | a2c4ebc | 2015-10-05 20:34:39 +0000 | [diff] [blame] | 16 | import time |
Jane Zadorozhna | 9c938c6 | 2015-07-01 17:06:16 +0300 | [diff] [blame] | 17 | |
Rodrigo Duarte Sousa | 2d78e8e | 2016-09-28 10:38:08 -0300 | [diff] [blame] | 18 | import testtools |
| 19 | |
Jane Zadorozhna | 9c938c6 | 2015-07-01 17:06:16 +0300 | [diff] [blame] | 20 | from tempest.api.identity import base |
Rodrigo Duarte Sousa | 2d78e8e | 2016-09-28 10:38:08 -0300 | [diff] [blame] | 21 | from tempest import config |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 22 | from tempest.lib.common.utils import data_utils |
Ken'ichi Ohmichi | 44f0127 | 2017-01-27 18:44:14 -0800 | [diff] [blame] | 23 | from tempest.lib import decorators |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 24 | from tempest.lib import exceptions |
Jane Zadorozhna | 9c938c6 | 2015-07-01 17:06:16 +0300 | [diff] [blame] | 25 | |
| 26 | |
Rodrigo Duarte Sousa | 2d78e8e | 2016-09-28 10:38:08 -0300 | [diff] [blame] | 27 | CONF = config.CONF |
| 28 | |
| 29 | |
Jane Zadorozhna | 9c938c6 | 2015-07-01 17:06:16 +0300 | [diff] [blame] | 30 | class IdentityV3UsersTest(base.BaseIdentityV3Test): |
zhufl | 8e3aacd | 2020-04-27 14:46:46 +0800 | [diff] [blame] | 31 | """Test identity user password""" |
Jane Zadorozhna | 9c938c6 | 2015-07-01 17:06:16 +0300 | [diff] [blame] | 32 | |
| 33 | @classmethod |
| 34 | def resource_setup(cls): |
| 35 | super(IdentityV3UsersTest, cls).resource_setup() |
Jordan Pittier | 8160d31 | 2017-04-18 11:52:23 +0200 | [diff] [blame] | 36 | cls.creds = cls.os_primary.credentials |
Jane Zadorozhna | 9c938c6 | 2015-07-01 17:06:16 +0300 | [diff] [blame] | 37 | cls.user_id = cls.creds.user_id |
Jane Zadorozhna | 9c938c6 | 2015-07-01 17:06:16 +0300 | [diff] [blame] | 38 | |
Rodrigo Duarte Sousa | 2d78e8e | 2016-09-28 10:38:08 -0300 | [diff] [blame] | 39 | def _update_password(self, original_password, password): |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 40 | self.non_admin_users_client.update_user_password( |
Rodrigo Duarte Sousa | 2d78e8e | 2016-09-28 10:38:08 -0300 | [diff] [blame] | 41 | self.user_id, |
| 42 | password=password, |
| 43 | original_password=original_password) |
Lance Bragstad | 144c2f4 | 2015-11-19 16:42:37 +0000 | [diff] [blame] | 44 | |
Morgan Fainberg | 5b2c745 | 2016-02-02 20:15:47 -0800 | [diff] [blame] | 45 | # NOTE(morganfainberg): Fernet tokens are not subsecond aware and |
| 46 | # Keystone should only be precise to the second. Sleep to ensure |
Yaroslav Lobankov | cbcb611 | 2016-03-08 12:30:01 -0600 | [diff] [blame] | 47 | # we are passing the second boundary. |
Lance Bragstad | a2c4ebc | 2015-10-05 20:34:39 +0000 | [diff] [blame] | 48 | time.sleep(1) |
| 49 | |
Jane Zadorozhna | 9c938c6 | 2015-07-01 17:06:16 +0300 | [diff] [blame] | 50 | # check authorization with new password |
Rodrigo Duarte Sousa | 2d78e8e | 2016-09-28 10:38:08 -0300 | [diff] [blame] | 51 | self.non_admin_token.auth(user_id=self.user_id, password=password) |
| 52 | |
| 53 | # Reset auth to get a new token with the new password |
| 54 | self.non_admin_users_client.auth_provider.clear_auth() |
| 55 | self.non_admin_users_client.auth_provider.credentials.password = ( |
| 56 | password) |
| 57 | |
| 58 | def _restore_password(self, old_pass, new_pass): |
| 59 | if CONF.identity_feature_enabled.security_compliance: |
| 60 | # First we need to clear the password history |
| 61 | unique_count = CONF.identity.user_unique_last_password_count |
zhufl | 8e9a073 | 2017-01-26 16:15:21 +0800 | [diff] [blame] | 62 | for _ in range(unique_count): |
Rodrigo Duarte Sousa | 2d78e8e | 2016-09-28 10:38:08 -0300 | [diff] [blame] | 63 | random_pass = data_utils.rand_password() |
| 64 | self._update_password( |
| 65 | original_password=new_pass, password=random_pass) |
| 66 | new_pass = random_pass |
| 67 | |
| 68 | self._update_password(original_password=new_pass, password=old_pass) |
| 69 | # Reset auth again to verify the password restore does work. |
| 70 | # Clear auth restores the original credentials and deletes |
| 71 | # cached auth data |
| 72 | self.non_admin_users_client.auth_provider.clear_auth() |
| 73 | # NOTE(lbragstad): Fernet tokens are not subsecond aware and |
| 74 | # Keystone should only be precise to the second. Sleep to ensure we |
| 75 | # are passing the second boundary before attempting to |
| 76 | # authenticate. |
| 77 | time.sleep(1) |
| 78 | self.non_admin_users_client.auth_provider.set_auth() |
| 79 | |
Ken'ichi Ohmichi | 44f0127 | 2017-01-27 18:44:14 -0800 | [diff] [blame] | 80 | @decorators.idempotent_id('ad71bd23-12ad-426b-bb8b-195d2b635f27') |
nicolas | c98a325 | 2018-12-17 13:06:02 -0800 | [diff] [blame] | 81 | @testtools.skipIf(CONF.identity_feature_enabled.immutable_user_source, |
| 82 | 'Skipped because environment has an ' |
| 83 | 'immutable user source and solely ' |
| 84 | 'provides read-only access to users.') |
Rodrigo Duarte Sousa | 2d78e8e | 2016-09-28 10:38:08 -0300 | [diff] [blame] | 85 | def test_user_update_own_password(self): |
zhufl | 8e3aacd | 2020-04-27 14:46:46 +0800 | [diff] [blame] | 86 | """Test updating user's own password""" |
Rodrigo Duarte Sousa | 2d78e8e | 2016-09-28 10:38:08 -0300 | [diff] [blame] | 87 | old_pass = self.creds.password |
| 88 | old_token = self.non_admin_client.token |
| 89 | new_pass = data_utils.rand_password() |
| 90 | |
Ken'ichi Ohmichi | 553d7cb | 2018-07-13 22:53:03 +0000 | [diff] [blame] | 91 | # to change password back. important for use_dynamic_credentials=false |
Rodrigo Duarte Sousa | 2d78e8e | 2016-09-28 10:38:08 -0300 | [diff] [blame] | 92 | self.addCleanup(self._restore_password, old_pass, new_pass) |
| 93 | |
| 94 | # user updates own password |
| 95 | self._update_password(original_password=old_pass, password=new_pass) |
Jane Zadorozhna | 9c938c6 | 2015-07-01 17:06:16 +0300 | [diff] [blame] | 96 | |
| 97 | # authorize with old token should lead to IdentityError (404 code) |
| 98 | self.assertRaises(exceptions.IdentityError, |
| 99 | self.non_admin_token.auth, |
Rodrigo Duarte Sousa | 2d78e8e | 2016-09-28 10:38:08 -0300 | [diff] [blame] | 100 | token=old_token) |
Jane Zadorozhna | 9c938c6 | 2015-07-01 17:06:16 +0300 | [diff] [blame] | 101 | |
| 102 | # authorize with old password should lead to Unauthorized |
| 103 | self.assertRaises(exceptions.Unauthorized, |
| 104 | self.non_admin_token.auth, |
| 105 | user_id=self.user_id, |
| 106 | password=old_pass) |
Rodrigo Duarte Sousa | 2d78e8e | 2016-09-28 10:38:08 -0300 | [diff] [blame] | 107 | |
| 108 | @testtools.skipUnless(CONF.identity_feature_enabled.security_compliance, |
| 109 | 'Security compliance not available.') |
Ken'ichi Ohmichi | 44f0127 | 2017-01-27 18:44:14 -0800 | [diff] [blame] | 110 | @decorators.idempotent_id('941784ee-5342-4571-959b-b80dd2cea516') |
nicolas | c98a325 | 2018-12-17 13:06:02 -0800 | [diff] [blame] | 111 | @testtools.skipIf(CONF.identity_feature_enabled.immutable_user_source, |
| 112 | 'Skipped because environment has an ' |
| 113 | 'immutable user source and solely ' |
| 114 | 'provides read-only access to users.') |
Rodrigo Duarte Sousa | 2d78e8e | 2016-09-28 10:38:08 -0300 | [diff] [blame] | 115 | def test_password_history_check_self_service_api(self): |
zhufl | 8e3aacd | 2020-04-27 14:46:46 +0800 | [diff] [blame] | 116 | """Test checking password changing history""" |
Rodrigo Duarte Sousa | 2d78e8e | 2016-09-28 10:38:08 -0300 | [diff] [blame] | 117 | old_pass = self.creds.password |
| 118 | new_pass1 = data_utils.rand_password() |
| 119 | new_pass2 = data_utils.rand_password() |
| 120 | |
| 121 | self.addCleanup(self._restore_password, old_pass, new_pass2) |
| 122 | |
| 123 | # Update password |
| 124 | self._update_password(original_password=old_pass, password=new_pass1) |
| 125 | |
| 126 | if CONF.identity.user_unique_last_password_count > 1: |
| 127 | # Can not reuse a previously set password |
| 128 | self.assertRaises(exceptions.BadRequest, |
| 129 | self.non_admin_users_client.update_user_password, |
| 130 | self.user_id, |
| 131 | password=new_pass1, |
| 132 | original_password=new_pass1) |
| 133 | |
| 134 | self.assertRaises(exceptions.BadRequest, |
| 135 | self.non_admin_users_client.update_user_password, |
| 136 | self.user_id, |
| 137 | password=old_pass, |
| 138 | original_password=new_pass1) |
| 139 | |
| 140 | # A different password can be set |
| 141 | self._update_password(original_password=new_pass1, password=new_pass2) |
| 142 | |
| 143 | @testtools.skipUnless(CONF.identity_feature_enabled.security_compliance, |
| 144 | 'Security compliance not available.') |
Ken'ichi Ohmichi | 44f0127 | 2017-01-27 18:44:14 -0800 | [diff] [blame] | 145 | @decorators.idempotent_id('a7ad8bbf-2cff-4520-8c1d-96332e151658') |
Rodrigo Duarte Sousa | 2d78e8e | 2016-09-28 10:38:08 -0300 | [diff] [blame] | 146 | def test_user_account_lockout(self): |
zhufl | 8e3aacd | 2020-04-27 14:46:46 +0800 | [diff] [blame] | 147 | """Test locking out user account after failure attempts""" |
zhufl | 270c25a | 2018-10-18 14:56:16 +0800 | [diff] [blame] | 148 | if (CONF.identity.user_lockout_failure_attempts <= 0 or |
| 149 | CONF.identity.user_lockout_duration <= 0): |
| 150 | raise self.skipException( |
| 151 | "Both CONF.identity.user_lockout_failure_attempts and " |
| 152 | "CONF.identity.user_lockout_duration should be greater than " |
| 153 | "zero to test this feature") |
| 154 | |
Rodrigo Duarte Sousa | 2d78e8e | 2016-09-28 10:38:08 -0300 | [diff] [blame] | 155 | password = self.creds.password |
| 156 | |
| 157 | # First, we login using the correct credentials |
| 158 | self.non_admin_token.auth(user_id=self.user_id, password=password) |
| 159 | |
| 160 | # Lock user account by using the wrong password to login |
| 161 | bad_password = data_utils.rand_password() |
zhufl | 8e9a073 | 2017-01-26 16:15:21 +0800 | [diff] [blame] | 162 | for _ in range(CONF.identity.user_lockout_failure_attempts): |
Rodrigo Duarte Sousa | 2d78e8e | 2016-09-28 10:38:08 -0300 | [diff] [blame] | 163 | self.assertRaises(exceptions.Unauthorized, |
| 164 | self.non_admin_token.auth, |
| 165 | user_id=self.user_id, |
| 166 | password=bad_password) |
| 167 | |
| 168 | # The user account must be locked, so now it is not possible to login |
| 169 | # even using the correct password |
| 170 | self.assertRaises(exceptions.Unauthorized, |
| 171 | self.non_admin_token.auth, |
| 172 | user_id=self.user_id, |
| 173 | password=password) |
| 174 | |
| 175 | # If we wait the required time, the user account will be unlocked |
| 176 | time.sleep(CONF.identity.user_lockout_duration + 1) |
Rodrigo Duarte Sousa | 36f6882 | 2017-01-05 16:05:00 -0300 | [diff] [blame] | 177 | self.non_admin_token.auth(user_id=self.user_id, password=password) |