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