forked from apache/hive
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
251 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
service/src/java/org/apache/hive/service/auth/ldap/PatternFilterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.apache.hive.service.auth.ldap; | ||
|
||
import org.apache.hadoop.hive.conf.HiveConf; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.security.sasl.AuthenticationException; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
public class PatternFilterFactory implements FilterFactory { | ||
|
||
@Override | ||
public Filter getInstance(HiveConf conf) { | ||
final boolean usePatternFilter = conf.getBoolVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_USE_PATTERN_FILTER); | ||
|
||
if (usePatternFilter) { | ||
final List<String> userPatterns = LdapUtils.parseDnPatterns(conf, | ||
HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_USERDNPATTERN); | ||
final List<String> groupBases = LdapUtils.patternsToBaseDns(LdapUtils.parseDnPatterns(conf, | ||
HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_GROUPDNPATTERN)); | ||
final List<String> userBases = LdapUtils.patternsToBaseDns(userPatterns); | ||
return new PatternFilter(userBases, groupBases); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
|
||
private static final class PatternFilter implements Filter { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(PatternFilter.class); | ||
|
||
private final List<String> userBases; | ||
private final List<String> groupBases; | ||
|
||
public PatternFilter(List<String> userBases, List<String> groupBases) { | ||
this.userBases = userBases; | ||
this.groupBases = groupBases; | ||
} | ||
|
||
@Override | ||
public void apply(DirSearch client, String user) throws AuthenticationException { | ||
LOG.info("Authenticating user '{}' using {}", user, | ||
PatternFilter.class.getSimpleName()); | ||
try { | ||
String userDn = client.findUserDn(user); | ||
String baseDn = LdapUtils.extractBaseDn(userDn); | ||
if (!userBases.contains(baseDn)) { | ||
LOG.debug("{} does not contain user {} base DN {}", | ||
String.join(":", userBases), userDn, baseDn); | ||
List<String> userGroups = client.findGroupsForUser(userDn).stream() | ||
.map(LdapUtils::extractBaseDn) | ||
.collect(Collectors.toList()); | ||
Optional<String> intersectWithBaseGroupsCount = userGroups.stream() | ||
.filter(groupBases::contains) | ||
.findAny(); | ||
if (!intersectWithBaseGroupsCount.isPresent()) { | ||
LOG.debug("{} is not a member of any group {}", userDn, String.join(":", groupBases)); | ||
throw new AuthenticationException( | ||
String.format("Authentication failed: User %s with baseDn %s not a member of specified lists: %s ; %s", | ||
userDn, baseDn, String.join(":", userBases), String.join(":", groupBases))); | ||
} | ||
} | ||
LOG.info("Authentication succeeded based on user-group pattern membership"); | ||
} catch (Exception e) { | ||
throw new AuthenticationException(e.getMessage()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
service/src/test/org/apache/hive/service/auth/ldap/TestPatternFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
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.runners.MockitoJUnitRunner; | ||
|
||
import javax.naming.NamingException; | ||
import javax.security.sasl.AuthenticationException; | ||
|
||
import java.util.Collections; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
import static org.mockito.Mockito.*; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class TestPatternFilter { | ||
|
||
private FilterFactory factory; | ||
private HiveConf conf; | ||
|
||
@Mock | ||
private DirSearch search; | ||
|
||
@Before | ||
public void setup() { | ||
conf = new HiveConf(); | ||
factory = new PatternFilterFactory(); | ||
} | ||
|
||
@Test | ||
public void testFactory() { | ||
conf.unset(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_USE_PATTERN_FILTER.varname); | ||
assertNull(factory.getInstance(conf)); | ||
|
||
conf.setBoolVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_USE_PATTERN_FILTER, false); | ||
assertNull(factory.getInstance(conf)); | ||
|
||
conf.setBoolVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_USE_PATTERN_FILTER, true); | ||
assertNotNull(factory.getInstance(conf)); | ||
} | ||
|
||
@Test | ||
public void testApplyPositive() throws AuthenticationException, NamingException { | ||
conf.setBoolVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_USE_PATTERN_FILTER, true); | ||
conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_USERDNPATTERN, | ||
"sAMAccountname=%s,OU=ldap_ou,DC=example,DC=com:sAMAccountname=%s,OU=hive_ou,DC=example,DC=com"); | ||
conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_GROUPDNPATTERN, | ||
"CN=%s,OU=ldap_ou,DC=example,DC=com:CN=%s,OU=hive_ou,DC=example,DC=com"); | ||
when(search.findUserDn(eq("User1"))) | ||
.thenReturn("CN=User1,OU=ldap_ou,DC=example,DC=com"); | ||
when(search.findUserDn(eq("User2"))) | ||
.thenReturn("CN=User2,OU=hive_ou,DC=example,DC=com"); | ||
|
||
when(search.findUserDn(eq("User3"))) | ||
.thenReturn("CN=User3,OU=hive_test_ou,DC=example,DC=com"); | ||
when(search.findGroupsForUser("CN=User3,OU=hive_test_ou,DC=example,DC=com")) | ||
.thenReturn(Collections.singletonList("CN=group1,OU=ldap_ou,DC=example,DC=com")); | ||
|
||
when(search.findUserDn(eq("User4"))) | ||
.thenReturn("CN=User4,OU=hive_test_ou,DC=example,DC=com"); | ||
when(search.findGroupsForUser("CN=User4,OU=hive_test_ou,DC=example,DC=com")) | ||
.thenReturn(Collections.singletonList("CN=group2,OU=hive_ou,DC=example,DC=com")); | ||
|
||
Filter filter = factory.getInstance(conf); | ||
filter.apply(search, "User1"); | ||
filter.apply(search, "User2"); | ||
filter.apply(search, "User3"); | ||
filter.apply(search, "User4"); | ||
} | ||
|
||
@Test(expected = AuthenticationException.class) | ||
public void testApplyNegative() throws AuthenticationException, NamingException { | ||
conf.setBoolVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_USE_PATTERN_FILTER, true); | ||
conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_USERDNPATTERN, | ||
"sAMAccountname=%s,OU=ldap_ou,DC=example,DC=com:sAMAccountname=%s,OU=hive_ou,DC=example,DC=com"); | ||
conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_GROUPDNPATTERN, | ||
"CN=%s,OU=ldap_ou,DC=example,DC=com:CN=%s,OU=hive_ou,DC=example,DC=com"); | ||
|
||
when(search.findUserDn(eq("User2"))) | ||
.thenReturn("CN=User2,OU=hive_test_ou,DC=example,DC=com"); | ||
Filter filter = factory.getInstance(conf); | ||
filter.apply(search, "User1"); | ||
filter.apply(search, "User2"); | ||
} | ||
} |