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 2018/03/28 01:35:18 UTC

[GitHub] merlimat closed pull request #1417: Replace cached data key from the map if present already

merlimat closed pull request #1417: Replace cached data key from the map if present already
URL: https://github.com/apache/incubator-pulsar/pull/1417
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/pulsar-client-cpp/lib/MessageCrypto.cc b/pulsar-client-cpp/lib/MessageCrypto.cc
index 0cc5decac..c5f41d8f2 100644
--- a/pulsar-client-cpp/lib/MessageCrypto.cc
+++ b/pulsar-client-cpp/lib/MessageCrypto.cc
@@ -122,7 +122,7 @@ void MessageCrypto::removeExpiredDataKey() {
     }
 }
 
-std::string MessageCrypto::stringToHex(const std::string& inputStr, size_t len) {
+std::string MessageCrypto::stringToHex(const char* inputStr, size_t len) {
     static const char* hexVals = "0123456789ABCDEF";
 
     std::string outHex;
@@ -130,19 +130,28 @@ std::string MessageCrypto::stringToHex(const std::string& inputStr, size_t len)
     outHex.push_back('0');
     outHex.push_back('x');
     for (size_t i = 0; i < len; ++i) {
-        const unsigned char c = inputStr[i];
+        const unsigned char c = *(inputStr + i);
         outHex.push_back(hexVals[c >> 4]);
         outHex.push_back(hexVals[c & 15]);
     }
     return outHex;
 }
 
+std::string MessageCrypto::stringToHex(const std::string& inputStr, size_t len) {
+    return stringToHex(inputStr.c_str(), len);
+}
+
 Result MessageCrypto::addPublicKeyCipher(std::set<std::string>& keyNames,
                                          const CryptoKeyReaderPtr keyReader) {
     Lock lock(mutex_);
 
     // Generate data key
     RAND_bytes(dataKey_.get(), dataKeyLen_);
+    if (LOG4CXX_UNLIKELY(logger()->isDebugEnabled())) {
+        std::string dataKeyStr(reinterpret_cast<char*>(dataKey_.get()), dataKeyLen_);
+        std::string strHex = stringToHex(dataKeyStr, dataKeyStr.size());
+        LOG_DEBUG(logCtx_ << "Generated Data key " << strHex);
+    }
 
     Result result = ResultOk;
     for (auto it = keyNames.begin(); it != keyNames.end(); it++) {
@@ -191,7 +200,9 @@ Result MessageCrypto::addPublicKeyCipher(const std::string& keyName, const Crypt
     eki->setKey(encryptedKeyStr);
     eki->setMetadata(keyInfo.getMetadata());
 
-    encryptedDataKeyMap_.insert(std::make_pair(keyName, eki));
+    // Add a new entry or replace existing entry, if one is present.
+    encryptedDataKeyMap_[keyName] = eki;
+
     if (LOG4CXX_UNLIKELY(logger()->isDebugEnabled())) {
         std::string strHex = stringToHex(encryptedKeyStr, encryptedKeyStr.size());
         LOG_DEBUG(logCtx_ << " Data key encrypted for key " << keyName
@@ -200,7 +211,7 @@ Result MessageCrypto::addPublicKeyCipher(const std::string& keyName, const Crypt
     return ResultOk;
 }
 
-bool MessageCrypto::removeKeyCipher(std::string& keyName) {
+bool MessageCrypto::removeKeyCipher(const std::string& keyName) {
     if (!keyName.size()) {
         return false;
     }
diff --git a/pulsar-client-cpp/lib/MessageCrypto.h b/pulsar-client-cpp/lib/MessageCrypto.h
index a4b77f08d..566313f3a 100644
--- a/pulsar-client-cpp/lib/MessageCrypto.h
+++ b/pulsar-client-cpp/lib/MessageCrypto.h
@@ -65,7 +65,7 @@ class MessageCrypto {
      * @param keyName Unique name to identify the key
      * @return true if succeeded, false otherwise
      */
-    bool removeKeyCipher(std::string& keyName);
+    bool removeKeyCipher(const std::string& keyName);
 
     /*
      * Encrypt the payload using the data key and update message metadata with the keyname & encrypted data
@@ -136,6 +136,7 @@ class MessageCrypto {
     bool getKeyAndDecryptData(const proto::MessageMetadata& msgMetadata, SharedBuffer& payload,
                               SharedBuffer& decryptedPayload);
     std::string stringToHex(const std::string& inputStr, size_t len);
+    std::string stringToHex(const char* inputStr, size_t len);
 };
 
 } /* namespace pulsar */


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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


With regards,
Apache Git Services