use of com.github.ambry.config.CryptoServiceConfig in project ambry by linkedin.
the class ChunkFillTest method fillChunksAndAssertSuccess.
/**
* Create a {@link PutOperation} and pass in a channel with the blobSize set by the caller; and test the chunk
* filling flow for puts.
* Note that this test is for the chunk filling flow, not for the ChunkFiller thread (which never gets exercised,
* as we do not even instantiate the {@link PutManager})
*/
private void fillChunksAndAssertSuccess() throws Exception {
VerifiableProperties vProps = getNonBlockingRouterProperties();
MockClusterMap mockClusterMap = new MockClusterMap();
RouterConfig routerConfig = new RouterConfig(vProps);
routerMetrics = new NonBlockingRouterMetrics(mockClusterMap, routerConfig);
ResponseHandler responseHandler = new ResponseHandler(mockClusterMap);
short accountId = Utils.getRandomShort(random);
short containerId = Utils.getRandomShort(random);
BlobProperties putBlobProperties = new BlobProperties(blobSize, "serviceId", "memberId", "contentType", false, Utils.Infinite_Time, accountId, containerId, testEncryption, null, null, null);
Random random = new Random();
byte[] putUserMetadata = new byte[10];
random.nextBytes(putUserMetadata);
putContent = new byte[blobSize];
random.nextBytes(putContent);
final ReadableStreamChannel putChannel = new ByteBufferReadableStreamChannel(ByteBuffer.wrap(putContent));
FutureResult<String> futureResult = new FutureResult<String>();
MockTime time = new MockTime();
MockNetworkClientFactory networkClientFactory = new MockNetworkClientFactory(vProps, null, 0, 0, 0, null, time);
if (testEncryption) {
kms = new MockKeyManagementService(new KMSConfig(vProps), TestUtils.getRandomKey(SingleKeyManagementServiceTest.DEFAULT_KEY_SIZE_CHARS));
cryptoService = new MockCryptoService(new CryptoServiceConfig(vProps));
cryptoJobHandler = new CryptoJobHandler(CryptoJobHandlerTest.DEFAULT_THREAD_COUNT);
}
MockRouterCallback routerCallback = new MockRouterCallback(networkClientFactory.getNetworkClient(), Collections.EMPTY_LIST);
PutOperation op = PutOperation.forUpload(routerConfig, routerMetrics, mockClusterMap, new LoggingNotificationSystem(), new InMemAccountService(true, false), putUserMetadata, putChannel, PutBlobOptions.DEFAULT, futureResult, null, routerCallback, null, kms, cryptoService, cryptoJobHandler, time, putBlobProperties, MockClusterMap.DEFAULT_PARTITION_CLASS, quotaChargeCallback);
op.startOperation();
numChunks = RouterUtils.getNumChunksForBlobAndChunkSize(blobSize, chunkSize);
compositeBuffers = new ByteBuf[numChunks];
compositeEncryptionKeys = new ByteBuffer[numChunks];
compositeBlobIds = new BlobId[numChunks];
final AtomicReference<Exception> operationException = new AtomicReference<Exception>(null);
int chunksLeftToBeFilled = numChunks;
do {
if (testEncryption) {
int chunksPerBatch = Math.min(routerConfig.routerMaxInMemPutChunks, chunksLeftToBeFilled);
CountDownLatch onPollLatch = new CountDownLatch(chunksPerBatch);
routerCallback.setOnPollLatch(onPollLatch);
op.fillChunks();
Assert.assertTrue("Latch should have been zeroed out", onPollLatch.await(1000, TimeUnit.MILLISECONDS));
chunksLeftToBeFilled -= chunksPerBatch;
} else {
op.fillChunks();
}
// since the channel is ByteBuffer based.
for (PutOperation.PutChunk putChunk : op.putChunks) {
if (putChunk.isFree()) {
continue;
}
Assert.assertEquals("Chunk should be ready.", PutOperation.ChunkState.Ready, putChunk.getState());
ByteBuf buf = putChunk.buf.retainedDuplicate();
totalSizeWritten += buf.readableBytes();
compositeBuffers[putChunk.getChunkIndex()] = buf;
if (testEncryption) {
compositeEncryptionKeys[putChunk.getChunkIndex()] = putChunk.encryptedPerBlobKey.duplicate();
compositeBlobIds[putChunk.getChunkIndex()] = putChunk.chunkBlobId;
}
putChunk.clear();
}
} while (!op.isChunkFillingDone());
if (!testEncryption) {
Assert.assertEquals("total size written out should match the blob size", blobSize, totalSizeWritten);
}
// for encrypted path, size will be implicitly tested via assertDataIdentity
Exception exception = operationException.get();
if (exception != null) {
throw exception;
}
assertDataIdentity(mockClusterMap);
}
use of com.github.ambry.config.CryptoServiceConfig in project ambry by linkedin.
the class GetManagerTest method resetEncryptionObjects.
/**
* Resets objects related to encryption testing
* @throws GeneralSecurityException
*/
private void resetEncryptionObjects() throws GeneralSecurityException {
if (testEncryption) {
VerifiableProperties vProps = new VerifiableProperties(new Properties());
kms = new SingleKeyManagementService(new KMSConfig(vProps), TestUtils.getRandomKey(SingleKeyManagementServiceTest.DEFAULT_KEY_SIZE_CHARS));
cryptoService = new GCMCryptoService(new CryptoServiceConfig(vProps));
cryptoJobHandler = new CryptoJobHandler(CryptoJobHandlerTest.DEFAULT_THREAD_COUNT);
}
}
use of com.github.ambry.config.CryptoServiceConfig in project ambry by linkedin.
the class CryptoJobHandlerTest method testEncryptionFailure.
/**
* Tests {@link CryptoJobHandler} for failures during encryption
* @throws InterruptedException
* @throws GeneralSecurityException
*/
@Test
public void testEncryptionFailure() throws InterruptedException, GeneralSecurityException {
cryptoJobHandler.close();
MockCryptoService mockCryptoService = new MockCryptoService(new CryptoServiceConfig(verifiableProperties));
mockCryptoService.exceptionOnEncryption.set(new GeneralSecurityException("Exception to test", new IllegalStateException()));
cryptoJobHandler = new CryptoJobHandler(DEFAULT_THREAD_COUNT);
SecretKeySpec perBlobSecretKey = kms.getRandomKey();
testFailureOnEncryption(perBlobSecretKey, mockCryptoService, kms);
mockCryptoService.clearStates();
cryptoJobHandler.close();
MockKeyManagementService mockKms = new MockKeyManagementService(new KMSConfig(verifiableProperties), defaultKey);
mockKms.exceptionToThrow.set(new GeneralSecurityException("Exception to test", new IllegalStateException()));
cryptoJobHandler = new CryptoJobHandler(DEFAULT_THREAD_COUNT);
testFailureOnEncryption(perBlobSecretKey, cryptoService, mockKms);
}
use of com.github.ambry.config.CryptoServiceConfig in project ambry by linkedin.
the class CryptoJobHandlerTest method testDecryptionFailure.
/**
* Tests {@link CryptoJobHandler} for failures during decryption
* @throws InterruptedException
* @throws GeneralSecurityException
*/
@Test
public void testDecryptionFailure() throws InterruptedException, GeneralSecurityException {
cryptoJobHandler.close();
MockCryptoService mockCryptoService = new MockCryptoService(new CryptoServiceConfig(verifiableProperties));
mockCryptoService.exceptionOnDecryption.set(new GeneralSecurityException("Exception to test", new IllegalStateException()));
cryptoJobHandler = new CryptoJobHandler(DEFAULT_THREAD_COUNT);
SecretKeySpec perBlobSecretKey = kms.getRandomKey();
testFailureOnDecryption(perBlobSecretKey, null, false, mockCryptoService, kms);
mockCryptoService.clearStates();
cryptoJobHandler.close();
MockKeyManagementService mockKms = new MockKeyManagementService(new KMSConfig(verifiableProperties), defaultKey);
cryptoJobHandler = new CryptoJobHandler(DEFAULT_THREAD_COUNT);
testFailureOnDecryption(perBlobSecretKey, mockKms, true, cryptoService, mockKms);
}
Aggregations