You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by kd...@apache.org on 2018/08/20 16:56:04 UTC

nifi-registry git commit: NIFIREG-190 Add support for whitelist filtering Event Hook providers

Repository: nifi-registry
Updated Branches:
  refs/heads/master b2e8ef340 -> d48c9b8cf


NIFIREG-190 Add support for whitelist filtering Event Hook providers

This closes #133.

Signed-off-by: Kevin Doran <kd...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/nifi-registry/repo
Commit: http://git-wip-us.apache.org/repos/asf/nifi-registry/commit/d48c9b8c
Tree: http://git-wip-us.apache.org/repos/asf/nifi-registry/tree/d48c9b8c
Diff: http://git-wip-us.apache.org/repos/asf/nifi-registry/diff/d48c9b8c

Branch: refs/heads/master
Commit: d48c9b8cfb2d57f69a190bf0632c2950770ca8b7
Parents: b2e8ef3
Author: Jeremy Dyer <je...@apache.org>
Authored: Wed Aug 8 13:41:42 2018 -0400
Committer: Kevin Doran <kd...@apache.org>
Committed: Mon Aug 20 12:55:05 2018 -0400

----------------------------------------------------------------------
 .../src/main/asciidoc/administration-guide.adoc | 62 +++++++++++++++++
 .../nifi/registry/event/EventService.java       |  5 +-
 .../provider/hook/LoggingEventHookProvider.java |  8 ++-
 .../provider/hook/ScriptEventHookProvider.java  | 10 ++-
 .../nifi/registry/hook/EventHookProvider.java   | 14 ++++
 .../WhitelistFilteringEventHookProvider.java    | 70 ++++++++++++++++++++
 .../src/main/resources/conf/providers.xml       |  6 ++
 7 files changed, 168 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi-registry/blob/d48c9b8c/nifi-registry-docs/src/main/asciidoc/administration-guide.adoc
----------------------------------------------------------------------
diff --git a/nifi-registry-docs/src/main/asciidoc/administration-guide.adoc b/nifi-registry-docs/src/main/asciidoc/administration-guide.adoc
index f1e4621..9e8d0c0 100644
--- a/nifi-registry-docs/src/main/asciidoc/administration-guide.adoc
+++ b/nifi-registry-docs/src/main/asciidoc/administration-guide.adoc
@@ -1147,3 +1147,65 @@ Here is the data model version histories:
 |2|0.2|JSON formatted text file. The root object contains header and Flow content object.
 |1|0.1|Binary format having header bytes at the beginning followed by Flow content represented as XML.
 |====
