use of org.apache.airavata.credential.store.store.CredentialStoreException in project airavata by apache.
the class CredentialsDAO method convertObjectToByteArray.
public byte[] convertObjectToByteArray(Serializable o) throws CredentialStoreException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(o);
objectOutputStream.flush();
} catch (IOException e) {
throw new CredentialStoreException("Error serializing object.", e);
} finally {
if (objectOutputStream != null) {
try {
objectOutputStream.close();
} catch (IOException e) {
log.error("Error occurred while closing object output stream", e);
}
}
}
// encrypt the byte array
if (encrypt()) {
byte[] array = byteArrayOutputStream.toByteArray();
try {
return SecurityUtil.encrypt(this.keyStorePath, this.secretKeyAlias, this.keyStorePasswordCallback, array);
} catch (GeneralSecurityException e) {
throw new CredentialStoreException("Error encrypting data", e);
} catch (IOException e) {
throw new CredentialStoreException("Error encrypting data. IO exception.", e);
}
} else {
return byteArrayOutputStream.toByteArray();
}
}
use of org.apache.airavata.credential.store.store.CredentialStoreException in project airavata by apache.
the class CredentialsDAO method deleteCredentials.
public void deleteCredentials(String gatewayName, String tokenId, Connection connection) throws CredentialStoreException {
String sql = "DELETE FROM CREDENTIALS WHERE GATEWAY_ID=? AND TOKEN_ID=?";
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, gatewayName);
preparedStatement.setString(2, tokenId);
preparedStatement.executeUpdate();
connection.commit();
} catch (SQLException e) {
StringBuilder stringBuilder = new StringBuilder("Error deleting credentials for .");
stringBuilder.append("gateway - ").append(gatewayName);
stringBuilder.append("token id - ").append(tokenId);
log.error(stringBuilder.toString(), e);
throw new CredentialStoreException(stringBuilder.toString(), e);
} finally {
DBUtil.cleanup(preparedStatement);
}
}
use of org.apache.airavata.credential.store.store.CredentialStoreException in project airavata by apache.
the class CredentialsDAO method updateCredentials.
/**
* String createTable = "CREATE TABLE CREDENTIALS\n" + "(\n" + " GATEWAY_ID VARCHAR(256) NOT NULL,\n" +
* " TOKEN_ID VARCHAR(256) NOT NULL,\n" + // Actual token used to identify the credential
* " CREDENTIAL BLOB NOT NULL,\n" + " PORTAL_USER_ID VARCHAR(256) NOT NULL,\n" +
* " TIME_PERSISTED TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n" + " PRIMARY KEY (GATEWAY_ID, TOKEN_ID)\n"
* + ")";
*/
public void updateCredentials(String gatewayId, Credential credential, Connection connection) throws CredentialStoreException {
String sql = "UPDATE CREDENTIALS set CREDENTIAL = ?, PORTAL_USER_ID = ?, TIME_PERSISTED = ?, DESCRIPTION = ?, CREDENTIAL_OWNER_TYPE = ? where GATEWAY_ID = ? and TOKEN_ID = ?";
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement(sql);
InputStream isCert = new ByteArrayInputStream(convertObjectToByteArray(credential));
preparedStatement.setBinaryStream(1, isCert);
preparedStatement.setString(2, credential.getPortalUserName());
preparedStatement.setTimestamp(3, new Timestamp(new java.util.Date().getTime()));
preparedStatement.setString(4, credential.getDescription());
preparedStatement.setString(5, credential.getCredentialOwnerType().toString());
preparedStatement.setString(6, gatewayId);
preparedStatement.setString(7, credential.getToken());
preparedStatement.executeUpdate();
} catch (SQLException e) {
StringBuilder stringBuilder = new StringBuilder("Error updating credentials.");
stringBuilder.append(" gateway - ").append(gatewayId);
stringBuilder.append(" token id - ").append(credential.getToken());
log.error(stringBuilder.toString(), e);
throw new CredentialStoreException(stringBuilder.toString(), e);
} finally {
DBUtil.cleanup(preparedStatement);
}
}
use of org.apache.airavata.credential.store.store.CredentialStoreException in project airavata by apache.
the class CredentialsDAO method addCredentials.
/**
* String createTable = "CREATE TABLE CREDENTIALS\n" + "(\n" + " GATEWAY_ID VARCHAR(256) NOT NULL,\n" +
* " TOKEN_ID VARCHAR(256) NOT NULL,\n" + // Actual token used to identify the credential
* " CREDENTIAL BLOB NOT NULL,\n" + " PORTAL_USER_ID VARCHAR(256) NOT NULL,\n" +
* " TIME_PERSISTED TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n" + " PRIMARY KEY (GATEWAY_ID, TOKEN_ID)\n"
* + ")";
*/
public void addCredentials(String gatewayId, Credential credential, Connection connection) throws CredentialStoreException {
String sql = "INSERT INTO CREDENTIALS (GATEWAY_ID, TOKEN_ID, CREDENTIAL, PORTAL_USER_ID, TIME_PERSISTED, DESCRIPTION, CREDENTIAL_OWNER_TYPE) VALUES (?, ?, ?, ?, ?, ?, ?)";
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, gatewayId);
preparedStatement.setString(2, credential.getToken());
InputStream isCert = new ByteArrayInputStream(convertObjectToByteArray(credential));
preparedStatement.setBinaryStream(3, isCert);
preparedStatement.setString(4, credential.getPortalUserName());
java.util.Date date = new java.util.Date();
Timestamp timestamp = new Timestamp(date.getTime());
preparedStatement.setTimestamp(5, timestamp);
preparedStatement.setString(6, credential.getDescription());
preparedStatement.setString(7, credential.getCredentialOwnerType().toString());
preparedStatement.executeUpdate();
} catch (SQLException e) {
StringBuilder stringBuilder = new StringBuilder("Error persisting credentials.");
stringBuilder.append(" gateway - ").append(gatewayId);
stringBuilder.append(" token id - ").append(credential.getToken());
log.error(stringBuilder.toString(), e);
throw new CredentialStoreException(stringBuilder.toString(), e);
} finally {
DBUtil.cleanup(preparedStatement);
}
}
use of org.apache.airavata.credential.store.store.CredentialStoreException in project airavata by apache.
the class CredentialsDAO method convertByteArrayToObject.
public Object convertByteArrayToObject(byte[] data) throws CredentialStoreException {
ObjectInputStream objectInputStream = null;
Object o = null;
try {
try {
// decrypt the data first
if (encrypt()) {
data = SecurityUtil.decrypt(this.keyStorePath, this.secretKeyAlias, this.keyStorePasswordCallback, data);
}
objectInputStream = new ObjectInputStream(new ByteArrayInputStream(data));
o = objectInputStream.readObject();
} catch (IOException e) {
throw new CredentialStoreException("Error de-serializing object.", e);
} catch (ClassNotFoundException e) {
throw new CredentialStoreException("Error de-serializing object.", e);
} catch (GeneralSecurityException e) {
throw new CredentialStoreException("Error decrypting data.", e);
}
} finally {
if (objectInputStream != null) {
try {
objectInputStream.close();
} catch (IOException e) {
log.error("Error occurred while closing the stream", e);
}
}
}
return o;
}
Aggregations