Search in sources :

Example 1 with ContentPack

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

the class ContentPackService method getUninstallDetails.

private ContentPackUninstallDetails getUninstallDetails(ContentPackV1 contentPack, ContentPackInstallation installation) {
    final Entity rootEntity = EntityV1.createRoot(contentPack);
    final ImmutableMap<String, ValueReference> parameters = installation.parameters();
    final ImmutableGraph<Entity> dependencyGraph = buildEntityGraph(rootEntity, contentPack.entities(), parameters);
    final Traverser<Entity> entityTraverser = Traverser.forGraph(dependencyGraph);
    final Iterable<Entity> entitiesInOrder = entityTraverser.breadthFirst(rootEntity);
    final Set<NativeEntityDescriptor> nativeEntityDescriptors = new HashSet<>();
    entitiesInOrder.forEach((entity -> {
        if (entity.equals(rootEntity)) {
            return;
        }
        final Optional<NativeEntityDescriptor> nativeEntityDescriptorOptional = installation.entities().stream().filter(descriptor -> entity.id().equals(descriptor.contentPackEntityId())).findFirst();
        if (nativeEntityDescriptorOptional.isPresent()) {
            NativeEntityDescriptor nativeEntityDescriptor = nativeEntityDescriptorOptional.get();
            if (contentPackInstallationPersistenceService.countInstallationOfEntityById(nativeEntityDescriptor.id()) <= 1) {
                nativeEntityDescriptors.add(nativeEntityDescriptor);
            }
        }
    }));
    return ContentPackUninstallDetails.create(nativeEntityDescriptors);
}
Also used : ElementOrder(com.google.common.graph.ElementOrder) Graphs(org.graylog2.utilities.Graphs) Constraint(org.graylog2.contentpacks.model.constraints.Constraint) ImmutableGraph(com.google.common.graph.ImmutableGraph) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) ContentPack(org.graylog2.contentpacks.model.ContentPack) LoggerFactory(org.slf4j.LoggerFactory) ConstraintCheckResult(org.graylog2.contentpacks.model.constraints.ConstraintCheckResult) ModelType(org.graylog2.contentpacks.model.ModelType) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference) LegacyContentPack(org.graylog2.contentpacks.model.LegacyContentPack) Map(java.util.Map) FailedConstraintsException(org.graylog2.contentpacks.exceptions.FailedConstraintsException) InvalidParameterTypeException(org.graylog2.contentpacks.exceptions.InvalidParameterTypeException) MissingParametersException(org.graylog2.contentpacks.exceptions.MissingParametersException) UnsupportedEntityFacade(org.graylog2.contentpacks.facades.UnsupportedEntityFacade) ImmutableSet(com.google.common.collect.ImmutableSet) ModelId(org.graylog2.contentpacks.model.ModelId) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) MutableGraph(com.google.common.graph.MutableGraph) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) Collection(java.util.Collection) EntityWithExcerptFacade(org.graylog2.contentpacks.facades.EntityWithExcerptFacade) Set(java.util.Set) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) GraphBuilder(com.google.common.graph.GraphBuilder) InvalidParametersException(org.graylog2.contentpacks.exceptions.InvalidParametersException) Parameter(org.graylog2.contentpacks.model.parameters.Parameter) Optional(java.util.Optional) ConstraintChecker(org.graylog2.contentpacks.constraints.ConstraintChecker) ContentPackUninstallDetails(org.graylog2.contentpacks.model.ContentPackUninstallDetails) EmptyDefaultValueException(org.graylog2.contentpacks.exceptions.EmptyDefaultValueException) ContentPackV1(org.graylog2.contentpacks.model.ContentPackV1) Entity(org.graylog2.contentpacks.model.entities.Entity) HashMap(java.util.HashMap) ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) ValueType(org.graylog2.contentpacks.model.entities.references.ValueType) Singleton(javax.inject.Singleton) Function(java.util.function.Function) Inject(javax.inject.Inject) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) ImmutableList(com.google.common.collect.ImmutableList) EntityExcerpt(org.graylog2.contentpacks.model.entities.EntityExcerpt) EntityFacade(org.graylog2.contentpacks.facades.EntityFacade) Logger(org.slf4j.Logger) ContentPackUninstallation(org.graylog2.contentpacks.model.ContentPackUninstallation) ContentPackInstallation(org.graylog2.contentpacks.model.ContentPackInstallation) UnexpectedEntitiesException(org.graylog2.contentpacks.exceptions.UnexpectedEntitiesException) EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) Traverser(com.google.common.graph.Traverser) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) Collections(java.util.Collections) Graph(com.google.common.graph.Graph) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) Optional(java.util.Optional) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference) HashSet(java.util.HashSet)

Example 2 with ContentPack

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

the class ContentPackPersistenceService method loadAllLatest.

public Set<ContentPack> loadAllLatest() {
    final Set<ContentPack> allContentPacks = loadAll();
    final ImmutableMultimap.Builder<ModelId, ContentPack> byIdBuilder = ImmutableMultimap.builder();
    for (ContentPack contentPack : allContentPacks) {
        byIdBuilder.put(contentPack.id(), contentPack);
    }
    final ImmutableMultimap<ModelId, ContentPack> contentPacksById = byIdBuilder.build();
    final ImmutableSet.Builder<ContentPack> latestContentPacks = ImmutableSet.builderWithExpectedSize(contentPacksById.keySet().size());
    for (ModelId id : contentPacksById.keySet()) {
        final ImmutableCollection<ContentPack> contentPacks = contentPacksById.get(id);
        final ContentPack latestContentPackRevision = Collections.max(contentPacks, Comparator.comparingInt(Revisioned::revision));
        latestContentPacks.add(latestContentPackRevision);
    }
    return latestContentPacks.build();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) ContentPack(org.graylog2.contentpacks.model.ContentPack) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) ModelId(org.graylog2.contentpacks.model.ModelId)

Example 3 with ContentPack

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

the class ContentPackPersistenceService method filterMissingResourcesAndInsert.

public Optional<ContentPack> filterMissingResourcesAndInsert(final ContentPack pack) {
    ContentPackV1 cpv1 = (ContentPackV1) pack;
    final Set<String> allStreams = streamService.loadAll().stream().map(stream -> stream.getTitle()).collect(Collectors.toSet());
    final Map<String, String> streamsInContentPack = new HashMap<>();
    cpv1.entities().stream().filter(entity -> "stream".equals(entity.type().name()) && "1".equals(entity.type().version())).map(entity -> new Tuple2<String, JsonNode>(entity.id().id(), ((EntityV1) entity).data().findValue("title"))).forEach(tuple2 -> {
        JsonNode title = tuple2.v2().findValue("@value");
        streamsInContentPack.put(tuple2.v1(), title.textValue());
    });
    cpv1.entities().stream().filter(entity -> "dashboard".equals(entity.type().name()) && "2".equals(entity.type().version())).map(entity -> ((EntityV1) entity).data().findValue("search")).map(node -> node.findValue("queries")).map(node -> node.findValue("search_types")).forEach(node -> {
        final ObjectNode parent = (ObjectNode) node.findParent("streams");
        final ArrayNode streams = (ArrayNode) node.findValue("streams");
        if (streams != null) {
            final ArrayNode filtered = streams.deepCopy();
            filtered.removeAll();
            streams.forEach(stream -> {
                final String sid = stream.textValue();
                final String stitle = streamsInContentPack.get(sid);
                if (allStreams.contains(stitle))
                    filtered.add(stream);
            });
            parent.replace("streams", filtered);
        }
    });
    return this.insert(cpv1);
}
Also used : DuplicateKeyException(com.mongodb.DuplicateKeyException) Identified(org.graylog2.contentpacks.model.Identified) ContentPack(org.graylog2.contentpacks.model.ContentPack) ContentPackV1(org.graylog2.contentpacks.model.ContentPackV1) LoggerFactory(org.slf4j.LoggerFactory) ImmutableCollection(com.google.common.collect.ImmutableCollection) HashMap(java.util.HashMap) Singleton(javax.inject.Singleton) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Revisioned(org.graylog2.contentpacks.model.Revisioned) WriteResult(org.mongojack.WriteResult) Inject(javax.inject.Inject) HashSet(java.util.HashSet) Tuple2(org.jooq.lambda.tuple.Tuple2) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) NotFoundException(org.graylog2.database.NotFoundException) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) ModelId(org.graylog2.contentpacks.model.ModelId) DBCursor(org.mongojack.DBCursor) BasicDBObject(com.mongodb.BasicDBObject) MongoJackObjectMapperProvider(org.graylog2.bindings.providers.MongoJackObjectMapperProvider) JacksonDBCollection(org.mongojack.JacksonDBCollection) Set(java.util.Set) DBQuery(org.mongojack.DBQuery) Collectors(java.util.stream.Collectors) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) StreamService(org.graylog2.streams.StreamService) ObjectId(org.bson.types.ObjectId) Optional(java.util.Optional) MongoConnection(org.graylog2.database.MongoConnection) Comparator(java.util.Comparator) Collections(java.util.Collections) EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HashMap(java.util.HashMap) Tuple2(org.jooq.lambda.tuple.Tuple2) ContentPackV1(org.graylog2.contentpacks.model.ContentPackV1) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 4 with ContentPack

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

