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 2019/03/13 20:33:54 UTC

[nifi-registry] branch master updated: NIFIREG-234: Publish events for tenant CRUD

This is an automated email from the ASF dual-hosted git repository.

kdoran pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nifi-registry.git


The following commit(s) were added to refs/heads/master by this push:
     new 563bfed  NIFIREG-234: Publish events for tenant CRUD
563bfed is described below

commit 563bfeda9766b7fe9d4480c55ac7212679db10c8
Author: Bryan Rosander <br...@apache.org>
AuthorDate: Mon Mar 11 10:54:36 2019 -0400

    NIFIREG-234: Publish events for tenant CRUD
    
    This closes #161.
    
    Signed-off-by: Kevin Doran <kd...@apache.org>
---
 .../apache/nifi/registry/event/EventFactory.java   |  50 +++++++
 .../apache/nifi/registry/event/StandardEvent.java  |  23 +++
 .../nifi/registry/event/StandardEventField.java    |  23 +++
 .../apache/nifi/registry/hook/EventFieldName.java  |   7 +-
 .../org/apache/nifi/registry/hook/EventType.java   |  24 ++++
 .../nifi/registry/web/api/TenantResource.java      |   8 ++
 .../nifi/registry/web/api/TenantResourceTest.java  | 158 +++++++++++++++++++++
 7 files changed, 291 insertions(+), 2 deletions(-)

diff --git a/nifi-registry-core/nifi-registry-framework/src/main/java/org/apache/nifi/registry/event/EventFactory.java b/nifi-registry-core/nifi-registry-framework/src/main/java/org/apache/nifi/registry/event/EventFactory.java
index 8246301..42912ab 100644
--- a/nifi-registry-core/nifi-registry-framework/src/main/java/org/apache/nifi/registry/event/EventFactory.java
+++ b/nifi-registry-core/nifi-registry-framework/src/main/java/org/apache/nifi/registry/event/EventFactory.java
@@ -16,6 +16,8 @@
  */
 package org.apache.nifi.registry.event;
 
+import org.apache.nifi.registry.authorization.User;
+import org.apache.nifi.registry.authorization.UserGroup;
 import org.apache.nifi.registry.bucket.Bucket;
 import org.apache.nifi.registry.extension.bundle.Bundle;
 import org.apache.nifi.registry.extension.bundle.BundleVersion;
@@ -133,4 +135,52 @@ public class EventFactory {
                 .addField(EventFieldName.USER, NiFiUserUtils.getNiFiUserIdentity())
                 .build();
     }
+
+    public static Event userCreated(final User user) {
+        return new StandardEvent.Builder()
+                .eventType(EventType.CREATE_USER)
+                .addField(EventFieldName.USER_ID, user.getIdentifier())
+                .addField(EventFieldName.USER_IDENTITY, user.getIdentity())
+                .build();
+    }
+
+    public static Event userUpdated(final User user) {
+        return new StandardEvent.Builder()
+                .eventType(EventType.UPDATE_USER)
+                .addField(EventFieldName.USER_ID, user.getIdentifier())
+                .addField(EventFieldName.USER_IDENTITY, user.getIdentity())
+                .build();
+    }
+
+    public static Event userDeleted(final User user) {
+        return new StandardEvent.Builder()
+                .eventType(EventType.DELETE_USER)
+                .addField(EventFieldName.USER_ID, user.getIdentifier())
+                .addField(EventFieldName.USER_IDENTITY, user.getIdentity())
+                .build();
+    }
+
+    public static Event userGroupCreated(final UserGroup userGroup) {
+        return new StandardEvent.Builder()
+                .eventType(EventType.CREATE_USER_GROUP)
+                .addField(EventFieldName.USER_GROUP_ID, userGroup.getIdentifier())
+                .addField(EventFieldName.USER_GROUP_IDENTITY, userGroup.getIdentity())
+                .build();
+    }
+
+    public static Event userGroupUpdated(final UserGroup userGroup) {
+        return new StandardEvent.Builder()
+                .eventType(EventType.UPDATE_USER_GROUP)
+                .addField(EventFieldName.USER_GROUP_ID, userGroup.getIdentifier())
+                .addField(EventFieldName.USER_GROUP_IDENTITY, userGroup.getIdentity())
+                .build();
+    }
+
+    public static Event userGroupDeleted(final UserGroup userGroup) {
+        return new StandardEvent.Builder()
+                .eventType(EventType.DELETE_USER_GROUP)
+                .addField(EventFieldName.USER_GROUP_ID, userGroup.getIdentifier())
+                .addField(EventFieldName.USER_GROUP_IDENTITY, userGroup.getIdentity())
+                .build();
+    }
 }
