Search in sources :

Example 1 with AesCryptoKey

use of com.fathomdb.crypto.AesCryptoKey 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 2 with AesCryptoKey

use of com.fathomdb.crypto.AesCryptoKey in project platformlayer by platformlayer.

the class JdbcManagedItemRepository method mapToModel.

static <T extends ItemBase> T mapToModel(ProjectId project, ServiceType serviceType, ItemType itemType, ItemEntity entity, JaxbHelper jaxb, SecretProvider secretProvider) throws RepositoryException {
    try {
        int id = entity.id;
        String key = entity.key;
        int stateCode = entity.state;
        byte[] data = entity.data;
        SecretInfo secret = new SecretInfo(entity.secret);
        CryptoKey itemSecret = secretProvider.getItemSecret(secret);
        if (itemSecret == null) {
            throw new RepositoryException("Could not get secret to decrypt item");
        }
        if (itemSecret instanceof AesCryptoKey) {
            log.warn("Legacy AES crypto key on {} {} {} {}", new Object[] { project, serviceType, itemType, entity });
        }
        secret.unlock(itemSecret);
        byte[] plaintext = FathomdbCrypto.decrypt(itemSecret, data);
        String xml = new String(plaintext, Charsets.UTF_8);
        T model = (T) jaxb.unmarshal(xml);
        model.state = ManagedItemState.fromCode(stateCode);
        model.secret = secret;
        PlatformLayerKey plk = new PlatformLayerKey(null, project, serviceType, itemType, new ManagedItemId(key));
        model.setKey(plk);
        return model;
    } catch (JAXBException e) {
        throw new RepositoryException("Error deserializing data", e);
    }
}
Also used : AesCryptoKey(com.fathomdb.crypto.AesCryptoKey) SecretInfo(org.platformlayer.core.model.SecretInfo) JAXBException(javax.xml.bind.JAXBException) CryptoKey(com.fathomdb.crypto.CryptoKey) AesCryptoKey(com.fathomdb.crypto.AesCryptoKey) RepositoryException(org.platformlayer.RepositoryException) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) ManagedItemId(org.platformlayer.ids.ManagedItemId)

Aggregations

AesCryptoKey (com.fathomdb.crypto.AesCryptoKey)2 CryptoKey (com.fathomdb.crypto.CryptoKey)2 RepositoryException (org.platformlayer.RepositoryException)2 JdbcTransaction (com.fathomdb.jdbc.JdbcTransaction)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 JAXBException (javax.xml.bind.JAXBException)1 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)1 SecretInfo (org.platformlayer.core.model.SecretInfo)1 ManagedItemId (org.platformlayer.ids.ManagedItemId)1