Search in sources :

Example 6 with ParseException

use of org.graylog.plugins.pipelineprocessor.parser.ParseException in project graylog2-server by Graylog2.

the class RuleResource method parse.

@ApiOperation(value = "Parse a processing rule without saving it", notes = "")
@POST
@Path("/parse")
@NoAuditEvent("only used to parse a rule, no changes made in the system")
public RuleSource parse(@ApiParam(name = "rule", required = true) @NotNull RuleSource ruleSource) throws ParseException {
    final Rule rule;
    try {
        // be silent about parse errors here, many requests will result in invalid syntax
        rule = pipelineRuleParser.parseRule(ruleSource.id(), ruleSource.source(), true);
    } catch (ParseException e) {
        throw new BadRequestException(Response.status(Response.Status.BAD_REQUEST).entity(e.getErrors()).build());
    }
    final DateTime now = DateTime.now(DateTimeZone.UTC);
    return RuleSource.builder().title(rule.name()).description(ruleSource.description()).source(ruleSource.source()).createdAt(now).modifiedAt(now).build();
}
Also used : BadRequestException(javax.ws.rs.BadRequestException) Rule(org.graylog.plugins.pipelineprocessor.ast.Rule) ParseException(org.graylog.plugins.pipelineprocessor.parser.ParseException) DateTime(org.joda.time.DateTime) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent)

Example 7 with ParseException

use of org.graylog.plugins.pipelineprocessor.parser.ParseException in project graylog2-server by Graylog2.

the class ConfigurationStateUpdater method reloadAndSave.

// only the singleton instance should mutate itself, others are welcome to reload a new state, but we don't
// currently allow direct global state updates from external sources (if you need to, send an event on the bus instead)
private synchronized PipelineInterpreter.State reloadAndSave() {
    // read all rules and parse them
    Map<String, Rule> ruleNameMap = Maps.newHashMap();
    ruleService.loadAll().forEach(ruleDao -> {
        Rule rule;
        try {
            rule = pipelineRuleParser.parseRule(ruleDao.id(), ruleDao.source(), false);
        } catch (ParseException e) {
            log.warn("Ignoring non parseable rule <{}/{}> with errors <{}>", ruleDao.title(), ruleDao.id(), e.getErrors());
            rule = Rule.alwaysFalse("Failed to parse rule: " + ruleDao.id());
        }
        ruleNameMap.put(rule.name(), rule);
    });
    // read all pipelines and parse them
    ImmutableMap.Builder<String, Pipeline> pipelineIdMap = ImmutableMap.builder();
    pipelineService.loadAll().forEach(pipelineDao -> {
        Pipeline pipeline;
        try {
            pipeline = pipelineRuleParser.parsePipeline(pipelineDao.id(), pipelineDao.source());
        } catch (ParseException e) {
            pipeline = Pipeline.empty("Failed to parse pipeline" + pipelineDao.id());
        }
        // noinspection ConstantConditions
        pipelineIdMap.put(pipelineDao.id(), resolvePipeline(pipeline, ruleNameMap));
    });
    final ImmutableMap<String, Pipeline> currentPipelines = pipelineIdMap.build();
    // read all stream connections of those pipelines to allow processing messages through them
    final HashMultimap<String, Pipeline> connections = HashMultimap.create();
    for (PipelineConnections streamConnection : pipelineStreamConnectionsService.loadAll()) {
        streamConnection.pipelineIds().stream().map(currentPipelines::get).filter(Objects::nonNull).forEach(pipeline -> connections.put(streamConnection.streamId(), pipeline));
    }
    ImmutableSetMultimap<String, Pipeline> streamPipelineConnections = ImmutableSetMultimap.copyOf(connections);
    final RuleMetricsConfigDto ruleMetricsConfig = ruleMetricsConfigService.get();
    final PipelineInterpreter.State newState = stateFactory.newState(currentPipelines, streamPipelineConnections, ruleMetricsConfig);
    latestState.set(newState);
    return newState;
}
Also used : PipelineConnections(org.graylog.plugins.pipelineprocessor.rest.PipelineConnections) ImmutableMap(com.google.common.collect.ImmutableMap) RuleMetricsConfigDto(org.graylog.plugins.pipelineprocessor.db.RuleMetricsConfigDto) Pipeline(org.graylog.plugins.pipelineprocessor.ast.Pipeline) Rule(org.graylog.plugins.pipelineprocessor.ast.Rule) ParseException(org.graylog.plugins.pipelineprocessor.parser.ParseException)

Example 8 with ParseException

use of org.graylog.plugins.pipelineprocessor.parser.ParseException in project graylog2-server by Graylog2.

the class PipelineResource method update.

