You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by bb...@apache.org on 2017/11/14 21:44:50 UTC

nifi-registry git commit: NIFIREG-49 Adding UserClient to nifi-registry-client

Repository: nifi-registry
Updated Branches:
  refs/heads/master 3deda91c9 -> a66503552


NIFIREG-49 Adding UserClient to nifi-registry-client

This closes #34.

Signed-off-by: Bryan Bende <bb...@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/a6650355
Tree: http://git-wip-us.apache.org/repos/asf/nifi-registry/tree/a6650355
Diff: http://git-wip-us.apache.org/repos/asf/nifi-registry/diff/a6650355

Branch: refs/heads/master
Commit: a665035528f4c4d7e2c92835333870e7565026cb
Parents: 3deda91
Author: Bryan Bende <bb...@apache.org>
Authored: Wed Nov 8 12:38:33 2017 -0500
Committer: Bryan Bende <bb...@apache.org>
Committed: Tue Nov 14 16:44:30 2017 -0500

----------------------------------------------------------------------
 .../registry/client/NiFiRegistryClient.java     | 10 ++++
 .../apache/nifi/registry/client/UserClient.java | 42 ++++++++++++++
 .../client/impl/JerseyNiFiRegistryClient.java   | 12 ++++
 .../registry/client/impl/JerseyUserClient.java  | 47 ++++++++++++++++
 .../registry/client/impl/TestUserClient.java    | 58 ++++++++++++++++++++
 5 files changed, 169 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi-registry/blob/a6650355/nifi-registry-client/src/main/java/org/apache/nifi/registry/client/NiFiRegistryClient.java
