Search in sources :

Example 1 with MessageCryptoBc

use of org.apache.pulsar.client.impl.crypto.MessageCryptoBc in project pulsar by apache.

the class SimpleProducerConsumerTest method decryptMessage.

private String decryptMessage(TopicMessageImpl<byte[]> msg, String encryptionKeyName, CryptoKeyReader reader) throws Exception {
    Optional<EncryptionContext> ctx = msg.getEncryptionCtx();
    assertTrue(ctx.isPresent());
    EncryptionContext encryptionCtx = ctx.orElseThrow(() -> new IllegalStateException("encryption-ctx not present for encrypted message"));
    Map<String, EncryptionContext.EncryptionKey> keys = encryptionCtx.getKeys();
    assertEquals(keys.size(), 1);
    EncryptionContext.EncryptionKey encryptionKey = keys.get(encryptionKeyName);
    byte[] dataKey = encryptionKey.getKeyValue();
    Map<String, String> metadata = encryptionKey.getMetadata();
    String version = metadata.get("version");
    assertEquals(version, "1.0");
    CompressionType compressionType = encryptionCtx.getCompressionType();
    int uncompressedSize = encryptionCtx.getUncompressedMessageSize();
    byte[] encrParam = encryptionCtx.getParam();
    String encAlgo = encryptionCtx.getAlgorithm();
    int batchSize = encryptionCtx.getBatchSize().orElse(0);
    ByteBuffer payloadBuf = ByteBuffer.wrap(msg.getData());
    // try to decrypt use default MessageCryptoBc
    MessageCrypto crypto = new MessageCryptoBc("test", false);
    MessageMetadata msgMetadata = new MessageMetadata().setEncryptionParam(encrParam).setProducerName("test").setSequenceId(123).setPublishTime(12333453454L).setCompression(CompressionCodecProvider.convertToWireProtocol(compressionType)).setUncompressedSize(uncompressedSize);
    if (encAlgo != null) {
        msgMetadata.setEncryptionAlgo(encAlgo);
    }
    msgMetadata.addEncryptionKey().setKey(encryptionKeyName).setValue(dataKey);
    ByteBuffer decryptedPayload = ByteBuffer.allocate(crypto.getMaxOutputSize(payloadBuf.remaining()));
    crypto.decrypt(() -> msgMetadata, payloadBuf, decryptedPayload, reader);
    // try to uncompress
    CompressionCodec codec = CompressionCodecProvider.getCompressionCodec(compressionType);
    ByteBuf uncompressedPayload = codec.decode(Unpooled.wrappedBuffer(decryptedPayload), uncompressedSize);
    if (batchSize > 0) {
        SingleMessageMetadata singleMessageMetadata = new SingleMessageMetadata();
        uncompressedPayload = Commands.deSerializeSingleMessageInBatch(uncompressedPayload, singleMessageMetadata, 0, batchSize);
    }
    byte[] data = new byte[uncompressedPayload.readableBytes()];
    uncompressedPayload.readBytes(data);
    uncompressedPayload.release();
    return new String(data);
}
Also used : MessageCrypto(org.apache.pulsar.client.api.MessageCrypto) MessageCryptoBc(org.apache.pulsar.client.impl.crypto.MessageCryptoBc) ByteBuf(org.apache.pulsar.shade.io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) MessageMetadata(org.apache.pulsar.common.api.proto.MessageMetadata) SingleMessageMetadata(org.apache.pulsar.common.api.proto.SingleMessageMetadata) EncryptionContext(org.apache.pulsar.common.api.EncryptionContext) SingleMessageMetadata(org.apache.pulsar.common.api.proto.SingleMessageMetadata) CompressionCodec(org.apache.pulsar.common.compression.CompressionCodec) CompressionType(org.apache.pulsar.client.api.CompressionType)

Example 2 with MessageCryptoBc

use of org.apache.pulsar.client.impl.crypto.MessageCryptoBc in project incubator-pulsar by apache.

the class SimpleProducerConsumerTest method decryptMessage.

private String decryptMessage(TopicMessageImpl<byte[]> msg, String encryptionKeyName, CryptoKeyReader reader) throws Exception {
    Optional<EncryptionContext> ctx = msg.getEncryptionCtx();
    Assert.assertTrue(ctx.isPresent());
    EncryptionContext encryptionCtx = ctx.orElseThrow(() -> new IllegalStateException("encryption-ctx not present for encrypted message"));
    Map<String, EncryptionKey> keys = encryptionCtx.getKeys();
    assertEquals(keys.size(), 1);
    EncryptionKey encryptionKey = keys.get(encryptionKeyName);
    byte[] dataKey = encryptionKey.getKeyValue();
    Map<String, String> metadata = encryptionKey.getMetadata();
    String version = metadata.get("version");
    assertEquals(version, "1.0");
    CompressionType compressionType = encryptionCtx.getCompressionType();
    int uncompressedSize = encryptionCtx.getUncompressedMessageSize();
    byte[] encrParam = encryptionCtx.getParam();
    String encAlgo = encryptionCtx.getAlgorithm();
    int batchSize = encryptionCtx.getBatchSize().orElse(0);
    ByteBuffer payloadBuf = ByteBuffer.wrap(msg.getData());
    // try to decrypt use default MessageCryptoBc
    MessageCrypto<MessageMetadata, MessageMetadata> crypto = new MessageCryptoBc("test", false);
    MessageMetadata messageMetadata = new MessageMetadata().setEncryptionParam(encrParam).setProducerName("test").setSequenceId(123).setPublishTime(12333453454L).setCompression(CompressionCodecProvider.convertToWireProtocol(compressionType)).setUncompressedSize(uncompressedSize);
    messageMetadata.addEncryptionKey().setKey(encryptionKeyName).setValue(dataKey);
    if (encAlgo != null) {
        messageMetadata.setEncryptionAlgo(encAlgo);
    }
    ByteBuffer decryptedPayload = ByteBuffer.allocate(crypto.getMaxOutputSize(payloadBuf.remaining()));
    crypto.decrypt(() -> messageMetadata, payloadBuf, decryptedPayload, reader);
    // try to uncompress
    CompressionCodec codec = CompressionCodecProvider.getCompressionCodec(compressionType);
    ByteBuf uncompressedPayload = codec.decode(Unpooled.wrappedBuffer(decryptedPayload), uncompressedSize);
    if (batchSize > 0) {
        SingleMessageMetadata singleMessageMetadata = new SingleMessageMetadata();
        uncompressedPayload = Commands.deSerializeSingleMessageInBatch(uncompressedPayload, singleMessageMetadata, 0, batchSize);
    }
    byte[] data = new byte[uncompressedPayload.readableBytes()];
    uncompressedPayload.readBytes(data);
    uncompressedPayload.release();
    return new String(data);
}
Also used : MessageCryptoBc(org.apache.pulsar.client.impl.crypto.MessageCryptoBc) EncryptionKey(org.apache.pulsar.common.api.EncryptionContext.EncryptionKey) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) MessageMetadata(org.apache.pulsar.common.api.proto.MessageMetadata) SingleMessageMetadata(org.apache.pulsar.common.api.proto.SingleMessageMetadata) EncryptionContext(org.apache.pulsar.common.api.EncryptionContext) SingleMessageMetadata(org.apache.pulsar.common.api.proto.SingleMessageMetadata) CompressionCodec(org.apache.pulsar.common.compression.CompressionCodec)

Example 3 with MessageCryptoBc

use of org.apache.pulsar.client.impl.crypto.MessageCryptoBc in project incubator-pulsar by apache.

the class SimpleProducerConsumerTest method decryptMessage.

