| 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 ldap |
| 10 |
import pytest |
| 11 |
import os |
| 12 |
from lib389.schema import Schema |
| 13 |
from lib389.config import Config |
| 14 |
from lib389.idm.user import UserAccounts |
| 15 |
from lib389.idm.group import Group, Groups |
| 16 |
from lib389._constants import DEFAULT_SUFFIX, PW_DM |
| 17 |
from test389.topologies import log, topology_st as topo |
| 18 |
|
| 19 |
pytestmark = pytest.mark.tier0 |
| 20 |
|
| 21 |
log = log.getChild(__name__) |
| 22 |
|
| 23 |
|
| 24 |
@pytest.fixture(scope="function") |
| 25 |
def validate_syntax_off(topo, request): |
| 26 |
config = Config(topo.standalone) |
| 27 |
config.replace("nsslapd-syntaxcheck", "off") |
| 28 |
|
| 29 |
def fin(): |
| 30 |
config.replace("nsslapd-syntaxcheck", "on") |
| 31 |
request.addfinalizer(fin) |
| 32 |
|
| 33 |
|
| 34 |
def test_valid(topo, validate_syntax_off): |
| 35 |
"""Test syntax-validate task with valid entries |
| 36 |
|
| 37 |
:id: ec402a5b-bfb1-494d-b751-71b0d31a4d83 |
| 38 |
:setup: Standalone instance |
| 39 |
:steps: |
| 40 |
1. Set nsslapd-syntaxcheck to off |
| 41 |
2. Clean error log |
| 42 |
3. Run syntax validate task |
| 43 |
4. Assert that there are no errors in the error log |
| 44 |
5. Set nsslapd-syntaxcheck to on |
| 45 |
:expectedresults: |
| 46 |
1. It should succeed |
| 47 |
2. It should succeed |
| 48 |
3. It should succeed |
| 49 |
4. It should succeed |
| 50 |
5. It should succeed |
| 51 |
""" |
| 52 |
|
| 53 |
inst = topo.standalone |
| 54 |
|
| 55 |
log.info('Clean the error log') |
| 56 |
inst.deleteErrorLogs() |
| 57 |
|
| 58 |
schema = Schema(inst) |
| 59 |
log.info('Attempting to add task entry...') |
| 60 |
validate_task = schema.validate_syntax(DEFAULT_SUFFIX) |
| 61 |
validate_task.wait() |
| 62 |
exitcode = validate_task.get_exit_code() |
| 63 |
assert exitcode == 0 |
| 64 |
error_lines = inst.ds_error_log.match('.*Found 0 invalid entries.*') |
| 65 |
assert (len(error_lines) == 1) |
| 66 |
log.info('Found 0 invalid entries - Success') |
| 67 |
|
| 68 |
|
| 69 |
def test_invalid_uidnumber(topo, validate_syntax_off): |
| 70 |
"""Test syntax-validate task with invalid uidNumber attribute value |
| 71 |
|
| 72 |
:id: 30fdcae6-ffa6-4ec4-8da9-6fb138fc1828 |
| 73 |
:setup: Standalone instance |
| 74 |
:steps: |
| 75 |
1. Set nsslapd-syntaxcheck to off |
| 76 |
2. Clean error log |
| 77 |
3. Add a user with uidNumber attribute set to an invalid value (string) |
| 78 |
4. Run syntax validate task |
| 79 |
5. Assert that there is corresponding error in the error log |
| 80 |
6. Set nsslapd-syntaxcheck to on |
| 81 |
:expectedresults: |
| 82 |
1. It should succeed |
| 83 |
2. It should succeed |
| 84 |
3. It should succeed |
| 85 |
4. It should succeed |
| 86 |
5. It should succeed |
| 87 |
6. It should succeed |
| 88 |
""" |
| 89 |
|
| 90 |
inst = topo.standalone |
| 91 |
|
| 92 |
log.info('Clean the error log') |
| 93 |
inst.deleteErrorLogs() |
| 94 |
|
| 95 |
users = UserAccounts(inst, DEFAULT_SUFFIX) |
| 96 |
users.create_test_user(uid="invalid_value") |
| 97 |
|
| 98 |
schema = Schema(inst) |
| 99 |
log.info('Attempting to add task entry...') |
| 100 |
validate_task = schema.validate_syntax(DEFAULT_SUFFIX) |
| 101 |
validate_task.wait() |
| 102 |
exitcode = validate_task.get_exit_code() |
| 103 |
assert exitcode == 0 |
| 104 |
error_lines = inst.ds_error_log.match('.*uidNumber: value #0 invalid per syntax.*') |
| 105 |
assert (len(error_lines) == 1) |
| 106 |
log.info('Found an invalid entry with wrong uidNumber - Success') |
| 107 |
|
| 108 |
|
| 109 |
def test_invalid_dn_syntax_crash(topo): |
| 110 |
"""Add an entry with an escaped space, restart the server, and try to delete |
| 111 |
it. In this case the DN is not correctly parsed and causes cache revert to |
| 112 |
to dereference a NULL pointer. So the delete can fail as long as the server |
| 113 |
does not crash. |
| 114 |
|
| 115 |
:id: 62d87272-dfb8-4627-9ca1-dbe33082caf8 |
| 116 |
:setup: Standalone Instance |
| 117 |
:steps: |
| 118 |
1. Add entry with leading escaped space in the RDN |
| 119 |
2. Restart the server so the entry is rebuilt from the database |
| 120 |
3. Delete the entry |
| 121 |
4. The server should still be running |
| 122 |
:expectedresults: |
| 123 |
1. Success |
| 124 |
2. Success |
| 125 |
3. Success |
| 126 |
4. Success |
| 127 |
""" |
| 128 |
|
| 129 |
# Create group |
| 130 |
groups = Groups(topo.standalone, DEFAULT_SUFFIX) |
| 131 |
group = groups.create(properties={'cn': ' test'}) |
| 132 |
|
| 133 |
# Restart the server |
| 134 |
topo.standalone.restart() |
| 135 |
|
| 136 |
# Delete group |
| 137 |
try: |
| 138 |
group.delete() |
| 139 |
except ldap.NO_SUCH_OBJECT: |
| 140 |
# This is okay in this case as we are only concerned about a crash |
| 141 |
pass |
| 142 |
|
| 143 |
# Make sure server is still running |
| 144 |
groups.list() |
| 145 |
|
| 146 |
|
| 147 |
@pytest.mark.parametrize("props, rawdn", [ |
| 148 |
({'cn': ' leadingSpace'}, "cn=\\20leadingSpace,ou=Groups,dc=example,dc=com"), |
| 149 |
({'cn': 'trailingSpace '}, "cn=trailingSpace\\20,ou=Groups,dc=example,dc=com")]) |
| 150 |
def test_dn_syntax_spaces_delete(topo, props, rawdn): |
| 151 |
"""Test that an entry with a space as the first character in the DN can be |
| 152 |
deleted without error. We also want to make sure the indexes are properly |
| 153 |
updated by repeatedly adding and deleting the entry, and that the entry cache |
| 154 |
is properly maintained. |
| 155 |
|
| 156 |
:id: b993f37c-c2b0-4312-992c-a9048ff98965 |
| 157 |
:customerscenario: True |
| 158 |
:parametrized: yes |
| 159 |
:setup: Standalone Instance |
| 160 |
:steps: |
| 161 |
1. Create a group with a DN that has a space as the first/last |
| 162 |
character. |
| 163 |
2. Delete group |
| 164 |
3. Add group |
| 165 |
4. Modify group |
| 166 |
5. Restart server and modify entry |
| 167 |
6. Delete group |
| 168 |
7. Add group back |
| 169 |
8. Delete group using specific DN |
| 170 |
:expectedresults: |
| 171 |
1. Success |
| 172 |
2. Success |
| 173 |
3. Success |
| 174 |
4. Success |
| 175 |
5. Success |
| 176 |
6. Success |
| 177 |
7. Success |
| 178 |
8. Success |
| 179 |
""" |
| 180 |
|
| 181 |
# Create group |
| 182 |
groups = Groups(topo.standalone, DEFAULT_SUFFIX) |
| 183 |
group = groups.create(properties=props.copy()) |
| 184 |
|
| 185 |
# Delete group (verifies DN/RDN parsing works and cache is correct) |
| 186 |
group.delete() |
| 187 |
|
| 188 |
# Add group again (verifies entryrdn index was properly updated) |
| 189 |
groups = Groups(topo.standalone, DEFAULT_SUFFIX) |
| 190 |
group = groups.create(properties=props.copy()) |
| 191 |
|
| 192 |
# Modify the group (verifies dn/rdn parsing is correct) |
| 193 |
group.replace('description', 'escaped space group') |
| 194 |
|
| 195 |
# Restart the server. This will pull the entry from the database and |
| 196 |
# convert it into a cache entry, which is different than how a client |
| 197 |
# first adds an entry and is put into the cache before being written to |
| 198 |
# disk. |
| 199 |
topo.standalone.restart() |
| 200 |
|
| 201 |
# Make sure we can modify the entry (verifies cache entry was created |
| 202 |
# correctly) |
| 203 |
group.replace('description', 'escaped space group after restart') |
| 204 |
|
| 205 |
# Make sure it can still be deleted (verifies cache again). |
| 206 |
group.delete() |
| 207 |
|
| 208 |
# Add it back so we can delete it using a specific DN (sanity test to verify |
| 209 |
# another DN/RDN parsing variation). |
| 210 |
groups = Groups(topo.standalone, DEFAULT_SUFFIX) |
| 211 |
group = groups.create(properties=props.copy()) |
| 212 |
group = Group(topo.standalone, dn=rawdn) |
| 213 |
group.delete() |
| 214 |
|
| 215 |
|
| 216 |
def test_boolean_case(topo): |
| 217 |
"""Test that we can a boolean value in any case |
| 218 |
|
| 219 |
:id: 56777c1d-b058-41e1-abd5-87a6f1512db2 |
| 220 |
:customerscenario: True |
| 221 |
:setup: Standalone Instance |
| 222 |
:steps: |
| 223 |
1. Create test user |
| 224 |
2. Add boolean attribute value that is lowercase "false" |
| 225 |
:expectedresults: |
| 226 |
1. Success |
| 227 |
2. Success |
| 228 |
""" |
| 229 |
inst = topo.standalone |
| 230 |
users = UserAccounts(inst, DEFAULT_SUFFIX) |
| 231 |
user = users.create_test_user(uid=1011) |
| 232 |
|
| 233 |
user.add('objectclass', 'extensibleObject') |
| 234 |
user.add('pamsecure', 'false') |
| 235 |
user.replace('pamsecure', 'FALSE') |
| 236 |
user.replace('pamsecure', 'true') |
| 237 |
user.replace('pamsecure', 'TRUE') |
| 238 |
|
| 239 |
# Test some invalid syntax |
| 240 |
with pytest.raises(ldap.INVALID_SYNTAX): |
| 241 |
user.replace('pamsecure', 'blah') |
| 242 |
|
| 243 |
def test_escaped_space(topo, request): |
| 244 |
"""Test that we can bind with DN containing escaped space |
| 245 |
|
| 246 |
:id: 480adf31-f6c0-48a1-ba5b-29e0324c5702 |
| 247 |
:customerscenario: True |
| 248 |
:setup: Standalone Instance |
| 249 |
:steps: |
| 250 |
1. Create test user with RDN containing space |
| 251 |
2. Authenticate with that user escaping the space |
| 252 |
3. Do few searches |
| 253 |
:expectedresults: |
| 254 |
1. Success |
| 255 |
2. Success |
| 256 |
3. Success |
| 257 |
""" |
| 258 |
inst = topo.standalone |
| 259 |
users = UserAccounts(inst, DEFAULT_SUFFIX) |
| 260 |
user_properties = { |
| 261 |
'uid': 'my uid', |
| 262 |
'cn': 'my uid', |
| 263 |
'sn': 'user', |
| 264 |
'userpassword': PW_DM, |
| 265 |
'uidNumber': '111', |
| 266 |
'gidNumber': '111', |
| 267 |
'homeDirectory': '/home/testuser111' |
| 268 |
} |
| 269 |
# Step 1 the user RDN contains space |
| 270 |
user = users.create(properties=user_properties) |
| 271 |
|
| 272 |
# Step 2 authenticate escaping the space |
| 273 |
escaped_dn = 'uid=my\\20uid,%s' % ','.join(user.dn.split(',')[1:]) |
| 274 |
ldc = ldap.initialize(f'ldap://{inst.host}:{inst.port}') |
| 275 |
ldc.bind_s(escaped_dn, PW_DM) |
| 276 |
|
| 277 |
# Step 3 few searches |
| 278 |
assert len(ldc.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, "uid=my\\20uid")) == 1 |
| 279 |
assert len(ldc.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, "uid=my uid")) == 1 |
| 280 |
|
| 281 |
def fin(): |
| 282 |
try: |
| 283 |
user.delete() |
| 284 |
except ldap.LDAPError: |
| 285 |
pass |
| 286 |
try: |
| 287 |
ldc.unbind() |
| 288 |
except: |
| 289 |
pass |
| 290 |
|
| 291 |
request.addfinalizer(fin) |
| 292 |
|
| 293 |
|
| 294 |
if __name__ == '__main__': |
| 295 |
# Run isolated |
| 296 |
# -s for DEBUG mode |
| 297 |
CURRENT_FILE = os.path.realpath(__file__) |
| 298 |
pytest.main("-s %s" % CURRENT_FILE) |