Search in sources :

Example 1 with ExtractorEntity

use of org.graylog2.contentpacks.model.entities.ExtractorEntity 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 ExtractorEntity

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

the class InputFacadeTest method resolveForInstallationLookupTable.

@Test
@MongoDBFixtures("InputFacadeTest.json")
public void resolveForInstallationLookupTable() throws NotFoundException {
    when(lookupuptableBuilder.lookupTable("whois")).thenReturn(lookupuptableBuilder);
    when(lookupuptableBuilder.lookupTable("tor-exit-node-list")).thenReturn(lookupuptableBuilder);
    when(lookupuptableBuilder.build()).thenReturn(lookupTable);
    when(lookupTableService.newBuilder()).thenReturn(lookupuptableBuilder);
    when(lookupTableService.hasTable("whois")).thenReturn(true);
    when(lookupTableService.hasTable("tor-exit-node-list")).thenReturn(true);
    final Input input = inputService.find("5ae2eb0a3d27464477f0fd8b");
    final Map<String, Object> lookupTableConfig = new HashedMap(1);
    lookupTableConfig.put("lookup_table_name", "tor-exit-node-list");
    final ConverterEntity converterEntity = ConverterEntity.create(ValueReference.of(Converter.Type.LOOKUP_TABLE.name()), ReferenceMapUtils.toReferenceMap(lookupTableConfig));
    final List<ConverterEntity> converterEntities = new ArrayList<>(1);
    converterEntities.add(converterEntity);
    final InputWithExtractors inputWithExtractors = InputWithExtractors.create(input, inputService.getExtractors(input));
    final LookupTableExtractor extractor = (LookupTableExtractor) inputWithExtractors.extractors().iterator().next();
    final ExtractorEntity extractorEntity = ExtractorEntity.create(ValueReference.of(extractor.getTitle()), ValueReference.of(extractor.getType()), ValueReference.of(extractor.getCursorStrategy()), ValueReference.of(extractor.getTargetField()), ValueReference.of(extractor.getSourceField()), ReferenceMapUtils.toReferenceMap(extractor.getExtractorConfig()), converterEntities, ValueReference.of(extractor.getConditionType()), ValueReference.of(extractor.getConditionValue()), ValueReference.of(extractor.getOrder()));
    List<ExtractorEntity> extractors = new ArrayList<>();
    extractors.add(extractorEntity);
    InputEntity inputEntity = InputEntity.create(ValueReference.of(input.getTitle()), ReferenceMapUtils.toReferenceMap(input.getConfiguration()), Collections.emptyMap(), ValueReference.of(input.getType()), ValueReference.of(input.isGlobal()), extractors);
    final Entity entity = EntityV1.builder().id(ModelId.of(input.getId())).type(ModelTypes.INPUT_V1).data(objectMapper.convertValue(inputEntity, JsonNode.class)).build();
    final LookupTableEntity whoIsEntity = LookupTableEntity.create(ValueReference.of("whois"), ValueReference.of("title"), ValueReference.of("description"), ValueReference.of("cache_name"), ValueReference.of("dataadapter_name"), ValueReference.of("default_single_value"), ValueReference.of("BOOLEAN"), ValueReference.of("default_multi_value"), ValueReference.of("BOOLEAN"));
    final LookupTableEntity torNodeEntity = LookupTableEntity.create(ValueReference.of("tor-exit-node-list"), ValueReference.of("title"), ValueReference.of("description"), ValueReference.of("cache_name"), ValueReference.of("dataadapter_name"), ValueReference.of("default_single_value"), ValueReference.of("BOOLEAN"), ValueReference.of("default_multi_value"), ValueReference.of("BOOLEAN"));
    final Entity expectedWhoIsEntity = EntityV1.builder().id(ModelId.of("dead-beef")).data(objectMapper.convertValue(whoIsEntity, JsonNode.class)).type(ModelTypes.LOOKUP_TABLE_V1).build();
    final Entity expectedTorEntity = EntityV1.builder().id(ModelId.of("dead-feed")).data(objectMapper.convertValue(torNodeEntity, JsonNode.class)).type(ModelTypes.LOOKUP_TABLE_V1).build();
    final EntityDescriptor whoisDescriptor = expectedWhoIsEntity.toEntityDescriptor();
    final EntityDescriptor torDescriptor = expectedTorEntity.toEntityDescriptor();
    final Map<EntityDescriptor, Entity> entityDescriptorEntityMap = new HashMap<>(2);
    entityDescriptorEntityMap.put(whoisDescriptor, expectedWhoIsEntity);
    entityDescriptorEntityMap.put(torDescriptor, expectedTorEntity);
    Graph<Entity> graph = facade.resolveForInstallation(entity, Collections.emptyMap(), entityDescriptorEntityMap);
    assertThat(graph.nodes()).contains(expectedWhoIsEntity);
    assertThat(graph.nodes()).contains(expectedTorEntity);
}
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) HashMap(java.util.HashMap) LookupTableExtractor(org.graylog2.inputs.extractors.LookupTableExtractor) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) RawUDPInput(org.graylog2.inputs.raw.udp.RawUDPInput) Input(org.graylog2.inputs.Input) FakeHttpMessageInput(org.graylog2.inputs.random.FakeHttpMessageInput) MessageInput(org.graylog2.plugin.inputs.MessageInput) LookupTableEntity(org.graylog2.contentpacks.model.entities.LookupTableEntity) ConverterEntity(org.graylog2.contentpacks.model.entities.ConverterEntity) ExtractorEntity(org.graylog2.contentpacks.model.entities.ExtractorEntity) InputEntity(org.graylog2.contentpacks.model.entities.InputEntity) HashedMap(org.apache.commons.collections.map.HashedMap) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 3 with ExtractorEntity

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

the class InputFacadeTest method resolveForInstallationGrokPattern.

@Test
@MongoDBFixtures("InputFacadeTest.json")
public void resolveForInstallationGrokPattern() throws NotFoundException {
    final Input input = inputService.find("5ae2ebbeef27464477f0fd8b");
    final InputWithExtractors inputWithExtractors = InputWithExtractors.create(input, inputService.getExtractors(input));
    final GrokExtractor grokExtractor = (GrokExtractor) inputWithExtractors.extractors().iterator().next();
    final ExtractorEntity extractorEntity = ExtractorEntity.create(ValueReference.of(grokExtractor.getTitle()), ValueReference.of(grokExtractor.getType()), ValueReference.of(grokExtractor.getCursorStrategy()), ValueReference.of(grokExtractor.getTargetField()), ValueReference.of(grokExtractor.getSourceField()), ReferenceMapUtils.toReferenceMap(grokExtractor.getExtractorConfig()), Collections.emptyList(), ValueReference.of(grokExtractor.getConditionType()), ValueReference.of(grokExtractor.getConditionValue()), ValueReference.of(grokExtractor.getOrder()));
    List<ExtractorEntity> extractorEntities = new ArrayList<>(1);
    extractorEntities.add(extractorEntity);
    InputEntity inputEntity = InputEntity.create(ValueReference.of(input.getTitle()), ReferenceMapUtils.toReferenceMap(input.getConfiguration()), Collections.emptyMap(), ValueReference.of(input.getType()), ValueReference.of(input.isGlobal()), extractorEntities);
    Entity entity = EntityV1.builder().id(ModelId.of(input.getId())).type(ModelTypes.INPUT_V1).data(objectMapper.convertValue(inputEntity, JsonNode.class)).build();
    final GrokPatternEntity grokPatternEntity = GrokPatternEntity.create("GREEDY", ".*");
    final Entity expectedEntity = EntityV1.builder().id(ModelId.of("dead-feed")).data(objectMapper.convertValue(grokPatternEntity, JsonNode.class)).type(ModelTypes.GROK_PATTERN_V1).build();
    final EntityDescriptor entityDescriptor = expectedEntity.toEntityDescriptor();
    final Map<EntityDescriptor, Entity> entities = new HashMap<>(1);
    entities.put(entityDescriptor, expectedEntity);
    Graph<Entity> graph = facade.resolveForInstallation(entity, Collections.emptyMap(), entities);
    assertThat(graph.nodes()).contains(expectedEntity);
}
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) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) RawUDPInput(org.graylog2.inputs.raw.udp.RawUDPInput) Input(org.graylog2.inputs.Input) FakeHttpMessageInput(org.graylog2.inputs.random.FakeHttpMessageInput) MessageInput(org.graylog2.plugin.inputs.MessageInput) GrokExtractor(org.graylog2.inputs.extractors.GrokExtractor) ExtractorEntity(org.graylog2.contentpacks.model.entities.ExtractorEntity) InputEntity(org.graylog2.contentpacks.model.entities.InputEntity) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 4 with ExtractorEntity

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