private String decryptMessage(TopicMessageImpl<byte[]> msg, String encryptionKeyName, CryptoKeyReader reader) throws Exception {
    Optional<EncryptionContext> ctx = msg.getEncryptionCtx();
    assertTrue(ctx.isPresent());
    EncryptionContext encryptionCtx = ctx.orElseThrow(() -> new IllegalStateException("encryption-ctx not present for encrypted message"));
    Map<String, EncryptionContext.EncryptionKey> keys = encryptionCtx.getKeys();
    assertEquals(keys.size(), 1);
    EncryptionContext.EncryptionKey encryptionKey = keys.get(encryptionKeyName);
    byte[] dataKey = encryptionKey.getKeyValue();
    Map<String, String> metadata = encryptionKey.getMetadata();
    String version = metadata.get("version");
    assertEquals(version, "1.0");
    CompressionType compressionType = encryptionCtx.getCompressionType();
    int uncompressedSize = encryptionCtx.getUncompressedMessageSize();
    byte[] encrParam = encryptionCtx.getParam();
    String encAlgo = encryptionCtx.getAlgorithm();
    int batchSize = encryptionCtx.getBatchSize().orElse(0);
    ByteBuffer payloadBuf = ByteBuffer.wrap(msg.getData());
    // try to decrypt use default MessageCryptoBc
    MessageCrypto crypto = new MessageCryptoBc("test", false);
    MessageMetadata msgMetadata = new MessageMetadata().setEncryptionParam(encrParam).setProducerName("test").setSequenceId(123).setPublishTime(12333453454L).setCompression(CompressionCodecProvider.convertToWireProtocol(compressionType)).setUncompressedSize(uncompressedSize);
    if (encAlgo != null) {
        msgMetadata.setEncryptionAlgo(encAlgo);
    }
    msgMetadata.addEncryptionKey().setKey(encryptionKeyName).setValue(dataKey);
    ByteBuffer decryptedPayload = ByteBuffer.allocate(crypto.getMaxOutputSize(payloadBuf.remaining()));
    crypto.decrypt(() -> msgMetadata, payloadBuf, decryptedPayload, reader);
    // try to uncompress
    CompressionCodec codec = CompressionCodecProvider.getCompressionCodec(compressionType);
    ByteBuf uncompressedPayload = codec.decode(Unpooled.wrappedBuffer(decryptedPayload), uncompressedSize);
    if (batchSize > 0) {
        SingleMessageMetadata singleMessageMetadata = new SingleMessageMetadata();
        uncompressedPayload = Commands.deSerializeSingleMessageInBatch(uncompressedPayload, singleMessageMetadata, 0, batchSize);
    }
    byte[] data = new byte[uncompressedPayload.readableBytes()];
    uncompressedPayload.readBytes(data);
    uncompressedPayload.release();
    return new String(data);
}
Also used : MessageCrypto(org.apache.pulsar.client.api.MessageCrypto) MessageCryptoBc(org.apache.pulsar.client.impl.crypto.MessageCryptoBc) ByteBuf(org.apache.pulsar.shade.io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) MessageMetadata(org.apache.pulsar.common.api.proto.MessageMetadata) SingleMessageMetadata(org.apache.pulsar.common.api.proto.SingleMessageMetadata) EncryptionContext(org.apache.pulsar.common.api.EncryptionContext) SingleMessageMetadata(org.apache.pulsar.common.api.proto.SingleMessageMetadata) CompressionCodec(org.apache.pulsar.common.compression.CompressionCodec) CompressionType(org.apache.pulsar.client.api.CompressionType)

Example 4 with MessageCryptoBc

use of org.apache.pulsar.client.impl.crypto.MessageCryptoBc in project pulsar by yahoo.

the class SimpleProducerConsumerTest method decryptMessage.

private String decryptMessage(TopicMessageImpl<byte[]> msg, String encryptionKeyName, CryptoKeyReader reader) throws Exception {
    Optional<EncryptionContext> ctx = msg.getEncryptionCtx();
    Assert.assertTrue(ctx.isPresent());
    EncryptionContext encryptionCtx = ctx.orElseThrow(() -> new IllegalStateException("encryption-ctx not present for encrypted message"));
    Map<String, EncryptionKey> keys = encryptionCtx.getKeys();
    assertEquals(keys.size(), 1);
    EncryptionKey encryptionKey = keys.get(encryptionKeyName);
    byte[] dataKey = encryptionKey.getKeyValue();
    Map<String, String> metadata = encryptionKey.getMetadata();
    String version = metadata.get("version");
    assertEquals(version, "1.0");
    CompressionType compressionType = encryptionCtx.getCompressionType();
    int uncompressedSize = encryptionCtx.getUncompressedMessageSize();
    byte[] encrParam = encryptionCtx.getParam();
    String encAlgo = encryptionCtx.getAlgorithm();
    int batchSize = encryptionCtx.getBatchSize().orElse(0);
    ByteBuffer payloadBuf = ByteBuffer.wrap(msg.getData());
    // try to decrypt use default MessageCryptoBc
    MessageCrypto<MessageMetadata, MessageMetadata> crypto = new MessageCryptoBc("test", false);
    MessageMetadata messageMetadata = new MessageMetadata().setEncryptionParam(encrParam).setProducerName("test").setSequenceId(123).setPublishTime(12333453454L).setCompression(CompressionCodecProvider.convertToWireProtocol(compressionType)).setUncompressedSize(uncompressedSize);
    messageMetadata.addEncryptionKey().setKey(encryptionKeyName).setValue(dataKey);
    if (encAlgo != null) {
        messageMetadata.setEncryptionAlgo(encAlgo);
    }
    ByteBuffer decryptedPayload = ByteBuffer.allocate(crypto.getMaxOutputSize(payloadBuf.remaining()));
    crypto.decrypt(() -> messageMetadata, payloadBuf, decryptedPayload, reader);
    // try to uncompress
    CompressionCodec codec = CompressionCodecProvider.getCompressionCodec(compressionType);
    ByteBuf uncompressedPayload = codec.decode(Unpooled.wrappedBuffer(decryptedPayload), uncompressedSize);
    if (batchSize > 0) {
        SingleMessageMetadata singleMessageMetadata = new SingleMessageMetadata();
        uncompressedPayload = Commands.deSerializeSingleMessageInBatch(uncompressedPayload, singleMessageMetadata, 0, batchSize);
    }
    byte[] data = new byte[uncompressedPayload.readableBytes()];
    uncompressedPayload.readBytes(data);
    uncompressedPayload.release();
    return new String(data);
}
Also used : MessageCryptoBc(org.apache.pulsar.client.impl.crypto.MessageCryptoBc) EncryptionKey(org.apache.pulsar.common.api.EncryptionContext.EncryptionKey) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) MessageMetadata(org.apache.pulsar.common.api.proto.MessageMetadata) SingleMessageMetadata(org.apache.pulsar.common.api.proto.SingleMessageMetadata) EncryptionContext(org.apache.pulsar.common.api.EncryptionContext) SingleMessageMetadata(org.apache.pulsar.common.api.proto.SingleMessageMetadata) CompressionCodec(org.apache.pulsar.common.compression.CompressionCodec)

Example 5 with MessageCryptoBc

use of org.apache.pulsar.client.impl.crypto.MessageCryptoBc in project pulsar by yahoo.

the class SimpleProducerConsumerTest method decryptMessage.

