CentralDB.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  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. #include "CentralDB.hpp"
  14. #ifdef ZT_CONTROLLER_USE_LIBPQ
  15. #include "../../node/Constants.hpp"
  16. #include "../../node/SHA512.hpp"
  17. #include "../../version.h"
  18. #include "BigTableStatusWriter.hpp"
  19. #include "ControllerConfig.hpp"
  20. #include "CtlUtil.hpp"
  21. #include "EmbeddedNetworkController.hpp"
  22. #include "PostgresStatusWriter.hpp"
  23. #include "PubSubListener.hpp"
  24. #include "Redis.hpp"
  25. #include "RedisListener.hpp"
  26. #include "RedisStatusWriter.hpp"
  27. #include "opentelemetry/trace/provider.h"
  28. #include <chrono>
  29. #include <climits>
  30. #include <iomanip>
  31. #include <libpq-fe.h>
  32. #include <optional>
  33. #include <pqxx/pqxx>
  34. #include <rustybits.h>
  35. #include <sstream>
  36. // #define REDIS_TRACE 1
  37. using json = nlohmann::json;
  38. using namespace ZeroTier;
  39. using Attrs = std::vector<std::pair<std::string, std::string> >;
  40. using Item = std::pair<std::string, Attrs>;
  41. using ItemStream = std::vector<Item>;
  42. CentralDB::CentralDB(
  43. const Identity& myId,
  44. const char* connString,
  45. int listenPort,
  46. CentralDB::ListenerMode listenMode,
  47. CentralDB::StatusWriterMode statusMode,
  48. const ControllerConfig* cc)
  49. : DB()
  50. , _listenerMode(listenMode)
  51. , _statusWriterMode(statusMode)
  52. , _cc(cc)
  53. , _pool()
  54. , _myId(myId)
  55. , _myAddress(myId.address())
  56. , _ready(0)
  57. , _connected(1)
  58. , _run(1)
  59. , _waitNoticePrinted(false)
  60. , _listenPort(listenPort)
  61. , _redis(NULL)
  62. , _cluster(NULL)
  63. , _redisMemberStatus(false)
  64. , _smee(NULL)
  65. {
  66. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  67. auto tracer = provider->GetTracer("CentralDB");
  68. auto span = tracer->StartSpan("CentralDB::CentralDB");
  69. auto scope = tracer->WithActiveSpan(span);
  70. rustybits::init_async_runtime();
  71. char myAddress[64];
  72. _myAddressStr = myId.address().toString(myAddress);
  73. _connString = std::string(connString);
  74. auto f = std::make_shared<PostgresConnFactory>(_connString);
  75. _pool =
  76. std::make_shared<ConnectionPool<PostgresConnection> >(15, 5, std::static_pointer_cast<ConnectionFactory>(f));
  77. memset(_ssoPsk, 0, sizeof(_ssoPsk));
  78. char* const ssoPskHex = getenv("ZT_SSO_PSK");
  79. #ifdef ZT_TRACE
  80. fprintf(stderr, "ZT_SSO_PSK: %s\n", ssoPskHex);
  81. #endif
  82. if (ssoPskHex) {
  83. // SECURITY: note that ssoPskHex will always be null-terminated if libc actually
  84. // returns something non-NULL. If the hex encodes something shorter than 48 bytes,
  85. // it will be padded at the end with zeroes. If longer, it'll be truncated.
  86. Utils::unhex(ssoPskHex, _ssoPsk, sizeof(_ssoPsk));
  87. }
  88. const char* redisMemberStatus = getenv("ZT_REDIS_MEMBER_STATUS");
  89. if (redisMemberStatus && (strcmp(redisMemberStatus, "true") == 0)) {
  90. _redisMemberStatus = true;
  91. fprintf(stderr, "Using redis for member status\n");
  92. }
  93. if ((listenMode == LISTENER_MODE_REDIS || statusMode == STATUS_WRITER_MODE_REDIS) && _cc->redisConfig != NULL) {
  94. auto innerspan = tracer->StartSpan("CentralDB::CentralDB::configureRedis");
  95. auto innerscope = tracer->WithActiveSpan(innerspan);
  96. sw::redis::ConnectionOptions opts;
  97. sw::redis::ConnectionPoolOptions poolOpts;
  98. opts.host = _cc->redisConfig->hostname;
  99. opts.port = _cc->redisConfig->port;
  100. opts.password = _cc->redisConfig->password;
  101. opts.db = 0;
  102. opts.keep_alive = true;
  103. opts.connect_timeout = std::chrono::seconds(3);
  104. poolOpts.size = 25;
  105. poolOpts.wait_timeout = std::chrono::seconds(5);
  106. poolOpts.connection_lifetime = std::chrono::minutes(3);
  107. poolOpts.connection_idle_time = std::chrono::minutes(1);
  108. if (_cc->redisConfig->clusterMode) {
  109. innerspan->SetAttribute("cluster_mode", "true");
  110. fprintf(stderr, "Using Redis in Cluster Mode\n");
  111. _cluster = std::make_shared<sw::redis::RedisCluster>(opts, poolOpts);
  112. }
  113. else {
  114. innerspan->SetAttribute("cluster_mode", "false");
  115. fprintf(stderr, "Using Redis in Standalone Mode\n");
  116. _redis = std::make_shared<sw::redis::Redis>(opts, poolOpts);
  117. }
  118. }
  119. _readyLock.lock();
  120. fprintf(
  121. stderr, "[%s] NOTICE: %.10llx controller PostgreSQL waiting for initial data download..." ZT_EOL_S,
  122. ::_timestr(), (unsigned long long)_myAddress.toInt());
  123. _waitNoticePrinted = true;
  124. initializeNetworks();
  125. initializeMembers();
  126. _heartbeatThread = std::thread(&CentralDB::heartbeat, this);
  127. switch (listenMode) {
  128. case LISTENER_MODE_REDIS:
  129. if (_cc->redisConfig != NULL) {
  130. if (_cc->redisConfig->clusterMode) {
  131. _membersDbWatcher = std::make_shared<RedisMemberListener>(_myAddressStr, _cluster, this);
  132. _networksDbWatcher = std::make_shared<RedisNetworkListener>(_myAddressStr, _cluster, this);
  133. }
  134. else {
  135. _membersDbWatcher = std::make_shared<RedisMemberListener>(_myAddressStr, _redis, this);
  136. _networksDbWatcher = std::make_shared<RedisNetworkListener>(_myAddressStr, _redis, this);
  137. }
  138. }
  139. else {
  140. throw std::runtime_error("CentralDB: Redis listener mode selected but no Redis configuration provided");
  141. }
  142. case LISTENER_MODE_PUBSUB:
  143. if (cc->pubSubConfig != NULL) {
  144. _membersDbWatcher =
  145. std::make_shared<PubSubMemberListener>(_myAddressStr, cc->pubSubConfig->project_id, this);
  146. _networksDbWatcher =
  147. std::make_shared<PubSubNetworkListener>(_myAddressStr, cc->pubSubConfig->project_id, this);
  148. }
  149. else {
  150. throw std::runtime_error(
  151. "CentralDB: PubSub listener mode selected but no PubSub configuration provided");
  152. }
  153. break;
  154. case LISTENER_MODE_PGSQL:
  155. default:
  156. _membersDbWatcher = std::make_shared<PostgresMemberListener>(this, _pool, "member_" + _myAddressStr, 5);
  157. _networksDbWatcher = std::make_shared<PostgresNetworkListener>(this, _pool, "network_" + _myAddressStr, 5);
  158. break;
  159. }
  160. switch (statusMode) {
  161. case STATUS_WRITER_MODE_REDIS:
  162. if (_cc->redisConfig != NULL) {
  163. if (_cc->redisConfig->clusterMode) {
  164. _statusWriter = std::make_shared<RedisStatusWriter>(_cluster, _myAddressStr);
  165. }
  166. else {
  167. _statusWriter = std::make_shared<RedisStatusWriter>(_redis, _myAddressStr);
  168. }
  169. }
  170. else {
  171. throw std::runtime_error("CentralDB: Redis status mode selected but no Redis configuration provided");
  172. }
  173. break;
  174. case STATUS_WRITER_MODE_BIGTABLE:
  175. _statusWriter = std::make_shared<BigTableStatusWriter>(
  176. cc->bigTableConfig->project_id, cc->bigTableConfig->instance_id, cc->bigTableConfig->table_id);
  177. break;
  178. case STATUS_WRITER_MODE_PGSQL:
  179. default:
  180. _statusWriter = std::make_shared<PostgresStatusWriter>(_pool);
  181. break;
  182. }
  183. for (int i = 0; i < ZT_CENTRAL_CONTROLLER_COMMIT_THREADS; ++i) {
  184. _commitThread[i] = std::thread(&CentralDB::commitThread, this);
  185. }
  186. _onlineNotificationThread = std::thread(&CentralDB::onlineNotificationThread, this);
  187. configureSmee();
  188. }
  189. CentralDB::~CentralDB()
  190. {
  191. if (_smee != NULL) {
  192. rustybits::smee_client_delete(_smee);
  193. _smee = NULL;
  194. }
  195. rustybits::shutdown_async_runtime();
  196. _run = 0;
  197. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  198. _heartbeatThread.join();
  199. _commitQueue.stop();
  200. for (int i = 0; i < ZT_CENTRAL_CONTROLLER_COMMIT_THREADS; ++i) {
  201. _commitThread[i].join();
  202. }
  203. _onlineNotificationThread.join();
  204. }
  205. void CentralDB::configureSmee()
  206. {
  207. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  208. auto tracer = provider->GetTracer("CentralDB");
  209. auto span = tracer->StartSpan("CentralDB::configureSmee");
  210. auto scope = tracer->WithActiveSpan(span);
  211. const char* TEMPORAL_SCHEME = "ZT_TEMPORAL_SCHEME";
  212. const char* TEMPORAL_HOST = "ZT_TEMPORAL_HOST";
  213. const char* TEMPORAL_PORT = "ZT_TEMPORAL_PORT";
  214. const char* TEMPORAL_NAMESPACE = "ZT_TEMPORAL_NAMESPACE";
  215. const char* SMEE_TASK_QUEUE = "ZT_SMEE_TASK_QUEUE";
  216. const char* scheme = getenv(TEMPORAL_SCHEME);
  217. if (scheme == NULL) {
  218. scheme = "http";
  219. }
  220. const char* host = getenv(TEMPORAL_HOST);
  221. const char* port = getenv(TEMPORAL_PORT);
  222. const char* ns = getenv(TEMPORAL_NAMESPACE);
  223. const char* task_queue = getenv(SMEE_TASK_QUEUE);
  224. if (scheme != NULL && host != NULL && port != NULL && ns != NULL && task_queue != NULL) {
  225. fprintf(stderr, "creating smee client\n");
  226. std::string hostPort =
  227. std::string(scheme) + std::string("://") + std::string(host) + std::string(":") + std::string(port);
  228. this->_smee = rustybits::smee_client_new(hostPort.c_str(), ns, task_queue);
  229. }
  230. else {
  231. fprintf(stderr, "Smee client not configured\n");
  232. }
  233. }
  234. bool CentralDB::waitForReady()
  235. {
  236. while (_ready < 2) {
  237. _readyLock.lock();
  238. _readyLock.unlock();
  239. }
  240. return true;
  241. }
  242. bool CentralDB::isReady()
  243. {
  244. return ((_ready == 2) && (_connected));
  245. }
  246. bool CentralDB::save(nlohmann::json& record, bool notifyListeners)
  247. {
  248. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  249. auto tracer = provider->GetTracer("CentralDB");
  250. auto span = tracer->StartSpan("CentralDB::save");
  251. auto scope = tracer->WithActiveSpan(span);
  252. bool modified = false;
  253. try {
  254. if (! record.is_object()) {
  255. fprintf(stderr, "record is not an object?!?\n");
  256. return false;
  257. }
  258. const std::string objtype = record["objtype"];
  259. if (objtype == "network") {
  260. // fprintf(stderr, "network save\n");
  261. const uint64_t nwid = OSUtils::jsonIntHex(record["id"], 0ULL);
  262. if (nwid) {
  263. nlohmann::json old;
  264. get(nwid, old);
  265. if ((! old.is_object()) || (! _compareRecords(old, record))) {
  266. record["revision"] = OSUtils::jsonInt(record["revision"], 0ULL) + 1ULL;
  267. _commitQueue.post(std::pair<nlohmann::json, bool>(record, notifyListeners));
  268. modified = true;
  269. }
  270. }
  271. }
  272. else if (objtype == "member") {
  273. std::string networkId = record["nwid"];
  274. std::string memberId = record["id"];
  275. const uint64_t nwid = OSUtils::jsonIntHex(record["nwid"], 0ULL);
  276. const uint64_t id = OSUtils::jsonIntHex(record["id"], 0ULL);
  277. // fprintf(stderr, "member save %s-%s\n", networkId.c_str(), memberId.c_str());
  278. if ((id) && (nwid)) {
  279. nlohmann::json network, old;
  280. get(nwid, network, id, old);
  281. if ((! old.is_object()) || (! _compareRecords(old, record))) {
  282. // fprintf(stderr, "commit queue post\n");
  283. record["revision"] = OSUtils::jsonInt(record["revision"], 0ULL) + 1ULL;
  284. _commitQueue.post(std::pair<nlohmann::json, bool>(record, notifyListeners));
  285. modified = true;
  286. }
  287. else {
  288. // fprintf(stderr, "no change\n");
  289. }
  290. }
  291. }
  292. else {
  293. fprintf(stderr, "uhh waaat\n");
  294. }
  295. }
  296. catch (std::exception& e) {
  297. fprintf(stderr, "Error on PostgreSQL::save: %s\n", e.what());
  298. }
  299. catch (...) {
  300. fprintf(stderr, "Unknown error on PostgreSQL::save\n");
  301. }
  302. return modified;
  303. }
  304. void CentralDB::eraseNetwork(const uint64_t networkId)
  305. {
  306. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  307. auto tracer = provider->GetTracer("CentralDB");
  308. auto span = tracer->StartSpan("CentralDB::eraseNetwork");
  309. auto scope = tracer->WithActiveSpan(span);
  310. char networkIdStr[17];
  311. span->SetAttribute("network_id", Utils::hex(networkId, networkIdStr));
  312. fprintf(stderr, "PostgreSQL::eraseNetwork\n");
  313. char tmp2[24];
  314. waitForReady();
  315. Utils::hex(networkId, tmp2);
  316. std::pair<nlohmann::json, bool> tmp;
  317. tmp.first["id"] = tmp2;
  318. tmp.first["objtype"] = "_delete_network";
  319. tmp.second = true;
  320. _commitQueue.post(tmp);
  321. nlohmann::json nullJson;
  322. _networkChanged(tmp.first, nullJson, true);
  323. }
  324. void CentralDB::eraseMember(const uint64_t networkId, const uint64_t memberId)
  325. {
  326. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  327. auto tracer = provider->GetTracer("CentralDB");
  328. auto span = tracer->StartSpan("CentralDB::eraseMember");
  329. auto scope = tracer->WithActiveSpan(span);
  330. char networkIdStr[17];
  331. char memberIdStr[11];
  332. span->SetAttribute("network_id", Utils::hex(networkId, networkIdStr));
  333. span->SetAttribute("member_id", Utils::hex10(memberId, memberIdStr));
  334. fprintf(stderr, "PostgreSQL::eraseMember\n");
  335. char tmp2[24];
  336. waitForReady();
  337. std::pair<nlohmann::json, bool> tmp, nw;
  338. Utils::hex(networkId, tmp2);
  339. tmp.first["nwid"] = tmp2;
  340. Utils::hex(memberId, tmp2);
  341. tmp.first["id"] = tmp2;
  342. tmp.first["objtype"] = "_delete_member";
  343. tmp.second = true;
  344. _commitQueue.post(tmp);
  345. nlohmann::json nullJson;
  346. _memberChanged(tmp.first, nullJson, true);
  347. }
  348. void CentralDB::nodeIsOnline(
  349. const uint64_t networkId,
  350. const uint64_t memberId,
  351. const InetAddress& physicalAddress,
  352. const char* osArch)
  353. {
  354. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  355. auto tracer = provider->GetTracer("CentralDB");
  356. auto span = tracer->StartSpan("CentralDB::nodeIsOnline");
  357. auto scope = tracer->WithActiveSpan(span);
  358. char networkIdStr[17];
  359. char memberIdStr[11];
  360. char ipStr[INET6_ADDRSTRLEN];
  361. span->SetAttribute("network_id", Utils::hex(networkId, networkIdStr));
  362. span->SetAttribute("member_id", Utils::hex10(memberId, memberIdStr));
  363. span->SetAttribute("physical_address", physicalAddress.toString(ipStr));
  364. span->SetAttribute("os_arch", osArch);
  365. std::lock_guard<std::mutex> l(_lastOnline_l);
  366. NodeOnlineRecord& i = _lastOnline[std::pair<uint64_t, uint64_t>(networkId, memberId)];
  367. i.lastSeen = OSUtils::now();
  368. if (physicalAddress) {
  369. i.physicalAddress = physicalAddress;
  370. }
  371. i.osArch = std::string(osArch);
  372. }
  373. void CentralDB::nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress& physicalAddress)
  374. {
  375. this->nodeIsOnline(networkId, memberId, physicalAddress, "unknown/unknown");
  376. }
  377. AuthInfo CentralDB::getSSOAuthInfo(const nlohmann::json& member, const std::string& redirectURL)
  378. {
  379. if (_cc->ssoEnabled) {
  380. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  381. auto tracer = provider->GetTracer("CentralDB");
  382. auto span = tracer->StartSpan("CentralDB::getSSOAuthInfo");
  383. auto scope = tracer->WithActiveSpan(span);
  384. Metrics::db_get_sso_info++;
  385. // NONCE is just a random character string. no semantic meaning
  386. // state = HMAC SHA384 of Nonce based on shared sso key
  387. //
  388. // need nonce timeout in database? make sure it's used within X time
  389. // X is 5 minutes for now. Make configurable later?
  390. //
  391. // how do we tell when a nonce is used? if auth_expiration_time is set
  392. std::string networkId = member["nwid"];
  393. std::string memberId = member["id"];
  394. char authenticationURL[4096] = { 0 };
  395. AuthInfo info;
  396. info.enabled = true;
  397. // if (memberId == "a10dccea52" && networkId == "8056c2e21c24673d") {
  398. // fprintf(stderr, "invalid authinfo for grant's machine\n");
  399. // info.version=1;
  400. // return info;
  401. // }
  402. // fprintf(stderr, "PostgreSQL::updateMemberOnLoad: %s-%s\n", networkId.c_str(), memberId.c_str());
  403. std::shared_ptr<PostgresConnection> c;
  404. try {
  405. c = _pool->borrow();
  406. pqxx::work w(*c->c);
  407. char nonceBytes[16] = { 0 };
  408. std::string nonce = "";
  409. // check if the member exists first.
  410. pqxx::row count = w.exec_params1(
  411. "SELECT count(id) FROM ztc_member WHERE id = $1 AND network_id = $2 AND deleted = false", memberId,
  412. networkId);
  413. if (count[0].as<int>() == 1) {
  414. // get active nonce, if exists.
  415. pqxx::result r = w.exec_params(
  416. "SELECT nonce FROM ztc_sso_expiry "
  417. "WHERE network_id = $1 AND member_id = $2 "
  418. "AND ((NOW() AT TIME ZONE 'UTC') <= authentication_expiry_time) AND ((NOW() AT TIME ZONE 'UTC') <= "
  419. "nonce_expiration)",
  420. networkId, memberId);
  421. if (r.size() == 0) {
  422. // no active nonce.
  423. // find an unused nonce, if one exists.
  424. pqxx::result r = w.exec_params(
  425. "SELECT nonce FROM ztc_sso_expiry "
  426. "WHERE network_id = $1 AND member_id = $2 "
  427. "AND authentication_expiry_time IS NULL AND ((NOW() AT TIME ZONE 'UTC') <= nonce_expiration)",
  428. networkId, memberId);
  429. if (r.size() == 1) {
  430. // we have an existing nonce. Use it
  431. nonce = r.at(0)[0].as<std::string>();
  432. Utils::unhex(nonce.c_str(), nonceBytes, sizeof(nonceBytes));
  433. }
  434. else if (r.empty()) {
  435. // create a nonce
  436. Utils::getSecureRandom(nonceBytes, 16);
  437. char nonceBuf[64] = { 0 };
  438. Utils::hex(nonceBytes, sizeof(nonceBytes), nonceBuf);
  439. nonce = std::string(nonceBuf);
  440. pqxx::result ir = w.exec_params0(
  441. "INSERT INTO ztc_sso_expiry "
  442. "(nonce, nonce_expiration, network_id, member_id) VALUES "
  443. "($1, TO_TIMESTAMP($2::double precision/1000), $3, $4)",
  444. nonce, OSUtils::now() + 300000, networkId, memberId);
  445. w.commit();
  446. }
  447. else {
  448. // > 1 ?!? Thats an error!
  449. fprintf(stderr, "> 1 unused nonce!\n");
  450. exit(6);
  451. }
  452. }
  453. else if (r.size() == 1) {
  454. nonce = r.at(0)[0].as<std::string>();
  455. Utils::unhex(nonce.c_str(), nonceBytes, sizeof(nonceBytes));
  456. }
  457. else {
  458. // more than 1 nonce in use? Uhhh...
  459. fprintf(stderr, "> 1 nonce in use for network member?!?\n");
  460. exit(7);
  461. }
  462. r = w.exec_params(
  463. "SELECT oc.client_id, oc.authorization_endpoint, oc.issuer, oc.provider, oc.sso_impl_version "
  464. "FROM ztc_network AS n "
  465. "INNER JOIN ztc_org o "
  466. " ON o.owner_id = n.owner_id "
  467. "LEFT OUTER JOIN ztc_network_oidc_config noc "
  468. " ON noc.network_id = n.id "
  469. "LEFT OUTER JOIN ztc_oidc_config oc "
  470. " ON noc.client_id = oc.client_id AND oc.org_id = o.org_id "
  471. "WHERE n.id = $1 AND n.sso_enabled = true",
  472. networkId);
  473. std::string client_id = "";
  474. std::string authorization_endpoint = "";
  475. std::string issuer = "";
  476. std::string provider = "";
  477. uint64_t sso_version = 0;
  478. if (r.size() == 1) {
  479. client_id = r.at(0)[0].as<std::optional<std::string> >().value_or("");
  480. authorization_endpoint = r.at(0)[1].as<std::optional<std::string> >().value_or("");
  481. issuer = r.at(0)[2].as<std::optional<std::string> >().value_or("");
  482. provider = r.at(0)[3].as<std::optional<std::string> >().value_or("");
  483. sso_version = r.at(0)[4].as<std::optional<uint64_t> >().value_or(1);
  484. }
  485. else if (r.size() > 1) {
  486. fprintf(
  487. stderr, "ERROR: More than one auth endpoint for an organization?!?!? NetworkID: %s\n",
  488. networkId.c_str());
  489. }
  490. else {
  491. fprintf(stderr, "No client or auth endpoint?!?\n");
  492. }
  493. info.version = sso_version;
  494. // no catch all else because we don't actually care if no records exist here. just continue as normal.
  495. if ((! client_id.empty()) && (! authorization_endpoint.empty())) {
  496. uint8_t state[48];
  497. HMACSHA384(_ssoPsk, nonceBytes, sizeof(nonceBytes), state);
  498. char state_hex[256];
  499. Utils::hex(state, 48, state_hex);
  500. if (info.version == 0) {
  501. char url[2048] = { 0 };
  502. OSUtils::ztsnprintf(
  503. url, sizeof(authenticationURL),
  504. "%s?response_type=id_token&response_mode=form_post&scope=openid+email+profile&redirect_uri="
  505. "%s&nonce=%s&state=%s&client_id=%s",
  506. authorization_endpoint.c_str(), url_encode(redirectURL).c_str(), nonce.c_str(), state_hex,
  507. client_id.c_str());
  508. info.authenticationURL = std::string(url);
  509. }
  510. else if (info.version == 1) {
  511. info.ssoClientID = client_id;
  512. info.issuerURL = issuer;
  513. info.ssoProvider = provider;
  514. info.ssoNonce = nonce;
  515. info.ssoState = std::string(state_hex) + "_" + networkId;
  516. info.centralAuthURL = redirectURL;
  517. #ifdef ZT_DEBUG
  518. fprintf(
  519. stderr,
  520. "ssoClientID: %s\nissuerURL: %s\nssoNonce: %s\nssoState: %s\ncentralAuthURL: %s\nprovider: "
  521. "%s\n",
  522. info.ssoClientID.c_str(), info.issuerURL.c_str(), info.ssoNonce.c_str(),
  523. info.ssoState.c_str(), info.centralAuthURL.c_str(), provider.c_str());
  524. #endif
  525. }
  526. }
  527. else {
  528. fprintf(
  529. stderr, "client_id: %s\nauthorization_endpoint: %s\n", client_id.c_str(),
  530. authorization_endpoint.c_str());
  531. }
  532. }
  533. _pool->unborrow(c);
  534. }
  535. catch (std::exception& e) {
  536. span->SetStatus(opentelemetry::trace::StatusCode::kError, e.what());
  537. fprintf(stderr, "ERROR: Error updating member on load for network %s: %s\n", networkId.c_str(), e.what());
  538. }
  539. return info; // std::string(authenticationURL);
  540. }
  541. return AuthInfo();
  542. }
  543. void CentralDB::initializeNetworks()
  544. {
  545. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  546. auto tracer = provider->GetTracer("CentralDB");
  547. auto span = tracer->StartSpan("CentralDB::initializeNetworks");
  548. auto scope = tracer->WithActiveSpan(span);
  549. fprintf(stderr, "Initializing networks...\n");
  550. try {
  551. char qbuf[2048];
  552. sprintf(
  553. qbuf,
  554. "SELECT id, name, configuration , (EXTRACT(EPOCH FROM creation_time AT TIME ZONE 'UTC')*1000)::bigint, "
  555. "(EXTRACT(EPOCH FROM last_modified AT TIME ZONE 'UTC')*1000)::bigint, revision, frontend "
  556. "FROM networks_ctl WHERE controller_id = '%s'",
  557. _myAddressStr.c_str());
  558. auto c = _pool->borrow();
  559. pqxx::work w(*c->c);
  560. fprintf(stderr, "Load networks from psql...\n");
  561. auto stream = pqxx::stream_from::query(w, qbuf);
  562. std::tuple<
  563. std::string // network ID
  564. ,
  565. std::optional<std::string> // name
  566. ,
  567. std::string // configuration
  568. ,
  569. std::optional<uint64_t> // creation_time
  570. ,
  571. std::optional<uint64_t> // last_modified
  572. ,
  573. std::optional<uint64_t> // revision
  574. ,
  575. std::string // frontend
  576. >
  577. row;
  578. uint64_t count = 0;
  579. uint64_t total = 0;
  580. while (stream >> row) {
  581. auto start = std::chrono::high_resolution_clock::now();
  582. json empty;
  583. json config;
  584. initNetwork(config);
  585. std::string nwid = std::get<0>(row);
  586. std::string name = std::get<1>(row).value_or("");
  587. json cfgtmp = json::parse(std::get<2>(row));
  588. std::optional<uint64_t> created_at = std::get<3>(row);
  589. std::optional<uint64_t> last_modified = std::get<4>(row);
  590. std::optional<uint64_t> revision = std::get<5>(row);
  591. config["id"] = nwid;
  592. config["name"] = name;
  593. config["creationTime"] = created_at.value_or(0);
  594. config["lastModified"] = last_modified.value_or(0);
  595. config["revision"] = revision.value_or(0);
  596. config["capabilities"] = cfgtmp["capabilities"].is_array() ? cfgtmp["capabilities"] : json::array();
  597. config["enableBroadcast"] =
  598. cfgtmp["enableBroadcast"].is_boolean() ? cfgtmp["enableBroadcast"].get<bool>() : false;
  599. config["mtu"] = cfgtmp["mtu"].is_number() ? cfgtmp["mtu"].get<int32_t>() : 2800;
  600. config["multicastLimit"] =
  601. cfgtmp["multicastLimit"].is_number() ? cfgtmp["multicastLimit"].get<int32_t>() : 64;
  602. config["private"] = cfgtmp["private"].is_boolean() ? cfgtmp["private"].get<bool>() : true;
  603. config["remoteTraceLevel"] =
  604. cfgtmp["remoteTraceLevel"].is_number() ? cfgtmp["remoteTraceLevel"].get<int32_t>() : 0;
  605. config["remoteTraceTarget"] =
  606. cfgtmp["remoteTraceTarget"].is_string() ? cfgtmp["remoteTraceTarget"].get<std::string>() : "";
  607. config["revision"] = revision.value_or(0);
  608. config["rules"] = cfgtmp["rules"].is_array() ? cfgtmp["rules"] : json::array();
  609. config["tags"] = cfgtmp["tags"].is_array() ? cfgtmp["tags"] : json::array();
  610. if (cfgtmp["v4AssignMode"].is_object()) {
  611. config["v4AssignMode"] = cfgtmp["v4AssignMode"];
  612. }
  613. else {
  614. config["v4AssignMode"] = json::object();
  615. config["v4AssignMode"]["zt"] = true;
  616. }
  617. if (cfgtmp["v6AssignMode"].is_object()) {
  618. config["v6AssignMode"] = cfgtmp["v6AssignMode"];
  619. }
  620. else {
  621. config["v6AssignMode"] = json::object();
  622. config["v6AssignMode"]["zt"] = true;
  623. config["v6AssignMode"]["6plane"] = true;
  624. config["v6AssignMode"]["rfc4193"] = false;
  625. }
  626. config["ssoEnabled"] = cfgtmp["ssoEnabled"].is_boolean() ? cfgtmp["ssoEnabled"].get<bool>() : false;
  627. config["objtype"] = "network";
  628. config["routes"] = cfgtmp["routes"].is_array() ? cfgtmp["routes"] : json::array();
  629. config["clientId"] = cfgtmp["clientId"].is_string() ? cfgtmp["clientId"].get<std::string>() : "";
  630. config["authorizationEndpoint"] = cfgtmp["authorizationEndpoint"].is_string()
  631. ? cfgtmp["authorizationEndpoint"].get<std::string>()
  632. : nullptr;
  633. config["provider"] = cfgtmp["ssoProvider"].is_string() ? cfgtmp["ssoProvider"].get<std::string>() : "";
  634. if (! cfgtmp["dns"].is_object()) {
  635. cfgtmp["dns"] = json::object();
  636. cfgtmp["dns"]["domain"] = "";
  637. cfgtmp["dns"]["servers"] = json::array();
  638. }
  639. else {
  640. config["dns"] = cfgtmp["dns"];
  641. }
  642. config["ipAssignmentPools"] =
  643. cfgtmp["ipAssignmentPools"].is_array() ? cfgtmp["ipAssignmentPools"] : json::array();
  644. config["frontend"] = std::get<6>(row);
  645. Metrics::network_count++;
  646. _networkChanged(empty, config, false);
  647. auto end = std::chrono::high_resolution_clock::now();
  648. auto dur = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
  649. ;
  650. total += dur.count();
  651. ++count;
  652. if (count > 0 && count % 10000 == 0) {
  653. fprintf(stderr, "Averaging %lu us per network\n", (total / count));
  654. }
  655. }
  656. w.commit();
  657. _pool->unborrow(c);
  658. fprintf(stderr, "done.\n");
  659. if (++this->_ready == 2) {
  660. if (_waitNoticePrinted) {
  661. fprintf(
  662. stderr, "[%s] NOTICE: %.10llx controller PostgreSQL data download complete." ZT_EOL_S, _timestr(),
  663. (unsigned long long)_myAddress.toInt());
  664. }
  665. _readyLock.unlock();
  666. }
  667. fprintf(stderr, "network init done\n");
  668. }
  669. catch (std::exception& e) {
  670. fprintf(stderr, "ERROR: Error initializing networks: %s\n", e.what());
  671. span->SetStatus(opentelemetry::trace::StatusCode::kError, e.what());
  672. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  673. exit(-1);
  674. }
  675. }
  676. void CentralDB::initializeMembers()
  677. {
  678. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  679. auto tracer = provider->GetTracer("CentralDB");
  680. auto span = tracer->StartSpan("CentralDB::initializeMembers");
  681. auto scope = tracer->WithActiveSpan(span);
  682. std::string memberId;
  683. std::string networkId;
  684. try {
  685. std::unordered_map<std::string, std::string> networkMembers;
  686. fprintf(stderr, "Initializing Members...\n");
  687. std::string setKeyBase = "network-nodes-all:{" + _myAddressStr + "}:";
  688. if (_redisMemberStatus) {
  689. fprintf(stderr, "Initialize Redis for members...\n");
  690. std::unique_lock<std::shared_mutex> l(_networks_l);
  691. std::unordered_set<std::string> deletes;
  692. for (auto it : _networks) {
  693. uint64_t nwid_i = it.first;
  694. char nwidTmp[64] = { 0 };
  695. OSUtils::ztsnprintf(nwidTmp, sizeof(nwidTmp), "%.16llx", nwid_i);
  696. std::string nwid(nwidTmp);
  697. std::string key = setKeyBase + nwid;
  698. deletes.insert(key);
  699. }
  700. if (! deletes.empty()) {
  701. try {
  702. if (_cc->redisConfig->clusterMode) {
  703. auto tx = _cluster->transaction(_myAddressStr, true, false);
  704. for (std::string k : deletes) {
  705. tx.del(k);
  706. }
  707. tx.exec();
  708. }
  709. else {
  710. auto tx = _redis->transaction(true, false);
  711. for (std::string k : deletes) {
  712. tx.del(k);
  713. }
  714. tx.exec();
  715. }
  716. }
  717. catch (sw::redis::Error& e) {
  718. // ignore
  719. }
  720. }
  721. }
  722. char qbuf[2048];
  723. sprintf(
  724. qbuf,
  725. "SELECT nm.device_id, nm.network_id, nm.authorized, nm.active_bridge, nm.ip_assignments, "
  726. "nm.no_auto_assign_ips, "
  727. "nm.sso_exempt, (EXTRACT(EPOCH FROM nm.authentication_expiry_time AT TIME ZONE 'UTC')*1000)::bigint, "
  728. "(EXTRACT(EPOCH FROM nm.creation_time AT TIME ZONE 'UTC')*1000)::bigint, nm.identity, "
  729. "(EXTRACT(EPOCH FROM nm.last_authorized_time AT TIME ZONE 'UTC')*1000)::bigint, "
  730. "(EXTRACT(EPOCH FROM nm.last_deauthorized_time AT TIME ZONE 'UTC')*1000)::bigint, "
  731. "nm.remote_trace_level, nm.remote_trace_target, nm.revision, nm.capabilities, nm.tags "
  732. "nm.frontend "
  733. "FROM network_memberships_ctl nm "
  734. "INNER JOIN networks_ctl n "
  735. " ON nm.network_id = n.id "
  736. "WHERE n.controller_id = '%s'",
  737. _myAddressStr.c_str());
  738. auto c = _pool->borrow();
  739. pqxx::work w(*c->c);
  740. fprintf(stderr, "Load members from psql...\n");
  741. auto stream = pqxx::stream_from::query(w, qbuf);
  742. std::tuple<
  743. std::string // device ID
  744. ,
  745. std::string // network ID
  746. ,
  747. bool // authorized
  748. ,
  749. std::optional<bool> // active_bridge
  750. ,
  751. std::optional<std::string> // ip_assignments
  752. ,
  753. std::optional<bool> // no_auto_assign_ips
  754. ,
  755. std::optional<bool> // sso_exempt
  756. ,
  757. std::optional<uint64_t> // authentication_expiry_time
  758. ,
  759. std::optional<uint64_t> // creation_time
  760. ,
  761. std::optional<std::string> // identity
  762. ,
  763. std::optional<uint64_t> // last_authorized_time
  764. ,
  765. std::optional<uint64_t> // last_deauthorized_time
  766. ,
  767. std::optional<int32_t> // remote_trace_level
  768. ,
  769. std::optional<std::string> // remote_trace_target
  770. ,
  771. std::optional<uint64_t> // revision
  772. ,
  773. std::optional<std::string> // capabilities
  774. ,
  775. std::optional<std::string> // tags
  776. ,
  777. std::string // frontend
  778. >
  779. row;
  780. auto tmp = std::chrono::high_resolution_clock::now();
  781. uint64_t count = 0;
  782. uint64_t total = 0;
  783. while (stream >> row) {
  784. auto start = std::chrono::high_resolution_clock::now();
  785. json empty;
  786. json config;
  787. initMember(config);
  788. memberId = std::get<0>(row);
  789. networkId = std::get<1>(row);
  790. bool authorized = std::get<2>(row);
  791. std::optional<bool> active_bridge = std::get<3>(row);
  792. std::string ip_assignments = std::get<4>(row).value_or("");
  793. std::optional<bool> no_auto_assign_ips = std::get<5>(row);
  794. std::optional<bool> sso_exempt = std::get<6>(row);
  795. std::optional<uint64_t> authentication_expiry_time = std::get<7>(row);
  796. std::optional<uint64_t> creation_time = std::get<8>(row);
  797. std::optional<std::string> identity = std::get<9>(row);
  798. std::optional<uint64_t> last_authorized_time = std::get<10>(row);
  799. std::optional<uint64_t> last_deauthorized_time = std::get<11>(row);
  800. std::optional<int32_t> remote_trace_level = std::get<12>(row);
  801. std::optional<std::string> remote_trace_target = std::get<13>(row);
  802. std::optional<uint64_t> revision = std::get<14>(row);
  803. std::optional<std::string> capabilities = std::get<15>(row);
  804. std::optional<std::string> tags = std::get<16>(row);
  805. networkMembers.insert(std::pair<std::string, std::string>(setKeyBase + networkId, memberId));
  806. config["objtype"] = "member";
  807. config["id"] = memberId;
  808. config["address"] = identity.value_or("");
  809. config["nwid"] = networkId;
  810. config["authorized"] = authorized;
  811. config["activeBridge"] = active_bridge.value_or(false);
  812. config["ipAssignments"] = json::array();
  813. if (ip_assignments != "{}") {
  814. std::string tmp = ip_assignments.substr(1, ip_assignments.length() - 2);
  815. std::vector<std::string> addrs = split(tmp, ',');
  816. for (auto it = addrs.begin(); it != addrs.end(); ++it) {
  817. config["ipAssignments"].push_back(*it);
  818. }
  819. }
  820. config["capabilities"] = json::parse(capabilities.value_or("[]"));
  821. config["creationTime"] = creation_time.value_or(0);
  822. config["lastAuthorizedTime"] = last_authorized_time.value_or(0);
  823. config["lastDeauthorizedTime"] = last_deauthorized_time.value_or(0);
  824. config["noAutoAssignIPs"] = no_auto_assign_ips.value_or(false);
  825. config["remoteTraceLevel"] = remote_trace_level.value_or(0);
  826. config["remoteTraceTarget"] = remote_trace_target.value_or(nullptr);
  827. config["revision"] = revision.value_or(0);
  828. config["ssoExempt"] = sso_exempt.value_or(false);
  829. config["authenticationExpiryTime"] = authentication_expiry_time.value_or(0);
  830. config["tags"] = json::parse(tags.value_or("[]"));
  831. config["ipAssignments"] = json::array();
  832. config["frontend"] = std::get<17>(row);
  833. Metrics::member_count++;
  834. _memberChanged(empty, config, false);
  835. memberId = "";
  836. networkId = "";
  837. auto end = std::chrono::high_resolution_clock::now();
  838. auto dur = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
  839. total += dur.count();
  840. ++count;
  841. if (count > 0 && count % 10000 == 0) {
  842. fprintf(stderr, "Averaging %llu us per member\n", (total / count));
  843. }
  844. }
  845. if (count > 0) {
  846. fprintf(stderr, "Took %llu us per member to load\n", (total / count));
  847. }
  848. stream.complete();
  849. w.commit();
  850. _pool->unborrow(c);
  851. fprintf(stderr, "done.\n");
  852. if (_listenerMode == LISTENER_MODE_REDIS)
  853. if (! networkMembers.empty()) {
  854. if (_redisMemberStatus) {
  855. fprintf(stderr, "Load member data into redis...\n");
  856. if (_cc->redisConfig->clusterMode) {
  857. auto tx = _cluster->transaction(_myAddressStr, true, false);
  858. uint64_t count = 0;
  859. for (auto it : networkMembers) {
  860. tx.sadd(it.first, it.second);
  861. if (++count % 30000 == 0) {
  862. tx.exec();
  863. tx = _cluster->transaction(_myAddressStr, true, false);
  864. }
  865. }
  866. tx.exec();
  867. }
  868. else {
  869. auto tx = _redis->transaction(true, false);
  870. uint64_t count = 0;
  871. for (auto it : networkMembers) {
  872. tx.sadd(it.first, it.second);
  873. if (++count % 30000 == 0) {
  874. tx.exec();
  875. tx = _redis->transaction(true, false);
  876. }
  877. }
  878. tx.exec();
  879. }
  880. fprintf(stderr, "done.\n");
  881. }
  882. }
  883. fprintf(stderr, "Done loading members...\n");
  884. if (++this->_ready == 2) {
  885. if (_waitNoticePrinted) {
  886. fprintf(
  887. stderr, "[%s] NOTICE: %.10llx controller PostgreSQL data download complete." ZT_EOL_S, _timestr(),
  888. (unsigned long long)_myAddress.toInt());
  889. }
  890. _readyLock.unlock();
  891. }
  892. }
  893. catch (sw::redis::Error& e) {
  894. span->SetStatus(opentelemetry::trace::StatusCode::kError, e.what());
  895. fprintf(stderr, "ERROR: Error initializing members (redis): %s\n", e.what());
  896. exit(-1);
  897. }
  898. catch (std::exception& e) {
  899. span->SetStatus(opentelemetry::trace::StatusCode::kError, e.what());
  900. fprintf(stderr, "ERROR: Error initializing member: %s-%s %s\n", networkId.c_str(), memberId.c_str(), e.what());
  901. exit(-1);
  902. }
  903. }
  904. void CentralDB::heartbeat()
  905. {
  906. char publicId[1024];
  907. char hostnameTmp[1024];
  908. _myId.toString(false, publicId);
  909. if (gethostname(hostnameTmp, sizeof(hostnameTmp)) != 0) {
  910. hostnameTmp[0] = (char)0;
  911. }
  912. else {
  913. for (int i = 0; i < (int)sizeof(hostnameTmp); ++i) {
  914. if ((hostnameTmp[i] == '.') || (hostnameTmp[i] == 0)) {
  915. hostnameTmp[i] = (char)0;
  916. break;
  917. }
  918. }
  919. }
  920. const char* controllerId = _myAddressStr.c_str();
  921. const char* publicIdentity = publicId;
  922. const char* hostname = hostnameTmp;
  923. while (_run == 1) {
  924. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  925. auto tracer = provider->GetTracer("CentralDB");
  926. auto span = tracer->StartSpan("CentralDB::heartbeat");
  927. auto scope = tracer->WithActiveSpan(span);
  928. // fprintf(stderr, "%s: heartbeat\n", controllerId);
  929. auto c = _pool->borrow();
  930. int64_t ts = OSUtils::now();
  931. if (c->c) {
  932. std::string major = std::to_string(ZEROTIER_ONE_VERSION_MAJOR);
  933. std::string minor = std::to_string(ZEROTIER_ONE_VERSION_MINOR);
  934. std::string rev = std::to_string(ZEROTIER_ONE_VERSION_REVISION);
  935. std::string version = major + "." + minor + "." + rev;
  936. std::string versionStr = "v" + version;
  937. try {
  938. pqxx::work w { *c->c };
  939. w.exec_params0(
  940. "INSERT INTO controllers_ctl (id, hostname, last_heartbeat, public_identity, version) VALUES "
  941. "($1, $2, TO_TIMESTAMP($3::double precision/1000), $4, $5) "
  942. "ON CONFLICT (id) DO UPDATE SET hostname = EXCLUDED.hostname, last_heartbeat = "
  943. "EXCLUDED.last_heartbeat, "
  944. "public_identity = EXCLUDED.public_identity, version = EXCLUDED.version",
  945. controllerId, hostname, ts, publicIdentity, versionStr);
  946. w.commit();
  947. }
  948. catch (std::exception& e) {
  949. fprintf(stderr, "%s: Heartbeat update failed: %s\n", controllerId, e.what());
  950. span->End();
  951. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  952. continue;
  953. }
  954. }
  955. _pool->unborrow(c);
  956. try {
  957. if (_listenerMode == LISTENER_MODE_REDIS && _redisMemberStatus) {
  958. if (_cc->redisConfig->clusterMode) {
  959. _cluster->zadd("controllers", "controllerId", ts);
  960. }
  961. else {
  962. _redis->zadd("controllers", "controllerId", ts);
  963. }
  964. }
  965. }
  966. catch (sw::redis::Error& e) {
  967. fprintf(stderr, "ERROR: Redis error in heartbeat thread: %s\n", e.what());
  968. }
  969. span->End();
  970. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  971. }
  972. fprintf(stderr, "Exited heartbeat thread\n");
  973. }
  974. void CentralDB::commitThread()
  975. {
  976. fprintf(stderr, "%s: commitThread start\n", _myAddressStr.c_str());
  977. std::pair<nlohmann::json, bool> qitem;
  978. while (_commitQueue.get(qitem) & (_run == 1)) {
  979. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  980. auto tracer = provider->GetTracer("CentralDB");
  981. auto span = tracer->StartSpan("CentralDB::commitThread");
  982. auto scope = tracer->WithActiveSpan(span);
  983. // fprintf(stderr, "commitThread tick\n");
  984. if (! qitem.first.is_object()) {
  985. fprintf(stderr, "not an object\n");
  986. continue;
  987. }
  988. std::shared_ptr<PostgresConnection> c;
  989. try {
  990. c = _pool->borrow();
  991. }
  992. catch (std::exception& e) {
  993. fprintf(stderr, "ERROR: %s\n", e.what());
  994. continue;
  995. }
  996. if (! c) {
  997. fprintf(stderr, "Error getting database connection\n");
  998. continue;
  999. }
  1000. Metrics::pgsql_commit_ticks++;
  1001. try {
  1002. nlohmann::json& config = (qitem.first);
  1003. const std::string objtype = config["objtype"];
  1004. if (objtype == "member") {
  1005. auto mspan = tracer->StartSpan("CentralDB::commitThread::member");
  1006. auto mscope = tracer->WithActiveSpan(mspan);
  1007. // fprintf(stderr, "%s: commitThread: member\n", _myAddressStr.c_str());
  1008. std::string memberId;
  1009. std::string networkId;
  1010. try {
  1011. pqxx::work w(*c->c);
  1012. memberId = config["id"];
  1013. networkId = config["nwid"];
  1014. std::string target = "NULL";
  1015. if (! config["remoteTraceTarget"].is_null()) {
  1016. target = config["remoteTraceTarget"];
  1017. }
  1018. pqxx::row nwrow = w.exec_params1("SELECT COUNT(id) FROM networks_ctl WHERE id = $1", networkId);
  1019. int nwcount = nwrow[0].as<int>();
  1020. if (nwcount != 1) {
  1021. fprintf(stderr, "network %s does not exist. skipping member upsert\n", networkId.c_str());
  1022. w.abort();
  1023. _pool->unborrow(c);
  1024. continue;
  1025. }
  1026. pqxx::row mrow = w.exec_params1(
  1027. "SELECT COUNT(device_id) FROM network_memberships_ctl WHERE device_id = $1 AND network_id = $2",
  1028. memberId, networkId);
  1029. int membercount = mrow[0].as<int>();
  1030. bool isNewMember = (membercount == 0);
  1031. pqxx::result res = w.exec_params0(
  1032. "INSERT INTO network_memberships_ctl (device_id, network_id, authorized, active_bridge, "
  1033. "ip_assignments, "
  1034. "no_auto_assign_ips, sso_exempt, authentication_expiry_time, capabilities, creation_time, "
  1035. "identity, last_authorized_time, last_deauthorized_time, "
  1036. "remote_trace_level, remote_trace_target, revision, tags, version_major, version_minor, "
  1037. "version_revision, version_protocol) "
  1038. "VALUES ($1, $2, $3, $4, $5, $6, $7, TO_TIMESTAMP($8::double precision/1000), $9, "
  1039. "TO_TIMESTAMP($10::double precision/1000), $11, TO_TIMESTAMP($12::double precision/1000), "
  1040. "TO_TIMESTAMP($13::double precision/1000), $14, $15, $16, $17, $18, $19, $20, $21) "
  1041. "ON CONFLICT (device_id, network_id) DO UPDATE SET "
  1042. "authorized = EXCLUDED.authorized, active_bridge = EXCLUDED.active_bridge, "
  1043. "ip_assignments = EXCLUDED.ip_assignments, no_auto_assign_ips = EXCLUDED.no_auto_assign_ips, "
  1044. "sso_exempt = EXCLUDED.sso_exempt, authentication_expiry_time = "
  1045. "EXCLUDED.authentication_expiry_time, "
  1046. "capabilities = EXCLUDED.capabilities, creation_time = EXCLUDED.creation_time, "
  1047. "identity = EXCLUDED.identity, last_authorized_time = EXCLUDED.last_authorized_time, "
  1048. "last_deauthorized_time = EXCLUDED.last_deauthorized_time, "
  1049. "remote_trace_level = EXCLUDED.remote_trace_level, remote_trace_target = "
  1050. "EXCLUDED.remote_trace_target, "
  1051. "revision = EXCLUDED.revision, tags = EXCLUDED.tags, version_major = EXCLUDED.version_major, "
  1052. "version_minor = EXCLUDED.version_minor, version_revision = EXCLUDED.version_revision, "
  1053. "version_protocol = EXCLUDED.version_protocol",
  1054. memberId, networkId, (bool)config["authorized"], (bool)config["activeBridge"],
  1055. config["ipAssignments"].get<std::vector<std::string> >(), (bool)config["noAutoAssignIps"],
  1056. (bool)config["ssoExempt"], (uint64_t)config["authenticationExpiryTime"],
  1057. OSUtils::jsonDump(config["capabilities"], -1), (uint64_t)config["creationTime"],
  1058. OSUtils::jsonString(config["identity"], ""), (uint64_t)config["lastAuthorizedTime"],
  1059. (uint64_t)config["lastDeauthorizedTime"], (int)config["remoteTraceLevel"], target,
  1060. (uint64_t)config["revision"], OSUtils::jsonDump(config["tags"], -1), (int)config["vMajor"],
  1061. (int)config["vMinor"], (int)config["vRev"], (int)config["vProto"]);
  1062. w.commit();
  1063. if (_smee != NULL && isNewMember) {
  1064. // TODO: Smee Notifications for New Members
  1065. // pqxx::row row = w.exec_params1(
  1066. // "SELECT "
  1067. // " count(h.hook_id) "
  1068. // "FROM "
  1069. // " ztc_hook h "
  1070. // " INNER JOIN ztc_org o ON o.org_id = h.org_id "
  1071. // " INNER JOIN ztc_network n ON n.owner_id = o.owner_id "
  1072. // " WHERE "
  1073. // "n.id = $1 ",
  1074. // networkId);
  1075. // int64_t hookCount = row[0].as<int64_t>();
  1076. // if (hookCount > 0) {
  1077. // notifyNewMember(networkId, memberId);
  1078. // }
  1079. }
  1080. const uint64_t nwidInt = OSUtils::jsonIntHex(config["nwid"], 0ULL);
  1081. const uint64_t memberidInt = OSUtils::jsonIntHex(config["id"], 0ULL);
  1082. if (nwidInt && memberidInt) {
  1083. nlohmann::json nwOrig;
  1084. nlohmann::json memOrig;
  1085. nlohmann::json memNew(config);
  1086. get(nwidInt, nwOrig, memberidInt, memOrig);
  1087. _memberChanged(memOrig, memNew, qitem.second);
  1088. }
  1089. else {
  1090. fprintf(
  1091. stderr, "%s: Can't notify of change. Error parsing nwid or memberid: %llu-%llu\n",
  1092. _myAddressStr.c_str(), (unsigned long long)nwidInt, (unsigned long long)memberidInt);
  1093. }
  1094. }
  1095. catch (std::exception& e) {
  1096. fprintf(
  1097. stderr, "%s ERROR: Error updating member %s-%s: %s\n", _myAddressStr.c_str(), networkId.c_str(),
  1098. memberId.c_str(), e.what());
  1099. mspan->SetStatus(opentelemetry::trace::StatusCode::kError, e.what());
  1100. }
  1101. }
  1102. else if (objtype == "network") {
  1103. auto nspan = tracer->StartSpan("CentralDB::commitThread::network");
  1104. auto nscope = tracer->WithActiveSpan(nspan);
  1105. try {
  1106. // fprintf(stderr, "%s: commitThread: network\n", _myAddressStr.c_str());
  1107. pqxx::work w(*c->c);
  1108. std::string id = config["id"];
  1109. pqxx::result res = w.exec_params0(
  1110. "INSERT INTO networks_ctl (id, name, configuration, controller_id, revision) "
  1111. "VALUES ($1, $2, $3, $4, $5) "
  1112. "ON CONFLICT (id) DO UPDATE SET "
  1113. "name = EXCLUDED.name, configuration = EXCLUDED.configuration, revision = EXCLUDED.revision+1",
  1114. id, OSUtils::jsonString(config["name"], ""), OSUtils::jsonDump(config, -1), _myAddressStr,
  1115. ((uint64_t)config["revision"]));
  1116. w.commit();
  1117. // res = w.exec_params0("DELETE FROM ztc_network_assignment_pool WHERE network_id = $1", 0);
  1118. // auto pool = config["ipAssignmentPools"];
  1119. // bool err = false;
  1120. // for (auto i = pool.begin(); i != pool.end(); ++i) {
  1121. // std::string start = (*i)["ipRangeStart"];
  1122. // std::string end = (*i)["ipRangeEnd"];
  1123. // res = w.exec_params0(
  1124. // "INSERT INTO ztc_network_assignment_pool (network_id, ip_range_start, ip_range_end) "
  1125. // "VALUES ($1, $2, $3)",
  1126. // id, start, end);
  1127. // }
  1128. const uint64_t nwidInt = OSUtils::jsonIntHex(config["nwid"], 0ULL);
  1129. if (nwidInt) {
  1130. nlohmann::json nwOrig;
  1131. nlohmann::json nwNew(config);
  1132. get(nwidInt, nwOrig);
  1133. _networkChanged(nwOrig, nwNew, qitem.second);
  1134. }
  1135. else {
  1136. fprintf(
  1137. stderr, "%s: Can't notify network changed: %llu\n", _myAddressStr.c_str(),
  1138. (unsigned long long)nwidInt);
  1139. }
  1140. }
  1141. catch (std::exception& e) {
  1142. nspan->SetStatus(opentelemetry::trace::StatusCode::kError, e.what());
  1143. fprintf(stderr, "%s ERROR: Error updating network: %s\n", _myAddressStr.c_str(), e.what());
  1144. }
  1145. if (_listenerMode == LISTENER_MODE_REDIS && _redisMemberStatus) {
  1146. try {
  1147. std::string id = config["id"];
  1148. std::string controllerId = _myAddressStr.c_str();
  1149. std::string key = "networks:{" + controllerId + "}";
  1150. if (_cc->redisConfig->clusterMode) {
  1151. _cluster->sadd(key, id);
  1152. }
  1153. else {
  1154. _redis->sadd(key, id);
  1155. }
  1156. }
  1157. catch (sw::redis::Error& e) {
  1158. nspan->SetStatus(opentelemetry::trace::StatusCode::kError, e.what());
  1159. fprintf(stderr, "ERROR: Error adding network to Redis: %s\n", e.what());
  1160. }
  1161. }
  1162. }
  1163. else if (objtype == "_delete_network") {
  1164. auto dspan = tracer->StartSpan("CentralDB::commitThread::_delete_network");
  1165. auto dscope = tracer->WithActiveSpan(dspan);
  1166. // fprintf(stderr, "%s: commitThread: delete network\n", _myAddressStr.c_str());
  1167. try {
  1168. pqxx::work w(*c->c);
  1169. std::string networkId = config["id"];
  1170. fprintf(stderr, "Deleting network %s\n", networkId.c_str());
  1171. w.exec_params0("DELETE FROM network_memberships_ctl WHERE network_id = $1", networkId);
  1172. w.exec_params0("DELETE FROM networks_ctl WHERE id = $1", networkId);
  1173. w.commit();
  1174. uint64_t nwidInt = OSUtils::jsonIntHex(config["nwid"], 0ULL);
  1175. json oldConfig;
  1176. get(nwidInt, oldConfig);
  1177. json empty;
  1178. _networkChanged(oldConfig, empty, qitem.second);
  1179. }
  1180. catch (std::exception& e) {
  1181. dspan->SetStatus(opentelemetry::trace::StatusCode::kError, e.what());
  1182. fprintf(stderr, "%s ERROR: Error deleting network: %s\n", _myAddressStr.c_str(), e.what());
  1183. }
  1184. if (_listenerMode == LISTENER_MODE_REDIS && _redisMemberStatus) {
  1185. try {
  1186. std::string id = config["id"];
  1187. std::string controllerId = _myAddressStr.c_str();
  1188. std::string key = "networks:{" + controllerId + "}";
  1189. if (_cc->redisConfig->clusterMode) {
  1190. _cluster->srem(key, id);
  1191. _cluster->del("network-nodes-online:{" + controllerId + "}:" + id);
  1192. }
  1193. else {
  1194. _redis->srem(key, id);
  1195. _redis->del("network-nodes-online:{" + controllerId + "}:" + id);
  1196. }
  1197. }
  1198. catch (sw::redis::Error& e) {
  1199. dspan->SetStatus(opentelemetry::trace::StatusCode::kError, e.what());
  1200. fprintf(stderr, "ERROR: Error adding network to Redis: %s\n", e.what());
  1201. }
  1202. }
  1203. }
  1204. else if (objtype == "_delete_member") {
  1205. auto mspan = tracer->StartSpan("CentralDB::commitThread::_delete_member");
  1206. auto mscope = tracer->WithActiveSpan(mspan);
  1207. // fprintf(stderr, "%s commitThread: delete member\n", _myAddressStr.c_str());
  1208. try {
  1209. pqxx::work w(*c->c);
  1210. std::string memberId = config["id"];
  1211. std::string networkId = config["nwid"];
  1212. pqxx::result res = w.exec_params0(
  1213. "DELETE FROM network_memberships_ctl WHERE device_id = $1 AND network_id = $2", memberId,
  1214. networkId);
  1215. w.commit();
  1216. uint64_t nwidInt = OSUtils::jsonIntHex(config["nwid"], 0ULL);
  1217. uint64_t memberidInt = OSUtils::jsonIntHex(config["id"], 0ULL);
  1218. nlohmann::json networkConfig;
  1219. nlohmann::json oldConfig;
  1220. get(nwidInt, networkConfig, memberidInt, oldConfig);
  1221. json empty;
  1222. _memberChanged(oldConfig, empty, qitem.second);
  1223. }
  1224. catch (std::exception& e) {
  1225. mspan->SetStatus(opentelemetry::trace::StatusCode::kError, e.what());
  1226. fprintf(stderr, "%s ERROR: Error deleting member: %s\n", _myAddressStr.c_str(), e.what());
  1227. }
  1228. if (_listenerMode == LISTENER_MODE_REDIS && _redisMemberStatus) {
  1229. try {
  1230. std::string memberId = config["id"];
  1231. std::string networkId = config["nwid"];
  1232. std::string controllerId = _myAddressStr.c_str();
  1233. std::string key = "network-nodes-all:{" + controllerId + "}:" + networkId;
  1234. if (_cc->redisConfig->clusterMode) {
  1235. _cluster->srem(key, memberId);
  1236. _cluster->del("member:{" + controllerId + "}:" + networkId + ":" + memberId);
  1237. }
  1238. else {
  1239. _redis->srem(key, memberId);
  1240. _redis->del("member:{" + controllerId + "}:" + networkId + ":" + memberId);
  1241. }
  1242. }
  1243. catch (sw::redis::Error& e) {
  1244. mspan->SetStatus(opentelemetry::trace::StatusCode::kError, e.what());
  1245. fprintf(stderr, "ERROR: Error deleting member from Redis: %s\n", e.what());
  1246. }
  1247. }
  1248. }
  1249. else {
  1250. fprintf(stderr, "%s ERROR: unknown objtype\n", _myAddressStr.c_str());
  1251. }
  1252. }
  1253. catch (std::exception& e) {
  1254. span->SetStatus(opentelemetry::trace::StatusCode::kError, e.what());
  1255. fprintf(stderr, "%s ERROR: Error getting objtype: %s\n", _myAddressStr.c_str(), e.what());
  1256. }
  1257. _pool->unborrow(c);
  1258. c.reset();
  1259. }
  1260. fprintf(stderr, "%s commitThread finished\n", _myAddressStr.c_str());
  1261. }
  1262. void CentralDB::notifyNewMember(const std::string& networkID, const std::string& memberID)
  1263. {
  1264. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  1265. auto tracer = provider->GetTracer("CentralDB");
  1266. auto span = tracer->StartSpan("CentralDB::notifyNewMember");
  1267. auto scope = tracer->WithActiveSpan(span);
  1268. rustybits::smee_client_notify_network_joined(_smee, networkID.c_str(), memberID.c_str());
  1269. }
  1270. void CentralDB::onlineNotificationThread()
  1271. {
  1272. waitForReady();
  1273. while (_run == 1) {
  1274. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  1275. auto tracer = provider->GetTracer("CentralDB");
  1276. auto span = tracer->StartSpan("CentralDB::onlineNotificationThread");
  1277. auto scope = tracer->WithActiveSpan(span);
  1278. try {
  1279. std::unordered_map<std::pair<uint64_t, uint64_t>, NodeOnlineRecord, _PairHasher> lastOnline;
  1280. {
  1281. std::lock_guard<std::mutex> l(_lastOnline_l);
  1282. lastOnline.swap(_lastOnline);
  1283. }
  1284. uint64_t updateCount = 0;
  1285. auto c = _pool->borrow();
  1286. pqxx::work w(*c->c);
  1287. for (auto i = lastOnline.begin(); i != lastOnline.end(); ++i) {
  1288. updateCount += 1;
  1289. uint64_t nwid_i = i->first.first;
  1290. char nwidTmp[64];
  1291. char memTmp[64];
  1292. char ipTmp[64];
  1293. OSUtils::ztsnprintf(nwidTmp, sizeof(nwidTmp), "%.16llx", nwid_i);
  1294. OSUtils::ztsnprintf(memTmp, sizeof(memTmp), "%.10llx", i->first.second);
  1295. nlohmann::json jtmp1, jtmp2;
  1296. if (! get(nwid_i, jtmp1, i->first.second, jtmp2)) {
  1297. continue; // skip non existent networks/members
  1298. }
  1299. std::string networkId(nwidTmp);
  1300. std::string memberId(memTmp);
  1301. try {
  1302. // check if the member exists first.
  1303. //
  1304. // exec_params1 will throw pqxx::unexpected_rows if not exactly one row is returned. If that's the
  1305. // case, skip this record and move on.
  1306. pqxx::row r = w.exec_params1(
  1307. "SELECT device_id, network_id FROM network_memberships_ctl WHERE network_id = $1 AND device_id "
  1308. "= $2",
  1309. networkId, memberId);
  1310. }
  1311. catch (pqxx::unexpected_rows& e) {
  1312. continue;
  1313. }
  1314. int64_t ts = i->second.lastSeen;
  1315. std::string ipAddr = i->second.physicalAddress.toIpString(ipTmp);
  1316. std::string timestamp = std::to_string(ts);
  1317. std::string osArch = i->second.osArch;
  1318. std::vector<std::string> osArchSplit = split(osArch, '/');
  1319. std::string os = "unknown";
  1320. std::string arch = "unknown";
  1321. if (osArchSplit.size() == 2) {
  1322. os = osArchSplit[0];
  1323. arch = osArchSplit[1];
  1324. }
  1325. _statusWriter->updateNodeStatus(networkId, memberId, os, arch, "", i->second.physicalAddress, ts);
  1326. }
  1327. _statusWriter->writePending();
  1328. w.commit();
  1329. _pool->unborrow(c);
  1330. }
  1331. catch (std::exception& e) {
  1332. fprintf(stderr, "%s: error in onlinenotification thread: %s\n", _myAddressStr.c_str(), e.what());
  1333. }
  1334. std::this_thread::sleep_for(std::chrono::seconds(10));
  1335. }
  1336. }
  1337. #endif // ZT_CONTROLLER_USE_LIBPQ