difin commented on code in PR #5399:
URL: https://github.com/apache/hive/pull/5399#discussion_r1731723038


##########
service/src/java/org/apache/hive/service/auth/HttpAuthService.java:
##########
@@ -0,0 +1,267 @@
+/*
+ * 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.service.auth;
+
+import org.apache.hive.service.CookieSigner;
+import org.apache.hive.service.auth.ldap.HttpEmptyAuthenticationException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.core.NewCookie;
+import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
+import java.security.SecureRandom;
+import java.util.Base64;
+
+public class HttpAuthService {
+  private static final Logger LOG = 
LoggerFactory.getLogger(HttpAuthService.class);
+  public static final String USERNAME_REQUEST_PARAM_NAME = "username";
+  public static final String PASSWORD_REQUEST_PARAM_NAME = "password";
+  private static final SecureRandom RAN = new SecureRandom();
+  private final CookieSigner signer;
+  private final String cookieDomain;
+  private final String cookiePath;
+  private final int cookieMaxAge;
+  private final boolean isCookieSecure;
+  private final String authCookieName;
+  
+  public HttpAuthService(String cookieDomain, String cookiePath, int 
cookieMaxAge, boolean isCookieSecure, 
+      String authCookieName) {
+    String secret = Long.toString(RAN.nextLong());
+    this.signer = new CookieSigner(secret.getBytes());
+    this.cookieMaxAge = cookieMaxAge;
+    this.cookieDomain = cookieDomain;
+    this.cookiePath = cookiePath;
+    this.authCookieName = authCookieName;
+    this.isCookieSecure = isCookieSecure;
+  }
+
+  public Cookie signAndcreateCookie(String cookieToken) throws 
UnsupportedEncodingException {
+    return createCookie(signer.signCookie(cookieToken));
+  }
+
+  /**
+   * Generate a server side cookie given the cookie value as the input.
+   * @param str Signed input string token.
+   * @return The generated cookie.
+   */
+  public Cookie createCookie(String str) {
+    if (LOG.isDebugEnabled()) {
+      LOG.debug("Cookie name = " + authCookieName + " value = " + str);

Review Comment:
   Done



##########
service/src/java/org/apache/hive/service/auth/ldap/LdapAuthService.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * 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.service.auth.ldap;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hive.service.auth.AuthenticationProviderFactory;
+import org.apache.hive.service.auth.HttpAuthService;
+import org.apache.hive.service.auth.HttpAuthUtils;
+import org.apache.hive.service.auth.HttpAuthenticationException;
+import org.apache.hive.service.auth.PasswdAuthenticationProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.security.sasl.AuthenticationException;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.UnsupportedEncodingException;
+import java.util.concurrent.TimeUnit;
+
+public class LdapAuthService extends HttpAuthService {
+  private static final Logger LOG = 
LoggerFactory.getLogger(LdapAuthService.class);
+  public static final String HIVE_SERVER2_WEBUI_AUTH_COOKIE_NAME = 
"hive.server2.webui.auth";
+  private final PasswdAuthenticationProvider authProvider;
+  
+  public LdapAuthService(HiveConf hiveConf) {
+    super(
+        
hiveConf.getVar(HiveConf.ConfVars.HIVE_SERVER2_WEBUI_HTTP_COOKIE_DOMAIN),
+        
hiveConf.getVar(HiveConf.ConfVars.HIVE_SERVER2_WEBUI_HTTP_COOKIE_PATH), 
+        (int) 
hiveConf.getTimeVar(HiveConf.ConfVars.HIVE_SERVER2_WEBUI_HTTP_COOKIE_MAX_AGE, 
TimeUnit.SECONDS),
+        hiveConf.getBoolVar(HiveConf.ConfVars.HIVE_SERVER2_USE_SSL),
+        HIVE_SERVER2_WEBUI_AUTH_COOKIE_NAME);
+    try {
+      this.authProvider = AuthenticationProviderFactory
+          
.getAuthenticationProvider(AuthenticationProviderFactory.AuthMethods.LDAP, 
hiveConf);
+      // always send secure cookies for SSL mode
+    } catch (AuthenticationException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  @VisibleForTesting
+  public LdapAuthService(HiveConf hiveConf, PasswdAuthenticationProvider 
provider) {
+    super(
+        
hiveConf.getVar(HiveConf.ConfVars.HIVE_SERVER2_WEBUI_HTTP_COOKIE_DOMAIN),
+        hiveConf.getVar(HiveConf.ConfVars.HIVE_SERVER2_WEBUI_HTTP_COOKIE_PATH),
+        (int) 
hiveConf.getTimeVar(HiveConf.ConfVars.HIVE_SERVER2_WEBUI_HTTP_COOKIE_MAX_AGE, 
TimeUnit.SECONDS),
+        hiveConf.getBoolVar(HiveConf.ConfVars.HIVE_SERVER2_USE_SSL),
+        HIVE_SERVER2_WEBUI_AUTH_COOKIE_NAME);
+    this.authProvider = provider;
+  }
+  
+  public boolean authorize(HttpServletRequest request, HttpServletResponse 
response) {
+    try {
+      String clientUserName = validateCookie(request);
+      if (clientUserName == null) {
+        clientUserName = getUsername(request);
+        authProvider.Authenticate(clientUserName, getPassword(request));
+
+        String cookieToken = HttpAuthUtils.createCookieToken(clientUserName);
+        Cookie hs2Cookie = signAndcreateCookie(cookieToken);
+
+        response.addCookie(hs2Cookie);
+        LOG.info("Cookie added for clientUserName " + clientUserName);

Review Comment:
   Done



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

To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org
For additional commands, e-mail: gitbox-h...@hive.apache.org

Reply via email to