vihangk1 commented on a change in pull request #1791:
URL: https://github.com/apache/hive/pull/1791#discussion_r555489042



##########
File path: jdbc/src/java/org/apache/hive/jdbc/saml/HiveJdbcBrowserClient.java
##########
@@ -0,0 +1,322 @@
+/*
+ * 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.hive.jdbc.saml;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import java.awt.Desktop;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.io.UnsupportedEncodingException;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.SocketTimeoutException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URLDecoder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import org.apache.hive.jdbc.Utils.JdbcConnectionParams;
+import org.apache.hive.service.auth.saml.HiveSamlUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This class is used to execute a browser based SSO workflow with the 
authentication mode
+ * is browser.
+ */
+public class HiveJdbcBrowserClient implements IJdbcBrowserClient {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(HiveJdbcBrowserClient.class);
+  // error message when the socket times out.
+  @VisibleForTesting
+  public static final String TIMEOUT_ERROR_MSG = "Timed out while waiting for 
server response";
+  private final ServerSocket serverSocket;
+  private HiveJdbcBrowserServerResponse serverResponse;
+  protected JdbcBrowserClientContext clientContext;
+  // By default we wait for 2 min unless overridden by a JDBC connection param
+  // browserResponseTimeout
+  private static final int DEFAULT_SOCKET_TIMEOUT_SECS = 120;
+  private final ExecutorService serverResponseThread = 
Executors.newSingleThreadExecutor(
+      new ThreadFactoryBuilder().setNameFormat("Hive-Jdbc-Browser-Client-%d")
+          .setDaemon(true).build());
+
+  HiveJdbcBrowserClient(JdbcConnectionParams connectionParams)
+      throws HiveJdbcBrowserException {
+    serverSocket = getServerSocket(connectionParams.getSessionVars());
+  }
+
+  private ServerSocket getServerSocket(Map<String, String> sessionConf)
+      throws HiveJdbcBrowserException {
+    final ServerSocket serverSocket;
+    int port = Integer.parseInt(sessionConf
+        .getOrDefault(JdbcConnectionParams.AUTH_BROWSER_RESPONSE_PORT, "0"));
+    int timeout = Integer.parseInt(
+        
sessionConf.getOrDefault(JdbcConnectionParams.AUTH_BROWSER_RESPONSE_TIMEOUT_SECS,
+            String.valueOf(DEFAULT_SOCKET_TIMEOUT_SECS)));
+    try {
+      serverSocket = new ServerSocket(port, 0,
+          InetAddress.getByName(HiveSamlUtils.LOOP_BACK_INTERFACE));
+      LOG.debug("Browser response timeout is set to {} seconds", timeout);
+      serverSocket.setSoTimeout(timeout * 1000);
+    } catch (IOException e) {
+      throw new HiveJdbcBrowserException("Unable to bind to the localhost");
+    }
+    return serverSocket;
+  }
+
+  public Integer getPort() {
+    return serverSocket.getLocalPort();
+  }
+
+  @Override
+  public void close() throws IOException {
+    if (serverSocket != null) {
+      serverSocket.close();
+    }
+  }
+
+  public void init(JdbcBrowserClientContext clientContext) {
+    // everytime we set the sso URI we should clean up the previous state if 
its set.
+    // this may be from the previous invalid connection attempt or if the 
token has
+    // expired
+    reset();
+    this.clientContext = clientContext;
+    LOG.trace("Initialized the JDBCBrowser client with URL {}",
+        clientContext.getSsoUri());
+  }
+
+  private void reset() {
+    serverResponse = null;
+    clientContext = null;
+  }
+
+  private boolean validateSSOUrl(URI ssoUrl) {
+    //TODO(Vihang) add URL validation code here
+    return true;

Review comment:
       Initially I thought we can add a validation check to confirm that the 
SSO URL is from an expected domain. However, I plan to move that to a separate 
task since it is not critical. I removed this from the patch.




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



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to