Search in sources :

Example 1 with RuleDao

use of org.graylog.plugins.pipelineprocessor.db.RuleDao in project graylog2-server by Graylog2.

the class MongoDbRuleService method loadByName.

@Override
public RuleDao loadByName(String name) throws NotFoundException {
    final DBQuery.Query query = DBQuery.is("title", name);
    final RuleDao rule = dbCollection.findOne(query);
    if (rule == null) {
        throw new NotFoundException("No rule with name " + name);
    }
    return rule;
}
Also used : RuleDao(org.graylog.plugins.pipelineprocessor.db.RuleDao) DBQuery(org.mongojack.DBQuery) NotFoundException(org.graylog2.database.NotFoundException)

Example 2 with RuleDao

use of org.graylog.plugins.pipelineprocessor.db.RuleDao in project graylog2-server by Graylog2.

the class PipelineRuleFacade method findExisting.

private Optional<NativeEntity<RuleDao>> findExisting(EntityV1 entity, Map<String, ValueReference> parameters) {
    final PipelineRuleEntity ruleEntity = objectMapper.convertValue(entity.data(), PipelineRuleEntity.class);
    final String title = ruleEntity.title().asString(parameters);
    final String source = ruleEntity.source().asString(parameters);
    try {
        final RuleDao ruleDao = ruleService.loadByName(title);
        compareRuleSources(title, source, ruleDao.source());
        return Optional.of(NativeEntity.create(entity.id(), ruleDao.id(), TYPE_V1, ruleDao.title(), ruleDao));
    } catch (NotFoundException e) {
        return Optional.empty();
    }
}
Also used : RuleDao(org.graylog.plugins.pipelineprocessor.db.RuleDao) PipelineRuleEntity(org.graylog2.contentpacks.model.entities.PipelineRuleEntity) NotFoundException(org.graylog2.database.NotFoundException)

Example 3 with RuleDao

use of org.graylog.plugins.pipelineprocessor.db.RuleDao in project graylog2-server by Graylog2.

the class RuleResource method getPage.

@GET
@Path("/paginated")
@ApiOperation(value = "Get a paginated list of pipeline rules")
@Produces(MediaType.APPLICATION_JSON)
@RequiresPermissions(PipelineRestPermissions.PIPELINE_RULE_READ)
public PaginatedResponse<RuleSource> getPage(@ApiParam(name = "page") @QueryParam("page") @DefaultValue("1") int page, @ApiParam(name = "per_page") @QueryParam("per_page") @DefaultValue("50") int perPage, @ApiParam(name = "query") @QueryParam("query") @DefaultValue("") String query, @ApiParam(name = "sort", value = "The field to sort the result on", required = true, allowableValues = "title,description,id") @DefaultValue(RuleDao.FIELD_TITLE) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("asc") @QueryParam("order") String order) {
    SearchQuery searchQuery;
    try {
        searchQuery = searchQueryParser.parse(query);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException("Invalid argument in search query: " + e.getMessage());
    }
    final PaginatedList<RuleDao> result = paginatedRuleService.findPaginated(searchQuery, page, perPage, sort, order);
    final List<RuleSource> ruleSourceList = result.stream().map(dao -> RuleSource.fromDao(pipelineRuleParser, dao)).collect(Collectors.toList());
    final PaginatedList<RuleSource> rules = new PaginatedList<>(ruleSourceList, result.pagination().total(), result.pagination().page(), result.pagination().perPage());
    return PaginatedResponse.create("rules", rules, prepareContextForPaginatedResponse(result.delegate()));
}
Also used : SearchQuery(org.graylog2.search.SearchQuery) RuleDao(org.graylog.plugins.pipelineprocessor.db.RuleDao) DateTimeZone(org.joda.time.DateTimeZone) Produces(javax.ws.rs.Produces) RuleDao(org.graylog.plugins.pipelineprocessor.db.RuleDao) RuleService(org.graylog.plugins.pipelineprocessor.db.RuleService) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) ApiParam(io.swagger.annotations.ApiParam) ApiOperation(io.swagger.annotations.ApiOperation) PaginatedList(org.graylog2.database.PaginatedList) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) PipelineService(org.graylog.plugins.pipelineprocessor.db.PipelineService) Consumes(javax.ws.rs.Consumes) SearchQueryField(org.graylog2.search.SearchQueryField) Map(java.util.Map) PluginRestResource(org.graylog2.plugin.rest.PluginRestResource) DefaultValue(javax.ws.rs.DefaultValue) PipelineRuleParser(org.graylog.plugins.pipelineprocessor.parser.PipelineRuleParser) BadRequestException(javax.ws.rs.BadRequestException) DELETE(javax.ws.rs.DELETE) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) NotNull(javax.validation.constraints.NotNull) PipelineProcessorAuditEventTypes(org.graylog.plugins.pipelineprocessor.audit.PipelineProcessorAuditEventTypes) Collectors(java.util.stream.Collectors) List(java.util.List) RuleMetricsConfigDto(org.graylog.plugins.pipelineprocessor.db.RuleMetricsConfigDto) Response(javax.ws.rs.core.Response) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) PathParam(javax.ws.rs.PathParam) SearchQueryParser(org.graylog2.search.SearchQueryParser) GET(javax.ws.rs.GET) ParseException(org.graylog.plugins.pipelineprocessor.parser.ParseException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) RuleMetricsConfigService(org.graylog.plugins.pipelineprocessor.db.RuleMetricsConfigService) AuditEvent(org.graylog2.audit.jersey.AuditEvent) Api(io.swagger.annotations.Api) SearchQuery(org.graylog2.search.SearchQuery) Rule(org.graylog.plugins.pipelineprocessor.ast.Rule) Nonnull(javax.annotation.Nonnull) NotFoundException(org.graylog2.database.NotFoundException) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) DateTime(org.joda.time.DateTime) Function(org.graylog.plugins.pipelineprocessor.ast.functions.Function) RestResource(org.graylog2.shared.rest.resources.RestResource) PipelineServiceHelper(org.graylog.plugins.pipelineprocessor.db.PipelineServiceHelper) PaginatedRuleService(org.graylog.plugins.pipelineprocessor.db.PaginatedRuleService) PUT(javax.ws.rs.PUT) VisibleForTesting(com.google.common.annotations.VisibleForTesting) FunctionRegistry(org.graylog.plugins.pipelineprocessor.parser.FunctionRegistry) PaginatedResponse(org.graylog2.rest.models.PaginatedResponse) BadRequestException(javax.ws.rs.BadRequestException) PaginatedList(org.graylog2.database.PaginatedList) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 4 with RuleDao

