Search in sources :

Example 1 with ContentPackException

use of org.graylog2.contentpacks.exceptions.ContentPackException in project graylog2-server by Graylog2.

the class EventNotificationHandlerConfigEntity method toNativeEntity.

@Override
public EventNotificationHandler.Config toNativeEntity(Map<String, ValueReference> parameters, Map<EntityDescriptor, Object> natvieEntities) {
    String notificationId = notificationId().asString(parameters);
    final EntityDescriptor notificationDescriptor = EntityDescriptor.create(notificationId, ModelTypes.NOTIFICATION_V1);
    final Object notification = natvieEntities.get(notificationDescriptor);
    final EventNotificationHandler.Config.Builder configBuilder = EventNotificationHandler.Config.builder();
    if (notification == null) {
        throw new ContentPackException("Missing notification (" + notificationId + ") for event definition");
    } else if (notification instanceof NotificationDto) {
        NotificationDto notificationDto = (NotificationDto) notification;
        configBuilder.notificationId(notificationDto.id());
    } else {
        throw new ContentPackException("Invalid type for notification (" + notificationId + ") of event definition: " + notification.getClass());
    }
    return configBuilder.notificationParameters(notificationParameters().orElse(null)).build();
}
Also used : ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NotificationDto(org.graylog.events.notifications.NotificationDto)

Example 2 with ContentPackException

use of org.graylog2.contentpacks.exceptions.ContentPackException in project graylog2-server by Graylog2.

the class LookupTableFacade method resolveForInstallation.

private Graph<Entity> resolveForInstallation(EntityV1 entity, Map<String, ValueReference> parameters, Map<EntityDescriptor, Entity> entities) {
    final MutableGraph<Entity> mutableGraph = GraphBuilder.directed().build();
    mutableGraph.addNode(entity);
    final LookupTableEntity lookupTableEntity = objectMapper.convertValue(entity.data(), LookupTableEntity.class);
    final String dataAdapterName = lookupTableEntity.dataAdapterName().asString(parameters);
    final EntityDescriptor dataAdapterDescriptor = adapterDescriptor(dataAdapterName);
    final Entity dataAdapterEntity = entities.get(dataAdapterDescriptor);
    if (dataAdapterEntity == null) {
        throw new ContentPackException("Missing data adapter \"" + dataAdapterName + "\" for lookup table " + entity.toEntityDescriptor());
    } else {
        mutableGraph.putEdge(entity, dataAdapterEntity);
    }
    final String cacheName = lookupTableEntity.cacheName().asString(parameters);
    final EntityDescriptor cacheDescriptor = cacheDescriptor(cacheName);
    final Entity cacheEntity = entities.get(cacheDescriptor);
    if (cacheEntity == null) {
        throw new ContentPackException("Missing cache \"" + cacheName + "\" for lookup table " + entity.toEntityDescriptor());
    } else {
        mutableGraph.putEdge(entity, cacheEntity);
    }
    return ImmutableGraph.copyOf(mutableGraph);
}
Also used : ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) LookupTableEntity(org.graylog2.contentpacks.model.entities.LookupTableEntity) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) LookupTableEntity(org.graylog2.contentpacks.model.entities.LookupTableEntity)

Example 3 with ContentPackException

use of org.graylog2.contentpacks.exceptions.ContentPackException in project graylog2-server by Graylog2.

the class LookupTableFacade method exportNativeEntity.

@VisibleForTesting
Entity exportNativeEntity(LookupTableDto lookupTableDto, EntityDescriptorIds entityDescriptorIds) {
    final String tableId = entityDescriptorIds.get(EntityDescriptor.create(lookupTableDto.id(), ModelTypes.LOOKUP_TABLE_V1)).orElseThrow(() -> new ContentPackException("Couldn't find lookup table entity " + lookupTableDto.id()));
    final String cacheId = entityDescriptorIds.get(cacheDescriptor(lookupTableDto.cacheId())).orElseThrow(() -> new ContentPackException("Couldn't find lookup cache entity " + lookupTableDto.cacheId()));
    final String adapterId = entityDescriptorIds.get(adapterDescriptor(lookupTableDto.dataAdapterId())).orElseThrow(() -> new ContentPackException("Couldn't find lookup data adapter entity " + lookupTableDto.dataAdapterId()));
    final LookupTableEntity lookupTableEntity = LookupTableEntity.create(ValueReference.of(lookupTableDto.name()), ValueReference.of(lookupTableDto.title()), ValueReference.of(lookupTableDto.description()), ValueReference.of(cacheId), ValueReference.of(adapterId), ValueReference.of(lookupTableDto.defaultSingleValue()), ValueReference.of(lookupTableDto.defaultSingleValueType()), ValueReference.of(lookupTableDto.defaultMultiValue()), ValueReference.of(lookupTableDto.defaultMultiValueType()));
    final JsonNode data = objectMapper.convertValue(lookupTableEntity, JsonNode.class);
    return EntityV1.builder().id(ModelId.of(tableId)).type(ModelTypes.LOOKUP_TABLE_V1).data(data).build();
}
Also used : ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) LookupTableEntity(org.graylog2.contentpacks.model.entities.LookupTableEntity) JsonNode(com.fasterxml.jackson.databind.JsonNode) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with ContentPackException

