Search in sources :

Example 1 with UserSecretKey

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");
}
Also used : DbClient(com.emc.storageos.db.client.DbClient) InternalDbClient(com.emc.storageos.db.client.upgrade.InternalDbClient) HashMap(java.util.HashMap) NamedURI(com.emc.storageos.db.client.model.NamedURI) URI(java.net.URI) PrefixConstraint(com.emc.storageos.db.client.constraint.PrefixConstraint) AlternateIdConstraint(com.emc.storageos.db.client.constraint.AlternateIdConstraint) ContainmentPrefixConstraint(com.emc.storageos.db.client.constraint.ContainmentPrefixConstraint) AggregatedConstraint(com.emc.storageos.db.client.constraint.AggregatedConstraint) ContainmentPermissionsConstraint(com.emc.storageos.db.client.constraint.ContainmentPermissionsConstraint) Constraint(com.emc.storageos.db.client.constraint.Constraint) ContainmentConstraint(com.emc.storageos.db.client.constraint.ContainmentConstraint) UserSecretKey(com.emc.storageos.db.client.model.UserSecretKey) Test(org.junit.Test)

Aggregations

DbClient (com.emc.storageos.db.client.DbClient)1 AggregatedConstraint (com.emc.storageos.db.client.constraint.AggregatedConstraint)1 AlternateIdConstraint (com.emc.storageos.db.client.constraint.AlternateIdConstraint)1 Constraint (com.emc.storageos.db.client.constraint.Constraint)1 ContainmentConstraint (com.emc.storageos.db.client.constraint.ContainmentConstraint)1 ContainmentPermissionsConstraint (com.emc.storageos.db.client.constraint.ContainmentPermissionsConstraint)1 ContainmentPrefixConstraint (com.emc.storageos.db.client.constraint.ContainmentPrefixConstraint)1 PrefixConstraint (com.emc.storageos.db.client.constraint.PrefixConstraint)1 NamedURI (com.emc.storageos.db.client.model.NamedURI)1 UserSecretKey (com.emc.storageos.db.client.model.UserSecretKey)1 InternalDbClient (com.emc.storageos.db.client.upgrade.InternalDbClient)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1