All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.graylog2.plugin.PluginModule Maven / Gradle / Ivy

There is a newer version: 6.1.4
Show newest version
/*
 * Copyright (C) 2020 Graylog, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Server Side Public License, version 1,
 * as published by MongoDB, Inc.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Server Side Public License for more details.
 *
 * You should have received a copy of the Server Side Public License
 * along with this program. If not, see
 * .
 */
package org.graylog2.plugin;

import com.google.common.util.concurrent.Service;
import com.google.inject.Scopes;
import com.google.inject.TypeLiteral;
import com.google.inject.assistedinject.FactoryModuleBuilder;
import com.google.inject.multibindings.MapBinder;
import com.google.inject.multibindings.Multibinder;
import org.graylog.events.contentpack.entities.EventNotificationConfigEntity;
import org.graylog.events.fields.providers.FieldValueProvider;
import org.graylog.events.notifications.EventNotification;
import org.graylog.events.notifications.EventNotificationConfig;
import org.graylog.events.processor.EventProcessor;
import org.graylog.events.processor.EventProcessorConfig;
import org.graylog.events.processor.EventProcessorParameters;
import org.graylog.events.processor.storage.EventStorageHandler;
import org.graylog.grn.GRNDescriptorProvider;
import org.graylog.grn.GRNType;
import org.graylog.plugins.views.search.export.ExportFormat;
import org.graylog.scheduler.Job;
import org.graylog.scheduler.JobDefinitionConfig;
import org.graylog.scheduler.JobSchedule;
import org.graylog.scheduler.JobTriggerData;
import org.graylog.scheduler.capabilities.SchedulerCapabilities;
import org.graylog.scheduler.rest.JobResourceHandler;
import org.graylog.security.authservice.AuthServiceBackend;
import org.graylog.security.authservice.AuthServiceBackendConfig;
import org.graylog2.audit.AuditEventType;
import org.graylog2.audit.PluginAuditEventTypes;
import org.graylog2.audit.formatter.AuditEventFormatter;
import org.graylog2.contentpacks.constraints.ConstraintChecker;
import org.graylog2.contentpacks.facades.EntityWithExcerptFacade;
import org.graylog2.contentpacks.model.ModelType;
import org.graylog2.database.entities.EntityScope;
import org.graylog2.migrations.Migration;
import org.graylog2.plugin.alarms.AlertCondition;
import org.graylog2.plugin.alarms.callbacks.AlarmCallback;
import org.graylog2.plugin.filters.MessageFilter;
import org.graylog2.plugin.indexer.retention.RetentionStrategy;
import org.graylog2.plugin.indexer.rotation.RotationStrategy;
import org.graylog2.plugin.inject.Graylog2Module;
import org.graylog2.plugin.inputs.MessageInput;
import org.graylog2.plugin.inputs.codecs.Codec;
import org.graylog2.plugin.inputs.transports.Transport;
import org.graylog2.plugin.messageprocessors.MessageProcessor;
import org.graylog2.plugin.outputs.MessageOutput;
import org.graylog2.plugin.periodical.Periodical;
import org.graylog2.plugin.rest.PluginRestResource;
import org.graylog2.plugin.security.PasswordAlgorithm;
import org.graylog2.plugin.security.PluginPermissions;
import org.graylog2.plugin.validate.ClusterConfigValidator;
import org.graylog2.shared.messageq.MessageQueueAcknowledger;
import org.graylog2.shared.messageq.MessageQueueReader;
import org.graylog2.shared.messageq.MessageQueueWriter;
import org.graylog2.web.PluginUISettingsProvider;

import javax.ws.rs.ext.ExceptionMapper;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Stream;

public abstract class PluginModule extends Graylog2Module {
    public Set getConfigBeans() {
        return Collections.emptySet();
    }

    protected void addMessageInput(Class messageInputClass) {
        installInput(inputsMapBinder(), messageInputClass);
    }

    protected  void addMessageInput(Class messageInputClass,
                                                            Class> factoryClass) {
        installInput(inputsMapBinder(), messageInputClass, factoryClass);
    }

    protected void addMessageFilter(Class messageFilterClass) {
        Multibinder messageInputs = Multibinder.newSetBinder(binder(), MessageFilter.class);
        messageInputs.addBinding().to(messageFilterClass);
    }

