-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Description
Is your feature request related to a problem? Please describe
As a plugin developer, its necessary to differentiate between performing transport actions within 1) an authenticated user context and 2) outside of an authenticated user context.
When using the OpenSearch Security plugin, plugin developers use the threadPool.getThreadContext().stashContext() method to accomplish this as the security plugin uses headers in the ThreadContext to determine whether a transport action is being executed within the context of an authenticated user or not.
stashContext allows a plugin to execute code in a fresh threadContext and the original context is restored upon closure.
stashContext is a low-level API that requires a plugin developer to have specific knowledge about the implementation of security. Ideally, there is a higher-level API that plugin developers can use to "switch contexts".
Note: Switching contexts is necessary for plugins that have system indices. Stashing the context is required before system index interaction.
Describe the solution you'd like
I am thinking about introducing a new parameter that is passed into plugin.createComponents. The new parameter would be an instance of an interface like ContextSwitcher:
public interface ContextSwitcher {
ThreadContext.StoredContext switchContext();
}
There are 2 types of ContextSwitchers:
- SystemContextSwitcher -> when calling switch context is will by default call markAsSystemContext (currently a public method of ThreadContext). This would be an
@Internalclass and can only be used in this repo. Not by plugins. - PluginContextSwitcher -> When stashing the context, this will also populate a special header (
_plugin_execution_context) with the canonical class name of the class thatextends Plugin. Canonical class name is chosen because its unique to every plugin and should match the key to this map.- This special header will have special protections to prevent it from being written to from higher-level ThreadContext APIs. A notion of FORBIDDEN_HEADERS would be introduced into the ThreadContext class which will prevent populating these headers directly.
Along with the ContextSwitcher, the ThreadContext class will be revisited to determine what level of access each method should have. For instance, the markAsSystemContext() method is currently public which means its accessible to plugins. I propose making markAsSystemContext and all stashContext methods to be package-private.
Related component
Plugins
Describe alternatives you've considered
Modify the NodeClient that is passed to plugins and expose select methods.
Additional context
stashContext nullifies all request + transient headers except for transient headers that have a registered propagator.