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