You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2020/09/03 17:26:16 UTC

[GitHub] [nifi-registry] bbende commented on a change in pull request #293: NIFIREG-411 Refactor nifi-registry-client to support other authN mechanisms

bbende commented on a change in pull request #293:
URL: https://github.com/apache/nifi-registry/pull/293#discussion_r483140845



##########
File path: nifi-registry-core/nifi-registry-client/src/main/java/org/apache/nifi/registry/client/impl/JerseyAccessClient.java
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.commons.lang3.StringUtils;
+import org.apache.nifi.registry.client.AccessClient;
+import org.apache.nifi.registry.client.NiFiRegistryException;
+import org.apache.nifi.registry.client.RequestConfig;
+import org.apache.nifi.registry.client.impl.request.BasicAuthRequestConfig;
+import org.apache.nifi.registry.client.impl.request.BearerTokenRequestConfig;
+
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.Invocation;
+import javax.ws.rs.client.WebTarget;
+import java.io.IOException;
+import java.util.Map;
+
+/**
+ * Jersey implementation of AccessClient.
+ */
+public class JerseyAccessClient extends AbstractJerseyClient implements AccessClient {
+
+    private final WebTarget accessTarget;
+
+    public JerseyAccessClient(final WebTarget baseTarget) {
+        super(null);
+        this.accessTarget = baseTarget.path("/access");
+    }
+
+    @Override
+    public String getToken(final String username, final String password) throws NiFiRegistryException, IOException {
+        if (StringUtils.isBlank(username)) {
+            throw new IllegalArgumentException("Username is required");
+        }
+
+        if (StringUtils.isBlank(password)) {
+            throw new IllegalArgumentException("Password is required");
+        }
+
+        return executeAction("Error performing login", () -> {
+            final WebTarget target = accessTarget.path("token/login");
+            final Invocation.Builder requestBuilder = getRequestBuilder(target);
+
+            final RequestConfig basicCredsConfig = new BasicAuthRequestConfig(username, password);
+            final Map<String,String> basicAuthHeaders = basicCredsConfig.getHeaders();
+            basicAuthHeaders.entrySet().stream().forEach(e -> requestBuilder.header(e.getKey(), e.getValue()));
+
+            return requestBuilder.post(Entity.json(null), String.class);
+        });
+    }
+
+    @Override
+    public String getTokenFromKerberosTicket() throws NiFiRegistryException, IOException {
+        return executeAction("Error performing kerberos login", () -> {
+            final WebTarget target = accessTarget.path("token/kerberos");
+            return getRequestBuilder(target).post(Entity.json(null), String.class);
+        });
+    }

Review comment:
       Good question... Since there are different ways of getting an authenticated Subject, I didn't want to build that into the method, so it is expected that the caller has done that. 
   
   I put this in the Java docs of this method in the interface:
   > 
   >  Gets an access token via spnego. It is expected that the caller of this method has wrapped the call
   >    in a {@code doAs()} using a {@link javax.security.auth.Subject}.
   
   An example from the other PR on NiFi CLI side would be this...
   
   https://github.com/apache/nifi/blob/55af2ceb17bcff3ff6cb24ca230e2b64b8e5e855/nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/registry/access/GetAccessTokenSpnego.java#L74-L90




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org