Search in sources :

Example 1 with MessageCrypto

use of org.apache.pulsar.client.api.MessageCrypto 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 MessageCrypto

use of org.apache.pulsar.client.api.MessageCrypto 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 3 with MessageCrypto

use of org.apache.pulsar.client.api.MessageCrypto 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)3 CompressionType (org.apache.pulsar.client.api.CompressionType)3 MessageCrypto (org.apache.pulsar.client.api.MessageCrypto)3 MessageCryptoBc (org.apache.pulsar.client.impl.crypto.MessageCryptoBc)3 EncryptionContext (org.apache.pulsar.common.api.EncryptionContext)3 MessageMetadata (org.apache.pulsar.common.api.proto.MessageMetadata)3 SingleMessageMetadata (org.apache.pulsar.common.api.proto.SingleMessageMetadata)3 CompressionCodec (org.apache.pulsar.common.compression.CompressionCodec)3 ByteBuf (org.apache.pulsar.shade.io.netty.buffer.ByteBuf)3