Blame SOURCES/0004-Issue-4956-Automember-allows-invalid-regex-and-does-.patch

b2bc38
From d037688c072c4cb84fbf9b2a6cb24927f7950605 Mon Sep 17 00:00:00 2001
b2bc38
From: Mark Reynolds <mreynolds@redhat.com>
b2bc38
Date: Wed, 20 Oct 2021 10:04:06 -0400
b2bc38
Subject: [PATCH 04/12] Issue 4956 - Automember allows invalid regex, and does
b2bc38
 not log proper error
b2bc38
b2bc38
Bug Description:  The server was detecting an invalid automember
b2bc38
                  regex, but it did not reject it, and it did not
b2bc38
                  log which regex rule was invalid.
b2bc38
b2bc38
Fix Description:  By properly rejecting the invalid regex will also
b2bc38
                  trigger the proper error logging to occur.
b2bc38
b2bc38
relates: https://github.com/389ds/389-ds-base/issues/4956
b2bc38
b2bc38
Reviewed by: tbordaz & spichugi(Thanks!!)
b2bc38
---
b2bc38
 .../automember_plugin/configuration_test.py   | 49 +++++++++++++++++--
b2bc38
 ldap/servers/plugins/automember/automember.c  |  1 +
b2bc38
 2 files changed, 46 insertions(+), 4 deletions(-)
b2bc38
b2bc38
diff --git a/dirsrvtests/tests/suites/automember_plugin/configuration_test.py b/dirsrvtests/tests/suites/automember_plugin/configuration_test.py
b2bc38
index 0f9cc49dc..4a6b596db 100644
b2bc38
--- a/dirsrvtests/tests/suites/automember_plugin/configuration_test.py
b2bc38
+++ b/dirsrvtests/tests/suites/automember_plugin/configuration_test.py
b2bc38
@@ -1,21 +1,20 @@
b2bc38
 # --- BEGIN COPYRIGHT BLOCK ---
b2bc38
-# Copyright (C) 2019 Red Hat, Inc.
b2bc38
+# Copyright (C) 2021 Red Hat, Inc.
b2bc38
 # All rights reserved.
b2bc38
 #
b2bc38
 # License: GPL (version 3 or any later version).
b2bc38
 # See LICENSE for details.
b2bc38
 # --- END COPYRIGHT BLOCK ---
b2bc38
 
b2bc38
+import ldap
b2bc38
 import os
b2bc38
 import pytest
b2bc38
-
b2bc38
 from lib389.topologies import topology_st as topo
b2bc38
 from lib389.plugins import AutoMembershipPlugin, AutoMembershipDefinitions, MemberOfPlugin
b2bc38
-import ldap
b2bc38
+from lib389._constants import DEFAULT_SUFFIX
b2bc38
 
b2bc38
 pytestmark = pytest.mark.tier1
b2bc38
 
b2bc38
-
b2bc38
 @pytest.mark.bz834056
b2bc38
 def test_configuration(topo):
b2bc38
     """
b2bc38
@@ -52,6 +51,48 @@ def test_configuration(topo):
b2bc38
                                               '"cn=SuffDef1,ou=autouserGroups,cn=config" '
b2bc38
                                               'can not be a child of the plugin config area "cn=config"')
b2bc38
 
b2bc38
+def test_invalid_regex(topo):
b2bc38
+    """Test invalid regex is properly reportedin the error log
b2bc38
+
b2bc38
+    :id: a6d89f84-ec76-4871-be96-411d051800b1
b2bc38
+    :setup: Standalone Instance
b2bc38
+    :steps:
b2bc38
+        1. Setup automember
b2bc38
+        2. Add invalid regex
b2bc38
+        3. Error log reports useful message
b2bc38
+    :expectedresults:
b2bc38
+        1. Success
b2bc38
+        2. Success
b2bc38
+        3. Success
b2bc38
+    """
b2bc38
+    REGEX_DN = "cn=regex1,cn=testregex,cn=auto membership plugin,cn=plugins,cn=config"
b2bc38
+    REGEX_VALUE = "cn=*invalid*"
b2bc38
+    REGEX_ESC_VALUE = "cn=\\*invalid\\*"
b2bc38
+    GROUP_DN = "cn=demo_group,ou=groups,"  + DEFAULT_SUFFIX
b2bc38
+
b2bc38
+    AutoMembershipPlugin(topo.standalone).remove_all("nsslapd-pluginConfigArea")
b2bc38
+    automemberplugin = AutoMembershipPlugin(topo.standalone)
b2bc38
+
b2bc38
+    automember_prop = {
b2bc38
+        'cn': 'testRegex',
b2bc38
+        'autoMemberScope': 'ou=People,' + DEFAULT_SUFFIX,
b2bc38
+        'autoMemberFilter': 'objectclass=*',
b2bc38
+        'autoMemberDefaultGroup': GROUP_DN,
b2bc38
+        'autoMemberGroupingAttr': 'member:dn',
b2bc38
+    }
b2bc38
+    automember_defs = AutoMembershipDefinitions(topo.standalone, "cn=Auto Membership Plugin,cn=plugins,cn=config")
b2bc38
+    automember_def = automember_defs.create(properties=automember_prop)
b2bc38
+    automember_def.add_regex_rule("regex1", GROUP_DN, include_regex=[REGEX_VALUE])
b2bc38
+
b2bc38
+    automemberplugin.enable()
b2bc38
+    topo.standalone.restart()
b2bc38
+
b2bc38
+    # Check errors log for invalid message
b2bc38
+    ERR_STR1 = "automember_parse_regex_rule - Unable to parse regex rule"
b2bc38
+    ERR_STR2 = f"Skipping invalid inclusive regex rule in rule entry \"{REGEX_DN}\" \\(rule = \"{REGEX_ESC_VALUE}\"\\)"
b2bc38
+    assert topo.standalone.searchErrorsLog(ERR_STR1)
b2bc38
+    assert topo.standalone.searchErrorsLog(ERR_STR2)
b2bc38
+
b2bc38
 
b2bc38
 if __name__ == "__main__":
b2bc38
     CURRENT_FILE = os.path.realpath(__file__)
b2bc38
diff --git a/ldap/servers/plugins/automember/automember.c b/ldap/servers/plugins/automember/automember.c
b2bc38
index 39350ad53..b92b89bd5 100644
b2bc38
--- a/ldap/servers/plugins/automember/automember.c
b2bc38
+++ b/ldap/servers/plugins/automember/automember.c
b2bc38
@@ -1217,6 +1217,7 @@ automember_parse_regex_rule(char *rule_string)
b2bc38
                       "automember_parse_regex_rule - Unable to parse "
b2bc38
                       "regex rule (invalid regex).  Error \"%s\".\n",
b2bc38
                       recomp_result ? recomp_result : "unknown");
b2bc38
+        goto bail;
b2bc38
     }
b2bc38
 
b2bc38
     /* Validation has passed, so create the regex rule struct and fill it in.
b2bc38
-- 
b2bc38
2.31.1
b2bc38