use of org.apache.metron.stellar.dsl.ParseException in project metron by apache.
the class StellarServiceImpl method validateRules.
@Override
public Map<String, Boolean> validateRules(List<String> rules) {
Map<String, Boolean> results = new HashMap<>();
StellarProcessor stellarProcessor = new StellarProcessor();
for (String rule : rules) {
try {
boolean result = stellarProcessor.validate(rule, Context.EMPTY_CONTEXT());
results.put(rule, result);
} catch (ParseException e) {
results.put(rule, false);
}
}
return results;
}
use of org.apache.metron.stellar.dsl.ParseException in project metron by apache.
the class DefaultProfileBuilder method execute.
/**
* Executes the expressions contained within the profile definition.
*
* @param expressions A list of expressions to execute.
* @param transientState Additional transient state provided to the expressions.
* @param expressionType The type of expression; init, update, result. Provides additional context if expression execution fails.
* @return The result of executing each expression.
*/
private List<Object> execute(List<String> expressions, Map<String, Object> transientState, String expressionType) {
List<Object> results = new ArrayList<>();
for (String expr : ListUtils.emptyIfNull(expressions)) {
try {
// execute an expression
Object result = executor.execute(expr, transientState, Object.class);
results.add(result);
} catch (Throwable e) {
// in-scope variables = persistent state maintained by the profiler + the transient state
Set<String> variablesInScope = new HashSet<>();
variablesInScope.addAll(transientState.keySet());
variablesInScope.addAll(executor.getState().keySet());
String msg = format("Bad '%s' expression: error='%s', expr='%s', profile='%s', entity='%s', variables-available='%s'", expressionType, e.getMessage(), expr, profileName, entity, variablesInScope);
LOG.error(msg, e);
throw new ParseException(msg, e);
}
}
return results;
}
use of org.apache.metron.stellar.dsl.ParseException in project metron by apache.
the class BasicStellarTest method testLogicalFunctions.
@Test
public void testLogicalFunctions() throws Exception {
final Map<String, String> variableMap = new HashMap<String, String>() {
{
put("foo", "casey");
put("ip", "192.168.0.1");
put("ip_src_addr", "192.168.0.1");
put("ip_dst_addr", "10.0.0.1");
put("other_ip", "10.168.0.1");
put("empty", "");
put("spaced", "metron is great");
}
};
Assert.assertTrue(runPredicate("IN_SUBNET(ip, '192.168.0.0/24')", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertTrue(runPredicate("IN_SUBNET(ip, '192.168.0.0/24', '11.0.0.0/24')", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertTrue(runPredicate("IN_SUBNET(ip, '192.168.0.0/24', '11.0.0.0/24') in [true]", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertTrue(runPredicate("true in IN_SUBNET(ip, '192.168.0.0/24', '11.0.0.0/24')", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertFalse(runPredicate("IN_SUBNET(ip_dst_addr, '192.168.0.0/24', '11.0.0.0/24')", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertFalse(runPredicate("IN_SUBNET(other_ip, '192.168.0.0/24')", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
boolean thrown = false;
try {
runPredicate("IN_SUBNET(blah, '192.168.0.0/24')", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v)));
} catch (ParseException pe) {
thrown = true;
}
Assert.assertTrue(thrown);
Assert.assertTrue(runPredicate("true and STARTS_WITH(foo, 'ca')", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertTrue(runPredicate("true and STARTS_WITH(TO_UPPER(foo), 'CA')", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertTrue(runPredicate("(true and STARTS_WITH(TO_UPPER(foo), 'CA')) || true", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertTrue(runPredicate("true and ENDS_WITH(foo, 'sey')", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertTrue(runPredicate("not(IN_SUBNET(ip_src_addr, '192.168.0.0/24') and IN_SUBNET(ip_dst_addr, '192.168.0.0/24'))", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertTrue(runPredicate("IN_SUBNET(ip_src_addr, '192.168.0.0/24')", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertFalse(runPredicate("not(IN_SUBNET(ip_src_addr, '192.168.0.0/24'))", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertFalse(runPredicate("IN_SUBNET(ip_dst_addr, '192.168.0.0/24')", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertTrue(runPredicate("not(IN_SUBNET(ip_dst_addr, '192.168.0.0/24'))", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
}
use of org.apache.metron.stellar.dsl.ParseException in project metron by apache.
the class StringFunctionsTest method testLeftRightFills.
@Test
public void testLeftRightFills() throws Exception {
final Map<String, Object> variableMap = new HashMap<String, Object>() {
{
put("foo", null);
put("bar", null);
put("notInt", "oh my");
}
};
// LEFT
Object left = run("FILL_LEFT('123','X', 10)", new HashedMap());
Assert.assertNotNull(left);
Assert.assertEquals(10, ((String) left).length());
Assert.assertEquals("XXXXXXX123", (String) left);
// RIGHT
Object right = run("FILL_RIGHT('123','X', 10)", new HashedMap());
Assert.assertNotNull(right);
Assert.assertEquals(10, ((String) right).length());
Assert.assertEquals("123XXXXXXX", (String) right);
// INPUT ALREADY LENGTH
Object same = run("FILL_RIGHT('123','X', 3)", new HashedMap());
Assert.assertEquals(3, ((String) same).length());
Assert.assertEquals("123", (String) same);
// INPUT BIGGER THAN LENGTH
Object tooBig = run("FILL_RIGHT('1234567890','X', 3)", new HashedMap());
Assert.assertEquals(10, ((String) tooBig).length());
Assert.assertEquals("1234567890", (String) tooBig);
// NULL VARIABLES
boolean thrown = false;
try {
run("FILL_RIGHT('123',foo,bar)", variableMap);
} catch (ParseException pe) {
thrown = true;
Assert.assertTrue(pe.getMessage().contains("are both required"));
}
Assert.assertTrue(thrown);
thrown = false;
// NULL LENGTH
try {
run("FILL_RIGHT('123','X',bar)", variableMap);
} catch (ParseException pe) {
thrown = true;
Assert.assertTrue(pe.getMessage().contains("are both required"));
}
Assert.assertTrue(thrown);
thrown = false;
// NULL FILL
try {
run("FILL_RIGHT('123',foo, 7)", variableMap);
} catch (ParseException pe) {
thrown = true;
Assert.assertTrue(pe.getMessage().contains("are both required"));
}
Assert.assertTrue(thrown);
thrown = false;
// NON INTEGER LENGTH
try {
run("FILL_RIGHT('123','X', 'z' )", new HashedMap());
} catch (ParseException pe) {
thrown = true;
Assert.assertTrue(pe.getMessage().contains("not a valid Integer"));
}
Assert.assertTrue(thrown);
thrown = false;
// EMPTY STRING PAD
try {
Object returnValue = run("FILL_RIGHT('123','', 10 )", new HashedMap());
} catch (ParseException pe) {
thrown = true;
Assert.assertTrue(pe.getMessage().contains("cannot be an empty"));
}
Assert.assertTrue(thrown);
thrown = false;
// MISSING LENGTH PARAMETER
try {
run("FILL_RIGHT('123',foo)", variableMap);
} catch (ParseException pe) {
thrown = true;
Assert.assertTrue(pe.getMessage().contains("expects three"));
}
Assert.assertTrue(thrown);
}
use of org.apache.metron.stellar.dsl.ParseException in project metron by apache.
the class StringFunctionsTest method testChomp.
/**
* CHOMP StringFunction
*
* @throws Exception
*/
@Test
public void testChomp() throws Exception {
Assert.assertEquals("abc", run("CHOMP('abc')", new HashedMap()));
Assert.assertEquals("abc", run("CHOMP(msg)", ImmutableMap.of("msg", "abc\r\n")));
Assert.assertEquals("", run("CHOMP(msg)", ImmutableMap.of("msg", "\n")));
Assert.assertEquals("", run("CHOMP('')", new HashedMap()));
Assert.assertEquals(null, run("CHOMP(null)", new HashedMap()));
// No input
boolean thrown = false;
try {
run("CHOMP()", Collections.emptyMap());
} catch (ParseException pe) {
thrown = true;
Assert.assertTrue(pe.getMessage().contains("missing argument"));
}
Assert.assertTrue(thrown);
thrown = false;
// Variable missing
try {
run("CHOMP(msg)", new HashedMap());
} catch (ParseException pe) {
thrown = true;
}
thrown = false;
// Integer input
try {
run("CHOMP(123)", Collections.emptyMap());
} catch (ParseException pe) {
thrown = true;
Assert.assertTrue(pe.getMessage().contains("cannot be cast"));
}
Assert.assertTrue(thrown);
}
Aggregations