----------------------------------------------------------------------
diff --git a/nifi-registry-client/src/main/java/org/apache/nifi/registry/client/NiFiRegistryClient.java b/nifi-registry-client/src/main/java/org/apache/nifi/registry/client/NiFiRegistryClient.java
index 9ee1573..07fb817 100644
--- a/nifi-registry-client/src/main/java/org/apache/nifi/registry/client/NiFiRegistryClient.java
+++ b/nifi-registry-client/src/main/java/org/apache/nifi/registry/client/NiFiRegistryClient.java
@@ -64,6 +64,16 @@ public interface NiFiRegistryClient extends Closeable {
     ItemsClient getItemsClient(String ... proxiedEntity);
 
     /**
+     * @return the client for obtaining information about the current user
+     */
+    UserClient getUserClient();
+
+    /**
+     * @return the client for obtaining information about the current user based on the given proxied entities
+     */
+    UserClient getUserClient(String ... proxiedEntity);
+
+    /**
      * The builder interface that implementations should provide for obtaining the client.
      */
     interface Builder {

http://git-wip-us.apache.org/repos/asf/nifi-registry/blob/a6650355/nifi-registry-client/src/main/java/org/apache/nifi/registry/client/UserClient.java
----------------------------------------------------------------------
diff --git a/nifi-registry-client/src/main/java/org/apache/nifi/registry/client/UserClient.java b/nifi-registry-client/src/main/java/org/apache/nifi/registry/client/UserClient.java
new file mode 100644
index 0000000..5529f60
--- /dev/null
+++ b/nifi-registry-client/src/main/java/org/apache/nifi/registry/client/UserClient.java
@@ -0,0 +1,42 @@
+/*
+ * 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.client;
+
+import org.apache.nifi.registry.model.authorization.AccessStatus;
+
+import java.io.IOException;
+
+public interface UserClient {
+
+    /**
+     * Obtains the access status of the current user.
+     *
+     * If the UserClient was obtained with proxied entities, then the access status should represent the status
+     * of the last identity in the chain.
+     *
+     * If the UserClient was obtained without proxied entities, then it would represent the identity of the certificate
+     * in the keystore used by the client.
+     *
+     * If the registry is not in secure mode, or if the user is unauthorized for any reason, an exception
+     * will be thrown.
+     *
+     * @return the access status of the current user
+     * @throws NiFiRegistryException if the user is unauthorized, or the proxying user is not a valid proxy, or nifi-registry is not secured
+     */
+    AccessStatus getAccessStatus() throws NiFiRegistryException, IOException;
+
+}

http://git-wip-us.apache.org/repos/asf/nifi-registry/blob/a6650355/nifi-registry-client/src/main/java/org/apache/nifi/registry/client/impl/JerseyNiFiRegistryClient.java
----------------------------------------------------------------------
diff --git a/nifi-registry-client/src/main/java/org/apache/nifi/registry/client/impl/JerseyNiFiRegistryClient.java b/nifi-registry-client/src/main/java/org/apache/nifi/registry/client/impl/JerseyNiFiRegistryClient.java
index 1061455..454d872 100644
--- a/nifi-registry-client/src/main/java/org/apache/nifi/registry/client/impl/JerseyNiFiRegistryClient.java
+++ b/nifi-registry-client/src/main/java/org/apache/nifi/registry/client/impl/JerseyNiFiRegistryClient.java
@@ -29,6 +29,7 @@ import org.apache.nifi.registry.client.FlowSnapshotClient;
 import org.apache.nifi.registry.client.ItemsClient;
 import org.apache.nifi.registry.client.NiFiRegistryClient;
 import org.apache.nifi.registry.client.NiFiRegistryClientConfig;
+import org.apache.nifi.registry.client.UserClient;
 import org.glassfish.jersey.client.ClientConfig;
 import org.glassfish.jersey.client.ClientProperties;
 import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJaxbJsonProvider;
@@ -167,6 +168,17 @@ public class JerseyNiFiRegistryClient implements NiFiRegistryClient {
         return new JerseyItemsClient(baseTarget, headers);
     }
 
+    @Override
+    public UserClient getUserClient() {
+        return new JerseyUserClient(baseTarget);
+    }
+
+    @Override
+    public UserClient getUserClient(String... proxiedEntity) {
+        final Map<String,String> headers = getHeaders(proxiedEntity);
+        return new JerseyUserClient(baseTarget, headers);
+    }
+
     private Map<String,String> getHeaders(String[] proxiedEntities) {
         final String proxiedEntitiesValue = getProxiedEntitesValue(proxiedEntities);
 

http://git-wip-us.apache.org/repos/asf/nifi-registry/blob/a6650355/nifi-registry-client/src/main/java/org/apache/nifi/registry/client/impl/JerseyUserClient.java
----------------------------------------------------------------------
diff --git a/nifi-registry-client/src/main/java/org/apache/nifi/registry/client/impl/JerseyUserClient.java b/nifi-registry-client/src/main/java/org/apache/nifi/registry/client/impl/JerseyUserClient.java
new file mode 100644
index 0000000..b5c9770
--- /dev/null
+++ b/nifi-registry-client/src/main/java/org/apache/nifi/registry/client/impl/JerseyUserClient.java
@@ -0,0 +1,47 @@
+/*
+ * 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.client.impl;
+
+import org.apache.nifi.registry.client.NiFiRegistryException;
+import org.apache.nifi.registry.client.UserClient;
+import org.apache.nifi.registry.model.authorization.AccessStatus;
+
+import javax.ws.rs.client.WebTarget;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Map;
+
+public class JerseyUserClient extends AbstractJerseyClient implements UserClient {
+
+    private final WebTarget accessTarget;
+
+    public JerseyUserClient(final WebTarget baseTarget) {
+        this(baseTarget, Collections.emptyMap());
+    }
+
+    public JerseyUserClient(final WebTarget baseTarget, final Map<String,String> headers) {
+        super(headers);
+        this.accessTarget = baseTarget.path("/access");
+    }
+
+    @Override
+    public AccessStatus getAccessStatus() throws NiFiRegistryException, IOException {
+        return executeAction("Error retrieving access status for the current user", () -> {
+            return getRequestBuilder(accessTarget).get(AccessStatus.class);
+        });
+    }
+}

http://git-wip-us.apache.org/repos/asf/nifi-registry/blob/a6650355/nifi-registry-client/src/test/java/org/apache/nifi/registry/client/impl/TestUserClient.java
----------------------------------------------------------------------
diff --git a/nifi-registry-client/src/test/java/org/apache/nifi/registry/client/impl/TestUserClient.java b/nifi-registry-client/src/test/java/org/apache/nifi/registry/client/impl/TestUserClient.java
new file mode 100644
index 0000000..1184e56
--- /dev/null
+++ b/nifi-registry-client/src/test/java/org/apache/nifi/registry/client/impl/TestUserClient.java
@@ -0,0 +1,58 @@
+/*
+ * 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.client.impl;
+
+import org.apache.nifi.registry.client.NiFiRegistryClient;
+import org.apache.nifi.registry.client.NiFiRegistryClientConfig;
+import org.apache.nifi.registry.client.UserClient;
+import org.apache.nifi.registry.model.authorization.AccessStatus;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TestUserClient {
+
+    public static final Logger LOGGER = LoggerFactory.getLogger(TestUserClient.class);
+
+    public static void main(String[] args) {
+        final NiFiRegistryClientConfig config = new NiFiRegistryClientConfig.Builder()
+                .baseUrl("http://localhost:8080")
+                .build();
+
+        final NiFiRegistryClient client = new JerseyNiFiRegistryClient.Builder()
+                .config(config)
+                .build();
+
+        final UserClient userClient = client.getUserClient();
+
+        try {
+            final AccessStatus status = userClient.getAccessStatus();
+            System.out.println("Identity: " + status.getIdentity());
+            System.out.println("Status: " + status.getStatus());
+            System.out.println("Message: " + status.getMessage());
+
+        } catch (Exception e) {
+            LOGGER.error(e.getMessage(), e);
+        } finally {
+            try {
+                client.close();
+            } catch (Exception e) {
+
+            }
+        }
+    }
+
+}