use of org.graylog2.contentpacks.exceptions.ContentPackException in project graylog2-server by Graylog2.

the class V20180924111644_AddDefaultGrokPatterns method upgrade.

@Override
public void upgrade() {
    if (configService.get(MigrationCompleted.class) != null) {
        LOG.debug("Migration already completed.");
        return;
    }
    try {
        final URL contentPackURL = V20180924111644_AddDefaultGrokPatterns.class.getResource("V20180924111644_AddDefaultGrokPatterns_Default_Grok_Patterns.json");
        final ContentPack contentPack = this.objectMapper.readValue(contentPackURL, ContentPack.class);
        final ContentPack pack = this.contentPackPersistenceService.insert(contentPack).orElseThrow(() -> {
            configService.write(MigrationCompleted.create(contentPack.id().toString()));
            return new ContentPackException("Content pack " + contentPack.id() + " with this revision " + contentPack.revision() + " already found!");
        });
        try {
            contentPackService.installContentPack(pack, Collections.emptyMap(), "Add default Grok patterns", "admin");
        } catch (ContentPackException e) {
            LOG.warn("Could not install default grok patterns: the installation found some modified default grok" + "patterns in your setup and did not update them. If you wish to use the default grok" + "patterns we provide, please delete the modified grok pattern and install the 'Default grok" + "patterns' content pack manually.");
        }
        configService.write(MigrationCompleted.create(pack.id().toString()));
    } catch (IOException e) {
        LOG.error("Unable to import content pack for default grok patterns: {}", e);
    }
}
Also used : ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) ContentPack(org.graylog2.contentpacks.model.ContentPack) IOException(java.io.IOException) URL(java.net.URL)

Example 5 with ContentPackException

use of org.graylog2.contentpacks.exceptions.ContentPackException in project graylog2-server by Graylog2.

the class StreamFacade method decode.

