DB.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. public:
  56. class ChangeListener {
  57. public:
  58. ChangeListener()
  59. {
  60. }
  61. virtual ~ChangeListener()
  62. {
  63. }
  64. virtual void onNetworkUpdate(const void* db, uint64_t networkId, const nlohmann::json& network)
  65. {
  66. }
  67. virtual void onNetworkMemberUpdate(const void* db, uint64_t networkId, uint64_t memberId, const nlohmann::json& member)
  68. {
  69. }
  70. virtual void onNetworkMemberDeauthorize(const void* db, uint64_t networkId, uint64_t memberId)
  71. {
  72. }
  73. };
  74. struct NetworkSummaryInfo {
  75. NetworkSummaryInfo() : authorizedMemberCount(0), totalMemberCount(0), mostRecentDeauthTime(0)
  76. {
  77. }
  78. std::vector<Address> activeBridges;
  79. std::vector<InetAddress> allocatedIps;
  80. unsigned long authorizedMemberCount;
  81. unsigned long totalMemberCount;
  82. int64_t mostRecentDeauthTime;
  83. };
  84. static void initNetwork(nlohmann::json& network);
  85. static void initMember(nlohmann::json& member);
  86. static void cleanNetwork(nlohmann::json& network);
  87. static void cleanMember(nlohmann::json& member);
  88. DB();
  89. virtual ~DB();
  90. virtual bool waitForReady() = 0;
  91. virtual bool isReady() = 0;
  92. inline bool hasNetwork(const uint64_t networkId) const
  93. {
  94. std::shared_lock<std::shared_mutex> l(_networks_l);
  95. return (_networks.find(networkId) != _networks.end());
  96. }
  97. bool get(const uint64_t networkId, nlohmann::json& network);
  98. bool get(const uint64_t networkId, nlohmann::json& network, const uint64_t memberId, nlohmann::json& member);
  99. bool get(const uint64_t networkId, nlohmann::json& network, const uint64_t memberId, nlohmann::json& member, NetworkSummaryInfo& info);
  100. bool get(const uint64_t networkId, nlohmann::json& network, std::vector<nlohmann::json>& members);
  101. void networks(std::set<uint64_t>& networks);
  102. template <typename F> inline void each(F f)
  103. {
  104. nlohmann::json nullJson;
  105. std::unique_lock<std::shared_mutex> lck(_networks_l);
  106. for (auto nw = _networks.begin(); nw != _networks.end(); ++nw) {
  107. f(nw->first, nw->second->config, 0, nullJson); // first provide network with 0 for member ID
  108. for (auto m = nw->second->members.begin(); m != nw->second->members.end(); ++m) {
  109. f(nw->first, nw->second->config, m->first, m->second);
  110. }
  111. }
  112. }
  113. virtual bool save(nlohmann::json& record, bool notifyListeners) = 0;
  114. virtual void eraseNetwork(const uint64_t networkId) = 0;
  115. virtual void eraseMember(const uint64_t networkId, const uint64_t memberId) = 0;
  116. virtual void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress& physicalAddress) = 0;
  117. virtual void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress& physicalAddress, const char* osArch) = 0;
  118. virtual AuthInfo getSSOAuthInfo(const nlohmann::json& member, const std::string& redirectURL)
  119. {
  120. return AuthInfo();
  121. }
  122. inline void addListener(DB::ChangeListener* const listener)
  123. {
  124. std::unique_lock<std::shared_mutex> l(_changeListeners_l);
  125. _changeListeners.push_back(listener);
  126. }
  127. protected:
  128. static inline bool _compareRecords(const nlohmann::json& a, const nlohmann::json& b)
  129. {
  130. if (a.is_object() == b.is_object()) {
  131. if (a.is_object()) {
  132. if (a.size() != b.size())
  133. return false;
  134. auto amap = a.get<nlohmann::json::object_t>();
  135. auto bmap = b.get<nlohmann::json::object_t>();
  136. for (auto ai = amap.begin(); ai != amap.end(); ++ai) {
  137. if (ai->first != "revision") { // ignore revision, compare only non-revision-counter fields
  138. auto bi = bmap.find(ai->first);
  139. if ((bi == bmap.end()) || (bi->second != ai->second))
  140. return false;
  141. }
  142. }
  143. return true;
  144. }
  145. return (a == b);
  146. }
  147. return false;
  148. }
  149. struct _Network {
  150. _Network() : mostRecentDeauthTime(0)
  151. {
  152. }
  153. nlohmann::json config;
  154. std::unordered_map<uint64_t, nlohmann::json> members;
  155. std::unordered_set<uint64_t> activeBridgeMembers;
  156. std::unordered_set<uint64_t> authorizedMembers;
  157. std::unordered_set<InetAddress, InetAddress::Hasher> allocatedIps;
  158. int64_t mostRecentDeauthTime;
  159. std::shared_mutex lock;
  160. };
  161. virtual void _memberChanged(nlohmann::json& old, nlohmann::json& memberConfig, bool notifyListeners);
  162. virtual void _networkChanged(nlohmann::json& old, nlohmann::json& networkConfig, bool notifyListeners);
  163. void _fillSummaryInfo(const std::shared_ptr<_Network>& nw, NetworkSummaryInfo& info);
  164. std::vector<DB::ChangeListener*> _changeListeners;
  165. std::unordered_map<uint64_t, std::shared_ptr<_Network> > _networks;
  166. std::unordered_multimap<uint64_t, uint64_t> _networkByMember;
  167. mutable std::shared_mutex _changeListeners_l;
  168. mutable std::shared_mutex _networks_l;
  169. };
  170. } // namespace ZeroTier
  171. #endif