From: Oliver Kurth Date: Fri, 15 Sep 2017 18:22:51 +0000 (-0700) Subject: CAF does not synchronize correctly with Namespace DB. X-Git-Tag: stable-10.2.0~655 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=92efdf7562187cb1d165b2b91850663c013dedc4;p=thirdparty%2Fopen-vm-tools.git CAF does not synchronize correctly with Namespace DB. --- diff --git a/open-vm-tools/common-agent/Cpp/Framework/Framework/src/Common/CPersistenceUtils.cpp b/open-vm-tools/common-agent/Cpp/Framework/Framework/src/Common/CPersistenceUtils.cpp index ec5692797..64ccf4cab 100644 --- a/open-vm-tools/common-agent/Cpp/Framework/Framework/src/Common/CPersistenceUtils.cpp +++ b/open-vm-tools/common-agent/Cpp/Framework/Framework/src/Common/CPersistenceUtils.cpp @@ -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; } diff --git a/open-vm-tools/common-agent/Cpp/Framework/Framework/src/Common/CPersistenceUtils.h b/open-vm-tools/common-agent/Cpp/Framework/Framework/src/Common/CPersistenceUtils.h index 8cc77fc97..6350c6d9a 100644 --- a/open-vm-tools/common-agent/Cpp/Framework/Framework/src/Common/CPersistenceUtils.h +++ b/open-vm-tools/common-agent/Cpp/Framework/Framework/src/Common/CPersistenceUtils.h @@ -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( diff --git a/open-vm-tools/common-agent/Cpp/ManagementAgent/Subsystems/MaIntegration/src/CConfigEnv.cpp b/open-vm-tools/common-agent/Cpp/ManagementAgent/Subsystems/MaIntegration/src/CConfigEnv.cpp index 75ed74878..0f512b13a 100644 --- a/open-vm-tools/common-agent/Cpp/ManagementAgent/Subsystems/MaIntegration/src/CConfigEnv.cpp +++ b/open-vm-tools/common-agent/Cpp/ManagementAgent/Subsystems/MaIntegration/src/CConfigEnv.cpp @@ -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"); } } diff --git a/open-vm-tools/common-agent/Cpp/ManagementAgent/Subsystems/MaIntegration/src/CConfigEnvMerge.cpp b/open-vm-tools/common-agent/Cpp/ManagementAgent/Subsystems/MaIntegration/src/CConfigEnvMerge.cpp index 348d35ef1..1cf49c51d 100644 --- a/open-vm-tools/common-agent/Cpp/ManagementAgent/Subsystems/MaIntegration/src/CConfigEnvMerge.cpp +++ b/open-vm-tools/common-agent/Cpp/ManagementAgent/Subsystems/MaIntegration/src/CConfigEnvMerge.cpp @@ -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; diff --git a/open-vm-tools/common-agent/Cpp/ManagementAgent/Subsystems/MaIntegration/src/CPersistenceNamespaceDb.cpp b/open-vm-tools/common-agent/Cpp/ManagementAgent/Subsystems/MaIntegration/src/CPersistenceNamespaceDb.cpp index 2071d1960..2c702c671 100644 --- a/open-vm-tools/common-agent/Cpp/ManagementAgent/Subsystems/MaIntegration/src/CPersistenceNamespaceDb.cpp +++ b/open-vm-tools/common-agent/Cpp/ManagementAgent/Subsystems/MaIntegration/src/CPersistenceNamespaceDb.cpp @@ -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; } diff --git a/open-vm-tools/common-agent/Cpp/ManagementAgent/Subsystems/MaIntegration/src/CPersistenceNamespaceDb.h b/open-vm-tools/common-agent/Cpp/ManagementAgent/Subsystems/MaIntegration/src/CPersistenceNamespaceDb.h index a0ce089c4..e4e07a0d0 100644 --- a/open-vm-tools/common-agent/Cpp/ManagementAgent/Subsystems/MaIntegration/src/CPersistenceNamespaceDb.h +++ b/open-vm-tools/common-agent/Cpp/ManagementAgent/Subsystems/MaIntegration/src/CPersistenceNamespaceDb.h @@ -77,7 +77,8 @@ private: std::string _nsdbCmdPath; std::string _nsdbNamespace; - Cmapstrstr _cache; + Csetstr _removedKeys; + std::string _updates; SmartPtrCPersistenceDoc _persistenceUpdate; SmartPtrCPersistenceDoc _persistenceRemove;