    protected void addPeriodical(Class periodicalClass) {
        Multibinder periodicalBinder = Multibinder.newSetBinder(binder(), Periodical.class);
        periodicalBinder.addBinding().to(periodicalClass);
    }

    protected void addRotationStrategy(Class rotationStrategyClass) {
        installRotationStrategy(rotationStrategiesMapBinder(), rotationStrategyClass);
    }

    protected void addRetentionStrategy(Class retentionStrategyClass) {
        installRetentionStrategy(retentionStrategyMapBinder(), retentionStrategyClass);
    }

    protected void addAlarmCallback(Class alarmCallbackClass) {
        Multibinder alarmCallbackInstanceBinder = Multibinder.newSetBinder(binder(), AlarmCallback.class);
        alarmCallbackInstanceBinder.addBinding().to(alarmCallbackClass);

        TypeLiteral> type = new TypeLiteral>() {
        };
        Multibinder> alarmCallbackBinder = Multibinder.newSetBinder(binder(), type);
        alarmCallbackBinder.addBinding().toInstance(alarmCallbackClass);
    }

    protected void addInitializer(Class initializerClass) {
        Multibinder serviceBinder = serviceBinder();
        serviceBinder.addBinding().to(initializerClass);
    }

    // This should only be used by plugins that have been built before Graylog 3.0.1.
    // See comments in MessageOutput.Factory and MessageOutput.Factory2 for details
    protected void addMessageOutput(Class messageOutputClass) {
        installOutput(outputsMapBinder(), messageOutputClass);
    }

    // This should only be used by plugins that have been built before Graylog 3.0.1.
    // See comments in MessageOutput.Factory and MessageOutput.Factory2 for details
    protected  void addMessageOutput(Class messageOutputClass,
                                                              Class> factory) {
        installOutput(outputsMapBinder(), messageOutputClass, factory);
    }

    // This should be used by plugins that have been built for 3.0.1 or later.
    // See comments in MessageOutput.Factory and MessageOutput.Factory2 for details
    protected  void addMessageOutput2(Class messageOutputClass,
                                                               Class> factory) {
        installOutput2(outputsMapBinder2(), messageOutputClass, factory);
    }

    protected void addRestResource(Class restResourceClass) {
        MapBinder> pluginRestResourceMapBinder =
                MapBinder.newMapBinder(binder(), new TypeLiteral() {},
                                new TypeLiteral>() {})
                        .permitDuplicates();
        pluginRestResourceMapBinder.addBinding(this.getClass().getPackage().getName()).toInstance(restResourceClass);
    }

    protected void addJerseyExceptionMapper(Class exceptionMapperClass) {
        jerseyExceptionMapperBinder().addBinding().toInstance(exceptionMapperClass);
    }

    protected void addConfigBeans() {
        final Multibinder pluginConfigBeans = Multibinder.newSetBinder(binder(), PluginConfigBean.class);
        for (PluginConfigBean pluginConfigBean : getConfigBeans()) {
            pluginConfigBeans.addBinding().toInstance(pluginConfigBean);
        }
    }

    protected void addTransport(String name, Class transportClass) {
        installTransport(transportMapBinder(), name, transportClass);
    }

    protected void addTransport(String name,
                                Class transportClass,
                                Class configClass,
                                Class> factoryClass) {
        installTransport(transportMapBinder(), name, transportClass, configClass, factoryClass);
    }

    protected void addCodec(String name, Class codecClass) {
        installCodec(codecMapBinder(), name, codecClass);
    }

    protected void addCodec(String name,
                            Class codecClass,
                            Class configClass,
                            Class> factoryClass) {
        installCodec(codecMapBinder(), name, codecClass, configClass, factoryClass);
    }

    protected void addPasswordAlgorithm(String passwordAlgorithmName, Class passwordAlgorithmClass) {
        passwordAlgorithmBinder().addBinding(passwordAlgorithmName).to(passwordAlgorithmClass);
    }

    protected Multibinder processorBinder() {
        return Multibinder.newSetBinder(binder(), MessageProcessor.class);
    }

    protected Multibinder processorDescriptorBinder() {
        return Multibinder.newSetBinder(binder(), MessageProcessor.Descriptor.class);
    }

    protected void addMessageProcessor(Class processorClass, Class descriptorClass) {
        processorBinder().addBinding().to(processorClass);
        processorDescriptorBinder().addBinding().to(descriptorClass);
    }

    protected void addPermissions(Class permissionsClass) {
        installPermissions(permissionsBinder(), permissionsClass);
    }

    protected void addAuditEventTypes(Class auditEventTypesClass) {
        installAuditEventTypes(auditEventTypesBinder(), auditEventTypesClass);
    }

    protected void addAuditEventFormatter(AuditEventType auditEventType, Class auditEventFormatterClass) {
        installAuditEventFormatter(auditEventFormatterMapBinder(), auditEventType, auditEventFormatterClass);
    }

    protected void addAlertCondition(String name,
                                     Class alertConditionClass,
                                     Class alertConditionFactoryClass) {
        installAlertConditionWithCustomName(alertConditionBinder(), name, alertConditionClass, alertConditionFactoryClass);
    }

    protected void addMigration(Class migrationClass) {
        migrationsBinder().addBinding().to(migrationClass);
    }

    protected void addEntityFacade(ModelType entityType, Class> entityFacadeClass) {
        entityFacadeBinder().addBinding(entityType).to(entityFacadeClass);
    }

    protected void addConstraintChecker(Class constraintCheckerClass) {
        constraintCheckerBinder().addBinding().to(constraintCheckerClass);
    }

    private MapBinder eventProcessorBinder() {
        return MapBinder.newMapBinder(binder(), String.class, EventProcessor.Factory.class);
    }

    protected void addEventProcessor(String name,
                                     Class processorClass,
                                     Class factoryClass,
                                     Class configClass,
                                     Class parametersClass) {
        install(new FactoryModuleBuilder().implement(EventProcessor.class, processorClass).build(factoryClass));
        eventProcessorBinder().addBinding(name).to(factoryClass);
        registerJacksonSubtype(configClass, name);
        registerJacksonSubtype(parametersClass, name);
    }

    private MapBinder eventStorageHandlerBinder() {
        return MapBinder.newMapBinder(binder(), String.class, EventStorageHandler.Factory.class);
    }

    protected void addEventStorageHandler(String name,
                                          Class handlerClass,
                                          Class factoryClass,
                                          Class configClass) {
        install(new FactoryModuleBuilder().implement(EventStorageHandler.class, handlerClass).build(factoryClass));
        eventStorageHandlerBinder().addBinding(name).to(factoryClass);
        registerJacksonSubtype(configClass, name);
    }

    private MapBinder eventFieldValueProviderBinder() {
        return MapBinder.newMapBinder(binder(), String.class, FieldValueProvider.Factory.class);
    }

    protected void addEventFieldValueProvider(String name,
                                              Class fieldValueProviderClass,
                                              Class factoryClass,
                                              Class configClass) {
        install(new FactoryModuleBuilder().implement(FieldValueProvider.class, fieldValueProviderClass).build(factoryClass));
        eventFieldValueProviderBinder().addBinding(name).to(factoryClass);
        registerJacksonSubtype(configClass, name);
    }

    private MapBinder jobBinder() {
        return MapBinder.newMapBinder(binder(), String.class, Job.Factory.class);
    }

    protected void addSchedulerJob(String name,
                                   Class jobClass,
                                   Class factoryClass,
                                   Class configClass) {
        addSchedulerJob(name, jobClass, factoryClass, configClass, null);
    }

    protected void addSchedulerJob(String name,
                                   Class jobClass,
                                   Class factoryClass,
                                   Class configClass,
                                   Class dataClass) {
        install(new FactoryModuleBuilder().implement(Job.class, jobClass).build(factoryClass));
        jobBinder().addBinding(name).to(factoryClass);
        registerJacksonSubtype(configClass, name);

        // Some jobs might not have a custom data class
        if (dataClass != null) {
            registerJacksonSubtype(dataClass, name);
        }
    }

    protected void addJobSchedulerSchedule(String name, Class scheduleClass) {
        registerJacksonSubtype(scheduleClass, name);
    }

    private MapBinder eventNotificationBinder() {
        return MapBinder.newMapBinder(binder(), String.class, EventNotification.Factory.class);
    }

    /**
     * Deprecated. Please use the below version of the method that also accepts the contentPackEntityName and
     * contentPackEntityClass arguments, so that content pack entities are properly registered.
     * TODO: Consider removing in Graylog 5.0.
     */
    @Deprecated
    protected void addNotificationType(String name,
                                       Class notificationClass,
                                       Class handlerClass,
                                       Class factoryClass) {
        install(new FactoryModuleBuilder().implement(EventNotification.class, handlerClass).build(factoryClass));
        eventNotificationBinder().addBinding(name).to(factoryClass);
        registerJacksonSubtype(notificationClass, name);
    }

