Search in sources :

Example 1 with WhitelistAnnotationParser

use of org.opensearch.painless.spi.annotation.WhitelistAnnotationParser 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 WhitelistAnnotationParser

use of org.opensearch.painless.spi.annotation.WhitelistAnnotationParser 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)

Example 3 with WhitelistAnnotationParser

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

the class WhitelistLoader method parseAllowlistAnnotations.

private static List<Object> parseAllowlistAnnotations(Map<String, WhitelistAnnotationParser> parsers, String line) {
    List<Object> annotations;
    if ("".equals(line.replaceAll("\\s+", ""))) {
        annotations = Collections.emptyList();
    } else {
        line = line.trim();
        if (line.charAt(0) != '@') {
            throw new IllegalArgumentException("invalid annotation: expected at symbol [" + line + "]");
        }
        if (line.length() < 2) {
            throw new IllegalArgumentException("invalid annotation: expected name [" + line + "]");
        }
        String[] annotationStrings = line.substring(1).split("@");
        annotations = new ArrayList<>(annotationStrings.length);
        for (String annotationString : annotationStrings) {
            String name;
            Map<String, String> arguments;
            annotationString = annotationString.trim();
            int index = annotationString.indexOf('[');
            if (index == -1) {
                name = annotationString;
                arguments = Collections.emptyMap();
            } else {
                if (annotationString.charAt(annotationString.length() - 1) != ']') {
                    throw new IllegalArgumentException("invalid annotation: expected closing brace [" + line + "]");
                }
                name = annotationString.substring(0, index);
                arguments = new HashMap<>();
                String[] argumentsStrings = annotationString.substring(index + 1, annotationString.length() - 1).split(",");
                for (String argumentString : argumentsStrings) {
                    String[] argumentKeyValue = argumentString.split("=");
                    if (argumentKeyValue.length != 2) {
                        throw new IllegalArgumentException("invalid annotation: expected key=\"value\" [" + line + "]");
                    }
                    String argumentKey = argumentKeyValue[0].trim();
                    if (argumentKey.isEmpty()) {
                        throw new IllegalArgumentException("invalid annotation: expected key=\"value\" [" + line + "]");
                    }
                    String argumentValue = argumentKeyValue[1];
                    if (argumentValue.length() < 3 || argumentValue.charAt(0) != '"' || argumentValue.charAt(argumentValue.length() - 1) != '"') {
                        throw new IllegalArgumentException("invalid annotation: expected key=\"value\" [" + line + "]");
                    }
                    argumentValue = argumentValue.substring(1, argumentValue.length() - 1);
                    arguments.put(argumentKey, argumentValue);
                }
            }
            WhitelistAnnotationParser parser = parsers.get(name);
            if (parser == null) {
                throw new IllegalArgumentException("invalid annotation: parser not found for [" + name + "] [" + line + "]");
            }
            annotations.add(parser.parse(arguments));
        }
    }
    return annotations;
}
Also used : WhitelistAnnotationParser(org.opensearch.painless.spi.annotation.WhitelistAnnotationParser)

Aggregations

WhitelistAnnotationParser (org.opensearch.painless.spi.annotation.WhitelistAnnotationParser)3 HashMap (java.util.HashMap)2 Whitelist (org.opensearch.painless.spi.Whitelist)2 WhitelistClass (org.opensearch.painless.spi.WhitelistClass)1 WhitelistInstanceBinding (org.opensearch.painless.spi.WhitelistInstanceBinding)1 WhitelistMethod (org.opensearch.painless.spi.WhitelistMethod)1 NoImportAnnotation (org.opensearch.painless.spi.annotation.NoImportAnnotation)1