Search in sources :

Example 1 with LoadJavaScriptMappingFromK8s

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();
    }
}
Also used : Context(org.graalvm.polyglot.Context) Attribute(com.tremolosecurity.saml.Attribute) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) Value(org.graalvm.polyglot.Value) LoadJavaScriptMappingFromK8s(com.tremolosecurity.proxy.dynamicconfiguration.LoadJavaScriptMappingFromK8s)

Aggregations

ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)1 LoadJavaScriptMappingFromK8s (com.tremolosecurity.proxy.dynamicconfiguration.LoadJavaScriptMappingFromK8s)1 Attribute (com.tremolosecurity.saml.Attribute)1 Context (org.graalvm.polyglot.Context)1 Value (org.graalvm.polyglot.Value)1