    protected void addNotificationType(String name,
                                       Class notificationClass,
                                       Class handlerClass,
                                       Class factoryClass,
                                       String contentPackEntityName,
                                       Class contentPackEntityClass) {
        addNotificationType(name, notificationClass, handlerClass, factoryClass);
        registerJacksonSubtype(contentPackEntityClass, contentPackEntityName);
    }

    protected void addGRNType(GRNType type, Class descriptorProvider) {
        final MapBinder mapBinder = MapBinder.newMapBinder(binder(), GRNType.class, GRNDescriptorProvider.class);
        mapBinder.addBinding(type).to(descriptorProvider);
    }

    protected MapBinder> authServiceBackendBinder() {
        return MapBinder.newMapBinder(
                binder(),
                TypeLiteral.get(String.class),
                new TypeLiteral>() {}
        );
    }

    protected void addAuthServiceBackend(String name,
                                         Class backendClass,
                                         Class> factoryClass,
                                         Class configClass) {
        install(new FactoryModuleBuilder().implement(AuthServiceBackend.class, backendClass).build(factoryClass));
        authServiceBackendBinder().addBinding(name).to(factoryClass);
        registerJacksonSubtype(configClass, name);
    }

    protected MapBinder pluginUISettingsProviderBinder() {
        return MapBinder.newMapBinder(binder(), String.class, PluginUISettingsProvider.class);
    }

    protected void addPluginUISettingsProvider(String providerKey,
                                               Class uiSettingsProviderClass) {
        pluginUISettingsProviderBinder().addBinding(providerKey).to(uiSettingsProviderClass);
    }

    private Multibinder exportFormatBinder() {
        return Multibinder.newSetBinder(binder(), ExportFormat.class);
    }

    protected void addExportFormat(Class exportFormat) {
        exportFormatBinder().addBinding().to(exportFormat);
    }

    protected void addExportFormat(ExportFormat exportFormat) {
        exportFormatBinder().addBinding().toInstance(exportFormat);
    }

    /**
     * Bind a message queue implementation. If any of the given classes implements the {@link Service} interface, it
     * will also be registered with the {@link #serviceBinder()}.
     *
     * @param readerClass       Reader implementation
     * @param writerClass       Writer implementation
     * @param acknowledgerClass Acknowledger implementation
     */
    protected void bindMessageQueueImplementation(Class readerClass,
                                                  Class writerClass,
                                                  Class acknowledgerClass) {

        bind(MessageQueueReader.class).to(readerClass).in(Scopes.SINGLETON);
        bind(MessageQueueWriter.class).to(writerClass).in(Scopes.SINGLETON);
        bind(MessageQueueAcknowledger.class).to(acknowledgerClass).in(Scopes.SINGLETON);

        //noinspection unchecked
        Stream.of(readerClass, writerClass, acknowledgerClass)
                .filter(Service.class::isAssignableFrom)
                .forEach(service ->
                        serviceBinder().addBinding().to((Class) service).in(Scopes.SINGLETON));
    }


    protected void addClusterConfigValidator(Class configClass, Class configValidatorClass) {
        clusterConfigMapBinder().addBinding(configClass).to(configValidatorClass);
    }

    protected Multibinder schdulerCapabilitiesBinder() {
        return Multibinder.newSetBinder(binder(), SchedulerCapabilities.class);
    }

    protected void addSchedulerCapabilities(Class schedulerCapabilitiesClass) {
        schdulerCapabilitiesBinder().addBinding().to(schedulerCapabilitiesClass);
    }

    protected MapBinder jobResourceHandlerBinder() {
        return MapBinder.newMapBinder(binder(), String.class, JobResourceHandler.class);
    }

    protected void addJobResourceHandler(String jobType, Class jobResourceHandlerClass) {
        jobResourceHandlerBinder().addBinding(jobType).to(jobResourceHandlerClass);
    }

    protected void addEntityScope(Class entityScopeType) {
        Multibinder scopeBinder = Multibinder.newSetBinder(binder(), EntityScope.class);
        scopeBinder.addBinding().to(entityScopeType);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy