use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.
the class HardDeleteRecoveryMetadata method getBlobRecordInfo.
private DeserializedBlob getBlobRecordInfo(MessageReadSet readSet, int readSetIndex, int relativeOffset, long blobRecordSize) throws MessageFormatException, IOException {
/* Read the field from the channel */
ByteBuffer blobRecord = ByteBuffer.allocate((int) blobRecordSize);
readSet.writeTo(readSetIndex, Channels.newChannel(new ByteBufferOutputStream(blobRecord)), relativeOffset, blobRecordSize);
blobRecord.flip();
return deserializeAndGetBlobWithVersion(new ByteBufferInputStream(blobRecord));
}
use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.
the class MessageReadSetIndexInputStream method extractEncryptionKey.
/**
* Extract the encryption key from the message at the given index from the readSet.
* @param readSetIndex the index in the readSet for the message from which the encryption key has to be extracted.
* @param encryptionKeyRelativeOffset the relative offset of the encryption key record in the message.
* @param encryptionKeySize the size of the encryption key record in the message.
* @return the extracted encryption key.
* @throws IOException if an IO error is encountered while deserializing the message.
* @throws MessageFormatException if a Message Format error is encountered while deserializing the message.
*/
private ByteBuffer extractEncryptionKey(int readSetIndex, int encryptionKeyRelativeOffset, int encryptionKeySize) throws IOException, MessageFormatException {
ByteBuffer serializedEncryptionKeyRecord = ByteBuffer.allocate(encryptionKeySize);
readSet.writeTo(readSetIndex, Channels.newChannel(new ByteBufferOutputStream(serializedEncryptionKeyRecord)), encryptionKeyRelativeOffset, encryptionKeySize);
serializedEncryptionKeyRecord.flip();
return deserializeBlobEncryptionKey(new ByteBufferInputStream(serializedEncryptionKeyRecord));
}
use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.
the class MessageFormatRecordTest method testBlobCorruption.
private void testBlobCorruption(ByteBuffer blob, long blobSize, int metadataContentSize) throws IOException {
// test corruption cases
blob.rewind();
// case 1: corrupt blob record version
byte savedByte = blob.get(1);
blob.put(1, (byte) (savedByte + 1));
try {
MessageFormatRecord.deserializeBlob(new ByteBufferInputStream(blob));
fail("Failed to detect corruption of Blob record version ");
} catch (MessageFormatException e) {
Assert.assertEquals("Error code mismatch", MessageFormatErrorCodes.Unknown_Format_Version, e.getErrorCode());
}
// case 2: corrupt blob type
blob.rewind();
// reset previously corrupt byte
blob.put(1, savedByte);
savedByte = blob.get(2);
blob.put(2, (byte) (savedByte + 1));
try {
MessageFormatRecord.deserializeBlob(new ByteBufferInputStream(blob));
fail("Failed to detect corruption of blob type");
} catch (MessageFormatException e) {
Assert.assertEquals("Error code mismatch", MessageFormatErrorCodes.Data_Corrupt, e.getErrorCode());
}
// case 3: corrupt part of metadata content
blob.rewind();
// reset previously corrupt byte
blob.put(2, savedByte);
// corrupt part of metadata content
savedByte = blob.get((int) blobSize - metadataContentSize + 10);
blob.put((int) blobSize - metadataContentSize + 10, (byte) (savedByte + 1));
try {
MessageFormatRecord.deserializeBlob(new ByteBufferInputStream(blob));
fail("Failed to detect corruption of metadata content");
} catch (MessageFormatException e) {
Assert.assertEquals("Error code mismatch", MessageFormatErrorCodes.Data_Corrupt, e.getErrorCode());
}
}
use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.
the class ReplicationTestHelper method addPutMessagesToReplicasOfPartition.
public static void addPutMessagesToReplicasOfPartition(List<StoreKey> ids, List<Transformer> transformPerId, List<MockHost> hosts) throws MessageFormatException, IOException {
Iterator<Transformer> transformerIterator = transformPerId.iterator();
for (StoreKey storeKey : ids) {
Transformer transformer = transformerIterator.next();
BlobId id = (BlobId) storeKey;
PutMsgInfoAndBuffer msgInfoAndBuffer = createPutMessage(id, id.getAccountId(), id.getContainerId(), BlobId.isEncrypted(id.toString()));
MessageInfo msgInfo = msgInfoAndBuffer.messageInfo;
ByteBuffer byteBuffer = msgInfoAndBuffer.byteBuffer;
if (transformer != null) {
Message message = new Message(msgInfo, new ByteBufferInputStream(byteBuffer));
TransformationOutput output = transformer.transform(message);
assertNull(output.getException());
message = output.getMsg();
byteBuffer = ByteBuffer.wrap(Utils.readBytesFromStream(message.getStream(), (int) message.getMessageInfo().getSize()));
msgInfo = message.getMessageInfo();
}
for (MockHost host : hosts) {
host.addMessage(id.getPartition(), msgInfo, byteBuffer.duplicate());
}
}
}
use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.
the class GCMCryptoService method decryptKey.
@Override
public SecretKeySpec decryptKey(ByteBuffer toDecrypt, SecretKeySpec key) throws GeneralSecurityException {
ByteBuffer decryptedKey = decrypt(toDecrypt, key);
DeserializedKey deserializedKey = null;
try {
deserializedKey = deserializeKey(new ByteBufferInputStream(decryptedKey));
} catch (Exception e) {
throw new GeneralSecurityException("Exception thrown on deserializing key");
}
return new SecretKeySpec(deserializedKey.getEncodedKey(), deserializedKey.getKeyGenAlgo());
}
Aggregations