use of org.graylog.plugins.pipelineprocessor.db.RuleDao in project graylog2-server by Graylog2.

the class RuleResource method update.

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

Example 5 with RuleDao

use of org.graylog.plugins.pipelineprocessor.db.RuleDao in project graylog2-server by Graylog2.

the class RuleResource method createFromParser.

@ApiOperation(value = "Create a processing rule from source", notes = "")
@POST
@RequiresPermissions(PipelineRestPermissions.PIPELINE_RULE_CREATE)
@AuditEvent(type = PipelineProcessorAuditEventTypes.RULE_CREATE)
public RuleSource createFromParser(@ApiParam(name = "rule", required = true) @NotNull RuleSource ruleSource) throws ParseException {
    final Rule rule;
    try {
        rule = pipelineRuleParser.parseRule(ruleSource.id(), ruleSource.source(), false);
    } catch (ParseException e) {
        throw new BadRequestException(Response.status(Response.Status.BAD_REQUEST).entity(e.getErrors()).build());
    }
    final DateTime now = DateTime.now(DateTimeZone.UTC);
    final RuleDao newRuleSource = RuleDao.builder().title(// use the name from the parsed rule source.
    rule.name()).description(ruleSource.description()).source(ruleSource.source()).createdAt(now).modifiedAt(now).build();
    final RuleDao save = ruleService.save(newRuleSource);
    log.debug("Created new rule {}", save);
    return RuleSource.fromDao(pipelineRuleParser, save);
}
Also used : RuleDao(org.graylog.plugins.pipelineprocessor.db.RuleDao) BadRequestException(javax.ws.rs.BadRequestException) Rule(org.graylog.plugins.pipelineprocessor.ast.Rule) ParseException(org.graylog.plugins.pipelineprocessor.parser.ParseException) DateTime(org.joda.time.DateTime) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent)

Aggregations

RuleDao (org.graylog.plugins.pipelineprocessor.db.RuleDao)19 Test (org.junit.Test)10 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)4 PipelineRuleEntity (org.graylog2.contentpacks.model.entities.PipelineRuleEntity)4 NotFoundException (org.graylog2.database.NotFoundException)4 ApiOperation (io.swagger.annotations.ApiOperation)3 BadRequestException (javax.ws.rs.BadRequestException)3 Rule (org.graylog.plugins.pipelineprocessor.ast.Rule)3 ParseException (org.graylog.plugins.pipelineprocessor.parser.ParseException)3 AuditEvent (org.graylog2.audit.jersey.AuditEvent)3 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)3 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)3 DateTime (org.joda.time.DateTime)3 POST (javax.ws.rs.POST)2 PUT (javax.ws.rs.PUT)2 Path (javax.ws.rs.Path)2 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Api (io.swagger.annotations.Api)1