diff --git a/nifi-registry-core/nifi-registry-framework/src/main/java/org/apache/nifi/registry/event/StandardEvent.java b/nifi-registry-core/nifi-registry-framework/src/main/java/org/apache/nifi/registry/event/StandardEvent.java
index 4ad459d..26ca93c 100644
--- a/nifi-registry-core/nifi-registry-framework/src/main/java/org/apache/nifi/registry/event/StandardEvent.java
+++ b/nifi-registry-core/nifi-registry-framework/src/main/java/org/apache/nifi/registry/event/StandardEvent.java
@@ -26,6 +26,7 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
+import java.util.Objects;
 
 /**
  * Standard implementation of Event.
@@ -121,4 +122,26 @@ public class StandardEvent implements Event {
             return new StandardEvent(this);
         }
     }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+        StandardEvent that = (StandardEvent) o;
+        return eventType == that.eventType && Objects.equals(eventFields, that.eventFields);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(eventType, eventFields);
+    }
+
+    @Override
+    public String toString() {
+        return "StandardEvent{eventType=" + eventType + ", eventFields=" + eventFields + '}';
+    }
 }
diff --git a/nifi-registry-core/nifi-registry-framework/src/main/java/org/apache/nifi/registry/event/StandardEventField.java b/nifi-registry-core/nifi-registry-framework/src/main/java/org/apache/nifi/registry/event/StandardEventField.java
index 21266bb..978d6fe 100644
--- a/nifi-registry-core/nifi-registry-framework/src/main/java/org/apache/nifi/registry/event/StandardEventField.java
+++ b/nifi-registry-core/nifi-registry-framework/src/main/java/org/apache/nifi/registry/event/StandardEventField.java
@@ -20,6 +20,8 @@ import org.apache.commons.lang3.Validate;
 import org.apache.nifi.registry.hook.EventField;
 import org.apache.nifi.registry.hook.EventFieldName;
 
+import java.util.Objects;
+
 /**
  * Standard implementation of EventField.
  */
@@ -46,4 +48,25 @@ public class StandardEventField implements EventField {
         return value;
     }
 
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+        StandardEventField that = (StandardEventField) o;
+        return name == that.name && Objects.equals(value, that.value);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(name, value);
+    }
+
+    @Override
+    public String toString() {
+        return "StandardEventField{name=" + name + ", value='" + value + '\'' + '}';
+    }
 }
diff --git a/nifi-registry-core/nifi-registry-provider-api/src/main/java/org/apache/nifi/registry/hook/EventFieldName.java b/nifi-registry-core/nifi-registry-provider-api/src/main/java/org/apache/nifi/registry/hook/EventFieldName.java
index 3f2fa6e..3e35058 100644
--- a/nifi-registry-core/nifi-registry-provider-api/src/main/java/org/apache/nifi/registry/hook/EventFieldName.java
+++ b/nifi-registry-core/nifi-registry-provider-api/src/main/java/org/apache/nifi/registry/hook/EventFieldName.java
@@ -26,6 +26,9 @@ public enum EventFieldName {
     EXTENSION_BUNDLE_ID,
     VERSION,
     USER,
-    COMMENT;
-
+    USER_ID,
+    USER_IDENTITY,
+    USER_GROUP_ID,
+    USER_GROUP_IDENTITY,
+    COMMENT
 }
diff --git a/nifi-registry-core/nifi-registry-provider-api/src/main/java/org/apache/nifi/registry/hook/EventType.java b/nifi-registry-core/nifi-registry-provider-api/src/main/java/org/apache/nifi/registry/hook/EventType.java
index 0af35dc..3568611 100644
--- a/nifi-registry-core/nifi-registry-provider-api/src/main/java/org/apache/nifi/registry/hook/EventType.java
+++ b/nifi-registry-core/nifi-registry-provider-api/src/main/java/org/apache/nifi/registry/hook/EventType.java
@@ -77,6 +77,30 @@ public enum EventType {
             EventFieldName.EXTENSION_BUNDLE_ID,
             EventFieldName.VERSION,
             EventFieldName.USER
+    ),
+    CREATE_USER(
+            EventFieldName.USER_ID,
+            EventFieldName.USER_IDENTITY
+    ),
+    UPDATE_USER(
+            EventFieldName.USER_ID,
+            EventFieldName.USER_IDENTITY
+    ),
+    DELETE_USER(
+            EventFieldName.USER_ID,
+            EventFieldName.USER_IDENTITY
+    ),
+    CREATE_USER_GROUP(
+            EventFieldName.USER_GROUP_ID,
+            EventFieldName.USER_GROUP_IDENTITY
+    ),
+    UPDATE_USER_GROUP(
+            EventFieldName.USER_GROUP_ID,
+            EventFieldName.USER_GROUP_IDENTITY
+    ),
+    DELETE_USER_GROUP(
+            EventFieldName.USER_GROUP_ID,
+            EventFieldName.USER_GROUP_IDENTITY
     )
     ;
 
