Search in sources :

Example 6 with ConfigurationException

use of org.graylog2.ConfigurationException in project graylog2-server by Graylog2.

the class RegexReplaceTesterResource method testRegexReplaceExtractor.

private RegexReplaceTesterResponse testRegexReplaceExtractor(String example, String regex, String replacement, boolean replaceAll) {
    final Map<String, Object> config = ImmutableMap.<String, Object>of("regex", regex, "replacement", replacement, "replace_all", replaceAll);
    final RegexReplaceExtractor extractor;
    try {
        extractor = new RegexReplaceExtractor(new MetricRegistry(), "test", "Test", 0L, Extractor.CursorStrategy.COPY, "test", "test", config, getCurrentUser().getName(), Collections.<Converter>emptyList(), Extractor.ConditionType.NONE, "");
    } catch (Extractor.ReservedFieldException e) {
        throw new BadRequestException("Trying to overwrite a reserved message field", e);
    } catch (ConfigurationException e) {
        throw new BadRequestException("Invalid extractor configuration", e);
    }
    final Extractor.Result result = extractor.runExtractor(example);
    final RegexReplaceTesterResponse.Match match = result == null ? null : RegexReplaceTesterResponse.Match.create(String.valueOf(result.getValue()), result.getBeginIndex(), result.getEndIndex());
    return RegexReplaceTesterResponse.create(result != null, match, regex, replacement, replaceAll, example);
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) RegexReplaceExtractor(org.graylog2.inputs.extractors.RegexReplaceExtractor) RegexReplaceTesterResponse(org.graylog2.rest.models.tools.responses.RegexReplaceTesterResponse) ConfigurationException(org.graylog2.ConfigurationException) Converter(org.graylog2.plugin.inputs.Converter) BadRequestException(javax.ws.rs.BadRequestException) Extractor(org.graylog2.plugin.inputs.Extractor) RegexReplaceExtractor(org.graylog2.inputs.extractors.RegexReplaceExtractor)

Example 7 with ConfigurationException

use of org.graylog2.ConfigurationException in project graylog2-server by Graylog2.

the class InputsResource method update.

@PUT
@Timed
@Path("/{inputId}")
@ApiOperation(value = "Update input on this node", response = InputCreated.class)
@ApiResponses(value = { @ApiResponse(code = 404, message = "No such input on this node."), @ApiResponse(code = 400, message = "Missing or invalid input configuration.") })
@AuditEvent(type = AuditEventTypes.MESSAGE_INPUT_UPDATE)
public Response update(@ApiParam(name = "JSON body", required = true) @Valid @NotNull InputCreateRequest lr, @ApiParam(name = "inputId", required = true) @PathParam("inputId") String inputId) throws org.graylog2.database.NotFoundException, NoSuchInputTypeException, ConfigurationException, ValidationException {
    checkPermission(RestPermissions.INPUTS_EDIT, inputId);
    final Input input = inputService.find(inputId);
    final Map<String, Object> mergedInput = input.getFields();
    final MessageInput messageInput = messageInputFactory.create(lr, getCurrentUser().getName(), lr.node());
    messageInput.checkConfiguration();
    mergedInput.putAll(messageInput.asMap());
    final Input newInput = inputService.create(input.getId(), mergedInput);
    inputService.save(newInput);
    final URI inputUri = getUriBuilderToSelf().path(InputsResource.class).path("{inputId}").build(input.getId());
    return Response.created(inputUri).entity(InputCreated.create(input.getId())).build();
}
Also used : Input(org.graylog2.inputs.Input) MessageInput(org.graylog2.plugin.inputs.MessageInput) MessageInput(org.graylog2.plugin.inputs.MessageInput) URI(java.net.URI) Path(javax.ws.rs.Path) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 8 with ConfigurationException

use of org.graylog2.ConfigurationException in project graylog2-server by Graylog2.

the class BundleImporter method createInputs.

private void createInputs(final String bundleId, final Set<Input> inputs, final String userName) throws org.graylog2.plugin.inputs.Extractor.ReservedFieldException, org.graylog2.ConfigurationException, NoSuchInputTypeException, ValidationException, ExtractorFactory.NoSuchExtractorException, NotFoundException, ConfigurationException {
    for (final Input input : inputs) {
        final MessageInput messageInput = createMessageInput(bundleId, input, userName);
        createdInputs.put(messageInput.getId(), messageInput);
        // Launch input. (this will run async and clean up itself in case of an error.)
        inputLauncher.launch(messageInput);
    }
}
Also used : MessageInput(org.graylog2.plugin.inputs.MessageInput) MessageInput(org.graylog2.plugin.inputs.MessageInput)

Example 9 with ConfigurationException

use of org.graylog2.ConfigurationException in project graylog2-server by Graylog2.

the class BundleImporter method createMessageInput.

