keeratsingh commented on a change in pull request #7994:
URL: https://github.com/apache/arrow/pull/7994#discussion_r482255128
##########
File path:
java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/BasicServerAuthHandler.java
##########
@@ -39,37 +41,62 @@ public BasicServerAuthHandler(BasicAuthValidator
authValidator) {
this.authValidator = authValidator;
}
- /**
- * Interface that this handler delegates for determining if credentials are
valid.
- */
- public interface BasicAuthValidator {
+ @Override
+ public AuthResult 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 int colonPos = authDecoded.indexOf(':');
+ if (colonPos == -1) {
+ throw new FlightRuntimeException(CallStatus.UNAUTHORIZED);
+ }
- Optional<String> isValid(byte[] token);
+ final String user = authDecoded.substring(0, colonPos);
+ final String password = authDecoded.substring(colonPos + 1);
+ final Optional<String> bearerToken =
authValidator.validateCredentials(user, password);
+ return new AuthResult() {
+ @Override
+ public String getPeerIdentity() {
+ return user;
+ }
- }
+ @Override
+ public Optional<String> getBearerToken() {
+ return bearerToken;
+ }
+ };
- @Override
- public boolean authenticate(ServerAuthSender outgoing, Iterator<byte[]>
incoming) {
- byte[] bytes = incoming.next();
- try {
- BasicAuth auth = BasicAuth.parseFrom(bytes);
- byte[] token = authValidator.getToken(auth.getUsername(),
auth.getPassword());
- outgoing.send(token);
- return true;
- } catch (InvalidProtocolBufferException e) {
- logger.debug("Failure parsing auth message.", e);
- } catch (Exception e) {
- logger.debug("Unknown error during authorization.", e);
+ } catch (UnsupportedEncodingException ex) {
+ throw new FlightRuntimeException(CallStatus.INTERNAL.withCause(ex));
+ } catch (FlightRuntimeException ex) {
+ throw ex;
+ } catch (Exception ex) {
+ throw new FlightRuntimeException(CallStatus.UNAUTHORIZED.withCause(ex));
}
+ }
+ @Override
+ public boolean validateBearer(String bearerToken) {
return false;
}
@Override
- public Optional<String> isValid(byte[] token) {
- return authValidator.isValid(token);
+ public boolean enableCachedCredentials() {
+ return true;
}
+ /**
+ * Interface that this handler delegates to forS determining if credentials
are valid.
Review comment:
Nit: should be for?
##########
File path:
java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ServerAuthMiddleware.java
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.CallContext;
+import org.apache.arrow.flight.CallHeaders;
+import org.apache.arrow.flight.CallInfo;
+import org.apache.arrow.flight.CallStatus;
+import org.apache.arrow.flight.FlightServerMiddleware;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * 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;
+
+ /**
+ * Construct a factory with the given auth handler.
+ * @param authHandler The auth handler what will be used for
authenticating requests.
+ */
+ public Factory(ServerAuthHandler authHandler) {
+ this.authHandler = authHandler;
+ bearerTokenAuthHandler = authHandler.enableCachedCredentials() ?
+ new GeneratedBearerTokenAuthHandler() : null;
+ }
+
+ @Override
+ public ServerAuthMiddleware onCallStarted(CallInfo callInfo, CallHeaders
incomingHeaders, CallContext context) {
+ logger.debug("Call name: {}", callInfo.method().name());
+ // Check if bearer token auth is being used, and if we've enabled use of
server-generated
+ // bearer tokens.
+ if (authHandler.enableCachedCredentials()) {
+ final String bearerTokenFromHeaders =
+ AuthUtilities.getValueFromAuthHeader(incomingHeaders,
AuthConstants.BEARER_PREFIX);
+ if (bearerTokenFromHeaders != null) {
+ final ServerAuthHandler.AuthResult result =
bearerTokenAuthHandler.authenticate(incomingHeaders);
Review comment:
@jduo Should the authenticate method take a token as an argument instead
of the incoming headers, as we execute the method `getValueFromAuthHeader`
again in the authenticate method to get the token from the incoming headers?
----------------------------------------------------------------
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:
[email protected]