Search in sources :

Example 1 with ByteBufPair

use of org.apache.pulsar.common.protocol.ByteBufPair in project pulsar by apache.

the class ServerCnx method newMessageAndIntercept.

public ByteBufPair newMessageAndIntercept(long consumerId, long ledgerId, long entryId, int partition, int redeliveryCount, ByteBuf metadataAndPayload, long[] ackSet, String topic, long epoch) {
    BaseCommand command = Commands.newMessageCommand(consumerId, ledgerId, entryId, partition, redeliveryCount, ackSet, epoch);
    ByteBufPair res = Commands.serializeCommandMessageWithSize(command, metadataAndPayload);
    try {
        val brokerInterceptor = getBrokerService().getInterceptor();
        if (brokerInterceptor != null) {
            brokerInterceptor.onPulsarCommand(command, this);
            CompletableFuture<Consumer> consumerFuture = consumers.get(consumerId);
            if (consumerFuture != null && consumerFuture.isDone() && !consumerFuture.isCompletedExceptionally()) {
                Consumer consumer = consumerFuture.getNow(null);
                brokerInterceptor.messageDispatched(this, consumer, ledgerId, entryId, metadataAndPayload);
            }
        } else {
            log.debug("BrokerInterceptor is not set in newMessageAndIntercept");
        }
    } catch (Exception e) {
        log.error("Exception occur when intercept messages.", e);
    }
    return res;
}
Also used : lombok.val(lombok.val) CommandCloseConsumer(org.apache.pulsar.common.api.proto.CommandCloseConsumer) BaseCommand(org.apache.pulsar.common.api.proto.BaseCommand) ByteBufPair(org.apache.pulsar.common.protocol.ByteBufPair) ServiceUnitNotReadyException(org.apache.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException) RestException(org.apache.pulsar.broker.web.RestException) InterceptException(org.apache.pulsar.common.intercept.InterceptException) TopicNotFoundException(org.apache.pulsar.broker.service.BrokerServiceException.TopicNotFoundException) ServerMetadataException(org.apache.pulsar.broker.service.BrokerServiceException.ServerMetadataException) AuthenticationException(javax.naming.AuthenticationException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) SubscriptionNotFoundException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionNotFoundException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) IncompatibleSchemaException(org.apache.pulsar.broker.service.schema.exceptions.IncompatibleSchemaException) CoordinatorException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException) NoSuchElementException(java.util.NoSuchElementException) ConsumerBusyException(org.apache.pulsar.broker.service.BrokerServiceException.ConsumerBusyException)

Example 2 with ByteBufPair

use of org.apache.pulsar.common.protocol.ByteBufPair in project pulsar by apache.

the class MessageChecksumTest method testTamperingMessageIsDetected.

@Test
public void testTamperingMessageIsDetected() throws Exception {
    // GIVEN
    ProducerImpl<byte[]> producer = (ProducerImpl<byte[]>) pulsarClient.newProducer().topic("persistent://prop/use/ns-abc/testTamperingMessageIsDetected").enableBatching(false).messageRoutingMode(MessageRoutingMode.SinglePartition).create();
    TypedMessageBuilderImpl<byte[]> msgBuilder = (TypedMessageBuilderImpl<byte[]>) producer.newMessage().value("a message".getBytes());
    MessageMetadata msgMetadata = msgBuilder.getMetadataBuilder().setProducerName("test").setSequenceId(1).setPublishTime(10L);
    ByteBuf payload = Unpooled.wrappedBuffer(msgBuilder.getContent());
    // WHEN
    // protocol message is created with checksum
    ByteBufPair cmd = Commands.newSend(1, 1, 1, ChecksumType.Crc32c, msgMetadata, payload);
    OpSendMsg op = OpSendMsg.create((MessageImpl<byte[]>) msgBuilder.getMessage(), cmd, 1, null);
    // THEN
    // the checksum validation passes
    assertTrue(producer.verifyLocalBufferIsNotCorrupted(op));
    // WHEN
    // the content of the message is tampered
    msgBuilder.getContent().put(0, (byte) 'b');
    // the checksum validation fails
    assertFalse(producer.verifyLocalBufferIsNotCorrupted(op));
}
Also used : MessageMetadata(org.apache.pulsar.common.api.proto.MessageMetadata) OpSendMsg(org.apache.pulsar.client.impl.ProducerImpl.OpSendMsg) ByteBuf(io.netty.buffer.ByteBuf) ByteBufPair(org.apache.pulsar.common.protocol.ByteBufPair) Test(org.testng.annotations.Test)

