From 773a88346c3b10af72cc2b411c4b3c9cac35eeee Mon Sep 17 00:00:00 2001 From: lucio Date: Wed, 15 Jun 2022 16:51:29 +0200 Subject: [PATCH] added SecurityException --- .../common/security/AuthorizedTasks.java | 71 ++++++++----------- .../security/exception/SecurityException.java | 36 ++++++++++ 2 files changed, 64 insertions(+), 43 deletions(-) create mode 100644 src/main/java/org/gcube/common/security/exception/SecurityException.java diff --git a/src/main/java/org/gcube/common/security/AuthorizedTasks.java b/src/main/java/org/gcube/common/security/AuthorizedTasks.java index 83d2523..7cea94a 100644 --- a/src/main/java/org/gcube/common/security/AuthorizedTasks.java +++ b/src/main/java/org/gcube/common/security/AuthorizedTasks.java @@ -67,53 +67,38 @@ public class AuthorizedTasks { } }; } - + /** - * Binds a {@link Runnable} task to the current scope and user. + * Execute a runnable inline with set and reset of the SecretManagerProvider * @param task the task + * @param secret the secret that must be used in the function * @return an equivalent {@link Runnable} task bound to the current scope and user */ - static public void executeSafely(final Runnable task, final Secret secret) throws Throwable { - - SafelyExecution se = new SafelyExecution(new Runnable() { - - @Override - public void run() { - SecretManagerProvider.instance.set(secret); - - try { - logger.info("setting on authorized task context {} ", secret.getContext()); - task.run(); - }finally { - SecretManagerProvider.instance.reset(); - } - - } - }); - - se.run(); - - if (se.e != null) throw se.e; - + static public void executeSafely(final Runnable task, final Secret secret){ + Secret previousSecret = SecretManagerProvider.instance.get(); + try { + SecretManagerProvider.instance.set(secret); + task.run(); + } finally { + SecretManagerProvider.instance.set(previousSecret); + } } - - static private class SafelyExecution extends Thread{ - - protected Throwable e; - - public SafelyExecution(Runnable target) { - super(target); - } - - @Override - public void run() { - try { - super.run(); - }catch (Throwable t) { - e = t; - } - } - + + + /** + * Execute a callable inline with set and reset of the SecretManagerProvider + * @param task the task + * @param secret the secret that must be used in the function + * @return an equivalent {@link Runnable} task bound to the current scope and user + */ + static public T executeSafely(final Callable task, final Secret secret) throws Throwable { + Secret previousSecret = SecretManagerProvider.instance.get(); + try { + SecretManagerProvider.instance.set(secret); + return task.call(); + } finally { + SecretManagerProvider.instance.set(previousSecret); + } } - + } diff --git a/src/main/java/org/gcube/common/security/exception/SecurityException.java b/src/main/java/org/gcube/common/security/exception/SecurityException.java new file mode 100644 index 0000000..d2d664f --- /dev/null +++ b/src/main/java/org/gcube/common/security/exception/SecurityException.java @@ -0,0 +1,36 @@ +package org.gcube.common.security.exception; + +public class SecurityException extends RuntimeException{ + + /** + * + */ + private static final long serialVersionUID = 1L; + + public SecurityException() { + super(); + // TODO Auto-generated constructor stub + } + + public SecurityException(String message, Throwable cause, boolean enableSuppression, + boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + // TODO Auto-generated constructor stub + } + + public SecurityException(String message, Throwable cause) { + super(message, cause); + // TODO Auto-generated constructor stub + } + + public SecurityException(String message) { + super(message); + // TODO Auto-generated constructor stub + } + + public SecurityException(Throwable cause) { + super(cause); + // TODO Auto-generated constructor stub + } + +}