Search in sources :

Example 1 with GrokException

use of io.krakens.grok.api.exception.GrokException in project graylog2-server by Graylog2.

the class MongoDbGrokPatternService method save.

@Override
public GrokPattern save(GrokPattern pattern) throws ValidationException {
    try {
        if (!validate(pattern)) {
            throw new ValidationException("Invalid pattern " + pattern);
        }
    } catch (GrokException | PatternSyntaxException e) {
        throw new ValidationException("Invalid pattern " + pattern + "\n" + e.getMessage());
    }
    if (loadByName(pattern.name()).isPresent()) {
        throw new ValidationException("Grok pattern " + pattern.name() + " already exists");
    }
    final WriteResult<GrokPattern, ObjectId> result = dbCollection.save(pattern);
    final GrokPattern savedGrokPattern = result.getSavedObject();
    clusterBus.post(GrokPatternsUpdatedEvent.create(ImmutableSet.of(savedGrokPattern.name())));
    return savedGrokPattern;
}
Also used : ValidationException(org.graylog2.plugin.database.ValidationException) ObjectId(org.bson.types.ObjectId) GrokException(io.krakens.grok.api.exception.GrokException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 2 with GrokException

use of io.krakens.grok.api.exception.GrokException in project graylog2-server by Graylog2.

the class InMemoryGrokPatternService method save.

@Override
public GrokPattern save(GrokPattern pattern) throws ValidationException {
    try {
        if (!validate(pattern)) {
            throw new ValidationException("Pattern " + pattern.name() + " invalid.");
        }
    } catch (GrokException | PatternSyntaxException e) {
        throw new ValidationException("Invalid pattern " + pattern + "\n" + e.getMessage());
    }
    GrokPattern toSave;
    if (pattern.id() == null) {
        toSave = pattern.toBuilder().id(createId()).build();
    } else {
        toSave = pattern;
    }
    store.put(toSave.id(), toSave);
    clusterBus.post(GrokPatternsUpdatedEvent.create(ImmutableSet.of(toSave.name())));
    return toSave;
}
Also used : ValidationException(org.graylog2.plugin.database.ValidationException) GrokException(io.krakens.grok.api.exception.GrokException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 3 with GrokException

use of io.krakens.grok.api.exception.GrokException in project graylog2-server by Graylog2.

the class GrokTesterResource method doTestGrok.

private GrokTesterResponse doTestGrok(String string, String pattern, boolean namedCapturesOnly) throws GrokException {
    final Set<GrokPattern> grokPatterns = grokPatternService.loadAll();
    final GrokCompiler grokCompiler = GrokCompiler.newInstance();
    for (GrokPattern grokPattern : grokPatterns) {
        grokCompiler.register(grokPattern.name(), grokPattern.pattern());
    }
    final Grok grok;
    try {
        grok = grokCompiler.compile(pattern, namedCapturesOnly);
    } catch (Exception e) {
        return GrokTesterResponse.createError(pattern, string, e.getMessage());
    }
    final Match match = grok.match(string);
    final Map<String, Object> matches = match.captureFlattened();
    final GrokTesterResponse response;
    if (matches.isEmpty()) {
        response = GrokTesterResponse.createSuccess(false, Collections.<GrokTesterResponse.Match>emptyList(), pattern, string);
    } else {
        final List<GrokTesterResponse.Match> responseMatches = Lists.newArrayList();
        for (final Map.Entry<String, Object> entry : matches.entrySet()) {
            final Object value = entry.getValue();
            if (value != null) {
                responseMatches.add(GrokTesterResponse.Match.create(entry.getKey(), value.toString()));
            }
        }
        response = GrokTesterResponse.createSuccess(true, responseMatches, pattern, string);
    }
    return response;
}
Also used : Grok(io.krakens.grok.api.Grok) GrokCompiler(io.krakens.grok.api.GrokCompiler) GrokException(io.krakens.grok.api.exception.GrokException) Match(io.krakens.grok.api.Match) GrokPattern(org.graylog2.grok.GrokPattern) Map(java.util.Map) GrokTesterResponse(org.graylog2.rest.resources.tools.responses.GrokTesterResponse)

Example 4 with GrokException

use of io.krakens.grok.api.exception.GrokException in project graylog2-server by Graylog2.

the class GrokResource method bulkUpdatePatterns.

@PUT
@Timed
@ApiOperation("Add a list of new patterns")
@AuditEvent(type = AuditEventTypes.GROK_PATTERN_IMPORT_CREATE)
public Response bulkUpdatePatterns(@ApiParam(name = "patterns", required = true) @NotNull GrokPatternList patternList, // 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 {
    checkPermission(RestPermissions.INPUTS_CREATE);
    try {
        if (!grokPatternService.validateAll(patternList.patterns())) {
            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(patternList.patterns(), resolvedStrategy);
    return Response.accepted().build();
}
Also used : ImportStrategy(org.graylog2.grok.GrokPatternService.ImportStrategy) ValidationException(org.graylog2.plugin.database.ValidationException) GrokException(io.krakens.grok.api.exception.GrokException) 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 5 with GrokException

use of io.krakens.grok.api.exception.GrokException 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)

Aggregations

GrokException (io.krakens.grok.api.exception.GrokException)7 ValidationException (org.graylog2.plugin.database.ValidationException)6 PatternSyntaxException (java.util.regex.PatternSyntaxException)4 Timed (com.codahale.metrics.annotation.Timed)2 Grok (io.krakens.grok.api.Grok)2 GrokCompiler (io.krakens.grok.api.GrokCompiler)2 ApiOperation (io.swagger.annotations.ApiOperation)2 Map (java.util.Map)2 ObjectId (org.bson.types.ObjectId)2 AuditEvent (org.graylog2.audit.jersey.AuditEvent)2 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)2 GrokPattern (org.graylog2.grok.GrokPattern)2 ImportStrategy (org.graylog2.grok.GrokPatternService.ImportStrategy)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Strings (com.google.common.base.Strings)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables (com.google.common.collect.Iterables)1 Sets (com.google.common.collect.Sets)1 IndexOptions (com.mongodb.client.model.IndexOptions)1 Indexes (com.mongodb.client.model.Indexes)1