Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Jan 14, 2025
1 parent b53e726 commit 5415ab3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
package org.opensearch.bootstrap;

import org.opensearch.OpenSearchException;
import org.opensearch.client.Client;
import org.opensearch.common.settings.Settings;
import org.opensearch.identity.IdentityService;
import org.opensearch.identity.noop.NoopIdentityPlugin;
Expand All @@ -22,12 +21,10 @@

import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.mock;

public class IdentityPluginTests extends OpenSearchTestCase {

public void testSingleIdentityPluginSucceeds() {
Client client = mock(Client.class);
TestThreadPool threadPool = new TestThreadPool(getTestName());
IdentityPlugin identityPlugin1 = new NoopIdentityPlugin(threadPool);
List<IdentityPlugin> pluginList1 = List.of(identityPlugin1);
Expand All @@ -38,7 +35,6 @@ public void testSingleIdentityPluginSucceeds() {
}

public void testMultipleIdentityPluginsFail() {
Client client = mock(Client.class);
TestThreadPool threadPool = new TestThreadPool(getTestName());
IdentityPlugin identityPlugin1 = new NoopIdentityPlugin(threadPool);
IdentityPlugin identityPlugin2 = new NoopIdentityPlugin(threadPool);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.identity.noop;

import org.opensearch.common.settings.Settings;
import org.opensearch.identity.IdentityService;
import org.opensearch.identity.NamedPrincipal;
import org.opensearch.identity.PluginSubject;
import org.opensearch.plugins.IdentityAwarePlugin;
import org.opensearch.plugins.Plugin;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.threadpool.TestThreadPool;
import org.opensearch.threadpool.ThreadPool;

import java.util.List;

import static org.hamcrest.Matchers.equalTo;

public class NoopPluginSubjectTests extends OpenSearchTestCase {
public static class TestPlugin extends Plugin implements IdentityAwarePlugin {
private PluginSubject subject;

@Override
public void assignSubject(PluginSubject subject) {
this.subject = subject;
}

public PluginSubject getSubject() {
return subject;
}
}

public void testInitializeIdentityAwarePlugin() throws Exception {
ThreadPool threadPool = new TestThreadPool(getTestName());
IdentityService identityService = new IdentityService(Settings.EMPTY, threadPool, List.of());

TestPlugin testPlugin = new TestPlugin();
identityService.initializeIdentityAwarePlugins(List.of(testPlugin));

PluginSubject testPluginSubject = new NoopPluginSubject(threadPool);
assertThat(testPlugin.getSubject().getPrincipal().getName(), equalTo(NamedPrincipal.UNAUTHENTICATED.getName()));
assertThat(testPluginSubject.getPrincipal().getName(), equalTo(NamedPrincipal.UNAUTHENTICATED.getName()));
threadPool.getThreadContext().putHeader("test_header", "foo");
assertThat(threadPool.getThreadContext().getHeader("test_header"), equalTo("foo"));
testPluginSubject.runAs(() -> {
assertNull(threadPool.getThreadContext().getHeader("test_header"));
return null;
});
assertThat(threadPool.getThreadContext().getHeader("test_header"), equalTo("foo"));
terminate(threadPool);
}
}

0 comments on commit 5415ab3

Please sign in to comment.