| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2025 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 time |
| 11 |
import pytest |
| 12 |
import logging |
| 13 |
import os |
| 14 |
from datetime import datetime, timedelta |
| 15 |
|
| 16 |
from lib389 import DEFAULT_SUFFIX, DN_PLUGIN, DN_CONFIG |
| 17 |
from lib389.cli_idm.account import entry_status, unlock |
| 18 |
from test389.topologies import topology_st |
| 19 |
from lib389.cli_base import FakeArgs |
| 20 |
from lib389.utils import ds_is_older |
| 21 |
from lib389.plugins import AccountPolicyPlugin, AccountPolicyConfigs |
| 22 |
from lib389.idm.role import FilteredRoles |
| 23 |
from lib389.idm.user import UserAccounts |
| 24 |
from lib389.cos import CosTemplate, CosPointerDefinition |
| 25 |
from lib389.idm.domain import Domain |
| 26 |
from . import check_value_in_log_and_reset |
| 27 |
|
| 28 |
pytestmark = pytest.mark.tier0 |
| 29 |
|
| 30 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 31 |
log = logging.getLogger(__name__) |
| 32 |
|
| 33 |
# Constants |
| 34 |
PLUGIN_ACCT_POLICY = "Account Policy Plugin" |
| 35 |
ACCP_DN = f"cn={PLUGIN_ACCT_POLICY},{DN_PLUGIN}" |
| 36 |
ACCP_CONF = f"{DN_CONFIG},{ACCP_DN}" |
| 37 |
POLICY_NAME = "Account Inactivity Policy" |
| 38 |
POLICY_DN = f"cn={POLICY_NAME},{DEFAULT_SUFFIX}" |
| 39 |
COS_TEMPLATE_NAME = "TemplateCoS" |
| 40 |
COS_TEMPLATE_DN = f"cn={COS_TEMPLATE_NAME},{DEFAULT_SUFFIX}" |
| 41 |
COS_DEFINITION_NAME = "DefinitionCoS" |
| 42 |
COS_DEFINITION_DN = f"cn={COS_DEFINITION_NAME},{DEFAULT_SUFFIX}" |
| 43 |
TEST_USER_NAME = "test_inactive_user" |
| 44 |
TEST_USER_DN = f"uid={TEST_USER_NAME},{DEFAULT_SUFFIX}" |
| 45 |
TEST_USER_PW = "password" |
| 46 |
INACTIVITY_LIMIT = 30 |
| 47 |
|
| 48 |
|
| 49 |
@pytest.fixture(scope="function") |
| 50 |
def account_policy_setup(topology_st, request): |
| 51 |
"""Set up account policy plugin, configuration, and CoS objects""" |
| 52 |
log.info("Setting up Account Policy Plugin and CoS") |
| 53 |
|
| 54 |
# Enable Account Policy Plugin |
| 55 |
plugin = AccountPolicyPlugin(topology_st.standalone) |
| 56 |
if not plugin.status(): |
| 57 |
plugin.enable() |
| 58 |
plugin.set('nsslapd-pluginarg0', ACCP_CONF) |
| 59 |
|
| 60 |
# Configure Account Policy |
| 61 |
accp_configs = AccountPolicyConfigs(topology_st.standalone) |
| 62 |
accp_config = accp_configs.ensure_state( |
| 63 |
properties={ |
| 64 |
'cn': 'config', |
| 65 |
'alwaysrecordlogin': 'yes', |
| 66 |
'stateattrname': 'lastLoginTime', |
| 67 |
'altstateattrname': '1.1', |
| 68 |
'specattrname': 'acctPolicySubentry', |
| 69 |
'limitattrname': 'accountInactivityLimit' |
| 70 |
} |
| 71 |
) |
| 72 |
|
| 73 |
# Add ACI for anonymous access if it doesn't exist |
| 74 |
domain = Domain(topology_st.standalone, DEFAULT_SUFFIX) |
| 75 |
anon_aci = '(targetattr="*")(version 3.0; acl "Anonymous read access"; allow (read,search,compare) userdn="ldap:///anyone";)' |
| 76 |
domain.ensure_present('aci', anon_aci) |
| 77 |
|
| 78 |
# Restart the server to apply plugin configuration |
| 79 |
topology_st.standalone.restart() |
| 80 |
|
| 81 |
# Create or update account policy entry |
| 82 |
accp_configs = AccountPolicyConfigs(topology_st.standalone, basedn=DEFAULT_SUFFIX) |
| 83 |
policy = accp_configs.ensure_state( |
| 84 |
properties={ |
| 85 |
'cn': POLICY_NAME, |
| 86 |
'objectClass': ['top', 'ldapsubentry', 'extensibleObject', 'accountpolicy'], |
| 87 |
'accountInactivityLimit': str(INACTIVITY_LIMIT) |
| 88 |
} |
| 89 |
) |
| 90 |
|
| 91 |
# Create or update CoS template entry |
| 92 |
cos_template = CosTemplate(topology_st.standalone, dn=COS_TEMPLATE_DN) |
| 93 |
cos_template.ensure_state( |
| 94 |
properties={ |
| 95 |
'cn': COS_TEMPLATE_NAME, |
| 96 |
'objectClass': ['top', 'cosTemplate', 'extensibleObject'], |
| 97 |
'acctPolicySubentry': policy.dn |
| 98 |
} |
| 99 |
) |
| 100 |
|
| 101 |
# Create or update CoS definition entry |
| 102 |
cos_def = CosPointerDefinition(topology_st.standalone, dn=COS_DEFINITION_DN) |
| 103 |
cos_def.ensure_state( |
| 104 |
properties={ |
| 105 |
'cn': COS_DEFINITION_NAME, |
| 106 |
'objectClass': ['top', 'ldapsubentry', 'cosSuperDefinition', 'cosPointerDefinition'], |
| 107 |
'cosTemplateDn': COS_TEMPLATE_DN, |
| 108 |
'cosAttribute': 'acctPolicySubentry default operational-default' |
| 109 |
} |
| 110 |
) |
| 111 |
|
| 112 |
# Restart server to ensure CoS is applied |
| 113 |
topology_st.standalone.restart() |
| 114 |
|
| 115 |
def fin(): |
| 116 |
log.info('Cleaning up Account Policy settings') |
| 117 |
try: |
| 118 |
# Delete CoS and policy entries |
| 119 |
if cos_def.exists(): |
| 120 |
cos_def.delete() |
| 121 |
if cos_template.exists(): |
| 122 |
cos_template.delete() |
| 123 |
if policy.exists(): |
| 124 |
policy.delete() |
| 125 |
|
| 126 |
# Disable the plugin |
| 127 |
if plugin.status(): |
| 128 |
plugin.disable() |
| 129 |
topology_st.standalone.restart() |
| 130 |
except Exception as e: |
| 131 |
log.error(f'Failed to clean up: {e}') |
| 132 |
|
| 133 |
request.addfinalizer(fin) |
| 134 |
|
| 135 |
return topology_st.standalone |
| 136 |
|
| 137 |
|
| 138 |
@pytest.fixture(scope="function") |
| 139 |
def create_test_user(topology_st, account_policy_setup, request): |
| 140 |
"""Create a test user for the inactivity test""" |
| 141 |
log.info('Creating test user') |
| 142 |
|
| 143 |
users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX) |
| 144 |
user = users.ensure_state( |
| 145 |
properties={ |
| 146 |
'uid': TEST_USER_NAME, |
| 147 |
'cn': TEST_USER_NAME, |
| 148 |
'sn': TEST_USER_NAME, |
| 149 |
'userPassword': TEST_USER_PW, |
| 150 |
'uidNumber': '1000', |
| 151 |
'gidNumber': '2000', |
| 152 |
'homeDirectory': f'/home/{TEST_USER_NAME}' |
| 153 |
} |
| 154 |
) |
| 155 |
|
| 156 |
def fin(): |
| 157 |
log.info('Deleting test user') |
| 158 |
if user.exists(): |
| 159 |
user.delete() |
| 160 |
|
| 161 |
request.addfinalizer(fin) |
| 162 |
return user |
| 163 |
|
| 164 |
|
| 165 |
@pytest.mark.skipif(ds_is_older("1.4.2"), reason="Indirect account locking not implemented") |
| 166 |
def test_dsidm_account_inactivity_lock_unlock(topology_st, create_test_user): |
| 167 |
"""Test dsidm account unlock functionality with indirectly locked accounts |
| 168 |
|
| 169 |
:id: d7b57083-6111-4dbf-af84-6fca7fc7fb31 |
| 170 |
:setup: Standalone instance with Account Policy Plugin and CoS configured |
| 171 |
:steps: |
| 172 |
1. Create a test user |
| 173 |
2. Bind as the test user to set lastLoginTime |
| 174 |
3. Check account status - should be active |
| 175 |
4. Set user's lastLoginTime to a time in the past that exceeds inactivity limit |
| 176 |
5. Check account status - should be locked due to inactivity |
| 177 |
6. Attempt to bind as the user - should fail with constraint violation |
| 178 |
7. Unlock the account using dsidm account unlock |
| 179 |
8. Verify account status is active again |
| 180 |
9. Verify the user can bind again |
| 181 |
:expectedresults: |
| 182 |
1. Success |
| 183 |
2. Success |
| 184 |
3. Account status shows as activated |
| 185 |
4. Success |
| 186 |
5. Account status shows as inactivity limit exceeded |
| 187 |
6. Bind attempt fails with constraint violation |
| 188 |
7. Account unlocked successfully |
| 189 |
8. Account status shows as activated |
| 190 |
9. User can bind successfully |
| 191 |
""" |
| 192 |
standalone = topology_st.standalone |
| 193 |
user = create_test_user |
| 194 |
|
| 195 |
# Set up FakeArgs for dsidm commands |
| 196 |
args = FakeArgs() |
| 197 |
args.dn = user.dn |
| 198 |
args.json = False |
| 199 |
args.details = False |
| 200 |
|
| 201 |
# 1. Check initial account status - should be active |
| 202 |
log.info('Step 1: Checking initial account status') |
| 203 |
entry_status(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args) |
| 204 |
check_value_in_log_and_reset(topology_st, check_value='Entry State: activated') |
| 205 |
|
| 206 |
# 2. Bind as test user to set initial lastLoginTime |
| 207 |
log.info('Step 2: Binding as test user to set lastLoginTime') |
| 208 |
try: |
| 209 |
conn = user.bind(TEST_USER_PW) |
| 210 |
conn.unbind() |
| 211 |
log.info("Successfully bound as test user") |
| 212 |
except ldap.LDAPError as e: |
| 213 |
pytest.fail(f"Failed to bind as test user: {e}") |
| 214 |
|
| 215 |
# 3. Set lastLoginTime to a time in the past that exceeds inactivity limit |
| 216 |
log.info('Step 3: Setting lastLoginTime to the past') |
| 217 |
past_time = datetime.utcnow() - timedelta(seconds=INACTIVITY_LIMIT * 2) |
| 218 |
past_time_str = past_time.strftime('%Y%m%d%H%M%SZ') |
| 219 |
user.replace('lastLoginTime', past_time_str) |
| 220 |
|
| 221 |
# 4. Check account status - should now be locked due to inactivity |
| 222 |
log.info('Step 4: Checking account status after setting old lastLoginTime') |
| 223 |
entry_status(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args) |
| 224 |
check_value_in_log_and_reset(topology_st, check_value='Entry State: inactivity limit exceeded') |
| 225 |
|
| 226 |
# 5. Attempt to bind as the user - should fail |
| 227 |
log.info('Step 5: Attempting to bind as user (should fail)') |
| 228 |
with pytest.raises(ldap.CONSTRAINT_VIOLATION) as excinfo: |
| 229 |
conn = user.bind(TEST_USER_PW) |
| 230 |
assert "Account inactivity limit exceeded" in str(excinfo.value) |
| 231 |
|
| 232 |
# 6. Unlock the account using dsidm account unlock |
| 233 |
log.info('Step 6: Unlocking the account with dsidm') |
| 234 |
unlock(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args) |
| 235 |
check_value_in_log_and_reset(topology_st, |
| 236 |
check_value='now unlocked by resetting lastLoginTime') |
| 237 |
|
| 238 |
# 7. Verify account status is active again |
| 239 |
log.info('Step 7: Checking account status after unlock') |
| 240 |
entry_status(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args) |
| 241 |
check_value_in_log_and_reset(topology_st, check_value='Entry State: activated') |
| 242 |
|
| 243 |
# 8. Verify the user can bind again |
| 244 |
log.info('Step 8: Verifying user can bind again') |
| 245 |
try: |
| 246 |
conn = user.bind(TEST_USER_PW) |
| 247 |
conn.unbind() |
| 248 |
log.info("Successfully bound as test user after unlock") |
| 249 |
except ldap.LDAPError as e: |
| 250 |
pytest.fail(f"Failed to bind as test user after unlock: {e}") |
| 251 |
|
| 252 |
|
| 253 |
@pytest.mark.skipif(ds_is_older("1.4.2"), reason="Indirect account locking not implemented") |
| 254 |
def test_dsidm_indirectly_locked_via_role(topology_st, create_test_user): |
| 255 |
"""Test dsidm account unlock functionality with accounts indirectly locked via role |
| 256 |
|
| 257 |
:id: 7bfe69bb-cf99-4214-a763-051ab2b9cf89 |
| 258 |
:setup: Standalone instance with Role and user configured |
| 259 |
:steps: |
| 260 |
1. Create a test user |
| 261 |
2. Create a Filtered Role that includes the test user |
| 262 |
3. Lock the role |
| 263 |
4. Check account status - should be indirectly locked through the role |
| 264 |
5. Attempt to unlock the account - should fail with appropriate message |
| 265 |
6. Unlock the role |
| 266 |
7. Verify account status is active again |
| 267 |
:expectedresults: |
| 268 |
1. Success |
| 269 |
2. Success |
| 270 |
3. Success |
| 271 |
4. Account status shows as indirectly locked |
| 272 |
5. Unlock attempt fails with appropriate error message |
| 273 |
6. Success |
| 274 |
7. Account status shows as activated |
| 275 |
""" |
| 276 |
standalone = topology_st.standalone |
| 277 |
user = create_test_user |
| 278 |
|
| 279 |
# Use FilteredRoles and ensure_state for role creation |
| 280 |
log.info('Step 1: Creating Filtered Role') |
| 281 |
roles = FilteredRoles(standalone, DEFAULT_SUFFIX) |
| 282 |
role = roles.ensure_state( |
| 283 |
properties={ |
| 284 |
'cn': 'TestFilterRole', |
| 285 |
'nsRoleFilter': f'(uid={TEST_USER_NAME})' |
| 286 |
} |
| 287 |
) |
| 288 |
|
| 289 |
# Set up FakeArgs for dsidm commands |
| 290 |
args = FakeArgs() |
| 291 |
args.dn = user.dn |
| 292 |
args.json = False |
| 293 |
args.details = False |
| 294 |
|
| 295 |
# 2. Check account status before locking role |
| 296 |
log.info('Step 2: Checking account status before locking role') |
| 297 |
entry_status(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args) |
| 298 |
check_value_in_log_and_reset(topology_st, check_value='Entry State: activated') |
| 299 |
|
| 300 |
# 3. Lock the role |
| 301 |
log.info('Step 3: Locking the role') |
| 302 |
role.lock() |
| 303 |
|
| 304 |
# 4. Check account status - should be indirectly locked |
| 305 |
log.info('Step 4: Checking account status after locking role') |
| 306 |
entry_status(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args) |
| 307 |
check_value_in_log_and_reset(topology_st, check_value='Entry State: indirectly locked through a Role') |
| 308 |
|
| 309 |
# 5. Attempt to unlock the account - should fail |
| 310 |
log.info('Step 5: Attempting to unlock indirectly locked account') |
| 311 |
unlock(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args) |
| 312 |
check_value_in_log_and_reset(topology_st, |
| 313 |
check_value='Account is locked through role') |
| 314 |
|
| 315 |
# 6. Unlock the role |
| 316 |
log.info('Step 6: Unlocking the role') |
| 317 |
role.unlock() |
| 318 |
|
| 319 |
# 7. Verify account status is active again |
| 320 |
log.info('Step 7: Checking account status after unlocking role') |
| 321 |
entry_status(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args) |
| 322 |
check_value_in_log_and_reset(topology_st, check_value='Entry State: activated') |
| 323 |
|
| 324 |
|
| 325 |
if __name__ == '__main__': |
| 326 |
# Run isolated |
| 327 |
# -s for DEBUG mode |
| 328 |
CURRENT_FILE = os.path.realpath(__file__) |
| 329 |
pytest.main(["-s", CURRENT_FILE]) |