Testing: Create rule for starting/stopping ApacheDS
This ensures, that resources are cleaned up appropriately. Original commit: elastic/x-pack-elasticsearch@d881562a3e
This commit is contained in:
parent
954ea51ef3
commit
f15d5c4aa3
|
@ -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.junit.rules.MethodRule;
|
||||||
|
import org.junit.runners.model.FrameworkMethod;
|
||||||
|
import org.junit.runners.model.Statement;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class ApacheDsRule implements MethodRule {
|
||||||
|
|
||||||
|
private ApacheDsEmbedded ldap;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
|
||||||
|
|
||||||
|
return new Statement() {
|
||||||
|
@Override
|
||||||
|
public void evaluate() throws Throwable {
|
||||||
|
try {
|
||||||
|
ldap = new ApacheDsEmbedded("o=sevenSeas", "seven-seas.ldif", target.getClass().getName());
|
||||||
|
ldap.startServer();
|
||||||
|
base.evaluate();
|
||||||
|
} finally {
|
||||||
|
ldap.stopAndCleanup();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUrl() {
|
||||||
|
return ldap.getUrl();
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,8 +8,7 @@ package org.elasticsearch.shield.authc.ldap;
|
||||||
import org.elasticsearch.common.settings.ImmutableSettings;
|
import org.elasticsearch.common.settings.ImmutableSettings;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.test.ElasticsearchTestCase;
|
import org.elasticsearch.test.ElasticsearchTestCase;
|
||||||
import org.junit.AfterClass;
|
import org.junit.Rule;
|
||||||
import org.junit.BeforeClass;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -20,20 +19,12 @@ import static org.hamcrest.Matchers.*;
|
||||||
public class LdapConnectionTests extends ElasticsearchTestCase {
|
public class LdapConnectionTests extends ElasticsearchTestCase {
|
||||||
public static String SETTINGS_PREFIX = LdapRealm.class.getPackage().getName().substring("com.elasticsearch.".length()) + '.';
|
public static String SETTINGS_PREFIX = LdapRealm.class.getPackage().getName().substring("com.elasticsearch.".length()) + '.';
|
||||||
|
|
||||||
static ApacheDsEmbedded ldap = new ApacheDsEmbedded("o=sevenSeas", "seven-seas.ldif", LdapConnectionTests.class.getName());
|
@Rule
|
||||||
|
public static ApacheDsRule apacheDsRule = new ApacheDsRule();
|
||||||
@BeforeClass
|
|
||||||
public static void startServer() throws Exception {
|
|
||||||
ldap.startServer();
|
|
||||||
}
|
|
||||||
@AfterClass
|
|
||||||
public static void stopServer() throws Exception {
|
|
||||||
ldap.stopAndCleanup();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBindWithTemplates() {
|
public void testBindWithTemplates() {
|
||||||
String[] ldapUrls = new String[]{ldap.getUrl()};
|
String[] ldapUrls = new String[]{apacheDsRule.getUrl()};
|
||||||
String groupSearchBase = "o=sevenSeas";
|
String groupSearchBase = "o=sevenSeas";
|
||||||
boolean isSubTreeSearch = true;
|
boolean isSubTreeSearch = true;
|
||||||
String[] userTemplates = new String[]{
|
String[] userTemplates = new String[]{
|
||||||
|
@ -55,7 +46,7 @@ public class LdapConnectionTests extends ElasticsearchTestCase {
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
public void testBindWithBogusTemplates() {
|
public void testBindWithBogusTemplates() {
|
||||||
String[] ldapUrl = new String[]{ldap.getUrl()};
|
String[] ldapUrl = new String[]{apacheDsRule.getUrl()};
|
||||||
String groupSearchBase = "o=sevenSeas";
|
String groupSearchBase = "o=sevenSeas";
|
||||||
boolean isSubTreeSearch = true;
|
boolean isSubTreeSearch = true;
|
||||||
String[] userTemplates = new String[]{
|
String[] userTemplates = new String[]{
|
||||||
|
@ -85,7 +76,7 @@ public class LdapConnectionTests extends ElasticsearchTestCase {
|
||||||
|
|
||||||
boolean isSubTreeSearch = true;
|
boolean isSubTreeSearch = true;
|
||||||
StandardLdapConnectionFactory ldapFac = new StandardLdapConnectionFactory(
|
StandardLdapConnectionFactory ldapFac = new StandardLdapConnectionFactory(
|
||||||
buildLdapSettings(ldap.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch));
|
buildLdapSettings(apacheDsRule.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch));
|
||||||
|
|
||||||
String user = "Horatio Hornblower";
|
String user = "Horatio Hornblower";
|
||||||
char[] userPass = "pass".toCharArray();
|
char[] userPass = "pass".toCharArray();
|
||||||
|
@ -102,7 +93,7 @@ public class LdapConnectionTests extends ElasticsearchTestCase {
|
||||||
String userTemplate = "cn={0},ou=people,o=sevenSeas";
|
String userTemplate = "cn={0},ou=people,o=sevenSeas";
|
||||||
boolean isSubTreeSearch = false;
|
boolean isSubTreeSearch = false;
|
||||||
StandardLdapConnectionFactory ldapFac = new StandardLdapConnectionFactory(
|
StandardLdapConnectionFactory ldapFac = new StandardLdapConnectionFactory(
|
||||||
buildLdapSettings(ldap.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch));
|
buildLdapSettings(apacheDsRule.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch));
|
||||||
|
|
||||||
String user = "Horatio Hornblower";
|
String user = "Horatio Hornblower";
|
||||||
LdapConnection ldap = ldapFac.bind(user, "pass".toCharArray());
|
LdapConnection ldap = ldapFac.bind(user, "pass".toCharArray());
|
||||||
|
|
|
@ -13,9 +13,8 @@ import org.elasticsearch.shield.authc.support.UsernamePasswordToken;
|
||||||
import org.elasticsearch.test.ElasticsearchTestCase;
|
import org.elasticsearch.test.ElasticsearchTestCase;
|
||||||
import org.elasticsearch.threadpool.ThreadPool;
|
import org.elasticsearch.threadpool.ThreadPool;
|
||||||
import org.elasticsearch.watcher.ResourceWatcherService;
|
import org.elasticsearch.watcher.ResourceWatcherService;
|
||||||
import org.junit.AfterClass;
|
|
||||||
import org.junit.BeforeClass;
|
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.*;
|
import static org.hamcrest.Matchers.*;
|
||||||
|
@ -24,7 +23,7 @@ import static org.mockito.Matchers.anyString;
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
public class LdapRealmTest extends ElasticsearchTestCase {
|
public class LdapRealmTest extends ElasticsearchTestCase {
|
||||||
static ApacheDsEmbedded ldap = new ApacheDsEmbedded("o=sevenSeas", "seven-seas.ldif", LdapRealmTest.class.getName());
|
|
||||||
public static String AD_IP = "54.213.145.20";
|
public static String AD_IP = "54.213.145.20";
|
||||||
public static String AD_URL = "ldap://" + AD_IP + ":389";
|
public static String AD_URL = "ldap://" + AD_IP + ":389";
|
||||||
|
|
||||||
|
@ -32,21 +31,15 @@ public class LdapRealmTest extends ElasticsearchTestCase {
|
||||||
public static final String VALID_USERNAME = "Thomas Masterman Hardy";
|
public static final String VALID_USERNAME = "Thomas Masterman Hardy";
|
||||||
public static final String PASSWORD = "pass";
|
public static final String PASSWORD = "pass";
|
||||||
|
|
||||||
@BeforeClass
|
@Rule
|
||||||
public static void startServer() throws Exception {
|
public static ApacheDsRule apacheDsRule = new ApacheDsRule();
|
||||||
ldap.startServer();
|
|
||||||
}
|
|
||||||
@AfterClass
|
|
||||||
public static void stopServer() throws Exception {
|
|
||||||
ldap.stopAndCleanup();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAuthenticate_subTreeGroupSearch(){
|
public void testAuthenticate_subTreeGroupSearch(){
|
||||||
String groupSearchBase = "o=sevenSeas";
|
String groupSearchBase = "o=sevenSeas";
|
||||||
boolean isSubTreeSearch = true;
|
boolean isSubTreeSearch = true;
|
||||||
String userTemplate = VALID_USER_TEMPLATE;
|
String userTemplate = VALID_USER_TEMPLATE;
|
||||||
Settings settings = LdapConnectionTests.buildLdapSettings(ldap.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch);
|
Settings settings = LdapConnectionTests.buildLdapSettings(apacheDsRule.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch);
|
||||||
StandardLdapConnectionFactory ldapFactory = new StandardLdapConnectionFactory(settings);
|
StandardLdapConnectionFactory ldapFactory = new StandardLdapConnectionFactory(settings);
|
||||||
LdapRealm ldap = new LdapRealm(buildNonCachingSettings(), ldapFactory, buildGroupAsRoleMapper());
|
LdapRealm ldap = new LdapRealm(buildNonCachingSettings(), ldapFactory, buildGroupAsRoleMapper());
|
||||||
|
|
||||||
|
@ -61,7 +54,7 @@ public class LdapRealmTest extends ElasticsearchTestCase {
|
||||||
boolean isSubTreeSearch = false;
|
boolean isSubTreeSearch = false;
|
||||||
String userTemplate = VALID_USER_TEMPLATE;
|
String userTemplate = VALID_USER_TEMPLATE;
|
||||||
StandardLdapConnectionFactory ldapFactory = new StandardLdapConnectionFactory(
|
StandardLdapConnectionFactory ldapFactory = new StandardLdapConnectionFactory(
|
||||||
LdapConnectionTests.buildLdapSettings(ldap.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch));
|
LdapConnectionTests.buildLdapSettings(apacheDsRule.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch));
|
||||||
|
|
||||||
LdapRealm ldap = new LdapRealm(buildNonCachingSettings(), ldapFactory, buildGroupAsRoleMapper());
|
LdapRealm ldap = new LdapRealm(buildNonCachingSettings(), ldapFactory, buildGroupAsRoleMapper());
|
||||||
|
|
||||||
|
@ -77,7 +70,7 @@ public class LdapRealmTest extends ElasticsearchTestCase {
|
||||||
boolean isSubTreeSearch = true;
|
boolean isSubTreeSearch = true;
|
||||||
String userTemplate = VALID_USER_TEMPLATE;
|
String userTemplate = VALID_USER_TEMPLATE;
|
||||||
StandardLdapConnectionFactory ldapFactory = new StandardLdapConnectionFactory(
|
StandardLdapConnectionFactory ldapFactory = new StandardLdapConnectionFactory(
|
||||||
LdapConnectionTests.buildLdapSettings( ldap.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch) );
|
LdapConnectionTests.buildLdapSettings( apacheDsRule.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch) );
|
||||||
|
|
||||||
ldapFactory = spy(ldapFactory);
|
ldapFactory = spy(ldapFactory);
|
||||||
LdapRealm ldap = new LdapRealm( buildCachingSettings(), ldapFactory, buildGroupAsRoleMapper());
|
LdapRealm ldap = new LdapRealm( buildCachingSettings(), ldapFactory, buildGroupAsRoleMapper());
|
||||||
|
@ -94,7 +87,7 @@ public class LdapRealmTest extends ElasticsearchTestCase {
|
||||||
boolean isSubTreeSearch = true;
|
boolean isSubTreeSearch = true;
|
||||||
String userTemplate = VALID_USER_TEMPLATE;
|
String userTemplate = VALID_USER_TEMPLATE;
|
||||||
StandardLdapConnectionFactory ldapFactory = new StandardLdapConnectionFactory(
|
StandardLdapConnectionFactory ldapFactory = new StandardLdapConnectionFactory(
|
||||||
LdapConnectionTests.buildLdapSettings(ldap.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch) );
|
LdapConnectionTests.buildLdapSettings(apacheDsRule.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch) );
|
||||||
|
|
||||||
ldapFactory = spy(ldapFactory);
|
ldapFactory = spy(ldapFactory);
|
||||||
LdapRealm ldap = new LdapRealm( buildNonCachingSettings(), ldapFactory, buildGroupAsRoleMapper());
|
LdapRealm ldap = new LdapRealm( buildNonCachingSettings(), ldapFactory, buildGroupAsRoleMapper());
|
||||||
|
|
Loading…
Reference in New Issue