]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
CAF does not synchronize correctly with Namespace DB.
authorOliver Kurth <okurth@vmware.com>
Fri, 15 Sep 2017 18:22:51 +0000 (11:22 -0700)
committerOliver Kurth <okurth@vmware.com>
Fri, 15 Sep 2017 18:22:51 +0000 (11:22 -0700)
open-vm-tools/common-agent/Cpp/Framework/Framework/src/Common/CPersistenceUtils.cpp
open-vm-tools/common-agent/Cpp/Framework/Framework/src/Common/CPersistenceUtils.h
open-vm-tools/common-agent/Cpp/ManagementAgent/Subsystems/MaIntegration/src/CConfigEnv.cpp
open-vm-tools/common-agent/Cpp/ManagementAgent/Subsystems/MaIntegration/src/CConfigEnvMerge.cpp
open-vm-tools/common-agent/Cpp/ManagementAgent/Subsystems/MaIntegration/src/CPersistenceNamespaceDb.cpp
open-vm-tools/common-agent/Cpp/ManagementAgent/Subsystems/MaIntegration/src/CPersistenceNamespaceDb.h

index ec56927974de0da0a05b205cb2b113021dc49faf..64ccf4cab643fc321205e884110081b94672db69 100644 (file)
@@ -224,20 +224,25 @@ void CPersistenceUtils::savePersistence(
        CAF_CM_VALIDATE_SMARTPTR(persistence);
        CAF_CM_VALIDATE_STRING(persistenceDir);
 
-       const SmartPtrCLocalSecurityDoc localSecurity = persistence->getLocalSecurity();
-       const SmartPtrCRemoteSecurityCollectionDoc remoteSecurityCollection =
-                       persistence->getRemoteSecurityCollection();
-       const SmartPtrCPersistenceProtocolCollectionDoc persistenceProtocolCollection =
-                       persistence->getPersistenceProtocolCollection();
+       const std::string protocolDir = FileSystemUtils::buildPath(persistenceDir, "protocol", "amqpBroker_default");
+       const std::string uriAmqp = loadTextFile(protocolDir, "uri_amqp.txt");
+       const std::string uriTunnel = loadTextFile(protocolDir, "uri_tunnel.txt");
 
        if (FileSystemUtils::doesDirectoryExist(persistenceDir)) {
                CAF_CM_LOG_DEBUG_VA1("Removing directory - %s", persistenceDir.c_str());
                FileSystemUtils::recursiveRemoveDirectory(persistenceDir);
        }
 
+       const SmartPtrCLocalSecurityDoc localSecurity = persistence->getLocalSecurity();
+       const SmartPtrCRemoteSecurityCollectionDoc remoteSecurityCollection =
+                       persistence->getRemoteSecurityCollection();
+       const SmartPtrCPersistenceProtocolCollectionDoc persistenceProtocolCollection =
+                       persistence->getPersistenceProtocolCollection();
+
        saveLocalSecurity(localSecurity, persistenceDir);
        saveRemoteSecurityCollection(remoteSecurityCollection, persistenceDir);
-       savePersistenceProtocolCollection(persistenceProtocolCollection, persistenceDir);
+       savePersistenceProtocolCollection(persistenceProtocolCollection, persistenceDir,
+                       uriAmqp, uriTunnel);
        FileSystemUtils::saveTextFile(persistenceDir, "version.txt", persistence->getVersion());
 }
 
