Blame SOURCES/0035-Issue-4775-Add-entryuuid-CLI-and-Fixup-4776.patch

b03d2c
From cf620c104ac50af48e48e8c0040820b6c073f7d7 Mon Sep 17 00:00:00 2001
b03d2c
From: Firstyear <william@blackhats.net.au>
b03d2c
Date: Thu, 19 Aug 2021 10:46:00 +1000
b03d2c
Subject: [PATCH 2/4] Issue 4775 - Add entryuuid CLI and Fixup (#4776)
b03d2c
b03d2c
Bug Description: EntryUUID when added was missing it's CLI
b03d2c
and helpers for fixups.
b03d2c
b03d2c
Fix Description: Add the CLI elements.
b03d2c
b03d2c
fixes: https://github.com/389ds/389-ds-base/issues/4775
b03d2c
b03d2c
Author: William Brown <william@blackhats.net.au>
b03d2c
b03d2c
Review by: @mreynolds389 (thanks!)
b03d2c
---
b03d2c
 src/lib389/lib389/cli_conf/plugin.py          |  2 +
b03d2c
 .../lib389/cli_conf/plugins/entryuuid.py      | 39 +++++++++++++++++++
b03d2c
 src/plugins/entryuuid/src/lib.rs              | 34 +++++++++-------
b03d2c
 3 files changed, 60 insertions(+), 15 deletions(-)
b03d2c
 create mode 100644 src/lib389/lib389/cli_conf/plugins/entryuuid.py
b03d2c
b03d2c
diff --git a/src/lib389/lib389/cli_conf/plugin.py b/src/lib389/lib389/cli_conf/plugin.py
b03d2c
index b50837cb8..1bd0c70db 100644
b03d2c
--- a/src/lib389/lib389/cli_conf/plugin.py
b03d2c
+++ b/src/lib389/lib389/cli_conf/plugin.py
b03d2c
@@ -27,6 +27,7 @@ from lib389.cli_conf.plugins import passthroughauth as cli_passthroughauth
b03d2c
 from lib389.cli_conf.plugins import retrochangelog as cli_retrochangelog
b03d2c
 from lib389.cli_conf.plugins import automember as cli_automember
b03d2c
 from lib389.cli_conf.plugins import posix_winsync as cli_posix_winsync
b03d2c
+from lib389.cli_conf.plugins import entryuuid as cli_entryuuid
b03d2c
 
b03d2c
 SINGULAR = Plugin
b03d2c
 MANY = Plugins
b03d2c
@@ -113,6 +114,7 @@ def create_parser(subparsers):
b03d2c
     cli_passthroughauth.create_parser(subcommands)
b03d2c
     cli_retrochangelog.create_parser(subcommands)
b03d2c
     cli_posix_winsync.create_parser(subcommands)
b03d2c
+    cli_entryuuid.create_parser(subcommands)
b03d2c
 
b03d2c
     list_parser = subcommands.add_parser('list', help="List current configured (enabled and disabled) plugins")
b03d2c
     list_parser.set_defaults(func=plugin_list)
b03d2c
diff --git a/src/lib389/lib389/cli_conf/plugins/entryuuid.py b/src/lib389/lib389/cli_conf/plugins/entryuuid.py
b03d2c
new file mode 100644
b03d2c
index 000000000..6c86bff4b
b03d2c
--- /dev/null
b03d2c
+++ b/src/lib389/lib389/cli_conf/plugins/entryuuid.py
b03d2c
@@ -0,0 +1,39 @@
b03d2c
+# --- BEGIN COPYRIGHT BLOCK ---
b03d2c
+# Copyright (C) 2021 William Brown <william@blackhats.net.au>
b03d2c
+# All rights reserved.
b03d2c
+#
b03d2c
+# License: GPL (version 3 or any later version).
b03d2c
+# See LICENSE for details.
b03d2c
+# --- END COPYRIGHT BLOCK ---
b03d2c
+
b03d2c
+import ldap
b03d2c
+from lib389.plugins import EntryUUIDPlugin
b03d2c
+from lib389.cli_conf import add_generic_plugin_parsers, generic_object_edit, generic_object_add
b03d2c
+
b03d2c
+def do_fixup(inst, basedn, log, args):
b03d2c
+    plugin = EntryUUIDPlugin(inst)
b03d2c
+    log.info('Attempting to add task entry...')
b03d2c
+    if not plugin.status():
b03d2c
+        log.error("'%s' is disabled. Fix up task can't be executed" % plugin.rdn)
b03d2c
+        return
b03d2c
+    fixup_task = plugin.fixup(args.DN, args.filter)
b03d2c
+    fixup_task.wait()
b03d2c
+    exitcode = fixup_task.get_exit_code()
b03d2c
+    if exitcode != 0:
b03d2c
+        log.error('EntryUUID fixup task has failed. Please, check the error log for more - %s' % exitcode)
b03d2c
+    else:
b03d2c
+        log.info('Successfully added task entry')
b03d2c
+
b03d2c
+def create_parser(subparsers):
b03d2c
+    referint = subparsers.add_parser('entryuuid', help='Manage and configure EntryUUID plugin')
b03d2c
+    subcommands = referint.add_subparsers(help='action')
b03d2c
+
b03d2c
+    add_generic_plugin_parsers(subcommands, EntryUUIDPlugin)
b03d2c
+
b03d2c
+    fixup = subcommands.add_parser('fixup', help='Run the fix-up task for EntryUUID plugin')
b03d2c
+    fixup.set_defaults(func=do_fixup)
b03d2c
+    fixup.add_argument('DN', help="Base DN that contains entries to fix up")
b03d2c
+    fixup.add_argument('-f', '--filter',
b03d2c
+                       help='Filter for entries to fix up.\n If omitted, all entries under base DN'
b03d2c
+                            'will have their EntryUUID attribute regenerated if not present.')
b03d2c
+
b03d2c
diff --git a/src/plugins/entryuuid/src/lib.rs b/src/plugins/entryuuid/src/lib.rs
b03d2c
index 0197c5e83..29a9f1258 100644
b03d2c
--- a/src/plugins/entryuuid/src/lib.rs
b03d2c
+++ b/src/plugins/entryuuid/src/lib.rs
b03d2c
@@ -33,7 +33,7 @@ fn assign_uuid(e: &mut EntryRef) {
b03d2c
     // 🚧 safety barrier 🚧
b03d2c
     if e.contains_attr("entryUUID") {
b03d2c
         log_error!(
b03d2c
-            ErrorLevel::Trace,
b03d2c
+            ErrorLevel::Plugin,
b03d2c
             "assign_uuid -> entryUUID exists, skipping dn {}",
b03d2c
             sdn.to_dn_string()
b03d2c
         );
b03d2c
@@ -47,7 +47,7 @@ fn assign_uuid(e: &mut EntryRef) {
b03d2c
     if sdn.is_below_suffix(&*config_sdn) || sdn.is_below_suffix(&*schema_sdn) {
b03d2c
         // We don't need to assign to these suffixes.
b03d2c
         log_error!(
b03d2c
-            ErrorLevel::Trace,
b03d2c
+            ErrorLevel::Plugin,
b03d2c
             "assign_uuid -> not assigning to {:?} as part of system suffix",
b03d2c
             sdn.to_dn_string()
b03d2c
         );
b03d2c
@@ -57,7 +57,7 @@ fn assign_uuid(e: &mut EntryRef) {
b03d2c
     // Generate a new Uuid.
b03d2c
     let u: Uuid = Uuid::new_v4();
b03d2c
     log_error!(
b03d2c
-        ErrorLevel::Trace,
b03d2c
+        ErrorLevel::Plugin,
b03d2c
         "assign_uuid -> assigning {:?} to dn {}",
b03d2c
         u,
b03d2c
         sdn.to_dn_string()
b03d2c
@@ -78,13 +78,13 @@ impl SlapiPlugin3 for EntryUuid {
b03d2c
     fn betxn_pre_add(pb: &mut PblockRef) -> Result<(), PluginError> {
b03d2c
         if pb.get_is_replicated_operation() {
b03d2c
             log_error!(
b03d2c
-                ErrorLevel::Trace,
b03d2c
+                ErrorLevel::Plugin,
b03d2c
                 "betxn_pre_add -> replicated operation, will not change"
b03d2c
             );
b03d2c
             return Ok(());
b03d2c
         }
b03d2c
 
b03d2c
-        log_error!(ErrorLevel::Trace, "betxn_pre_add -> start");
b03d2c
+        log_error!(ErrorLevel::Plugin, "betxn_pre_add -> start");
b03d2c
 
b03d2c
         let mut e = pb.get_op_add_entryref().map_err(|_| PluginError::Pblock)?;
b03d2c
         assign_uuid(&mut e);
b03d2c
@@ -105,7 +105,7 @@ impl SlapiPlugin3 for EntryUuid {
b03d2c
                 .first()
b03d2c
                 .ok_or_else(|| {
b03d2c
                     log_error!(
b03d2c
-                        ErrorLevel::Trace,
b03d2c
+                        ErrorLevel::Plugin,
b03d2c
                         "task_validate basedn error -> empty value array?"
b03d2c
                     );
b03d2c
                     LDAPError::Operation
b03d2c
@@ -113,7 +113,7 @@ impl SlapiPlugin3 for EntryUuid {
b03d2c
                 .as_ref()
b03d2c
                 .try_into()
b03d2c
                 .map_err(|e| {
b03d2c
-                    log_error!(ErrorLevel::Trace, "task_validate basedn error -> {:?}", e);
b03d2c
+                    log_error!(ErrorLevel::Plugin, "task_validate basedn error -> {:?}", e);
b03d2c
                     LDAPError::Operation
b03d2c
                 })?,
b03d2c
             None => return Err(LDAPError::ObjectClassViolation),
b03d2c
@@ -124,7 +124,7 @@ impl SlapiPlugin3 for EntryUuid {
b03d2c
                 .first()
b03d2c
                 .ok_or_else(|| {
b03d2c
                     log_error!(
b03d2c
-                        ErrorLevel::Trace,
b03d2c
+                        ErrorLevel::Plugin,
b03d2c
                         "task_validate filter error -> empty value array?"
b03d2c
                     );
b03d2c
                     LDAPError::Operation
b03d2c
@@ -132,7 +132,7 @@ impl SlapiPlugin3 for EntryUuid {
b03d2c
                 .as_ref()
b03d2c
                 .try_into()
b03d2c
                 .map_err(|e| {
b03d2c
-                    log_error!(ErrorLevel::Trace, "task_validate filter error -> {:?}", e);
b03d2c
+                    log_error!(ErrorLevel::Plugin, "task_validate filter error -> {:?}", e);
b03d2c
                     LDAPError::Operation
b03d2c
                 })?,
b03d2c
             None => {
b03d2c
@@ -144,7 +144,11 @@ impl SlapiPlugin3 for EntryUuid {
b03d2c
         // Error if the first filter is empty?
b03d2c
 
b03d2c
         // Now, to make things faster, we wrap the filter in a exclude term.
b03d2c
-        let raw_filter = format!("(&{}(!(entryuuid=*)))", raw_filter);
b03d2c
+        let raw_filter = if !raw_filter.starts_with('(') && !raw_filter.ends_with('(') {
b03d2c
+            format!("(&({})(!(entryuuid=*)))", raw_filter)
b03d2c
+        } else {
b03d2c
+            format!("(&{}(!(entryuuid=*)))", raw_filter)
b03d2c
+        };
b03d2c
 
b03d2c
         Ok(FixupData { basedn, raw_filter })
b03d2c
     }
b03d2c
@@ -155,7 +159,7 @@ impl SlapiPlugin3 for EntryUuid {
b03d2c
 
b03d2c
     fn task_handler(_task: &Task, data: Self::TaskData) -> Result<Self::TaskData, PluginError> {
b03d2c
         log_error!(
b03d2c
-            ErrorLevel::Trace,
b03d2c
+            ErrorLevel::Plugin,
b03d2c
             "task_handler -> start thread with -> {:?}",
b03d2c
             data
b03d2c
         );
b03d2c
@@ -195,12 +199,12 @@ impl SlapiPlugin3 for EntryUuid {
b03d2c
     }
b03d2c
 
b03d2c
     fn start(_pb: &mut PblockRef) -> Result<(), PluginError> {
b03d2c
-        log_error!(ErrorLevel::Trace, "plugin start");
b03d2c
+        log_error!(ErrorLevel::Plugin, "plugin start");
b03d2c
         Ok(())
b03d2c
     }
b03d2c
 
b03d2c
     fn close(_pb: &mut PblockRef) -> Result<(), PluginError> {
b03d2c
-        log_error!(ErrorLevel::Trace, "plugin close");
b03d2c
+        log_error!(ErrorLevel::Plugin, "plugin close");
b03d2c
         Ok(())
b03d2c
     }
b03d2c
 }
b03d2c
@@ -212,7 +216,7 @@ pub fn entryuuid_fixup_mapfn(e: &EntryRef, _data: &()) -> Result<(), PluginError
b03d2c
     /* Sanity check that entryuuid doesn't already exist */
b03d2c
     if e.contains_attr("entryUUID") {
b03d2c
         log_error!(
b03d2c
-            ErrorLevel::Trace,
b03d2c
+            ErrorLevel::Plugin,
b03d2c
             "skipping fixup for -> {}",
b03d2c
             sdn.to_dn_string()
b03d2c
         );
b03d2c
@@ -232,7 +236,7 @@ pub fn entryuuid_fixup_mapfn(e: &EntryRef, _data: &()) -> Result<(), PluginError
b03d2c
 
b03d2c
     match lmod.execute() {
b03d2c
         Ok(_) => {
b03d2c
-            log_error!(ErrorLevel::Trace, "fixed-up -> {}", sdn.to_dn_string());
b03d2c
+            log_error!(ErrorLevel::Plugin, "fixed-up -> {}", sdn.to_dn_string());
b03d2c
             Ok(())
b03d2c
         }
b03d2c
         Err(e) => {
b03d2c
-- 
b03d2c
2.31.1
b03d2c