| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2019 Red Hat, Inc. |
| 3 |
# All rights reserved. |
| 4 |
# |
| 5 |
# License: GPL (version 3 or any later version). |
| 6 |
# See LICENSE for details. |
| 7 |
# --- END COPYRIGHT BLOCK --- |
| 8 |
# |
| 9 |
import logging |
| 10 |
import time |
| 11 |
|
| 12 |
import ldap |
| 13 |
import pytest |
| 14 |
from lib389.utils import * |
| 15 |
from lib389._constants import * |
| 16 |
from lib389.pwpolicy import PwPolicyManager |
| 17 |
from test389.topologies import topology_st |
| 18 |
from lib389.idm.organizationalunit import OrganizationalUnits |
| 19 |
from lib389.idm.user import UserAccounts, TEST_USER_PROPERTIES |
| 20 |
|
| 21 |
pytestmark = pytest.mark.tier1 |
| 22 |
|
| 23 |
logging.getLogger(__name__).setLevel(logging.INFO) |
| 24 |
log = logging.getLogger(__name__) |
| 25 |
|
| 26 |
OU_PEOPLE = 'ou=People,' + DEFAULT_SUFFIX |
| 27 |
ATTR_INHERIT_GLOBAL = 'nsslapd-pwpolicy-inherit-global' |
| 28 |
ATTR_CHECK_SYNTAX = 'passwordCheckSyntax' |
| 29 |
|
| 30 |
BN = 'uid=buser,' + OU_PEOPLE |
| 31 |
TEMP_USER = 'cn=test{}' |
| 32 |
TEMP_USER_DN = '%s,%s' % (TEMP_USER, OU_PEOPLE) |
| 33 |
|
| 34 |
|
| 35 |
@pytest.fixture(scope="module") |
| 36 |
def create_user(topology_st, request): |
| 37 |
"""User for binding operation""" |
| 38 |
|
| 39 |
log.info('Adding user {}'.format(BN)) |
| 40 |
|
| 41 |
users = UserAccounts(topology_st.standalone, OU_PEOPLE, rdn=None) |
| 42 |
user_props = TEST_USER_PROPERTIES.copy() |
| 43 |
user_props.update({'uid': 'buser', 'cn': 'buser', 'userpassword': PASSWORD}) |
| 44 |
user = users.create(properties=user_props) |
| 45 |
|
| 46 |
log.info('Adding an aci for the bind user') |
| 47 |
BN_ACI = '(targetattr="*")(version 3.0; acl "pwp test"; allow (all) userdn="ldap:///%s";)' % user.dn |
| 48 |
ous = OrganizationalUnits(topology_st.standalone, DEFAULT_SUFFIX) |
| 49 |
ou_people = ous.get('people') |
| 50 |
ou_people.add('aci', BN_ACI) |
| 51 |
|
| 52 |
def fin(): |
| 53 |
log.info('Deleting user {}'.format(BN)) |
| 54 |
user.delete() |
| 55 |
ous = OrganizationalUnits(topology_st.standalone, DEFAULT_SUFFIX) |
| 56 |
ou_people = ous.get('people') |
| 57 |
ou_people.remove('aci', BN_ACI) |
| 58 |
|
| 59 |
request.addfinalizer(fin) |
| 60 |
|
| 61 |
|
| 62 |
@pytest.fixture(scope="module") |
| 63 |
def password_policy(topology_st, create_user): |
| 64 |
"""Set global password policy. |
| 65 |
Then, set fine-grained subtree level password policy |
| 66 |
to ou=People with no password syntax. |
| 67 |
|
| 68 |
Note: do not touch nsslapd-pwpolicy-inherit-global -- off by default |
| 69 |
""" |
| 70 |
|
| 71 |
log.info('Enable fine-grained policy') |
| 72 |
pwp = PwPolicyManager(topology_st.standalone) |
| 73 |
policy_props = { |
| 74 |
'passwordMustChange': 'off', |
| 75 |
'passwordExp': 'off', |
| 76 |
'passwordMinAge': '0', |
| 77 |
'passwordChange': 'off', |
| 78 |
'passwordStorageScheme': 'ssha' |
| 79 |
} |
| 80 |
pwp.create_subtree_policy(OU_PEOPLE, policy_props) |
| 81 |
check_attr_val(topology_st.standalone, ATTR_INHERIT_GLOBAL, 'off') |
| 82 |
check_attr_val(topology_st.standalone, ATTR_CHECK_SYNTAX, 'off') |
| 83 |
|
| 84 |
|
| 85 |
def check_attr_val(inst, attr, expected): |
| 86 |
"""Check that entry has the value""" |
| 87 |
|
| 88 |
val = inst.config.get_attr_val_utf8(attr) |
| 89 |
assert val == expected, 'Default value of %s is not %s, but %s' % ( |
| 90 |
attr, expected, val) |
| 91 |
|
| 92 |
log.info('Default value of %s is %s' % (attr, expected)) |
| 93 |
|
| 94 |
|
| 95 |
@pytest.mark.parametrize('inherit_value,checksyntax_value', |
| 96 |
[('off', 'off'), ('on', 'off'), ('off', 'on')]) |
| 97 |
def test_entry_has_no_restrictions(topology_st, password_policy, create_user, |
| 98 |
inherit_value, checksyntax_value): |
| 99 |
"""Make sure an entry added to ou=people has no password syntax restrictions |
| 100 |
|
| 101 |
:id: 2f07ff40-76ca-45a9-a556-331c94084945 |
| 102 |
:parametrized: yes |
| 103 |
:setup: Standalone instance, test user, |
| 104 |
password policy entries for a subtree |
| 105 |
:steps: |
| 106 |
1. Bind as test user |
| 107 |
2. Set 'nsslapd-pwpolicy-inherit-global' and |
| 108 |
'passwordCheckSyntax' accordingly: |
| 109 |
'off' and 'off'; 'on' and 'off'; 'off' and 'on' |
| 110 |
3. Try to add user with a short password |
| 111 |
4. Cleanup - remove temp user bound as DM |
| 112 |
:expectedresults: |
| 113 |
1. Bind should be successful |
| 114 |
2. Attributes should be successfully set |
| 115 |
3. No exceptions should occur |
| 116 |
4. Operation should be successful |
| 117 |
""" |
| 118 |
|
| 119 |
log.info('Set {} to {}'.format(ATTR_INHERIT_GLOBAL, inherit_value)) |
| 120 |
log.info('Set {} to {}'.format(ATTR_CHECK_SYNTAX, checksyntax_value)) |
| 121 |
topology_st.standalone.config.set(ATTR_INHERIT_GLOBAL, inherit_value) |
| 122 |
topology_st.standalone.config.set(ATTR_CHECK_SYNTAX, checksyntax_value) |
| 123 |
|
| 124 |
# Wait a second for cn=config to apply |
| 125 |
time.sleep(1) |
| 126 |
check_attr_val(topology_st.standalone, ATTR_INHERIT_GLOBAL, inherit_value) |
| 127 |
check_attr_val(topology_st.standalone, ATTR_CHECK_SYNTAX, checksyntax_value) |
| 128 |
|
| 129 |
log.info('Bind as test user') |
| 130 |
topology_st.standalone.simple_bind_s(BN, PASSWORD) |
| 131 |
|
| 132 |
log.info('Make sure an entry added to ou=people has ' |
| 133 |
'no password syntax restrictions.') |
| 134 |
|
| 135 |
users = UserAccounts(topology_st.standalone, OU_PEOPLE, rdn=None) |
| 136 |
user_props = TEST_USER_PROPERTIES.copy() |
| 137 |
user_props.update({'cn': 'test0', 'userpassword': 'short'}) |
| 138 |
user = users.create(properties=user_props) |
| 139 |
|
| 140 |
topology_st.standalone.simple_bind_s(DN_DM, PASSWORD) |
| 141 |
|
| 142 |
# Remove test user |
| 143 |
user.delete() |
| 144 |
|
| 145 |
|
| 146 |
def test_entry_has_restrictions(topology_st, password_policy, create_user): |
| 147 |
"""Set 'nsslapd-pwpolicy-inherit-global: on' and 'passwordCheckSyntax: on'. |
| 148 |
Make sure that syntax rules work, if set them at both: cn=config and |
| 149 |
ou=people policy container. |
| 150 |
|
| 151 |
:id: 4bb0f474-17c1-40f7-aab4-4ddc17d019e8 |
| 152 |
:setup: Standalone instance, test user, |
| 153 |
password policy entries for a subtree |
| 154 |
:steps: |
| 155 |
1. Bind as test user |
| 156 |
2. Switch 'nsslapd-pwpolicy-inherit-global: on' |
| 157 |
3. Switch 'passwordCheckSyntax: on' |
| 158 |
4. Set 'passwordMinLength: 9' to: |
| 159 |
cn=config and ou=people policy container |
| 160 |
5. Try to add user with a short password (<9) |
| 161 |
6. Try to add user with a long password (>9) |
| 162 |
7. Cleanup - remove temp users bound as DM |
| 163 |
:expectedresults: |
| 164 |
1. Bind should be successful |
| 165 |
2. nsslapd-pwpolicy-inherit-global should be successfully set |
| 166 |
3. passwordCheckSyntax should be successfully set |
| 167 |
4. passwordMinLength should be successfully set |
| 168 |
5. User should be rejected |
| 169 |
6. User should be rejected |
| 170 |
7. Operation should be successful |
| 171 |
""" |
| 172 |
|
| 173 |
log.info('Set {} to {}'.format(ATTR_INHERIT_GLOBAL, 'on')) |
| 174 |
log.info('Set {} to {}'.format(ATTR_CHECK_SYNTAX, 'on')) |
| 175 |
topology_st.standalone.config.set(ATTR_INHERIT_GLOBAL, 'on') |
| 176 |
topology_st.standalone.config.set(ATTR_CHECK_SYNTAX, 'on') |
| 177 |
|
| 178 |
pwp = PwPolicyManager(topology_st.standalone) |
| 179 |
policy = pwp.get_pwpolicy_entry(OU_PEOPLE) |
| 180 |
policy.set('passwordMinLength', '9') |
| 181 |
|
| 182 |
# Wait a second for cn=config to apply |
| 183 |
time.sleep(1) |
| 184 |
check_attr_val(topology_st.standalone, ATTR_INHERIT_GLOBAL, 'on') |
| 185 |
check_attr_val(topology_st.standalone, ATTR_CHECK_SYNTAX, 'on') |
| 186 |
|
| 187 |
log.info('Bind as test user') |
| 188 |
topology_st.standalone.simple_bind_s(BN, PASSWORD) |
| 189 |
users = UserAccounts(topology_st.standalone, OU_PEOPLE, rdn=None) |
| 190 |
user_props = TEST_USER_PROPERTIES.copy() |
| 191 |
|
| 192 |
log.info('Try to add user with a short password (<9)') |
| 193 |
with pytest.raises(ldap.CONSTRAINT_VIOLATION): |
| 194 |
user_props.update({'cn': 'test0', 'userpassword': 'short'}) |
| 195 |
user = users.create(properties=user_props) |
| 196 |
|
| 197 |
log.info('Try to add user with a long password (>9)') |
| 198 |
user_props.update({'cn': 'test1', 'userpassword': 'Reallylong1'}) |
| 199 |
user = users.create(properties=user_props) |
| 200 |
|
| 201 |
log.info('Bind as DM user') |
| 202 |
topology_st.standalone.simple_bind_s(DN_DM, PASSWORD) |
| 203 |
|
| 204 |
# Remove test user 1 |
| 205 |
user.delete() |
| 206 |
|
| 207 |
|
| 208 |
if __name__ == '__main__': |
| 209 |
# Run isolated |
| 210 |
# -s for DEBUG mode |
| 211 |
CURRENT_FILE = os.path.realpath(__file__) |
| 212 |
pytest.main("-s %s" % CURRENT_FILE) |