the class InputFacade method createExtractors.

private List<Extractor> createExtractors(final Input input, final List<ExtractorEntity> extractorEntities, final String username, final Map<String, ValueReference> parameters) throws Extractor.ReservedFieldException, org.graylog2.ConfigurationException, ExtractorFactory.NoSuchExtractorException, ValidationException {
    final ImmutableList.Builder<Extractor> result = ImmutableList.builder();
    for (ExtractorEntity extractorEntity : extractorEntities) {
        final List<Converter> converters = createConverters(extractorEntity.converters(), parameters);
        final Extractor extractor = addExtractor(input, extractorEntity.title().asString(parameters), extractorEntity.order().asInteger(parameters), extractorEntity.cursorStrategy().asEnum(parameters, Extractor.CursorStrategy.class), extractorEntity.type().asEnum(parameters, Extractor.Type.class), extractorEntity.sourceField().asString(parameters), extractorEntity.targetField().asString(parameters), toValueMap(extractorEntity.configuration(), parameters), converters, extractorEntity.conditionType().asEnum(parameters, Extractor.ConditionType.class), extractorEntity.conditionValue().asString(parameters), username);
        result.add(extractor);
    }
    return result.build();
}
Also used : ModelType(org.graylog2.contentpacks.model.ModelType) ImmutableList(com.google.common.collect.ImmutableList) ExtractorEntity(org.graylog2.contentpacks.model.entities.ExtractorEntity) Converter(org.graylog2.plugin.inputs.Converter) Extractor(org.graylog2.plugin.inputs.Extractor) GrokExtractor(org.graylog2.inputs.extractors.GrokExtractor) LookupTableExtractor(org.graylog2.inputs.extractors.LookupTableExtractor)

Aggregations

ExtractorEntity (org.graylog2.contentpacks.model.entities.ExtractorEntity)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 ConverterEntity (org.graylog2.contentpacks.model.entities.ConverterEntity)3 Entity (org.graylog2.contentpacks.model.entities.Entity)3 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)3 GrokPatternEntity (org.graylog2.contentpacks.model.entities.GrokPatternEntity)3 InputEntity (org.graylog2.contentpacks.model.entities.InputEntity)3 LookupTableEntity (org.graylog2.contentpacks.model.entities.LookupTableEntity)3 NativeEntity (org.graylog2.contentpacks.model.entities.NativeEntity)3 ImmutableList (com.google.common.collect.ImmutableList)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)2 ModelType (org.graylog2.contentpacks.model.ModelType)2 Input (org.graylog2.inputs.Input)2 GrokExtractor (org.graylog2.inputs.extractors.GrokExtractor)2 LookupTableExtractor (org.graylog2.inputs.extractors.LookupTableExtractor)2 FakeHttpMessageInput (org.graylog2.inputs.random.FakeHttpMessageInput)2 RawUDPInput (org.graylog2.inputs.raw.udp.RawUDPInput)2 MessageInput (org.graylog2.plugin.inputs.MessageInput)2