Search in sources :

Example 1 with InputEntity

use of org.graylog2.contentpacks.model.entities.InputEntity in project graylog2-server by Graylog2.

the class InputFacade method exportNativeEntity.

@VisibleForTesting
Entity exportNativeEntity(InputWithExtractors inputWithExtractors, EntityDescriptorIds entityDescriptorIds) {
    final Input input = inputWithExtractors.input();
    // TODO: Create independent representation of entity?
    final Map<String, ValueReference> staticFields = input.getStaticFields().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, kv -> ValueReference.of(kv.getValue())));
    final ReferenceMap configuration = toReferenceMap(input.getConfiguration());
    final List<ExtractorEntity> extractors = inputWithExtractors.extractors().stream().map(this::encodeExtractor).collect(Collectors.toList());
    final InputEntity inputEntity = InputEntity.create(ValueReference.of(input.getTitle()), configuration, staticFields, ValueReference.of(input.getType()), ValueReference.of(input.isGlobal()), extractors);
    final JsonNode data = objectMapper.convertValue(inputEntity, JsonNode.class);
    final Set<Constraint> constraints = versionConstraints(input);
    return EntityV1.builder().id(ModelId.of(entityDescriptorIds.getOrThrow(input.getId(), ModelTypes.INPUT_V1))).type(ModelTypes.INPUT_V1).data(data).constraints(ImmutableSet.copyOf(constraints)).build();
}
Also used : Extractor(org.graylog2.plugin.inputs.Extractor) Constraint(org.graylog2.contentpacks.model.constraints.Constraint) ImmutableGraph(com.google.common.graph.ImmutableGraph) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Tools(org.graylog2.plugin.Tools) InputService(org.graylog2.inputs.InputService) NoSuchInputTypeException(org.graylog2.shared.inputs.NoSuchInputTypeException) LoggerFactory(org.slf4j.LoggerFactory) ModelType(org.graylog2.contentpacks.model.ModelType) ConverterEntity(org.graylog2.contentpacks.model.entities.ConverterEntity) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference) InputRegistry(org.graylog2.shared.inputs.InputRegistry) MessageInputFactory(org.graylog2.shared.inputs.MessageInputFactory) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) ConverterFactory(org.graylog2.inputs.converters.ConverterFactory) ImmutableSet(com.google.common.collect.ImmutableSet) ModelId(org.graylog2.contentpacks.model.ModelId) GrokPatternService(org.graylog2.grok.GrokPatternService) ImmutableMap(com.google.common.collect.ImmutableMap) MutableGraph(com.google.common.graph.MutableGraph) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) Collection(java.util.Collection) Set(java.util.Set) InputEntity(org.graylog2.contentpacks.model.entities.InputEntity) ServerStatus(org.graylog2.plugin.ServerStatus) ReferenceMapUtils.toReferenceMap(org.graylog2.contentpacks.model.entities.references.ReferenceMapUtils.toReferenceMap) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) GraphBuilder(com.google.common.graph.GraphBuilder) List(java.util.List) Stream(java.util.stream.Stream) GrokExtractor(org.graylog2.inputs.extractors.GrokExtractor) PluginMetaData(org.graylog2.plugin.PluginMetaData) Optional(java.util.Optional) ModelTypes(org.graylog2.contentpacks.model.ModelTypes) Configuration(org.graylog2.plugin.configuration.Configuration) EntityDescriptorIds(org.graylog2.contentpacks.EntityDescriptorIds) Converter(org.graylog2.plugin.inputs.Converter) Entity(org.graylog2.contentpacks.model.entities.Entity) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) ExtractorEntity(org.graylog2.contentpacks.model.entities.ExtractorEntity) DBLookupTableService(org.graylog2.lookup.db.DBLookupTableService) Inject(javax.inject.Inject) ImmutableList(com.google.common.collect.ImmutableList) EntityExcerpt(org.graylog2.contentpacks.model.entities.EntityExcerpt) ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) Input(org.graylog2.inputs.Input) ExtractorFactory(org.graylog2.inputs.extractors.ExtractorFactory) NotFoundException(org.graylog2.database.NotFoundException) ReferenceMap(org.graylog2.contentpacks.model.entities.references.ReferenceMap) PluginVersionConstraint(org.graylog2.contentpacks.model.constraints.PluginVersionConstraint) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DateTime(org.joda.time.DateTime) LookupTableEntity(org.graylog2.contentpacks.model.entities.LookupTableEntity) Ints(com.google.common.primitives.Ints) LookupTableExtractor(org.graylog2.inputs.extractors.LookupTableExtractor) EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) MessageInput(org.graylog2.plugin.inputs.MessageInput) ValidationException(org.graylog2.plugin.database.ValidationException) VisibleForTesting(com.google.common.annotations.VisibleForTesting) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) Message(org.graylog2.plugin.Message) ReferenceMapUtils.toValueMap(org.graylog2.contentpacks.model.entities.references.ReferenceMapUtils.toValueMap) Graph(com.google.common.graph.Graph) Constraint(org.graylog2.contentpacks.model.constraints.Constraint) PluginVersionConstraint(org.graylog2.contentpacks.model.constraints.PluginVersionConstraint) ReferenceMapUtils.toReferenceMap(org.graylog2.contentpacks.model.entities.references.ReferenceMapUtils.toReferenceMap) ReferenceMap(org.graylog2.contentpacks.model.entities.references.ReferenceMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) Input(org.graylog2.inputs.Input) MessageInput(org.graylog2.plugin.inputs.MessageInput) ExtractorEntity(org.graylog2.contentpacks.model.entities.ExtractorEntity) InputEntity(org.graylog2.contentpacks.model.entities.InputEntity) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ReferenceMapUtils.toReferenceMap(org.graylog2.contentpacks.model.entities.references.ReferenceMapUtils.toReferenceMap) ReferenceMap(org.graylog2.contentpacks.model.entities.references.ReferenceMap) ReferenceMapUtils.toValueMap(org.graylog2.contentpacks.model.entities.references.ReferenceMapUtils.toValueMap) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with InputEntity

use of org.graylog2.contentpacks.model.entities.InputEntity in project graylog2-server by Graylog2.

the class InputFacade method resolveForInstallationV1.

private Graph<Entity> resolveForInstallationV1(EntityV1 entity, Map<String, ValueReference> parameters, Map<EntityDescriptor, Entity> entities) {
    final MutableGraph<Entity> graph = GraphBuilder.directed().build();
    graph.addNode(entity);
    final InputEntity input = objectMapper.convertValue(entity.data(), InputEntity.class);
    resolveForInstallationV1LookupTable(entity, input, parameters, entities, graph);
    resolveForInstallationV1GrokPattern(entity, input, parameters, entities, graph);
    return ImmutableGraph.copyOf(graph);
}
Also used : NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) ConverterEntity(org.graylog2.contentpacks.model.entities.ConverterEntity) InputEntity(org.graylog2.contentpacks.model.entities.InputEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) ExtractorEntity(org.graylog2.contentpacks.model.entities.ExtractorEntity) LookupTableEntity(org.graylog2.contentpacks.model.entities.LookupTableEntity) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) InputEntity(org.graylog2.contentpacks.model.entities.InputEntity)

Example 3 with InputEntity

use of org.graylog2.contentpacks.model.entities.InputEntity in project graylog2-server by Graylog2.

the class InputFacade method decode.

private NativeEntity<InputWithExtractors> decode(EntityV1 entity, Map<String, ValueReference> parameters, String username) {
    final InputEntity inputEntity = objectMapper.convertValue(entity.data(), InputEntity.class);
    final Map<String, ValueReference> staticFields = inputEntity.staticFields();
    final MessageInput messageInput;
    try {
        messageInput = createMessageInput(inputEntity.title().asString(parameters), inputEntity.type().asString(parameters), inputEntity.global().asBoolean(parameters), toValueMap(inputEntity.configuration(), parameters), username);
    } catch (Exception e) {
        throw new RuntimeException("Couldn't create input", e);
    }
    final Input input;
    try {
        input = inputService.find(messageInput.getPersistId());
    } catch (NotFoundException e) {
        throw new RuntimeException("Couldn't find persisted input", e);
    }
    try {
        addStaticFields(input, messageInput, staticFields, parameters);
    } catch (ValidationException e) {
        throw new RuntimeException("Couldn't add static fields to input", e);
    }
    final List<Extractor> extractors;
    try {
        extractors = createExtractors(input, inputEntity.extractors(), username, parameters);
    } catch (Exception e) {
        throw new RuntimeException("Couldn't create extractors", e);
    }
    return NativeEntity.create(entity.id(), input.getId(), TYPE_V1, input.getTitle(), InputWithExtractors.create(input, extractors));
}
Also used : Input(org.graylog2.inputs.Input) MessageInput(org.graylog2.plugin.inputs.MessageInput) ValidationException(org.graylog2.plugin.database.ValidationException) InputEntity(org.graylog2.contentpacks.model.entities.InputEntity) MessageInput(org.graylog2.plugin.inputs.MessageInput) NotFoundException(org.graylog2.database.NotFoundException) Extractor(org.graylog2.plugin.inputs.Extractor) GrokExtractor(org.graylog2.inputs.extractors.GrokExtractor) LookupTableExtractor(org.graylog2.inputs.extractors.LookupTableExtractor) NoSuchInputTypeException(org.graylog2.shared.inputs.NoSuchInputTypeException) ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) NotFoundException(org.graylog2.database.NotFoundException) ValidationException(org.graylog2.plugin.database.ValidationException) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference)

Example 4 with InputEntity

use of org.graylog2.contentpacks.model.entities.InputEntity in project graylog2-server by Graylog2.

the class InputFacadeTest method exportEntity.

@Test
@MongoDBFixtures("InputFacadeTest.json")
public void exportEntity() {
    final ModelId id = ModelId.of("5acc84f84b900a4ff290d9a7");
    final EntityDescriptor descriptor = EntityDescriptor.create(id, ModelTypes.INPUT_V1);
    final EntityDescriptorIds entityDescriptorIds = EntityDescriptorIds.of(descriptor);
    final Entity entity = facade.exportEntity(descriptor, entityDescriptorIds).orElseThrow(AssertionError::new);
    assertThat(entity).isInstanceOf(EntityV1.class);
    assertThat(entity.id()).isEqualTo(ModelId.of(entityDescriptorIds.get(descriptor).orElse(null)));
    assertThat(entity.type()).isEqualTo(ModelTypes.INPUT_V1);
    final EntityV1 entityV1 = (EntityV1) entity;
    final InputEntity inputEntity = objectMapper.convertValue(entityV1.data(), InputEntity.class);
    assertThat(inputEntity.title()).isEqualTo(ValueReference.of("Local Raw UDP"));
    assertThat(inputEntity.type()).isEqualTo(ValueReference.of("org.graylog2.inputs.raw.udp.RawUDPInput"));
    assertThat(inputEntity.global()).isEqualTo(ValueReference.of(false));
    assertThat(inputEntity.configuration()).containsEntry("bind_address", ValueReference.of("127.0.0.1")).containsEntry("port", ValueReference.of(5555));
}
Also used : EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) ConverterEntity(org.graylog2.contentpacks.model.entities.ConverterEntity) InputEntity(org.graylog2.contentpacks.model.entities.InputEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) ExtractorEntity(org.graylog2.contentpacks.model.entities.ExtractorEntity) LookupTableEntity(org.graylog2.contentpacks.model.entities.LookupTableEntity) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) EntityDescriptorIds(org.graylog2.contentpacks.EntityDescriptorIds) InputEntity(org.graylog2.contentpacks.model.entities.InputEntity) ModelId(org.graylog2.contentpacks.model.ModelId) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 5 with InputEntity

use of org.graylog2.contentpacks.model.entities.InputEntity in project graylog2-server by Graylog2.

the class InputFacadeTest method collectEntity.

@Test
@MongoDBFixtures("InputFacadeTest.json")
public void collectEntity() {
    final EntityDescriptor descriptor = EntityDescriptor.create("5adf25294b900a0fdb4e5365", ModelTypes.INPUT_V1);
    final EntityDescriptorIds entityDescriptorIds = EntityDescriptorIds.of(descriptor);
    final Optional<Entity> collectedEntity = facade.exportEntity(descriptor, entityDescriptorIds);
    assertThat(collectedEntity).isPresent().containsInstanceOf(EntityV1.class);
    final EntityV1 entity = (EntityV1) collectedEntity.get();
    assertThat(entity.id()).isEqualTo(ModelId.of(entityDescriptorIds.get(descriptor).orElse(null)));
    assertThat(entity.type()).isEqualTo(ModelTypes.INPUT_V1);
    final InputEntity inputEntity = objectMapper.convertValue(entity.data(), InputEntity.class);
    assertThat(inputEntity.title()).isEqualTo(ValueReference.of("Global Random HTTP"));
    assertThat(inputEntity.type()).isEqualTo(ValueReference.of("org.graylog2.inputs.random.FakeHttpMessageInput"));
    assertThat(inputEntity.global()).isEqualTo(ValueReference.of(true));
    assertThat(inputEntity.staticFields()).containsEntry("custom_field", ValueReference.of("foobar"));
    assertThat(inputEntity.configuration()).isNotEmpty();
    assertThat(inputEntity.extractors()).hasSize(5);
}
Also used : EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) ConverterEntity(org.graylog2.contentpacks.model.entities.ConverterEntity) InputEntity(org.graylog2.contentpacks.model.entities.InputEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) ExtractorEntity(org.graylog2.contentpacks.model.entities.ExtractorEntity) LookupTableEntity(org.graylog2.contentpacks.model.entities.LookupTableEntity) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) EntityDescriptorIds(org.graylog2.contentpacks.EntityDescriptorIds) InputEntity(org.graylog2.contentpacks.model.entities.InputEntity) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Aggregations

InputEntity (org.graylog2.contentpacks.model.entities.InputEntity)8 ConverterEntity (org.graylog2.contentpacks.model.entities.ConverterEntity)7 Entity (org.graylog2.contentpacks.model.entities.Entity)7 ExtractorEntity (org.graylog2.contentpacks.model.entities.ExtractorEntity)7 GrokPatternEntity (org.graylog2.contentpacks.model.entities.GrokPatternEntity)7 LookupTableEntity (org.graylog2.contentpacks.model.entities.LookupTableEntity)7 NativeEntity (org.graylog2.contentpacks.model.entities.NativeEntity)7 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)6 EntityDescriptorIds (org.graylog2.contentpacks.EntityDescriptorIds)4 EntityV1 (org.graylog2.contentpacks.model.entities.EntityV1)4 Input (org.graylog2.inputs.Input)4 GrokExtractor (org.graylog2.inputs.extractors.GrokExtractor)4 LookupTableExtractor (org.graylog2.inputs.extractors.LookupTableExtractor)4 MessageInput (org.graylog2.plugin.inputs.MessageInput)4 Test (org.junit.Test)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)3 Extractor (org.graylog2.plugin.inputs.Extractor)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2