private String decryptMessage(TopicMessageImpl<byte[]> msg, String encryptionKeyName, CryptoKeyReader reader) throws Exception {
    Optional<EncryptionContext> ctx = msg.getEncryptionCtx();
    assertTrue(ctx.isPresent());
    EncryptionContext encryptionCtx = ctx.orElseThrow(() -> new IllegalStateException("encryption-ctx not present for encrypted message"));
    Map<String, EncryptionContext.EncryptionKey> keys = encryptionCtx.getKeys();
    assertEquals(keys.size(), 1);
    EncryptionContext.EncryptionKey encryptionKey = keys.get(encryptionKeyName);
    byte[] dataKey = encryptionKey.getKeyValue();
    Map<String, String> metadata = encryptionKey.getMetadata();
    String version = metadata.get("version");
    assertEquals(version, "1.0");
    CompressionType compressionType = encryptionCtx.getCompressionType();
    int uncompressedSize = encryptionCtx.getUncompressedMessageSize();
    byte[] encrParam = encryptionCtx.getParam();
    String encAlgo = encryptionCtx.getAlgorithm();
    int batchSize = encryptionCtx.getBatchSize().orElse(0);
    ByteBuffer payloadBuf = ByteBuffer.wrap(msg.getData());
    // try to decrypt use default MessageCryptoBc
    MessageCrypto crypto = new MessageCryptoBc("test", false);
    MessageMetadata msgMetadata = new MessageMetadata().setEncryptionParam(encrParam).setProducerName("test").setSequenceId(123).setPublishTime(12333453454L).setCompression(CompressionCodecProvider.convertToWireProtocol(compressionType)).setUncompressedSize(uncompressedSize);
    if (encAlgo != null) {
        msgMetadata.setEncryptionAlgo(encAlgo);
    }
    msgMetadata.addEncryptionKey().setKey(encryptionKeyName).setValue(dataKey);
    ByteBuffer decryptedPayload = ByteBuffer.allocate(crypto.getMaxOutputSize(payloadBuf.remaining()));
    crypto.decrypt(() -> msgMetadata, payloadBuf, decryptedPayload, reader);
    // try to uncompress
    CompressionCodec codec = CompressionCodecProvider.getCompressionCodec(compressionType);
    ByteBuf uncompressedPayload = codec.decode(Unpooled.wrappedBuffer(decryptedPayload), uncompressedSize);
    if (batchSize > 0) {
        SingleMessageMetadata singleMessageMetadata = new SingleMessageMetadata();
        uncompressedPayload = Commands.deSerializeSingleMessageInBatch(uncompressedPayload, singleMessageMetadata, 0, batchSize);
    }
    byte[] data = new byte[uncompressedPayload.readableBytes()];
    uncompressedPayload.readBytes(data);
    uncompressedPayload.release();
    return new String(data);
}
Also used : MessageCrypto(org.apache.pulsar.client.api.MessageCrypto) MessageCryptoBc(org.apache.pulsar.client.impl.crypto.MessageCryptoBc) ByteBuf(org.apache.pulsar.shade.io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) MessageMetadata(org.apache.pulsar.common.api.proto.MessageMetadata) SingleMessageMetadata(org.apache.pulsar.common.api.proto.SingleMessageMetadata) EncryptionContext(org.apache.pulsar.common.api.EncryptionContext) SingleMessageMetadata(org.apache.pulsar.common.api.proto.SingleMessageMetadata) CompressionCodec(org.apache.pulsar.common.compression.CompressionCodec) CompressionType(org.apache.pulsar.client.api.CompressionType)

Aggregations

ByteBuffer (java.nio.ByteBuffer)6 MessageCryptoBc (org.apache.pulsar.client.impl.crypto.MessageCryptoBc)6 EncryptionContext (org.apache.pulsar.common.api.EncryptionContext)6 MessageMetadata (org.apache.pulsar.common.api.proto.MessageMetadata)6 SingleMessageMetadata (org.apache.pulsar.common.api.proto.SingleMessageMetadata)6 CompressionCodec (org.apache.pulsar.common.compression.CompressionCodec)6 ByteBuf (io.netty.buffer.ByteBuf)3 CompressionType (org.apache.pulsar.client.api.CompressionType)3 MessageCrypto (org.apache.pulsar.client.api.MessageCrypto)3 EncryptionKey (org.apache.pulsar.common.api.EncryptionContext.EncryptionKey)3 ByteBuf (org.apache.pulsar.shade.io.netty.buffer.ByteBuf)3