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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations