Search in sources :

Example 1 with ContentPackLoaderConfig

use of org.graylog2.bundles.ContentPackLoaderConfig 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)

Aggregations

MongoException (com.mongodb.MongoException)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ConfigurationBundle (org.graylog2.bundles.ConfigurationBundle)1 ContentPackLoaderConfig (org.graylog2.bundles.ContentPackLoaderConfig)1