Skip to content

Feat/user context executor - #3341

Open
ppatel9703 wants to merge 3 commits into
devfrom
feat/user-context-executor
Open

ppatel9703 wants to merge 3 commits into
devfrom
feat/user-context-executor

Conversation

@ppatel9703

@ppatel9703 ppatel9703 commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Description

Gives a project's custom reactor a sanctioned way to run work on other threads as the user who is signed in. Today it has none: the sandbox blocks Thread and the thread pools, the ThreadStore setters (setUser, setSessionId, setInsightId, ...) and InsightStore. A worker thread therefore has no user, and the engine access check refuses every engine or model call it makes ("Custom code runs only for the user signed in on the thread").

Found while moving AIMES onto the sandboxed build. Its review step ran model calls four at a time on a pool, and every call failed with NoClassDefFoundError: prerna/om/InsightStore. Apps that fan work out to threads cannot do it under the sandbox. This also covers the idea of looking an insight up by the current user. A lookup alone would not help, because the worker thread would still carry no user.

prerna.util.UserContextExecutor does the identity work on the platform's side. The reactor passes its own Insight, never a User, so there is nothing for it to forge.

Changes Made

  • New prerna.util.UserContextExecutor (AutoCloseable). create(insight, threads) runs on the reactor's thread and reads the signed-in user from ThreadStore. submit(Function<Insight, T>) returns a Future<T>. close() stops the executor.
  • Same-user check. create refuses with a SemossPixelException when no user is signed in on the thread, when the insight is missing, or when the insight's user is not the signed-in user. Users match if they are the same object or have the same login ids and types.
  • Each task runs as the caller. On a platform-created daemon thread it gets the caller's user, session, route, job, scheduler mode, local host details, project context and log context. The engine access check therefore sees the real signed-in user, exactly as if the reactor had made the call.
  • Each task gets its own child insight. It has the same user and is registered in InsightStore only while the task runs. An insight carries state that is not safe to share between threads. The child's folder is granted to the user when CHROOT_ENABLE is on, then revoked and deleted afterwards.
  • The caller's folder rule goes with the task. SandboxedJavaExecution gets one package-private getter, sandboxRootOfCurrentThread(), so file work on the worker stays inside the user's chroot.
  • Cleanup always happens. The thread state, log context, child insight, folder grant and temp folder are removed whether the task returns or throws. The calling thread is never changed.
  • Thread cap. The number of threads is the smaller of what the reactor asks for and the optional RDF_Map setting PROJECT_USER_THREADS_MAX (default 8). Idle threads end after 60 seconds if nobody closes the executor.
  • Logging follows the neighbouring files: {} placeholders, ids only, exception last. Warn for refusals and for tasks dropped on close, info when the cap trims a request, debug for lifecycle.
  • Docs. project_reactor_sandbox.md gets a "Running work on other threads" section with an example and a rule/test table. The Threads row and "What custom reactors use instead" are updated.
  • Tests. 21 new tests in UserContextExecutorUnitTests. SandboxProbe gets test helpers so a project class loaded through the sandbox can hand a task to the executor.

No existing block list changes. The class is not blocked, like EmailUtility and HttpHelperUtility. No RDF_Map file change is needed, because the setting is optional.

How to Test

Unit tests (new, plus the sandbox suites that share SandboxProbe and SandboxedJavaExecution):

mvn test -Dtest='UserContextExecutorUnitTests,SandboxEngineAccessUnitTests,SandboxBlockedClassesUnitTests,SandboxFilesUnitTests,SandboxReplacedCallsUnitTests'

Expected: all green. I ran them with the JUnit launcher on a hand-built classpath, not through Maven. Run them once with Maven before merge.

The tests that show the fix itself:

  • aCustomReactorsTaskSeesTheSignedInUserOnAnExecutorThread: a class loaded through the project sandbox hands a task to the executor, and the task sees the signed-in user.
  • theSameTaskOnAThreadMadeByHandSeesNoUser: the same task on a bare thread sees no user. This is the failure the executor fixes.
  • anInsightOfAnotherUserIsRefused and aCreatorWithNoSignedInUserIsRefused: the same-user check.

Manual, from a custom reactor (needs a project with a model engine the user can read):

  1. In a reactor's execute(), run the following. Use a model call that needs the user's permission, such as Utility.getModel(id).ask(...), inside taskInsight -> ...:
    try (UserContextExecutor executor = UserContextExecutor.create(this.insight, 4)) {
    	List<Future<String>> futures = new ArrayList<>();
    	for (String item : items) {
    		futures.add(executor.submit(taskInsight -> callModel(taskInsight, item)));
    	}
    	for (Future<String> future : futures) {
    		results.add(future.get());
    	}
    }
  2. Expected: the calls succeed and are checked against the signed-in user's permission. A user with no permission on the engine is still refused, so the helper does not widen access.
  3. Replace the executor with new Thread(...) or Executors.newFixedThreadPool. Expected: the class is blocked, as before.
  4. Set PROJECT_USER_THREADS_MAX=2 in RDF_Map and ask for 8 threads. Expected: 2 threads, plus an info log line about the cap.

Notes

  • Not covered by tests: the engine access check run against a live permission database from a worker thread. Mockito static mocks only apply to the thread that creates them, so on a worker they switch the check off. The tests therefore prove what the check reads, the thread's signed-in user inside project code. Worth one manual pass (step 2 above) before merge.
  • Cap scope: the cap is per executor, not per user. A per-user limit, or an admin on/off switch for the helper, would be a follow-up if you want an approval gate.
  • Closing: close() drops tasks that have not started and interrupts running ones, so callers read their results first. The docs say so.
  • Why not a plain InsightStore lookup: it would let project code reach other users' insights, and a worker would still have no user. Keeping the user comparison and the thread setup inside one platform class avoids both.
  • Unrelated working-tree files (RDF_Map.prop, project/platform__*) are not part of this PR.

@snyk-io

snyk-io Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@ppatel9703
ppatel9703 marked this pull request as ready for review November 19, 2026 19:25
@ppatel9703
ppatel9703 requested a review from a team as a code owner November 19, 2026 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant