Search in sources :

Example 16 with ContentPackInstallation

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

the class ContentPackService method installContentPack.

private ContentPackInstallation installContentPack(ContentPackV1 contentPack, Map<String, ValueReference> parameters, String comment, String user) {
    ensureConstraints(contentPack.constraints());
    final Entity rootEntity = EntityV1.createRoot(contentPack);
    final ImmutableMap<String, ValueReference> validatedParameters = validateParameters(parameters, contentPack.parameters());
    final ImmutableGraph<Entity> dependencyGraph = buildEntityGraph(rootEntity, contentPack.entities(), validatedParameters);
    final Traverser<Entity> entityTraverser = Traverser.forGraph(dependencyGraph);
    final Iterable<Entity> entitiesInOrder = entityTraverser.depthFirstPostOrder(rootEntity);
    // Insertion order is important for created entities so we can roll back in order!
    final Map<EntityDescriptor, Object> createdEntities = new LinkedHashMap<>();
    final Map<EntityDescriptor, Object> allEntities = new HashMap<>();
    final ImmutableSet.Builder<NativeEntityDescriptor> allEntityDescriptors = ImmutableSet.builder();
    try {
        for (Entity entity : entitiesInOrder) {
            if (entity.equals(rootEntity)) {
                continue;
            }
            final EntityDescriptor entityDescriptor = entity.toEntityDescriptor();
            final EntityWithExcerptFacade facade = entityFacades.getOrDefault(entity.type(), UnsupportedEntityFacade.INSTANCE);
            @SuppressWarnings({ "rawtypes", "unchecked" }) final Optional<NativeEntity> existingEntity = facade.findExisting(entity, parameters);
            if (existingEntity.isPresent()) {
                LOG.trace("Found existing entity for {}", entityDescriptor);
                final NativeEntity<?> nativeEntity = existingEntity.get();
                final NativeEntityDescriptor nativeEntityDescriptor = nativeEntity.descriptor();
                /* Found entity on the system or we found a other installation which stated that */
                if (contentPackInstallationPersistenceService.countInstallationOfEntityById(nativeEntityDescriptor.id()) <= 0 || contentPackInstallationPersistenceService.countInstallationOfEntityByIdAndFoundOnSystem(nativeEntityDescriptor.id()) > 0) {
                    final NativeEntityDescriptor serverDescriptor = nativeEntityDescriptor.toBuilder().foundOnSystem(true).build();
                    allEntityDescriptors.add(serverDescriptor);
                } else {
                    allEntityDescriptors.add(nativeEntity.descriptor());
                }
                allEntities.put(entityDescriptor, nativeEntity.entity());
            } else {
                LOG.trace("Creating new entity for {}", entityDescriptor);
                final NativeEntity<?> createdEntity = facade.createNativeEntity(entity, validatedParameters, allEntities, user);
                allEntityDescriptors.add(createdEntity.descriptor());
                createdEntities.put(entityDescriptor, createdEntity.entity());
                allEntities.put(entityDescriptor, createdEntity.entity());
            }
        }
    } catch (Exception e) {
        rollback(createdEntities);
        throw new ContentPackException("Failed to install content pack <" + contentPack.id() + "/" + contentPack.revision() + ">", e);
    }
    final ContentPackInstallation installation = ContentPackInstallation.builder().contentPackId(contentPack.id()).contentPackRevision(contentPack.revision()).parameters(validatedParameters).comment(comment).entities(allEntityDescriptors.build()).createdAt(Instant.now()).createdBy(user).build();
    return contentPackInstallationPersistenceService.insert(installation);
}
Also used : ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FailedConstraintsException(org.graylog2.contentpacks.exceptions.FailedConstraintsException) InvalidParameterTypeException(org.graylog2.contentpacks.exceptions.InvalidParameterTypeException) MissingParametersException(org.graylog2.contentpacks.exceptions.MissingParametersException) InvalidParametersException(org.graylog2.contentpacks.exceptions.InvalidParametersException) EmptyDefaultValueException(org.graylog2.contentpacks.exceptions.EmptyDefaultValueException) ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) UnexpectedEntitiesException(org.graylog2.contentpacks.exceptions.UnexpectedEntitiesException) LinkedHashMap(java.util.LinkedHashMap) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) ContentPackInstallation(org.graylog2.contentpacks.model.ContentPackInstallation) ImmutableSet(com.google.common.collect.ImmutableSet) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) EntityWithExcerptFacade(org.graylog2.contentpacks.facades.EntityWithExcerptFacade) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference)

Example 17 with ContentPackInstallation

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

the class ContentPackService method uninstallContentPack.

private ContentPackUninstallation uninstallContentPack(ContentPackInstallation installation, ContentPackV1 contentPack) {
    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> removedEntities = new HashSet<>();
    final Set<NativeEntityDescriptor> failedEntities = new HashSet<>();
    final Set<NativeEntityDescriptor> skippedEntities = new HashSet<>();
    try {
        for (Entity entity : entitiesInOrder) {
            if (entity.equals(rootEntity)) {
                continue;
            }
            final Optional<NativeEntityDescriptor> nativeEntityDescriptorOptional = installation.entities().stream().filter(descriptor -> entity.id().equals(descriptor.contentPackEntityId())).findFirst();
            final EntityWithExcerptFacade facade = entityFacades.getOrDefault(entity.type(), UnsupportedEntityFacade.INSTANCE);
            if (nativeEntityDescriptorOptional.isPresent()) {
                final NativeEntityDescriptor nativeEntityDescriptor = nativeEntityDescriptorOptional.get();
                final Optional nativeEntityOptional = facade.loadNativeEntity(nativeEntityDescriptor);
                final ModelId entityId = nativeEntityDescriptor.id();
                final long installCount = contentPackInstallationPersistenceService.countInstallationOfEntityById(entityId);
                final long systemFoundCount = contentPackInstallationPersistenceService.countInstallationOfEntityByIdAndFoundOnSystem(entityId);
                if (installCount > 1 || (installCount == 1 && systemFoundCount >= 1)) {
                    skippedEntities.add(nativeEntityDescriptor);
                    LOG.debug("Did not remove entity since other content pack installations still use them: {}", nativeEntityDescriptor);
                } else if (nativeEntityOptional.isPresent()) {
                    final Object nativeEntity = nativeEntityOptional.get();
                    LOG.trace("Removing existing native entity for {} ({})", nativeEntityDescriptor);
                    try {
                        // The EntityFacade#delete() method expects the actual entity object
                        // noinspection unchecked
                        facade.delete(((NativeEntity) nativeEntity).entity());
                        removedEntities.add(nativeEntityDescriptor);
                    } catch (Exception e) {
                        LOG.warn("Couldn't remove native entity {}", nativeEntity);
                        failedEntities.add(nativeEntityDescriptor);
                    }
                } else {
                    LOG.trace("Couldn't find existing native entity for {} ({})", nativeEntityDescriptor);
                }
            }
        }
    } catch (Exception e) {
        throw new ContentPackException("Failed to remove content pack <" + contentPack.id() + "/" + contentPack.revision() + ">", e);
    }
    final int deletedInstallations = contentPackInstallationPersistenceService.deleteById(installation.id());
    LOG.debug("Deleted {} installation(s) of content pack {}", deletedInstallations, contentPack.id());
    return ContentPackUninstallation.builder().entities(ImmutableSet.copyOf(removedEntities)).skippedEntities(ImmutableSet.copyOf(skippedEntities)).failedEntities(ImmutableSet.copyOf(failedEntities)).build();
}
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) ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) Optional(java.util.Optional) FailedConstraintsException(org.graylog2.contentpacks.exceptions.FailedConstraintsException) InvalidParameterTypeException(org.graylog2.contentpacks.exceptions.InvalidParameterTypeException) MissingParametersException(org.graylog2.contentpacks.exceptions.MissingParametersException) InvalidParametersException(org.graylog2.contentpacks.exceptions.InvalidParametersException) EmptyDefaultValueException(org.graylog2.contentpacks.exceptions.EmptyDefaultValueException) ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) UnexpectedEntitiesException(org.graylog2.contentpacks.exceptions.UnexpectedEntitiesException) Constraint(org.graylog2.contentpacks.model.constraints.Constraint) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) EntityWithExcerptFacade(org.graylog2.contentpacks.facades.EntityWithExcerptFacade) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) ModelId(org.graylog2.contentpacks.model.ModelId) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference) HashSet(java.util.HashSet)

Example 18 with ContentPackInstallation

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

the class ContentPackInstallationPersistenceService method findByContentPackIds.

public Set<ContentPackInstallation> findByContentPackIds(Set<ModelId> ids) {
    final Set<String> stringIds = ids.stream().map(x -> x.toString()).collect(Collectors.toSet());
    final DBObject query = BasicDBObjectBuilder.start().push(ContentPackInstallation.FIELD_CONTENT_PACK_ID).append("$in", stringIds).get();
    final DBCursor<ContentPackInstallation> result = dbCollection.find(query);
    return ImmutableSet.copyOf((Iterable<ContentPackInstallation>) result);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) ModelId(org.graylog2.contentpacks.model.ModelId) Iterator(java.util.Iterator) BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) DBCursor(org.mongojack.DBCursor) BasicDBObject(com.mongodb.BasicDBObject) MongoJackObjectMapperProvider(org.graylog2.bindings.providers.MongoJackObjectMapperProvider) ContentPackInstallation(org.graylog2.contentpacks.model.ContentPackInstallation) JacksonDBCollection(org.mongojack.JacksonDBCollection) Set(java.util.Set) DBQuery(org.mongojack.DBQuery) HashMap(java.util.HashMap) Singleton(javax.inject.Singleton) Collectors(java.util.stream.Collectors) WriteResult(org.mongojack.WriteResult) Inject(javax.inject.Inject) DBObject(com.mongodb.DBObject) ContentPackMetadata(org.graylog2.rest.models.system.contentpacks.responses.ContentPackMetadata) Locale(java.util.Locale) Map(java.util.Map) ObjectId(org.bson.types.ObjectId) Optional(java.util.Optional) MongoConnection(org.graylog2.database.MongoConnection) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) ContentPackInstallation(org.graylog2.contentpacks.model.ContentPackInstallation) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Example 19 with ContentPackInstallation

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

the class ContentPackResource method installContentPack.

@POST
@Path("{contentPackId}/{revision}/installations")
@Timed
@ApiOperation(value = "Install a revision of a content pack")
@ApiResponses(value = { @ApiResponse(code = 500, message = "Error loading content packs") })
@AuditEvent(type = AuditEventTypes.CONTENT_PACK_INSTALL)
@JsonView(ContentPackView.HttpView.class)
public ContentPackInstallation installContentPack(@ApiParam(name = "contentPackId", value = "Content pack ID", required = true) @PathParam("contentPackId") ModelId id, @ApiParam(name = "revision", value = "Content pack revision", required = true) @PathParam("revision") int revision, @ApiParam(name = "installation request", value = "Content pack installation request", required = true) @Valid @NotNull ContentPackInstallationRequest contentPackInstallationRequest) {
    checkPermission(RestPermissions.CONTENT_PACK_INSTALL, id.toString());
    final ContentPack contentPack = contentPackPersistenceService.findByIdAndRevision(id, revision).orElseThrow(() -> new NotFoundException("Content pack " + id + " with revision " + revision + " not found!"));
    final User currentUser = getCurrentUser();
    final String userName = currentUser == null ? "unknown" : currentUser.getName();
    final ContentPackInstallation installation = contentPackService.installContentPack(contentPack, contentPackInstallationRequest.parameters(), contentPackInstallationRequest.comment(), userName);
    return installation;
}
Also used : User(org.graylog2.plugin.database.users.User) ContentPackInstallation(org.graylog2.contentpacks.model.ContentPackInstallation) ContentPack(org.graylog2.contentpacks.model.ContentPack) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) JsonView(com.fasterxml.jackson.annotation.JsonView) AuditEvent(org.graylog2.audit.jersey.AuditEvent) ApiResponses(io.swagger.annotations.ApiResponses)

Example 20 with ContentPackInstallation

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

the class ContentPackResource method uninstallDetails.

@GET
@Path("{contentPackId}/installations/{installationId}/uninstall_details")
@Timed
@ApiOperation(value = "Get details about which entities will actually be uninstalled")
@ApiResponses(value = { @ApiResponse(code = 500, message = "Error loading content packs") })
@JsonView(ContentPackView.HttpView.class)
public ContentPackUninstallDetails uninstallDetails(@ApiParam(name = "contentPackId", value = "Content pack ID", required = true) @PathParam("contentPackId") ModelId id, @ApiParam(name = "installationId", value = "Installation ID", required = true) @PathParam("installationId") String installationId) {
    final ContentPackInstallation installation = contentPackInstallationPersistenceService.findById(new ObjectId(installationId)).orElseThrow(() -> new NotFoundException("Couldn't find installation " + installationId));
    final ContentPack contentPack = contentPackPersistenceService.findByIdAndRevision(installation.contentPackId(), installation.contentPackRevision()).orElseThrow(() -> new NotFoundException("Couldn't find content pack " + installation.contentPackId() + " rev " + installation.contentPackRevision()));
    return contentPackService.getUninstallDetails(contentPack, installation);
}
Also used : ContentPackInstallation(org.graylog2.contentpacks.model.ContentPackInstallation) ObjectId(org.bson.types.ObjectId) ContentPack(org.graylog2.contentpacks.model.ContentPack) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) JsonView(com.fasterxml.jackson.annotation.JsonView) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

ContentPackInstallation (org.graylog2.contentpacks.model.ContentPackInstallation)17 Test (org.junit.Test)11 ObjectId (org.bson.types.ObjectId)8 ContentPack (org.graylog2.contentpacks.model.ContentPack)7 HashMap (java.util.HashMap)6 ModelId (org.graylog2.contentpacks.model.ModelId)6 ImmutableSet (com.google.common.collect.ImmutableSet)5 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)5 NativeEntityDescriptor (org.graylog2.contentpacks.model.entities.NativeEntityDescriptor)5 Map (java.util.Map)4 Optional (java.util.Optional)4 Set (java.util.Set)4 EntityWithExcerptFacade (org.graylog2.contentpacks.facades.EntityWithExcerptFacade)4 ContentPackUninstallation (org.graylog2.contentpacks.model.ContentPackUninstallation)4 Entity (org.graylog2.contentpacks.model.entities.Entity)4 Timed (com.codahale.metrics.annotation.Timed)3 JsonView (com.fasterxml.jackson.annotation.JsonView)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3