@@ -1456,16 +1456,18 @@ void messageReceived(CommandMessage cmdMessage, ByteBuf headersAndPayload, Clien
1456
1456
return ;
1457
1457
}
1458
1458
1459
- ByteBuf decryptedPayload = decryptPayloadIfNeeded (messageId , redeliveryCount , msgMetadata , headersAndPayload ,
1459
+ DecryptResult decryptResult = decryptPayloadIfNeeded (messageId , redeliveryCount , msgMetadata , headersAndPayload ,
1460
1460
cnx );
1461
1461
1462
- boolean isMessageUndecryptable = isMessageUndecryptable (msgMetadata );
1463
-
1464
- if (decryptedPayload == null ) {
1462
+ if (decryptResult .shouldDiscard ()) {
1465
1463
// Message was discarded or CryptoKeyReader isn't implemented
1466
1464
return ;
1467
1465
}
1468
1466
1467
+ boolean isMessageUndecryptable = !decryptResult .success ;
1468
+
1469
+ ByteBuf decryptedPayload = decryptResult .payload ;
1470
+
1469
1471
// uncompress decryptedPayload and release decryptedPayload-ByteBuf
1470
1472
ByteBuf uncompressedPayload = (isMessageUndecryptable || isChunkedMessage ) ? decryptedPayload .retain ()
1471
1473
: uncompressPayloadIfNeeded (messageId , msgMetadata , decryptedPayload , cnx , true );
@@ -1951,11 +1953,53 @@ public long getLastDisconnectedTimestamp() {
1951
1953
return connectionHandler .lastConnectionClosedTimestamp ;
1952
1954
}
1953
1955
1954
- private ByteBuf decryptPayloadIfNeeded (MessageIdData messageId , int redeliveryCount , MessageMetadata msgMetadata ,
1955
- ByteBuf payload , ClientCnx currentCnx ) {
1956
+ /**
1957
+ * Represents the outcome of a message decryption attempt for the consumer.
1958
+ */
1959
+ private static class DecryptResult {
1960
+ private final boolean success ;
1961
+ private final ByteBuf payload ;
1962
+
1963
+ private DecryptResult (boolean success , ByteBuf decryptedPayload ) {
1964
+ this .success = success ;
1965
+ this .payload = decryptedPayload ;
1966
+ }
1967
+
1968
+ /**
1969
+ * Returns true if the message should be discarded and not delivered to the consumer user.
1970
+ */
1971
+ public boolean shouldDiscard () {
1972
+ return this .payload == null ;
1973
+ }
1974
+
1975
+ /**
1976
+ * Creates a result indicating decryption succeeded and the payload is ready for use.
1977
+ */
1978
+ public static DecryptResult success (ByteBuf decryptedPayload ) {
1979
+ return new DecryptResult (true , decryptedPayload );
1980
+ }
1981
+
1982
+ /**
1983
+ * Creates a result indicating decryption failed, but the message should still be delivered.
1984
+ */
1985
+ public static DecryptResult failure (ByteBuf decryptedPayload ) {
1986
+ return new DecryptResult (false , decryptedPayload );
1987
+ }
1988
+
1989
+ /**
1990
+ * Creates a result indicating the message should be discarded.
1991
+ */
1992
+ public static DecryptResult discard () {
1993
+ return new DecryptResult (false , null );
1994
+ }
1995
+ }
1996
+
1997
+ private DecryptResult decryptPayloadIfNeeded (MessageIdData messageId , int redeliveryCount ,
1998
+ MessageMetadata msgMetadata ,
1999
+ ByteBuf payload , ClientCnx currentCnx ) {
1956
2000
1957
2001
if (msgMetadata .getEncryptionKeysCount () == 0 ) {
1958
- return payload .retain ();
2002
+ return DecryptResult . success ( payload .retain () );
1959
2003
}
1960
2004
int batchSize = msgMetadata .getNumMessagesInBatch ();
1961
2005
// If KeyReader is not configured throw exception based on config param
@@ -1969,15 +2013,15 @@ private ByteBuf decryptPayloadIfNeeded(MessageIdData messageId, int redeliveryCo
1969
2013
ByteBuffer nioDecryptedData = decryptedData .nioBuffer (0 , maxDecryptedSize );
1970
2014
if (msgCrypto .decrypt (() -> msgMetadata , payload .nioBuffer (), nioDecryptedData , conf .getCryptoKeyReader ())) {
1971
2015
decryptedData .writerIndex (nioDecryptedData .limit ());
1972
- return decryptedData ;
2016
+ return DecryptResult . success ( decryptedData ) ;
1973
2017
}
1974
2018
1975
2019
decryptedData .release ();
1976
2020
1977
2021
return handleCryptoFailure (payload , messageId , currentCnx , redeliveryCount , batchSize , false );
1978
2022
}
1979
2023
1980
- private ByteBuf handleCryptoFailure (ByteBuf payload , MessageIdData messageId , ClientCnx currentCnx ,
2024
+ private DecryptResult handleCryptoFailure (ByteBuf payload , MessageIdData messageId , ClientCnx currentCnx ,
1981
2025
int redeliveryCount , int batchSize , boolean cryptoReaderNotExist ) {
1982
2026
1983
2027
switch (conf .getCryptoFailureAction ()) {
@@ -1990,7 +2034,7 @@ private ByteBuf handleCryptoFailure(ByteBuf payload, MessageIdData messageId, Cl
1990
2034
log .warn ("[{}][{}][{}][{}] Decryption failed. Consuming encrypted message since config is set to"
1991
2035
+ " consume." , topic , subscription , consumerName , messageId );
1992
2036
}
1993
- return payload .retain ();
2037
+ return DecryptResult . failure ( payload .retain () );
1994
2038
case DISCARD :
1995
2039
if (cryptoReaderNotExist ) {
1996
2040
log .warn (
@@ -2005,7 +2049,7 @@ private ByteBuf handleCryptoFailure(ByteBuf payload, MessageIdData messageId, Cl
2005
2049
messageId .getBatchIndex ());
2006
2050
}
2007
2051
discardMessage (messageId , currentCnx , ValidationError .DecryptionError , batchSize );
2008
- return null ;
2052
+ return DecryptResult . discard () ;
2009
2053
case FAIL :
2010
2054
if (cryptoReaderNotExist ) {
2011
2055
log .error (
@@ -2020,11 +2064,11 @@ private ByteBuf handleCryptoFailure(ByteBuf payload, MessageIdData messageId, Cl
2020
2064
}
2021
2065
MessageId m = new MessageIdImpl (messageId .getLedgerId (), messageId .getEntryId (), partitionIndex );
2022
2066
unAckedMessageTracker .add (m , redeliveryCount );
2023
- return null ;
2067
+ return DecryptResult . discard () ;
2024
2068
default :
2025
2069
log .warn ("[{}][{}][{}] Invalid crypto failure state found, continue message consumption." , topic ,
2026
2070
subscription , consumerName );
2027
- return payload .retain ();
2071
+ return DecryptResult . failure ( payload .retain () );
2028
2072
}
2029
2073
}
2030
2074
0 commit comments