@ApiOperation(value = "Modify a processing pipeline", notes = "It can take up to a second until the change is applied")
@Path("/{id}")
@PUT
@AuditEvent(type = PipelineProcessorAuditEventTypes.PIPELINE_UPDATE)
public PipelineSource update(@ApiParam(name = "id") @PathParam("id") String id, @ApiParam(name = "pipeline", required = true) @NotNull PipelineSource update) throws NotFoundException {
    checkPermission(PipelineRestPermissions.PIPELINE_EDIT, id);
    final PipelineDao dao = pipelineService.load(id);
    final Pipeline pipeline;
    try {
        pipeline = pipelineRuleParser.parsePipeline(update.id(), update.source());
    } catch (ParseException e) {
        throw new BadRequestException(Response.status(Response.Status.BAD_REQUEST).entity(e.getErrors()).build());
    }
    final PipelineDao toSave = dao.toBuilder().title(pipeline.name()).description(update.description()).source(update.source()).modifiedAt(DateTime.now(DateTimeZone.UTC)).build();
    final PipelineDao savedPipeline = pipelineService.save(toSave);
    return PipelineSource.fromDao(pipelineRuleParser, savedPipeline);
}
Also used : PipelineDao(org.graylog.plugins.pipelineprocessor.db.PipelineDao) BadRequestException(javax.ws.rs.BadRequestException) ParseException(org.graylog.plugins.pipelineprocessor.parser.ParseException) Pipeline(org.graylog.plugins.pipelineprocessor.ast.Pipeline) Path(javax.ws.rs.Path) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) PUT(javax.ws.rs.PUT)

Example 9 with ParseException

use of org.graylog.plugins.pipelineprocessor.parser.ParseException in project graylog2-server by Graylog2.

the class PipelineSource method fromDao.

public static PipelineSource fromDao(PipelineRuleParser parser, PipelineDao dao) {
    Set<ParseError> errors = null;
    Pipeline pipeline = null;
    try {
        pipeline = parser.parsePipeline(dao.id(), dao.source());
    } catch (ParseException e) {
        errors = e.getErrors();
    }
    final List<StageSource> stageSources = (pipeline == null) ? Collections.emptyList() : pipeline.stages().stream().map(stage -> StageSource.builder().match(stage.match()).rules(stage.ruleReferences()).stage(stage.stage()).build()).collect(Collectors.toList());
    return builder().id(dao.id()).title(dao.title()).description(dao.description()).source(dao.source()).createdAt(dao.createdAt()).modifiedAt(dao.modifiedAt()).stages(stageSources).errors(errors).build();
}
Also used : ParseError(org.graylog.plugins.pipelineprocessor.parser.errors.ParseError) ParseException(org.graylog.plugins.pipelineprocessor.parser.ParseException) Pipeline(org.graylog.plugins.pipelineprocessor.ast.Pipeline)

Example 10 with ParseException

use of org.graylog.plugins.pipelineprocessor.parser.ParseException in project graylog2-server by Graylog2.

the class PipelineResourceTest method shouldNotParseAPipelineSuccessfullyIfRaisingAnError.

@Test
public void shouldNotParseAPipelineSuccessfullyIfRaisingAnError() {
    final PipelineSource pipelineSource = PipelineSource.builder().source("foo").stages(Collections.emptyList()).title("Graylog Git Pipeline").build();
    when(pipelineRuleParser.parsePipeline(pipelineSource.id(), pipelineSource.source())).thenThrow(new ParseException(Collections.emptySet()));
    assertThatExceptionOfType(BadRequestException.class).isThrownBy(() -> this.pipelineResource.parse(pipelineSource));
}
Also used : BadRequestException(javax.ws.rs.BadRequestException) ParseException(org.graylog.plugins.pipelineprocessor.parser.ParseException) Test(org.junit.Test)

Aggregations

ParseException (org.graylog.plugins.pipelineprocessor.parser.ParseException)11 BadRequestException (javax.ws.rs.BadRequestException)7 ApiOperation (io.swagger.annotations.ApiOperation)6 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)6 Pipeline (org.graylog.plugins.pipelineprocessor.ast.Pipeline)5 Rule (org.graylog.plugins.pipelineprocessor.ast.Rule)5 AuditEvent (org.graylog2.audit.jersey.AuditEvent)5 POST (javax.ws.rs.POST)4 Path (javax.ws.rs.Path)4 DateTime (org.joda.time.DateTime)4 PUT (javax.ws.rs.PUT)3 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)3 PipelineDao (org.graylog.plugins.pipelineprocessor.db.PipelineDao)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 RuleDao (org.graylog.plugins.pipelineprocessor.db.RuleDao)2 Test (org.junit.Test)2 Lists (com.google.common.collect.Lists)1 Api (io.swagger.annotations.Api)1 ApiParam (io.swagger.annotations.ApiParam)1 ArrayList (java.util.ArrayList)1