Search in sources :

Example 26 with GrokPattern

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

the class V20191121145100_FixDefaultGrokPatterns method migratePattern.

private void migratePattern(PatternToMigrate patternToMigrate) throws ValidationException {
    final Optional<GrokPattern> currentPattern = grokPatternService.loadByName(patternToMigrate.name());
    if (!currentPattern.isPresent()) {
        log.debug("Couldn't find default pattern '{}'. Skipping migration.", patternToMigrate.name());
        return;
    }
    final GrokPattern pattern = currentPattern.get();
    if (!patternToMigrate.migrateFrom().equals(pattern.pattern())) {
        log.info("Skipping migration of modified default Grok Pattern '{}'.", pattern.name());
    } else {
        log.info("Migrating default Grok Pattern '{}'.", pattern.name());
        final GrokPattern migratedPattern = pattern.toBuilder().pattern(patternToMigrate.migrateTo()).build();
        grokPatternService.update(migratedPattern);
    }
}
Also used : GrokPattern(org.graylog2.grok.GrokPattern)

Example 27 with GrokPattern

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

the class GrokResource method getPage.

@GET
@Timed
@Path("/paginated")
@ApiOperation("Get existing grok patterns paged")
@Produces(MediaType.APPLICATION_JSON)
public PaginatedResponse<GrokPattern> 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(GrokPattern.FIELD_NAME) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("asc") @QueryParam("order") String order) {
    checkPermission(RestPermissions.INPUTS_READ);
    SearchQuery searchQuery;
    try {
        searchQuery = searchQueryParser.parse(query);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException("Invalid argument in search query: " + e.getMessage());
    }
    final PaginatedList<GrokPattern> result = paginatedGrokPatternService.findPaginated(searchQuery, page, perPage, sort, order);
    return PaginatedResponse.create("patterns", result);
}
Also used : SearchQuery(org.graylog2.search.SearchQuery) GrokPattern(org.graylog2.grok.GrokPattern) BadRequestException(javax.ws.rs.BadRequestException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 28 with GrokPattern

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

the class GrokResource method updatePattern.

@PUT
@Timed
@Path("/{patternId}")
@ApiOperation("Update an existing pattern")
@AuditEvent(type = AuditEventTypes.GROK_PATTERN_UPDATE)
public GrokPattern updatePattern(@ApiParam(name = "patternId", required = true) @PathParam("patternId") String patternId, @ApiParam(name = "pattern", required = true) GrokPattern pattern) throws ValidationException {
    checkPermission(RestPermissions.INPUTS_EDIT);
    final GrokPattern grokPattern = GrokPattern.builder().id(patternId).name(pattern.name()).pattern(pattern.pattern()).build();
    return grokPatternService.update(grokPattern);
}
Also used : GrokPattern(org.graylog2.grok.GrokPattern) Path(javax.ws.rs.Path) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT)

Example 29 with GrokPattern

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

the class GrokResourceTest method testPatternWithSampleData.

@Test
public void testPatternWithSampleData() throws Exception {
    final String sampleData = "1.2.3.4";
    final GrokPattern grokPattern = GrokPattern.create("IP", "\\d.\\d.\\d.\\d");
    grokPatternService.save(grokPattern);
    final GrokPatternTestRequest grokPatternTestRequest = GrokPatternTestRequest.create(grokPattern, sampleData);
    final Map<String, Object> expectedReturn = Collections.singletonMap("IP", "1.2.3.4");
    final Response response = grokResource.testPattern(grokPatternTestRequest);
    assertThat(response.hasEntity()).isTrue();
    assertThat(response.getEntity()).isEqualTo(expectedReturn);
}
Also used : Response(javax.ws.rs.core.Response) GrokPattern(org.graylog2.grok.GrokPattern) GrokPatternTestRequest(org.graylog2.rest.models.system.grokpattern.requests.GrokPatternTestRequest) Test(org.junit.Test)

Example 30 with GrokPattern

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

the class GrokResourceTest method bulkUpdatePatternsFromTextFileWithCRLF.

@Test
public void bulkUpdatePatternsFromTextFileWithCRLF() throws Exception {
    final String patterns = Arrays.stream(GROK_LINES).collect(Collectors.joining("\r\n"));
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(patterns.getBytes(StandardCharsets.UTF_8));
    final GrokPattern expectedPattern = GrokPattern.create("TEST_PATTERN_0", "Foo");
    final Response response = grokResource.bulkUpdatePatternsFromTextFile(inputStream, true, null);
    assertThat(response.getStatusInfo()).isEqualTo(Response.Status.ACCEPTED);
    assertThat(response.hasEntity()).isFalse();
    await().atMost(Duration.FIVE_SECONDS).until(() -> !subscriber.events.isEmpty());
    assertThat(subscriber.events).containsOnly(GrokPatternsUpdatedEvent.create(Collections.singleton(expectedPattern.name())));
}
Also used : Response(javax.ws.rs.core.Response) GrokPattern(org.graylog2.grok.GrokPattern) ByteArrayInputStream(java.io.ByteArrayInputStream) 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