Search in sources :

Example 1 with ConfigurationBundle

use of org.graylog2.bundles.ConfigurationBundle in project graylog2-server by Graylog2.

the class ContentPackLoaderPeriodical method doRun.

@Override
public void doRun() {
    final ContentPackLoaderConfig contentPackLoaderConfig = clusterConfigService.getOrDefault(ContentPackLoaderConfig.class, ContentPackLoaderConfig.EMPTY);
    final List<Path> files = getFiles(contentPacksDir, FILENAME_GLOB);
    final Map<String, ConfigurationBundle> contentPacks = new HashMap<>(files.size());
    final Set<String> loadedContentPacks = new HashSet<>(contentPackLoaderConfig.loadedContentPacks());
    final Set<String> appliedContentPacks = new HashSet<>(contentPackLoaderConfig.appliedContentPacks());
    final Map<String, String> checksums = new HashMap<>(contentPackLoaderConfig.checksums());
    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;
        }
        final String encodedFileName = encodeFileNameForMongo(fileName);
        final String checksum = HASH_FUNCTION.hashBytes(bytes).toString();
        final String storedChecksum = checksums.get(encodedFileName);
        if (storedChecksum == null) {
            checksums.put(encodedFileName, checksum);
        } else if (!checksum.equals(storedChecksum)) {
            LOG.info("Checksum of {} changed (expected: {}, actual: {})", file, storedChecksum, checksum);
            continue;
        }
        if (contentPackLoaderConfig.loadedContentPacks().contains(fileName)) {
            LOG.debug("Skipping already loaded content pack {} (SHA-256: {})", file, storedChecksum);
            continue;
        }
        LOG.debug("Parsing content pack from {}", file);
        final ConfigurationBundle contentPack;
        try {
            contentPack = objectMapper.readValue(bytes, ConfigurationBundle.class);
        } catch (IOException e) {
            LOG.warn("Couldn't parse content pack in file " + file + ". Skipping", e);
            continue;
        }
        final ConfigurationBundle existingContentPack = bundleService.findByNameAndCategory(contentPack.getName(), contentPack.getCategory());
        if (existingContentPack != null) {
            LOG.debug("Content pack {}/{} already exists in database. Skipping.", contentPack.getCategory(), contentPack.getName());
            contentPacks.put(fileName, existingContentPack);
            continue;
        }
        final ConfigurationBundle insertedContentPack;
        try {
            insertedContentPack = bundleService.insert(contentPack);
            LOG.debug("Successfully inserted content pack {} into database with ID {}", file, insertedContentPack.getId());
        } catch (MongoException e) {
            LOG.error("Error while inserting content pack " + file + " into database. Skipping.", e);
            continue;
        }
        contentPacks.put(fileName, insertedContentPack);
        loadedContentPacks.add(fileName);
    }
    LOG.debug("Applying selected content packs");
    for (Map.Entry<String, ConfigurationBundle> entry : contentPacks.entrySet()) {
        final String fileName = entry.getKey();
        final ConfigurationBundle contentPack = entry.getValue();
        if (contentPacksAutoLoad.contains(fileName) && appliedContentPacks.contains(fileName)) {
            LOG.debug("Content pack {}/{} ({}) already applied. Skipping.", contentPack.getName(), contentPack.getCategory(), fileName);
            continue;
        }
        if (contentPacksAutoLoad.contains(fileName)) {
            LOG.debug("Applying content pack {}/{} ({})", contentPack.getName(), contentPack.getCategory(), fileName);
            bundleService.applyConfigurationBundle(contentPack, userService.getAdminUser());
            appliedContentPacks.add(fileName);
        }
    }
    final ContentPackLoaderConfig changedContentPackLoaderConfig = ContentPackLoaderConfig.create(loadedContentPacks, appliedContentPacks, checksums);
    if (!contentPackLoaderConfig.equals(changedContentPackLoaderConfig)) {
        clusterConfigService.write(changedContentPackLoaderConfig);
    }
}
Also used : Path(java.nio.file.Path) MongoException(com.mongodb.MongoException) HashMap(java.util.HashMap) IOException(java.io.IOException) ContentPackLoaderConfig(org.graylog2.bundles.ContentPackLoaderConfig) ConfigurationBundle(org.graylog2.bundles.ConfigurationBundle) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 2 with ConfigurationBundle

use of org.graylog2.bundles.ConfigurationBundle in project graylog2-server by Graylog2.

the class BundleResource method createBundle.

@POST
@Timed
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Upload a content pack")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Missing or invalid content pack"), @ApiResponse(code = 500, message = "Error while saving content pack") })
@AuditEvent(type = AuditEventTypes.CONTENT_PACK_CREATE)
public Response createBundle(@ApiParam(name = "Request body", value = "Content pack", required = true) @NotNull @Valid final ConfigurationBundle configurationBundle) {
    checkPermission(RestPermissions.BUNDLE_CREATE);
    final ConfigurationBundle bundle = bundleService.insert(configurationBundle);
    final URI bundleUri = getUriBuilderToSelf().path(BundleResource.class).path("{bundleId}").build(bundle.getId());
    return Response.created(bundleUri).build();
}
Also used : ConfigurationBundle(org.graylog2.bundles.ConfigurationBundle) URI(java.net.URI) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) ApiResponses(io.swagger.annotations.ApiResponses)

Example 3 with ConfigurationBundle

use of org.graylog2.bundles.ConfigurationBundle in project graylog2-server by Graylog2.

the class BundleResource method listBundles.

@GET
@Timed
@ApiOperation(value = "List available content packs")
@ApiResponses(value = { @ApiResponse(code = 500, message = "Error loading content packs") })
public Multimap<String, ConfigurationBundle> listBundles() {
    final ImmutableSetMultimap.Builder<String, ConfigurationBundle> categoryBundleMap = ImmutableSetMultimap.builder();
    for (final ConfigurationBundle bundle : bundleService.loadAll()) {
        checkPermission(RestPermissions.BUNDLE_READ, bundle.getId());
        categoryBundleMap.put(bundle.getCategory(), bundle);
    }
    return categoryBundleMap.build();
}
Also used : ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) ConfigurationBundle(org.graylog2.bundles.ConfigurationBundle) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

ConfigurationBundle (org.graylog2.bundles.ConfigurationBundle)3 Timed (com.codahale.metrics.annotation.Timed)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 ImmutableSetMultimap (com.google.common.collect.ImmutableSetMultimap)1 MongoException (com.mongodb.MongoException)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1 AuditEvent (org.graylog2.audit.jersey.AuditEvent)1 ContentPackLoaderConfig (org.graylog2.bundles.ContentPackLoaderConfig)1