From ea56c568a76b60cea5b181ee3549cdfc60af9089 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Mon, 12 Jun 2017 16:55:37 -0700 Subject: [PATCH] Add support for replicating by username --- domain-server/src/DomainServer.cpp | 31 +++++++++++++++++-- domain-server/src/DomainServer.h | 6 ++++ .../src/DomainServerSettingsManager.cpp | 10 +++--- .../src/DomainServerSettingsManager.h | 1 + libraries/networking/src/Node.h | 1 + 5 files changed, 43 insertions(+), 6 deletions(-) diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index de53898057..afe9b7ca50 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -117,6 +117,8 @@ DomainServer::DomainServer(int argc, char* argv[]) : // if permissions are updated, relay the changes to the Node datastructures connect(&_settingsManager, &DomainServerSettingsManager::updateNodePermissions, &_gatekeeper, &DomainGatekeeper::updateNodePermissions); + connect(&_settingsManager, &DomainServerSettingsManager::settingsUpdated, + this, &DomainServer::updateReplicatedNodes); setupGroupCacheRefresh(); @@ -2210,9 +2212,34 @@ void DomainServer::refreshStaticAssignmentAndAddToQueue(SharedAssignmentPointer& _unfulfilledAssignments.enqueue(assignment); } +void DomainServer::updateReplicatedNodes() { + static const QString REPLICATION_SETTINGS_KEY = "replication"; + _replicatedUsernames.clear(); + auto settings = _settingsManager.getSettingsMap(); + if (settings.contains(REPLICATION_SETTINGS_KEY)) { + auto replicationSettings = settings.value(REPLICATION_SETTINGS_KEY).toJsonObject(); + auto usersSettings = replicationSettings.value("users").toArray(); + for (auto& username : usersSettings) { + _replicatedUsernames.push_back(username.toString()); + } + } + + auto nodeList = DependencyManager::get(); + nodeList->eachNode([&](const SharedNodePointer& otherNode) { + if (shouldReplicateNode(*otherNode)) { + otherNode->setIsReplicated(true); + } + }); +} + +bool DomainServer::shouldReplicateNode(const Node& node) { + QString verifiedUsername = node.getPermissions().getVerifiedUserName(); + auto it = find(_replicatedUsernames.cbegin(), _replicatedUsernames.cend(), verifiedUsername); + return it != _replicatedUsernames.end() && node.getType() == NodeType::Agent; +}; + void DomainServer::nodeAdded(SharedNodePointer node) { - // TODO Check to see if node is in list of replicated nodes - if (node->getType() == NodeType::Agent) { + if (shouldReplicateNode(*node)) { node->setIsReplicated(true); } diff --git a/domain-server/src/DomainServer.h b/domain-server/src/DomainServer.h index 63b82cb37d..3538f85f68 100644 --- a/domain-server/src/DomainServer.h +++ b/domain-server/src/DomainServer.h @@ -102,6 +102,8 @@ private slots: void handleOctreeFileReplacement(QByteArray octreeFile); + void updateReplicatedNodes(); + signals: void iceServerChanged(); void userConnected(); @@ -161,12 +163,16 @@ private: QJsonObject jsonForSocket(const HifiSockAddr& socket); QJsonObject jsonObjectForNode(const SharedNodePointer& node); + bool DomainServer::shouldReplicateNode(const Node& node); + void setupGroupCacheRefresh(); QString pathForRedirect(QString path = QString()) const; SubnetList _acSubnetWhitelist; + std::vector _replicatedUsernames; + DomainGatekeeper _gatekeeper; HTTPManager _httpManager; diff --git a/domain-server/src/DomainServerSettingsManager.cpp b/domain-server/src/DomainServerSettingsManager.cpp index d6b57b450a..3d256bd2b9 100644 --- a/domain-server/src/DomainServerSettingsManager.cpp +++ b/domain-server/src/DomainServerSettingsManager.cpp @@ -991,6 +991,7 @@ bool DomainServerSettingsManager::handleAuthenticatedHTTPRequest(HTTPConnection unpackPermissions(); apiRefreshGroupInformation(); emit updateNodePermissions(); + emit settingsUpdated(); } return true; @@ -1196,13 +1197,14 @@ QJsonObject DomainServerSettingsManager::settingDescriptionFromGroup(const QJson bool DomainServerSettingsManager::recurseJSONObjectAndOverwriteSettings(const QJsonObject& postedObject) { static const QString SECURITY_ROOT_KEY = "security"; static const QString AC_SUBNET_WHITELIST_KEY = "ac_subnet_whitelist"; + static const QString REPLICATION_KEY = "replication"; auto& settingsVariant = _configMap.getConfig(); bool needRestart = false; // Iterate on the setting groups foreach(const QString& rootKey, postedObject.keys()) { - QJsonValue rootValue = postedObject[rootKey]; + const QJsonValue& rootValue = postedObject[rootKey]; if (!settingsVariant.contains(rootKey)) { // we don't have a map below this key yet, so set it up now @@ -1247,7 +1249,7 @@ bool DomainServerSettingsManager::recurseJSONObjectAndOverwriteSettings(const QJ if (!matchingDescriptionObject.isEmpty()) { updateSetting(rootKey, rootValue, *thisMap, matchingDescriptionObject); - if (rootKey != SECURITY_ROOT_KEY) { + if (rootKey != SECURITY_ROOT_KEY && rootKey != REPLICATION_KEY) { needRestart = true; } } else { @@ -1261,9 +1263,9 @@ bool DomainServerSettingsManager::recurseJSONObjectAndOverwriteSettings(const QJ // if we matched the setting then update the value if (!matchingDescriptionObject.isEmpty()) { - QJsonValue settingValue = rootValue.toObject()[settingKey]; + const QJsonValue& settingValue = rootValue.toObject()[settingKey]; updateSetting(settingKey, settingValue, *thisMap, matchingDescriptionObject); - if (rootKey != SECURITY_ROOT_KEY || settingKey == AC_SUBNET_WHITELIST_KEY) { + if ((rootKey != SECURITY_ROOT_KEY && rootKey != REPLICATION_KEY) || settingKey == AC_SUBNET_WHITELIST_KEY) { needRestart = true; } } else { diff --git a/domain-server/src/DomainServerSettingsManager.h b/domain-server/src/DomainServerSettingsManager.h index d56a786d4b..4c7d8dfbc9 100644 --- a/domain-server/src/DomainServerSettingsManager.h +++ b/domain-server/src/DomainServerSettingsManager.h @@ -108,6 +108,7 @@ public: signals: void updateNodePermissions(); + void settingsUpdated(); public slots: void apiGetGroupIDJSONCallback(QNetworkReply& requestReply); diff --git a/libraries/networking/src/Node.h b/libraries/networking/src/Node.h index fd2cf6b65b..33c9e2c205 100644 --- a/libraries/networking/src/Node.h +++ b/libraries/networking/src/Node.h @@ -105,6 +105,7 @@ private: bool _isUpstream { false }; tbb::concurrent_unordered_set _ignoredNodeIDSet; mutable QReadWriteLock _ignoredNodeIDSetLock; + std::vector _replicatedUsernames { }; std::atomic_bool _ignoreRadiusEnabled; };