Example 3 with ByteBufPair

use of org.apache.pulsar.common.protocol.ByteBufPair in project pulsar by apache.

the class BatchMessageContainerImpl method createOpSendMsg.

@Override
public OpSendMsg createOpSendMsg() throws IOException {
    ByteBuf encryptedPayload = producer.encryptMessage(messageMetadata, getCompressedBatchMetadataAndPayload());
    if (encryptedPayload.readableBytes() > ClientCnx.getMaxMessageSize()) {
        discard(new PulsarClientException.InvalidMessageException("Message size is bigger than " + ClientCnx.getMaxMessageSize() + " bytes"));
        return null;
    }
    messageMetadata.setNumMessagesInBatch(numMessagesInBatch);
    messageMetadata.setHighestSequenceId(highestSequenceId);
    if (currentTxnidMostBits != -1) {
        messageMetadata.setTxnidMostBits(currentTxnidMostBits);
    }
    if (currentTxnidLeastBits != -1) {
        messageMetadata.setTxnidLeastBits(currentTxnidLeastBits);
    }
    ByteBufPair cmd = producer.sendMessage(producer.producerId, messageMetadata.getSequenceId(), messageMetadata.getHighestSequenceId(), numMessagesInBatch, messageMetadata, encryptedPayload);
    OpSendMsg op = OpSendMsg.create(messages, cmd, messageMetadata.getSequenceId(), messageMetadata.getHighestSequenceId(), firstCallback);
    op.setNumMessagesInBatch(numMessagesInBatch);
    op.setBatchSizeByte(currentBatchSizeBytes);
    lowestSequenceId = -1L;
    return op;
}
Also used : OpSendMsg(org.apache.pulsar.client.impl.ProducerImpl.OpSendMsg) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ByteBuf(io.netty.buffer.ByteBuf) ByteBufPair(org.apache.pulsar.common.protocol.ByteBufPair)

Example 4 with ByteBufPair

use of org.apache.pulsar.common.protocol.ByteBufPair in project pulsar by apache.

the class BatchMessageKeyBasedContainer method createOpSendMsg.

private ProducerImpl.OpSendMsg createOpSendMsg(KeyedBatch keyedBatch) throws IOException {
    ByteBuf encryptedPayload = producer.encryptMessage(keyedBatch.messageMetadata, keyedBatch.getCompressedBatchMetadataAndPayload());
    if (encryptedPayload.readableBytes() > ClientCnx.getMaxMessageSize()) {
        keyedBatch.discard(new PulsarClientException.InvalidMessageException("Message size is bigger than " + ClientCnx.getMaxMessageSize() + " bytes"));
        return null;
    }
    final int numMessagesInBatch = keyedBatch.messages.size();
    long currentBatchSizeBytes = 0;
    for (MessageImpl<?> message : keyedBatch.messages) {
        currentBatchSizeBytes += message.getDataBuffer().readableBytes();
    }
    keyedBatch.messageMetadata.setNumMessagesInBatch(numMessagesInBatch);
    if (currentTxnidMostBits != -1) {
        keyedBatch.messageMetadata.setTxnidMostBits(currentTxnidMostBits);
    }
    if (currentTxnidLeastBits != -1) {
        keyedBatch.messageMetadata.setTxnidLeastBits(currentTxnidLeastBits);
    }
    ByteBufPair cmd = producer.sendMessage(producer.producerId, keyedBatch.sequenceId, numMessagesInBatch, keyedBatch.messageMetadata, encryptedPayload);
    ProducerImpl.OpSendMsg op = ProducerImpl.OpSendMsg.create(keyedBatch.messages, cmd, keyedBatch.sequenceId, keyedBatch.firstCallback);
    op.setNumMessagesInBatch(numMessagesInBatch);
    op.setBatchSizeByte(currentBatchSizeBytes);
    return op;
}
Also used : PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ByteBuf(io.netty.buffer.ByteBuf) ByteBufPair(org.apache.pulsar.common.protocol.ByteBufPair)