private MessageInput createMessageInput(final String bundleId, final Input inputDescription, final String userName) throws NoSuchInputTypeException, ConfigurationException, ValidationException, NotFoundException, org.graylog2.ConfigurationException, ExtractorFactory.NoSuchExtractorException, org.graylog2.plugin.inputs.Extractor.ReservedFieldException {
    final Configuration inputConfig = new Configuration(inputDescription.getConfiguration());
    final DateTime createdAt = Tools.nowUTC();
    final MessageInput messageInput = messageInputFactory.create(inputDescription.getType(), inputConfig);
    messageInput.setTitle(inputDescription.getTitle());
    messageInput.setGlobal(inputDescription.isGlobal());
    messageInput.setCreatorUserId(userName);
    messageInput.setCreatedAt(createdAt);
    messageInput.setContentPack(bundleId);
    messageInput.checkConfiguration();
    // Don't run if exclusive and another instance is already running.
    if (messageInput.isExclusive() && inputRegistry.hasTypeRunning(messageInput.getClass())) {
        LOG.error("Input type <{}> of input <{}> is exclusive and already has input running.", messageInput.getClass(), messageInput.getTitle());
    }
    final String id = inputDescription.getId();
    final org.graylog2.inputs.Input mongoInput;
    if (id == null) {
        mongoInput = inputService.create(buildMongoDbInput(inputDescription, userName, createdAt, bundleId));
    } else {
        mongoInput = inputService.create(id, buildMongoDbInput(inputDescription, userName, createdAt, bundleId));
    }
    // Persist input.
    final String persistId = inputService.save(mongoInput);
    messageInput.setPersistId(persistId);
    messageInput.initialize();
    addStaticFields(messageInput, inputDescription.getStaticFields());
    addExtractors(messageInput, inputDescription.getExtractors(), userName);
    return messageInput;
}
Also used : Configuration(org.graylog2.plugin.configuration.Configuration) MessageInput(org.graylog2.plugin.inputs.MessageInput) DateTime(org.joda.time.DateTime)

Example 10 with ConfigurationException

use of org.graylog2.ConfigurationException in project graylog2-server by Graylog2.

the class ConfigurationRequest method check.

public void check(Configuration configuration) throws ConfigurationException {
    for (ConfigurationField field : fields.values()) {
        if (field.isOptional().equals(ConfigurationField.Optional.NOT_OPTIONAL)) {
            final String type = field.getFieldType();
            log.debug("Checking for non-optional field {} of type {} in configuration", field.getName(), type);
            switch(type) {
                case BooleanField.FIELD_TYPE:
                    if (!configuration.booleanIsSet(field.getName())) {
                        throw new ConfigurationException("Mandatory configuration field " + field.getName() + " is missing or has the wrong data type");
                    }
                    break;
                case NumberField.FIELD_TYPE:
                    if (!configuration.intIsSet(field.getName())) {
                        throw new ConfigurationException("Mandatory configuration field " + field.getName() + " is missing or has the wrong data type");
                    }
                    break;
                case TextField.FIELD_TYPE:
                case DropdownField.FIELD_TYPE:
                    if (!configuration.stringIsSet(field.getName())) {
                        throw new ConfigurationException("Mandatory configuration field " + field.getName() + " is missing or has the wrong data type");
                    }
                    break;
                default:
                    throw new IllegalStateException("Unknown field type " + type + ". This is a bug.");
            }
        }
    }
}
Also used : ConfigurationField(org.graylog2.plugin.configuration.fields.ConfigurationField)

Aggregations

Timed (com.codahale.metrics.annotation.Timed)7 ApiOperation (io.swagger.annotations.ApiOperation)7 BadRequestException (javax.ws.rs.BadRequestException)7 AuditEvent (org.graylog2.audit.jersey.AuditEvent)7 ConfigurationException (org.graylog2.plugin.configuration.ConfigurationException)7 ApiResponses (io.swagger.annotations.ApiResponses)5 URI (java.net.URI)5 POST (javax.ws.rs.POST)4 Path (javax.ws.rs.Path)4 AlertCondition (org.graylog2.plugin.alarms.AlertCondition)4 MessageInput (org.graylog2.plugin.inputs.MessageInput)4 Consumes (javax.ws.rs.Consumes)3 PUT (javax.ws.rs.PUT)3 Produces (javax.ws.rs.Produces)3 ConfigurationException (org.graylog2.ConfigurationException)3 AlarmCallbackConfiguration (org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)3 Stream (org.graylog2.plugin.streams.Stream)3 MetricRegistry (com.codahale.metrics.MetricRegistry)2 Input (org.graylog2.inputs.Input)2 AlarmCallbackConfigurationException (org.graylog2.plugin.alarms.callbacks.AlarmCallbackConfigurationException)2