use of eu.bcvsolutions.idm.core.security.domain.GroovySandboxFilter in project CzechIdMng by bcvsolutions.
the class DefaultGroovyScriptService method evaluate.
@Override
public Object evaluate(String script, Map<String, Object> variables, List<Class<?>> extraAllowedClasses) {
Assert.notNull(script);
Binding binding = new Binding(variables);
Set<Class<?>> allowedVariableClass = resolveCustomAllowTypes(variables);
if (extraAllowedClasses != null) {
allowedVariableClass.addAll(extraAllowedClasses);
}
GroovySandboxFilter sandboxFilter = null;
//
try {
// if groovy filter exist add extraAllowedClasses, into this filter, otherwise create new
if (!GroovyInterceptor.getApplicableInterceptors().isEmpty()) {
// exists only one goovy filter
sandboxFilter = (GroovySandboxFilter) GroovyInterceptor.getApplicableInterceptors().get(0);
sandboxFilter.addCustomTypes(allowedVariableClass);
} else {
sandboxFilter = new GroovySandboxFilter(allowedVariableClass);
sandboxFilter.register();
}
// Get script and fill it with variables
Script scriptObj = scriptCache.getScript(script);
// Scripts aren't thread safe
synchronized (scriptObj) {
scriptObj.setBinding(binding);
return scriptObj.run();
}
} catch (SecurityException | IdmSecurityException ex) {
LOG.error("SecurityException [{}]", ex.getLocalizedMessage());
if (ex instanceof IdmSecurityException) {
throw ex;
}
throw new IdmSecurityException(CoreResultCode.GROOVY_SCRIPT_SECURITY_VALIDATION, ImmutableMap.of("message", ex.getLocalizedMessage()), ex);
} catch (Exception e) {
LOG.error("Exception [{}]", e.getLocalizedMessage());
if (e instanceof ResultCodeException) {
throw e;
}
throw new ResultCodeException(CoreResultCode.GROOVY_SCRIPT_EXCEPTION, ImmutableMap.of("message", e.getLocalizedMessage() != null ? e.getLocalizedMessage() : e.toString()), e);
} finally {
// otherwise unregister all filter.
if (sandboxFilter != null) {
if (sandboxFilter.isCustomTypesLast()) {
sandboxFilter.unregister();
} else {
sandboxFilter.removeLastCustomTypes();
}
}
}
}
Aggregations