Example 5 with ByteBufPair

use of org.apache.pulsar.common.protocol.ByteBufPair in project pulsar by apache.

the class ProducerImpl method verifyLocalBufferIsNotCorrupted.

/**
 * Computes checksum again and verifies it against existing checksum. If checksum doesn't match it means that
 * message is corrupt.
 *
 * @param op
 * @return returns true only if message is not modified and computed-checksum is same as previous checksum else
 *         return false that means that message is corrupted. Returns true if checksum is not present.
 */
protected boolean verifyLocalBufferIsNotCorrupted(OpSendMsg op) {
    ByteBufPair msg = op.cmd;
    if (msg != null) {
        ByteBuf headerFrame = msg.getFirst();
        headerFrame.markReaderIndex();
        try {
            // skip bytes up to checksum index
            // skip [total-size]
            headerFrame.skipBytes(4);
            int cmdSize = (int) headerFrame.readUnsignedInt();
            headerFrame.skipBytes(cmdSize);
            // verify if checksum present
            if (hasChecksum(headerFrame)) {
                int checksum = readChecksum(headerFrame);
                // msg.readerIndex is already at header-payload index, Recompute checksum for headers-payload
                int metadataChecksum = computeChecksum(headerFrame);
                long computedChecksum = resumeChecksum(metadataChecksum, msg.getSecond());
                return checksum == computedChecksum;
            } else {
                log.warn("[{}] [{}] checksum is not present into message with id {}", topic, producerName, op.sequenceId);
            }
        } finally {
            headerFrame.resetReaderIndex();
        }
        return true;
    } else {
        log.warn("[{}] Failed while casting empty ByteBufPair, ", producerName);
        return false;
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) ByteBufPair(org.apache.pulsar.common.protocol.ByteBufPair)

Aggregations

ByteBufPair (org.apache.pulsar.common.protocol.ByteBufPair)10 ByteBuf (io.netty.buffer.ByteBuf)9 MessageMetadata (org.apache.pulsar.common.api.proto.MessageMetadata)4 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)3 OpSendMsg (org.apache.pulsar.client.impl.ProducerImpl.OpSendMsg)3 Test (org.testng.annotations.Test)3 Field (java.lang.reflect.Field)1 NoSuchElementException (java.util.NoSuchElementException)1 AuthenticationException (javax.naming.AuthenticationException)1 lombok.val (lombok.val)1 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)1 ConsumerBusyException (org.apache.pulsar.broker.service.BrokerServiceException.ConsumerBusyException)1 ServerMetadataException (org.apache.pulsar.broker.service.BrokerServiceException.ServerMetadataException)1 ServiceUnitNotReadyException (org.apache.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException)1 SubscriptionNotFoundException (org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionNotFoundException)1 TopicNotFoundException (org.apache.pulsar.broker.service.BrokerServiceException.TopicNotFoundException)1 IncompatibleSchemaException (org.apache.pulsar.broker.service.schema.exceptions.IncompatibleSchemaException)1 RestException (org.apache.pulsar.broker.web.RestException)1 BaseCommand (org.apache.pulsar.common.api.proto.BaseCommand)1 CommandCloseConsumer (org.apache.pulsar.common.api.proto.CommandCloseConsumer)1