| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2020 William Brown <[email protected]> |
| 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 time |
| 12 |
import shutil |
| 13 |
import uuid |
| 14 |
import subprocess |
| 15 |
import pytest |
| 16 |
import logging |
| 17 |
from lib389.idm.user import nsUserAccounts, UserAccounts |
| 18 |
from lib389.idm.account import Accounts |
| 19 |
from lib389.idm.domain import Domain |
| 20 |
from test389.topologies import topology_st as topology |
| 21 |
from lib389.backend import Backends |
| 22 |
from lib389.paths import Paths |
| 23 |
from lib389.utils import ds_is_older |
| 24 |
from lib389._constants import * |
| 25 |
from lib389.plugins import EntryUUIDPlugin |
| 26 |
|
| 27 |
default_paths = Paths() |
| 28 |
|
| 29 |
pytestmark = pytest.mark.tier1 |
| 30 |
log = logging.getLogger(__name__) |
| 31 |
|
| 32 |
DATADIR1 = os.path.join(os.path.dirname(__file__), '../../data/entryuuid/') |
| 33 |
IMPORT_UUID_A = "973e1bbf-ba9c-45d4-b01b-ff7371fd9008" |
| 34 |
UUID_BETWEEN = "eeeeeeee-0000-0000-0000-000000000000" |
| 35 |
IMPORT_UUID_B = "f6df8fe9-6b30-46aa-aa13-f0bf755371e8" |
| 36 |
UUID_MIN = "00000000-0000-0000-0000-000000000000" |
| 37 |
UUID_MAX = "ffffffff-ffff-ffff-ffff-ffffffffffff" |
| 38 |
|
| 39 |
@pytest.mark.skipif(ds_is_older('1.4.3.27'), reason="CLI Entryuuid is not available in prior versions") |
| 40 |
def test_cli_entryuuid_plugin_fixup(topology): |
| 41 |
"""Test that dsconf CLI entryuuid attribute is enabled and can execute. |
| 42 |
|
| 43 |
:id: 91b46be2-ac3f-11ec-a38a-98fa9ba19b65 |
| 44 |
:parametrized: yes |
| 45 |
:customerscenario: True |
| 46 |
:setup: Standalone Instance |
| 47 |
:steps: |
| 48 |
1. Create DS Instance |
| 49 |
2. Create a user "jdoe" with a dn |
| 50 |
3. Verify dsconf command is working correctly with plugin entryuuid fixup |
| 51 |
|
| 52 |
:expectedresults: |
| 53 |
1. Success |
| 54 |
2. Success |
| 55 |
3. Success |
| 56 |
|
| 57 |
""" |
| 58 |
log.info("Use dsconf tool to configure entryuuid plugin") |
| 59 |
parent = "ou=People,dc=example,dc=com" |
| 60 |
name = 'jdoe' |
| 61 |
dn = 'uid=%s,%s' % (name, parent) |
| 62 |
log.info('Testing with User created for dn :{} .'.format(dn)) |
| 63 |
cmd=['/usr/sbin/dsconf',topology.standalone.get_ldap_uri(),'-D',DN_DM,'-w','password','plugin','entryuuid','fixup',dn] |
| 64 |
log.info(f'Dsconf Command used : %{cmd}') |
| 65 |
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
| 66 |
msg = proc.communicate() |
| 67 |
log.info(f'output message : {msg[0]}') |
| 68 |
assert proc.returncode == 0 |
| 69 |
|
| 70 |
|
| 71 |
def _entryuuid_import_and_search(topology): |
| 72 |
# 1 |
| 73 |
ldif_dir = topology.standalone.get_ldif_dir() |
| 74 |
target_ldif = os.path.join(ldif_dir, 'localhost-userRoot-2020_03_30_13_14_47.ldif') |
| 75 |
import_ldif = os.path.join(DATADIR1, 'localhost-userRoot-2020_03_30_13_14_47.ldif') |
| 76 |
shutil.copyfile(import_ldif, target_ldif) |
| 77 |
os.chmod(target_ldif, 0o777) |
| 78 |
|
| 79 |
be = Backends(topology.standalone).get('userRoot') |
| 80 |
task = be.import_ldif([target_ldif]) |
| 81 |
task.wait() |
| 82 |
assert(task.is_complete() and task.get_exit_code() == 0) |
| 83 |
|
| 84 |
accounts = Accounts(topology.standalone, DEFAULT_SUFFIX) |
| 85 |
# 2 - positive eq test |
| 86 |
r2 = accounts.filter("(entryUUID=%s)" % IMPORT_UUID_A) |
| 87 |
assert(len(r2) == 1) |
| 88 |
r3 = accounts.filter("(entryuuid=%s)" % IMPORT_UUID_B) |
| 89 |
assert(len(r3) == 1) |
| 90 |
# 3 - negative eq test |
| 91 |
r4 = accounts.filter("(entryuuid=%s)" % UUID_MAX) |
| 92 |
assert(len(r4) == 0) |
| 93 |
# 4 - le search |
| 94 |
r5 = accounts.filter("(entryuuid<=%s)" % UUID_BETWEEN) |
| 95 |
assert(len(r5) == 1) |
| 96 |
# 5 - ge search |
| 97 |
r6 = accounts.filter("(entryuuid>=%s)" % UUID_BETWEEN) |
| 98 |
assert(len(r6) == 1) |
| 99 |
# 6 - le 0 search |
| 100 |
r7 = accounts.filter("(entryuuid<=%s)" % UUID_MIN) |
| 101 |
assert(len(r7) == 0) |
| 102 |
# 7 - ge f search |
| 103 |
r8 = accounts.filter("(entryuuid>=%s)" % UUID_MAX) |
| 104 |
assert(len(r8) == 0) |
| 105 |
# 8 - export db |
| 106 |
task = be.export_ldif() |
| 107 |
task.wait() |
| 108 |
assert(task.is_complete() and task.get_exit_code() == 0) |
| 109 |
|
| 110 |
|
| 111 |
@pytest.mark.skipif(not default_paths.rust_enabled or ds_is_older('1.4.2.0'), reason="Entryuuid is not available in older versions") |
| 112 |
def test_entryuuid_indexed_import_and_search(topology): |
| 113 |
""" Test that an ldif of entries containing entryUUID's can be indexed and searched |
| 114 |
correctly. As https://tools.ietf.org/html/rfc4530 states, the MR's are equality and |
| 115 |
ordering, so we check these are correct. |
| 116 |
|
| 117 |
:id: c98ee6dc-a7ee-4bd4-974d-597ea966dad9 |
| 118 |
|
| 119 |
:setup: Standalone instance |
| 120 |
|
| 121 |
:steps: |
| 122 |
1. Import the db from the ldif |
| 123 |
2. EQ search for an entryuuid (match) |
| 124 |
3. EQ search for an entryuuid that does not exist |
| 125 |
4. LE search for an entryuuid lower (1 res) |
| 126 |
5. GE search for an entryuuid greater (1 res) |
| 127 |
6. LE for the 0 uuid (0 res) |
| 128 |
7. GE for the f uuid (0 res) |
| 129 |
8. export the db to ldif |
| 130 |
|
| 131 |
:expectedresults: |
| 132 |
1. Success |
| 133 |
2. 1 match |
| 134 |
3. 0 match |
| 135 |
4. 1 match |
| 136 |
5. 1 match |
| 137 |
6. 0 match |
| 138 |
7. 0 match |
| 139 |
8. success |
| 140 |
""" |
| 141 |
# Assert that the index correctly exists. |
| 142 |
be = Backends(topology.standalone).get('userRoot') |
| 143 |
indexes = be.get_indexes() |
| 144 |
indexes.ensure_state(properties={ |
| 145 |
'cn': 'entryUUID', |
| 146 |
'nsSystemIndex': 'false', |
| 147 |
'nsIndexType': ['eq', 'pres'], |
| 148 |
}) |
| 149 |
_entryuuid_import_and_search(topology) |
| 150 |
|
| 151 |
@pytest.mark.skipif(not default_paths.rust_enabled or ds_is_older('1.4.2.0'), reason="Entryuuid is not available in older versions") |
| 152 |
def test_entryuuid_unindexed_import_and_search(topology): |
| 153 |
""" Test that an ldif of entries containing entryUUID's can be UNindexed searched |
| 154 |
correctly. As https://tools.ietf.org/html/rfc4530 states, the MR's are equality and |
| 155 |
ordering, so we check these are correct. |
| 156 |
|
| 157 |
:id: b652b54d-f009-464b-b5bd-299a33f97243 |
| 158 |
|
| 159 |
:setup: Standalone instance |
| 160 |
|
| 161 |
:steps: |
| 162 |
1. Import the db from the ldif |
| 163 |
2. EQ search for an entryuuid (match) |
| 164 |
3. EQ search for an entryuuid that does not exist |
| 165 |
4. LE search for an entryuuid lower (1 res) |
| 166 |
5. GE search for an entryuuid greater (1 res) |
| 167 |
6. LE for the 0 uuid (0 res) |
| 168 |
7. GE for the f uuid (0 res) |
| 169 |
8. export the db to ldif |
| 170 |
|
| 171 |
:expectedresults: |
| 172 |
1. Success |
| 173 |
2. 1 match |
| 174 |
3. 0 match |
| 175 |
4. 1 match |
| 176 |
5. 1 match |
| 177 |
6. 0 match |
| 178 |
7. 0 match |
| 179 |
8. success |
| 180 |
""" |
| 181 |
# Assert that the index does NOT exist for this test. |
| 182 |
be = Backends(topology.standalone).get('userRoot') |
| 183 |
indexes = be.get_indexes() |
| 184 |
try: |
| 185 |
idx = indexes.get('entryUUID') |
| 186 |
idx.delete() |
| 187 |
except ldap.NO_SUCH_OBJECT: |
| 188 |
# It's already not present, move along, nothing to see here. |
| 189 |
pass |
| 190 |
_entryuuid_import_and_search(topology) |
| 191 |
|
| 192 |
# Test entryUUID generation |
| 193 |
@pytest.mark.skipif(not default_paths.rust_enabled or ds_is_older('1.4.2.0'), reason="Entryuuid is not available in older versions") |
| 194 |
def test_entryuuid_generation_on_add(topology): |
| 195 |
""" Test that when an entry is added, the entryuuid is added. |
| 196 |
|
| 197 |
:id: a7439b0a-dcee-4cd6-b8ef-771476c0b4f6 |
| 198 |
|
| 199 |
:setup: Standalone instance |
| 200 |
|
| 201 |
:steps: |
| 202 |
1. Create a new entry in the db |
| 203 |
2. Check it has an entry uuid |
| 204 |
|
| 205 |
:expectedresults: |
| 206 |
1. Success |
| 207 |
2. An entry uuid is present |
| 208 |
""" |
| 209 |
# Step one - create a user! |
| 210 |
account = nsUserAccounts(topology.standalone, DEFAULT_SUFFIX).create_test_user() |
| 211 |
# Step two - does it have an entryuuid? |
| 212 |
euuid = account.get_attr_val_utf8('entryUUID') |
| 213 |
print(euuid) |
| 214 |
assert(euuid is not None) |
| 215 |
|
| 216 |
# Test fixup task |
| 217 |
@pytest.mark.skipif(not default_paths.rust_enabled or ds_is_older('1.4.2.0'), reason="Entryuuid is not available in older versions") |
| 218 |
def test_entryuuid_fixup_task(topology): |
| 219 |
"""Test that when an entries without UUID's can have one generated via |
| 220 |
the fixup process. |
| 221 |
|
| 222 |
:id: ad42bba2-ffb2-4c22-a37d-cbe7bcf73d6b |
| 223 |
|
| 224 |
:setup: Standalone instance |
| 225 |
|
| 226 |
:steps: |
| 227 |
1. Disable the entryuuid plugin |
| 228 |
2. Create an entry |
| 229 |
3. Enable the entryuuid plugin |
| 230 |
4. Run the fixup |
| 231 |
5. Assert the entryuuid now exists |
| 232 |
6. Restart and check they persist |
| 233 |
|
| 234 |
:expectedresults: |
| 235 |
1. Success |
| 236 |
2. Success |
| 237 |
3. Success |
| 238 |
4. Success |
| 239 |
5. Suddenly EntryUUID! |
| 240 |
6. Still has EntryUUID! |
| 241 |
""" |
| 242 |
# 1. Disable the plugin |
| 243 |
plug = EntryUUIDPlugin(topology.standalone) |
| 244 |
plug.disable() |
| 245 |
topology.standalone.restart() |
| 246 |
|
| 247 |
# 2. create the account |
| 248 |
account = nsUserAccounts(topology.standalone, DEFAULT_SUFFIX).create_test_user(uid=2000) |
| 249 |
euuid = account.get_attr_val_utf8('entryUUID') |
| 250 |
assert(euuid is None) |
| 251 |
|
| 252 |
# 3. enable the plugin |
| 253 |
plug.enable() |
| 254 |
topology.standalone.restart() |
| 255 |
|
| 256 |
# 4. run the fix up |
| 257 |
# For now set the log level to high! |
| 258 |
topology.standalone.config.loglevel(vals=(ErrorLog.DEFAULT,ErrorLog.PLUGIN)) |
| 259 |
task = plug.fixup(DEFAULT_SUFFIX) |
| 260 |
task.wait() |
| 261 |
assert(task.is_complete() and task.get_exit_code() == 0) |
| 262 |
topology.standalone.config.loglevel(vals=(ErrorLog.DEFAULT,)) |
| 263 |
|
| 264 |
# 5.1 Assert the uuid on the user. |
| 265 |
euuid_user = account.get_attr_val_utf8('entryUUID') |
| 266 |
assert(euuid_user is not None) |
| 267 |
|
| 268 |
# 5.2 Assert it on the domain entry. |
| 269 |
domain = Domain(topology.standalone, dn=DEFAULT_SUFFIX) |
| 270 |
euuid_domain = domain.get_attr_val_utf8('entryUUID') |
| 271 |
assert(euuid_domain is not None) |
| 272 |
|
| 273 |
# Assert it persists after a restart. |
| 274 |
topology.standalone.restart() |
| 275 |
# 6.1 Assert the uuid on the use. |
| 276 |
euuid_user_2 = account.get_attr_val_utf8('entryUUID') |
| 277 |
assert(euuid_user_2 == euuid_user) |
| 278 |
|
| 279 |
# 6.2 Assert it on the domain entry. |
| 280 |
euuid_domain_2 = domain.get_attr_val_utf8('entryUUID') |
| 281 |
assert(euuid_domain_2 == euuid_domain) |
| 282 |
|
| 283 |
@pytest.mark.skipif(not default_paths.rust_enabled or ds_is_older('1.4.2.0'), reason="Entryuuid is not available in older versions") |
| 284 |
def test_entryuuid_import_and_fixup_of_invalid_values(topology): |
| 285 |
""" Test that when we import a database with an invalid entryuuid |
| 286 |
that it is accepted *and* that subsequently we can fix the invalid |
| 287 |
entryuuid during a fixup. |
| 288 |
|
| 289 |
:id: ec8ef3a7-3cd2-4cbd-b6f1-2449fa17be75 |
| 290 |
|
| 291 |
:setup: Standalone instance |
| 292 |
|
| 293 |
:steps: |
| 294 |
1. Import the db from the ldif |
| 295 |
2. Check the entryuuid is invalid |
| 296 |
3. Run the fixup |
| 297 |
4. Check the entryuuid is now valid (regenerated) |
| 298 |
|
| 299 |
:expectedresults: |
| 300 |
1. Success |
| 301 |
2. The entryuuid is invalid |
| 302 |
3. Success |
| 303 |
4. The entryuuid is valid |
| 304 |
""" |
| 305 |
|
| 306 |
# 1. Import the db |
| 307 |
ldif_dir = topology.standalone.get_ldif_dir() |
| 308 |
target_ldif = os.path.join(ldif_dir, 'localhost-userRoot-invalid.ldif') |
| 309 |
import_ldif = os.path.join(DATADIR1, 'localhost-userRoot-invalid.ldif') |
| 310 |
shutil.copyfile(import_ldif, target_ldif) |
| 311 |
os.chmod(target_ldif, 0o777) |
| 312 |
|
| 313 |
be = Backends(topology.standalone).get('userRoot') |
| 314 |
task = be.import_ldif([target_ldif]) |
| 315 |
task.wait() |
| 316 |
assert(task.is_complete() and task.get_exit_code() == 0) |
| 317 |
|
| 318 |
# 2. Check the entryuuid is invalid |
| 319 |
account = nsUserAccounts(topology.standalone, DEFAULT_SUFFIX).get("demo_user") |
| 320 |
euuid = account.get_attr_val_utf8('entryUUID') |
| 321 |
assert(euuid == "INVALID_UUID") |
| 322 |
|
| 323 |
# 3. Run the fixup |
| 324 |
topology.standalone.config.loglevel(vals=(ErrorLog.DEFAULT,ErrorLog.PLUGIN)) |
| 325 |
plug = EntryUUIDPlugin(topology.standalone) |
| 326 |
task = plug.fixup(DEFAULT_SUFFIX) |
| 327 |
task.wait() |
| 328 |
assert(task.is_complete() and task.get_exit_code() == 0) |
| 329 |
topology.standalone.config.loglevel(vals=(ErrorLog.DEFAULT,)) |
| 330 |
|
| 331 |
# 4. Check the entryuuid is valid |
| 332 |
euuid = account.get_attr_val_utf8('entryUUID') |
| 333 |
print(f"❄️ account entryUUID -> {euuid}"); |
| 334 |
assert(euuid != "INVALID_UUID") |
| 335 |
# Raises an error if invalid |
| 336 |
uuid.UUID(euuid) |
| 337 |
|