Search in sources :

Example 1 with IntruderStorageMethod

use of password.pwm.config.option.IntruderStorageMethod in project pwm by pwm-project.

the class IntruderManager method init.

@Override
@SuppressWarnings("checkstyle:MethodLength")
public void init(final PwmApplication pwmApplication) throws PwmException {
    this.pwmApplication = pwmApplication;
    final Configuration config = pwmApplication.getConfig();
    status = STATUS.OPENING;
    if (pwmApplication.getLocalDB() == null || pwmApplication.getLocalDB().status() != LocalDB.Status.OPEN) {
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_SERVICE_NOT_AVAILABLE, "unable to start IntruderManager, LocalDB unavailable");
        LOGGER.error(errorInformation.toDebugStr());
        startupError = errorInformation;
        status = STATUS.CLOSED;
        return;
    }
    if (!pwmApplication.getConfig().readSettingAsBoolean(PwmSetting.INTRUDER_ENABLE)) {
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_SERVICE_NOT_AVAILABLE, "intruder module not enabled");
        LOGGER.debug(errorInformation.toDebugStr());
        status = STATUS.CLOSED;
        return;
    }
    final DataStore dataStore;
    {
        final IntruderStorageMethod intruderStorageMethod = pwmApplication.getConfig().readSettingAsEnum(PwmSetting.INTRUDER_STORAGE_METHOD, IntruderStorageMethod.class);
        final String debugMsg;
        final DataStorageMethod storageMethodUsed;
        switch(intruderStorageMethod) {
            case AUTO:
                dataStore = DataStoreFactory.autoDbOrLocalDBstore(pwmApplication, DatabaseTable.INTRUDER, LocalDB.DB.INTRUDER);
                if (dataStore instanceof DatabaseDataStore) {
                    debugMsg = "starting using auto-configured data store, Remote Database selected";
                    storageMethodUsed = DataStorageMethod.DB;
                } else {
                    debugMsg = "starting using auto-configured data store, LocalDB selected";
                    storageMethodUsed = DataStorageMethod.LOCALDB;
                }
                break;
            case DATABASE:
                dataStore = new DatabaseDataStore(pwmApplication.getDatabaseService(), DatabaseTable.INTRUDER);
                debugMsg = "starting using Remote Database data store";
                storageMethodUsed = DataStorageMethod.DB;
                break;
            case LOCALDB:
                dataStore = new LocalDBDataStore(pwmApplication.getLocalDB(), LocalDB.DB.INTRUDER);
                debugMsg = "starting using LocalDB data store";
                storageMethodUsed = DataStorageMethod.LOCALDB;
                break;
            default:
                startupError = new ErrorInformation(PwmError.ERROR_UNKNOWN, "unknown storageMethod selected: " + intruderStorageMethod);
                status = STATUS.CLOSED;
                return;
        }
        LOGGER.info(debugMsg);
        serviceInfo = new ServiceInfoBean(Collections.singletonList(storageMethodUsed));
    }
    final RecordStore recordStore;
    {
        recordStore = new DataStoreRecordStore(dataStore, this);
        final String threadName = JavaHelper.makeThreadName(pwmApplication, this.getClass()) + " timer";
        timer = new Timer(threadName, true);
        final long maxRecordAge = Long.parseLong(pwmApplication.getConfig().readAppProperty(AppProperty.INTRUDER_RETENTION_TIME_MS));
        final long cleanerRunFrequency = Long.parseLong(pwmApplication.getConfig().readAppProperty(AppProperty.INTRUDER_CLEANUP_FREQUENCY_MS));
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                try {
                    recordStore.cleanup(new TimeDuration(maxRecordAge));
                } catch (Exception e) {
                    LOGGER.error("error cleaning recordStore: " + e.getMessage(), e);
                }
            }
        }, 1000, cleanerRunFrequency);
    }
    try {
        {
            final IntruderSettings settings = new IntruderSettings();
            settings.setCheckCount((int) config.readSettingAsLong(PwmSetting.INTRUDER_USER_MAX_ATTEMPTS));
            settings.setResetDuration(new TimeDuration(1000 * config.readSettingAsLong(PwmSetting.INTRUDER_USER_RESET_TIME)));
            settings.setCheckDuration(new TimeDuration(1000 * config.readSettingAsLong(PwmSetting.INTRUDER_USER_CHECK_TIME)));
            if (settings.getCheckCount() == 0 || settings.getCheckDuration().getTotalMilliseconds() == 0 || settings.getResetDuration().getTotalMilliseconds() == 0) {
                LOGGER.info("intruder user checking will remain disabled due to configuration settings");
            } else {
                recordManagers.put(RecordType.USERNAME, new RecordManagerImpl(RecordType.USERNAME, recordStore, settings));
                recordManagers.put(RecordType.USER_ID, new RecordManagerImpl(RecordType.USER_ID, recordStore, settings));
            }
        }
        {
            final IntruderSettings settings = new IntruderSettings();
            settings.setCheckCount((int) config.readSettingAsLong(PwmSetting.INTRUDER_ATTRIBUTE_MAX_ATTEMPTS));
            settings.setResetDuration(new TimeDuration(1000 * config.readSettingAsLong(PwmSetting.INTRUDER_ATTRIBUTE_RESET_TIME)));
            settings.setCheckDuration(new TimeDuration(1000 * config.readSettingAsLong(PwmSetting.INTRUDER_ATTRIBUTE_CHECK_TIME)));
            if (settings.getCheckCount() == 0 || settings.getCheckDuration().getTotalMilliseconds() == 0 || settings.getResetDuration().getTotalMilliseconds() == 0) {
                LOGGER.info("intruder user checking will remain disabled due to configuration settings");
            } else {
                recordManagers.put(RecordType.ATTRIBUTE, new RecordManagerImpl(RecordType.ATTRIBUTE, recordStore, settings));
            }
        }
        {
            final IntruderSettings settings = new IntruderSettings();
            settings.setCheckCount((int) config.readSettingAsLong(PwmSetting.INTRUDER_TOKEN_DEST_MAX_ATTEMPTS));
            settings.setResetDuration(new TimeDuration(1000 * config.readSettingAsLong(PwmSetting.INTRUDER_TOKEN_DEST_RESET_TIME)));
            settings.setCheckDuration(new TimeDuration(1000 * config.readSettingAsLong(PwmSetting.INTRUDER_TOKEN_DEST_CHECK_TIME)));
            if (settings.getCheckCount() == 0 || settings.getCheckDuration().getTotalMilliseconds() == 0 || settings.getResetDuration().getTotalMilliseconds() == 0) {
                LOGGER.info("intruder user checking will remain disabled due to configuration settings");
            } else {
                recordManagers.put(RecordType.TOKEN_DEST, new RecordManagerImpl(RecordType.TOKEN_DEST, recordStore, settings));
            }
        }
        {
            final IntruderSettings settings = new IntruderSettings();
            settings.setCheckCount((int) config.readSettingAsLong(PwmSetting.INTRUDER_ADDRESS_MAX_ATTEMPTS));
            settings.setResetDuration(new TimeDuration(1000 * config.readSettingAsLong(PwmSetting.INTRUDER_ADDRESS_RESET_TIME)));
            settings.setCheckDuration(new TimeDuration(1000 * config.readSettingAsLong(PwmSetting.INTRUDER_ADDRESS_CHECK_TIME)));
            if (settings.getCheckCount() == 0 || settings.getCheckDuration().getTotalMilliseconds() == 0 || settings.getResetDuration().getTotalMilliseconds() == 0) {
                LOGGER.info("intruder address checking will remain disabled due to configuration settings");
            } else {
                recordManagers.put(RecordType.ADDRESS, new RecordManagerImpl(RecordType.ADDRESS, recordStore, settings));
            }
        }
        status = STATUS.OPEN;
    } catch (Exception e) {
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_SERVICE_NOT_AVAILABLE, "unexpected error starting intruder manager: " + e.getMessage());
        LOGGER.error(errorInformation.toDebugStr());
        startupError = errorInformation;
        close();
    }
}
Also used : FormConfiguration(password.pwm.config.value.data.FormConfiguration) Configuration(password.pwm.config.Configuration) DataStorageMethod(password.pwm.config.option.DataStorageMethod) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PwmException(password.pwm.error.PwmException) ErrorInformation(password.pwm.error.ErrorInformation) LocalDBDataStore(password.pwm.util.localdb.LocalDBDataStore) DatabaseDataStore(password.pwm.util.db.DatabaseDataStore) Timer(java.util.Timer) TimerTask(java.util.TimerTask) DataStore(password.pwm.util.DataStore) LocalDBDataStore(password.pwm.util.localdb.LocalDBDataStore) DatabaseDataStore(password.pwm.util.db.DatabaseDataStore) IntruderStorageMethod(password.pwm.config.option.IntruderStorageMethod) TimeDuration(password.pwm.util.java.TimeDuration)

Aggregations

Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 Configuration (password.pwm.config.Configuration)1 DataStorageMethod (password.pwm.config.option.DataStorageMethod)1 IntruderStorageMethod (password.pwm.config.option.IntruderStorageMethod)1 FormConfiguration (password.pwm.config.value.data.FormConfiguration)1 ErrorInformation (password.pwm.error.ErrorInformation)1 PwmException (password.pwm.error.PwmException)1 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)1 DataStore (password.pwm.util.DataStore)1 DatabaseDataStore (password.pwm.util.db.DatabaseDataStore)1 TimeDuration (password.pwm.util.java.TimeDuration)1 LocalDBDataStore (password.pwm.util.localdb.LocalDBDataStore)1