You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2021/06/24 17:10:36 UTC
[GitHub] [pulsar-client-go] cckellogg commented on a change in pull request #552: Encryption support in Go client
cckellogg commented on a change in pull request #552:
URL: https://github.com/apache/pulsar-client-go/pull/552#discussion_r658123901
##########
File path: pulsar/consumer_impl.go
##########
@@ -304,25 +305,40 @@ func (c *consumer) internalTopicSubscribeToPartitions() error {
} else {
nackRedeliveryDelay = c.options.NackRedeliveryDelay
}
+
+ // use default message crypto if not already created
+ if c.options.KeyReader != nil && c.options.MessageCrypto == nil {
+ logCtx := fmt.Sprintf("[%v] [%v]", pt, c.options.SubscriptionName)
+ messageCrypto, err := crypto.NewDefaultMessageCrypto(logCtx, false, c.log)
+ if err != nil {
Review comment:
why is an error ok here
##########
File path: pulsar/consumer_impl.go
##########
@@ -25,9 +25,10 @@ import (
"sync"
"time"
+ "github.com/apache/pulsar-client-go/pulsar/crypto"
"github.com/apache/pulsar-client-go/pulsar/internal"
- pb "github.com/apache/pulsar-client-go/pulsar/internal/pulsar_proto"
"github.com/apache/pulsar-client-go/pulsar/log"
+ pb "github.com/apache/pulsar-client-go/pulsar/pulsar_proto"
Review comment:
We want to avoid exposing the proto as a public api. We should move this back to internal
##########
File path: pulsar/crypto/DefaultCryptoKeyReader.go
##########
@@ -0,0 +1,51 @@
+// 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 "io/ioutil"
+
+// DefaultKeyReader default implementation of KeyReader
+type DefaultKeyReader struct {
Review comment:
FileKeyReader?
##########
File path: pulsar/consumer_partition.go
##########
@@ -571,6 +627,114 @@ func (pc *partitionConsumer) MessageReceived(response *pb.CommandMessage, header
return nil
}
+func createEncryptionContext(msgMeta *pb.MessageMetadata) EncryptionContext {
+ encCtx := EncryptionContext{
+ Algorithm: msgMeta.GetEncryptionAlgo(),
+ Param: msgMeta.GetEncryptionParam(),
+ UncompressedSize: int(msgMeta.GetUncompressedSize()),
+ BatchSize: int(msgMeta.GetNumMessagesInBatch()),
+ }
+
+ if msgMeta.Compression != nil {
+ encCtx.CompressionType = CompressionType(*msgMeta.Compression)
+ }
+
+ kMap := map[string]EncryptionKey{}
+ for _, k := range msgMeta.GetEncryptionKeys() {
+ metaMap := map[string]string{}
+ for _, m := range k.GetMetadata() {
+ metaMap[*m.Key] = *m.Value
+ }
+
+ kMap[*k.Key] = EncryptionKey{
+ KeyValue: k.GetValue(),
+ Metadata: metaMap,
+ }
+ }
+
+ encCtx.Keys = kMap
+ return encCtx
+}
+
+func (pc *partitionConsumer) decryptPayLoadIfNeeded(msgID *pb.MessageIdData,
+ msgMeta *pb.MessageMetadata,
+ payload []byte) ([]byte, error) {
+
+ // No encryption keys found in message metadata, do not decrypt payload
+ if len(msgMeta.GetEncryptionKeys()) == 0 {
+ return payload, nil
+ }
+
+ // If KeyReader interface is not implemented, throw an error based on the configuration
+ if pc.options.KeyReader == nil {
+ switch pc.options.consumerCryptoFailureAcrion {
+ case crypto.Consume:
+ pc.log.Warnf("[%v][%v][%v] KeyReader interface is not implemented. Consuming encrypted message.",
+ pc.topic,
+ pc.options.subscription,
+ pc.options.consumerName,
+ )
+ return payload, errKeyReaderNotImplemted
+ case crypto.Discard:
+ pc.log.Warnf(`[%v][%v][%v] Skipping decryption since KeyReader
+ interface is not implemented and config is set to discard`,
+ pc.topic,
+ pc.options.subscription,
+ pc.options.consumerName,
+ )
+ pc.discardCorruptedMessage(msgID, pb.CommandAck_DecryptionError)
+ return nil, errKeyReaderNotImplemted
+ case crypto.FailConsume:
+ pc.log.Errorf(
+ `[%v][%v][%v][%v] Message delivery failed since KeyReader interface
+ is not implemented to consume encrypted message`,
+ pc.topic,
+ pc.options.subscription,
+ pc.options.consumerName,
+ msgID,
+ )
+ return nil, errKeyReaderNotImplemted
+ }
+ }
+
+ decryptedPayload, err := pc.options.messageCrypto.Decrypt(msgMeta, payload, pc.options.KeyReader)
+ if err != nil {
+ errMsg := fmt.Errorf(errDecryptingPayload, err.Error())
+ switch pc.options.consumerCryptoFailureAcrion {
+ case crypto.FailConsume:
+ pc.log.Warnf(
Review comment:
This info about the consumer will already be logged
```
pc.log = client.log.SubLogger(log.Fields{
"name": pc.name,
"topic": options.topic,
"subscription": options.subscription,
"consumerID": pc.consumerID,
})
```
##########
File path: pulsar/consumer_impl.go
##########
@@ -304,25 +305,40 @@ func (c *consumer) internalTopicSubscribeToPartitions() error {
} else {
nackRedeliveryDelay = c.options.NackRedeliveryDelay
}
+
+ // use default message crypto if not already created
+ if c.options.KeyReader != nil && c.options.MessageCrypto == nil {
+ logCtx := fmt.Sprintf("[%v] [%v]", pt, c.options.SubscriptionName)
Review comment:
what is log context and why is it needed?
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org