saihemanth-cloudera commented on code in PR #6085:
URL: https://github.com/apache/hive/pull/6085#discussion_r2361345384
##########
common/src/java/org/apache/hadoop/hive/conf/HiveConf.java:
##########
@@ -4353,6 +4353,13 @@ public static enum ConfVars {
HIVE_SERVER2_PLAIN_LDAP_BIND_PASSWORD("hive.server2.authentication.ldap.bindpw",
null,
"The password for the bind user, to be used to search for the full
name of the user being authenticated.\n" +
"If the username is specified, this parameter must also be
specified."),
+ HIVE_SERVER2_LDAP_ENABLE_GROUP_CHECK_AFTER_KERBEROS(
Review Comment:
IMO, instead of adding a new config, we should enable group check by default
if the auth mode is set to Kerberos
##########
service/src/java/org/apache/hive/service/auth/ldap/LdapGroupCallbackHandler.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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 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.apache.hive.service.cli.session.SessionManager;
+import org.jetbrains.annotations.NotNull;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import javax.security.sasl.AuthenticationException;
+import javax.security.sasl.AuthorizeCallback;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Callback handler that enforces LDAP filters on Kerberos-authenticated users.
+ * This handler applies the same LDAP filter resolution used for LDAP
authentication
+ * to Kerberos users, ensuring consistent authorization policies.
+ */
+public class LdapGroupCallbackHandler implements CallbackHandler {
+ private static final Logger LOG =
LoggerFactory.getLogger(LdapGroupCallbackHandler.class);
+
+ private final HiveConf conf;
+ private final boolean enableLdapGroupCheck;
+ private final CallbackHandler delegateHandler;
+ private final DirSearchFactory dirSearchFactory;
+ private final Filter filter;
+
+ public LdapGroupCallbackHandler(HiveConf conf) {
+ this(conf, new LdapSearchFactory(), new
SaslRpcServer.SaslGssCallbackHandler());
+ }
+
+ public LdapGroupCallbackHandler(HiveConf conf, DirSearchFactory
dirSearchFactory, CallbackHandler delegateHandler) {
+ this.conf = conf;
+ this.delegateHandler = delegateHandler;
+ 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");
+ }
+ }
+
+ @Override
+ public void handle(Callback[] callbacks) throws IOException,
UnsupportedCallbackException {
+ List<Callback> unhandledCallbacks = new ArrayList<>();
+
+ for (Callback callback : callbacks) {
+ if (callback instanceof AuthorizeCallback) {
+ AuthorizeCallback ac = (AuthorizeCallback) callback;
+ String authenticationID = ac.getAuthenticationID();
+ String authorizationID = ac.getAuthorizationID();
+
+ if (!authenticationID.equals(authorizationID)) {
+ LOG.debug("Delegating authorization for different auth IDs");
+ unhandledCallbacks.add(callback);
+ continue;
+ }
+
+ // Skip LDAP check for proxy users.
+ String proxyUser = SessionManager.getProxyUserName();
+ if (proxyUser != null && !proxyUser.isEmpty()) {
+ LOG.debug("Skipping LDAP filters for proxy user authorization");
+ ac.setAuthorized(true);
+ continue;
+ }
+
+ // If group check is not enabled or no filter configured, authorize
immediately.
+ if (!enableLdapGroupCheck || filter == null) {
+ ac.setAuthorized(true);
+ continue;
+ }
+
+ String user = extractUserName(authenticationID);
+ boolean authorized = applyLdapFilter(user);
+ ac.setAuthorized(authorized);
+ } else {
+ unhandledCallbacks.add(callback);
+ }
+ }
+
+ if (!unhandledCallbacks.isEmpty()) {
+ delegateHandler.handle(unhandledCallbacks.toArray(new Callback[0]));
+ }
+ }
+
+ /**
+ * Applies configured LDAP filters to authenticate a user.
+ *
+ * @param user the username to validate
+ * @return true if the user passes all configured filters, false otherwise
+ */
+ private boolean applyLdapFilter(String user) {
+ try {
+ String bindDN =
conf.getVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_BIND_USER);
+ char[] rawPassword =
conf.getPassword(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_BIND_PASSWORD.varname);
+ String bindPassword = (rawPassword == null) ? null : new
String(rawPassword);
+
+ if (StringUtils.isBlank(bindDN) || StringUtils.isBlank(bindPassword)) {
+ LOG.error("LDAP bind DN or password is not configured");
+ return false;
+ }
+
+ DirSearch dirSearch = this.dirSearchFactory.getInstance(conf, bindDN,
bindPassword);
+
+ filter.apply(dirSearch, user);
+ LOG.debug("User {} passed LDAP filter validation", user);
+ return true;
+
+ } catch (AuthenticationException e) {
+ LOG.warn("User {} failed LDAP filter validation: {}", user,
e.getMessage());
+ return false;
+ } catch (Exception e) {
+ LOG.error("Error applying LDAP filter for user {}", user, e);
+ return false;
+ }
+ }
+
+ private String extractUserName(@NotNull String principal) {
Review Comment:
make this public and add '@VisibleForTesting' and use this in
TestThriftHttpKerberosLdapFilter.java#L299
##########
service/src/java/org/apache/hive/service/auth/ldap/LdapGroupCallbackHandler.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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 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.apache.hive.service.cli.session.SessionManager;
+import org.jetbrains.annotations.NotNull;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import javax.security.sasl.AuthenticationException;
+import javax.security.sasl.AuthorizeCallback;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Callback handler that enforces LDAP filters on Kerberos-authenticated users.
+ * This handler applies the same LDAP filter resolution used for LDAP
authentication
+ * to Kerberos users, ensuring consistent authorization policies.
+ */
+public class LdapGroupCallbackHandler implements CallbackHandler {
+ private static final Logger LOG =
LoggerFactory.getLogger(LdapGroupCallbackHandler.class);
+
+ private final HiveConf conf;
+ private final boolean enableLdapGroupCheck;
+ private final CallbackHandler delegateHandler;
+ private final DirSearchFactory dirSearchFactory;
+ private final Filter filter;
+
+ public LdapGroupCallbackHandler(HiveConf conf) {
+ this(conf, new LdapSearchFactory(), new
SaslRpcServer.SaslGssCallbackHandler());
+ }
+
+ public LdapGroupCallbackHandler(HiveConf conf, DirSearchFactory
dirSearchFactory, CallbackHandler delegateHandler) {
+ this.conf = conf;
+ this.delegateHandler = delegateHandler;
+ 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");
+ }
+ }
+
+ @Override
+ public void handle(Callback[] callbacks) throws IOException,
UnsupportedCallbackException {
+ List<Callback> unhandledCallbacks = new ArrayList<>();
+
+ for (Callback callback : callbacks) {
+ if (callback instanceof AuthorizeCallback) {
+ AuthorizeCallback ac = (AuthorizeCallback) callback;
+ String authenticationID = ac.getAuthenticationID();
+ String authorizationID = ac.getAuthorizationID();
+
+ if (!authenticationID.equals(authorizationID)) {
Review Comment:
Is there a chance that authorizationID or authenticationID be null? If so,
we'll get a NullPointerException here
##########
service/src/test/org/apache/hive/service/auth/TestLdapKerberosWithGroupFilter.java:
##########
@@ -0,0 +1,312 @@
+/*
+ * 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.hadoop.hive.conf.HiveConf;
+import org.apache.hive.service.auth.ldap.DirSearch;
+import org.apache.hive.service.auth.ldap.DirSearchFactory;
+import org.apache.hive.service.auth.ldap.Filter;
+import org.apache.hive.service.auth.ldap.LdapGroupCallbackHandler;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.sasl.AuthenticationException;
+import javax.security.sasl.AuthorizeCallback;
+
+import java.util.Collections;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+
+/**
+ * Tests for Kerberos authentication with LDAP group filtering.
+ * This test uses mocks to avoid the need for real LDAP or Kerberos servers.
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class TestLdapKerberosWithGroupFilter {
+
+ private static final String GROUP1_NAME = "group1";
+ private static final String GROUP2_NAME = "group2";
+ private static final String USER1_ID = "user1";
+ private static final String USER2_ID = "user2";
+ private static final String USER1_PRINCIPAL = USER1_ID + "@TEST.REALM";
+ private static final String USER2_PRINCIPAL = USER2_ID + "@TEST.REALM";
+
+ @Mock
+ private DirSearch dirSearch;
+
+ @Mock
+ private DirSearchFactory dirSearchFactory;
+
+ @Mock
+ private CallbackHandler delegateHandler;
+
+ private HiveConf conf;
+
+ @Before
+ public void setup() throws Exception {
+ conf = new HiveConf();
+ conf.set("hive.root.logger", "DEBUG,console");
+
+ // Setup LDAP connection parameters
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_URL,
"ldap://localhost:10389");
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_BIND_USER,
"cn=admin,dc=example,dc=com");
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_BIND_PASSWORD,
"admin");
+
+ // Configure Kerberos auth
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_AUTHENTICATION, "KERBEROS");
Review Comment:
Can you also verify multiple auth scenarios?
##########
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/AuthFactory.java:
##########
@@ -21,6 +21,7 @@
import java.io.IOException;
import java.util.HashMap;
+import javax.security.auth.callback.CallbackHandler;
Review Comment:
Is this required?
##########
service/src/java/org/apache/hive/service/auth/ldap/LdapGroupCallbackHandler.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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 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.apache.hive.service.cli.session.SessionManager;
+import org.jetbrains.annotations.NotNull;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import javax.security.sasl.AuthenticationException;
+import javax.security.sasl.AuthorizeCallback;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Callback handler that enforces LDAP filters on Kerberos-authenticated users.
+ * This handler applies the same LDAP filter resolution used for LDAP
authentication
+ * to Kerberos users, ensuring consistent authorization policies.
+ */
+public class LdapGroupCallbackHandler implements CallbackHandler {
+ private static final Logger LOG =
LoggerFactory.getLogger(LdapGroupCallbackHandler.class);
+
+ private final HiveConf conf;
+ private final boolean enableLdapGroupCheck;
+ private final CallbackHandler delegateHandler;
+ private final DirSearchFactory dirSearchFactory;
+ private final Filter filter;
+
+ public LdapGroupCallbackHandler(HiveConf conf) {
+ this(conf, new LdapSearchFactory(), new
SaslRpcServer.SaslGssCallbackHandler());
+ }
+
+ public LdapGroupCallbackHandler(HiveConf conf, DirSearchFactory
dirSearchFactory, CallbackHandler delegateHandler) {
+ this.conf = conf;
+ this.delegateHandler = delegateHandler;
+ 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");
+ }
+ }
+
+ @Override
+ public void handle(Callback[] callbacks) throws IOException,
UnsupportedCallbackException {
+ List<Callback> unhandledCallbacks = new ArrayList<>();
+
+ for (Callback callback : callbacks) {
+ if (callback instanceof AuthorizeCallback) {
+ AuthorizeCallback ac = (AuthorizeCallback) callback;
+ String authenticationID = ac.getAuthenticationID();
+ String authorizationID = ac.getAuthorizationID();
+
+ if (!authenticationID.equals(authorizationID)) {
+ LOG.debug("Delegating authorization for different auth IDs");
+ unhandledCallbacks.add(callback);
+ continue;
+ }
+
+ // Skip LDAP check for proxy users.
+ String proxyUser = SessionManager.getProxyUserName();
+ if (proxyUser != null && !proxyUser.isEmpty()) {
+ LOG.debug("Skipping LDAP filters for proxy user authorization");
+ ac.setAuthorized(true);
+ continue;
+ }
Review Comment:
This class executes at the service setup stage, do we really need this?
because we anyway skip proxy user authorization check at run time.
##########
service/src/test/org/apache/hive/service/auth/TestLdapKerberosWithGroupFilter.java:
##########
@@ -0,0 +1,312 @@
+/*
+ * 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.hadoop.hive.conf.HiveConf;
+import org.apache.hive.service.auth.ldap.DirSearch;
+import org.apache.hive.service.auth.ldap.DirSearchFactory;
+import org.apache.hive.service.auth.ldap.Filter;
+import org.apache.hive.service.auth.ldap.LdapGroupCallbackHandler;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.sasl.AuthenticationException;
+import javax.security.sasl.AuthorizeCallback;
+
+import java.util.Collections;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+
+/**
+ * Tests for Kerberos authentication with LDAP group filtering.
+ * This test uses mocks to avoid the need for real LDAP or Kerberos servers.
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class TestLdapKerberosWithGroupFilter {
+
+ private static final String GROUP1_NAME = "group1";
+ private static final String GROUP2_NAME = "group2";
+ private static final String USER1_ID = "user1";
+ private static final String USER2_ID = "user2";
+ private static final String USER1_PRINCIPAL = USER1_ID + "@TEST.REALM";
+ private static final String USER2_PRINCIPAL = USER2_ID + "@TEST.REALM";
+
+ @Mock
+ private DirSearch dirSearch;
+
+ @Mock
+ private DirSearchFactory dirSearchFactory;
+
+ @Mock
+ private CallbackHandler delegateHandler;
+
+ private HiveConf conf;
+
+ @Before
+ public void setup() throws Exception {
+ conf = new HiveConf();
+ conf.set("hive.root.logger", "DEBUG,console");
+
+ // Setup LDAP connection parameters
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_URL,
"ldap://localhost:10389");
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_BIND_USER,
"cn=admin,dc=example,dc=com");
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_BIND_PASSWORD,
"admin");
+
+ // Configure Kerberos auth
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_AUTHENTICATION, "KERBEROS");
+
+ // Reset mocks before each test
+ reset(dirSearchFactory, dirSearch, delegateHandler);
+
+ // Set up the default mock behavior
+ when(dirSearchFactory.getInstance(any(HiveConf.class), anyString(),
anyString()))
+ .thenReturn(dirSearch);
+ }
+
+ @After
+ public void tearDown() {
+ conf = null;
+ }
+
+ @Test
+ public void testKerberosAuthWithLdapGroupCheckPositive() throws Exception {
+ // Configure LDAP to allow only users in group1
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_GROUPFILTER,
GROUP1_NAME);
+
conf.setBoolVar(HiveConf.ConfVars.HIVE_SERVER2_LDAP_ENABLE_GROUP_CHECK_AFTER_KERBEROS,
true);
+ String userDn = "uid=user1,dc=example,dc=com";
+ String groupDn = "cn=group1,dc=example,dc=com";
+
+ // Mock the DirSearch to succeed for both UserSearchFilter and
GroupMembershipKeyFilter
+ when(dirSearch.findUserDn(USER1_ID)).thenReturn(userDn);
+
when(dirSearch.findGroupsForUser(eq(userDn))).thenReturn(Collections.singletonList(groupDn));
+
+ // Create the callback handler with our test configuration
+ LdapGroupCallbackHandler callbackHandler = new LdapGroupCallbackHandler(
+ conf, dirSearchFactory, delegateHandler);
+
+ // Create an AuthorizeCallback as would be done by Kerberos authentication
+ AuthorizeCallback ac = new AuthorizeCallback(USER1_PRINCIPAL,
USER1_PRINCIPAL);
+ Callback[] callbacks = {ac};
+ callbackHandler.handle(callbacks);
+
+ assertTrue(ac.isAuthorized());
+
+ // Verify LDAP operations occurred
+ verify(dirSearchFactory).getInstance(eq(conf),
eq("cn=admin,dc=example,dc=com"), eq("admin")); // More specific
+ verify(dirSearch, times(2)).findUserDn(USER1_ID);
+ verify(dirSearch).findGroupsForUser(eq(userDn)); // Changed from
anyString()
+
+ }
+
+ @Test
+ public void testKerberosAuthWithLdapGroupCheckNegative() throws Exception {
+ // Configure LDAP to allow only users in group1
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_GROUPFILTER,
GROUP1_NAME);
+
conf.setBoolVar(HiveConf.ConfVars.HIVE_SERVER2_LDAP_ENABLE_GROUP_CHECK_AFTER_KERBEROS,
true);
+
+ String userDn = "uid=user2,dc=example,dc=com";
+ String wrongGroupDn = "cn=group3,dc=example,dc=com";
+
+ when(dirSearch.findUserDn(USER2_ID)).thenReturn(userDn);
+
when(dirSearch.findGroupsForUser(eq(userDn))).thenReturn(Collections.singletonList(wrongGroupDn));
+
+ LdapGroupCallbackHandler callbackHandler = new LdapGroupCallbackHandler(
+ conf, dirSearchFactory, delegateHandler);
+
+ AuthorizeCallback ac = new AuthorizeCallback(USER2_PRINCIPAL,
USER2_PRINCIPAL);
+ Callback[] callbacks = {ac};
+ callbackHandler.handle(callbacks);
+
+ assertFalse(ac.isAuthorized());
+
+ verify(dirSearch, times(2)).findUserDn(USER2_ID);
+ verify(dirSearch).findGroupsForUser(eq(userDn));
+ }
+
+ @Test
+ public void testKerberosAuthWithMultipleLdapGroupCheckPositive() throws
Exception {
+ // Configure LDAP to allow users in either group1 or group2
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_GROUPFILTER,
GROUP1_NAME + "," + GROUP2_NAME);
+
conf.setBoolVar(HiveConf.ConfVars.HIVE_SERVER2_LDAP_ENABLE_GROUP_CHECK_AFTER_KERBEROS,
true);
+
+ // Test user1 in group1
+
when(dirSearch.findUserDn(USER1_ID)).thenReturn("uid=user1,dc=example,dc=com");
+
when(dirSearch.findGroupsForUser("uid=user1,dc=example,dc=com")).thenReturn(
+ Collections.singletonList("cn=group1,dc=example,dc=com"));
+
+ LdapGroupCallbackHandler callbackHandler1 = new LdapGroupCallbackHandler(
+ conf, dirSearchFactory, delegateHandler);
+
+ AuthorizeCallback ac1 = new AuthorizeCallback(USER1_PRINCIPAL,
USER1_PRINCIPAL);
+ callbackHandler1.handle(new Callback[]{ac1});
+ assertTrue("User1 should be authorized", ac1.isAuthorized());
+
+ // Reset mocks for user2 test
+ reset(dirSearch);
+
when(dirSearch.findUserDn(USER2_ID)).thenReturn("uid=user2,dc=example,dc=com");
+
when(dirSearch.findGroupsForUser("uid=user2,dc=example,dc=com")).thenReturn(
+ Collections.singletonList("cn=group2,dc=example,dc=com"));
+
+ // Need to reset dirSearchFactory mock to return the updated dirSearch
+ reset(dirSearchFactory);
+ when(dirSearchFactory.getInstance(any(HiveConf.class), anyString(),
anyString()))
+ .thenReturn(dirSearch);
+
+ LdapGroupCallbackHandler callbackHandler2 = new LdapGroupCallbackHandler(
+ conf, dirSearchFactory, delegateHandler);
+
+ AuthorizeCallback ac2 = new AuthorizeCallback(USER2_PRINCIPAL,
USER2_PRINCIPAL);
+ callbackHandler2.handle(new Callback[]{ac2});
+ assertTrue("User2 should be authorized", ac2.isAuthorized());
+ }
+
+ @Test
+ public void testKerberosAuthWithUserGroupSearchFilter() throws Exception {
+ // Configure UserGroupSearchFilter
+
conf.setBoolVar(HiveConf.ConfVars.HIVE_SERVER2_LDAP_ENABLE_GROUP_CHECK_AFTER_KERBEROS,
true);
+
+ String userSearchFilter = "(&(uid={0})(objectClass=person))";
+ String baseDn = "dc=example,dc=com";
+ String groupSearchFilter = "(&(memberUid={0})(objectClass=posixGroup))";
+ String groupBaseDn = "ou=groups,dc=example,dc=com";
+
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_USERSEARCHFILTER,
userSearchFilter);
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_BASEDN, baseDn);
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_GROUPSEARCHFILTER,
groupSearchFilter);
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_GROUPBASEDN,
groupBaseDn);
+
+ String userDn = "uid=user1,dc=example,dc=com";
+
+ when(dirSearch.findUserDn(eq(USER1_ID), eq(userSearchFilter), eq(baseDn)))
+ .thenReturn(userDn);
+
+ when(dirSearch.executeUserAndGroupFilterQuery(
+ eq(USER1_ID),
+ eq(userDn),
+ eq(groupSearchFilter),
+ eq(groupBaseDn)))
+
.thenReturn(Collections.singletonList("cn=group1,ou=groups,dc=example,dc=com"));
+
+ LdapGroupCallbackHandler callbackHandler = new LdapGroupCallbackHandler(
+ conf, dirSearchFactory, delegateHandler);
+
+ AuthorizeCallback ac = new AuthorizeCallback(USER1_PRINCIPAL,
USER1_PRINCIPAL);
+ callbackHandler.handle(new Callback[]{ac});
+
+ assertTrue("User should be authorized with UserGroupSearchFilter",
ac.isAuthorized());
+
+ verify(dirSearch).findUserDn(eq(USER1_ID), eq(userSearchFilter),
eq(baseDn));
+ verify(dirSearch).executeUserAndGroupFilterQuery(eq(USER1_ID), eq(userDn),
eq(groupSearchFilter), eq(groupBaseDn));
+ }
+
+ @Test
+ public void testKerberosAuthWithDisabledLdapGroupCheck() throws Exception {
+ // Disable LDAP group check
+
conf.setBoolVar(HiveConf.ConfVars.HIVE_SERVER2_LDAP_ENABLE_GROUP_CHECK_AFTER_KERBEROS,
false);
+ // Even if a group filter is set, it should be ignored
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_GROUPFILTER,
GROUP1_NAME);
+
+ LdapGroupCallbackHandler callbackHandler = new LdapGroupCallbackHandler(
+ conf, dirSearchFactory, delegateHandler);
+
+ AuthorizeCallback ac1 = new AuthorizeCallback(USER1_PRINCIPAL,
USER1_PRINCIPAL);
+ AuthorizeCallback ac2 = new AuthorizeCallback(USER2_PRINCIPAL,
USER2_PRINCIPAL);
+
+ callbackHandler.handle(new Callback[]{ac1});
+ callbackHandler.handle(new Callback[]{ac2});
Review Comment:
nit: callbackHandler.handle(new Callback[]{ac1, ac2});
##########
service/src/test/org/apache/hive/service/auth/ldap/TestLdapGroupCallbackHandler.java:
##########
@@ -0,0 +1,288 @@
+/*
+ * 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 org.apache.hadoop.hive.conf.HiveConf;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import javax.security.sasl.AuthorizeCallback;
+
+import java.util.Collections;
+
+import static org.junit.Assert.*;
+import static org.mockito.ArgumentMatchers.*;
+import static org.mockito.Mockito.*;
+
+@RunWith(MockitoJUnitRunner.class)
+public class TestLdapGroupCallbackHandler {
+
+ private static final String TEST_USER = "user";
+ private static final String TEST_PRINCIPAL = TEST_USER + "@TEST.REALM";
+
+ @Mock
+ private DirSearch dirSearch;
+
+ @Mock
+ private DirSearchFactory dirSearchFactory;
+
+ @Mock
+ private javax.security.auth.callback.CallbackHandler delegateHandler;
+
+ private HiveConf conf;
+ private LdapGroupCallbackHandler callbackHandler;
+
+ @Before
+ public void setup() throws Exception {
+ conf = new HiveConf();
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_BIND_USER,
"bindUser");
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_BIND_PASSWORD,
"bindPassword");
+ when(dirSearchFactory.getInstance(any(HiveConf.class), anyString(),
anyString()))
+ .thenReturn(dirSearch);
+ }
+
+ @Test
+ public void testAuthorizeWithNoLdapFilter() throws Exception {
+ // Disable LDAP filter check
+
conf.setBoolVar(HiveConf.ConfVars.HIVE_SERVER2_LDAP_ENABLE_GROUP_CHECK_AFTER_KERBEROS,
false);
+
+ callbackHandler = new LdapGroupCallbackHandler(conf, dirSearchFactory,
delegateHandler);
+
+ AuthorizeCallback ac = new AuthorizeCallback(TEST_PRINCIPAL,
TEST_PRINCIPAL);
+ callbackHandler.handle(new Callback[]{ac});
+
+ // Expect immediate authorization when no LDAP filtering is enabled
+ assertTrue(ac.isAuthorized());
+ verifyNoInteractions(dirSearch);
+ }
+
+ @Test
+ public void testAuthorizeWithUserGroupSearchFilterSuccess() throws Exception
{
+ // Enable group check and configure UserGroupSearchFilter
+
conf.setBoolVar(HiveConf.ConfVars.HIVE_SERVER2_LDAP_ENABLE_GROUP_CHECK_AFTER_KERBEROS,
true);
+
+ String userSearchFilter = "(&(uid={0})(objectClass=person))";
+ String baseDn = "dc=example,dc=com";
+ String groupSearchFilter = "(&(memberUid={0})(objectClass=posixGroup))";
+ String groupBaseDn = "ou=groups,dc=example,dc=com";
+ String userDn = "uid=user,dc=example,dc=com";
+
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_USERSEARCHFILTER,
userSearchFilter);
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_BASEDN, baseDn);
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_GROUPSEARCHFILTER,
groupSearchFilter);
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_GROUPBASEDN,
groupBaseDn);
+
+ // Mock with specific values
+ when(dirSearch.findUserDn(eq(TEST_USER), eq(userSearchFilter), eq(baseDn)))
+ .thenReturn(userDn);
+ when(dirSearch.executeUserAndGroupFilterQuery(
+ eq(TEST_USER),
+ eq(userDn),
+ eq(groupSearchFilter),
+ eq(groupBaseDn)))
+
.thenReturn(Collections.singletonList("cn=group1,ou=groups,dc=example,dc=com"));
+
+ callbackHandler = new LdapGroupCallbackHandler(conf, dirSearchFactory,
delegateHandler);
+
+ AuthorizeCallback ac = new AuthorizeCallback(TEST_PRINCIPAL,
TEST_PRINCIPAL);
+ callbackHandler.handle(new Callback[]{ac});
+
+ assertTrue(ac.isAuthorized());
+ verify(dirSearchFactory).getInstance(eq(conf), eq("bindUser"),
eq("bindPassword"));
+ verify(dirSearch).findUserDn(eq(TEST_USER), eq(userSearchFilter),
eq(baseDn));
+ verify(dirSearch).executeUserAndGroupFilterQuery(
+ eq(TEST_USER), eq(userDn), eq(groupSearchFilter), eq(groupBaseDn));
+ }
+
+ @Test
+ public void testAuthorizeWithCustomQueryFilterSuccess() throws Exception {
+ // Enable group check and configure CustomQueryFilter
+
conf.setBoolVar(HiveConf.ConfVars.HIVE_SERVER2_LDAP_ENABLE_GROUP_CHECK_AFTER_KERBEROS,
true);
+
+ String customQuery = "(&(objectClass=person)(uid=" + TEST_USER + "))";
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_CUSTOMLDAPQUERY,
customQuery);
+
+ // Mock with specific query
+ when(dirSearch.executeCustomQuery(eq(customQuery)))
+ .thenReturn(Collections.singletonList("uid=" + TEST_USER +
",dc=example,dc=com"));
+
+ callbackHandler = new LdapGroupCallbackHandler(conf, dirSearchFactory,
delegateHandler);
+
+ AuthorizeCallback ac = new AuthorizeCallback(TEST_PRINCIPAL,
TEST_PRINCIPAL);
+ callbackHandler.handle(new Callback[]{ac});
+
+ assertTrue(ac.isAuthorized());
+ verify(dirSearchFactory).getInstance(eq(conf), eq("bindUser"),
eq("bindPassword"));
+ verify(dirSearch).executeCustomQuery(eq(customQuery));
+ }
+
+ @Test
+ public void testAuthorizeWithGroupFilterFailure() throws Exception {
+ // Enable group check and configure GroupFilter
+
conf.setBoolVar(HiveConf.ConfVars.HIVE_SERVER2_LDAP_ENABLE_GROUP_CHECK_AFTER_KERBEROS,
true);
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_GROUPFILTER,
"group1,group2");
+
+ String userDn = "uid=user,dc=example,dc=com";
+ String wrongGroupDn = "cn=group3,dc=example,dc=com";
+
+ // Mock with specific DN values
+ when(dirSearch.findUserDn(TEST_USER)).thenReturn(userDn);
+ when(dirSearch.findGroupsForUser(eq(userDn)))
+ .thenReturn(Collections.singletonList(wrongGroupDn));
+
+ callbackHandler = new LdapGroupCallbackHandler(conf, dirSearchFactory,
delegateHandler);
+
+ AuthorizeCallback ac = new AuthorizeCallback(TEST_PRINCIPAL,
TEST_PRINCIPAL);
+ callbackHandler.handle(new Callback[]{ac});
+
+ assertFalse(ac.isAuthorized());
+ verify(dirSearchFactory).getInstance(eq(conf), eq("bindUser"),
eq("bindPassword"));
+ verify(dirSearch, times(2)).findUserDn(TEST_USER);
+ verify(dirSearch).findGroupsForUser(eq(userDn));
+ }
+
+ @Test
+ public void testAuthorizeWithNoFilterConfigured() throws Exception {
+ // Enable group check but don't configure any filters
+
conf.setBoolVar(HiveConf.ConfVars.HIVE_SERVER2_LDAP_ENABLE_GROUP_CHECK_AFTER_KERBEROS,
true);
+ // No filters configured - resolveFilter will return null
+
+ callbackHandler = new LdapGroupCallbackHandler(conf, dirSearchFactory,
delegateHandler);
+
+ AuthorizeCallback ac = new AuthorizeCallback(TEST_PRINCIPAL,
TEST_PRINCIPAL);
+ callbackHandler.handle(new Callback[]{ac});
+
+ // Should authorize since no filter is configured (logged warning)
+ assertTrue(ac.isAuthorized());
+ verifyNoInteractions(dirSearch);
+ }
+
+ @Test
+ public void testDelegationWithDifferentAuthIds() throws Exception {
+
conf.setBoolVar(HiveConf.ConfVars.HIVE_SERVER2_LDAP_ENABLE_GROUP_CHECK_AFTER_KERBEROS,
true);
+
+ String authorizationId = "[email protected]";
+ AuthorizeCallback ac = new AuthorizeCallback(TEST_PRINCIPAL,
authorizationId);
+ Callback[] callbacks = {ac};
+
+ callbackHandler = new LdapGroupCallbackHandler(conf, dirSearchFactory,
delegateHandler);
+ callbackHandler.handle(callbacks);
+
+ // Since authentication and authorization IDs differ, the handler should
delegate
+ verify(delegateHandler).handle(argThat(callbackArray ->
+ callbackArray.length == 1 && callbackArray[0] == ac));
+ verifyNoInteractions(dirSearch);
+ }
+
+ @Test
+ public void testAuthorizeWithMissingBindCredentials() throws Exception {
+
conf.setBoolVar(HiveConf.ConfVars.HIVE_SERVER2_LDAP_ENABLE_GROUP_CHECK_AFTER_KERBEROS,
true);
+ conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_GROUPFILTER,
"group1");
+ conf.unset(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_BIND_USER.varname);
+
+ callbackHandler = new LdapGroupCallbackHandler(conf, dirSearchFactory,
delegateHandler);
+
+ AuthorizeCallback ac = new AuthorizeCallback(TEST_PRINCIPAL,
TEST_PRINCIPAL);
+ callbackHandler.handle(new Callback[]{ac});
+
+ // Missing bind credentials should cause authorization failure
+ assertFalse(ac.isAuthorized());
+ verifyNoInteractions(dirSearch);
+ }
+
+ @Test
+ public void testHandleUnsupportedCallback() throws Exception {
+ Callback unsupportedCallback = mock(Callback.class);
+ Callback[] callbacks = {unsupportedCallback};
+
+ doThrow(new UnsupportedCallbackException(unsupportedCallback))
+ .when(delegateHandler).handle(any(Callback[].class));
+
+ callbackHandler = new LdapGroupCallbackHandler(conf, dirSearchFactory,
delegateHandler);
+
+ try {
+ callbackHandler.handle(callbacks);
+ fail("Expected UnsupportedCallbackException");
+ } catch (UnsupportedCallbackException e) {
+ assertEquals(unsupportedCallback, e.getCallback());
+ }
Review Comment:
Instead of this, why not add: @Test(expected =
UnsupportedCallbackException.class)
--
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]