4949
5050from _pulsar import Result , CompressionType , ConsumerType , InitialPosition , PartitionsRoutingMode , BatchingType , \
5151 LoggerLevel , BatchReceivePolicy , KeySharedPolicy , KeySharedMode , ProducerAccessMode , RegexSubscriptionMode , \
52- DeadLetterPolicyBuilder # noqa: F401
52+ DeadLetterPolicyBuilder , ConsumerCryptoFailureAction # noqa: F401
5353
5454from pulsar .__about__ import __version__
5555
@@ -846,6 +846,7 @@ def subscribe(self, topic, subscription_name,
846846 batch_index_ack_enabled = False ,
847847 regex_subscription_mode : RegexSubscriptionMode = RegexSubscriptionMode .PersistentOnly ,
848848 dead_letter_policy : Union [None , ConsumerDeadLetterPolicy ] = None ,
849+ crypto_failure_action : ConsumerCryptoFailureAction = ConsumerCryptoFailureAction .FAIL ,
849850 ):
850851 """
851852 Subscribe to the given topic and subscription combination.
@@ -949,6 +950,19 @@ def my_listener(consumer, message):
949950 stopped. By using the dead letter mechanism, messages have the max redelivery count, when they're
950951 exceeding the maximum number of redeliveries. Messages are sent to dead letter topics and acknowledged
951952 automatically.
953+ crypto_failure_action: ConsumerCryptoFailureAction, default=ConsumerCryptoFailureAction.FAIL
954+ Set the behavior when the decryption fails. The default is to fail the message.
955+
956+ Supported actions:
957+
958+ * ConsumerCryptoFailureAction.FAIL: Fail consume until crypto succeeds
959+ * ConsumerCryptoFailureAction.DISCARD:
960+ Message is silently acknowledged and not delivered to the application.
961+ * ConsumerCryptoFailureAction.CONSUME:
962+ Deliver the encrypted message to the application. It's the application's responsibility
963+ to decrypt the message. If message is also compressed, decompression will fail. If the
964+ message contains batch messages, client will not be able to retrieve individual messages
965+ in the batch.
952966 """
953967 _check_type (str , subscription_name , 'subscription_name' )
954968 _check_type (ConsumerType , consumer_type , 'consumer_type' )
@@ -972,6 +986,7 @@ def my_listener(consumer, message):
972986 _check_type_or_none (ConsumerKeySharedPolicy , key_shared_policy , 'key_shared_policy' )
973987 _check_type (bool , batch_index_ack_enabled , 'batch_index_ack_enabled' )
974988 _check_type (RegexSubscriptionMode , regex_subscription_mode , 'regex_subscription_mode' )
989+ _check_type (ConsumerCryptoFailureAction , crypto_failure_action , 'crypto_failure_action' )
975990
976991 conf = _pulsar .ConsumerConfiguration ()
977992 conf .consumer_type (consumer_type )
@@ -1010,6 +1025,7 @@ def my_listener(consumer, message):
10101025 conf .batch_index_ack_enabled (batch_index_ack_enabled )
10111026 if dead_letter_policy :
10121027 conf .dead_letter_policy (dead_letter_policy .policy ())
1028+ conf .crypto_failure_action (crypto_failure_action )
10131029
10141030 c = Consumer ()
10151031 if isinstance (topic , str ):
@@ -1038,7 +1054,8 @@ def create_reader(self, topic, start_message_id,
10381054 subscription_role_prefix = None ,
10391055 is_read_compacted = False ,
10401056 crypto_key_reader : Union [None , CryptoKeyReader ] = None ,
1041- start_message_id_inclusive = False
1057+ start_message_id_inclusive = False ,
1058+ crypto_failure_action : ConsumerCryptoFailureAction = ConsumerCryptoFailureAction .FAIL ,
10421059 ):
10431060 """
10441061 Create a reader on a particular topic
@@ -1099,6 +1116,19 @@ def my_listener(reader, message):
10991116 and private key decryption messages for the consumer
11001117 start_message_id_inclusive: bool, default=False
11011118 Set the reader to include the startMessageId or given position of any reset operation like Reader.seek
1119+ crypto_failure_action: ConsumerCryptoFailureAction, default=ConsumerCryptoFailureAction.FAIL
1120+ Set the behavior when the decryption fails. The default is to fail the message.
1121+
1122+ Supported actions:
1123+
1124+ * ConsumerCryptoFailureAction.FAIL: Fail consume until crypto succeeds
1125+ * ConsumerCryptoFailureAction.DISCARD:
1126+ Message is silently acknowledged and not delivered to the application.
1127+ * ConsumerCryptoFailureAction.CONSUME:
1128+ Deliver the encrypted message to the application. It's the application's responsibility
1129+ to decrypt the message. If message is also compressed, decompression will fail. If the
1130+ message contains batch messages, client will not be able to retrieve individual messages
1131+ in the batch.
11021132 """
11031133
11041134 # If a pulsar.MessageId object is passed, access the _pulsar.MessageId object
@@ -1114,6 +1144,7 @@ def my_listener(reader, message):
11141144 _check_type (bool , is_read_compacted , 'is_read_compacted' )
11151145 _check_type_or_none (CryptoKeyReader , crypto_key_reader , 'crypto_key_reader' )
11161146 _check_type (bool , start_message_id_inclusive , 'start_message_id_inclusive' )
1147+ _check_type (ConsumerCryptoFailureAction , crypto_failure_action , 'crypto_failure_action' )
11171148
11181149 conf = _pulsar .ReaderConfiguration ()
11191150 if reader_listener :
@@ -1128,6 +1159,7 @@ def my_listener(reader, message):
11281159 if crypto_key_reader :
11291160 conf .crypto_key_reader (crypto_key_reader .cryptoKeyReader )
11301161 conf .start_message_id_inclusive (start_message_id_inclusive )
1162+ conf .crypto_failure_action (crypto_failure_action )
11311163
11321164 c = Reader ()
11331165 c ._reader = self ._client .create_reader (topic , start_message_id , conf )
0 commit comments