Skip to content

Commit 1c678de

Browse files
cwperkstandonks
authored andcommitted
Update Subject interface to use CheckedRunnable (opensearch-project#18570)
* Update Subject interface to use CheckedRunnable Signed-off-by: Craig Perkins <[email protected]> * Add to CHANGELOG Signed-off-by: Craig Perkins <[email protected]> * Update more instances Signed-off-by: Craig Perkins <[email protected]> * Mark with PublicApi Signed-off-by: Craig Perkins <[email protected]> * Update javadoc Signed-off-by: Craig Perkins <[email protected]> --------- Signed-off-by: Craig Perkins <[email protected]>
1 parent b45e719 commit 1c678de

File tree

6 files changed

+15
-14
lines changed

6 files changed

+15
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1010
- Introduce SecureHttpTransportParameters experimental API (to complement SecureTransportParameters counterpart) ([#18572](https://github.com/opensearch-project/OpenSearch/issues/18572))
1111

1212
### Changed
13+
- Update Subject interface to use CheckedRunnable ([#18570](https://github.com/opensearch-project/OpenSearch/issues/18570))
1314

1415
### Dependencies
1516
- Bump `stefanzweifel/git-auto-commit-action` from 5 to 6 ([#18524](https://github.com/opensearch-project/OpenSearch/pull/18524))

libs/common/src/main/java/org/opensearch/common/CheckedRunnable.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@
3232

3333
package org.opensearch.common;
3434

35+
import org.opensearch.common.annotation.PublicApi;
36+
3537
/**
3638
* A {@link Runnable}-like interface which allows throwing checked exceptions.
3739
*
3840
* @opensearch.api
3941
*/
42+
@PublicApi(since = "3.2.0")
4043
@FunctionalInterface
4144
public interface CheckedRunnable<E extends Exception> {
4245
void run() throws E;

plugins/identity-shiro/src/main/java/org/opensearch/identity/shiro/ShiroPluginSubject.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
package org.opensearch.identity.shiro;
1010

11+
import org.opensearch.common.CheckedRunnable;
1112
import org.opensearch.common.annotation.ExperimentalApi;
1213
import org.opensearch.common.util.concurrent.ThreadContext;
1314
import org.opensearch.identity.NamedPrincipal;
1415
import org.opensearch.identity.PluginSubject;
1516
import org.opensearch.threadpool.ThreadPool;
1617

1718
import java.security.Principal;
18-
import java.util.concurrent.Callable;
1919

2020
/**
2121
* Implementation of subject that is always authenticated
@@ -41,9 +41,9 @@ public Principal getPrincipal() {
4141
}
4242

4343
@Override
44-
public <T> T runAs(Callable<T> callable) throws Exception {
44+
public <E extends Exception> void runAs(CheckedRunnable<E> r) throws E {
4545
try (ThreadContext.StoredContext ctx = threadPool.getThreadContext().stashContext()) {
46-
return callable.call();
46+
r.run();
4747
}
4848
}
4949
}

server/src/main/java/org/opensearch/identity/Subject.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
package org.opensearch.identity;
77

8+
import org.opensearch.common.CheckedRunnable;
89
import org.opensearch.common.annotation.ExperimentalApi;
910

1011
import java.security.Principal;
11-
import java.util.concurrent.Callable;
1212

1313
/**
1414
* An individual, process, or device that causes information to flow among objects or change to the system state.
@@ -24,9 +24,9 @@ public interface Subject {
2424
Principal getPrincipal();
2525

2626
/**
27-
* runAs allows the caller to run a callable function as this subject
27+
* runAs allows the caller to run a {@link CheckedRunnable} as this subject
2828
*/
29-
default <T> T runAs(Callable<T> callable) throws Exception {
30-
return callable.call();
29+
default <E extends Exception> void runAs(CheckedRunnable<E> r) throws E {
30+
r.run();
3131
};
3232
}

server/src/main/java/org/opensearch/identity/noop/NoopPluginSubject.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
package org.opensearch.identity.noop;
1010

11+
import org.opensearch.common.CheckedRunnable;
1112
import org.opensearch.common.annotation.InternalApi;
1213
import org.opensearch.common.util.concurrent.ThreadContext;
1314
import org.opensearch.identity.NamedPrincipal;
1415
import org.opensearch.identity.PluginSubject;
1516
import org.opensearch.threadpool.ThreadPool;
1617

1718
import java.security.Principal;
18-
import java.util.concurrent.Callable;
1919

2020
/**
2121
* Implementation of subject that is always authenticated
@@ -41,9 +41,9 @@ public Principal getPrincipal() {
4141
}
4242

4343
@Override
44-
public <T> T runAs(Callable<T> callable) throws Exception {
44+
public <E extends Exception> void runAs(CheckedRunnable<E> r) throws E {
4545
try (ThreadContext.StoredContext ctx = threadPool.getThreadContext().stashContext()) {
46-
return callable.call();
46+
r.run();
4747
}
4848
}
4949
}

server/src/test/java/org/opensearch/identity/noop/NoopPluginSubjectTests.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ public void testInitializeIdentityAwarePlugin() throws Exception {
4848
assertThat(testPluginSubject.getPrincipal().getName(), equalTo(NamedPrincipal.UNAUTHENTICATED.getName()));
4949
threadPool.getThreadContext().putHeader("test_header", "foo");
5050
assertThat(threadPool.getThreadContext().getHeader("test_header"), equalTo("foo"));
51-
testPluginSubject.runAs(() -> {
52-
assertNull(threadPool.getThreadContext().getHeader("test_header"));
53-
return null;
54-
});
51+
testPluginSubject.runAs(() -> { assertNull(threadPool.getThreadContext().getHeader("test_header")); });
5552
assertThat(threadPool.getThreadContext().getHeader("test_header"), equalTo("foo"));
5653
terminate(threadPool);
5754
}

0 commit comments

Comments
 (0)