Blame SOURCES/0002-Issue-4701-RFE-Exclude-attributes-from-retro-changel.patch

e4a41f
From 1e1c2b23c35282481628af7e971ac683da334502 Mon Sep 17 00:00:00 2001
e4a41f
From: James Chapman <jachapma@redhat.com>
e4a41f
Date: Tue, 27 Apr 2021 17:00:15 +0100
e4a41f
Subject: [PATCH 02/12] Issue 4701 - RFE - Exclude attributes from retro
e4a41f
 changelog (#4723)
e4a41f
e4a41f
Description: When the retro changelog plugin is enabled it writes the
e4a41f
             added/modified values to the "cn-changelog" suffix. In
e4a41f
             some cases an entries attribute values can be of a
e4a41f
             sensitive nature and should be excluded. This RFE adds
e4a41f
             functionality that will allow an admin exclude certain
e4a41f
             attributes from the retro changelog DB.
e4a41f
e4a41f
Relates: https://github.com/389ds/389-ds-base/issues/4701
e4a41f
e4a41f
Reviewed by: mreynolds389, droideck (Thanks folks)
e4a41f
---
e4a41f
 .../tests/suites/retrocl/basic_test.py        | 292 ++++++++++++++++++
e4a41f
 1 file changed, 292 insertions(+)
e4a41f
 create mode 100644 dirsrvtests/tests/suites/retrocl/basic_test.py
e4a41f
e4a41f
diff --git a/dirsrvtests/tests/suites/retrocl/basic_test.py b/dirsrvtests/tests/suites/retrocl/basic_test.py
e4a41f
new file mode 100644
e4a41f
index 000000000..112c73cb9
e4a41f
--- /dev/null
e4a41f
+++ b/dirsrvtests/tests/suites/retrocl/basic_test.py
e4a41f
@@ -0,0 +1,292 @@
e4a41f
+# --- BEGIN COPYRIGHT BLOCK ---
e4a41f
+# Copyright (C) 2021 Red Hat, Inc.
e4a41f
+# All rights reserved.
e4a41f
+#
e4a41f
+# License: GPL (version 3 or any later version).
e4a41f
+# See LICENSE for details.
e4a41f
+# --- END COPYRIGHT BLOCK ---
e4a41f
+
e4a41f
+import logging
e4a41f
+import ldap
e4a41f
+import time
e4a41f
+import pytest
e4a41f
+from lib389.topologies import topology_st
e4a41f
+from lib389.plugins import RetroChangelogPlugin
e4a41f
+from lib389._constants import *
e4a41f
+from lib389.utils import *
e4a41f
+from lib389.tasks import *
e4a41f
+from lib389.cli_base import FakeArgs, connect_instance, disconnect_instance
e4a41f
+from lib389.cli_base.dsrc import dsrc_arg_concat
e4a41f
+from lib389.cli_conf.plugins.retrochangelog import retrochangelog_add
e4a41f
+from lib389.idm.user import UserAccount, UserAccounts, nsUserAccounts
e4a41f
+
e4a41f
+pytestmark = pytest.mark.tier1
e4a41f
+
e4a41f
+USER1_DN = 'uid=user1,ou=people,'+ DEFAULT_SUFFIX
e4a41f
+USER2_DN = 'uid=user2,ou=people,'+ DEFAULT_SUFFIX
e4a41f
+USER_PW = 'password'
e4a41f
+ATTR_HOMEPHONE = 'homePhone'
e4a41f
+ATTR_CARLICENSE = 'carLicense'
e4a41f
+
e4a41f
+log = logging.getLogger(__name__)
e4a41f
+
e4a41f
+def test_retrocl_exclude_attr_add(topology_st):
e4a41f
+    """ Test exclude attribute feature of the retrocl plugin for add operation
e4a41f
+
e4a41f
+    :id: 3481650f-2070-45ef-9600-2500cfc51559
e4a41f
+
e4a41f
+    :setup: Standalone instance
e4a41f
+
e4a41f
+    :steps:
e4a41f
+        1. Enable dynamic plugins
e4a41f
+        2. Confige retro changelog plugin
e4a41f
+        3. Add an entry
e4a41f
+        4. Ensure entry attrs are in the changelog
e4a41f
+        5. Exclude an attr
e4a41f
+        6. Add another entry
e4a41f
+        7. Ensure excluded attr is not in the changelog
e4a41f
+
e4a41f
+    :expectedresults:
e4a41f
+        1. Success
e4a41f
+        2. Success
e4a41f
+        3. Success
e4a41f
+        4. Success
e4a41f
+        5. Success
e4a41f
+        6. Success
e4a41f
+        7. Success
e4a41f
+    """
e4a41f
+
e4a41f
+    st = topology_st.standalone
e4a41f
+
e4a41f
+    log.info('Enable dynamic plugins')
e4a41f
+    try:
e4a41f
+        st.config.set('nsslapd-dynamic-plugins', 'on')
e4a41f
+    except ldap.LDAPError as e:
e4a41f
+        ldap.error('Failed to enable dynamic plugins ' + e.args[0]['desc'])
e4a41f
+        assert False
e4a41f
+
e4a41f
+    log.info('Configure retrocl plugin')
e4a41f
+    rcl = RetroChangelogPlugin(st)
e4a41f
+    rcl.disable()
e4a41f
+    rcl.enable()
e4a41f
+    rcl.replace('nsslapd-attribute', 'nsuniqueid:targetUniqueId')
e4a41f
+
e4a41f
+    log.info('Restarting instance')
e4a41f
+    try:
e4a41f
+        st.restart()
e4a41f
+    except ldap.LDAPError as e:
e4a41f
+        ldap.error('Failed to restart instance ' + e.args[0]['desc'])
e4a41f
+        assert False
e4a41f
+
e4a41f
+    users = UserAccounts(st, DEFAULT_SUFFIX)
e4a41f
+
e4a41f
+    log.info('Adding user1')
e4a41f
+    try:
e4a41f
+        user1 = users.create(properties={
e4a41f
+            'sn': '1',
e4a41f
+            'cn': 'user 1',
e4a41f
+            'uid': 'user1',
e4a41f
+            'uidNumber': '11',
e4a41f
+            'gidNumber': '111',
e4a41f
+            'givenname': 'user1',
e4a41f
+            'homePhone': '0861234567',
e4a41f
+            'carLicense': '131D16674',
e4a41f
+            'mail': 'user1@whereever.com',
e4a41f
+            'homeDirectory': '/home/user1',
e4a41f
+            'userpassword': USER_PW})
e4a41f
+    except ldap.ALREADY_EXISTS:
e4a41f
+        pass
e4a41f
+    except ldap.LDAPError as e:
e4a41f
+        log.error("Failed to add user1")
e4a41f
+
e4a41f
+    log.info('Verify homePhone and carLicense attrs are in the changelog changestring')
e4a41f
+    try:
e4a41f
+        cllist = st.search_s(RETROCL_SUFFIX, ldap.SCOPE_SUBTREE, '(targetDn=%s)' % USER1_DN)
e4a41f
+    except ldap.LDAPError as e:
e4a41f
+        log.fatal("Changelog search failed, error: " +str(e))
e4a41f
+        assert False
e4a41f
+    assert len(cllist) > 0
e4a41f
+    if  cllist[0].hasAttr('changes'):
e4a41f
+        clstr = (cllist[0].getValue('changes')).decode()
e4a41f
+        assert ATTR_HOMEPHONE in clstr
e4a41f
+        assert ATTR_CARLICENSE in clstr
e4a41f
+
e4a41f
+    log.info('Excluding attribute ' + ATTR_HOMEPHONE)
e4a41f
+    args = FakeArgs()
e4a41f
+    args.connections = [st.host + ':' + str(st.port) + ':' + DN_DM + ':' + PW_DM]
e4a41f
+    args.instance = 'standalone1'
e4a41f
+    args.basedn = None
e4a41f
+    args.binddn = None
e4a41f
+    args.starttls = False
e4a41f
+    args.pwdfile = None
e4a41f
+    args.bindpw = None
e4a41f
+    args.prompt = False
e4a41f
+    args.exclude_attrs = ATTR_HOMEPHONE
e4a41f
+    args.func = retrochangelog_add
e4a41f
+    dsrc_inst = dsrc_arg_concat(args, None)
e4a41f
+    inst = connect_instance(dsrc_inst, False, args)
e4a41f
+    result = args.func(inst, None, log, args)
e4a41f
+    disconnect_instance(inst)
e4a41f
+    assert result is None
e4a41f
+
e4a41f
+    log.info("5s delay for retrocl plugin to restart")
e4a41f
+    time.sleep(5)
e4a41f
+
e4a41f
+    log.info('Adding user2')
e4a41f
+    try:
e4a41f
+        user2 = users.create(properties={
e4a41f
+            'sn': '2',
e4a41f
+            'cn': 'user 2',
e4a41f
+            'uid': 'user2',
e4a41f
+            'uidNumber': '22',
e4a41f
+            'gidNumber': '222',
e4a41f
+            'givenname': 'user2',
e4a41f
+            'homePhone': '0879088363',
e4a41f
+            'carLicense': '04WX11038',
e4a41f
+            'mail': 'user2@whereever.com',
e4a41f
+            'homeDirectory': '/home/user2',
e4a41f
+            'userpassword': USER_PW})
e4a41f
+    except ldap.ALREADY_EXISTS:
e4a41f
+        pass
e4a41f
+    except ldap.LDAPError as e:
e4a41f
+        log.error("Failed to add user2")
e4a41f
+
e4a41f
+    log.info('Verify homePhone attr is not in the changelog changestring')
e4a41f
+    try:
e4a41f
+        cllist = st.search_s(RETROCL_SUFFIX, ldap.SCOPE_SUBTREE, '(targetDn=%s)' % USER2_DN)
e4a41f
+        assert len(cllist) > 0
e4a41f
+        if  cllist[0].hasAttr('changes'):
e4a41f
+            clstr = (cllist[0].getValue('changes')).decode()
e4a41f
+            assert ATTR_HOMEPHONE not in clstr
e4a41f
+            assert ATTR_CARLICENSE in clstr
e4a41f
+    except ldap.LDAPError as e:
e4a41f
+        log.fatal("Changelog search failed, error: " +str(e))
e4a41f
+        assert False
e4a41f
+
e4a41f
+def test_retrocl_exclude_attr_mod(topology_st):
e4a41f
+    """ Test exclude attribute feature of the retrocl plugin for mod operation
e4a41f
+
e4a41f
+    :id: f6bef689-685b-4f86-a98d-f7e6b1fcada3
e4a41f
+
e4a41f
+    :setup: Standalone instance
e4a41f
+
e4a41f
+    :steps:
e4a41f
+        1. Enable dynamic plugins
e4a41f
+        2. Confige retro changelog plugin
e4a41f
+        3. Add user1 entry
e4a41f
+        4. Ensure entry attrs are in the changelog
e4a41f
+        5. Exclude an attr
e4a41f
+        6. Modify user1 entry
e4a41f
+        7. Ensure excluded attr is not in the changelog
e4a41f
+
e4a41f
+    :expectedresults:
e4a41f
+        1. Success
e4a41f
+        2. Success
e4a41f
+        3. Success
e4a41f
+        4. Success
e4a41f
+        5. Success
e4a41f
+        6. Success
e4a41f
+        7. Success
e4a41f
+    """
e4a41f
+
e4a41f
+    st = topology_st.standalone
e4a41f
+
e4a41f
+    log.info('Enable dynamic plugins')
e4a41f
+    try:
e4a41f
+        st.config.set('nsslapd-dynamic-plugins', 'on')
e4a41f
+    except ldap.LDAPError as e:
e4a41f
+        ldap.error('Failed to enable dynamic plugins ' + e.args[0]['desc'])
e4a41f
+        assert False
e4a41f
+
e4a41f
+    log.info('Configure retrocl plugin')
e4a41f
+    rcl = RetroChangelogPlugin(st)
e4a41f
+    rcl.disable()
e4a41f
+    rcl.enable()
e4a41f
+    rcl.replace('nsslapd-attribute', 'nsuniqueid:targetUniqueId')
e4a41f
+
e4a41f
+    log.info('Restarting instance')
e4a41f
+    try:
e4a41f
+        st.restart()
e4a41f
+    except ldap.LDAPError as e:
e4a41f
+        ldap.error('Failed to restart instance ' + e.args[0]['desc'])
e4a41f
+        assert False
e4a41f
+
e4a41f
+    users = UserAccounts(st, DEFAULT_SUFFIX)
e4a41f
+
e4a41f
+    log.info('Adding user1')
e4a41f
+    try:
e4a41f
+        user1 = users.create(properties={
e4a41f
+            'sn': '1',
e4a41f
+            'cn': 'user 1',
e4a41f
+            'uid': 'user1',
e4a41f
+            'uidNumber': '11',
e4a41f
+            'gidNumber': '111',
e4a41f
+            'givenname': 'user1',
e4a41f
+            'homePhone': '0861234567',
e4a41f
+            'carLicense': '131D16674',
e4a41f
+            'mail': 'user1@whereever.com',
e4a41f
+            'homeDirectory': '/home/user1',
e4a41f
+            'userpassword': USER_PW})
e4a41f
+    except ldap.ALREADY_EXISTS:
e4a41f
+        pass
e4a41f
+    except ldap.LDAPError as e:
e4a41f
+        log.error("Failed to add user1")
e4a41f
+
e4a41f
+    log.info('Verify homePhone and carLicense attrs are in the changelog changestring')
e4a41f
+    try:
e4a41f
+        cllist = st.search_s(RETROCL_SUFFIX, ldap.SCOPE_SUBTREE, '(targetDn=%s)' % USER1_DN)
e4a41f
+    except ldap.LDAPError as e:
e4a41f
+        log.fatal("Changelog search failed, error: " +str(e))
e4a41f
+        assert False
e4a41f
+    assert len(cllist) > 0
e4a41f
+    if  cllist[0].hasAttr('changes'):
e4a41f
+        clstr = (cllist[0].getValue('changes')).decode()
e4a41f
+        assert ATTR_HOMEPHONE in clstr
e4a41f
+        assert ATTR_CARLICENSE in clstr
e4a41f
+
e4a41f
+    log.info('Excluding attribute ' + ATTR_CARLICENSE)
e4a41f
+    args = FakeArgs()
e4a41f
+    args.connections = [st.host + ':' + str(st.port) + ':' + DN_DM + ':' + PW_DM]
e4a41f
+    args.instance = 'standalone1'
e4a41f
+    args.basedn = None
e4a41f
+    args.binddn = None
e4a41f
+    args.starttls = False
e4a41f
+    args.pwdfile = None
e4a41f
+    args.bindpw = None
e4a41f
+    args.prompt = False
e4a41f
+    args.exclude_attrs = ATTR_CARLICENSE
e4a41f
+    args.func = retrochangelog_add
e4a41f
+    dsrc_inst = dsrc_arg_concat(args, None)
e4a41f
+    inst = connect_instance(dsrc_inst, False, args)
e4a41f
+    result = args.func(inst, None, log, args)
e4a41f
+    disconnect_instance(inst)
e4a41f
+    assert result is None
e4a41f
+
e4a41f
+    log.info("5s delay for retrocl plugin to restart")
e4a41f
+    time.sleep(5)
e4a41f
+
e4a41f
+    log.info('Modify user1 carLicense attribute')
e4a41f
+    try:
e4a41f
+        st.modify_s(USER1_DN, [(ldap.MOD_REPLACE, ATTR_CARLICENSE, b"123WX321")])
e4a41f
+    except ldap.LDAPError as e:
e4a41f
+        log.fatal('test_retrocl_exclude_attr_mod: Failed to update user1 attribute: error ' + e.message['desc'])
e4a41f
+        assert False
e4a41f
+
e4a41f
+    log.info('Verify carLicense attr is not in the changelog changestring')
e4a41f
+    try:
e4a41f
+        cllist = st.search_s(RETROCL_SUFFIX, ldap.SCOPE_SUBTREE, '(targetDn=%s)' % USER1_DN)
e4a41f
+        assert len(cllist) > 0
e4a41f
+        # There will be 2 entries in the changelog for this user, we are only
e4a41f
+        #interested in the second one, the modify operation.
e4a41f
+        if  cllist[1].hasAttr('changes'):
e4a41f
+            clstr = (cllist[1].getValue('changes')).decode()
e4a41f
+            assert ATTR_CARLICENSE not in clstr
e4a41f
+    except ldap.LDAPError as e:
e4a41f
+        log.fatal("Changelog search failed, error: " +str(e))
e4a41f
+        assert False
e4a41f
+
e4a41f
+if __name__ == '__main__':
e4a41f
+    # Run isolated
e4a41f
+    # -s for DEBUG mode
e4a41f
+    CURRENT_FILE = os.path.realpath(__file__)
e4a41f
+    pytest.main("-s %s" % CURRENT_FILE)
e4a41f
-- 
e4a41f
2.26.3
e4a41f