cckellogg commented on a change in pull request #612: URL: https://github.com/apache/pulsar-client-go/pull/612#discussion_r703778525
########## File path: pulsar/internal/crypto/decryptor.go ########## @@ -0,0 +1,28 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package crypto + +import ( + pb "github.com/apache/pulsar-client-go/pulsar/internal/pulsar_proto" +) + +// Decryptor support decrypting of message +type Decryptor interface { + Decrypt(payload []byte, msgID *pb.MessageIdData, msgMetadata *pb.MessageMetadata) ([]byte, error) + CryptoFailureAction() int Review comment: Why is this needed? I think the Decryptor should just have Decrypt and return an error. The caller of this can then figure out what to do with the error. ########## File path: pulsar/message.go ########## @@ -116,6 +116,10 @@ type Message interface { //Get the de-serialized value of the message, according the configured GetSchemaValue(v interface{}) error + + // GetEncryptionContext get the ecryption context of message + // It will be used by the application to parse undecrypted message + GetEncryptionContext() EncryptionContext Review comment: Let's return a pointer or interface ########## File path: pulsar/consumer_partition.go ########## @@ -479,7 +505,49 @@ func (pc *partitionConsumer) MessageReceived(response *pb.CommandMessage, header return err } - uncompressedHeadersAndPayload, err := pc.Decompress(msgMeta, headersAndPayload) + decryptedPayload, err := pc.decryptor.Decrypt(headersAndPayload.ReadableSlice(), pbMsgID, msgMeta) + messages := make([]*message, 0) + + // error decrypting the payload + if err != nil { + pc.log.Error(err) + switch pc.decryptor.CryptoFailureAction() { + case crypto.ConsumerCryptoFailureActionFail: + pc.log.Errorf("consuming message failed due to decryption err :%v", err) + return err + case crypto.ConsumerCryptoFailureActionDiscard: + pc.discardCorruptedMessage(pbMsgID, pb.CommandAck_DecryptionError) + return fmt.Errorf("discarding message on decryption error :%v", err) + case crypto.ConsumerCryptoFailureActionConsume: + pc.log.Warnf("consuming encrypted message due to error in decryption :%v", err) + messages = append(messages, &message{ + publishTime: timeFromUnixTimestampMillis(msgMeta.GetPublishTime()), Review comment: Nit. we can use create a new variable here and move `messages := make([]*message, 0)` back to where it was ``` messages := []*message{ { }, } ``` ########## File path: pulsar/consumer_partition.go ########## @@ -479,7 +505,49 @@ func (pc *partitionConsumer) MessageReceived(response *pb.CommandMessage, header return err } - uncompressedHeadersAndPayload, err := pc.Decompress(msgMeta, headersAndPayload) + decryptedPayload, err := pc.decryptor.Decrypt(headersAndPayload.ReadableSlice(), pbMsgID, msgMeta) + messages := make([]*message, 0) + + // error decrypting the payload + if err != nil { + pc.log.Error(err) + switch pc.decryptor.CryptoFailureAction() { + case crypto.ConsumerCryptoFailureActionFail: + pc.log.Errorf("consuming message failed due to decryption err :%v", err) + return err + case crypto.ConsumerCryptoFailureActionDiscard: + pc.discardCorruptedMessage(pbMsgID, pb.CommandAck_DecryptionError) + return fmt.Errorf("discarding message on decryption error :%v", err) + case crypto.ConsumerCryptoFailureActionConsume: + pc.log.Warnf("consuming encrypted message due to error in decryption :%v", err) + messages = append(messages, &message{ + publishTime: timeFromUnixTimestampMillis(msgMeta.GetPublishTime()), + eventTime: timeFromUnixTimestampMillis(msgMeta.GetEventTime()), + key: msgMeta.GetPartitionKey(), + producerName: msgMeta.GetProducerName(), + properties: internal.ConvertToStringMap(msgMeta.GetProperties()), + topic: pc.topic, + msgID: newMessageID( + int64(pbMsgID.GetLedgerId()), + int64(pbMsgID.GetEntryId()), + pbMsgID.GetBatchIndex(), + pc.partitionIdx, + ), + payLoad: headersAndPayload.ReadableSlice(), + schema: pc.options.schema, + replicationClusters: msgMeta.GetReplicateTo(), + replicatedFrom: msgMeta.GetReplicatedFrom(), + redeliveryCount: response.GetRedeliveryCount(), + encryptionContext: createEncryptionContext(msgMeta), Review comment: Does the encryptionContext need to be added to messages that don't fail? ########## File path: pulsar/consumer_partition.go ########## @@ -479,7 +505,49 @@ func (pc *partitionConsumer) MessageReceived(response *pb.CommandMessage, header return err } - uncompressedHeadersAndPayload, err := pc.Decompress(msgMeta, headersAndPayload) + decryptedPayload, err := pc.decryptor.Decrypt(headersAndPayload.ReadableSlice(), pbMsgID, msgMeta) + messages := make([]*message, 0) + + // error decrypting the payload + if err != nil { + pc.log.Error(err) Review comment: remove this and add a log under ConsumerCryptoFailureActionDiscard so there is more context. ########## File path: pulsar/consumer_partition.go ########## @@ -479,7 +505,49 @@ func (pc *partitionConsumer) MessageReceived(response *pb.CommandMessage, header return err } - uncompressedHeadersAndPayload, err := pc.Decompress(msgMeta, headersAndPayload) + decryptedPayload, err := pc.decryptor.Decrypt(headersAndPayload.ReadableSlice(), pbMsgID, msgMeta) + messages := make([]*message, 0) + + // error decrypting the payload + if err != nil { + pc.log.Error(err) + switch pc.decryptor.CryptoFailureAction() { + case crypto.ConsumerCryptoFailureActionFail: + pc.log.Errorf("consuming message failed due to decryption err :%v", err) Review comment: The java clients add this to the unacked message tracker do we need to do the same? ########## File path: pulsar/impl_message.go ########## @@ -215,6 +233,7 @@ type message struct { replicatedFrom string redeliveryCount uint32 schema Schema + encryptionContext EncryptionContext Review comment: Let's make this a pointer -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
