-
Notifications
You must be signed in to change notification settings - Fork 346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Encryption support producer #560
Changes from 11 commits
d8bb8fe
aa40a8d
3b8d912
e1a8108
d200b7c
feaf120
a2bd72b
f204347
a561923
559dbaf
d7246bc
62937c5
39d43b2
d88b04e
41b10bc
c362133
06c7612
4dc8ddb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// 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 pulsar | ||
|
||
import "github.com/apache/pulsar-client-go/pulsar/crypto" | ||
|
||
// ProducerEncryptionInfo encryption related fields required by the producer | ||
type ProducerEncryptionInfo struct { | ||
// KeyReader read RSA public/private key pairs | ||
Keyreader crypto.KeyReader | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
// MessageCrypto used to encrypt and decrypt the data and session keys | ||
MessageCrypto crypto.MessageCrypto | ||
|
||
// Keys list of encryption key names to encrypt session key | ||
Keys []string | ||
|
||
// ProducerCryptoFailureAction action to be taken on failure of message encryption | ||
// default is ProducerCryptoFailureActionFail | ||
ProducerCryptoFailureAction int | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ import ( | |
"github.com/gogo/protobuf/proto" | ||
|
||
"github.com/apache/pulsar-client-go/pulsar/internal/compression" | ||
"github.com/apache/pulsar-client-go/pulsar/internal/crypto" | ||
pb "github.com/apache/pulsar-client-go/pulsar/internal/pulsar_proto" | ||
) | ||
|
||
|
@@ -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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We want to compress before encrypting? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per java implementation => Yes compress and then encrypt. |
||
|
||
// 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)) | ||
GPrabhudas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
cmdSize := uint32(proto.Size(cmdSend)) | ||
msgMetadataSize := uint32(proto.Size(msgMetadata)) | ||
|
||
|
@@ -234,7 +247,7 @@ func serializeBatch(wb Buffer, | |
// Write cmd | ||
wb.WriteUint32(cmdSize) | ||
wb.ResizeIfNeeded(cmdSize) | ||
_, err := cmdSend.MarshalToSizedBuffer(wb.WritableSlice()[:cmdSize]) | ||
_, err = cmdSend.MarshalToSizedBuffer(wb.WritableSlice()[:cmdSize]) | ||
if err != nil { | ||
panic(fmt.Sprintf("Protobuf error when serializing cmdSend: %v", err)) | ||
} | ||
|
@@ -255,12 +268,8 @@ func serializeBatch(wb Buffer, | |
} | ||
wb.WrittenBytes(msgMetadataSize) | ||
|
||
// Make sure the buffer has enough space to hold the compressed data | ||
// and perform the compression in-place | ||
maxSize := uint32(compressionProvider.CompressMaxSize(int(uncompressedPayload.ReadableBytes()))) | ||
wb.ResizeIfNeeded(maxSize) | ||
b := compressionProvider.Compress(wb.WritableSlice()[:0], uncompressedPayload.ReadableSlice()) | ||
wb.WrittenBytes(uint32(len(b))) | ||
// add payload to the buffer | ||
wb.Write(encryptedPayload) | ||
|
||
// Write checksum at created checksum-placeholder | ||
frameEndIdx := wb.WriterIndex() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// 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 ( | ||
"github.com/apache/pulsar-client-go/pulsar/crypto" | ||
pb "github.com/apache/pulsar-client-go/pulsar/internal/pulsar_proto" | ||
) | ||
|
||
// Encryptor support encryption | ||
type Encryptor interface { | ||
Encrypt([]byte, crypto.MessageMetadataSupplier) ([]byte, error) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is internal we can pass the *pb.MessageMetadata and avoid having to create a supplier for each message when encryption is not being used. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
|
||
// NewMessageMetadataSupplier returns wrapper for message metadata | ||
func NewMessageMetadataSupplier(msgMetadata *pb.MessageMetadata) crypto.MessageMetadataSupplier { | ||
return crypto.NewMessageMetadataSupplier(msgMetadata) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// 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 "github.com/apache/pulsar-client-go/pulsar/crypto" | ||
|
||
type noopEncryptor struct{} | ||
|
||
func NewNoopEncryptor() Encryptor { | ||
return &noopEncryptor{} | ||
} | ||
|
||
// Encrypt Noop ecryptor | ||
func (e *noopEncryptor) Encrypt(data []byte, msgMetadata crypto.MessageMetadataSupplier) ([]byte, error) { | ||
return data, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// 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" | ||
"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 crypto.MessageMetadataSupplier) ([]byte, error) { | ||
// encryption is enabled but KeyReader interface is not implemented | ||
if e.keyReader == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be detected and an error raised while setting up the producer? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
// crypto failure action is set to send | ||
// send unencrypted message | ||
if e.producerCryptoFailureAction == crypto.ProducerCryptoFailureActionSend { | ||
return payload, nil | ||
} | ||
return nil, fmt.Errorf("KeyReader interface is not implemented and ProducerCryptoFailureAction is set to fail") | ||
} | ||
|
||
// encrypt payload | ||
encryptedPayload, err := e.messageCrypto.Encrypt(e.keys, e.keyReader, 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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would log only one warning message here.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
return payload, nil | ||
} | ||
|
||
return nil, fmt.Errorf("ProducerCryptoFailureAction is set to Fail and error occurred in encrypting payload :%v", err) | ||
} | ||
return encryptedPayload, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious why is the mod and sum file changing? Can these changes be done in a separate PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me recheck again :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
synced with master branch