Search in sources :

Example 51 with ErrorInformation

use of password.pwm.error.ErrorInformation in project pwm by pwm-project.

the class LocalDbOtpOperator method writeOtpUserConfiguration.

@Override
public void writeOtpUserConfiguration(final PwmSession pwmSession, final UserIdentity theUser, final String userGUID, final OTPUserRecord otpConfig) throws PwmUnrecoverableException {
    LOGGER.trace(pwmSession, String.format("Enter: writeOtpUserConfiguration(%s, %s, %s)", theUser, userGUID, otpConfig));
    if (userGUID == null || userGUID.length() < 1) {
        throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_MISSING_GUID, "cannot save otp to localDB, user does not have a pwmGUID"));
    }
    if (localDB == null || localDB.status() != LocalDB.Status.OPEN) {
        final String errorMsg = "LocalDB is not available, unable to write user otp";
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_LOCALDB_UNAVAILABLE, errorMsg);
        throw new PwmUnrecoverableException(errorInformation);
    }
    try {
        final Configuration config = this.getPwmApplication().getConfig();
        String value = composeOtpAttribute(otpConfig);
        if (config.readSettingAsBoolean(PwmSetting.OTP_SECRET_ENCRYPT)) {
            LOGGER.debug(pwmSession, "Encrypting OTP secret for storage");
            value = encryptAttributeValue(value);
        }
        localDB.put(LocalDB.DB.OTP_SECRET, userGUID, value);
        LOGGER.info(pwmSession, "saved OTP secret for user in LocalDB");
    } catch (LocalDBException ex) {
        final ErrorInformation errorInfo = new ErrorInformation(PwmError.ERROR_WRITING_OTP_SECRET, "unexpected LocalDB error saving otp to localDB: " + ex.getMessage());
        final PwmUnrecoverableException pwmOE = new PwmUnrecoverableException(errorInfo);
        pwmOE.initCause(ex);
        throw pwmOE;
    } catch (PwmOperationalException ex) {
        final ErrorInformation errorInfo = new ErrorInformation(PwmError.ERROR_WRITING_OTP_SECRET, "unexpected error saving otp to localDB: " + ex.getMessage());
        final PwmUnrecoverableException pwmOE = new PwmUnrecoverableException(errorInfo);
        pwmOE.initCause(ex);
        throw pwmOE;
    }
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) Configuration(password.pwm.config.Configuration) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) LocalDBException(password.pwm.util.localdb.LocalDBException) PwmOperationalException(password.pwm.error.PwmOperationalException)

Example 52 with ErrorInformation

use of password.pwm.error.ErrorInformation in project pwm by pwm-project.

the class AbstractJDBCLocalDB method size.

public int size(final LocalDB.DB db) throws LocalDBException {
    preCheck(false);
    final StringBuilder sb = new StringBuilder();
    sb.append("SELECT COUNT(" + KEY_COLUMN + ") FROM ").append(db.toString());
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    try {
        lock.readLock().lock();
        try {
            statement = dbConnection.prepareStatement(sb.toString());
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                return resultSet.getInt(1);
            }
        } catch (final SQLException ex) {
            throw new LocalDBException(new ErrorInformation(PwmError.ERROR_LOCALDB_UNAVAILABLE, ex.getMessage()));
        } finally {
            close(statement);
            close(resultSet);
        }
    } finally {
        lock.readLock().unlock();
    }
    return 0;
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 53 with ErrorInformation

use of password.pwm.error.ErrorInformation in project pwm by pwm-project.

the class AbstractJDBCLocalDB method iterator.

public LocalDB.LocalDBIterator<String> iterator(final LocalDB.DB db) throws LocalDBException {
    try {
        if (dbIterators.size() > ITERATOR_LIMIT) {
            throw new LocalDBException(new ErrorInformation(PwmError.ERROR_UNKNOWN, "over " + ITERATOR_LIMIT + " iterators are outstanding, maximum limit exceeded"));
        }
        final LocalDB.LocalDBIterator iterator = new DbIterator(db);
        dbIterators.add(iterator);
        LOGGER.trace(this.getClass().getSimpleName() + " issued iterator for " + db.toString() + ", outstanding iterators: " + dbIterators.size());
        return iterator;
    } catch (final Exception e) {
        throw new LocalDBException(new ErrorInformation(PwmError.ERROR_LOCALDB_UNAVAILABLE, e.getMessage()));
    }
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) SQLException(java.sql.SQLException)

Example 54 with ErrorInformation

use of password.pwm.error.ErrorInformation in project pwm by pwm-project.

the class AbstractJDBCLocalDB method executeUpdateStatement.

private void executeUpdateStatement(final String sqlText, final String... values) throws LocalDBException {
    lock.writeLock().lock();
    try {
        PreparedStatement statement = null;
        try {
            statement = dbConnection.prepareStatement(sqlText);
            for (int i = 0; i < values.length; i++) {
                statement.setString(i + 1, values[i]);
            }
            statement.executeUpdate();
            dbConnection.commit();
        } catch (final SQLException ex) {
            throw new LocalDBException(new ErrorInformation(PwmError.ERROR_LOCALDB_UNAVAILABLE, ex.getMessage()));
        } finally {
            close(statement);
        }
    } finally {
        lock.writeLock().unlock();
    }
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement)

Example 55 with ErrorInformation

use of password.pwm.error.ErrorInformation in project pwm by pwm-project.

the class AbstractJDBCLocalDB method truncate.

public void truncate(final LocalDB.DB db) throws LocalDBException {
    preCheck(true);
    final Instant startTime = Instant.now();
    final StringBuilder sqlText = new StringBuilder();
    sqlText.append("DROP TABLE ").append(db.toString());
    PreparedStatement statement = null;
    try {
        lock.writeLock().lock();
        try {
            final Set<LocalDB.LocalDBIterator<String>> copiedIterators = new HashSet<>();
            copiedIterators.addAll(dbIterators);
            for (final LocalDB.LocalDBIterator dbIterator : copiedIterators) {
                dbIterator.close();
            }
            statement = dbConnection.prepareStatement(sqlText.toString());
            statement.executeUpdate();
            dbConnection.commit();
            LOGGER.debug("truncated table " + db.toString() + " (" + TimeDuration.fromCurrent(startTime).asCompactString() + ")");
            initTable(dbConnection, db);
        } catch (final SQLException ex) {
            throw new LocalDBException(new ErrorInformation(PwmError.ERROR_LOCALDB_UNAVAILABLE, ex.getMessage()));
        } finally {
            close(statement);
        }
    } finally {
        lock.writeLock().unlock();
    }
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) SQLException(java.sql.SQLException) Instant(java.time.Instant) PreparedStatement(java.sql.PreparedStatement) HashSet(java.util.HashSet)

Aggregations

ErrorInformation (password.pwm.error.ErrorInformation)325 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)216 PwmOperationalException (password.pwm.error.PwmOperationalException)125 PwmException (password.pwm.error.PwmException)67 UserIdentity (password.pwm.bean.UserIdentity)62 IOException (java.io.IOException)58 PwmApplication (password.pwm.PwmApplication)54 ChaiUnavailableException (com.novell.ldapchai.exception.ChaiUnavailableException)53 ChaiUser (com.novell.ldapchai.ChaiUser)38 PwmSession (password.pwm.http.PwmSession)38 LinkedHashMap (java.util.LinkedHashMap)35 Configuration (password.pwm.config.Configuration)33 ChaiOperationException (com.novell.ldapchai.exception.ChaiOperationException)32 Map (java.util.Map)32 Instant (java.time.Instant)30 ArrayList (java.util.ArrayList)30 FormConfiguration (password.pwm.config.value.data.FormConfiguration)29 ServletException (javax.servlet.ServletException)28 RestResultBean (password.pwm.ws.server.RestResultBean)26 UserInfo (password.pwm.ldap.UserInfo)23