the class ContentPackLoaderPeriodical method doRun.

@Override
public void doRun() {
    if (!Files.exists(contentPacksDir.toAbsolutePath())) {
        LOG.warn("Could not find content packs directory {}. Please check your graylog configuration", contentPacksDir.toAbsolutePath());
        return;
    }
    final List<Path> files = getFiles(contentPacksDir);
    final Map<String, ContentPack> contentPacks = new HashMap<>(files.size());
    for (Path file : files) {
        final String fileName = file.getFileName().toString();
        LOG.debug("Reading content pack from {}", file);
        final byte[] bytes;
        try {
            bytes = Files.readAllBytes(file);
        } catch (IOException e) {
            LOG.warn("Couldn't read " + file + ". Skipping.", e);
            continue;
        }
        LOG.debug("Parsing content pack from {}", file);
        final ContentPack contentPack;
        try {
            contentPack = objectMapper.readValue(bytes, ContentPack.class);
        } catch (IOException e) {
            LOG.warn("Couldn't parse content pack in file " + file + ". Skipping", e);
            continue;
        }
        if (contentPack instanceof ContentPackV1) {
            ContentPackV1 contentPackV1 = (ContentPackV1) contentPack;
            if (contentPackV1.parameters().asList().size() > 0) {
                LOG.warn("Cannot accept content packs with parameters. Content Pack {}/{} rejected", contentPack.id(), contentPack.revision());
            }
        }
        final Optional<ContentPack> existingContentPack = contentPackPersistenceService.findByIdAndRevision(contentPack.id(), contentPack.revision());
        if (existingContentPack.isPresent()) {
            LOG.debug("Content pack {}/{} already exists in database. Skipping.", contentPack.id(), contentPack.revision());
            contentPacks.put(fileName, existingContentPack.get());
            continue;
        }
        final Optional<ContentPack> insertedContentPack = contentPackPersistenceService.insert(contentPack);
        if (!insertedContentPack.isPresent()) {
            LOG.error("Error while inserting content pack " + file + " into database. Skipping.");
            continue;
        }
        contentPacks.put(fileName, insertedContentPack.get());
    }
    LOG.debug("Applying selected content packs");
    for (Map.Entry<String, ContentPack> entry : contentPacks.entrySet()) {
        final String fileName = entry.getKey();
        final ContentPack contentPack = entry.getValue();
        if (contentPacksAutoInstall.contains(fileName)) {
            if (!contentPackInstallationPersistenceService.findByContentPackIdAndRevision(contentPack.id(), contentPack.revision()).isEmpty()) {
                LOG.debug("Content pack {}/{} ({}) already applied. Skipping.", contentPack.id(), contentPack.revision(), fileName);
                continue;
            }
            contentPackService.installContentPack(contentPack, Collections.emptyMap(), "Installed by auto loader", configuration.getRootUsername());
        }
    }
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) ContentPack(org.graylog2.contentpacks.model.ContentPack) ContentPackV1(org.graylog2.contentpacks.model.ContentPackV1) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with ContentPack

use of org.graylog2.contentpacks.model.ContentPack 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)

Aggregations

ContentPack (org.graylog2.contentpacks.model.ContentPack)21 Test (org.junit.Test)11 ContentPackInstallation (org.graylog2.contentpacks.model.ContentPackInstallation)10 HashMap (java.util.HashMap)7 ContentPackV1 (org.graylog2.contentpacks.model.ContentPackV1)7 ModelId (org.graylog2.contentpacks.model.ModelId)7 Timed (com.codahale.metrics.annotation.Timed)6 JsonView (com.fasterxml.jackson.annotation.JsonView)6 ApiOperation (io.swagger.annotations.ApiOperation)6 ApiResponses (io.swagger.annotations.ApiResponses)6 ImmutableSet (com.google.common.collect.ImmutableSet)5 URL (java.net.URL)5 Map (java.util.Map)5 NotFoundException (javax.ws.rs.NotFoundException)5 Path (javax.ws.rs.Path)5 ContentPackException (org.graylog2.contentpacks.exceptions.ContentPackException)5 Entity (org.graylog2.contentpacks.model.entities.Entity)5 ObjectId (org.bson.types.ObjectId)4 EntityWithExcerptFacade (org.graylog2.contentpacks.facades.EntityWithExcerptFacade)4 ContentPackUninstallation (org.graylog2.contentpacks.model.ContentPackUninstallation)4