[TEST] introduce base class for ldap tests that starts apache ds up and cleans it up afterwards
ApacheDSRule has been moved to `ExternalResource` which requires less code and implements `TestRule` instead of `MethodRule`. `TestRule` supports `ClassRule`s as well as ordinary `Rule`s. A class rule is exactly what we need for the ldap tests since we want to start the ldap server once before class and shut it down after all tests (after class). Also made sure that the static fields are cleaned up, otherwise `StaticFieldsInvariantRule` barfs. Added `extends ElasticsearchTestsCase` where missing also. Closes elastic/elasticsearch#80 Original commit: elastic/x-pack-elasticsearch@2143a2dcc6
This commit is contained in:
parent
723725753a
commit
f1b0c88bd4
|
@ -21,8 +21,6 @@ public class ActiveDirectoryFactoryTests extends ElasticsearchTestCase {
|
|||
public static final String AD_LDAP_URL = "ldap://54.213.145.20:389";
|
||||
public static final String PASSWORD = "4joD8LmWcrEfRa&p";
|
||||
|
||||
public static String SETTINGS_PREFIX = LdapRealm.class.getPackage().getName().substring("com.elasticsearch.".length()) + '.';
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void testAdAuth() {
|
||||
|
@ -47,7 +45,7 @@ public class ActiveDirectoryFactoryTests extends ElasticsearchTestCase {
|
|||
String userTemplate = "cn={0},cn=Users,dc=ad,dc=test,dc=elasticsearch,dc=com";
|
||||
boolean isSubTreeSearch = true;
|
||||
StandardLdapConnectionFactory connectionFactory = new StandardLdapConnectionFactory(
|
||||
LdapConnectionTests.buildLdapSettings(AD_LDAP_URL, userTemplate, groupSearchBase, isSubTreeSearch));
|
||||
LdapTest.buildLdapSettings(AD_LDAP_URL, userTemplate, groupSearchBase, isSubTreeSearch));
|
||||
|
||||
String user = "Tony Stark";
|
||||
LdapConnection ldap = connectionFactory.bind(user, PASSWORD.toCharArray());
|
||||
|
@ -62,8 +60,8 @@ public class ActiveDirectoryFactoryTests extends ElasticsearchTestCase {
|
|||
|
||||
public static Settings buildAdSettings(String ldapUrl, String adDomainName) {
|
||||
return ImmutableSettings.builder()
|
||||
.putArray(SETTINGS_PREFIX + ActiveDirectoryConnectionFactory.URLS_SETTING, ldapUrl)
|
||||
.put(SETTINGS_PREFIX + ActiveDirectoryConnectionFactory.AD_DOMAIN_NAME_SETTING, adDomainName)
|
||||
.putArray(LdapTest.SETTINGS_PREFIX + ActiveDirectoryConnectionFactory.URLS_SETTING, ldapUrl)
|
||||
.put(LdapTest.SETTINGS_PREFIX + ActiveDirectoryConnectionFactory.AD_DOMAIN_NAME_SETTING, adDomainName)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,15 +5,17 @@
|
|||
*/
|
||||
package org.elasticsearch.shield.authc.ldap;
|
||||
|
||||
import org.junit.rules.MethodRule;
|
||||
import org.elasticsearch.common.logging.ESLogger;
|
||||
import org.elasticsearch.common.logging.Loggers;
|
||||
import org.junit.rules.ExternalResource;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.runners.model.FrameworkMethod;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class ApacheDsRule implements MethodRule {
|
||||
public class ApacheDsRule extends ExternalResource {
|
||||
|
||||
private final ESLogger logger = Loggers.getLogger(getClass());
|
||||
|
||||
private ApacheDsEmbedded ldap;
|
||||
private final TemporaryFolder temporaryFolder;
|
||||
|
@ -23,20 +25,18 @@ public class ApacheDsRule implements MethodRule {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
|
||||
protected void before() throws Throwable {
|
||||
ldap = new ApacheDsEmbedded("o=sevenSeas", "seven-seas.ldif", temporaryFolder.newFolder());
|
||||
ldap.startServer();
|
||||
}
|
||||
|
||||
return new Statement() {
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
try {
|
||||
ldap = new ApacheDsEmbedded("o=sevenSeas", "seven-seas.ldif", temporaryFolder.newFolder());
|
||||
ldap.startServer();
|
||||
base.evaluate();
|
||||
} finally {
|
||||
ldap.stopAndCleanup();
|
||||
}
|
||||
}
|
||||
};
|
||||
@Override
|
||||
protected void after() {
|
||||
try {
|
||||
ldap.stopAndCleanup();
|
||||
} catch (Exception e) {
|
||||
logger.error("failed to stop and cleanup the embedded ldap server", e);
|
||||
}
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
|
|
|
@ -5,33 +5,14 @@
|
|||
*/
|
||||
package org.elasticsearch.shield.authc.ldap;
|
||||
|
||||
import org.elasticsearch.common.settings.ImmutableSettings;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.test.ElasticsearchTestCase;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
public class LdapConnectionTests extends ElasticsearchTestCase {
|
||||
public static String SETTINGS_PREFIX = LdapRealm.class.getPackage().getName().substring("com.elasticsearch.".length()) + '.';
|
||||
|
||||
@Rule
|
||||
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@Rule
|
||||
public static ApacheDsRule apacheDsRule = new ApacheDsRule(temporaryFolder);
|
||||
|
||||
@AfterClass
|
||||
public static void cleanup() {
|
||||
temporaryFolder = null;
|
||||
apacheDsRule = null;
|
||||
}
|
||||
public class LdapConnectionTests extends LdapTest {
|
||||
|
||||
@Test
|
||||
public void testBindWithTemplates() {
|
||||
|
@ -55,7 +36,8 @@ public class LdapConnectionTests extends ElasticsearchTestCase {
|
|||
assertThat(attrs, hasKey("uid"));
|
||||
assertThat( attrs.get("uid"), arrayContaining("hhornblo"));
|
||||
}
|
||||
@Test
|
||||
|
||||
@Test(expected = LdapException.class)
|
||||
public void testBindWithBogusTemplates() {
|
||||
String[] ldapUrl = new String[]{apacheDsRule.getUrl()};
|
||||
String groupSearchBase = "o=sevenSeas";
|
||||
|
@ -70,14 +52,7 @@ public class LdapConnectionTests extends ElasticsearchTestCase {
|
|||
|
||||
String user = "Horatio Hornblower";
|
||||
char[] userPass = "pass".toCharArray();
|
||||
|
||||
try {
|
||||
LdapConnection ldap = ldapFac.bind(user, userPass);
|
||||
fail("bindWithUserTemplates should have failed");
|
||||
} catch (LdapException le) {
|
||||
|
||||
}
|
||||
|
||||
ldapFac.bind(user, userPass);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -113,17 +88,4 @@ public class LdapConnectionTests extends ElasticsearchTestCase {
|
|||
System.out.println("groups:"+groups);
|
||||
assertThat(groups, contains("cn=HMS Lydia,ou=crews,ou=groups,o=sevenSeas"));
|
||||
}
|
||||
|
||||
public static Settings buildLdapSettings(String ldapUrl, String userTemplate, String groupSearchBase, boolean isSubTreeSearch) {
|
||||
return buildLdapSettings( new String[]{ldapUrl}, new String[]{userTemplate}, groupSearchBase, isSubTreeSearch );
|
||||
}
|
||||
|
||||
public static Settings buildLdapSettings(String[] ldapUrl, String[] userTemplate, String groupSearchBase, boolean isSubTreeSearch) {
|
||||
return ImmutableSettings.builder()
|
||||
.putArray(SETTINGS_PREFIX + StandardLdapConnectionFactory.URLS_SETTING, ldapUrl)
|
||||
.putArray(SETTINGS_PREFIX + StandardLdapConnectionFactory.USER_DN_TEMPLATES_SETTING, userTemplate)
|
||||
.put(SETTINGS_PREFIX + StandardLdapConnectionFactory.GROUP_SEARCH_BASEDN_SETTING, groupSearchBase)
|
||||
.put(SETTINGS_PREFIX + StandardLdapConnectionFactory.GROUP_SEARCH_SUBTREE_SETTING, isSubTreeSearch).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -11,21 +11,21 @@ import org.elasticsearch.env.Environment;
|
|||
import org.elasticsearch.rest.RestController;
|
||||
import org.elasticsearch.shield.User;
|
||||
import org.elasticsearch.shield.authc.support.UsernamePasswordToken;
|
||||
import org.elasticsearch.test.ElasticsearchTestCase;
|
||||
import org.elasticsearch.threadpool.ThreadPool;
|
||||
import org.elasticsearch.watcher.ResourceWatcherService;
|
||||
import org.junit.*;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class LdapRealmTest extends ElasticsearchTestCase {
|
||||
public class LdapRealmTest extends LdapTest {
|
||||
|
||||
public static String AD_IP = "54.213.145.20";
|
||||
public static String AD_URL = "ldap://" + AD_IP + ":389";
|
||||
public static final String AD_IP = "54.213.145.20";
|
||||
public static final String AD_URL = "ldap://" + AD_IP + ":389";
|
||||
|
||||
public static final String VALID_USER_TEMPLATE = "cn={0},ou=people,o=sevenSeas";
|
||||
public static final String VALID_USERNAME = "Thomas Masterman Hardy";
|
||||
|
@ -38,18 +38,6 @@ public class LdapRealmTest extends ElasticsearchTestCase {
|
|||
restController = mock(RestController.class);
|
||||
}
|
||||
|
||||
@Rule
|
||||
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@Rule
|
||||
public static ApacheDsRule apacheDsRule = new ApacheDsRule(temporaryFolder);
|
||||
|
||||
@AfterClass
|
||||
public static void cleanup() {
|
||||
temporaryFolder = null;
|
||||
apacheDsRule = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRestHeaderRegistration() {
|
||||
new LdapRealm(ImmutableSettings.EMPTY, mock(LdapConnectionFactory.class), mock(LdapGroupToRoleMapper.class), restController);
|
||||
|
@ -143,7 +131,7 @@ public class LdapRealmTest extends ElasticsearchTestCase {
|
|||
//only set the adDomain, and see if it infers the rest correctly
|
||||
String adDomain = AD_IP;
|
||||
Settings settings = ImmutableSettings.builder()
|
||||
.put(LdapConnectionTests.SETTINGS_PREFIX + ActiveDirectoryConnectionFactory.AD_DOMAIN_NAME_SETTING, adDomain)
|
||||
.put(SETTINGS_PREFIX + ActiveDirectoryConnectionFactory.AD_DOMAIN_NAME_SETTING, adDomain)
|
||||
.build();
|
||||
|
||||
ActiveDirectoryConnectionFactory ldapFactory = new ActiveDirectoryConnectionFactory( settings );
|
||||
|
@ -177,6 +165,5 @@ public class LdapRealmTest extends ElasticsearchTestCase {
|
|||
return new LdapGroupToRoleMapper(settings,
|
||||
new Environment(settings),
|
||||
new ResourceWatcherService(settings, new ThreadPool("test")));
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.shield.authc.ldap;
|
||||
|
||||
import org.elasticsearch.common.settings.ImmutableSettings;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.test.ElasticsearchTestCase;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.rules.RuleChain;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
@Ignore
|
||||
public abstract class LdapTest extends ElasticsearchTestCase {
|
||||
|
||||
static String SETTINGS_PREFIX = LdapRealm.class.getPackage().getName().substring("com.elasticsearch.".length()) + '.';
|
||||
|
||||
private static final TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
protected static final ApacheDsRule apacheDsRule = new ApacheDsRule(temporaryFolder);
|
||||
|
||||
@ClassRule
|
||||
public static final RuleChain ruleChain = RuleChain.outerRule(temporaryFolder).around(apacheDsRule);
|
||||
|
||||
static Settings buildLdapSettings(String ldapUrl, String userTemplate, String groupSearchBase, boolean isSubTreeSearch) {
|
||||
return buildLdapSettings( new String[]{ldapUrl}, new String[]{userTemplate}, groupSearchBase, isSubTreeSearch );
|
||||
}
|
||||
|
||||
static Settings buildLdapSettings(String[] ldapUrl, String[] userTemplate, String groupSearchBase, boolean isSubTreeSearch) {
|
||||
return ImmutableSettings.builder()
|
||||
.putArray(SETTINGS_PREFIX + StandardLdapConnectionFactory.URLS_SETTING, ldapUrl)
|
||||
.putArray(SETTINGS_PREFIX + StandardLdapConnectionFactory.USER_DN_TEMPLATES_SETTING, userTemplate)
|
||||
.put(SETTINGS_PREFIX + StandardLdapConnectionFactory.GROUP_SEARCH_BASEDN_SETTING, groupSearchBase)
|
||||
.put(SETTINGS_PREFIX + StandardLdapConnectionFactory.GROUP_SEARCH_SUBTREE_SETTING, isSubTreeSearch).build();
|
||||
}
|
||||
}
|
|
@ -7,15 +7,16 @@ package org.elasticsearch.shield.authc.support;
|
|||
|
||||
import org.elasticsearch.common.settings.ImmutableSettings;
|
||||
import org.elasticsearch.shield.User;
|
||||
import org.elasticsearch.test.ElasticsearchTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class CachingUsernamePasswordRealmTests {
|
||||
public static class AlwaysAuthenticateCachingRealm extends CachingUsernamePasswordRealm {
|
||||
public class CachingUsernamePasswordRealmTests extends ElasticsearchTestCase {
|
||||
|
||||
static class AlwaysAuthenticateCachingRealm extends CachingUsernamePasswordRealm {
|
||||
public AlwaysAuthenticateCachingRealm() {
|
||||
super(ImmutableSettings.EMPTY);
|
||||
}
|
||||
|
@ -28,7 +29,6 @@ public class CachingUsernamePasswordRealmTests {
|
|||
@Override public String type() { return "test"; }
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testCache(){
|
||||
AlwaysAuthenticateCachingRealm realm = new AlwaysAuthenticateCachingRealm();
|
||||
|
|
|
@ -5,14 +5,13 @@
|
|||
*/
|
||||
package org.elasticsearch.shield.authc.support;
|
||||
|
||||
import org.elasticsearch.test.ElasticsearchTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class HasherTests {
|
||||
public class HasherTests extends ElasticsearchTestCase {
|
||||
|
||||
@Test
|
||||
public void testHtpasswdToolGenerated() throws Exception {
|
||||
|
|
Loading…
Reference in New Issue