Search in sources :

Example 26 with CredentialStoreException

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();
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) CredentialStoreException(org.apache.airavata.credential.store.store.CredentialStoreException)

Example 27 with CredentialStoreException

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);
    }
}
Also used : CredentialStoreException(org.apache.airavata.credential.store.store.CredentialStoreException)

Example 28 with CredentialStoreException

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);
    }
}
Also used : CredentialStoreException(org.apache.airavata.credential.store.store.CredentialStoreException)

Example 29 with CredentialStoreException

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);
    }
}
Also used : CredentialStoreException(org.apache.airavata.credential.store.store.CredentialStoreException)

Example 30 with CredentialStoreException

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;
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) CredentialStoreException(org.apache.airavata.credential.store.store.CredentialStoreException)

Aggregations

CredentialStoreException (org.apache.airavata.credential.store.store.CredentialStoreException)38 Credential (org.apache.airavata.credential.store.credential.Credential)14 org.apache.airavata.model.credential.store (org.apache.airavata.model.credential.store)10 SQLException (java.sql.SQLException)9 IOException (java.io.IOException)6 PreparedStatement (java.sql.PreparedStatement)6 ApplicationSettingsException (org.apache.airavata.common.exception.ApplicationSettingsException)6 CommunityUser (org.apache.airavata.credential.store.credential.CommunityUser)6 GFacException (org.apache.airavata.gfac.core.GFacException)5 TException (org.apache.thrift.TException)5 JSchException (com.jcraft.jsch.JSchException)4 Session (com.jcraft.jsch.Session)4 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 SSHCredential (org.apache.airavata.credential.store.credential.impl.ssh.SSHCredential)4 ProcessContext (org.apache.airavata.gfac.core.context.ProcessContext)4 File (java.io.File)3 ResultSet (java.sql.ResultSet)3 ArrayList (java.util.ArrayList)3 StorageResourceDescription (org.apache.airavata.model.appcatalog.storageresource.StorageResourceDescription)3