| 1 |
# Copyright (C) 2018 Red Hat, Inc. |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# License: GPL (version 3 or any later version). |
| 5 |
# See LICENSE for details. |
| 6 |
# --- END COPYRIGHT BLOCK --- |
| 7 |
# |
| 8 |
import pytest |
| 9 |
import re |
| 10 |
from ldap.controls import LDAPControl |
| 11 |
from lib389._constants import * |
| 12 |
from lib389.tasks import * |
| 13 |
from lib389.utils import * |
| 14 |
from test389.topologies import topology_st as topo |
| 15 |
from lib389.idm.user import UserAccounts, TEST_USER_PROPERTIES |
| 16 |
from lib389.idm.organizationalunit import OrganizationalUnits |
| 17 |
from lib389.pwpolicy import PwPolicyManager |
| 18 |
|
| 19 |
pytestmark = pytest.mark.tier1 |
| 20 |
|
| 21 |
DEBUGGING = os.getenv("DEBUGGING", default=False) |
| 22 |
if DEBUGGING: |
| 23 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 24 |
else: |
| 25 |
logging.getLogger(__name__).setLevel(logging.INFO) |
| 26 |
log = logging.getLogger(__name__) |
| 27 |
|
| 28 |
|
| 29 |
OLD_PASSWD = 'password' |
| 30 |
NEW_PASSWD = 'newpassword' |
| 31 |
SHORT_PASSWD = 'wd' |
| 32 |
TESTPEOPLE_OU = "TestPeople_bug834047" |
| 33 |
USER_ACI = '(targetattr="userpassword")(version 3.0; acl "pwp test"; allow (all) userdn="ldap:///self";)' |
| 34 |
|
| 35 |
|
| 36 |
@pytest.fixture(scope="function") |
| 37 |
def pwd_policy_setup(topo, request): |
| 38 |
""" |
| 39 |
Setup to set passwordStorageScheme as CLEAR |
| 40 |
passwordHistory to on |
| 41 |
passwordStorageScheme to SSHA |
| 42 |
passwordHistory off |
| 43 |
""" |
| 44 |
log.info("Change the pwd storage type to clear and change the password once to refresh it(for the rest of tests") |
| 45 |
topo.standalone.simple_bind_s(DN_DM, PASSWORD) |
| 46 |
topo.standalone.config.set('passwordStorageScheme', 'CLEAR') |
| 47 |
assert topo.standalone.passwd_s(user_2.dn, OLD_PASSWD, NEW_PASSWD) |
| 48 |
topo.standalone.config.set('passwordHistory', 'on') |
| 49 |
|
| 50 |
def fin(): |
| 51 |
topo.standalone.simple_bind_s(DN_DM, PASSWORD) |
| 52 |
topo.standalone.config.set('passwordStorageScheme', 'SSHA') |
| 53 |
topo.standalone.config.set('passwordHistory', 'off') |
| 54 |
request.addfinalizer(fin) |
| 55 |
|
| 56 |
|
| 57 |
def test_pwd_modify_with_different_operation(topo): |
| 58 |
"""Performing various password modify operation, |
| 59 |
make sure that password is actually modified |
| 60 |
|
| 61 |
:id: e36d68a8-0960-48e4-932c-6c2f64abaebc |
| 62 |
:setup: Standalone instance and TLS enabled |
| 63 |
:steps: |
| 64 |
1. Attempt for Password change for an entry that does not exists |
| 65 |
2. Attempt for Password change for an entry that exists |
| 66 |
3. Attempt for Password change to old for an entry that exists |
| 67 |
4. Attempt for Password Change with Binddn as testuser but with wrong old password |
| 68 |
5. Attempt for Password Change with Binddn as testuser |
| 69 |
6. Attempt for Password Change without giving newpassword |
| 70 |
7. Checking password change Operation using a Non-Secure connection |
| 71 |
8. Testuser attempts to change password for testuser2(userPassword attribute is Set) |
| 72 |
9. Directory Manager attempts to change password for testuser2(userPassword attribute is Set) |
| 73 |
10. Create a password syntax policy. Attempt to change to password that violates that policy |
| 74 |
11. userPassword mod with control results in ber decode error |
| 75 |
|
| 76 |
:expectedresults: |
| 77 |
1. Operation should be successful |
| 78 |
2. Operation should be successful |
| 79 |
3. Operation should be successful |
| 80 |
4. Operation should not be successful |
| 81 |
5. Operation should be successful |
| 82 |
6. Operation should be successful |
| 83 |
7. Operation should not be successful |
| 84 |
8. Operation should not be successful |
| 85 |
9. Operation should be successful |
| 86 |
10. Operation should violates the policy |
| 87 |
11. Operation should be successful |
| 88 |
""" |
| 89 |
|
| 90 |
topo.standalone.enable_tls() |
| 91 |
os.environ["LDAPTLS_CACERTDIR"] = topo.standalone.get_ssca_dir() |
| 92 |
users = UserAccounts(topo.standalone, DEFAULT_SUFFIX) |
| 93 |
TEST_USER_PROPERTIES['userpassword'] = OLD_PASSWD |
| 94 |
global user |
| 95 |
user = users.create(properties=TEST_USER_PROPERTIES) |
| 96 |
ous = OrganizationalUnits(topo.standalone, DEFAULT_SUFFIX) |
| 97 |
ou = ous.get('people') |
| 98 |
ou.add('aci', USER_ACI) |
| 99 |
|
| 100 |
with pytest.raises(ldap.NO_SUCH_OBJECT): |
| 101 |
log.info("Attempt for Password change for an entry that does not exists") |
| 102 |
assert topo.standalone.passwd_s('uid=testuser1,ou=People,dc=example,dc=com', OLD_PASSWD, NEW_PASSWD) |
| 103 |
log.info("Attempt for Password change for an entry that exists") |
| 104 |
assert topo.standalone.passwd_s(user.dn, OLD_PASSWD, NEW_PASSWD) |
| 105 |
log.info("Attempt for Password change to old for an entry that exists") |
| 106 |
assert topo.standalone.passwd_s(user.dn, NEW_PASSWD, OLD_PASSWD) |
| 107 |
log.info("Attempt for Password Change with Binddn as testuser but with wrong old password") |
| 108 |
topo.standalone.simple_bind_s(user.dn, OLD_PASSWD) |
| 109 |
with pytest.raises(ldap.INVALID_CREDENTIALS): |
| 110 |
topo.standalone.passwd_s(user.dn, NEW_PASSWD, NEW_PASSWD) |
| 111 |
log.info("Attempt for Password Change with Binddn as testuser") |
| 112 |
assert topo.standalone.passwd_s(user.dn, OLD_PASSWD, NEW_PASSWD) |
| 113 |
log.info("Attempt for Password Change without giving newpassword") |
| 114 |
assert topo.standalone.passwd_s(user.dn, None, OLD_PASSWD) |
| 115 |
assert user.get_attr_val_utf8('uid') == 'testuser' |
| 116 |
log.info("Change password to NEW_PASSWD i.e newpassword") |
| 117 |
assert topo.standalone.passwd_s(user.dn, None, NEW_PASSWD) |
| 118 |
assert topo.standalone.passwd_s(user.dn, NEW_PASSWD, None) |
| 119 |
log.info("Check binding with old/new password") |
| 120 |
password = [OLD_PASSWD, NEW_PASSWD] |
| 121 |
for pass_val in password: |
| 122 |
with pytest.raises(ldap.INVALID_CREDENTIALS): |
| 123 |
topo.standalone.simple_bind_s(user.dn, pass_val) |
| 124 |
log.info("Change password back to OLD_PASSWD i.e password") |
| 125 |
topo.standalone.simple_bind_s(DN_DM, PASSWORD) |
| 126 |
assert topo.standalone.passwd_s(user.dn, None, NEW_PASSWD) |
| 127 |
log.info("Checking password change Operation using a Non-Secure connection") |
| 128 |
conn = ldap.initialize("ldap://%s:%s" % (HOST_STANDALONE, PORT_STANDALONE)) |
| 129 |
with pytest.raises(ldap.CONFIDENTIALITY_REQUIRED): |
| 130 |
conn.passwd_s(user.dn, NEW_PASSWD, OLD_PASSWD) |
| 131 |
log.info("Testuser attempts to change password for testuser2(userPassword attribute is Set)") |
| 132 |
global user_2 |
| 133 |
users = UserAccounts(topo.standalone, DEFAULT_SUFFIX) |
| 134 |
user_2 = users.create(properties={ |
| 135 |
'uid': 'testuser2', |
| 136 |
'cn': 'testuser2', |
| 137 |
'sn': 'testuser2', |
| 138 |
'uidNumber': '3000', |
| 139 |
'gidNumber': '4000', |
| 140 |
'homeDirectory': '/home/testuser2', |
| 141 |
'userPassword': OLD_PASSWD |
| 142 |
}) |
| 143 |
|
| 144 |
topo.standalone.simple_bind_s(user.dn, NEW_PASSWD) |
| 145 |
with pytest.raises(ldap.INSUFFICIENT_ACCESS): |
| 146 |
assert topo.standalone.passwd_s(user_2.dn, OLD_PASSWD, NEW_PASSWD) |
| 147 |
log.info("Directory Manager attempts to change password for testuser2(userPassword attribute is Set)") |
| 148 |
topo.standalone.simple_bind_s(DN_DM, PASSWORD) |
| 149 |
assert topo.standalone.passwd_s(user_2.dn, OLD_PASSWD, NEW_PASSWD) |
| 150 |
log.info("Changing userPassword attribute to Undefined for testuser2") |
| 151 |
topo.standalone.modify_s(user_2.dn, [(ldap.MOD_REPLACE, 'userPassword', None)]) |
| 152 |
log.info("Testuser attempts to change password for testuser2(userPassword attribute is Undefined)") |
| 153 |
with pytest.raises(ldap.INSUFFICIENT_ACCESS): |
| 154 |
topo.standalone.simple_bind_s(user.dn, NEW_PASSWD) |
| 155 |
assert topo.standalone.passwd_s(user_2.dn, None, NEW_PASSWD) |
| 156 |
|
| 157 |
log.info("Reset password syntax policy") |
| 158 |
topo.standalone.simple_bind_s(DN_DM, PASSWORD) |
| 159 |
topo.standalone.config.set('PasswordCheckSyntax', 'off') |
| 160 |
log.info("userPassword mod with control results in ber decode error") |
| 161 |
assert topo.standalone.modify_ext_s(user.dn, [(ldap.MOD_REPLACE, 'userpassword', b'abcdefg')], |
| 162 |
serverctrls=[LDAPControl('2.16.840.1.113730.3.4.2', 1, None)]) |
| 163 |
log.info("Reseting the testuser's password") |
| 164 |
topo.standalone.passwd_s(user.dn, 'abcdefg', OLD_PASSWD) |
| 165 |
topo.standalone.passwd_s(user_2.dn, None, OLD_PASSWD) |
| 166 |
|
| 167 |
|
| 168 |
def test_pwd_modify_with_password_policy(topo, pwd_policy_setup): |
| 169 |
"""Performing various password modify operation, |
| 170 |
with passwordStorageScheme as CLEAR |
| 171 |
passwordHistory to on |
| 172 |
|
| 173 |
:id: 200bf0fd-20ab-4dde-849e-54067e98b917 |
| 174 |
:setup: Standalone instance (TLS enabled) with pwd_policy_setup |
| 175 |
:steps: |
| 176 |
1. Change the password and check that a new entry has been added to the history |
| 177 |
2. Try changing password to one stored in history |
| 178 |
3. Change the password several times in a row, and try binding after each change |
| 179 |
4. Try to bind using short password |
| 180 |
|
| 181 |
:expectedresults: |
| 182 |
1. Operation should be successful |
| 183 |
2. Operation should be unsuccessful |
| 184 |
3. Operation should be successful |
| 185 |
4. Operation should be unsuccessful |
| 186 |
""" |
| 187 |
log.info("Change the password and check that a new entry has been added to the history") |
| 188 |
topo.standalone.passwd_s(user_2.dn, NEW_PASSWD, OLD_PASSWD) |
| 189 |
regex = re.search('Z(.+)', user_2.get_attr_val_utf8('passwordhistory')) |
| 190 |
assert NEW_PASSWD == regex.group(1) |
| 191 |
|
| 192 |
log.info("Try changing password to one stored in history. Should fail") |
| 193 |
assert topo.standalone.simple_bind_s(user_2.dn, OLD_PASSWD) |
| 194 |
with pytest.raises(ldap.CONSTRAINT_VIOLATION): |
| 195 |
assert topo.standalone.passwd_s(user_2.dn, OLD_PASSWD, NEW_PASSWD) |
| 196 |
log.info("Change the password several times in a row, and try binding after each change") |
| 197 |
assert topo.standalone.simple_bind_s(user.dn, OLD_PASSWD) |
| 198 |
topo.standalone.passwd_s(user.dn, OLD_PASSWD, NEW_PASSWD) |
| 199 |
topo.standalone.passwd_s(user.dn, NEW_PASSWD, SHORT_PASSWD) |
| 200 |
with pytest.raises(ldap.CONSTRAINT_VIOLATION): |
| 201 |
topo.standalone.passwd_s(user.dn, SHORT_PASSWD, NEW_PASSWD) |
| 202 |
|
| 203 |
|
| 204 |
def test_pwd_modify_with_subsuffix(topo): |
| 205 |
"""Performing various password modify operation. |
| 206 |
|
| 207 |
:id: 2255b4e6-3546-4ec5-84a5-cd8b3d894ac5 |
| 208 |
:setup: Standalone instance (TLS enabled) |
| 209 |
:steps: |
| 210 |
1. Add a new SubSuffix & password policy |
| 211 |
2. Add two New users under the SubEntry |
| 212 |
3. Change password of uid=test_user0,ou=TestPeople_bug834047,dc=example,dc=com to newpassword |
| 213 |
4. Try to delete password- case when password is specified |
| 214 |
5. Try to delete password- case when password is not specified |
| 215 |
|
| 216 |
:expectedresults: |
| 217 |
1. Operation should be successful |
| 218 |
2. Operation should be successful |
| 219 |
3. Operation should be successful |
| 220 |
4. Operation should be successful |
| 221 |
5. Operation should be successful |
| 222 |
""" |
| 223 |
|
| 224 |
log.info("Add a new SubSuffix") |
| 225 |
topo.standalone.simple_bind_s(DN_DM, PASSWORD) |
| 226 |
|
| 227 |
ous = OrganizationalUnits(topo.standalone, DEFAULT_SUFFIX) |
| 228 |
ou_temp = ous.create(properties={'ou': TESTPEOPLE_OU}) |
| 229 |
ou_temp.add('aci', USER_ACI) |
| 230 |
|
| 231 |
log.info("Add the container & create password policies") |
| 232 |
policy = PwPolicyManager(topo.standalone) |
| 233 |
policy.create_subtree_policy(ou_temp.dn, properties={ |
| 234 |
'passwordHistory': 'on', |
| 235 |
'passwordInHistory': '6', |
| 236 |
'passwordChange': 'on', |
| 237 |
'passwordStorageScheme': 'CLEAR'}) |
| 238 |
|
| 239 |
log.info("Add two New users under the SubEntry") |
| 240 |
user = UserAccounts(topo.standalone, DEFAULT_SUFFIX, rdn='ou=TestPeople_bug834047') |
| 241 |
test_user0 = user.create(properties={ |
| 242 |
'uid': 'test_user0', |
| 243 |
'cn': 'test0', |
| 244 |
'sn': 'test0', |
| 245 |
'uidNumber': '3002', |
| 246 |
'gidNumber': '4002', |
| 247 |
'homeDirectory': '/home/test_user0', |
| 248 |
'userPassword': OLD_PASSWD |
| 249 |
}) |
| 250 |
|
| 251 |
test_user1 = user.create(properties={ |
| 252 |
'uid': 'test_user1', |
| 253 |
'cn': 'test1', |
| 254 |
'sn': 'test1', |
| 255 |
'uidNumber': '3003', |
| 256 |
'gidNumber': '4003', |
| 257 |
'homeDirectory': '/home/test_user3', |
| 258 |
'userPassword': OLD_PASSWD |
| 259 |
}) |
| 260 |
|
| 261 |
log.info("Changing password of {} to newpassword".format(test_user0.dn)) |
| 262 |
test_user0.rebind(OLD_PASSWD) |
| 263 |
test_user0.reset_password(NEW_PASSWD) |
| 264 |
test_user0.rebind(NEW_PASSWD) |
| 265 |
|
| 266 |
log.info("Try to delete password- case when password is specified") |
| 267 |
test_user0.remove('userPassword', NEW_PASSWD) |
| 268 |
|
| 269 |
test_user1.rebind(OLD_PASSWD) |
| 270 |
log.info("Try to delete password- case when password is not specified") |
| 271 |
test_user1.remove_all('userPassword') |
| 272 |
|
| 273 |
|
| 274 |
if __name__ == '__main__': |
| 275 |
# Run isolated |
| 276 |
# -s for DEBUG mode |
| 277 |
CURRENT_FILE = os.path.realpath(__file__) |
| 278 |
pytest.main(["-s", CURRENT_FILE]) |