Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Ability to run Code Coverage with Gradle and produce the jacoco reports locally ([#18509](https://github.com/opensearch-project/OpenSearch/issues/18509))

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

### Dependencies
- Bump `stefanzweifel/git-auto-commit-action` from 5 to 6 ([#18524](https://github.com/opensearch-project/OpenSearch/pull/18524))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@

package org.opensearch.common;

import org.opensearch.common.annotation.PublicApi;

/**
* A {@link Runnable}-like interface which allows throwing checked exceptions.
*
* @opensearch.api
*/
@PublicApi(since = "3.2.0")
@FunctionalInterface
public interface CheckedRunnable<E extends Exception> {
void run() throws E;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

package org.opensearch.identity.shiro;

import org.opensearch.common.CheckedRunnable;
import org.opensearch.common.annotation.ExperimentalApi;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.identity.NamedPrincipal;
import org.opensearch.identity.PluginSubject;
import org.opensearch.threadpool.ThreadPool;

import java.security.Principal;
import java.util.concurrent.Callable;

/**
* Implementation of subject that is always authenticated
Expand All @@ -41,9 +41,9 @@
}

@Override
public <T> T runAs(Callable<T> callable) throws Exception {
public <E extends Exception> void runAs(CheckedRunnable<E> r) throws E {
try (ThreadContext.StoredContext ctx = threadPool.getThreadContext().stashContext()) {
return callable.call();
r.run();

Check warning on line 46 in plugins/identity-shiro/src/main/java/org/opensearch/identity/shiro/ShiroPluginSubject.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L46 was not covered by tests
}
}
}
8 changes: 4 additions & 4 deletions server/src/main/java/org/opensearch/identity/Subject.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

package org.opensearch.identity;

import org.opensearch.common.CheckedRunnable;
import org.opensearch.common.annotation.ExperimentalApi;

import java.security.Principal;
import java.util.concurrent.Callable;

/**
* An individual, process, or device that causes information to flow among objects or change to the system state.
Expand All @@ -24,9 +24,9 @@
Principal getPrincipal();

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

Check warning on line 30 in server/src/main/java/org/opensearch/identity/Subject.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L30 was not covered by tests
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

package org.opensearch.identity.noop;

import org.opensearch.common.CheckedRunnable;
import org.opensearch.common.annotation.InternalApi;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.identity.NamedPrincipal;
import org.opensearch.identity.PluginSubject;
import org.opensearch.threadpool.ThreadPool;

import java.security.Principal;
import java.util.concurrent.Callable;

/**
* Implementation of subject that is always authenticated
Expand All @@ -41,9 +41,9 @@ public Principal getPrincipal() {
}

@Override
public <T> T runAs(Callable<T> callable) throws Exception {
public <E extends Exception> void runAs(CheckedRunnable<E> r) throws E {
try (ThreadContext.StoredContext ctx = threadPool.getThreadContext().stashContext()) {
return callable.call();
r.run();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ public void testInitializeIdentityAwarePlugin() throws Exception {
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;
});
testPluginSubject.runAs(() -> { assertNull(threadPool.getThreadContext().getHeader("test_header")); });
assertThat(threadPool.getThreadContext().getHeader("test_header"), equalTo("foo"));
terminate(threadPool);
}
Expand Down
Loading