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