Class AuthorizedTasks

java.lang.Object
org.gcube.common.security.AuthorizedTasks

public class AuthorizedTasks extends Object
A utility class for managing and executing tasks with specific authorization contexts. This class provides static methods to bind Callable and Runnable tasks to a specific Secret context. It ensures that the correct secret is set before the task runs and is properly reset afterward, preventing secrets from leaking into unintended scopes.
  • Constructor Details

    • AuthorizedTasks

      public AuthorizedTasks()
  • Method Details

    • bind

      public static <V> Callable<V> bind(Callable<V> task)
      Binds a Callable task to the current authorization context. This method captures the current secret from SecretManagerProvider and returns a new Callable that will automatically set that secret before executing the original task and reset it afterward. This is useful for delegating tasks to a different thread or for asynchronous execution while maintaining the correct authorization context.
      Type Parameters:
      V - The return type of the task.
      Parameters:
      task - The original Callable task to be bound.
      Returns:
      An equivalent Callable task bound to the current authorization context.
    • bind

      public static Runnable bind(Runnable task)
      Binds a Runnable task to the current authorization context. Similar to the bind(Callable) method, this captures the current secret and returns a new Runnable that will set the secret before execution and reset it upon completion.
      Parameters:
      task - The original Runnable task to be bound.
      Returns:
      An equivalent Runnable task bound to the current authorization context.
    • executeSafely

      public static void executeSafely(Runnable task, Secret secret)
      Executes a Runnable task immediately within a specific secret context. This method temporarily sets a specified secret, executes the task, and then restores the previous secret, guaranteeing that the original secret is always put back in place, even if the task throws an exception.
      Parameters:
      task - The Runnable task to be executed.
      secret - The Secret that must be used during the execution of the task.
    • executeSafely

      public static <T> T executeSafely(Callable<T> task, Secret secret) throws Throwable
      Executes a Callable task immediately within a specific secret context. Similar to the executeSafely(Runnable) method, this sets a specified secret, executes the Callable task, and restores the previous secret. It also handles propagating any exceptions thrown by the task.
      Type Parameters:
      T - The return type of the task.
      Parameters:
      task - The Callable task to be executed.
      secret - The Secret that must be used during the execution of the task.
      Returns:
      The result of the Callable task.
      Throws:
      Throwable - if the task execution throws an exception.