Search in sources :

Example 11 with VerifiableProperties

use of com.github.ambry.config.VerifiableProperties in project ambry by linkedin.

the class DeleteManagerTest method init.

/**
 * Initializes ClusterMap, Router, mock servers, and an {@code BlobId} to be deleted.
 */
@Before
public void init() throws Exception {
    VerifiableProperties vProps = new VerifiableProperties(getNonBlockingRouterProperties());
    mockTime = new MockTime();
    mockSelectorState = new AtomicReference<MockSelectorState>(MockSelectorState.Good);
    clusterMap = new MockClusterMap();
    serverLayout = new MockServerLayout(clusterMap);
    RouterConfig routerConfig = new RouterConfig(vProps);
    router = new NonBlockingRouter(routerConfig, new NonBlockingRouterMetrics(clusterMap), new MockNetworkClientFactory(vProps, mockSelectorState, MAX_PORTS_PLAIN_TEXT, MAX_PORTS_SSL, CHECKOUT_TIMEOUT_MS, serverLayout, mockTime), new LoggingNotificationSystem(), clusterMap, null, null, null, mockTime);
    List<PartitionId> mockPartitions = clusterMap.getWritablePartitionIds();
    partition = mockPartitions.get(ThreadLocalRandom.current().nextInt(mockPartitions.size()));
    blobId = new BlobId(routerConfig.routerBlobidCurrentVersion, BlobId.BlobIdType.NATIVE, clusterMap.getLocalDatacenterId(), Utils.getRandomShort(TestUtils.RANDOM), Utils.getRandomShort(TestUtils.RANDOM), partition, false);
    blobIdString = blobId.getID();
}
Also used : VerifiableProperties(com.github.ambry.config.VerifiableProperties) PartitionId(com.github.ambry.clustermap.PartitionId) RouterConfig(com.github.ambry.config.RouterConfig) LoggingNotificationSystem(com.github.ambry.commons.LoggingNotificationSystem) BlobId(com.github.ambry.commons.BlobId) MockTime(com.github.ambry.utils.MockTime) MockClusterMap(com.github.ambry.clustermap.MockClusterMap) Before(org.junit.Before)

Example 12 with VerifiableProperties

use of com.github.ambry.config.VerifiableProperties in project ambry by linkedin.

the class GCMCryptoServiceTest method testEncryptDecryptKeys.

/**
 * Tests encryption and decryption of keys with random data
 */
@Test
public void testEncryptDecryptKeys() throws Exception {
    for (int j = 0; j < 5; j++) {
        String keyToEncrypt = TestUtils.getRandomKey(DEFAULT_KEY_SIZE_IN_CHARS);
        Properties props = getKMSProperties(keyToEncrypt, DEFAULT_KEY_SIZE_IN_CHARS);
        VerifiableProperties verifiableProperties = new VerifiableProperties((props));
        SecretKeySpec secretKeyToEncrypt = new SecretKeySpec(Hex.decode(keyToEncrypt), "AES");
        String keyToBeEncrypted = TestUtils.getRandomKey(DEFAULT_KEY_SIZE_IN_CHARS);
        SecretKeySpec secretKeyToBeEncrypted = new SecretKeySpec(Hex.decode(keyToBeEncrypted), "AES");
        CryptoService<SecretKeySpec> cryptoService = new GCMCryptoServiceFactory(verifiableProperties, REGISTRY).getCryptoService();
        ByteBuffer encryptedBytes = cryptoService.encryptKey(secretKeyToBeEncrypted, secretKeyToEncrypt);
        Assert.assertFalse("Encrypted key and plain key should not match", Arrays.equals(keyToBeEncrypted.getBytes(), encryptedBytes.array()));
        SecretKeySpec decryptedKey = cryptoService.decryptKey(encryptedBytes, secretKeyToEncrypt);
        Assert.assertEquals("Decrypted Key and original key mismatch", secretKeyToBeEncrypted, decryptedKey);
    }
}
Also used : VerifiableProperties(com.github.ambry.config.VerifiableProperties) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 13 with VerifiableProperties

use of com.github.ambry.config.VerifiableProperties in project ambry by linkedin.

the class GCMCryptoServiceTest method testEncryptDecryptBytes.

/**
 * Tests basic encryption and decryption for different sizes of keys and random data in bytes
 */
