Search in sources :

Example 1 with Whitelist

use of org.opensearch.painless.spi.Whitelist in project OpenSearch by opensearch-project.

the class ExampleWhitelistExtension method getContextWhitelists.

@Override
public Map<ScriptContext<?>, List<Whitelist>> getContextWhitelists() {
    Map<String, WhitelistAnnotationParser> parsers = new HashMap<>(WhitelistAnnotationParser.BASE_ANNOTATION_PARSERS);
    parsers.put(ExamplePainlessAnnotation.NAME, ExampleWhitelistAnnotationParser.INSTANCE);
    Whitelist classAllowlist = WhitelistLoader.loadFromResourceFiles(ExampleWhitelistExtension.class, parsers, "example_whitelist.txt");
    ExampleWhitelistedInstance eai = new ExampleWhitelistedInstance(1);
    WhitelistInstanceBinding addValue = new WhitelistInstanceBinding("example addValue", eai, "addValue", "int", Collections.singletonList("int"), Collections.emptyList());
    WhitelistInstanceBinding getValue = new WhitelistInstanceBinding("example getValue", eai, "getValue", "int", Collections.emptyList(), Collections.emptyList());
    Whitelist instanceAllowlist = new Whitelist(eai.getClass().getClassLoader(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Arrays.asList(addValue, getValue));
    return Collections.singletonMap(FieldScript.CONTEXT, Arrays.asList(classAllowlist, instanceAllowlist));
}
Also used : HashMap(java.util.HashMap) WhitelistAnnotationParser(org.opensearch.painless.spi.annotation.WhitelistAnnotationParser) Whitelist(org.opensearch.painless.spi.Whitelist) WhitelistInstanceBinding(org.opensearch.painless.spi.WhitelistInstanceBinding)

Example 2 with Whitelist

use of org.opensearch.painless.spi.Whitelist in project OpenSearch by opensearch-project.

the class PainlessLookupBuilder method buildFromWhitelists.

public static PainlessLookup buildFromWhitelists(List<Whitelist> allowlists) {
    PainlessLookupBuilder painlessLookupBuilder = new PainlessLookupBuilder();
    String origin = "internal error";
    try {
        for (Whitelist allowlist : allowlists) {
            for (WhitelistClass allowlistClass : allowlist.whitelistClasses) {
                origin = allowlistClass.origin;
                painlessLookupBuilder.addPainlessClass(allowlist.classLoader, allowlistClass.javaClassName, allowlistClass.painlessAnnotations.containsKey(NoImportAnnotation.class) == false);
            }
        }
        for (Whitelist allowlist : allowlists) {
            for (WhitelistClass allowlistClass : allowlist.whitelistClasses) {
                String targetCanonicalClassName = allowlistClass.javaClassName.replace('$', '.');
                for (WhitelistConstructor allowlistConstructor : allowlistClass.whitelistConstructors) {
                    origin = allowlistConstructor.origin;
                    painlessLookupBuilder.addPainlessConstructor(targetCanonicalClassName, allowlistConstructor.canonicalTypeNameParameters, allowlistConstructor.painlessAnnotations);
                }
                for (WhitelistMethod allowlistMethod : allowlistClass.whitelistMethods) {
                    origin = allowlistMethod.origin;
                    painlessLookupBuilder.addPainlessMethod(allowlist.classLoader, targetCanonicalClassName, allowlistMethod.augmentedCanonicalClassName, allowlistMethod.methodName, allowlistMethod.returnCanonicalTypeName, allowlistMethod.canonicalTypeNameParameters, allowlistMethod.painlessAnnotations);
                }
                for (WhitelistField allowlistField : allowlistClass.whitelistFields) {
                    origin = allowlistField.origin;
                    painlessLookupBuilder.addPainlessField(targetCanonicalClassName, allowlistField.fieldName, allowlistField.canonicalTypeNameParameter);
                }
            }
            for (WhitelistMethod allowlistStatic : allowlist.whitelistImportedMethods) {
                origin = allowlistStatic.origin;
                painlessLookupBuilder.addImportedPainlessMethod(allowlist.classLoader, allowlistStatic.augmentedCanonicalClassName, allowlistStatic.methodName, allowlistStatic.returnCanonicalTypeName, allowlistStatic.canonicalTypeNameParameters, allowlistStatic.painlessAnnotations);
            }
            for (WhitelistClassBinding allowlistClassBinding : allowlist.whitelistClassBindings) {
                origin = allowlistClassBinding.origin;
                painlessLookupBuilder.addPainlessClassBinding(allowlist.classLoader, allowlistClassBinding.targetJavaClassName, allowlistClassBinding.methodName, allowlistClassBinding.returnCanonicalTypeName, allowlistClassBinding.canonicalTypeNameParameters, allowlistClassBinding.painlessAnnotations);
            }
            for (WhitelistInstanceBinding allowlistInstanceBinding : allowlist.whitelistInstanceBindings) {
                origin = allowlistInstanceBinding.origin;
                painlessLookupBuilder.addPainlessInstanceBinding(allowlistInstanceBinding.targetInstance, allowlistInstanceBinding.methodName, allowlistInstanceBinding.returnCanonicalTypeName, allowlistInstanceBinding.canonicalTypeNameParameters);
            }
        }
    } catch (Exception exception) {
        throw new IllegalArgumentException("error loading allowlist(s) " + origin, exception);
    }
    return painlessLookupBuilder.build();
}
Also used : WhitelistConstructor(org.opensearch.painless.spi.WhitelistConstructor) WhitelistField(org.opensearch.painless.spi.WhitelistField) WhitelistClassBinding(org.opensearch.painless.spi.WhitelistClassBinding) Whitelist(org.opensearch.painless.spi.Whitelist) WhitelistInstanceBinding(org.opensearch.painless.spi.WhitelistInstanceBinding) WhitelistClass(org.opensearch.painless.spi.WhitelistClass) WhitelistMethod(org.opensearch.painless.spi.WhitelistMethod) MalformedURLException(java.net.MalformedURLException)

Example 3 with Whitelist

use of org.opensearch.painless.spi.Whitelist in project OpenSearch by opensearch-project.

the class BindingsTests method beforeClass.

@BeforeClass
public static void beforeClass() {
    Map<ScriptContext<?>, List<Whitelist>> contexts = newDefaultContexts();
    List<Whitelist> allowlists = new ArrayList<>(Whitelist.BASE_WHITELISTS);
    allowlists.add(WhitelistLoader.loadFromResourceFiles(Whitelist.class, "org.opensearch.painless.test"));
    InstanceBindingTestClass instanceBindingTestClass = new InstanceBindingTestClass(1);
    WhitelistInstanceBinding getter = new WhitelistInstanceBinding("test", instanceBindingTestClass, "setInstanceBindingValue", "void", Collections.singletonList("int"), Collections.emptyList());
    WhitelistInstanceBinding setter = new WhitelistInstanceBinding("test", instanceBindingTestClass, "getInstanceBindingValue", "int", Collections.emptyList(), Collections.emptyList());
    List<WhitelistInstanceBinding> instanceBindingsList = new ArrayList<>();
    instanceBindingsList.add(getter);
    instanceBindingsList.add(setter);
    Whitelist instanceBindingsAllowlist = new Whitelist(instanceBindingTestClass.getClass().getClassLoader(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), instanceBindingsList);
    allowlists.add(instanceBindingsAllowlist);
    contexts.put(BindingsTestScript.CONTEXT, allowlists);
    SCRIPT_ENGINE = new PainlessScriptEngine(Settings.EMPTY, contexts);
}
Also used : ArrayList(java.util.ArrayList) ScriptContext(org.opensearch.script.ScriptContext) Whitelist(org.opensearch.painless.spi.Whitelist) WhitelistInstanceBinding(org.opensearch.painless.spi.WhitelistInstanceBinding) List(java.util.List) ArrayList(java.util.ArrayList) BeforeClass(org.junit.BeforeClass)

Example 4 with Whitelist

use of org.opensearch.painless.spi.Whitelist in project OpenSearch by opensearch-project.

the class WhitelistLoaderTests method testAnnotations.

public void testAnnotations() {
    Map<String, WhitelistAnnotationParser> parsers = new HashMap<>(WhitelistAnnotationParser.BASE_ANNOTATION_PARSERS);
    parsers.put(AnnotationTestObject.TestAnnotation.NAME, AnnotationTestObject.TestAnnotationParser.INSTANCE);
    Whitelist allowlist = WhitelistLoader.loadFromResourceFiles(Whitelist.class, parsers, "org.opensearch.painless.annotation");
    assertEquals(1, allowlist.whitelistClasses.size());
    WhitelistClass allowlistClass = allowlist.whitelistClasses.get(0);
    assertNotNull(allowlistClass.painlessAnnotations.get(NoImportAnnotation.class));
    assertEquals(1, allowlistClass.painlessAnnotations.size());
    assertEquals(3, allowlistClass.whitelistMethods.size());
    int count = 0;
    for (WhitelistMethod allowlistMethod : allowlistClass.whitelistMethods) {
        if ("deprecatedMethod".equals(allowlistMethod.methodName)) {
            assertEquals("use another method", ((DeprecatedAnnotation) allowlistMethod.painlessAnnotations.get(DeprecatedAnnotation.class)).getMessage());
            assertEquals(1, allowlistMethod.painlessAnnotations.size());
            ++count;
        }
        if ("annotatedTestMethod".equals(allowlistMethod.methodName)) {
            AnnotationTestObject.TestAnnotation ta = ((AnnotationTestObject.TestAnnotation) allowlistMethod.painlessAnnotations.get(AnnotationTestObject.TestAnnotation.class));
            assertEquals("one", ta.getOne());
            assertEquals("two", ta.getTwo());
            assertEquals("three", ta.getThree());
            assertEquals(1, allowlistMethod.painlessAnnotations.size());
            ++count;
        }
        if ("annotatedMultipleMethod".equals(allowlistMethod.methodName)) {
            assertEquals("test", ((DeprecatedAnnotation) allowlistMethod.painlessAnnotations.get(DeprecatedAnnotation.class)).getMessage());
            AnnotationTestObject.TestAnnotation ta = ((AnnotationTestObject.TestAnnotation) allowlistMethod.painlessAnnotations.get(AnnotationTestObject.TestAnnotation.class));
            assertEquals("one", ta.getOne());
            assertEquals("two", ta.getTwo());
            assertEquals("three", ta.getThree());
            assertEquals(2, allowlistMethod.painlessAnnotations.size());
            ++count;
        }
    }
    assertEquals(3, count);
}
Also used : NoImportAnnotation(org.opensearch.painless.spi.annotation.NoImportAnnotation) HashMap(java.util.HashMap) WhitelistAnnotationParser(org.opensearch.painless.spi.annotation.WhitelistAnnotationParser) Whitelist(org.opensearch.painless.spi.Whitelist) WhitelistClass(org.opensearch.painless.spi.WhitelistClass) WhitelistMethod(org.opensearch.painless.spi.WhitelistMethod)

Aggregations

Whitelist (org.opensearch.painless.spi.Whitelist)4 WhitelistInstanceBinding (org.opensearch.painless.spi.WhitelistInstanceBinding)3 HashMap (java.util.HashMap)2 WhitelistClass (org.opensearch.painless.spi.WhitelistClass)2 WhitelistMethod (org.opensearch.painless.spi.WhitelistMethod)2 WhitelistAnnotationParser (org.opensearch.painless.spi.annotation.WhitelistAnnotationParser)2 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 BeforeClass (org.junit.BeforeClass)1 WhitelistClassBinding (org.opensearch.painless.spi.WhitelistClassBinding)1 WhitelistConstructor (org.opensearch.painless.spi.WhitelistConstructor)1 WhitelistField (org.opensearch.painless.spi.WhitelistField)1 NoImportAnnotation (org.opensearch.painless.spi.annotation.NoImportAnnotation)1 ScriptContext (org.opensearch.script.ScriptContext)1