private NativeEntity<Stream> decode(EntityV1 entity, Map<String, ValueReference> parameters, Map<EntityDescriptor, Object> nativeEntities, User user) {
    final StreamEntity streamEntity = objectMapper.convertValue(entity.data(), StreamEntity.class);
    final CreateStreamRequest createStreamRequest = CreateStreamRequest.create(streamEntity.title().asString(parameters), streamEntity.description().asString(parameters), // ignored
    null, null, streamEntity.matchingType().asString(parameters), streamEntity.removeMatches().asBoolean(parameters), indexSetService.getDefault().id());
    final Stream stream = streamService.create(createStreamRequest, user.getName());
    final List<StreamRule> streamRules = streamEntity.streamRules().stream().map(streamRuleEntity -> createStreamRuleRequest(streamRuleEntity, parameters)).map(request -> streamRuleService.create(DUMMY_STREAM_ID, request)).collect(Collectors.toList());
    // TODO: The creation of legacy alert conditions should be avoided and a new event definition should be created instead
    final List<AlertCondition> alertConditions = streamEntity.alertConditions().stream().map(alertCondition -> createStreamAlertConditionRequest(alertCondition, parameters)).map(request -> {
        try {
            return streamAlertService.fromRequest(request, stream, user.getName());
        } catch (ConfigurationException e) {
            throw new ContentPackException("Couldn't create entity " + entity.toEntityDescriptor(), e);
        }
    }).collect(Collectors.toList());
    // TODO: The creation of legacy alarm callback should be avoided and a new event notification should be created instead
    final List<AlarmCallbackConfiguration> alarmCallbacks = streamEntity.alarmCallbacks().stream().map(alarmCallback -> createStreamAlarmCallbackRequest(alarmCallback, parameters)).map(request -> alarmCallbackConfigurationService.create(stream.getId(), request, user.getName())).collect(Collectors.toList());
    final String savedStreamId;
    try {
        savedStreamId = streamService.saveWithRulesAndOwnership(stream, streamRules, user);
        for (final AlertCondition alertCondition : alertConditions) {
            streamService.addAlertCondition(stream, alertCondition);
        }
        for (final AlarmCallbackConfiguration alarmCallback : alarmCallbacks) {
            alarmCallbackConfigurationService.save(alarmCallback);
        }
    } catch (ValidationException e) {
        throw new ContentPackException("Couldn't create entity " + entity.toEntityDescriptor(), e);
    }
    final Set<ObjectId> outputIds = streamEntity.outputs().stream().map(valueReference -> valueReference.asString(parameters)).map(ModelId::of).map(modelId -> EntityDescriptor.create(modelId, ModelTypes.OUTPUT_V1)).map(descriptor -> findOutput(descriptor, nativeEntities)).map(Output::getId).map(ObjectId::new).collect(Collectors.toSet());
    streamService.addOutputs(new ObjectId(savedStreamId), outputIds);
    if (!alertConditions.isEmpty() || !alarmCallbacks.isEmpty()) {
        // TODO: Remove migration call once we updated the above code to directly create event definitions and notifications
        try {
            legacyAlertsMigration.upgrade();
        } catch (Exception e) {
            LOG.error("Couldn't run migration for newly created legacy alert conditions and/or alarm callbacks", e);
        }
    }
    return NativeEntity.create(entity.id(), savedStreamId, TYPE_V1, stream.getTitle(), stream);
}
Also used : ImmutableGraph(com.google.common.graph.ImmutableGraph) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) LoggerFactory(org.slf4j.LoggerFactory) CreateStreamRuleRequest(org.graylog2.rest.resources.streams.rules.requests.CreateStreamRuleRequest) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration) AlertService(org.graylog2.alerts.AlertService) StreamRule(org.graylog2.plugin.streams.StreamRule) ModelType(org.graylog2.contentpacks.model.ModelType) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference) StreamRuleService(org.graylog2.streams.StreamRuleService) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) ModelId(org.graylog2.contentpacks.model.ModelId) MutableGraph(com.google.common.graph.MutableGraph) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) StreamAlarmCallbackEntity(org.graylog2.contentpacks.model.entities.StreamAlarmCallbackEntity) Set(java.util.Set) Collectors(java.util.stream.Collectors) GraphBuilder(com.google.common.graph.GraphBuilder) StreamRuleType(org.graylog2.plugin.streams.StreamRuleType) Objects(java.util.Objects) CreateStreamRequest(org.graylog2.rest.resources.streams.requests.CreateStreamRequest) CreateAlarmCallbackRequest(org.graylog2.rest.models.alarmcallbacks.requests.CreateAlarmCallbackRequest) List(java.util.List) IndexSetService(org.graylog2.indexer.indexset.IndexSetService) UserService(org.graylog2.shared.users.UserService) Stream(org.graylog2.plugin.streams.Stream) StreamService(org.graylog2.streams.StreamService) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) CreateConditionRequest(org.graylog2.rest.models.streams.alerts.requests.CreateConditionRequest) Optional(java.util.Optional) ModelTypes(org.graylog2.contentpacks.model.ModelTypes) EntityDescriptorIds(org.graylog2.contentpacks.EntityDescriptorIds) Strings.nullToEmpty(com.google.common.base.Strings.nullToEmpty) Entity(org.graylog2.contentpacks.model.entities.Entity) ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) StreamAlertConditionEntity(org.graylog2.contentpacks.model.entities.StreamAlertConditionEntity) Inject(javax.inject.Inject) ReferenceMapUtils(org.graylog2.contentpacks.model.entities.references.ReferenceMapUtils) V20190722150700_LegacyAlertConditionMigration(org.graylog.events.legacy.V20190722150700_LegacyAlertConditionMigration) EntityExcerpt(org.graylog2.contentpacks.model.entities.EntityExcerpt) ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) NotFoundException(org.graylog2.database.NotFoundException) Logger(org.slf4j.Logger) StreamEntity(org.graylog2.contentpacks.model.entities.StreamEntity) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StreamRuleEntity(org.graylog2.contentpacks.model.entities.StreamRuleEntity) AlarmCallbackConfigurationService(org.graylog2.alarmcallbacks.AlarmCallbackConfigurationService) EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) Output(org.graylog2.plugin.streams.Output) ValidationException(org.graylog2.plugin.database.ValidationException) ObjectId(org.bson.types.ObjectId) VisibleForTesting(com.google.common.annotations.VisibleForTesting) User(org.graylog2.plugin.database.users.User) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) Collections(java.util.Collections) Graph(com.google.common.graph.Graph) ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) ValidationException(org.graylog2.plugin.database.ValidationException) ObjectId(org.bson.types.ObjectId) StreamRule(org.graylog2.plugin.streams.StreamRule) StreamEntity(org.graylog2.contentpacks.model.entities.StreamEntity) ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) NotFoundException(org.graylog2.database.NotFoundException) ValidationException(org.graylog2.plugin.database.ValidationException) ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) CreateStreamRequest(org.graylog2.rest.resources.streams.requests.CreateStreamRequest) Output(org.graylog2.plugin.streams.Output) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)

Aggregations

ContentPackException (org.graylog2.contentpacks.exceptions.ContentPackException)9 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)5 Entity (org.graylog2.contentpacks.model.entities.Entity)4 NativeEntity (org.graylog2.contentpacks.model.entities.NativeEntity)4 NativeEntityDescriptor (org.graylog2.contentpacks.model.entities.NativeEntityDescriptor)4 ValueReference (org.graylog2.contentpacks.model.entities.references.ValueReference)4 ImmutableSet (com.google.common.collect.ImmutableSet)3 Collections (java.util.Collections)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Graph (com.google.common.graph.Graph)2 GraphBuilder (com.google.common.graph.GraphBuilder)2 ImmutableGraph (com.google.common.graph.ImmutableGraph)2 MutableGraph (com.google.common.graph.MutableGraph)2 Traverser (com.google.common.graph.Traverser)2 IOException (java.io.IOException)2