Search in sources :

Example 61 with ByteBufferInputStream

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));
}
Also used : ByteBufferOutputStream(com.github.ambry.utils.ByteBufferOutputStream) ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) ByteBuffer(java.nio.ByteBuffer)

Example 62 with ByteBufferInputStream

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));
}
Also used : ByteBufferOutputStream(com.github.ambry.utils.ByteBufferOutputStream) ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) ByteBuffer(java.nio.ByteBuffer)

Example 63 with ByteBufferInputStream

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());
    }
}
Also used : ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream)

Example 64 with ByteBufferInputStream

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());
        }
    }
}
Also used : Transformer(com.github.ambry.store.Transformer) Message(com.github.ambry.store.Message) TransformationOutput(com.github.ambry.store.TransformationOutput) ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) StoreKey(com.github.ambry.store.StoreKey) BlobId(com.github.ambry.commons.BlobId) ByteBuffer(java.nio.ByteBuffer) MessageInfo(com.github.ambry.store.MessageInfo)

Example 65 with ByteBufferInputStream

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());
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) MessageFormatException(com.github.ambry.messageformat.MessageFormatException) GeneralSecurityException(java.security.GeneralSecurityException)

Aggregations

ByteBufferInputStream (com.github.ambry.utils.ByteBufferInputStream)79 ByteBuffer (java.nio.ByteBuffer)48 DataInputStream (java.io.DataInputStream)34 Test (org.junit.Test)25 ArrayList (java.util.ArrayList)19 InputStream (java.io.InputStream)16 MessageInfo (com.github.ambry.store.MessageInfo)15 StoreKey (com.github.ambry.store.StoreKey)14 IOException (java.io.IOException)12 MetricRegistry (com.codahale.metrics.MetricRegistry)10 Random (java.util.Random)10 GetResponse (com.github.ambry.protocol.GetResponse)8 MockId (com.github.ambry.store.MockId)8 ByteBufferOutputStream (com.github.ambry.utils.ByteBufferOutputStream)8 Crc32 (com.github.ambry.utils.Crc32)6 BlobId (com.github.ambry.commons.BlobId)5 BlobProperties (com.github.ambry.messageformat.BlobProperties)5 CrcInputStream (com.github.ambry.utils.CrcInputStream)5 MessageFormatException (com.github.ambry.messageformat.MessageFormatException)4 RequestInfo (com.github.ambry.network.RequestInfo)4