DB.hpp 5.7 KB

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