2
0

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