added SecurityException
This commit is contained in:
parent
ef54fb4c6b
commit
773a88346c
|
@ -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 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
|
* @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 {
|
static public void executeSafely(final Runnable task, final Secret secret){
|
||||||
|
Secret previousSecret = SecretManagerProvider.instance.get();
|
||||||
SafelyExecution se = new SafelyExecution(new Runnable() {
|
try {
|
||||||
|
SecretManagerProvider.instance.set(secret);
|
||||||
@Override
|
task.run();
|
||||||
public void run() {
|
} finally {
|
||||||
SecretManagerProvider.instance.set(secret);
|
SecretManagerProvider.instance.set(previousSecret);
|
||||||
|
}
|
||||||
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 private class SafelyExecution extends Thread{
|
|
||||||
|
/**
|
||||||
protected Throwable e;
|
* Execute a callable inline with set and reset of the SecretManagerProvider
|
||||||
|
* @param task the task
|
||||||
public SafelyExecution(Runnable target) {
|
* @param secret the secret that must be used in the function
|
||||||
super(target);
|
* @return an equivalent {@link Runnable} task bound to the current scope and user
|
||||||
}
|
*/
|
||||||
|
static public <T> T executeSafely(final Callable<T> task, final Secret secret) throws Throwable {
|
||||||
@Override
|
Secret previousSecret = SecretManagerProvider.instance.get();
|
||||||
public void run() {
|
try {
|
||||||
try {
|
SecretManagerProvider.instance.set(secret);
|
||||||
super.run();
|
return task.call();
|
||||||
}catch (Throwable t) {
|
} finally {
|
||||||
e = t;
|
SecretManagerProvider.instance.set(previousSecret);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue