Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a create/update/changeId/delete posthook (#193) #242

Merged
merged 5 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<staging.dir>${project.build.directory}/staging</staging.dir>
<jmockit-version>1.5</jmockit-version>
<graalvm.version>22.3.1</graalvm.version>
<jackson.version>2.9.6</jackson.version>
<compiler.dir>${project.build.directory}/compiler</compiler.dir>
</properties>

Expand Down Expand Up @@ -872,6 +873,11 @@
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>

<distributionManagement>
Expand Down
171 changes: 171 additions & 0 deletions src/main/java/org/lsc/Hooks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
****************************************************************************
* Ldap Synchronization Connector provides tools to synchronize
* electronic identities from a list of data sources including
* any database with a JDBC connector, another LDAP directory,
* flat files...
*
* ==LICENSE NOTICE==
*
* Copyright (c) 2008 - 2011 LSC Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:

*    * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*     * Neither the name of the LSC Project nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ==LICENSE NOTICE==
*
* (c) 2008 - 2011 LSC Project
****************************************************************************
*/
package org.lsc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.ProcessBuilder;
import java.io.OutputStream;
import java.io.IOException;
import java.util.Optional;
import org.lsc.utils.output.LdifLayout;
import org.lsc.beans.syncoptions.ISyncOptions.OutputFormat;
import com.fasterxml.jackson.databind.ObjectMapper; // For encoding object to JSON
import com.fasterxml.jackson.databind.ObjectWriter;


/**
* This object is managing posthook scripts
*/
public class Hooks {

static final Logger LOGGER = LoggerFactory.getLogger(Hooks.class);

private static final ObjectMapper Mapper = new ObjectMapper();
/**
* Method calling a postSyncHook if necessary
*
* return nothing
*/
public final void postSyncHook(final Optional<String> hook, final OutputFormat outputFormat, final LscModifications lm) {

if( hook != null && hook.isPresent() )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks better.
The principle of an optional is to not be null, so you can remove the null check.

Also if instead of a String operationType you continue using LscModificationType, the code can be very simplified by:

hook.ifPresent(h -> callHook(lm.getOperation(), h, lm.getMainIdentifier(), outputFormat, lm)

{
switch (lm.getOperation()) {
case CREATE_OBJECT:
callHook("create", hook.get(), lm.getMainIdentifier(), outputFormat, lm);
break;

case UPDATE_OBJECT:
callHook("update", hook.get(), lm.getMainIdentifier(), outputFormat, lm);
break;

case CHANGE_ID:
callHook("changeId", hook.get(), lm.getMainIdentifier(), outputFormat, lm);
break;

case DELETE_OBJECT:
callHook("delete", hook.get(), lm.getMainIdentifier(), outputFormat, lm);
break;

default:
LOGGER.info("Error: unknown operation for posthook {}", hook);
}
}
}

public final static void callHook( String operationType,
String hook,
String identifier,
OutputFormat outputFormat,
LscModifications lm) {

LOGGER.info("Calling {} posthook {} with format {} for {}",
operationType,
hook,
outputFormat.toString(),
identifier);

String modifications = null;
// Compute modifications only in a create / update / changeid operation
if( ! operationType.equals("delete") )
{
if( outputFormat == OutputFormat.JSON ) {
modifications = getJsonModifications(lm);
}
else {
modifications = LdifLayout.format(lm);
}
}

try {
if( modifications != null ) {
Process p = new ProcessBuilder(
hook,
identifier,
operationType)
.start();

// sends ldif modifications to stdin of hook script
OutputStream stdin = p.getOutputStream();
stdin.write(modifications.getBytes());
stdin.write("\n".getBytes());
stdin.flush();
stdin.close();
}
else {
Process p = new ProcessBuilder(
hook,
identifier,
operationType)
.start();
}
}
catch(IOException e) {
LOGGER.error("Error while calling {} posthook {} with format {} for {}",
operationType,
hook,
outputFormat.toString(),
identifier);
LOGGER.error("posthook error: ", e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
LOGGER.error("Error while calling {} posthook {} with format {} for {}",
operationType,
hook,
outputFormat.toString(),
identifier);
LOGGER.error("posthook error: ", e);
LOGGER.error("Error while calling {} posthook {} with format {} for {}",
operationType,
hook,
outputFormat.toString(),
identifier,
e);

should work also

}
}

/**
* Method computing modifications as json
*
* @return modifications in a json String
*/
public final static String getJsonModifications(final LscModifications lm) {
ObjectWriter ow = Mapper.writer().withDefaultPrettyPrinter();
String json = "";
try {
json = ow.writeValueAsString(lm.getLscAttributeModifications());
}
catch(Exception e) {
LOGGER.error("Error while encoding LSC modifications to json");
LOGGER.error("encoding error: ", e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
LOGGER.error("Error while encoding LSC modifications to json");
LOGGER.error("encoding error: ", e);
LOGGER.error("Error while encoding LSC modifications to json", e);

}
return json;
}

}
25 changes: 25 additions & 0 deletions src/main/java/org/lsc/beans/syncoptions/ForceSyncOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

import java.util.List;
import java.util.Set;
import java.util.Optional;

import org.lsc.LscModificationType;
import org.lsc.configuration.PolicyType;
Expand Down Expand Up @@ -128,6 +129,30 @@ public String getCondition(LscModificationType operation) {
return DEFAULT_CONDITION;
}

public OutputFormat getPostHookOutputFormat() {
return OutputFormat.LDIF;
}

public Optional<String> getCreatePostHook() {
return Optional.ofNullable(null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Optional.ofNullable(null);
return Optional.empty();

}

public Optional<String> getDeletePostHook() {
return Optional.ofNullable(null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Optional.ofNullable(null);
return Optional.empty();

}

public Optional<String> getUpdatePostHook() {
return Optional.ofNullable(null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Optional.ofNullable(null);
return Optional.empty();

}

public Optional<String> getChangeIdPostHook() {
return Optional.ofNullable(null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Optional.ofNullable(null);
return Optional.empty();

}

public Optional<String> getPostHook(LscModificationType operation) {
return Optional.ofNullable(null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Optional.ofNullable(null);
return Optional.empty();

}

public String getDn() {
return null;//((Ldap)task.getDestinationService()).getDn();
}
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/org/lsc/beans/syncoptions/ISyncOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

import java.util.List;
import java.util.Set;
import java.util.Optional;

import org.lsc.LscModificationType;
import org.lsc.configuration.PolicyType;
Expand All @@ -60,6 +61,9 @@ public interface ISyncOptions {

/** default condition if none is given */
public static final String DEFAULT_CONDITION = "true";

/** list of output formats */
public enum OutputFormat { LDIF, JSON };

/**
* Initialize the synchronization options policy.
Expand Down Expand Up @@ -153,6 +157,44 @@ public interface ISyncOptions {

String getCondition(LscModificationType operation);

/**
* Returns the posthook output format
*
* @return the posthook output format (default = OutputFormat.LDIF)
*/
OutputFormat getPostHookOutputFormat();

/**
* Returns the posthook for a creation
*
* @return the posthook or "" if none is specified (default)
*/
Optional<String> getCreatePostHook();

/**
* Returns the posthook for an update
*
* @return the posthook or "" if none is specified (default)
*/
Optional<String> getUpdatePostHook();

/**
* Returns the posthook for a delete
*
* @return the posthook or "" if none is specified (default)
*/
Optional<String> getDeletePostHook();

/**
* Returns the posthook for a id change
*
* @return the posthook or "" if none is specified (default)
*/
Optional<String> getChangeIdPostHook();


Optional<String> getPostHook(LscModificationType operation);

/**
* Return the expression used to infer the new object DN
* @return the new object dn pattern
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Optional;

import org.lsc.LscModificationType;
import org.lsc.configuration.DatasetType;
Expand Down Expand Up @@ -196,6 +197,58 @@ public String getCondition(LscModificationType operation) {
}
return result;
}

public OutputFormat getPostHookOutputFormat() {
// default: returns LDIF format
if (conf.getHooks() == null || conf.getHooks().getOutputFormat() == null) {
return OutputFormat.LDIF;
}
switch(conf.getHooks().getOutputFormat()){
case "json":
return OutputFormat.JSON;
default:
return OutputFormat.LDIF;
}
}

public Optional<String> getCreatePostHook() {
Optional<String> hook = Optional.ofNullable(conf.getHooks().getCreatePostHook()).filter(s -> !s.isEmpty());
return hook;
}

public Optional<String> getDeletePostHook() {
Optional<String> hook = Optional.ofNullable(conf.getHooks().getDeletePostHook()).filter(s -> !s.isEmpty());
return hook;
}

public Optional<String> getUpdatePostHook() {
Optional<String> hook = Optional.ofNullable(conf.getHooks().getUpdatePostHook()).filter(s -> !s.isEmpty());
return hook;
}

public Optional<String> getChangeIdPostHook() {
Optional<String> hook = Optional.ofNullable(conf.getHooks().getChangeIdPostHook()).filter(s -> !s.isEmpty());
return hook;
}

public Optional<String> getPostHook(LscModificationType operation) {
Optional<String> result = Optional.ofNullable(null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can simplify this method by removing the result method and returning directly into each case

switch (operation) {
case CREATE_OBJECT:
result = this.getCreatePostHook();
break;
case UPDATE_OBJECT:
result = this.getUpdatePostHook();
break;
case DELETE_OBJECT:
result = this.getDeletePostHook();
break;
case CHANGE_ID:
result = this.getChangeIdPostHook();
break;
}
return result;
}

public String getDelimiter(String name) {
DatasetType dataset = LscConfiguration.getDataset(conf, name);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/lsc/configuration/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/AuditType.java
/AuditsType.java
/ConditionsType.java
/HooksType.java
/ConnectionType.java
/ConnectionsType.java
/CsvAuditType.java
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/lsc/runnable/CleanEntryRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;

import javax.naming.CommunicationException;

Expand All @@ -23,6 +24,8 @@
import org.lsc.utils.ScriptingEvaluator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.lsc.Hooks;
import org.lsc.beans.syncoptions.ISyncOptions.OutputFormat;

/**
* @author sfroger
Expand Down Expand Up @@ -108,6 +111,11 @@ public void run() {
// if we got here, we have a modification to apply - let's
// do it!
if (task.getDestinationService().apply(lm)) {
// Retrieve posthook for the current operation
Optional<String> hook = syncOptions.getDeletePostHook();
OutputFormat outputFormat = syncOptions.getPostHookOutputFormat();
Hooks hookObject = new Hooks();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hookObject (you can name it hooks) should be initialized in the constructor.

hookObject.postSyncHook(hook, outputFormat, lm);
counter.incrementCountCompleted();
abstractSynchronize.logAction(lm, id, task.getName());
} else {
Expand Down
Loading
Loading