Skip to content
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

Fix minor api issue and comments. #637

Merged
merged 1 commit into from Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pulsar/crypto/crypto_failure_action.go
Expand Up @@ -21,7 +21,7 @@ const (
// ProducerCryptoFailureActionFail this is the default option to fail send if crypto operation fails.
ProducerCryptoFailureActionFail = iota

// ProducerCryptoFailureActionSend ingnore crypto failure and proceed with sending unencrypted message.
// ProducerCryptoFailureActionSend ignore crypto failure and proceed with sending unencrypted message.
ProducerCryptoFailureActionSend
)

Expand Down
12 changes: 6 additions & 6 deletions pulsar/crypto/default_message_crypto.go
Expand Up @@ -34,7 +34,7 @@ import (
"github.com/apache/pulsar-client-go/pulsar/log"
)

// DefaultMessageCrypto implmentation of the interface MessageCryto
// DefaultMessageCrypto implementation of the interface MessageCryto
type DefaultMessageCrypto struct {
// data key which is used to encrypt/decrypt messages
dataKey []byte
Expand Down Expand Up @@ -134,7 +134,7 @@ func (d *DefaultMessageCrypto) RemoveKeyCipher(keyName string) bool {
return true
}

// Encrypt encrypt payload using encryption keys and add encrypted data key
// Encrypt payload using encryption keys and add encrypted data key
// to message metadata. Here data key is encrypted
// using public key
func (d *DefaultMessageCrypto) Encrypt(encKeys []string,
Expand All @@ -161,7 +161,7 @@ func (d *DefaultMessageCrypto) Encrypt(encKeys []string,
keyInfo, keyInfoOk := k.(*EncryptionKeyInfo)

if keyInfoOk {
msgMetadata.UpsertEncryptionkey(*keyInfo)
msgMetadata.UpsertEncryptionKey(*keyInfo)
} else {
d.logger.Error("failed to get EncryptionKeyInfo for key %v", keyName)
}
Expand Down Expand Up @@ -206,8 +206,8 @@ func (d *DefaultMessageCrypto) Encrypt(encKeys []string,
return gcm.Seal(nil, nonce, payload, nil), nil
}

// Decrypt decrypt the payload using decrypted data key.
// Here data key is read from from the message
// Decrypt the payload using decrypted data key.
// Here data key is read from the message
// metadata and decrypted using private key.
func (d *DefaultMessageCrypto) Decrypt(msgMetadata MessageMetadataSupplier,
payload []byte,
Expand Down Expand Up @@ -285,7 +285,7 @@ func (d *DefaultMessageCrypto) getKeyAndDecryptData(msgMetadata MessageMetadataS
return decryptedData, nil
}
} else {
// First time, entry wont be present in cache
// First time, entry won't be present in cache
d.logger.Debugf("%s Failed to decrypt data or data key is not in cache. Will attempt to refresh", d.logCtx)
}
}
Expand Down
8 changes: 4 additions & 4 deletions pulsar/crypto/encryption_key_Info.go
Expand Up @@ -24,7 +24,7 @@ type EncryptionKeyInfo struct {
name string
}

// NewEncryptionKeyInfo
// NewEncryptionKeyInfo create a new EncryptionKeyInfo
func NewEncryptionKeyInfo(name string, key []byte, metadata map[string]string) *EncryptionKeyInfo {
return &EncryptionKeyInfo{
metadata: metadata,
Expand All @@ -33,17 +33,17 @@ func NewEncryptionKeyInfo(name string, key []byte, metadata map[string]string) *
}
}

// GetKey get key
// Name get the name of the key
func (eci *EncryptionKeyInfo) Name() string {
return eci.name
}

// GetValue get value
// Key get the key data
func (eci *EncryptionKeyInfo) Key() []byte {
return eci.key
}

// GetMetadata get key metadata
// Metadata get key metadata
func (eci *EncryptionKeyInfo) Metadata() map[string]string {
return eci.metadata
}
12 changes: 8 additions & 4 deletions pulsar/crypto/message_crypto.go
Expand Up @@ -19,17 +19,21 @@ package crypto

// MessageCrypto implement this interface to encrypt and decrypt messages
type MessageCrypto interface {
// AddPublicKeyCipher

// AddPublicKeyCipher encrypt data using the public key(s) in the argument.
// If more than one key name is specified, data key is encrypted using each of those keys.
// If the public key is expired or changed, application is responsible to remove
// the old key and add the new key.
AddPublicKeyCipher(keyNames []string, keyReader KeyReader) error

// RemoveKeyCipher remove the key indentified by the keyname from the list
// RemoveKeyCipher remove the key from the list
RemoveKeyCipher(keyName string) bool

// Encrypt the payload using the data key and update
// message metadata with the keyname and encrypted data key
// message metadata with the key and encrypted data key
Encrypt(encKeys []string, KeyReader KeyReader, msgMetadata MessageMetadataSupplier, payload []byte) ([]byte, error)

// Decrypt the payload using the data key.
// Keys used to ecnrypt the data key can be retrieved from msgMetadata
// Keys used to encrypt the data key can be retrieved from msgMetadata
Decrypt(msgMetadata MessageMetadataSupplier, payload []byte, KeyReader KeyReader) ([]byte, error)
}
12 changes: 6 additions & 6 deletions pulsar/crypto/message_metadata.go
Expand Up @@ -23,13 +23,13 @@ import (

// MessageMetadataSupplier wrapper implementation around message metadata
type MessageMetadataSupplier interface {
// GetEncryptionKeys read all the encryption keys from the MessageMetadata
// EncryptionKeys read all the encryption keys from the MessageMetadata
EncryptionKeys() []EncryptionKeyInfo

// UpsertEncryptionkey add new or update existing EncryptionKeys in to the MessageMetadata
UpsertEncryptionkey(EncryptionKeyInfo)
// UpsertEncryptionKey add new or update existing EncryptionKeys in to the MessageMetadata
UpsertEncryptionKey(EncryptionKeyInfo)

// GetEncryptionParam read the ecryption parameter from the MessageMetadata
// EncryptionParam read the ecryption parameter from the MessageMetadata
EncryptionParam() []byte

// SetEncryptionParam set encryption parameter in to the MessageMetadata
Expand Down Expand Up @@ -58,7 +58,7 @@ func (m *MessageMetadata) EncryptionKeys() []EncryptionKeyInfo {
return nil
}

func (m *MessageMetadata) UpsertEncryptionkey(keyInfo EncryptionKeyInfo) {
func (m *MessageMetadata) UpsertEncryptionKey(keyInfo EncryptionKeyInfo) {
if m.messageMetadata != nil {
idx := m.encryptionKeyPresent(keyInfo)
newKey := &pb.EncryptionKeys{
Expand Down Expand Up @@ -101,7 +101,7 @@ func (m *MessageMetadata) encryptionKeyPresent(keyInfo EncryptionKeyInfo) int {

func getKeyMeta(metaMap map[string]string) []*pb.KeyValue {
if len(metaMap) > 0 {
meta := []*pb.KeyValue{}
var meta []*pb.KeyValue
for k, v := range metaMap {
meta = append(meta, &pb.KeyValue{Key: &k, Value: &v})
}
Expand Down
6 changes: 3 additions & 3 deletions pulsar/crypto/message_metadata_test.go
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestGetEncyptionKeys(t *testing.T) {
func TestGetEncryptionKeys(t *testing.T) {
msgMetadata := &pb.MessageMetadata{}
name1 := "key-1"
value1 := []byte{1, 2, 3, 4}
Expand Down Expand Up @@ -95,10 +95,10 @@ func TestUpsertEncryptionKey(t *testing.T) {

msgMetadataSupplier := NewMessageMetadataSupplier(msgMetadata)

msgMetadataSupplier.UpsertEncryptionkey(*keyInfo)
msgMetadataSupplier.UpsertEncryptionKey(*keyInfo)

// try to add same key again
msgMetadataSupplier.UpsertEncryptionkey(*keyInfo)
msgMetadataSupplier.UpsertEncryptionKey(*keyInfo)

actual := msgMetadataSupplier.EncryptionKeys()

Expand Down