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

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