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);
}
}
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();
}
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();
}
Aggregations