| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2023 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 pytest |
| 11 |
import os |
| 12 |
import ldap |
| 13 |
import time |
| 14 |
from lib389.utils import ensure_str |
| 15 |
from lib389.plugins import AliasEntriesPlugin |
| 16 |
from lib389.idm.user import UserAccount |
| 17 |
from lib389._constants import DEFAULT_SUFFIX |
| 18 |
from test389.topologies import topology_st as topo |
| 19 |
|
| 20 |
log = logging.getLogger(__name__) |
| 21 |
|
| 22 |
TEST_ENTRY_NAME = "entry" |
| 23 |
TEST_ENTRY_DN = "cn=entry," + DEFAULT_SUFFIX |
| 24 |
TEST_ALIAS_NAME = "alias entry" |
| 25 |
TEST_ALIAS_DN = "cn=alias_entry," + DEFAULT_SUFFIX |
| 26 |
TEST_ALIAS_DN_WRONG = "cn=alias_entry_not_there," + DEFAULT_SUFFIX |
| 27 |
EXPECTED_UIDNUM = "1000" |
| 28 |
|
| 29 |
|
| 30 |
def test_entry_alias(topo): |
| 31 |
"""Test that deref search for alias entry works |
| 32 |
|
| 33 |
:id: 454e85af-0e20-4a36-9b3a-02562b1db53d |
| 34 |
:setup: Standalone Instance |
| 35 |
:steps: |
| 36 |
1. Enable alias entry plugin |
| 37 |
2. Create entry and alias entry |
| 38 |
3. Set deref option and do a base search |
| 39 |
4. Test non-base scope ssearch returns error |
| 40 |
5. Test invalid alias DN returns error |
| 41 |
:expectedresults: |
| 42 |
1. Success |
| 43 |
2. Success |
| 44 |
3. Success |
| 45 |
4. Success |
| 46 |
5. Success |
| 47 |
""" |
| 48 |
inst = topo.standalone |
| 49 |
|
| 50 |
# Enable Alias Entries plugin |
| 51 |
alias_plugin = AliasEntriesPlugin(inst) |
| 52 |
alias_plugin.enable() |
| 53 |
inst.restart() |
| 54 |
|
| 55 |
# Add entry |
| 56 |
test_user = UserAccount(inst, TEST_ENTRY_DN) |
| 57 |
test_user.create(properties={ |
| 58 |
'uid': TEST_ENTRY_NAME, |
| 59 |
'cn': TEST_ENTRY_NAME, |
| 60 |
'sn': TEST_ENTRY_NAME, |
| 61 |
'userPassword': TEST_ENTRY_NAME, |
| 62 |
'uidNumber': '1000', |
| 63 |
'gidNumber': '2000', |
| 64 |
'homeDirectory': '/home/alias_test', |
| 65 |
}) |
| 66 |
|
| 67 |
# Add entry that has an alias set to the first entry |
| 68 |
test_alias = UserAccount(inst, TEST_ALIAS_DN) |
| 69 |
test_alias.create(properties={ |
| 70 |
'uid': TEST_ALIAS_NAME, |
| 71 |
'cn': TEST_ALIAS_NAME, |
| 72 |
'sn': TEST_ALIAS_NAME, |
| 73 |
'userPassword': TEST_ALIAS_NAME, |
| 74 |
'uidNumber': '1001', |
| 75 |
'gidNumber': '2001', |
| 76 |
'homeDirectory': '/home/alias_test', |
| 77 |
'objectclass': ['alias', 'extensibleObject'], |
| 78 |
'aliasedObjectName': TEST_ENTRY_DN, |
| 79 |
}) |
| 80 |
|
| 81 |
# Set the deref "finding" option |
| 82 |
inst.set_option(ldap.OPT_DEREF, ldap.DEREF_FINDING) |
| 83 |
|
| 84 |
# Do base search which could map entry to the aliased one |
| 85 |
log.info("Test alias") |
| 86 |
deref_user = UserAccount(inst, TEST_ALIAS_DN) |
| 87 |
result = deref_user.search(scope="base") |
| 88 |
assert result[0].dn == TEST_ENTRY_DN |
| 89 |
assert ensure_str(result[0].getValue('uidNumber')) == EXPECTED_UIDNUM |
| 90 |
|
| 91 |
# Do non-base search which could raise an error |
| 92 |
log.info("Test unsupported search scope") |
| 93 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 94 |
deref_user.search(scope="subtree") |
| 95 |
|
| 96 |
# Reset the aliasObjectname to a DN that does not exist, and try again |
| 97 |
log.info("Test invalid alias") |
| 98 |
test_alias.replace('aliasedObjectName', TEST_ALIAS_DN_WRONG) |
| 99 |
try: |
| 100 |
deref_user.search(scope="base") |
| 101 |
assert False |
| 102 |
except ldap.LDAPError as e: |
| 103 |
msg = e.args[0]['info'] |
| 104 |
assert msg.startswith("Failed to dereference alias object") |
| 105 |
|
| 106 |
|
| 107 |
if __name__ == '__main__': |
| 108 |
# Run isolated |
| 109 |
# -s for DEBUG mode |
| 110 |
CURRENT_FILE = os.path.realpath(__file__) |
| 111 |
pytest.main(["-s", CURRENT_FILE]) |