jintongjiang commented on code in PR #6085:
URL: https://github.com/apache/hive/pull/6085#discussion_r2379924388


##########
service/src/java/org/apache/hive/service/auth/ldap/KerberosLdapFilterEnforcer.java:
##########
@@ -0,0 +1,104 @@
+/*
+ * 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.commons.lang3.StringUtils;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.security.SaslRpcServer;
+import org.apache.hive.service.auth.LdapAuthenticationProviderImpl;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.security.sasl.AuthenticationException;
+import java.io.IOException;
+
+/**
+ * Helper that encapsulates LDAP filter resolution and enforcement for 
Kerberos-authenticated users.
+ */
+public final class KerberosLdapFilterEnforcer {
+  private static final Logger LOG = 
LoggerFactory.getLogger(KerberosLdapFilterEnforcer.class);
+
+  private final HiveConf conf;
+  private final DirSearchFactory dirSearchFactory;
+  private final boolean enableLdapGroupCheck;
+  private final Filter filter;
+
+  public KerberosLdapFilterEnforcer(HiveConf conf, DirSearchFactory 
dirSearchFactory) {
+    this.conf = conf;
+    this.dirSearchFactory = dirSearchFactory;
+    this.enableLdapGroupCheck = 
conf.getBoolVar(HiveConf.ConfVars.HIVE_SERVER2_LDAP_ENABLE_GROUP_CHECK_AFTER_KERBEROS);
+    this.filter = enableLdapGroupCheck ? 
LdapAuthenticationProviderImpl.resolveFilter(conf) : null;
+
+    if (enableLdapGroupCheck && filter == null) {
+      LOG.warn("LDAP group check enabled but no filters configured");
+    }
+  }
+
+  /**
+   * Applies configured LDAP filters to authenticate a user principal.
+   *
+   * @param principal Kerberos principal to validate
+   * @return {@code true} if the principal passes all configured filters; 
{@code false} otherwise
+   */
+  public boolean applyLdapFilter(String principal) {
+    if (!enableLdapGroupCheck || filter == null) {
+      return true;
+    }
+
+    String user = extractUserName(principal);
+    try (DirSearch dirSearch = createDirSearch()) {
+      filter.apply(dirSearch, user);
+      LOG.debug("Principal {} passed LDAP filter validation", principal);
+      return true;
+    } catch (AuthenticationException e) {
+      LOG.warn("Principal {} failed LDAP filter validation: {}", principal, 
e.getMessage());
+      return false;
+    } catch (Exception e) {

Review Comment:
   fix in decc8688eb8a663d5d60a9481b55a726b9d4ab68



##########
service/src/java/org/apache/hive/service/auth/ldap/KerberosLdapFilterEnforcer.java:
##########
@@ -0,0 +1,104 @@
+/*
+ * 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.commons.lang3.StringUtils;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.security.SaslRpcServer;
+import org.apache.hive.service.auth.LdapAuthenticationProviderImpl;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.security.sasl.AuthenticationException;
+import java.io.IOException;
+
+/**
+ * Helper that encapsulates LDAP filter resolution and enforcement for 
Kerberos-authenticated users.
+ */
+public final class KerberosLdapFilterEnforcer {
+  private static final Logger LOG = 
LoggerFactory.getLogger(KerberosLdapFilterEnforcer.class);
+
+  private final HiveConf conf;
+  private final DirSearchFactory dirSearchFactory;
+  private final boolean enableLdapGroupCheck;
+  private final Filter filter;
+
+  public KerberosLdapFilterEnforcer(HiveConf conf, DirSearchFactory 
dirSearchFactory) {
+    this.conf = conf;
+    this.dirSearchFactory = dirSearchFactory;
+    this.enableLdapGroupCheck = 
conf.getBoolVar(HiveConf.ConfVars.HIVE_SERVER2_LDAP_ENABLE_GROUP_CHECK_AFTER_KERBEROS);
+    this.filter = enableLdapGroupCheck ? 
LdapAuthenticationProviderImpl.resolveFilter(conf) : null;
+
+    if (enableLdapGroupCheck && filter == null) {
+      LOG.warn("LDAP group check enabled but no filters configured");
+    }
+  }
+
+  /**
+   * Applies configured LDAP filters to authenticate a user principal.
+   *
+   * @param principal Kerberos principal to validate
+   * @return {@code true} if the principal passes all configured filters; 
{@code false} otherwise
+   */
+  public boolean applyLdapFilter(String principal) {
+    if (!enableLdapGroupCheck || filter == null) {

Review Comment:
   fix in decc8688eb8a663d5d60a9481b55a726b9d4ab68



##########
service/src/java/org/apache/hive/service/cli/thrift/ThriftHttpServlet.java:
##########
@@ -516,7 +528,11 @@ public String run() throws HttpAuthenticationException {
               "unable to establish context with the service ticket " +
               "provided by the client.");
         } else {
-          return 
getPrincipalWithoutRealmAndHost(gssContext.getSrcName().toString());
+          String principal = gssContext.getSrcName().toString();
+          String shortName = getPrincipalWithoutRealmAndHost(principal);
+          LOG.debug("Kerberos authentication successful");
+          enforceLdapFilters(principal);

Review Comment:
   fix in decc8688eb8a663d5d60a9481b55a726b9d4ab68



-- 
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: [email protected]

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