use of com.tremolosecurity.proxy.dynamicconfiguration.LoadJavaScriptMappingFromK8s in project OpenUnison by TremoloSecurity.
the class JavaScriptMapping method doMapping.
@Override
public Attribute doMapping(User user, String attrname) {
String jsmapping = null;
LoadJavaScriptMappingFromK8s fromk8sns;
synchronized (fromk8s) {
fromk8sns = fromk8s.get(this.key);
if (fromk8sns == null) {
fromk8sns = new LoadJavaScriptMappingFromK8s();
try {
fromk8sns.loadJavaScriptMappings(GlobalEntries.getGlobalEntries().getConfigManager(), target, namespace);
} catch (ProvisioningException e) {
logger.warn("Could not create watch on " + target + "." + namespace, e);
return new Attribute(attrname);
}
fromk8s.put(key, fromk8sns);
}
}
String js = fromk8sns.getMapping(this.name);
if (js == null) {
logger.warn("JavaScriptMapping " + target + "." + namespace + "." + name + " does not exist");
return new Attribute(attrname);
}
Context context = Context.newBuilder("js").allowAllAccess(true).build();
try {
Value initicalCtx = context.eval("js", js);
Value doMapping = context.getBindings("js").getMember("doMapping");
if (doMapping == null) {
throw new ProvisioningException(target + "." + namespace + "." + name + " doMapping function does not exist");
}
if (!doMapping.canExecute()) {
throw new ProvisioningException(target + "." + namespace + "." + name + " doMapping is not a function");
}
Value finishedMapping = doMapping.execute(user, attrname);
return finishedMapping.as(Attribute.class);
} catch (Throwable t) {
logger.warn("Could not execute " + target + "." + namespace + "." + name, t);
return new Attribute(attrname);
} finally {
context.close();
}
}
Aggregations