+
+== Event Hooks
+Event hooks are an integration point that allows for custom code to to be triggered when NiFi Registry application events occur.
+
+[options="header,footer"]
+|==================================================================================================================================================
+| Event Name | Description
+|`CREATE_BUCKET` | A new registry bucket is created.
+|`CREATE_FLOW` | A new flow is created in a specified bucket. Only triggered on first time creation of a flow with a given name.
+|`CREATE_FLOW_VERSION` | A new version for a flow has been saved in the registry.
+|`UPDATE_BUCKET` | A bucket has been updated.
+|`UPDATE_FLOW` | A flow that exist in a bucket has been updated.
+|`DELETE_BUCKET` | An existing bucket in the registry is deleted.
+|`DELETE_FLOW` | An existing flow in the registry is deleted.
+|==================================================================================================================================================
+
+=== Shared Event Hook Properties
+There are certain properties that are shared amongst all of the NiFi Registry provided Event Hook implementations. Those properties and
+their purpose are listed below.
+
+[options="header,footer"]
+|==================================================================================================================================================
+| Property Name | Description
+|`Whitelisted Event Type` | EventTypes the hook provider configured with this property should respond to. If this property is left blank or not provided all events will fire for the configured hook provider. Multiple 'Whitelisted Event Type' can be specified and often are.
+EX: <property name="Whitelisted Event Type 1">CREATE_FLOW</property> and <property name="Whitelisted Event Type 2">UPDATE_FLOW</property> would invoke the configured hook provider for the CREATE_FLOW and UPDATE_FLOW EventTypes.
+|==================================================================================================================================================
+
+=== ScriptEventHookProvider
+Hook provider for invoking a shell script that has been written by a user and placed on a file system that is accessible
+by the NiFi Registry instance that the provider is configured for.
+
+....
+<eventHookProvider>
+    <class>
+      org.apache.nifi.registry.provider.hook.ScriptEventHookProvider
+    </class>
+    <property name="Script Path"></property>
+    <property name="Working Directory"></property>
+    <!-- optional -->
+        <property name="Whitelisted Event Type 1">CREATE_FLOW</property>
+        <property name="Whitelisted Event Type 2">UPDATE_FLOW</property>
+</eventHookProvider>
+....
+
+[options="header,footer"]
+|==================================================================================================================================================
+| Property Name | Description
+|`Script Path` | Full path to a script that will executed for each event. The arguments to the script will be the event fields in the order they are specified for the given event type.
+|`Working Directory` | Working directory from where the commands will be executed.
+|==================================================================================================================================================
+
+=== LoggingEventHookProvider
+The LoggingEventHookProvider logs a string representation of each event using an SLF4J logger. The logger can be configured
+via NiFi Registry’s logback.xml, which by default contains an appender that writes to a log file named nifi-registry-event.log in the logs directory.
+
+....
+<eventHookProvider>
+    <class>
+      org.apache.nifi.registry.provider.hook.LoggingEventHookProvider
+    </class>
+</eventHookProvider>
+....
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/nifi-registry/blob/d48c9b8c/nifi-registry-framework/src/main/java/org/apache/nifi/registry/event/EventService.java
----------------------------------------------------------------------
diff --git a/nifi-registry-framework/src/main/java/org/apache/nifi/registry/event/EventService.java b/nifi-registry-framework/src/main/java/org/apache/nifi/registry/event/EventService.java
index ef30f84..8a11493 100644
--- a/nifi-registry-framework/src/main/java/org/apache/nifi/registry/event/EventService.java
+++ b/nifi-registry-framework/src/main/java/org/apache/nifi/registry/event/EventService.java
@@ -70,7 +70,10 @@ public class EventService implements DisposableBean {
                     // event was available so notify each provider, contain errors per-provider
                     for(final EventHookProvider provider : eventHookProviders) {
                         try {
-                            provider.handle(event);
+                            if (event.getEventType() == null
+                                    || (event.getEventType() != null && provider.shouldHandle(event.getEventType()))) {
+                                provider.handle(event);
+                            }
                         } catch (Exception e) {
                             LOGGER.error("Error handling event hook", e);
                         }

http://git-wip-us.apache.org/repos/asf/nifi-registry/blob/d48c9b8c/nifi-registry-framework/src/main/java/org/apache/nifi/registry/provider/hook/LoggingEventHookProvider.java
----------------------------------------------------------------------
diff --git a/nifi-registry-framework/src/main/java/org/apache/nifi/registry/provider/hook/LoggingEventHookProvider.java b/nifi-registry-framework/src/main/java/org/apache/nifi/registry/provider/hook/LoggingEventHookProvider.java
index a942d5a..9ceb59f 100644
--- a/nifi-registry-framework/src/main/java/org/apache/nifi/registry/provider/hook/LoggingEventHookProvider.java
+++ b/nifi-registry-framework/src/main/java/org/apache/nifi/registry/provider/hook/LoggingEventHookProvider.java
@@ -25,17 +25,19 @@ import org.apache.nifi.registry.provider.ProviderCreationException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class LoggingEventHookProvider implements EventHookProvider {
+public class LoggingEventHookProvider
+    implements EventHookProvider {
 
     static final Logger LOGGER = LoggerFactory.getLogger(LoggingEventHookProvider.class);
 
     @Override
-    public void onConfigured(final ProviderConfigurationContext configurationContext) throws ProviderCreationException {
-        // nothing to do
+    public void onConfigured(ProviderConfigurationContext configurationContext) throws ProviderCreationException {
+        // Nothing to do
     }
 
     @Override
     public void handle(final Event event) throws EventHookException {
+
         final StringBuilder builder = new StringBuilder()
                 .append(event.getEventType())
                 .append(" [");

http://git-wip-us.apache.org/repos/asf/nifi-registry/blob/d48c9b8c/nifi-registry-framework/src/main/java/org/apache/nifi/registry/provider/hook/ScriptEventHookProvider.java
----------------------------------------------------------------------
diff --git a/nifi-registry-framework/src/main/java/org/apache/nifi/registry/provider/hook/ScriptEventHookProvider.java b/nifi-registry-framework/src/main/java/org/apache/nifi/registry/provider/hook/ScriptEventHookProvider.java
index 4900a2b..f96115e 100644
--- a/nifi-registry-framework/src/main/java/org/apache/nifi/registry/provider/hook/ScriptEventHookProvider.java
+++ b/nifi-registry-framework/src/main/java/org/apache/nifi/registry/provider/hook/ScriptEventHookProvider.java
@@ -25,7 +25,7 @@ import java.util.Map;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.nifi.registry.hook.Event;
 import org.apache.nifi.registry.hook.EventField;
-import org.apache.nifi.registry.hook.EventHookProvider;
+import org.apache.nifi.registry.hook.WhitelistFilteringEventHookProvider;
 import org.apache.nifi.registry.provider.ProviderConfigurationContext;
 import org.apache.nifi.registry.provider.ProviderCreationException;
 import org.apache.nifi.registry.util.FileUtils;
@@ -35,7 +35,8 @@ import org.slf4j.LoggerFactory;
 /**
  * A EventHookProvider that is used to execute a script to handle the event.
  */
-public class ScriptEventHookProvider implements EventHookProvider {
+public class ScriptEventHookProvider
+        extends WhitelistFilteringEventHookProvider {
 
     static final Logger LOGGER = LoggerFactory.getLogger(ScriptEventHookProvider.class);
     static final String SCRIPT_PATH_PROP = "Script Path";
@@ -43,9 +44,10 @@ public class ScriptEventHookProvider implements EventHookProvider {
     private File scriptFile;
     private File workDirFile;
 
+
     @Override
     public void handle(final Event event) {
-        List<String> command = new ArrayList<String>();
+        List<String> command = new ArrayList<>();
         command.add(scriptFile.getAbsolutePath());
         command.add(event.getEventType().name());
 
@@ -67,6 +69,8 @@ public class ScriptEventHookProvider implements EventHookProvider {
 
     @Override
     public void onConfigured(ProviderConfigurationContext configurationContext) throws ProviderCreationException {
+        super.onConfigured(configurationContext);
+
         final Map<String,String> props = configurationContext.getProperties();
         if (!props.containsKey(SCRIPT_PATH_PROP)) {
             throw new ProviderCreationException("The property " + SCRIPT_PATH_PROP + " must be provided");

http://git-wip-us.apache.org/repos/asf/nifi-registry/blob/d48c9b8c/nifi-registry-provider-api/src/main/java/org/apache/nifi/registry/hook/EventHookProvider.java
----------------------------------------------------------------------
diff --git a/nifi-registry-provider-api/src/main/java/org/apache/nifi/registry/hook/EventHookProvider.java b/nifi-registry-provider-api/src/main/java/org/apache/nifi/registry/hook/EventHookProvider.java
index e134e8f..8d9f51c 100644
--- a/nifi-registry-provider-api/src/main/java/org/apache/nifi/registry/hook/EventHookProvider.java
+++ b/nifi-registry-provider-api/src/main/java/org/apache/nifi/registry/hook/EventHookProvider.java
@@ -36,4 +36,18 @@ public interface EventHookProvider extends Provider {
      */
     void handle(Event event) throws EventHookException;
 
+    /**
+     * Examines the values from the 'Whitelisted Event Type ' properties in the hook provider definition to determine
+     * if the Event should be invoked for this particular EventType
+     *
+     * @param eventType
+     *  EventType that has been fired by the framework.
+     *
+     * @return
+     *  True if the hook provider should be 'handled' and false otherwise.
+     */
+    default boolean shouldHandle(EventType eventType) {
+        return true;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/nifi-registry/blob/d48c9b8c/nifi-registry-provider-api/src/main/java/org/apache/nifi/registry/hook/WhitelistFilteringEventHookProvider.java
----------------------------------------------------------------------
diff --git a/nifi-registry-provider-api/src/main/java/org/apache/nifi/registry/hook/WhitelistFilteringEventHookProvider.java b/nifi-registry-provider-api/src/main/java/org/apache/nifi/registry/hook/WhitelistFilteringEventHookProvider.java
new file mode 100644
index 0000000..24ac3a4
--- /dev/null
+++ b/nifi-registry-provider-api/src/main/java/org/apache/nifi/registry/hook/WhitelistFilteringEventHookProvider.java
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.registry.hook;
+
+import org.apache.nifi.registry.provider.ProviderConfigurationContext;
+import org.apache.nifi.registry.provider.ProviderCreationException;
+
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public abstract class WhitelistFilteringEventHookProvider
+        implements EventHookProvider {
+
+    static final String EVENT_WHITELIST_PREFIX = "Whitelisted Event Type ";
+    static final Pattern EVENT_WHITELIST_PATTERN = Pattern.compile(EVENT_WHITELIST_PREFIX + "\\S+");
+
+    protected Set<EventType> whiteListEvents = null;
+
+    @Override
+    public void onConfigured(ProviderConfigurationContext configurationContext) throws ProviderCreationException {
+        whiteListEvents = new HashSet<>();
+        for (Map.Entry<String,String> entry : configurationContext.getProperties().entrySet()) {
+            Matcher matcher = EVENT_WHITELIST_PATTERN.matcher(entry.getKey());
+            if (matcher.matches() && (entry.getValue() != null && entry.getValue().length() > 0)) {
+                whiteListEvents.add(EventType.valueOf(entry.getValue()));
+            }
+
+        }
+    }
+
+    /**
+     * Standard method for deciding if the EventType should be handled by the Hook provider or not.
+     *
+     * @param eventType
+     *  EventType that was fired by the framework.
+     *
+     * @return
+     *  True if the EventType is in the whitelist set and false otherwise.
+     */
+    @Override
+    public boolean shouldHandle(EventType eventType) {
+        if (whiteListEvents != null && whiteListEvents.size() > 0) {
+            if (whiteListEvents.contains(eventType)) {
+                return true;
+            }
+        } else {
+            // If the whitelist property is not set or empty we want to fire for all events.
+            return true;
+        }
+        return false;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/nifi-registry/blob/d48c9b8c/nifi-registry-resources/src/main/resources/conf/providers.xml
----------------------------------------------------------------------
diff --git a/nifi-registry-resources/src/main/resources/conf/providers.xml b/nifi-registry-resources/src/main/resources/conf/providers.xml
index e59e277..faf8d4f 100644
--- a/nifi-registry-resources/src/main/resources/conf/providers.xml
+++ b/nifi-registry-resources/src/main/resources/conf/providers.xml
@@ -35,6 +35,12 @@
     	<class>org.apache.nifi.registry.provider.hook.ScriptEventHookProvider</class>
     	<property name="Script Path"></property>
     	<property name="Working Directory"></property>
+    	-->
+    	<!-- Optional Whitelist Event types
+        <property name="Whitelisted Event Type 1">CREATE_FLOW</property>
+        <property name="Whitelisted Event Type 2">DELETE_FLOW</property>
+    	-->
+    <!--
     </eventHookProvider>
     -->