Security Context propgation into Runnables
mez5001 opened this issue · comments
I was using porcupine to create an ExecutorService in my project. When I would send in my first round of Runnables into the executor everything worked as expected. I then tried added another round of Runnables into the executor and was running into permissions issues. I checked the logs and the second round of runnables were running as the original user. I had to use a ManagedExecutorService instead to get the SessionContext to properly propagate into the Runnables. I wrote a stackoverflow question that provides a more clear example: http://stackoverflow.com/questions/40002010/how-to-avoid-executorservice-from-overridding-security-principal-of-a-runnable
Are the ExecutorService and the generated threads supposed to be container manager or is that an invalid assumption that I made?
I think this is a flaw that prevents procupine from being used in alot of cases.
Citing ManagedThreadFactory Javadoc:
"The Runnable task ... will run with the application component context of the component instance that created (looked-up) this ManagedThreadFactory instance."
Porcupine seems to assume the context to be capured from the thread that invokes managedThreadFactory.newThread(runnable), which is not the case - the context is captured form the point at which the ManagedThreadFactory was looked up or injected. The same context is applied to each new thread created by the ManagedThreadFactory.
So this code, executed inside a thread from ManagedThreadFactory,
@Resource
private SessionContext sessionContext;
...
private String getLoginName() {
final Principal principal = this.sessionContext.getCallerPrincipal();
}
will always return the name of the user that first invoked the ManagedThreadFactory, even if currently another user uses the ManagedThreadFactory.
The only solution i know of is to use ManagedExecutorService as you already stated - but then you do not need porcupine, since these can not be configured during runtime.