| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2022 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 |
|
| 10 |
import ldap |
| 11 |
import logging |
| 12 |
import os |
| 13 |
import pytest |
| 14 |
import time |
| 15 |
from lib389.backend import Backends, Backend |
| 16 |
from lib389._constants import HOST_STANDALONE, PORT_STANDALONE, DN_DM, PW_DM |
| 17 |
from lib389.dbgen import dbgen_users |
| 18 |
from lib389.mappingTree import MappingTrees |
| 19 |
from test389.topologies import topology_st |
| 20 |
from lib389.referral import Referrals, Referral |
| 21 |
|
| 22 |
|
| 23 |
try: |
| 24 |
from lib389.backend import BackendSuffixView |
| 25 |
has_orphan_attribute = True |
| 26 |
except ImportError: |
| 27 |
has_orphan_attribute = False |
| 28 |
|
| 29 |
pytestmark = pytest.mark.tier1 |
| 30 |
|
| 31 |
DEBUGGING = os.getenv("DEBUGGING", default=False) |
| 32 |
if DEBUGGING: |
| 33 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 34 |
else: |
| 35 |
logging.getLogger(__name__).setLevel(logging.INFO) |
| 36 |
log = logging.getLogger(__name__) |
| 37 |
|
| 38 |
PARENT_SUFFIX = "dc=parent" |
| 39 |
CHILD1_SUFFIX = f"dc=child1,{PARENT_SUFFIX}" |
| 40 |
CHILD2_SUFFIX = f"dc=child2,{PARENT_SUFFIX}" |
| 41 |
|
| 42 |
PARENT_REFERRAL_DN = f"cn=ref,ou=People,{PARENT_SUFFIX}" |
| 43 |
CHILD1_REFERRAL_DN = f"cn=ref,ou=people,{CHILD1_SUFFIX}" |
| 44 |
CHILD2_REFERRAL_DN = f"cn=ref,ou=people,{CHILD2_SUFFIX}" |
| 45 |
|
| 46 |
REFERRAL_CHECK_PEDIOD = 7 |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
BESTRUCT = [ |
| 51 |
{ "bename" : "parent", "suffix": PARENT_SUFFIX }, |
| 52 |
{ "bename" : "child1", "suffix": CHILD1_SUFFIX }, |
| 53 |
{ "bename" : "child2", "suffix": CHILD2_SUFFIX }, |
| 54 |
] |
| 55 |
|
| 56 |
|
| 57 |
@pytest.fixture(scope="module") |
| 58 |
def topo(topology_st, request): |
| 59 |
bes = [] |
| 60 |
|
| 61 |
def fin(): |
| 62 |
for be in bes: |
| 63 |
be.delete() |
| 64 |
|
| 65 |
if not DEBUGGING: |
| 66 |
request.addfinalizer(fin) |
| 67 |
|
| 68 |
inst = topology_st.standalone |
| 69 |
# Reduce nsslapd-referral-check-period to accelerate test |
| 70 |
topology_st.standalone.config.set("nsslapd-referral-check-period", str(REFERRAL_CHECK_PEDIOD)) |
| 71 |
|
| 72 |
ldif_files = {} |
| 73 |
for d in BESTRUCT: |
| 74 |
bename = d['bename'] |
| 75 |
suffix = d['suffix'] |
| 76 |
log.info(f'Adding suffix: {suffix} and backend: {bename}...') |
| 77 |
backends = Backends(inst) |
| 78 |
try: |
| 79 |
be = backends.create(properties={'nsslapd-suffix': suffix, 'name': bename}) |
| 80 |
# Insert at list head so that children backends get deleted before parent one. |
| 81 |
bes.insert(0, be) |
| 82 |
except ldap.UNWILLING_TO_PERFORM as e: |
| 83 |
if str(e) == "Mapping tree for this suffix exists!": |
| 84 |
pass |
| 85 |
else: |
| 86 |
raise e |
| 87 |
|
| 88 |
ldif_dir = inst.get_ldif_dir() |
| 89 |
ldif_files[bename] = os.path.join(ldif_dir, f'default_{bename}.ldif') |
| 90 |
dbgen_users(inst, 5, ldif_files[bename], suffix) |
| 91 |
inst.stop() |
| 92 |
for d in BESTRUCT: |
| 93 |
bename = d['bename'] |
| 94 |
inst.ldif2db(bename, None, None, None, ldif_files[bename]) |
| 95 |
inst.start() |
| 96 |
return topology_st |
| 97 |
|
| 98 |
# Parameters for test_sub_suffixes |
| 99 |
@pytest.mark.parametrize( |
| 100 |
"orphan_param", |
| 101 |
[ |
| 102 |
pytest.param( ( True, { PARENT_SUFFIX: 2, CHILD1_SUFFIX:1, CHILD2_SUFFIX:1}), id="orphan-is-true" ), |
| 103 |
pytest.param( ( False, { PARENT_SUFFIX: 3, CHILD1_SUFFIX:1, CHILD2_SUFFIX:1}), id="orphan-is-false" ), |
| 104 |
pytest.param( ( None, { PARENT_SUFFIX: 3, CHILD1_SUFFIX:1, CHILD2_SUFFIX:1}), id="no-orphan" ), |
| 105 |
], |
| 106 |
) |
| 107 |
|
| 108 |
|
| 109 |
@pytest.mark.skipif(not has_orphan_attribute, reason = "compatibility attribute not yet implemented in this version") |
| 110 |
def test_sub_suffixes(topo, orphan_param): |
| 111 |
""" check the entries found on suffix/sub-suffix |
| 112 |
|
| 113 |
:id: 5b4421c2-d851-11ec-a760-482ae39447e5 |
| 114 |
:parametrized: yes |
| 115 |
:feature: mapping-tree |
| 116 |
:setup: Standalone instance with 3 additional backends: |
| 117 |
dc=parent, dc=child1,dc=parent, dc=childr21,dc=parent |
| 118 |
:steps: |
| 119 |
1. Det orphan attribute mapping tree entry for dc=child1,dc=parent according to orphan_param value |
| 120 |
2. Restart the server to rebuild the mapping tree |
| 121 |
3. For each suffix: search the suffix |
| 122 |
:expectedresults: |
| 123 |
1. Success |
| 124 |
2. Success |
| 125 |
3. Number of entries should be the expected one |
| 126 |
""" |
| 127 |
mt = MappingTrees(topo.standalone).get('dc=child1,dc=parent') |
| 128 |
orphan = orphan_param[0] |
| 129 |
expected_values = orphan_param[1] |
| 130 |
if orphan is True: |
| 131 |
mt.replace('orphan', 'true') |
| 132 |
elif orphan is False: |
| 133 |
mt.replace('orphan', 'false') |
| 134 |
elif orphan is None: |
| 135 |
mt.remove_all('orphan') |
| 136 |
topo.standalone.restart() |
| 137 |
|
| 138 |
for suffix, expected in expected_values.items(): |
| 139 |
log.info(f'Verifying domain component entries count for search under {suffix} ...') |
| 140 |
entries = topo.standalone.search_s(suffix, ldap.SCOPE_SUBTREE, "(dc=*)") |
| 141 |
assert len(entries) == expected |
| 142 |
log.info(f'Found {expected} domain component entries as expected while searching {suffix}') |
| 143 |
|
| 144 |
log.info('Test PASSED') |
| 145 |
|
| 146 |
|
| 147 |
def test_one_level_search_on_sub_suffixes(topo): |
| 148 |
""" Perform one level scoped search accross suffix and sub-suffix |
| 149 |
|
| 150 |
:id: 92f3139e-280e-11ef-a989-482ae39447e5 |
| 151 |
:feature: mapping-tree |
| 152 |
:setup: Standalone instance with 3 additional backends: |
| 153 |
dc=parent, dc=child1,dc=parent, dc=childr21,dc=parent |
| 154 |
:steps: |
| 155 |
1. Perform a ONE LEVEL search on dc=parent |
| 156 |
2. Check that all expected entries have been returned |
| 157 |
3. Check that only the expected entries have been returned |
| 158 |
:expectedresults: |
| 159 |
1. Success |
| 160 |
2. each expected dn should be in the result set |
| 161 |
3. Number of returned entries should be the same as the number of expected entries |
| 162 |
""" |
| 163 |
expected_dns = ( 'dc=child1,dc=parent', |
| 164 |
'dc=child2,dc=parent', |
| 165 |
'ou=accounting,dc=parent', |
| 166 |
'ou=product development,dc=parent', |
| 167 |
'ou=product testing,dc=parent', |
| 168 |
'ou=human resources,dc=parent', |
| 169 |
'ou=payroll,dc=parent', |
| 170 |
'ou=people,dc=parent', |
| 171 |
'ou=groups,dc=parent', ) |
| 172 |
entries = topo.standalone.search_s("dc=parent", ldap.SCOPE_ONELEVEL, "(objectClass=*)", |
| 173 |
attrlist=("dc","ou"), escapehatch='i am sure') |
| 174 |
log.info(f'one level search on dc=parent returned the following entries: {entries}') |
| 175 |
dns = [ entry.dn for entry in entries ] |
| 176 |
for dn in expected_dns: |
| 177 |
assert dn in dns |
| 178 |
assert len(entries) == len(expected_dns) |
| 179 |
|
| 180 |
|
| 181 |
def test_sub_suffixes_errlog(topo): |
| 182 |
""" check the entries found on suffix/sub-suffix |
| 183 |
used int |
| 184 |
|
| 185 |
:id: 1db9d52e-28de-11ef-b286-482ae39447e5 |
| 186 |
:feature: mapping-tree |
| 187 |
:setup: Standalone instance with 3 additional backends: |
| 188 |
dc=parent, dc=child1,dc=parent, dc=childr21,dc=parent |
| 189 |
:steps: |
| 190 |
1. Check that id2entry error message is not in the error log. |
| 191 |
:expectedresults: |
| 192 |
1. Success |
| 193 |
""" |
| 194 |
inst = topo.standalone |
| 195 |
assert not inst.searchErrorsLog('id2entry - Could not open id2entry err 0') |
| 196 |
|
| 197 |
|
| 198 |
# Parameters for test_referral_subsuffix: |
| 199 |
# a tuple pair containing: |
| 200 |
# - list of referral dn that must be created |
| 201 |
# - dict of searches basedn: expected_number_of_referrals |
| 202 |
@pytest.mark.parametrize( |
| 203 |
"parameters", |
| 204 |
[ |
| 205 |
pytest.param( ((PARENT_REFERRAL_DN, CHILD1_REFERRAL_DN), {PARENT_SUFFIX: 2, CHILD1_SUFFIX:1, CHILD2_SUFFIX:0}), id="Both"), |
| 206 |
pytest.param( ((PARENT_REFERRAL_DN,), {PARENT_SUFFIX: 1, CHILD1_SUFFIX:0, CHILD2_SUFFIX:0}) , id="Parent"), |
| 207 |
pytest.param( ((CHILD1_REFERRAL_DN,), {PARENT_SUFFIX: 1, CHILD1_SUFFIX:1, CHILD2_SUFFIX:0}) , id="Child"), |
| 208 |
pytest.param( ((), {PARENT_SUFFIX: 0, CHILD1_SUFFIX:0, CHILD2_SUFFIX:0}), id="None"), |
| 209 |
]) |
| 210 |
|
| 211 |
def test_referral_subsuffix(topo, request, parameters): |
| 212 |
"""Test the results of an inverted parent suffix definition in the configuration. |
| 213 |
|
| 214 |
For more details see: |
| 215 |
https://www.port389.org/docs/389ds/design/mapping_tree_assembly.html |
| 216 |
|
| 217 |
:id: 4e111a22-2a5d-11ef-a890-482ae39447e5 |
| 218 |
:feature: referrals |
| 219 |
:setup: Standalone instance with 3 additional backends: |
| 220 |
dc=parent, dc=child1,dc=parent, dc=childr21,dc=parent |
| 221 |
|
| 222 |
:setup: Standalone instance |
| 223 |
:parametrized: yes |
| 224 |
:steps: |
| 225 |
refs,searches = referrals |
| 226 |
|
| 227 |
1. Create the referrals according to the current parameter |
| 228 |
2. Wait enough time so they get detected |
| 229 |
3. For each search base dn, in the current parameter, perform the two following steps |
| 230 |
4. In 3. loop: Perform a search with provided base dn |
| 231 |
5. In 3. loop: Check that the number of returned referrals is the expected one. |
| 232 |
|
| 233 |
:expectedresults: |
| 234 |
all steps succeeds |
| 235 |
""" |
| 236 |
inst = topo.standalone |
| 237 |
|
| 238 |
def fin(): |
| 239 |
log.info('Deleting all referrals') |
| 240 |
for ref in Referrals(inst, PARENT_SUFFIX).list(): |
| 241 |
ref.delete() |
| 242 |
|
| 243 |
# Set cleanup callback |
| 244 |
if DEBUGGING: |
| 245 |
request.addfinalizer(fin) |
| 246 |
|
| 247 |
# Remove all referrals |
| 248 |
fin() |
| 249 |
# Add requested referrals |
| 250 |
for dn in parameters[0]: |
| 251 |
refs = Referral(inst, dn=dn) |
| 252 |
refs.create(basedn=dn, properties={ 'cn': 'ref', 'ref': f'ldap://remote/{dn}'}) |
| 253 |
# Wait that the internal search detects the referrals |
| 254 |
time.sleep(REFERRAL_CHECK_PEDIOD + 1) |
| 255 |
# Open a test connection |
| 256 |
ldc = ldap.initialize(f"ldap://{HOST_STANDALONE}:{PORT_STANDALONE}") |
| 257 |
ldc.set_option(ldap.OPT_REFERRALS,0) |
| 258 |
ldc.simple_bind_s(DN_DM,PW_DM) |
| 259 |
|
| 260 |
# For each search base dn: |
| 261 |
for basedn,nbref in parameters[1].items(): |
| 262 |
log.info(f"Referrals are: {parameters[0]}") |
| 263 |
# Perform a search with provided base dn |
| 264 |
result = ldc.search_s(basedn, ldap.SCOPE_SUBTREE, filterstr="(ou=People)") |
| 265 |
found_dns = [ dn for dn,entry in result if dn is not None ] |
| 266 |
found_refs = [ entry for dn,entry in result if dn is None ] |
| 267 |
log.info(f"Search on {basedn} returned {found_dns} and {found_refs}") |
| 268 |
# Check that the number of returned referrals is the expected one. |
| 269 |
log.info(f"Search returned {len(found_refs)} referrals. {nbref} are expected.") |
| 270 |
assert len(found_refs) == nbref |
| 271 |
ldc.unbind() |
| 272 |
|