DB.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_CONTROLLER_DB_HPP
  14. #define ZT_CONTROLLER_DB_HPP
  15. // #define ZT_CONTROLLER_USE_LIBPQ
  16. #include "../node/Constants.hpp"
  17. #include "../node/Identity.hpp"
  18. #include "../node/InetAddress.hpp"
  19. #include "../osdep/BlockingQueue.hpp"
  20. #include "../osdep/OSUtils.hpp"
  21. #include <atomic>
  22. #include <map>
  23. #include <memory>
  24. #include <nlohmann/json.hpp>
  25. #include <prometheus/simpleapi.h>
  26. #include <set>
  27. #include <shared_mutex>
  28. #include <string>
  29. #include <thread>
  30. #include <unordered_map>
  31. #include <unordered_set>
  32. #include <vector>
  33. #define ZT_MEMBER_AUTH_TIMEOUT_NOTIFY_BEFORE 25000
  34. namespace ZeroTier {
  35. struct AuthInfo {
  36. public:
  37. AuthInfo() : enabled(false), version(0), authenticationURL(), authenticationExpiryTime(0), issuerURL(), centralAuthURL(), ssoNonce(), ssoState(), ssoClientID(), ssoProvider("default")
  38. {
  39. }
  40. bool enabled;
  41. uint64_t version;
  42. std::string authenticationURL;
  43. uint64_t authenticationExpiryTime;
  44. std::string issuerURL;
  45. std::string centralAuthURL;
  46. std::string ssoNonce;
  47. std::string ssoState;
  48. std::string ssoClientID;
  49. std::string ssoProvider;
  50. };
  51. /**
  52. * Base class with common infrastructure for all controller DB implementations
  53. */
  54. class DB {
  55. #ifdef ZT_CONTROLLER_USE_LIBPQ
  56. friend class MemberNotificationReceiver;
  57. friend class NetworkNotificationReceiver;
  58. #endif
  59. public:
  60. class ChangeListener {
  61. public:
  62. ChangeListener()
  63. {
  64. }
  65. virtual ~ChangeListener()
  66. {
  67. }
  68. virtual void onNetworkUpdate(const void* db, uint64_t networkId, const nlohmann::json& network)
  69. {
  70. }
  71. virtual void onNetworkMemberUpdate(const void* db, uint64_t networkId, uint64_t memberId, const nlohmann::json& member)
  72. {
  73. }
  74. virtual void onNetworkMemberDeauthorize(const void* db, uint64_t networkId, uint64_t memberId)
  75. {
  76. }
  77. };
  78. struct NetworkSummaryInfo {
  79. NetworkSummaryInfo() : authorizedMemberCount(0), totalMemberCount(0), mostRecentDeauthTime(0)
  80. {
  81. }
  82. std::vector<Address> activeBridges;
  83. std::vector<InetAddress> allocatedIps;
  84. unsigned long authorizedMemberCount;
  85. unsigned long totalMemberCount;
  86. int64_t mostRecentDeauthTime;
  87. };
  88. static void initNetwork(nlohmann::json& network);
  89. static void initMember(nlohmann::json& member);
  90. static void cleanNetwork(nlohmann::json& network);
  91. static void cleanMember(nlohmann::json& member);
  92. DB();
  93. virtual ~DB();
  94. virtual bool waitForReady() = 0;
  95. virtual bool isReady() = 0;
  96. inline bool hasNetwork(const uint64_t networkId) const
  97. {
  98. std::shared_lock<std::shared_mutex> l(_networks_l);
  99. return (_networks.find(networkId) != _networks.end());
  100. }
  101. bool get(const uint64_t networkId, nlohmann::json& network);
  102. bool get(const uint64_t networkId, nlohmann::json& network, const uint64_t memberId, nlohmann::json& member);
  103. bool get(const uint64_t networkId, nlohmann::json& network, const uint64_t memberId, nlohmann::json& member, NetworkSummaryInfo& info);
  104. bool get(const uint64_t networkId, nlohmann::json& network, std::vector<nlohmann::json>& members);
  105. void networks(std::set<uint64_t>& networks);
  106. template <typename F> inline void each(F f)
  107. {
  108. nlohmann::json nullJson;
  109. std::unique_lock<std::shared_mutex> lck(_networks_l);
  110. for (auto nw = _networks.begin(); nw != _networks.end(); ++nw) {
  111. f(nw->first, nw->second->config, 0, nullJson); // first provide network with 0 for member ID
  112. for (auto m = nw->second->members.begin(); m != nw->second->members.end(); ++m) {
  113. f(nw->first, nw->second->config, m->first, m->second);
  114. }
  115. }
  116. }
  117. virtual bool save(nlohmann::json& record, bool notifyListeners) = 0;
  118. virtual void eraseNetwork(const uint64_t networkId) = 0;
  119. virtual void eraseMember(const uint64_t networkId, const uint64_t memberId) = 0;
  120. virtual void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress& physicalAddress) = 0;
  121. virtual void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress& physicalAddress, const char* osArch) = 0;
  122. virtual AuthInfo getSSOAuthInfo(const nlohmann::json& member, const std::string& redirectURL)
  123. {
  124. return AuthInfo();
  125. }
  126. inline void addListener(DB::ChangeListener* const listener)
  127. {
  128. std::unique_lock<std::shared_mutex> l(_changeListeners_l);
  129. _changeListeners.push_back(listener);
  130. }
  131. protected:
  132. static inline bool _compareRecords(const nlohmann::json& a, const nlohmann::json& b)
  133. {
  134. if (a.is_object() == b.is_object()) {
  135. if (a.is_object()) {
  136. if (a.size() != b.size())
  137. return false;
  138. auto amap = a.get<nlohmann::json::object_t>();
  139. auto bmap = b.get<nlohmann::json::object_t>();
  140. for (auto ai = amap.begin(); ai != amap.end(); ++ai) {
  141. if (ai->first != "revision") { // ignore revision, compare only non-revision-counter fields
  142. auto bi = bmap.find(ai->first);
  143. if ((bi == bmap.end()) || (bi->second != ai->second))
  144. return false;
  145. }
  146. }
  147. return true;
  148. }
  149. return (a == b);
  150. }
  151. return false;
  152. }
  153. struct _Network {
  154. _Network() : mostRecentDeauthTime(0)
  155. {
  156. }
  157. nlohmann::json config;
  158. std::unordered_map<uint64_t, nlohmann::json> members;
  159. std::unordered_set<uint64_t> activeBridgeMembers;
  160. std::unordered_set<uint64_t> authorizedMembers;
  161. std::unordered_set<InetAddress, InetAddress::Hasher> allocatedIps;
  162. int64_t mostRecentDeauthTime;
  163. std::shared_mutex lock;
  164. };
  165. virtual void _memberChanged(nlohmann::json& old, nlohmann::json& memberConfig, bool notifyListeners);
  166. virtual void _networkChanged(nlohmann::json& old, nlohmann::json& networkConfig, bool notifyListeners);
  167. void _fillSummaryInfo(const std::shared_ptr<_Network>& nw, NetworkSummaryInfo& info);
  168. std::vector<DB::ChangeListener*> _changeListeners;
  169. std::unordered_map<uint64_t, std::shared_ptr<_Network> > _networks;
  170. std::unordered_multimap<uint64_t, uint64_t> _networkByMember;
  171. mutable std::shared_mutex _changeListeners_l;
  172. mutable std::shared_mutex _networks_l;
  173. };
  174. } // namespace ZeroTier
  175. #endif