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

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