@@ -325,7 +330,9 @@ void CPersistenceUtils::saveRemoteSecurityCollection(
 
 void CPersistenceUtils::savePersistenceProtocolCollection(
                const SmartPtrCPersistenceProtocolCollectionDoc& persistenceProtocolCollection,
-               const std::string& persistenceDir) {
+               const std::string& persistenceDir,
+               const std::string& uriAmqp,
+               const std::string& uriTunnel) {
        CAF_CM_STATIC_FUNC_VALIDATE("CPersistenceUtils", "savePersistenceProtocolCollection");
        CAF_CM_VALIDATE_STRING(persistenceDir);
 
@@ -354,14 +361,18 @@ void CPersistenceUtils::savePersistenceProtocolCollection(
                                                        amqpQueueDir, "uri.txt", persistenceProtocol->getUri());
                                }
 
-                               if (! persistenceProtocol->getUriAmqp().empty()) {
+                               const std::string uriAmqpTmp = persistenceProtocol->getUriAmqp().empty() ?
+                                               uriAmqp : persistenceProtocol->getUriAmqp();
+                               if (! uriAmqpTmp.empty()) {
                                        FileSystemUtils::saveTextFile(
-                                                       amqpQueueDir, "uri_amqp.txt", persistenceProtocol->getUriAmqp());
+                                                       amqpQueueDir, "uri_amqp.txt", uriAmqpTmp);
                                }
 
-                               if (! persistenceProtocol->getUriTunnel().empty()) {
+                               const std::string uriTunnelTmp = persistenceProtocol->getUriTunnel().empty() ?
+                                               uriTunnel : persistenceProtocol->getUriTunnel();
+                               if (! uriTunnelTmp.empty()) {
                                        FileSystemUtils::saveTextFile(
-                                                       amqpQueueDir, "uri_tunnel.txt", persistenceProtocol->getUriTunnel());
+                                                       amqpQueueDir, "uri_tunnel.txt", uriTunnelTmp);
                                }
 
                                if (! persistenceProtocol->getTlsCert().empty()) {
@@ -400,7 +411,7 @@ std::string CPersistenceUtils::loadTextFile(
                const std::string& file,
                const std::string& defaultVal,
                const bool isTrimRight) {
-       CAF_CM_STATIC_FUNC_VALIDATE("CPersistenceUtils", "loadTextFile");
+       CAF_CM_STATIC_FUNC_LOG_VALIDATE("CPersistenceUtils", "loadTextFile");
        CAF_CM_VALIDATE_STRING(dir);
        CAF_CM_VALIDATE_STRING(file);
 
@@ -413,6 +424,7 @@ std::string CPersistenceUtils::loadTextFile(
                        rc = CStringUtils::trimRight(rc);
                }
        } else {
+               CAF_CM_LOG_DEBUG_VA1("File not found - %s", path.c_str());
                rc = defaultVal;
        }
 
index 8cc77fc97b0b04f9dba960c9070e02ad512f6194..6350c6d9ab1fba40d96eca89f9171294b74f7fe8 100644 (file)
@@ -54,7 +54,9 @@ public:
 
        static void savePersistenceProtocolCollection(
                        const SmartPtrCPersistenceProtocolCollectionDoc& persistenceProtocolCollection,
-                       const std::string& persistenceDir);
+                       const std::string& persistenceDir,
+                       const std::string& uriAmqp,
+                       const std::string& uriTunnel);
 
 private:
        static std::string loadTextFile(
index 75ed74878aff6300d5957f94a37e093da491d38e..0f512b13a6c3f51ea2c044871ceb2299842e6900 100644 (file)
@@ -90,11 +90,11 @@ SmartPtrCPersistenceDoc CConfigEnv::getUpdated(
                const SmartPtrCPersistenceDoc persistenceTmp =
                                CConfigEnvMerge::mergePersistence(_persistence, _cacertPath, _vcidPath);
                if (! persistenceTmp.IsNull()) {
-                       _persistence = persistenceTmp;
+                       CPersistenceUtils::savePersistence(persistenceTmp, _persistenceDir);
+                       _persistence = CPersistenceUtils::loadPersistence(_persistenceDir);
                        _persistenceUpdated = _persistence;
 
                        savePersistenceAppconfig(_persistence, _configDir);
-                       CPersistenceUtils::savePersistence(_persistence, _persistenceDir);
 
                        const std::string reason = "Info changed in env";
                        listenerConfiguredStage2(reason);
@@ -118,22 +118,27 @@ void CConfigEnv::update(
        CAF_CM_LOCK_UNLOCK;
        CAF_CM_PRECOND_ISINITIALIZED(_isInitialized);
 
-       const SmartPtrCPersistenceDoc persistenceTmp =
+       const SmartPtrCPersistenceDoc persistenceTmp1 =
                        CPersistenceMerge::mergePersistence(_persistence, persistence);
+
+       const SmartPtrCPersistenceDoc persistenceIn = persistenceTmp1.IsNull() ? _persistence : persistenceTmp1;
+       const SmartPtrCPersistenceDoc persistenceTmp2 =
+                       CConfigEnvMerge::mergePersistence(persistenceIn, _cacertPath, _vcidPath);
+
+       const SmartPtrCPersistenceDoc persistenceTmp = persistenceTmp2.IsNull() ? persistenceTmp1 : persistenceTmp2;
+
        if (! persistenceTmp.IsNull()) {
-               _persistence = persistenceTmp;
+               CPersistenceUtils::savePersistence(persistenceTmp, _persistenceDir);
+               _persistence = CPersistenceUtils::loadPersistence(_persistenceDir);
                _persistenceUpdated = _persistence;
 
                savePersistenceAppconfig(_persistence, _configDir);
-               CPersistenceUtils::savePersistence(_persistence, _persistenceDir);
                removePrivateKey(_persistence, _persistenceRemove);
 
                const std::string reason = "Info changed at source";
                listenerConfiguredStage1(reason);
                listenerConfiguredStage2(reason);
                restartListener(reason);
-       } else {
-               CAF_CM_LOG_DEBUG_VA0("Persistence info did not change");
        }
 }
 
index 348d35ef1245d7269e6541865bc44b01d96d32d6..1cf49c51d3e6df72da40453f984ec53840e180a0 100644 (file)
@@ -163,13 +163,9 @@ std::string CConfigEnvMerge::mergeUri(
        CAF_CM_VALIDATE_STRING(localId);
 
        const std::string uri = persistenceProtocol->getUri();
-
-       std::string uriNew;
-       if (isTunnelEnabled) {
-               uriNew = loadTextFile(persistenceProtocol->getUriTunnelPath());
-       } else {
-               uriNew = loadTextFile(persistenceProtocol->getUriAmqpPath());
-       }
+       const std::string uriNew = isTunnelEnabled ?
+                       persistenceProtocol->getUriTunnel() :
+                       persistenceProtocol->getUriAmqp();
        CAF_CM_VALIDATE_STRING(uriNew);
 
        CAF_CM_LOG_DEBUG_VA3("uri: %s, uriNew: %s, localId: %s",
@@ -287,13 +283,15 @@ bool CConfigEnvMerge::isTunnelEnabledFunc() {
 
 std::string CConfigEnvMerge::loadTextFile(
                const std::string& path) {
-       CAF_CM_STATIC_FUNC_VALIDATE("CConfigEnvMerge", "loadTextFile");
+       CAF_CM_STATIC_FUNC_LOG_VALIDATE("CConfigEnvMerge", "loadTextFile");
        CAF_CM_VALIDATE_STRING(path);
 
        std::string rc;
        if (FileSystemUtils::doesFileExist(path)) {
                rc = FileSystemUtils::loadTextFile(path);
                rc = CStringUtils::trimRight(rc);
+       } else {
+               CAF_CM_LOG_DEBUG_VA1("File does not exist - %s", path.c_str());
        }
 
        return rc;
index 2071d19601b96fe4a551b86bb01751d2a04f375f..2c702c6717ececa2dbeae276a9952ccd78cd1329 100644 (file)
@@ -57,9 +57,9 @@ SmartPtrCPersistenceDoc CPersistenceNamespaceDb::getUpdated(
 
        SmartPtrCPersistenceDoc rc;
        if (isReady()) {
-               //If nothing has been updated, skip all of the unneeded work
-               const std::string updates = getValue("updates");
-               if (!updates.empty()) {
+               const std::string updatesCur = getValue("updates");
+               if (_updates.compare(updatesCur) != 0) {
+                       _updates = updatesCur;
                        const std::string version = getValue("version");
 
                        //EP Doc
@@ -131,11 +131,11 @@ SmartPtrCPersistenceDoc CPersistenceNamespaceDb::getUpdated(
 
                                //For now, we only support one broker.
                                const std::string protocolName = getValue(protocolKey + ".protocol_name");
+                               const std::string tlsCert = getValue(protocolKey + ".tls.cert");
+                               const std::string tlsProtocol = getValue(protocolKey + ".tls.protocol");
                                const std::string uri = getValue(protocolKey + ".uri");
                                const std::string uriAmqp = getValue(protocolKey + ".uri.amqp");
                                const std::string uriTunnel = getValue(protocolKey + ".uri.tunnel");
-                               const std::string tlsCert = getValue(protocolKey + ".tls.cert");
-                               const std::string tlsProtocol = getValue(protocolKey + ".tls.protocol");
 
                                SmartPtrCPersistenceProtocolDoc persistenceProtocol;
                                persistenceProtocol.CreateInstance();
@@ -380,51 +380,50 @@ std::string CPersistenceNamespaceDb::getValue(const std::string& key) {
        }
        return value;
 }
-       
+
 void CPersistenceNamespaceDb::setValue(
                const std::string& key,
                const std::string& value) {
        CAF_CM_FUNCNAME("setValue");
        CAF_CM_VALIDATE_STRING(key);
 
-       if (value.empty()) {
-               return;
-       }
+       if (_removedKeys.find(key) == _removedKeys.end()) {
+               if (value.empty()) {
+                       CAF_CM_LOG_DEBUG_VA1("Cannot set empty value: %s", key.c_str());
+                       return;
+               }
 
-       std::string stdoutContent;
-       std::string stderrContent;
-       Cdeqstr argv;
-       std::string tmpFile;
+               std::string stdoutContent;
+               std::string stderrContent;
+               Cdeqstr argv;
+               std::string tmpFile;
 
-       try {
-               //TODO: generate hash of value
-               //TODO: prepend delimitted hash to value
-
-               tmpFile = FileSystemUtils::saveTempTextFile("caf_nsdb_XXXXXX", value);
-               CAF_CM_LOG_DEBUG_VA2("Setting %s to %s", key.c_str(), value.c_str());
-               argv.push_back(_nsdbCmdPath);
-               argv.push_back("set-key");
-               argv.push_back(_nsdbNamespace);
-               argv.push_back("-k");
-               argv.push_back(key);
-               argv.push_back("-f");
-               argv.push_back(tmpFile);
-                               
-               ProcessUtils::runSync(argv, stdoutContent, stderrContent);
-               //Add to key+hash _cache
-               //TODO: generate a hash of the value std::string
-               _cache[key] = value; //As a temporary hack use the entire value as the "hash"
-       }
-       catch(ProcessFailedException* ex){
-               CAF_CM_LOG_DEBUG_VA1("exception: %s", ex->getMsg().c_str());
-               CAF_CM_EXCEPTION_VA3(E_UNEXPECTED,
-                               "NamespaceDB command failed - %s: %s: %s",
-                               ex->getMsg().c_str(),
-                               stdoutContent.c_str(),
-                               stderrContent.c_str());
-       }
-       if ( !tmpFile.empty() && FileSystemUtils::doesFileExist(tmpFile)) {
-               FileSystemUtils::removeFile(tmpFile);
+               try {
+                       tmpFile = FileSystemUtils::saveTempTextFile("caf_nsdb_XXXXXX", value);
+                       CAF_CM_LOG_DEBUG_VA2("Setting %s to %s", key.c_str(), value.c_str());
+                       argv.push_back(_nsdbCmdPath);
+                       argv.push_back("set-key");
+                       argv.push_back(_nsdbNamespace);
+                       argv.push_back("-k");
+                       argv.push_back(key);
+                       argv.push_back("-f");
+                       argv.push_back(tmpFile);
+
+                       ProcessUtils::runSync(argv, stdoutContent, stderrContent);
+               }
+               catch(ProcessFailedException* ex){
+                       CAF_CM_LOG_DEBUG_VA1("exception: %s", ex->getMsg().c_str());
+                       CAF_CM_EXCEPTION_VA3(E_UNEXPECTED,
+                                       "NamespaceDB command failed - %s: %s: %s",
+                                       ex->getMsg().c_str(),
+                                       stdoutContent.c_str(),
+                                       stderrContent.c_str());
+               }
+               if ( !tmpFile.empty() && FileSystemUtils::doesFileExist(tmpFile)) {
+                       FileSystemUtils::removeFile(tmpFile);
+               }
+       } else {
+               CAF_CM_LOG_DEBUG_VA1("Cannot set a removed key: %s", key.c_str());
        }
 }
        
@@ -432,32 +431,31 @@ void CPersistenceNamespaceDb::removeKey(const std::string& key) {
        CAF_CM_FUNCNAME("removeKey");   
        CAF_CM_VALIDATE_STRING(key);
 
-       std::string stdoutContent;
-       std::string stderrContent;
-       Cdeqstr argv;
+       if (_removedKeys.find(key) == _removedKeys.end()) {
+               std::string stdoutContent;
+               std::string stderrContent;
+               Cdeqstr argv;
 
-       try {
-               argv.push_back(_nsdbCmdPath);
-               argv.push_back("delete-key");
-               argv.push_back(_nsdbNamespace);
-               argv.push_back("-k");
-               argv.push_back(key);
-
-               ProcessUtils::runSync(argv, stdoutContent, stderrContent);
-
-               //Remove from _cache
-               Cmapstrstr::iterator it = _cache.find(key);
-               if (it != _cache.end()) {
-                       _cache.erase(it);
+               try {
+                       argv.push_back(_nsdbCmdPath);
+                       argv.push_back("delete-key");
+                       argv.push_back(_nsdbNamespace);
+                       argv.push_back("-k");
+                       argv.push_back(key);
+
+                       ProcessUtils::runSync(argv, stdoutContent, stderrContent);
+                       _removedKeys.insert(key);
                }
-       }
-       catch(ProcessFailedException* ex){
-               CAF_CM_LOG_DEBUG_VA1("exception: %s", ex->getMsg().c_str());            
-               CAF_CM_EXCEPTION_VA3(E_UNEXPECTED,
-                               "NamespaceDB command failed - %s: %s: %s",
-                               ex->getMsg().c_str(),
-                               stdoutContent.c_str(),
-                               stderrContent.c_str());
+               catch(ProcessFailedException* ex){
+                       CAF_CM_LOG_DEBUG_VA1("exception: %s", ex->getMsg().c_str());
+                       CAF_CM_EXCEPTION_VA3(E_UNEXPECTED,
+                                       "NamespaceDB command failed - %s: %s: %s",
+                                       ex->getMsg().c_str(),
+                                       stdoutContent.c_str(),
+                                       stderrContent.c_str());
+               }
+       } else {
+               CAF_CM_LOG_DEBUG_VA1("Key already removed: %s", key.c_str());
        }
 }
 
@@ -518,18 +516,6 @@ std::string CPersistenceNamespaceDb::getValueRaw(
                if (value[value.length()-1] == '"')
                        value.erase(value.length()-1,1);
        }
-       //TODO: parse hash from nsdb value
-       std::string hash = value; //As a temporary hack, use the entire value as the "hash"
-       //if hash has not changed, return empty
-       if (_cache[key] == hash) {
-               CAF_CM_LOG_DEBUG_VA1("Value for %s has not changed", key.c_str());
-               value = "";
-       }
-       else {
-       //if hash has changed, update key+hash _cache and return value
-               CAF_CM_LOG_DEBUG_VA1("Value for %s has changed", key.c_str());
-               _cache[key] = hash;
-       }
 
        return value;
 }
index a0ce089c4a04900a81f84ceaf95f567b2cb076e8..e4e07a0d01a52a172e4193f8171e5642cbd2331d 100644 (file)
@@ -77,7 +77,8 @@ private:
 
        std::string _nsdbCmdPath;
        std::string _nsdbNamespace;
-       Cmapstrstr _cache;
+       Csetstr _removedKeys;
+       std::string _updates;
 
        SmartPtrCPersistenceDoc _persistenceUpdate;
        SmartPtrCPersistenceDoc _persistenceRemove;