Search in sources :

Example 41 with ErrorInformation

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

the class LocalDbCrOperator method clearResponses.

public void clearResponses(final UserIdentity userIdentity, final ChaiUser theUser, final String userGUID) throws PwmUnrecoverableException {
    if (userGUID == null || userGUID.length() < 1) {
        throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_MISSING_GUID, "cannot clear responses to localDB, user does not have a pwmGUID"));
    }
    if (localDB == null) {
        final String errorMsg = "LocalDB is not available, unable to write user responses";
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_LOCALDB_UNAVAILABLE, errorMsg);
        throw new PwmUnrecoverableException(errorInformation);
    }
    try {
        localDB.remove(LocalDB.DB.RESPONSE_STORAGE, userGUID);
        LOGGER.info("cleared responses for user " + theUser.getEntryDN() + " in local LocalDB");
    } catch (LocalDBException e) {
        final ErrorInformation errorInfo = new ErrorInformation(PwmError.ERROR_CLEARING_RESPONSES, "unexpected LocalDB error clearing responses: " + e.getMessage());
        final PwmUnrecoverableException pwmOE = new PwmUnrecoverableException(errorInfo);
        pwmOE.initCause(e);
        throw pwmOE;
    }
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) LocalDBException(password.pwm.util.localdb.LocalDBException)

Example 42 with ErrorInformation

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

the class AbstractOtpOperator method composeOtpAttribute.

/**
 * Compose a single line of OTP information.
 *
 * @param otpUserRecord input user record
 * @return A string formatted record
 */
public String composeOtpAttribute(final OTPUserRecord otpUserRecord) throws PwmUnrecoverableException {
    String value = "";
    if (otpUserRecord != null) {
        final Configuration config = pwmApplication.getConfig();
        final OTPStorageFormat format = config.readSettingAsEnum(PwmSetting.OTP_SECRET_STORAGEFORMAT, OTPStorageFormat.class);
        switch(format) {
            case PWM:
                value = JsonUtil.serialize(otpUserRecord);
                break;
            case OTPURL:
                value = OTPUrlUtil.composeOtpUrl(otpUserRecord);
                break;
            case BASE32SECRET:
                value = otpUserRecord.getSecret();
                break;
            case PAM:
                value = OTPPamUtil.composePamData(otpUserRecord);
                break;
            default:
                final String errorStr = String.format("Unsupported storage format: %s", format.toString());
                final ErrorInformation error = new ErrorInformation(PwmError.ERROR_INVALID_CONFIG, errorStr);
                throw new PwmUnrecoverableException(error);
        }
    }
    return value;
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) Configuration(password.pwm.config.Configuration) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) OTPStorageFormat(password.pwm.config.option.OTPStorageFormat)

Example 43 with ErrorInformation

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

the class DatabaseService method openConnection.

private Connection openConnection(final DBConfiguration dbConfiguration) throws DatabaseException {
    final String connectionURL = dbConfiguration.getConnectionString();
    final JDBCDriverLoader.DriverWrapper wrapper = JDBCDriverLoader.loadDriver(pwmApplication, dbConfiguration);
    driver = wrapper.getDriver();
    jdbcDriverLoader = wrapper.getDriverLoader();
    try {
        LOGGER.debug("initiating connecting to database " + connectionURL);
        final Properties connectionProperties = new Properties();
        if (dbConfiguration.getUsername() != null && !dbConfiguration.getUsername().isEmpty()) {
            connectionProperties.setProperty("user", dbConfiguration.getUsername());
        }
        if (dbConfiguration.getPassword() != null) {
            connectionProperties.setProperty("password", dbConfiguration.getPassword().getStringValue());
        }
        final Connection connection = driver.connect(connectionURL, connectionProperties);
        LOGGER.debug("connected to database " + connectionURL);
        connection.setAutoCommit(false);
        return connection;
    } catch (Throwable e) {
        final String errorMsg = "error connecting to database: " + JavaHelper.readHostileExceptionMessage(e);
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_DB_UNAVAILABLE, errorMsg);
        LOGGER.error(errorInformation);
        throw new DatabaseException(errorInformation);
    }
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) Connection(java.sql.Connection) Properties(java.util.Properties)

Example 44 with ErrorInformation

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

the class DatabaseService method init.

private synchronized void init() {
    if (initialized) {
        return;
    }
    final Instant startTime = Instant.now();
    status = STATUS.OPENING;
    try {
        final Configuration config = pwmApplication.getConfig();
        this.dbConfiguration = DBConfiguration.fromConfiguration(config);
        if (!dbConfiguration.isEnabled()) {
            status = PwmService.STATUS.CLOSED;
            LOGGER.debug("skipping database connection open, no connection parameters configured");
            initialized = true;
            return;
        }
        LOGGER.debug("opening connection to database " + this.dbConfiguration.getConnectionString());
        slotIncrementer = new AtomicLoopIntIncrementer(dbConfiguration.getMaxConnections());
        {
            // make initial connection and establish schema
            clearCurrentAccessors();
            final Connection connection = openConnection(dbConfiguration);
            updateDebugProperties(connection);
            LOGGER.debug("established initial connection to " + dbConfiguration.getConnectionString() + ", properties: " + JsonUtil.serializeMap(this.debugInfo));
            for (final DatabaseTable table : DatabaseTable.values()) {
                DatabaseUtil.initTable(connection, table, dbConfiguration);
            }
            connection.close();
        }
        accessors.clear();
        {
            // set up connection pool
            final boolean traceLogging = config.readSettingAsBoolean(PwmSetting.DATABASE_DEBUG_TRACE);
            for (int i = 0; i < dbConfiguration.getMaxConnections(); i++) {
                final Connection connection = openConnection(dbConfiguration);
                final DatabaseAccessorImpl accessor = new DatabaseAccessorImpl(this, this.dbConfiguration, connection, traceLogging);
                accessors.put(i, accessor);
            }
        }
        LOGGER.debug("successfully connected to remote database (" + TimeDuration.fromCurrent(startTime).asCompactString() + ")");
        status = STATUS.OPEN;
        initialized = true;
    } catch (Throwable t) {
        final String errorMsg = "exception initializing database service: " + t.getMessage();
        LOGGER.warn(errorMsg);
        initialized = false;
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_DB_UNAVAILABLE, errorMsg);
        lastError = errorInformation;
    }
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) Configuration(password.pwm.config.Configuration) AtomicLoopIntIncrementer(password.pwm.util.java.AtomicLoopIntIncrementer) Instant(java.time.Instant) Connection(java.sql.Connection)

Example 45 with ErrorInformation

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

the class DatabaseUtil method createTable.

private static void createTable(final Connection connection, final DatabaseTable table, final DBConfiguration dbConfiguration) throws DatabaseException {
    {
        final String sqlString = "CREATE table " + table.toString() + " (" + "\n" + "  " + DatabaseService.KEY_COLUMN + " " + dbConfiguration.getColumnTypeKey() + "(" + dbConfiguration.getKeyColumnLength() + ") NOT NULL PRIMARY KEY," + "\n" + "  " + DatabaseService.VALUE_COLUMN + " " + dbConfiguration.getColumnTypeValue() + " " + "\n" + ")" + "\n";
        LOGGER.trace("attempting to execute the following sql statement:\n " + sqlString);
        Statement statement = null;
        try {
            statement = connection.createStatement();
            statement.execute(sqlString);
            connection.commit();
            LOGGER.debug("created table " + table.toString());
        } catch (SQLException ex) {
            final String errorMsg = "error creating new table " + table.toString() + ": " + ex.getMessage();
            final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_DB_UNAVAILABLE, errorMsg);
            throw new DatabaseException(errorInformation);
        } finally {
            DatabaseUtil.close(statement);
        }
    }
    {
        final String indexName = table.toString() + INDEX_NAME_SUFFIX;
        final String sqlString = "CREATE index " + indexName + " ON " + table.toString() + " (" + DatabaseService.KEY_COLUMN + ")";
        Statement statement = null;
        LOGGER.trace("attempting to execute the following sql statement:\n " + sqlString);
        try {
            statement = connection.createStatement();
            statement.execute(sqlString);
            connection.commit();
            LOGGER.debug("created index " + indexName);
        } catch (SQLException ex) {
            final String errorMsg = "error creating new index " + indexName + ": " + ex.getMessage();
            final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_DB_UNAVAILABLE, errorMsg);
            if (dbConfiguration.isFailOnIndexCreation()) {
                throw new DatabaseException(errorInformation);
            } else {
                LOGGER.warn(errorInformation.toDebugStr());
            }
        } finally {
            DatabaseUtil.close(statement);
        }
    }
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) SQLException(java.sql.SQLException) Statement(java.sql.Statement)

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