Search in sources :

Example 11 with JdbcTransaction

use of com.fathomdb.jdbc.JdbcTransaction in project platformlayer by platformlayer.

the class JdbcManagedItemRepository method findAll.

@Override
@JdbcTransaction
public <T extends ItemBase> List<T> findAll(ModelClass<T> modelClass, ProjectId project, boolean fetchTags, SecretProvider secretProvider, Filter filter) throws RepositoryException {
    DbHelper db = new DbHelper(modelClass, project);
    try {
        int projectId = db.mapToValue(project);
        int modelId = db.mapToValue(modelClass.getItemType());
        int serviceId = db.mapToValue(modelClass.getServiceType());
        String filterKey = null;
        JoinedQueryResult result = db.queries.listItems(serviceId, modelId, projectId, filterKey);
        List<T> items = mapItemsAndTags(project, secretProvider, db, result);
        return applyFilter(items, filter);
    } catch (SQLException e) {
        throw new RepositoryException("Error fetching items", e);
    } finally {
        db.close();
    }
}
Also used : SQLException(java.sql.SQLException) RepositoryException(org.platformlayer.RepositoryException) JoinedQueryResult(com.fathomdb.jpa.impl.JoinedQueryResult) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 12 with JdbcTransaction

use of com.fathomdb.jdbc.JdbcTransaction in project platformlayer by platformlayer.

the class JdbcServiceAuthorizationRepository method setPrivateData.

@Override
@JdbcTransaction
public void setPrivateData(ServiceType serviceType, ProjectId project, ServiceMetadataKey metadataKey, String value) throws RepositoryException {
    DbHelper db = new DbHelper(serviceType, project, metadataKey);
    try {
        CryptoKey secret = FathomdbCrypto.generateKey();
        byte[] plaintext = Utf8.getBytes(value);
        byte[] ciphertext = FathomdbCrypto.encrypt(secret, plaintext);
        // TODO: Encode this differently from items??
        byte[] secretData = secretHelper.encodeItemSecret(secret);
        if (serviceType == null) {
            db.insertProjectMetadata(ciphertext, secretData);
        } else {
            db.insertServiceMetadata(ciphertext, secretData);
        }
    } catch (SQLException e) {
        throw new RepositoryException("Error running query", e);
    } finally {
        db.close();
    }
}
Also used : SQLException(java.sql.SQLException) AesCryptoKey(com.fathomdb.crypto.AesCryptoKey) CryptoKey(com.fathomdb.crypto.CryptoKey) RepositoryException(org.platformlayer.RepositoryException) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 13 with JdbcTransaction

use of com.fathomdb.jdbc.JdbcTransaction in project platformlayer by platformlayer.

the class JdbcServiceAuthorizationRepository method findPrivateData.

@Override
@JdbcTransaction
public String findPrivateData(ServiceType serviceType, ProjectId project, ServiceMetadataKey metadataKey) throws RepositoryException {
    DbHelper db = new DbHelper(serviceType, project, metadataKey);
    List<String> values = Lists.newArrayList();
    ResultSet rs = null;
    try {
        if (serviceType == null) {
            rs = db.selectProjectMetadata();
        } else {
            rs = db.selectServiceMetadata();
        }
        while (rs.next()) {
            CryptoKey secretKey = secretHelper.getSecret(rs.getBytes("secret"));
            if (secretKey instanceof AesCryptoKey) {
                log.warn("Legacy AES crypto key: findPrivateData[{}, {}, {}]", new Object[] { serviceType, project, metadataKey });
            }
            byte[] plaintext = FathomdbCrypto.decrypt(secretKey, rs.getBytes("data"));
            String value = Utf8.toString(plaintext);
            values.add(value);
        }
    } catch (SQLException e) {
        throw new RepositoryException("Error running query", e);
    } finally {
        JdbcUtils.safeClose(rs);
        db.close();
    }
    if (values.size() == 0) {
        return null;
    }
    if (values.size() != 1) {
        throw new IllegalStateException("Found duplicate results for primary key");
    }
    return values.get(0);
}
Also used : SQLException(java.sql.SQLException) AesCryptoKey(com.fathomdb.crypto.AesCryptoKey) ResultSet(java.sql.ResultSet) AesCryptoKey(com.fathomdb.crypto.AesCryptoKey) CryptoKey(com.fathomdb.crypto.CryptoKey) RepositoryException(org.platformlayer.RepositoryException) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 14 with JdbcTransaction

use of com.fathomdb.jdbc.JdbcTransaction in project platformlayer by platformlayer.

the class JdbcServiceAuthorizationRepository method createAuthorization.

@Override
@JdbcTransaction
public ServiceAuthorization createAuthorization(ProjectId project, ServiceAuthorization authorization) throws RepositoryException {
    try {
        ServiceType serviceType = new ServiceType(authorization.serviceType);
        JdbcConnection connection = connectionProvider.get();
        int serviceId = JdbcRepositoryHelpers.getServiceKey(connection, serviceType);
        int projectId = JdbcRepositoryHelpers.getProjectKey(connection, project);
        final String sql = "INSERT INTO service_authorizations (service, project, data) VALUES (?, ?, ?)";
        PreparedStatement ps = connection.prepareStatement(sql);
        ResultSet rs = null;
        try {
            ps.setInt(1, serviceId);
            ps.setInt(2, projectId);
            ps.setString(3, authorization.data);
            int updateCount = ps.executeUpdate();
            if (updateCount != 1) {
                throw new IllegalStateException("Unexpected number of rows inserted");
            }
        } finally {
            JdbcUtils.safeClose(rs);
            JdbcUtils.safeClose(ps);
        }
        return authorization;
    } catch (SQLException e) {
        throw new RepositoryException("Error running query", e);
    }
}
Also used : SQLException(java.sql.SQLException) ServiceType(org.platformlayer.ids.ServiceType) ResultSet(java.sql.ResultSet) JdbcConnection(com.fathomdb.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) RepositoryException(org.platformlayer.RepositoryException) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 15 with JdbcTransaction

use of com.fathomdb.jdbc.JdbcTransaction in project platformlayer by platformlayer.

the class JdbcServiceAuthorizationRepository method findServiceAuthorization.

@Override
@JdbcTransaction
public ServiceAuthorization findServiceAuthorization(ServiceType serviceType, ProjectId project) throws RepositoryException {
    try {
        JdbcConnection connection = connectionProvider.get();
        int serviceId = JdbcRepositoryHelpers.getServiceKey(connection, serviceType);
        int projectId = JdbcRepositoryHelpers.getProjectKey(connection, project);
        String sql = "SELECT data FROM service_authorizations WHERE service=? and project=?";
        List<ServiceAuthorization> items = Lists.newArrayList();
        PreparedStatement ps = connection.prepareStatement(sql);
        ResultSet rs = null;
        try {
            ps.setInt(1, serviceId);
            ps.setInt(2, projectId);
            rs = ps.executeQuery();
            while (rs.next()) {
                items.add(mapRow(serviceType, rs));
            }
        } finally {
            JdbcUtils.safeClose(rs);
            JdbcUtils.safeClose(ps);
        }
        if (items.size() == 0) {
            return null;
        }
        if (items.size() != 1) {
            throw new IllegalStateException("Found duplicate results for primary key: " + serviceType + ":" + project);
        }
        return items.get(0);
    } catch (SQLException e) {
        throw new RepositoryException("Error running query", e);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) JdbcConnection(com.fathomdb.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) RepositoryException(org.platformlayer.RepositoryException) ServiceAuthorization(org.platformlayer.xaas.model.ServiceAuthorization) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Aggregations

JdbcTransaction (com.fathomdb.jdbc.JdbcTransaction)30 SQLException (java.sql.SQLException)30 RepositoryException (org.platformlayer.RepositoryException)30 ProjectId (org.platformlayer.ids.ProjectId)7 CryptoKey (com.fathomdb.crypto.CryptoKey)6 AesCryptoKey (com.fathomdb.crypto.AesCryptoKey)4 IOException (java.io.IOException)4 SecretStore (org.platformlayer.auth.crypto.SecretStore)4 Writer (org.platformlayer.auth.crypto.SecretStore.Writer)4 JoinedQueryResult (com.fathomdb.jpa.impl.JoinedQueryResult)3 PublicKey (java.security.PublicKey)3 ResultSet (java.sql.ResultSet)3 Tag (org.platformlayer.core.model.Tag)3 ManagedItemId (org.platformlayer.ids.ManagedItemId)3 JdbcConnection (com.fathomdb.jdbc.JdbcConnection)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 KeyPair (java.security.KeyPair)2 X509Certificate (java.security.cert.X509Certificate)2 PreparedStatement (java.sql.PreparedStatement)2 ItemBase (org.platformlayer.core.model.ItemBase)2