@Test
public void testEncryptDecryptBytes() throws Exception {
    for (int j = 0; j < 3; j++) {
        String key = TestUtils.getRandomKey(DEFAULT_KEY_SIZE_IN_CHARS);
        Properties props = getKMSProperties(key, DEFAULT_KEY_SIZE_IN_CHARS);
        VerifiableProperties verifiableProperties = new VerifiableProperties((props));
        SecretKeySpec secretKeySpec = new SecretKeySpec(Hex.decode(key), "AES");
        CryptoService<SecretKeySpec> cryptoService = new GCMCryptoServiceFactory(verifiableProperties, REGISTRY).getCryptoService();
        for (int i = 0; i < 5; i++) {
            int size = TestUtils.RANDOM.nextInt(MAX_DATA_SIZE);
            byte[] randomData = new byte[size];
            TestUtils.RANDOM.nextBytes(randomData);
            ByteBuffer toEncrypt = ByteBuffer.wrap(randomData);
            ByteBuffer encryptedBytes = cryptoService.encrypt(toEncrypt, secretKeySpec);
            Assert.assertFalse("Encrypted bytes and plain bytes should not match", Arrays.equals(randomData, encryptedBytes.array()));
            ByteBuffer decryptedBytes = cryptoService.decrypt(encryptedBytes, secretKeySpec);
            Assert.assertArrayEquals("Decrypted bytes and plain bytes should match", randomData, decryptedBytes.array());
        }
    }
}
Also used : VerifiableProperties(com.github.ambry.config.VerifiableProperties) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 14 with VerifiableProperties

use of com.github.ambry.config.VerifiableProperties in project ambry by linkedin.

the class GCMCryptoServiceTest method testGMCryptoServiceFactory.

/**
 * Test {@link GCMCryptoServiceFactory}
 */
@Test
public void testGMCryptoServiceFactory() throws Exception {
    // happy path
    VerifiableProperties verifiableProperties = new VerifiableProperties((new Properties()));
    new GCMCryptoServiceFactory(verifiableProperties, REGISTRY).getCryptoService();
    // unrecognized mode
    String key = TestUtils.getRandomKey(DEFAULT_KEY_SIZE_IN_CHARS);
    Properties props = getKMSProperties(key, DEFAULT_KEY_SIZE_IN_CHARS);
    props.setProperty("crypto.service.encryption.decryption.mode", "CBC");
    verifiableProperties = new VerifiableProperties((props));
    try {
        CryptoService<SecretKeySpec> cryptoService = new GCMCryptoServiceFactory(verifiableProperties, REGISTRY).getCryptoService();
        Assert.fail("IllegalArgumentException should have thrown for un-recognized mode ");
    } catch (IllegalArgumentException e) {
    }
}
Also used : VerifiableProperties(com.github.ambry.config.VerifiableProperties) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) Test(org.junit.Test)

Example 15 with VerifiableProperties

use of com.github.ambry.config.VerifiableProperties in project ambry by linkedin.

the class GCMCryptoServiceTest method testDecryptionFailure.

/**
 * Tests failure scenarios for decryption
 * @throws InstantiationException
 */
@Test
public void testDecryptionFailure() throws InstantiationException {
    String key = TestUtils.getRandomKey(DEFAULT_KEY_SIZE_IN_CHARS);
    Properties props = getKMSProperties(key, DEFAULT_KEY_SIZE_IN_CHARS);
    VerifiableProperties verifiableProperties = new VerifiableProperties((props));
    SecretKeySpec secretKeySpec = new SecretKeySpec(Hex.decode(key), "AES");
    CryptoService<SecretKeySpec> cryptoService = new GCMCryptoServiceFactory(verifiableProperties, REGISTRY).getCryptoService();
    int size = TestUtils.RANDOM.nextInt(MAX_DATA_SIZE);
    byte[] randomData = new byte[size];
    TestUtils.RANDOM.nextBytes(randomData);
    ByteBuffer toDecrypt = ByteBuffer.wrap(randomData);
    try {
        cryptoService.decrypt(toDecrypt, secretKeySpec);
        Assert.fail("Decryption should have failed as input data is not encrypted");
    } catch (GeneralSecurityException e) {
    }
    try {
        cryptoService.decryptKey(toDecrypt, secretKeySpec);
        Assert.fail("Decryption of key should have failed as input data is not encrypted");
    } catch (GeneralSecurityException e) {
    }
}
Also used : VerifiableProperties(com.github.ambry.config.VerifiableProperties) SecretKeySpec(javax.crypto.spec.SecretKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

VerifiableProperties (com.github.ambry.config.VerifiableProperties)137 Properties (java.util.Properties)88 Test (org.junit.Test)71 MetricRegistry (com.codahale.metrics.MetricRegistry)42 ClusterMapConfig (com.github.ambry.config.ClusterMapConfig)40 BlobProperties (com.github.ambry.messageformat.BlobProperties)35 MockClusterMap (com.github.ambry.clustermap.MockClusterMap)25 StoreConfig (com.github.ambry.config.StoreConfig)25 ArrayList (java.util.ArrayList)25 ClusterMap (com.github.ambry.clustermap.ClusterMap)24 IOException (java.io.IOException)21 LoggingNotificationSystem (com.github.ambry.commons.LoggingNotificationSystem)20 RouterConfig (com.github.ambry.config.RouterConfig)18 CountDownLatch (java.util.concurrent.CountDownLatch)18 ClusterAgentsFactory (com.github.ambry.clustermap.ClusterAgentsFactory)15 MockTime (com.github.ambry.utils.MockTime)14 BlobId (com.github.ambry.commons.BlobId)13 HashMap (java.util.HashMap)12 File (java.io.File)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)11