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

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