You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2020/08/19 03:22:12 UTC

[GitHub] [arrow] ryannicholson commented on a change in pull request #7994: Flight auth redesign

ryannicholson commented on a change in pull request #7994:
URL: https://github.com/apache/arrow/pull/7994#discussion_r472617468



##########
File path: java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ClientBearerTokenMiddleware.java
##########
@@ -0,0 +1,116 @@
+/*
+ * 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.arrow.flight.auth;
+
+import org.apache.arrow.flight.CallHeaders;
+import org.apache.arrow.flight.CallInfo;
+import org.apache.arrow.flight.CallStatus;
+import org.apache.arrow.flight.FlightClientMiddleware;
+import org.apache.arrow.flight.FlightRuntimeException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Middleware for capturing and sending back bearer tokens.
+ */
+public class ClientBearerTokenMiddleware implements FlightClientMiddleware {
+  private static final Logger logger = LoggerFactory.getLogger(ClientBearerTokenMiddleware.class);
+
+  private final String bearerToken;
+
+  /**
+   * Factory used within FlightClient.
+   */
+  public static class Factory implements FlightClientMiddleware.Factory {
+    private final String bearerToken = null;
+
+    @Override
+    public FlightClientMiddleware onCallStarted(CallInfo info) {
+      logger.debug("Call name: {}", info.method().name());
+      if (info.method().name().equalsIgnoreCase(AuthConstants.HANDSHAKE_DESCRIPTOR_NAME)) {
+        return new ClientAuthHandshakeMiddleware(this);
+      }
+
+      if (bearerToken == null) {

Review comment:
       Wouldn't this always evaluate as null as line 40 has a final member of the same name initialized to null?

##########
File path: java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ServerAuthMiddleware.java
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.arrow.flight.auth;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.arrow.flight.CallHeaders;
+import org.apache.arrow.flight.CallInfo;
+import org.apache.arrow.flight.CallStatus;
+import org.apache.arrow.flight.FlightRuntimeException;
+import org.apache.arrow.flight.FlightServerMiddleware;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.grpc.Context;
+
+/**
+ * Middleware that's used to validate credentials during the handshake and verify
+ * the bearer token in subsequent requests.
+ */
+public class ServerAuthMiddleware implements FlightServerMiddleware {
+  private static final Logger logger = LoggerFactory.getLogger(ServerAuthMiddleware.class);
+
+  /**
+   * Factory for accessing ServerAuthMiddleware.
+   */
+  public static class Factory implements FlightServerMiddleware.Factory<ServerAuthMiddleware> {
+    private final ServerAuthHandler authHandler;
+    private final GeneratedBearerTokenAuthHandler bearerTokenAuthHandler = new GeneratedBearerTokenAuthHandler();
+    private final Map<String, String> tokenToIdentityMap = new ConcurrentHashMap<>();

Review comment:
       This looks unused.

##########
File path: java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ServerHandshakeWrapper.java
##########
@@ -0,0 +1,79 @@
+/*
+ * 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.arrow.flight.auth;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+
+import org.apache.arrow.flight.impl.Flight.HandshakeRequest;
+import org.apache.arrow.flight.impl.Flight.HandshakeResponse;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.grpc.stub.StreamObserver;
+
+/**
+ * Contains utility methods for integrating authorization into a GRPC stream.
+ */
+public class ServerHandshakeWrapper {
+  private static final Logger LOGGER = LoggerFactory.getLogger(ServerHandshakeWrapper.class);

Review comment:
       This looks unused.

##########
File path: java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/ServerInterceptorAdapter.java
##########
@@ -87,10 +87,12 @@ public ServerInterceptorAdapter(List<KeyFactory<?>> factories) {
     // Use LinkedHashMap to preserve insertion order
     final Map<FlightServerMiddleware.Key<?>, FlightServerMiddleware> middlewareMap = new LinkedHashMap<>();
     final MetadataAdapter headerAdapter = new MetadataAdapter(headers);
+    Context currentContext = Context.current();
     for (final KeyFactory<?> factory : factories) {
       final FlightServerMiddleware m;
       try {
         m = factory.factory.onCallStarted(info, headerAdapter);
+        currentContext = m.onAuthenticationSuccess(currentContext);

Review comment:
       Could we find an alternative to changing the context while looping through the factories? Instead of having "onAuthenticationSuccess" as a new function in FlightServerMiddleware, could the logic required here be moved into ServerAuthMiddleware and have this check happen through the normal process of calling "onCallStarted"?

##########
File path: java/flight/flight-core/src/test/java/org/apache/arrow/flight/auth/TestBasicAuth.java
##########
@@ -44,29 +44,38 @@
 import org.junit.Ignore;
 import org.junit.Test;
 
+import com.google.common.base.Strings;
 import com.google.common.collect.ImmutableList;
 
 public class TestBasicAuth {
 
   private static final String USERNAME = "flight";
   private static final String PASSWORD = "woohoo";
-  private static final byte[] VALID_TOKEN = "my_token".getBytes(StandardCharsets.UTF_8);
+  private static final String VALID_TOKEN = "my_token";
 
+  private FlightClient.Builder clientBuilder;
   private FlightClient client;
   private FlightServer server;
   private BufferAllocator allocator;
 
   @Test
   public void validAuth() {
-    client.authenticateBasic(USERNAME, PASSWORD);
-    Assert.assertTrue(ImmutableList.copyOf(client.listFlights(Criteria.ALL)).size() == 0);
+    try {
+      client = clientBuilder.callCredentials(new BasicAuthCallCredentials(USERNAME, PASSWORD)).build();
+      client.handshake();
+      Assert.assertTrue(ImmutableList.copyOf(client.listFlights(Criteria.ALL)).size() == 0);
+    } catch (FlightRuntimeException ex) {
+      ex.printStackTrace();
+      System.out.println(ex.status());

Review comment:
       Are these needed?

##########
File path: java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ServerAuthMiddleware.java
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.arrow.flight.auth;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.arrow.flight.CallHeaders;
+import org.apache.arrow.flight.CallInfo;
+import org.apache.arrow.flight.CallStatus;
+import org.apache.arrow.flight.FlightRuntimeException;
+import org.apache.arrow.flight.FlightServerMiddleware;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.grpc.Context;
+
+/**
+ * Middleware that's used to validate credentials during the handshake and verify
+ * the bearer token in subsequent requests.
+ */
+public class ServerAuthMiddleware implements FlightServerMiddleware {
+  private static final Logger logger = LoggerFactory.getLogger(ServerAuthMiddleware.class);
+
+  /**
+   * Factory for accessing ServerAuthMiddleware.
+   */
+  public static class Factory implements FlightServerMiddleware.Factory<ServerAuthMiddleware> {
+    private final ServerAuthHandler authHandler;
+    private final GeneratedBearerTokenAuthHandler bearerTokenAuthHandler = new GeneratedBearerTokenAuthHandler();
+    private final Map<String, String> tokenToIdentityMap = new ConcurrentHashMap<>();
+
+    public Factory(ServerAuthHandler authHandler) {
+      this.authHandler = authHandler;
+    }
+
+    public String getPeerForBearer(String bearerToken) {
+      return bearerTokenAuthHandler.getIdentityForBearerToken(bearerToken);
+    }
+
+    @Override
+    public ServerAuthMiddleware onCallStarted(CallInfo callInfo, CallHeaders incomingHeaders) {
+      logger.debug("Call name: {}", callInfo.method().name());
+      if (callInfo.method().name().equals(AuthConstants.HANDSHAKE_DESCRIPTOR_NAME)) {
+        final ServerAuthHandler.HandshakeResult result = authHandler.authenticate(incomingHeaders);
+        final String bearerToken = bearerTokenAuthHandler.registerBearer(result);
+        return new ServerAuthMiddleware(result.getPeerIdentity(), bearerToken);
+      }
+
+      final String bearerToken = AuthUtilities.getValueFromAuthHeader(incomingHeaders, AuthConstants.BEARER_PREFIX);
+      // No bearer token provided. Auth handler may explicitly allow this.
+      if (bearerToken == null) {
+        if (authHandler.validateBearer(null)) {
+          return new ServerAuthMiddleware("", null);
+        }
+        logger.info("Client did not supply a bearer token.");
+        throw new FlightRuntimeException(CallStatus.UNAUTHENTICATED);
+      }
+
+      if (!authHandler.validateBearer(bearerToken) && !bearerTokenAuthHandler.validateBearer(bearerToken)) {
+        logger.info("Bearer token supplied by client was not authorized.");
+        throw new FlightRuntimeException(CallStatus.UNAUTHORIZED);
+      }
+
+      final String peerIdentity = bearerTokenAuthHandler.getIdentityForBearerToken(bearerToken);
+      return new ServerAuthMiddleware(peerIdentity, null);

Review comment:
       Perhaps there is opportunity for an optimization down the road in caching the ServerAuthMiddleware instances with the identity?

##########
File path: java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/BasicServerAuthHandler.java
##########
@@ -39,37 +42,58 @@ public BasicServerAuthHandler(BasicAuthValidator authValidator) {
     this.authValidator = authValidator;
   }
 
-  /**
-   * Interface that this handler delegates for determining if credentials are valid.
-   */
-  public interface BasicAuthValidator {
+  @Override
+  public HandshakeResult authenticate(CallHeaders headers) {
+    final String authEncoded = AuthUtilities.getValueFromAuthHeader(headers, AuthConstants.BASIC_PREFIX);
+    if (authEncoded == null) {
+      throw new FlightRuntimeException(CallStatus.UNAUTHENTICATED);
+    }
 
-    byte[] getToken(String username, String password) throws Exception;
+    try {
+      // The value has the format Base64(<username>:<password>)
+      final String authDecoded = new String(Base64.getDecoder().decode(authEncoded), StandardCharsets.UTF_8);
+      final String[] authInParts = authDecoded.split(":");
+      if (authInParts.length < 2) {
+        throw new FlightRuntimeException(CallStatus.UNAUTHORIZED);
+      }
 
-    Optional<String> isValid(byte[] token);
+      final String user = authInParts[0];
+      final String[] passwordParts = Arrays.copyOfRange(authInParts, 1, authInParts.length);
+      final String password = String.join(":", passwordParts);

Review comment:
       Would an indexOf(":") call with a substring each for username/password be fewer operations?

##########
File path: java/flight/flight-core/src/test/java/org/apache/arrow/flight/auth/TestBasicAuth.java
##########
@@ -44,29 +44,38 @@
 import org.junit.Ignore;
 import org.junit.Test;
 
+import com.google.common.base.Strings;
 import com.google.common.collect.ImmutableList;
 
 public class TestBasicAuth {
 
   private static final String USERNAME = "flight";
   private static final String PASSWORD = "woohoo";
-  private static final byte[] VALID_TOKEN = "my_token".getBytes(StandardCharsets.UTF_8);
+  private static final String VALID_TOKEN = "my_token";
 
+  private FlightClient.Builder clientBuilder;
   private FlightClient client;
   private FlightServer server;
   private BufferAllocator allocator;
 
   @Test
   public void validAuth() {
-    client.authenticateBasic(USERNAME, PASSWORD);
-    Assert.assertTrue(ImmutableList.copyOf(client.listFlights(Criteria.ALL)).size() == 0);
+    try {
+      client = clientBuilder.callCredentials(new BasicAuthCallCredentials(USERNAME, PASSWORD)).build();
+      client.handshake();
+      Assert.assertTrue(ImmutableList.copyOf(client.listFlights(Criteria.ALL)).size() == 0);

Review comment:
       Could this be changed to assertEquals?




----------------------------------------------------------------
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