diff --git a/nifi-registry-core/nifi-registry-web-api/src/main/java/org/apache/nifi/registry/web/api/TenantResource.java b/nifi-registry-core/nifi-registry-web-api/src/main/java/org/apache/nifi/registry/web/api/TenantResource.java
index 7215ccb..11ed1e1 100644
--- a/nifi-registry-core/nifi-registry-web-api/src/main/java/org/apache/nifi/registry/web/api/TenantResource.java
+++ b/nifi-registry-core/nifi-registry-web-api/src/main/java/org/apache/nifi/registry/web/api/TenantResource.java
@@ -27,6 +27,7 @@ import io.swagger.annotations.ExtensionProperty;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.nifi.registry.authorization.User;
 import org.apache.nifi.registry.authorization.UserGroup;
+import org.apache.nifi.registry.event.EventFactory;
 import org.apache.nifi.registry.event.EventService;
 import org.apache.nifi.registry.exception.ResourceNotFoundException;
 import org.apache.nifi.registry.security.authorization.Authorizer;
@@ -127,6 +128,7 @@ public class TenantResource extends AuthorizableApplicationResource {
         authorizeAccess(RequestAction.WRITE);
 
         User createdUser = authorizationService.createUser(requestUser);
+        publish(EventFactory.userCreated(createdUser));
 
         String locationUri = generateUserUri(createdUser);
         return generateCreatedResponse(URI.create(locationUri), createdUser).build();
@@ -264,6 +266,7 @@ public class TenantResource extends AuthorizableApplicationResource {
 
             throw new ResourceNotFoundException("The specified user ID does not exist in this registry.");
         }
+        publish(EventFactory.userUpdated(updatedUser));
 
         return generateOkResponse(updatedUser).build();
     }
@@ -311,6 +314,8 @@ public class TenantResource extends AuthorizableApplicationResource {
 
             throw new ResourceNotFoundException("The specified user ID does not exist in this registry.");
         }
+        publish(EventFactory.userDeleted(user));
+
         return generateOkResponse(user).build();
     }
 
@@ -364,6 +369,7 @@ public class TenantResource extends AuthorizableApplicationResource {
         }
 
         UserGroup createdGroup = authorizationService.createUserGroup(requestUserGroup);
+        publish(EventFactory.userGroupCreated(createdGroup));
 
         String locationUri = generateUserGroupUri(createdGroup);
         return generateCreatedResponse(URI.create(locationUri), createdGroup).build();
@@ -500,6 +506,7 @@ public class TenantResource extends AuthorizableApplicationResource {
 
             throw new ResourceNotFoundException("The specified user group ID does not exist in this registry.");
         }
+        publish(EventFactory.userGroupUpdated(updatedUserGroup));
 
         return generateOkResponse(updatedUserGroup).build();
     }
@@ -546,6 +553,7 @@ public class TenantResource extends AuthorizableApplicationResource {
 
             throw new ResourceNotFoundException("The specified user group ID does not exist in this registry.");
         }
+        publish(EventFactory.userGroupDeleted(userGroup));
 
         return generateOkResponse(userGroup).build();
     }
