Search in sources :

Example 1 with ContentPackV1

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

the class ContentPackService method checkConstraintsV1.

private Set<ConstraintCheckResult> checkConstraintsV1(ContentPackV1 contentPackV1) {
    Set<Constraint> requiredConstraints = contentPackV1.constraints();
    final Set<ConstraintCheckResult> fulfilledConstraints = new HashSet<>();
    for (ConstraintChecker constraintChecker : constraintCheckers) {
        fulfilledConstraints.addAll(constraintChecker.checkConstraints(requiredConstraints));
    }
    return fulfilledConstraints;
}
Also used : ConstraintCheckResult(org.graylog2.contentpacks.model.constraints.ConstraintCheckResult) Constraint(org.graylog2.contentpacks.model.constraints.Constraint) ConstraintChecker(org.graylog2.contentpacks.constraints.ConstraintChecker) HashSet(java.util.HashSet)

Example 2 with ContentPackV1

use of org.graylog2.contentpacks.model.ContentPackV1 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 3 with ContentPackV1

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

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

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

the class V20191219090834_AddSourcesPageTest method upgradeFailsBecauseSourcePageContentPackExists.

@Test
public void upgradeFailsBecauseSourcePageContentPackExists() throws IOException {
    final URL contentPackURL = V20191219090834_AddSourcesPage.class.getResource("V20191219090834_AddSourcesPage_Content_Pack.json");
    final ContentPack contentPack = ContentPackV1.builder().id(ModelId.of("04fcf179-49e0-4e8f-9c02-0ff13062efbe")).summary("summary").revision(1).name("Sources Page").description("description").vendor("").url(URI.create("http://graylog.com")).entities(ImmutableSet.of()).build();
    final ContentPackInstallation contentPackInstallation = ContentPackInstallation.builder().contentPackId(contentPack.id()).contentPackRevision(1).parameters(ImmutableMap.of()).entities(ImmutableSet.of()).comment("Comment").createdAt(Instant.now()).createdBy("admin").build();
    when(configService.get(V20191219090834_AddSourcesPage.MigrationCompleted.class)).thenReturn(null);
    when(objectMapper.readValue(contentPackURL, ContentPack.class)).thenReturn(contentPack);
    when(contentPackPersistenceService.insert(contentPack)).thenReturn(Optional.empty());
    assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> migration.upgrade()).withMessage("Could not install Source Page Content Pack.");
    verify(configService).write(V20191219090834_AddSourcesPage.MigrationCompleted.create("04fcf179-49e0-4e8f-9c02-0ff13062efbe"));
    verifyZeroInteractions(contentPackService);
}
Also used : ContentPackInstallation(org.graylog2.contentpacks.model.ContentPackInstallation) ContentPack(org.graylog2.contentpacks.model.ContentPack) URL(java.net.URL) Test(org.junit.Test)

Aggregations

ContentPack (org.graylog2.contentpacks.model.ContentPack)9 HashMap (java.util.HashMap)7 ContentPackV1 (org.graylog2.contentpacks.model.ContentPackV1)7 ContentPackInstallation (org.graylog2.contentpacks.model.ContentPackInstallation)5 Entity (org.graylog2.contentpacks.model.entities.Entity)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 HashSet (java.util.HashSet)4 Map (java.util.Map)4 EntityWithExcerptFacade (org.graylog2.contentpacks.facades.EntityWithExcerptFacade)4 NativeEntityDescriptor (org.graylog2.contentpacks.model.entities.NativeEntityDescriptor)4 Collections (java.util.Collections)3 Optional (java.util.Optional)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 ConstraintChecker (org.graylog2.contentpacks.constraints.ConstraintChecker)3 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)3 EntityV1 (org.graylog2.contentpacks.model.entities.EntityV1)3 NativeEntity (org.graylog2.contentpacks.model.entities.NativeEntity)3 Test (org.junit.Test)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2