Add tests for realm construction
Original commit: elastic/x-pack-elasticsearch@a35c103726
This commit is contained in:
parent
9df9957307
commit
525562e48f
|
@ -5,29 +5,71 @@
|
|||
*/
|
||||
package org.elasticsearch.xpack.security;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.env.Environment;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.junit.Before;
|
||||
import org.elasticsearch.threadpool.ThreadPool;
|
||||
import org.elasticsearch.xpack.extensions.XPackExtension;
|
||||
import org.elasticsearch.xpack.security.authc.Realm;
|
||||
import org.elasticsearch.xpack.security.authc.Realms;
|
||||
import org.elasticsearch.xpack.security.authc.file.FileRealm;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
public class SecurityTests extends ESTestCase {
|
||||
|
||||
private Environment env;
|
||||
public static class DummyExtension extends XPackExtension {
|
||||
private String realmType;
|
||||
DummyExtension(String realmType) {
|
||||
this.realmType = realmType;
|
||||
}
|
||||
@Override
|
||||
public String name() {
|
||||
return "dummy";
|
||||
}
|
||||
@Override
|
||||
public String description() {
|
||||
return "dummy";
|
||||
}
|
||||
@Override
|
||||
public Map<String, Realm.Factory> getRealms() {
|
||||
return Collections.singletonMap(realmType, config -> null);
|
||||
}
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setupEnv() {
|
||||
Settings settings = Settings.builder()
|
||||
private Collection<Object> createComponents(Settings testSettings, XPackExtension... extensions) throws IOException {
|
||||
Settings settings = Settings.builder().put(testSettings)
|
||||
.put("path.home", createTempDir()).build();
|
||||
env = new Environment(settings);
|
||||
Environment env = new Environment(settings);
|
||||
Security security = new Security(settings, env);
|
||||
ThreadPool threadPool = mock(ThreadPool.class);
|
||||
return security.createComponents(null, threadPool, null, Arrays.asList(extensions));
|
||||
}
|
||||
|
||||
private <T> T findComponent(Class<T> type, Collection<Object> components) {
|
||||
for (Object obj : components) {
|
||||
if (type.isInstance(obj)) {
|
||||
return type.cast(obj);
|
||||
}
|
||||
}
|
||||
throw new AssertionError("Could not find component of type " + type + " in components");
|
||||
}
|
||||
|
||||
public void testCustomRealmExtension() throws Exception {
|
||||
Security security = new Security(Settings.EMPTY, env);
|
||||
|
||||
//security.createComponents(null, null, null, )
|
||||
Collection<Object> components = createComponents(Settings.EMPTY, new DummyExtension("myrealm"));
|
||||
Realms realms = findComponent(Realms.class, components);
|
||||
assertNotNull(realms.realmFactory("myrealm"));
|
||||
}
|
||||
|
||||
public void testCustomRealmExtensionConflict() throws Exception {
|
||||
|
||||
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
|
||||
() -> createComponents(Settings.EMPTY, new DummyExtension(FileRealm.TYPE)));
|
||||
assertEquals("Realm type [" + FileRealm.TYPE + "] is already registered", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue