use of com.emc.storageos.db.client.model.UserSecretKey in project coprhd-controller by CoprHD.
the class DbClientTest method testEncryption.
@Test
public void testEncryption() throws Exception {
_logger.info("Starting encryption test");
final int count = 100;
Map<URI, UserSecretKey> expected = new HashMap<>();
DbClient dbClient = _dbClient;
TypeMap.setEncryptionProviders(_encryptionProvider, _encryptionProvider);
for (int index = 0; index < count; index++) {
UserSecretKey key = new UserSecretKey();
key.setId(URIUtil.createId(UserSecretKey.class));
key.setFirstKey(UUID.randomUUID().toString());
key.setSecondKey("");
expected.put(key.getId(), key);
dbClient.persistObject(key);
}
Iterator<URI> it = expected.keySet().iterator();
while (it.hasNext()) {
URI id = it.next();
UserSecretKey original = expected.get(id);
UserSecretKey queried = dbClient.queryObject(UserSecretKey.class, id);
Assert.assertEquals(original.getFirstKey(), queried.getFirstKey());
Assert.assertEquals(original.getSecondKey(), queried.getSecondKey());
}
// set encryption provider to null, so, we can read and write out in plain text
TypeMap.setEncryptionProviders(null, null);
UserSecretKey queried = null;
it = expected.keySet().iterator();
while (it.hasNext()) {
URI id = it.next();
UserSecretKey original = expected.get(id);
queried = dbClient.queryObject(UserSecretKey.class, id);
Assert.assertFalse(original.getFirstKey().equals(queried.getFirstKey()));
Assert.assertFalse(original.getSecondKey().equals(queried.getSecondKey()));
}
queried.setSecondKey("");
dbClient.persistObject(queried);
TypeMap.setEncryptionProviders(_encryptionProvider, _encryptionProvider);
// set the encryption provider, try to read plain data via a provider
// the provider will reject it == this is a state we should never be in
boolean good = false;
try {
queried = dbClient.queryObject(UserSecretKey.class, queried.getId());
} catch (IllegalStateException ex) {
good = true;
}
Assert.assertTrue(good);
// set encryption back so that the objects can be deleted
queried.setSecondKey("");
dbClient.persistObject(queried);
_logger.info("Ended encryption test");
}
Aggregations