diff --git a/nifi-registry-core/nifi-registry-web-api/src/test/java/org/apache/nifi/registry/web/api/TenantResourceTest.java b/nifi-registry-core/nifi-registry-web-api/src/test/java/org/apache/nifi/registry/web/api/TenantResourceTest.java
new file mode 100644
index 0000000..2e3f5fd
--- /dev/null
+++ b/nifi-registry-core/nifi-registry-web-api/src/test/java/org/apache/nifi/registry/web/api/TenantResourceTest.java
@@ -0,0 +1,158 @@
+/*
+ * 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.web.api;
+
+import org.apache.nifi.registry.authorization.User;
+import org.apache.nifi.registry.authorization.UserGroup;
+import org.apache.nifi.registry.event.EventFactory;
+import org.apache.nifi.registry.event.EventService;
+import org.apache.nifi.registry.security.authorization.AuthorizableLookup;
+import org.apache.nifi.registry.security.authorization.Authorizer;
+import org.apache.nifi.registry.security.authorization.ConfigurableAccessPolicyProvider;
+import org.apache.nifi.registry.security.authorization.ConfigurableUserGroupProvider;
+import org.apache.nifi.registry.security.authorization.ManagedAuthorizer;
+import org.apache.nifi.registry.service.AuthorizationService;
+import org.junit.Before;
+import org.junit.Test;
+
+import javax.servlet.http.HttpServletRequest;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class TenantResourceTest {
+
+    private TenantResource tenantResource;
+    private AuthorizationService authorizationService;
+    private Authorizer authorizer;
+    private ConfigurableAccessPolicyProvider accessPolicyProvider;
+    private EventService eventService;
+    private ConfigurableUserGroupProvider userGroupProvider;
+    private AuthorizableLookup authorizableLookup;
+
+    @Before
+    public void setUp() {
+        authorizationService = mock(AuthorizationService.class);
+        authorizer = mock(ManagedAuthorizer.class);
+        authorizableLookup = mock(AuthorizableLookup.class);
+        accessPolicyProvider = mock(ConfigurableAccessPolicyProvider.class);
+        userGroupProvider = mock(ConfigurableUserGroupProvider.class);
+        eventService = mock(EventService.class);
+
+        when(accessPolicyProvider.getUserGroupProvider()).thenReturn(userGroupProvider);
+        when(((ManagedAuthorizer) authorizer).getAccessPolicyProvider()).thenReturn(accessPolicyProvider);
+        when(authorizationService.getAuthorizer()).thenReturn(authorizer);
+        when(authorizationService.getAuthorizableLookup()).thenReturn(authorizableLookup);
+
+        tenantResource = new TenantResource(authorizationService, eventService) {
+
+            @Override
+            protected URI getBaseUri() {
+                try {
+                    return new URI("http://registry.nifi.apache.org/nifi-registry");
+                } catch (URISyntaxException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+        };
+    }
+
+    @Test
+    public void testCreateUser() {
+        HttpServletRequest request = mock(HttpServletRequest.class);
+        User user = new User(null, "identity");
+        User result = new User("identifier", user.getIdentity());
+
+        when(authorizationService.createUser(user)).thenReturn(result);
+
+        tenantResource.createUser(request, user);
+
+        verify(authorizationService).createUser(user);
+        verify(eventService).publish(eq(EventFactory.userCreated(result)));
+    }
+
+    @Test
+    public void testUpdateUser() {
+        HttpServletRequest request = mock(HttpServletRequest.class);
+        User user = new User("identifier", "new-identity");
+
+        when(authorizationService.updateUser(user)).thenReturn(user);
+
+        tenantResource.updateUser(request, user.getIdentifier(), user);
+
+        verify(authorizationService).updateUser(user);
+        verify(eventService).publish(eq(EventFactory.userUpdated(user)));
+    }
+
+    @Test
+    public void testDeleteUser() {
+        HttpServletRequest request = mock(HttpServletRequest.class);
+        User user = new User("identifier", "identity");
+
+        when(authorizationService.deleteUser(user.getIdentifier())).thenReturn(user);
+
+        tenantResource.removeUser(request, user.getIdentifier());
+
+        verify(authorizationService).deleteUser(user.getIdentifier());
+        verify(eventService).publish(eq(EventFactory.userDeleted(user)));
+    }
+
+    @Test
+    public void testCreateUserGroup() {
+        HttpServletRequest request = mock(HttpServletRequest.class);
+        UserGroup userGroup = new UserGroup(null, "identity");
+        UserGroup result = new UserGroup("identifier", userGroup.getIdentity());
+
+        when(authorizationService.createUserGroup(userGroup)).thenReturn(result);
+
+        tenantResource.createUserGroup(request, userGroup);
+
+        verify(authorizationService).createUserGroup(userGroup);
+        verify(eventService).publish(eq(EventFactory.userGroupCreated(result)));
+    }
+
+    @Test
+    public void testUpdateUserGroup() {
+        HttpServletRequest request = mock(HttpServletRequest.class);
+        UserGroup userGroup = new UserGroup("identifier", "new-identity");
+
+        when(authorizationService.updateUserGroup(userGroup)).thenReturn(userGroup);
+
+        tenantResource.updateUserGroup(request, userGroup.getIdentifier(), userGroup);
+
+        verify(authorizationService).updateUserGroup(userGroup);
+        verify(eventService).publish(eq(EventFactory.userGroupUpdated(userGroup)));
+    }
+
+    @Test
+    public void testDeleteUserGroup() {
+        HttpServletRequest request = mock(HttpServletRequest.class);
+        UserGroup userGroup = new UserGroup("identifier", "identity");
+
+        when(authorizationService.deleteUserGroup(userGroup.getIdentifier())).thenReturn(userGroup);
+
+        tenantResource.removeUserGroup(request, userGroup.getIdentifier());
+
+        verify(authorizationService).deleteUserGroup(userGroup.getIdentifier());
+        verify(eventService).publish(eq(EventFactory.userGroupDeleted(userGroup)));
+    }
+}