| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2017 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 time, ldap, re, os |
| 12 |
from lib389.schema import Schema |
| 13 |
from lib389.utils import ensure_bytes |
| 14 |
from test389.topologies import topology_st as topo |
| 15 |
from lib389._constants import DEFAULT_SUFFIX, DN_DM, PW_DM |
| 16 |
from lib389._mapped_object import DSLdapObjects |
| 17 |
from lib389.idm.user import UserAccounts |
| 18 |
|
| 19 |
pytestmark = pytest.mark.tier1 |
| 20 |
|
| 21 |
DEBUGGING = os.getenv("DEBUGGING", default=False) |
| 22 |
if DEBUGGING: |
| 23 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 24 |
else: |
| 25 |
logging.getLogger(__name__).setLevel(logging.INFO) |
| 26 |
log = logging.getLogger(__name__) |
| 27 |
|
| 28 |
INVALID_SCHEMA = 'givenName $ cn $ MoZiLLaATTRiBuTe' |
| 29 |
|
| 30 |
|
| 31 |
def test_schema_reload_with_searches(topo): |
| 32 |
"""Test that during the schema reload task there is a small window where the new schema is not loaded |
| 33 |
into the asi hashtables - this results in searches not returning entries. |
| 34 |
|
| 35 |
:id: 375f1fdc-a9ef-45de-984d-0b79a40ff219 |
| 36 |
:setup: Standalone instance |
| 37 |
:steps: |
| 38 |
1. Create a test user |
| 39 |
2. Run a schema_reload task while searching for our user |
| 40 |
3. While we wait for the task to complete search for our user |
| 41 |
4. Check the user is still being returned and if task is complete |
| 42 |
:expectedresults: |
| 43 |
1. Operation should be successful |
| 44 |
2. Operation should be successful |
| 45 |
3. Operation should be successful |
| 46 |
4. Operation should be successful |
| 47 |
""" |
| 48 |
|
| 49 |
log.info('Test the searches still work as expected during schema reload tasks') |
| 50 |
|
| 51 |
# Add a user |
| 52 |
users = UserAccounts(topo.standalone, DEFAULT_SUFFIX) |
| 53 |
user = users.create_test_user(uid=1) |
| 54 |
|
| 55 |
# Run a schema_reload tasks while searching for our user.Since |
| 56 |
# this is a race condition, run it several times. |
| 57 |
schema = Schema(topo.standalone) |
| 58 |
task = schema.reload(schema_dir=topo.standalone.schemadir) |
| 59 |
|
| 60 |
# While we wait for the task to complete search for our user |
| 61 |
search_count = 0 |
| 62 |
while search_count < 10: |
| 63 |
# Now check the user is still being returned |
| 64 |
# Check if task is complete |
| 65 |
assert user.exists() |
| 66 |
if task.get_exit_code() == 0: |
| 67 |
break |
| 68 |
time.sleep(1) |
| 69 |
search_count += 1 |
| 70 |
|
| 71 |
|
| 72 |
def test_schema_operation(topo): |
| 73 |
"""Test that the cases in original schema are preserved. |
| 74 |
Test that duplicated schema except cases are not loaded |
| 75 |
Test to use a custom schema |
| 76 |
|
| 77 |
:id: e7448863-ac62-4b49-b013-4efa412c0455 |
| 78 |
:setup: Standalone instance |
| 79 |
:steps: |
| 80 |
1. Create a test schema with cases |
| 81 |
2. Run a schema_reload task |
| 82 |
3. Check the attribute is present |
| 83 |
4. Case 2: Check duplicated schema except cases are not loaded |
| 84 |
5. Case 2-1: Use the custom schema |
| 85 |
|
| 86 |
:expectedresults: |
| 87 |
1. Operation should be successful |
| 88 |
2. Operation should be successful |
| 89 |
3. Operation should be successful |
| 90 |
4. Operation should be successful |
| 91 |
5. Operation should be successful |
| 92 |
""" |
| 93 |
|
| 94 |
log.info('case 1: Test the cases in the original schema are preserved.') |
| 95 |
|
| 96 |
schema_filename = topo.standalone.schemadir + '/98test.ldif' |
| 97 |
try: |
| 98 |
with open(schema_filename, "w") as schema_file: |
| 99 |
schema_file.write("dn: cn=schema\n") |
| 100 |
schema_file.write("attributetypes: ( 8.9.10.11.12.13.14 NAME " + |
| 101 |
"'MoZiLLaaTTRiBuTe' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 " + |
| 102 |
" X-ORIGIN 'Mozilla Dummy Schema' )\n") |
| 103 |
schema_file.write("objectclasses: ( 1.2.3.4.5.6.7 NAME 'MozillaObject' " + |
| 104 |
"SUP top MUST ( objectclass $ cn ) MAY ( MoZiLLaaTTRiBuTe )" + |
| 105 |
" X-ORIGIN 'user defined' )')\n") |
| 106 |
os.chmod(schema_filename, 0o777) |
| 107 |
except OSError as e: |
| 108 |
log.fatal("Failed to create schema file: " + |
| 109 |
"{} Error: {}".format(schema_filename, str(e))) |
| 110 |
|
| 111 |
|
| 112 |
# run the schema reload task with the default schemadir |
| 113 |
schema = Schema(topo.standalone) |
| 114 |
task = schema.reload(schema_dir=topo.standalone.schemadir) |
| 115 |
task.wait() |
| 116 |
|
| 117 |
subschema = topo.standalone.schema.get_subschema() |
| 118 |
at_obj = subschema.get_obj(ldap.schema.AttributeType, 'MoZiLLaaTTRiBuTe') |
| 119 |
|
| 120 |
assert at_obj is not None, "The attribute was not found on server" |
| 121 |
|
| 122 |
log.info('Case 2: Duplicated schema except cases are not loaded.') |
| 123 |
|
| 124 |
schema_filename = topo.standalone.schemadir + '/97test.ldif' |
| 125 |
try: |
| 126 |
with open(schema_filename, "w") as schema_file: |
| 127 |
Mozattr1 = "MOZILLAATTRIBUTE" |
| 128 |
schema_file.write("dn: cn=schema\n") |
| 129 |
schema_file.write("attributetypes: ( 8.9.10.11.12.13.14 NAME " + |
| 130 |
"'MOZILLAATTRIBUTE' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 " + |
| 131 |
"X-ORIGIN 'Mozilla Dummy Schema' )\n") |
| 132 |
schema_file.write("objectclasses: ( 1.2.3.4.5.6.7 NAME 'MozillaObject' "+ |
| 133 |
"SUP top MUST ( objectclass $ cn ) MAY ( MOZILLAATTRIBUTE ) "+ |
| 134 |
"X-ORIGIN 'user defined' )')\n") |
| 135 |
os.chmod(schema_filename, 0o777) |
| 136 |
except OSError as e: |
| 137 |
log.fatal("Failed to create schema file: " + |
| 138 |
"{} Error: {}".format(schema_filename, str(e))) |
| 139 |
|
| 140 |
# run the schema reload task with the default schemadir |
| 141 |
task = schema.reload(schema_dir=topo.standalone.schemadir) |
| 142 |
task.wait() |
| 143 |
|
| 144 |
subschema_duplicate = topo.standalone.schema.get_subschema() |
| 145 |
at_obj_duplicate = subschema_duplicate.get_obj(ldap.schema.AttributeType, 'MOZILLAATTRIBUTE') |
| 146 |
|
| 147 |
moz = re.findall('MOZILLAATTRIBUTE',str(at_obj_duplicate)) |
| 148 |
if moz: |
| 149 |
log.error('case 2: MOZILLAATTRIBUTE is in the objectclasses list -- FAILURE') |
| 150 |
assert False |
| 151 |
else: |
| 152 |
log.info('case 2: MOZILLAATTRIBUTE is not in the objectclasses list -- PASS') |
| 153 |
|
| 154 |
Mozattr2 = "mozillaattribute" |
| 155 |
log.info(f'Case 2-1: Use the custom schema with {Mozattr2}') |
| 156 |
name = "test_user" |
| 157 |
ld = ldap.initialize(topo.standalone.get_ldap_uri()) |
| 158 |
ld.simple_bind_s(DN_DM,PW_DM) |
| 159 |
ld.add_s(f"cn={name},{DEFAULT_SUFFIX}",[('objectclass', [b'top', b'person', b'MozillaObject']), |
| 160 |
('sn', [ensure_bytes(name)]), |
| 161 |
('cn', [ensure_bytes(name)]), |
| 162 |
(Mozattr2, [ensure_bytes(name)]) |
| 163 |
]) |
| 164 |
|
| 165 |
mozattrval = DSLdapObjects(topo.standalone,DEFAULT_SUFFIX).filter('(objectclass=mozillaobject)')[0] |
| 166 |
assert mozattrval.get_attr_val_utf8('mozillaattribute') == name |
| 167 |
|
| 168 |
|
| 169 |
def test_valid_schema(topo): |
| 170 |
"""Test schema-reload task with valid schema |
| 171 |
|
| 172 |
:id: 2ab304c0-3e58-4d34-b23b-a14b5997c7a8 |
| 173 |
:setup: Standalone instance |
| 174 |
:steps: |
| 175 |
1. Create schema file with valid schema |
| 176 |
2. Run schema-reload.pl script |
| 177 |
3. Run ldapsearch and check if schema was added |
| 178 |
:expectedresults: |
| 179 |
1. File creation should work |
| 180 |
2. The schema reload task should be successful |
| 181 |
3. Searching the server should return the new schema |
| 182 |
""" |
| 183 |
|
| 184 |
log.info("Test schema-reload task with valid schema") |
| 185 |
|
| 186 |
# Step 1 - Create schema file |
| 187 |
log.info("Create valid schema file (99user.ldif)...") |
| 188 |
schema_filename = (topo.standalone.schemadir + "/99user.ldif") |
| 189 |
try: |
| 190 |
with open(schema_filename, 'w') as schema_file: |
| 191 |
schema_file.write("dn: cn=schema\n") |
| 192 |
schema_file.write("attributetypes: ( 8.9.10.11.12.13.13 NAME " + |
| 193 |
"'ValidAttribute' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15" + |
| 194 |
" X-ORIGIN 'Mozilla Dummy Schema' )\n") |
| 195 |
schema_file.write("objectclasses: ( 1.2.3.4.5.6.7.8 NAME 'TestObject' " + |
| 196 |
"SUP top MUST ( objectclass $ cn ) MAY ( givenName $ " + |
| 197 |
"sn $ ValidAttribute ) X-ORIGIN 'user defined' )')\n") |
| 198 |
os.chmod(schema_filename, 0o777) |
| 199 |
except OSError as e: |
| 200 |
log.fatal("Failed to create schema file: " + |
| 201 |
"{} Error: {}".format(schema_filename, str(e))) |
| 202 |
|
| 203 |
# Step 2 - Run the schema-reload task |
| 204 |
log.info("Run the schema-reload task...") |
| 205 |
schema = Schema(topo.standalone) |
| 206 |
task = schema.reload(schema_dir=topo.standalone.schemadir) |
| 207 |
task.wait() |
| 208 |
assert task.get_exit_code() == 0, "The schema reload task failed" |
| 209 |
|
| 210 |
# Step 3 - Verify valid schema was added to the server |
| 211 |
log.info("Check cn=schema to verify the valid schema was added") |
| 212 |
subschema = topo.standalone.schema.get_subschema() |
| 213 |
|
| 214 |
oc_obj = subschema.get_obj(ldap.schema.ObjectClass, 'TestObject') |
| 215 |
assert oc_obj is not None, "The new objectclass was not found on server" |
| 216 |
|
| 217 |
at_obj = subschema.get_obj(ldap.schema.AttributeType, 'ValidAttribute') |
| 218 |
assert at_obj is not None, "The new attribute was not found on server" |
| 219 |
|
| 220 |
|
| 221 |
def test_invalid_schema(topo): |
| 222 |
"""Test schema-reload task with invalid schema |
| 223 |
|
| 224 |
:id: 2ab304c0-3e58-4d34-b23b-a14b5997c7a9 |
| 225 |
:setup: Standalone instance |
| 226 |
:steps: |
| 227 |
1. Create schema files with invalid schema |
| 228 |
2. Run schema-reload.pl script |
| 229 |
3. Run ldapsearch and check if schema was added |
| 230 |
:expectedresults: |
| 231 |
1. File creation should work |
| 232 |
2. The schema reload task should return an error |
| 233 |
3. Searching the server should not return the invalid schema |
| 234 |
""" |
| 235 |
log.info("Test schema-reload task with invalid schema") |
| 236 |
|
| 237 |
# Step 1 - Create schema files: one valid, one invalid |
| 238 |
log.info("Create valid schema file (98user.ldif)...") |
| 239 |
schema_filename = (topo.standalone.schemadir + "/98user.ldif") |
| 240 |
try: |
| 241 |
with open(schema_filename, 'w') as schema_file: |
| 242 |
schema_file.write("dn: cn=schema\n") |
| 243 |
schema_file.write("attributetypes: ( 8.9.10.11.12.13.14 NAME " + |
| 244 |
"'MozillaAttribute' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15" + |
| 245 |
" X-ORIGIN 'Mozilla Dummy Schema' )\n") |
| 246 |
schema_file.write("objectclasses: ( 1.2.3.4.5.6.7 NAME 'MoZiLLaOBJeCT' " + |
| 247 |
"SUP top MUST ( objectclass $ cn ) MAY ( givenName $ " + |
| 248 |
"sn $ MozillaAttribute ) X-ORIGIN 'user defined' )')\n") |
| 249 |
os.chmod(schema_filename, 0o777) |
| 250 |
except OSError as e: |
| 251 |
log.fatal("Failed to create schema file: " + |
| 252 |
"{} Error: {}".format(schema_filename, str(e))) |
| 253 |
|
| 254 |
log.info("Create invalid schema file (99user.ldif)...") |
| 255 |
schema_filename = (topo.standalone.schemadir + "/99user.ldif") |
| 256 |
try: |
| 257 |
with open(schema_filename, 'w') as schema_file: |
| 258 |
schema_file.write("dn: cn=schema\n") |
| 259 |
# Same attribute/objclass names, but different OIDs and MAY attributes |
| 260 |
schema_file.write("attributetypes: ( 8.9.10.11.12.13.140 NAME " + |
| 261 |
"'MozillaAttribute' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15" + |
| 262 |
" X-ORIGIN 'Mozilla Dummy Schema' )\n") |
| 263 |
schema_file.write("objectclasses: ( 1.2.3.4.5.6.70 NAME 'MoZiLLaOBJeCT' " + |
| 264 |
"SUP top MUST ( objectclass $ cn ) MAY ( givenName $ " + |
| 265 |
"cn $ MoZiLLaATTRiBuTe ) X-ORIGIN 'user defined' )')\n") |
| 266 |
os.chmod(schema_filename, 0o777) |
| 267 |
except OSError as e: |
| 268 |
log.fatal("Failed to create schema file: " + |
| 269 |
"{} Error: {}".format(schema_filename, str(e))) |
| 270 |
|
| 271 |
# Step 2 - Run the schema-reload task |
| 272 |
log.info("Run the schema-reload task, it should fail...") |
| 273 |
schema = Schema(topo.standalone) |
| 274 |
task = schema.reload(schema_dir=topo.standalone.schemadir) |
| 275 |
task.wait() |
| 276 |
assert task.get_exit_code() != 0, f"The schema reload task incorectly reported success{task.get_exit_code()}" |
| 277 |
|
| 278 |
# Step 3 - Verify invalid schema was not added to the server |
| 279 |
log.info("Check cn=schema to verify the invalid schema was not added") |
| 280 |
subschema = topo.standalone.schema.get_subschema() |
| 281 |
oc_obj = subschema.get_obj(ldap.schema.ObjectClass, 'MoZiLLaOBJeCT') |
| 282 |
if oc_obj is not None and INVALID_SCHEMA in str(oc_obj): |
| 283 |
log.fatal("The invalid schema was returned from the server: " + str(oc_obj)) |
| 284 |
assert False |
| 285 |
else: |
| 286 |
log.info("The invalid schema is not present on the server") |
| 287 |
|
| 288 |
|
| 289 |
if __name__ == '__main__': |
| 290 |
# Run isolated |
| 291 |
# -s for DEBUG mode |
| 292 |
CURRENT_FILE = os.path.realpath(__file__) |
| 293 |
pytest.main("-s %s" % CURRENT_FILE) |