use of org.apache.metron.stellar.dsl.Context in project metron by apache.
the class GetProfileTest method testMissingContext.
/**
* Initialization should fail if the required context values are missing.
*/
@Test(expected = IllegalStateException.class)
public void testMissingContext() {
Context empty = Context.EMPTY_CONTEXT();
// 'unset' the context that was created during setup()
executor.setContext(empty);
// force re-initialization with no context
SingletonFunctionResolver.getInstance().initialize(empty);
// validate - function should be unable to initialize
String expr = "PROFILE_GET('profile1', 'entity1', PROFILE_FIXED(1000, 'SECONDS'), groups)";
run(expr, List.class);
}
use of org.apache.metron.stellar.dsl.Context in project metron by apache.
the class StellarMaaSIntegrationTest method setup.
@BeforeClass
public static void setup() throws Exception {
UnitTestHelper.setJavaLoggingLevel(WebApplicationImpl.class, Level.WARNING);
MockDGAModel.start(8282);
testZkServer = new TestingServer(true);
zookeeperUrl = testZkServer.getConnectString();
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
client = CuratorFrameworkFactory.newClient(zookeeperUrl, retryPolicy);
client.start();
context = new Context.Builder().with(Context.Capabilities.ZOOKEEPER_CLIENT, () -> client).build();
MaaSConfig config = ConfigUtil.INSTANCE.read(client, "/metron/maas/config", new MaaSConfig(), MaaSConfig.class);
discoverer = new ServiceDiscoverer(client, config.getServiceRoot());
discoverer.start();
endpointUrl = new URL("http://localhost:8282");
ModelEndpoint endpoint = new ModelEndpoint();
{
endpoint.setName("dga");
endpoint.setContainerId("0");
Endpoint ep = new Endpoint();
ep.setUrl(endpointUrl.toString());
endpoint.setEndpoint(ep);
endpoint.setVersion("1.0");
}
;
ServiceInstanceBuilder<ModelEndpoint> builder = ServiceInstance.<ModelEndpoint>builder().address(endpointUrl.getHost()).id("0").name("dga").port(endpointUrl.getPort()).registrationTimeUTC(System.currentTimeMillis()).serviceType(ServiceType.STATIC).payload(endpoint);
final ServiceInstance<ModelEndpoint> instance = builder.build();
discoverer.getServiceDiscovery().registerService(instance);
// wait til the endpoint is installed...
for (int i = 0; i < 10; ++i) {
try {
Object o = discoverer.getEndpoint("dga");
if (o != null) {
break;
}
} catch (Exception e) {
}
Thread.sleep(1000);
}
}
use of org.apache.metron.stellar.dsl.Context in project metron by apache.
the class ThreatTriageProcessor method apply.
@Nullable
@Override
public ThreatScore apply(@Nullable Map input) {
ThreatScore threatScore = new ThreatScore();
StellarPredicateProcessor predicateProcessor = new StellarPredicateProcessor();
StellarProcessor processor = new StellarProcessor();
VariableResolver resolver = new MapVariableResolver(input, sensorConfig.getConfiguration(), threatIntelConfig.getConfig());
// attempt to apply each rule to the threat
for (RiskLevelRule rule : threatTriageConfig.getRiskLevelRules()) {
if (predicateProcessor.parse(rule.getRule(), resolver, functionResolver, context)) {
// add the rule's score to the overall threat score
String reason = execute(rule.getReason(), processor, resolver, String.class);
RuleScore score = new RuleScore(rule, reason);
threatScore.addRuleScore(score);
}
}
// calculate the aggregate threat score
Aggregators aggregators = threatTriageConfig.getAggregator();
List<Number> allScores = threatScore.getRuleScores().stream().map(score -> score.getRule().getScore()).collect(Collectors.toList());
Double aggregateScore = aggregators.aggregate(allScores, threatTriageConfig.getAggregationConfig());
threatScore.setScore(aggregateScore);
return threatScore;
}
use of org.apache.metron.stellar.dsl.Context in project metron by apache.
the class SimpleHBaseEnrichmentFunctionsTest method setup.
@Before
public void setup() throws Exception {
final MockHTable hbaseTable = (MockHTable) MockHBaseTableProvider.addToCache(hbaseTableName, cf);
EnrichmentHelper.INSTANCE.load(hbaseTable, cf, new ArrayList<LookupKV<EnrichmentKey, EnrichmentValue>>() {
{
for (int i = 0; i < 5; ++i) {
add(new LookupKV<>(new EnrichmentKey(ENRICHMENT_TYPE, "indicator" + i), new EnrichmentValue(ImmutableMap.of("key" + i, "value" + i))));
}
}
});
context = new Context.Builder().with(Context.Capabilities.GLOBAL_CONFIG, () -> ImmutableMap.of(SimpleHBaseEnrichmentFunctions.TABLE_PROVIDER_TYPE_CONF, MockHBaseTableProvider.class.getName())).build();
}
use of org.apache.metron.stellar.dsl.Context in project metron by apache.
the class StellarCompiler method exitVariable.
@Override
public void exitVariable(StellarParser.VariableContext ctx) {
final FrameContext.Context context = getArgContext();
expression.tokenDeque.push(new Token<>((tokenDeque, state) -> {
String varName = ctx.getText();
if (state.context.getActivityType().equals(ActivityType.PARSE_ACTIVITY) && !state.variableResolver.exists(varName)) {
// when parsing, missing variables are an error!
throw new ParseException(String.format("variable: %s is not defined", varName));
}
tokenDeque.push(new Token<>(state.variableResolver.resolve(varName), Object.class, context));
}, DeferredFunction.class, context));
expression.variablesUsed.add(ctx.getText());
}
Aggregations