Search in sources :

Example 11 with GrokPattern

use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.

the class GrokResource method bulkUpdatePatternsFromTextFile.

@POST
@Consumes(MediaType.TEXT_PLAIN)
@Timed
@ApiOperation("Add a list of new patterns")
@AuditEvent(type = AuditEventTypes.GROK_PATTERN_IMPORT_CREATE)
public Response bulkUpdatePatternsFromTextFile(@ApiParam(name = "patterns", required = true) @NotNull InputStream patternsFile, // deprecated. used to drop all existing patterns before import
@Deprecated @QueryParam("replace") @DefaultValue("false") boolean deprecatedDropAllExisting, @ApiParam(name = "import-strategy", value = "Strategy to apply when importing.") @QueryParam("import-strategy") ImportStrategy importStrategy) throws ValidationException, IOException {
    checkPermission(RestPermissions.INPUTS_CREATE);
    final List<GrokPattern> grokPatterns = readGrokPatterns(patternsFile);
    if (!grokPatterns.isEmpty()) {
        try {
            if (!grokPatternService.validateAll(grokPatterns)) {
                throw new ValidationException("Invalid pattern contained. Did not save any patterns.");
            }
        } catch (GrokException | IllegalArgumentException e) {
            throw new ValidationException("Invalid pattern. Did not save any patterns\n" + e.getMessage());
        }
        ImportStrategy resolvedStrategy = importStrategy != null ? importStrategy : deprecatedDropAllExisting ? ImportStrategy.DROP_ALL_EXISTING : ImportStrategy.ABORT_ON_CONFLICT;
        grokPatternService.saveAll(grokPatterns, resolvedStrategy);
    }
    return Response.accepted().build();
}
Also used : ImportStrategy(org.graylog2.grok.GrokPatternService.ImportStrategy) ValidationException(org.graylog2.plugin.database.ValidationException) GrokPattern(org.graylog2.grok.GrokPattern) GrokException(io.krakens.grok.api.exception.GrokException) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent)

Example 12 with GrokPattern

use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.

the class GrokPatternFacadeTest method createExcerpt.

@Test
public void createExcerpt() {
    final GrokPattern grokPattern = GrokPattern.create("01234567890", "name", "pattern", null);
    final EntityExcerpt excerpt = facade.createExcerpt(grokPattern);
    assertThat(excerpt.id()).isEqualTo(ModelId.of("01234567890"));
    assertThat(excerpt.type()).isEqualTo(ModelTypes.GROK_PATTERN_V1);
    assertThat(excerpt.title()).isEqualTo(grokPattern.name());
}
Also used : EntityExcerpt(org.graylog2.contentpacks.model.entities.EntityExcerpt) GrokPattern(org.graylog2.grok.GrokPattern) Test(org.junit.Test)

Example 13 with GrokPattern

use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.

the class GrokPatternFacadeTest method createNativeEntity.

@Test
public void createNativeEntity() throws NotFoundException {
    final Entity grokPatternEntity = EntityV1.builder().id(ModelId.of("1")).type(ModelTypes.GROK_PATTERN_V1).data(objectMapper.convertValue(GrokPatternEntity.create("Test", "[a-z]+"), JsonNode.class)).build();
    final NativeEntity<GrokPattern> nativeEntity = facade.createNativeEntity(grokPatternEntity, Collections.emptyMap(), Collections.emptyMap(), "admin");
    final GrokPattern expectedGrokPattern = GrokPattern.create("1", "Test", "[a-z]+", null);
    final NativeEntityDescriptor expectedDescriptor = NativeEntityDescriptor.create("1", "1", ModelTypes.GROK_PATTERN_V1, "Test");
    assertThat(nativeEntity.descriptor().title()).isEqualTo(expectedDescriptor.title());
    assertThat(nativeEntity.descriptor().type()).isEqualTo(expectedDescriptor.type());
    assertThat(nativeEntity.entity()).isEqualTo(expectedGrokPattern);
    assertThat(grokPatternService.load("1")).isEqualTo(expectedGrokPattern);
}
Also used : NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) GrokPattern(org.graylog2.grok.GrokPattern) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) Test(org.junit.Test)

Example 14 with GrokPattern

use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.

the class GrokPatternFacadeTest method findExisting.

@Test
public void findExisting() throws ValidationException {
    final GrokPattern grokPattern = grokPatternService.save(GrokPattern.create("Test", "[a-z]+"));
    final Entity grokPatternEntity = EntityV1.builder().id(ModelId.of("1")).type(ModelTypes.GROK_PATTERN_V1).data(objectMapper.convertValue(GrokPatternEntity.create("Test", "[a-z]+"), JsonNode.class)).build();
    final Optional<NativeEntity<GrokPattern>> existingGrokPattern = facade.findExisting(grokPatternEntity, Collections.emptyMap());
    final NativeEntityDescriptor expectedDescriptor = NativeEntityDescriptor.create(grokPatternEntity.id(), "1", ModelTypes.GROK_PATTERN_V1, grokPattern.name(), false);
    assertThat(existingGrokPattern).isPresent().get().satisfies(nativeEntity -> {
        assertThat(nativeEntity.descriptor()).isEqualTo(expectedDescriptor);
        assertThat(nativeEntity.entity()).isEqualTo(grokPattern);
    });
}
Also used : NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) GrokPattern(org.graylog2.grok.GrokPattern) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Test(org.junit.Test)

Example 15 with GrokPattern

use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.

the class GrokPatternFacadeTest method resolveEntityDescriptor.

@Test
public void resolveEntityDescriptor() throws ValidationException {
    final GrokPattern grokPattern = grokPatternService.save(GrokPattern.create("Test1", "[a-z]+"));
    final EntityDescriptor descriptor = EntityDescriptor.create(grokPattern.id(), ModelTypes.GROK_PATTERN_V1);
    final Graph<EntityDescriptor> graph = facade.resolveNativeEntity(descriptor);
    assertThat(graph.nodes()).containsOnly(descriptor);
}
Also used : EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) GrokPattern(org.graylog2.grok.GrokPattern) Test(org.junit.Test)

Aggregations

GrokPattern (org.graylog2.grok.GrokPattern)28 Test (org.junit.Test)15 ValidationException (org.graylog2.plugin.database.ValidationException)9 Timed (com.codahale.metrics.annotation.Timed)7 ApiOperation (io.swagger.annotations.ApiOperation)7 NativeEntityDescriptor (org.graylog2.contentpacks.model.entities.NativeEntityDescriptor)7 GrokException (io.krakens.grok.api.exception.GrokException)6 AuditEvent (org.graylog2.audit.jersey.AuditEvent)6 GrokPatternEntity (org.graylog2.contentpacks.model.entities.GrokPatternEntity)6 NotFoundException (org.graylog2.database.NotFoundException)5 PatternSyntaxException (java.util.regex.PatternSyntaxException)4 Response (javax.ws.rs.core.Response)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 POST (javax.ws.rs.POST)3 Path (javax.ws.rs.Path)3 Entity (org.graylog2.contentpacks.model.entities.Entity)3 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)3 Before (org.junit.Before)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Grok (io.krakens.grok.api.Grok)2