cckellogg commented on a change in pull request #560: URL: https://github.com/apache/pulsar-client-go/pull/560#discussion_r685401565
########## File path: pulsar/internal/crypto/producer_encryptor.go ########## @@ -0,0 +1,72 @@ +// 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 ( + "fmt" + + "github.com/apache/pulsar-client-go/pulsar/crypto" + pb "github.com/apache/pulsar-client-go/pulsar/internal/pulsar_proto" + "github.com/apache/pulsar-client-go/pulsar/log" +) + +type producerEncryptor struct { + keys []string + keyReader crypto.KeyReader + messageCrypto crypto.MessageCrypto + logger log.Logger + producerCryptoFailureAction int +} + +func NewProducerEncryptor(keys []string, + keyReader crypto.KeyReader, + messageCrypto crypto.MessageCrypto, + producerCryptoFailureAction int, + logger log.Logger) Encryptor { + return &producerEncryptor{ + keys: keys, + keyReader: keyReader, + messageCrypto: messageCrypto, + logger: logger, + producerCryptoFailureAction: producerCryptoFailureAction, + } +} + +// Encrypt producer encryptor +func (e *producerEncryptor) Encrypt(payload []byte, msgMetadata *pb.MessageMetadata) ([]byte, error) { + // encrypt payload + encryptedPayload, err := e.messageCrypto.Encrypt(e.keys, + e.keyReader, + crypto.NewMessageMetadataSupplier(msgMetadata), + payload) + + // error encryping the payload + if err != nil { + // error occurred in encrypting the payload + // crypto ProducerCryptoFailureAction is set to send + // send unencrypted message + if e.producerCryptoFailureAction == crypto.ProducerCryptoFailureActionSend { + e.logger.Errorf("Encryption of payload failed : %v", err) + e.logger.Warn("ProducerCryptoFailureAction is set to send, sending unecrypted message") Review comment: I would log only one warning message here. ``` logger.WithError(err).Warnf("Encryption failed for payload sending unencrypted message ProducerCryptoFailureAction is set to send") ``` ########## File path: pulsar/internal/commands.go ########## @@ -221,9 +222,21 @@ func serializeBatch(wb Buffer, cmdSend *pb.BaseCommand, msgMetadata *pb.MessageMetadata, uncompressedPayload Buffer, - compressionProvider compression.Provider) { + compressionProvider compression.Provider, + encryptor crypto.Encryptor) { // Wire format // [TOTAL_SIZE] [CMD_SIZE][CMD] [MAGIC_NUMBER][CHECKSUM] [METADATA_SIZE][METADATA] [PAYLOAD] + + // compress the payload + compressedPayload := compressionProvider.Compress(nil, uncompressedPayload.ReadableSlice()) + + // encrypt the compressed payload + encryptedPayload, err := encryptor.Encrypt(compressedPayload, crypto.NewMessageMetadataSupplier(msgMetadata)) + if err != nil { + // error occurred while encrypting the payload, ProducerCryptoFailureAction is set to Fail + panic(fmt.Sprintf("Encryption of message failed, ProducerCryptoFailureAction is set to Fail. Error :%v", err)) Review comment: Is that exception recoverable? ########## File path: pulsar/producer_partition.go ########## @@ -126,6 +129,29 @@ func newPartitionProducer(client *client, topic string, options *ProducerOptions p.producerName = options.Name } + encryption := options.Encryption + // add default message crypto if not provided + if encryption != nil && len(encryption.Keys) > 0 { + if encryption.KeyReader == nil { + return nil, fmt.Errorf("encryption is enabled, KeyReader can not be nil") + } + + if encryption.MessageCrypto == nil { + logCtx := fmt.Sprintf("[%v] [%v] [%v]", p.topic, p.producerName, p.producerID) Review comment: Maybe this can be update in another MR. It might be better to create a context logger instead of sending a string ``` cl := logger.WithFields(log.Fields{ "topic": topic, "producer": producerName, "producerID", producerID }) ``` ########## File path: pulsar/producer_partition.go ########## @@ -781,6 +821,17 @@ func (p *partitionProducer) internalClose(req *closeProducer) { p.batchFlushTicker.Stop() } +func (p *partitionProducer) generateDataKey() error { + if p.options.Encryption != nil { + if p.options.Encryption.KeyReader != nil { + return p.options.Encryption.MessageCrypto.AddPublicKeyCipher(p.options.Encryption.Keys, Review comment: Could the MessageCrypto be nil here? -- 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]
