CentralDB.cpp 50 KB

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