| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2024 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 ldap |
| 10 |
import logging |
| 11 |
import pytest |
| 12 |
import os |
| 13 |
import time |
| 14 |
from test389.topologies import topology_st as topo |
| 15 |
from lib389._constants import ( |
| 16 |
DEFAULT_SUFFIX, |
| 17 |
DN_CONFIG, |
| 18 |
DN_PLUGIN, |
| 19 |
LOG_DEFAULT, |
| 20 |
LOG_PLUGIN, |
| 21 |
PASSWORD, |
| 22 |
PLUGIN_ACCT_POLICY, |
| 23 |
) |
| 24 |
from lib389.idm.user import (UserAccount) |
| 25 |
from lib389.plugins import (AccountPolicyPlugin, AccountPolicyConfig) |
| 26 |
from lib389.cos import (CosTemplate, CosPointerDefinition) |
| 27 |
from lib389.idm.domain import Domain |
| 28 |
|
| 29 |
|
| 30 |
log = logging.getLogger(__name__) |
| 31 |
|
| 32 |
ACCPOL_DN = "cn={},{}".format(PLUGIN_ACCT_POLICY, DN_PLUGIN) |
| 33 |
ACCP_CONF = "{},{}".format(DN_CONFIG, ACCPOL_DN) |
| 34 |
TEST_ENTRY_NAME = 'actpol_test' |
| 35 |
TEST_ENTRY_DN = 'uid={},{}'.format(TEST_ENTRY_NAME, DEFAULT_SUFFIX) |
| 36 |
NEW_PASSWORD = 'password123' |
| 37 |
USER_SELF_MOD_ACI = '(targetattr="userpassword")(version 3.0; acl "pwp test"; allow (all) userdn="ldap:///self";)' |
| 38 |
ANON_ACI = "(targetattr=\"*\")(version 3.0; acl \"Anonymous Read access\"; allow (read,search,compare) userdn = \"ldap:///anyone\";)" |
| 39 |
|
| 40 |
|
| 41 |
def test_inactivty_and_expiration(topo): |
| 42 |
"""Test account expiration works when we are checking all state attributes |
| 43 |
|
| 44 |
:id: 704310de-a2eb-4ee7-baf3-9770c0fbf07c |
| 45 |
:setup: Standalone Instance |
| 46 |
:steps: |
| 47 |
1. Configure instance for password expiration |
| 48 |
2. Add ACI to allow users to update themselves |
| 49 |
3. Create test user |
| 50 |
4. Reset users password to set passwordExpirationtime |
| 51 |
5. Configure account policy plugin and restart |
| 52 |
6. Bind as test user to reset lastLoginTime |
| 53 |
7. Sleep, then bind as user which triggers error |
| 54 |
|
| 55 |
:expectedresults: |
| 56 |
1. Success |
| 57 |
2. Success |
| 58 |
3. Success |
| 59 |
4. Success |
| 60 |
5. Success |
| 61 |
6. Success |
| 62 |
7. Success |
| 63 |
""" |
| 64 |
|
| 65 |
# passwordMaxAge should be less than accountInactivityLimit divided by 2 |
| 66 |
INACTIVITY_LIMIT = 12 |
| 67 |
MAX_AGE = 2 |
| 68 |
|
| 69 |
# Configure instance |
| 70 |
inst = topo.standalone |
| 71 |
inst.config.set('passwordexp', 'on') |
| 72 |
inst.config.set('passwordmaxage', str(MAX_AGE)) |
| 73 |
inst.config.set('passwordGraceLimit', '5') |
| 74 |
inst.config.set('nsslapd-errorlog-level', str(LOG_PLUGIN + LOG_DEFAULT)) |
| 75 |
|
| 76 |
# Add aci so user and update password |
| 77 |
suffix = Domain(inst, DEFAULT_SUFFIX) |
| 78 |
suffix.add('aci', USER_SELF_MOD_ACI) |
| 79 |
suffix.add('aci', ANON_ACI) |
| 80 |
|
| 81 |
# Create the test user |
| 82 |
test_user = UserAccount(inst, TEST_ENTRY_DN) |
| 83 |
test_user.create(properties={ |
| 84 |
'uid': TEST_ENTRY_NAME, |
| 85 |
'cn': TEST_ENTRY_NAME, |
| 86 |
'sn': TEST_ENTRY_NAME, |
| 87 |
'userPassword': PASSWORD, |
| 88 |
'uidNumber': '1000', |
| 89 |
'gidNumber': '2000', |
| 90 |
'homeDirectory': '/home/test', |
| 91 |
}) |
| 92 |
|
| 93 |
# Configure account policy plugin |
| 94 |
plugin = AccountPolicyPlugin(inst) |
| 95 |
plugin.enable() |
| 96 |
plugin.set('nsslapd-pluginarg0', ACCP_CONF) |
| 97 |
accp = AccountPolicyConfig(inst, dn=ACCP_CONF) |
| 98 |
accp.set('alwaysrecordlogin', 'yes') |
| 99 |
accp.set('stateattrname', 'lastLoginTime') |
| 100 |
accp.set('altstateattrname', 'passwordexpirationtime') |
| 101 |
accp.set('specattrname', 'acctPolicySubentry') |
| 102 |
accp.set('limitattrname', 'accountInactivityLimit') |
| 103 |
accp.set('accountInactivityLimit', str(INACTIVITY_LIMIT)) |
| 104 |
accp.set('checkAllStateAttrs', 'on') |
| 105 |
inst.restart() |
| 106 |
|
| 107 |
# Reset test user password to reset passwordExpirationtime |
| 108 |
conn = test_user.bind(PASSWORD) |
| 109 |
test_user = UserAccount(conn, TEST_ENTRY_DN) |
| 110 |
test_user.reset_password(NEW_PASSWORD) |
| 111 |
|
| 112 |
# Sleep a little bit, we'll sleep the remaining time later |
| 113 |
time.sleep(INACTIVITY_LIMIT / 2) |
| 114 |
|
| 115 |
# Bind as test user to reset lastLoginTime |
| 116 |
test_user.bind(NEW_PASSWORD) |
| 117 |
|
| 118 |
# Sleep the remaining time plus extra double passwordMaxAge seconds |
| 119 |
time.sleep(INACTIVITY_LIMIT / 2 + MAX_AGE * 2) |
| 120 |
|
| 121 |
# Try to bind, but password expiration should reject this as lastLogintTime |
| 122 |
# has not exceeded the inactivity limit |
| 123 |
with pytest.raises(ldap.CONSTRAINT_VIOLATION): |
| 124 |
test_user.bind(NEW_PASSWORD) |
| 125 |
|
| 126 |
|
| 127 |
def test_alwaysrecordlogin_off(topo): |
| 128 |
"""Test the server does not crash when alwaysrecordlogin is "off" |
| 129 |
|
| 130 |
:id: 49eb0993-ee59-48a9-8324-fb965b202ba9 |
| 131 |
:setup: Standalone Instance |
| 132 |
:steps: |
| 133 |
1. Create test user |
| 134 |
2. Configure account policy, COS, and restart |
| 135 |
3. Bind as test user |
| 136 |
:expectedresults: |
| 137 |
1. Success |
| 138 |
2. Success |
| 139 |
3. Success |
| 140 |
""" |
| 141 |
|
| 142 |
LOCAL_POLICY = 'cn=Account Inactivation Policy,dc=example,dc=com' |
| 143 |
TEMPL_COS = 'cn=TempltCoS,ou=people,dc=example,dc=com' |
| 144 |
DEFIN_COS = 'cn=DefnCoS,ou=people,dc=example,dc=com' |
| 145 |
TEST_USER_NAME = 'crash' |
| 146 |
TEST_USER_DN = f'uid={TEST_USER_NAME},ou=people,{DEFAULT_SUFFIX}' |
| 147 |
|
| 148 |
inst = topo.standalone |
| 149 |
|
| 150 |
# Create the test user |
| 151 |
test_user = UserAccount(inst, TEST_USER_DN) |
| 152 |
test_user.create(properties={ |
| 153 |
'uid': TEST_USER_NAME, |
| 154 |
'cn': TEST_USER_NAME, |
| 155 |
'sn': TEST_USER_NAME, |
| 156 |
'userPassword': PASSWORD, |
| 157 |
'uidNumber': '1000', |
| 158 |
'gidNumber': '2000', |
| 159 |
'homeDirectory': '/home/crash', |
| 160 |
}) |
| 161 |
|
| 162 |
# Configure account policy plugin |
| 163 |
plugin = AccountPolicyPlugin(inst) |
| 164 |
plugin.enable() |
| 165 |
plugin.set('nsslapd-pluginarg0', ACCP_CONF) |
| 166 |
accp = AccountPolicyConfig(inst, dn=ACCP_CONF) |
| 167 |
accp.set('alwaysrecordlogin', 'no') |
| 168 |
accp.set('stateattrname', 'lastLoginTime') |
| 169 |
accp.set('altstateattrname', 'passwordexpirationtime') |
| 170 |
accp.set('specattrname', 'acctPolicySubentry') |
| 171 |
accp.set('limitattrname', 'accountInactivityLimit') |
| 172 |
accp.set('accountInactivityLimit', '123456') |
| 173 |
accp.set('checkAllStateAttrs', 'on') |
| 174 |
inst.restart() |
| 175 |
# Local policy |
| 176 |
laccp = AccountPolicyConfig(inst, dn=LOCAL_POLICY) |
| 177 |
laccp.create(properties={ |
| 178 |
'cn': 'Account Inactivation Policy', |
| 179 |
'accountInactivityLimit': '12312321' |
| 180 |
}) |
| 181 |
# COS |
| 182 |
cos_template = CosTemplate(inst, dn=TEMPL_COS) |
| 183 |
cos_template.create(properties={'cn': 'TempltCoS', |
| 184 |
'acctPolicySubentry': LOCAL_POLICY}) |
| 185 |
cos_def = CosPointerDefinition(inst, dn=DEFIN_COS) |
| 186 |
cos_def.create(properties={ |
| 187 |
'cn': 'DefnCoS', |
| 188 |
'cosTemplateDn': TEMPL_COS, |
| 189 |
'cosAttribute': 'acctPolicySubentry default operational-default'}) |
| 190 |
inst.restart() |
| 191 |
|
| 192 |
# Bind as test user to make sure the server does not crash |
| 193 |
conn = test_user.bind(PASSWORD) |
| 194 |
test_user = UserAccount(conn, TEST_USER_DN) |
| 195 |
test_user.bind(PASSWORD) |
| 196 |
|
| 197 |
|
| 198 |
if __name__ == '__main__': |
| 199 |
# Run isolated |
| 200 |
# -s for DEBUG mode |
| 201 |
CURRENT_FILE = os.path.realpath(__file__) |
| 202 |
pytest.main(["-s", CURRENT_FILE]) |