From 3bfc438ae8b043f88db07a8002e37109b8f8deb5 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Fri, 4 Jun 2021 14:40:14 -0700 Subject: [PATCH 1/4] null handling --- controller/PostgreSQL.cpp | 68 +++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/controller/PostgreSQL.cpp b/controller/PostgreSQL.cpp index 6200b9ef6..2b228476c 100644 --- a/controller/PostgreSQL.cpp +++ b/controller/PostgreSQL.cpp @@ -436,26 +436,26 @@ void PostgreSQL::initializeNetworks() config["id"] = nwid; config["nwid"] = nwid; - try { + if (!row[1].is_null()) { config["creationTime"] = row[1].as(); - } catch (std::exception &e) { + } else { config["creationTime"] = 0ULL; } config["capabilities"] = row[2].as(); config["enableBroadcast"] = row[3].as(); - try { + if (!row[4].is_null()) { config["lastModified"] = row[4].as(); - } catch (std::exception &e) { + } else { config["lastModified"] = 0ULL; } - try { + if (!row[5].is_null()) { config["mtu"] = row[5].as(); - } catch (std::exception &e) { + } else { config["mtu"] = 2800; } - try { + if (!row[6].is_null()) { config["multicastLimit"] = row[6].as(); - } catch (std::exception &e) { + } else { config["multicastLimit"] = 64; } config["name"] = row[7].as(); @@ -472,9 +472,9 @@ void PostgreSQL::initializeNetworks() config["remoteTraceTarget"] = nullptr; } - try { + if (!row[11].is_null()) { config["revision"] = row[11].as(); - } catch (std::exception &e) { + } else { config["revision"] = 0ULL; //fprintf(stderr, "Error converting revision: %s\n", PQgetvalue(res, i, 11)); } @@ -575,6 +575,8 @@ void PostgreSQL::initializeNetworks() void PostgreSQL::initializeMembers() { + std::string memberId; + std::string networkId; try { std::unordered_map networkMembers; @@ -595,6 +597,9 @@ void PostgreSQL::initializeMembers() for (auto row = r.begin(); row != r.end(); row++) { json empty; json config; + + memberId = ""; + networkId = ""; initMember(config); @@ -605,8 +610,8 @@ void PostgreSQL::initializeMembers() if (row[1].is_null()) { fprintf(stderr, "Null NetworkID?!?\n"); } - std::string memberId = row[0].as(); - std::string networkId = row[1].as(); + memberId = row[0].as(); + networkId = row[1].as(); config["id"] = memberId; config["nwid"] = networkId; @@ -623,29 +628,29 @@ void PostgreSQL::initializeMembers() } config["creationTime"] = row[5].as(); config["identity"] = row[6].as(); - try { + if (!row[7].is_null()) { config["lastAuthorizedTime"] = row[7].as(); - } catch(std::exception &e) { + } else { config["lastAuthorizedTime"] = 0ULL; //fprintf(stderr, "Error updating last auth time (member): %s\n", PQgetvalue(res, i, 7)); } - try { + if (!row[8].is_null()) { config["lastDeauthorizedTime"] = row[8].as(); - } catch( std::exception &e) { + } else { config["lastDeauthorizedTime"] = 0ULL; //fprintf(stderr, "Error updating last deauth time (member): %s\n", PQgetvalue(res, i, 8)); } - try { + if (!row[9].is_null()) { config["remoteTraceLevel"] = row[9].as(); - } catch (std::exception &e) { + } else { config["remoteTraceLevel"] = 0; } - if (!config["remoteTraceTarget"].is_null()) { + if (!row[10].is_null()) { config["remoteTraceTarget"] = row[10].as(); } else { config["remoteTraceTarget"] = ""; } - if (config["tags"].is_null()) { + if (row[11].is_null()) { config["tags"] = json::array(); } else { try { @@ -654,32 +659,31 @@ void PostgreSQL::initializeMembers() config["tags"] = json::array(); } } - try { + if (!row[12].is_null()) { config["vMajor"] = row[12].as(); - } catch(std::exception &e) { + } else { config["vMajor"] = -1; } - try { + if (!row[13].is_null()) { config["vMinor"] = row[13].as(); - } catch (std::exception &e) { + } else { config["vMinor"] = -1; } - try { + if (!row[14].is_null()) { config["vRev"] = row[14].as(); - } catch (std::exception &e) { + } else { config["vRev"] = -1; } - try { + if (!row[15].is_null()) { config["vProto"] = row[15].as(); - } catch (std::exception &e) { + } else { config["vProto"] = -1; } config["noAutoAssignIps"] = row[16].as(); - try { + if (!row[17].is_null()) { config["revision"] = row[17].as(); - } catch (std::exception &e) { + } else { config["revision"] = 0ULL; - //fprintf(stderr, "Error updating revision (member): %s\n", PQgetvalue(res, i, 17)); } config["ssoExempt"] = row[18].as(); @@ -728,7 +732,7 @@ void PostgreSQL::initializeMembers() } catch (sw::redis::Error &e) { fprintf(stderr, "ERROR: Error initializing members (redis): %s\n", e.what()); } catch (std::exception &e) { - fprintf(stderr, "ERROR: Error initializing members: %s\n", e.what()); + fprintf(stderr, "ERROR: Error initializing member: %s-%s %s\n", networkId.c_str(), memberId.c_str(), e.what()); exit(-1); } } From add33f1ab357b1917ba151e0e6ce52b769654eee Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Fri, 4 Jun 2021 14:48:41 -0700 Subject: [PATCH 2/4] cast to bigint in query --- controller/PostgreSQL.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/controller/PostgreSQL.cpp b/controller/PostgreSQL.cpp index 2b228476c..beb8f3bbc 100644 --- a/controller/PostgreSQL.cpp +++ b/controller/PostgreSQL.cpp @@ -418,8 +418,8 @@ void PostgreSQL::initializeNetworks() fprintf(stderr, "Initializing Networks...\n"); auto c = _pool->borrow(); pqxx::work w{*c->c}; - pqxx::result r = w.exec_params("SELECT id, EXTRACT(EPOCH FROM creation_time AT TIME ZONE 'UTC')*1000 as creation_time, capabilities, " - "enable_broadcast, EXTRACT(EPOCH FROM last_modified AT TIME ZONE 'UTC')*1000 AS last_modified, mtu, multicast_limit, name, private, remote_trace_level, " + pqxx::result r = w.exec_params("SELECT id, (EXTRACT(EPOCH FROM creation_time AT TIME ZONE 'UTC')*1000)::bigint as creation_time, capabilities, " + "enable_broadcast, (EXTRACT(EPOCH FROM last_modified AT TIME ZONE 'UTC')*1000)::bigint AS last_modified, mtu, multicast_limit, name, private, remote_trace_level, " "remote_trace_target, revision, rules, tags, v4_assign_mode, v6_assign_mode, sso_enabled FROM ztc_network " "WHERE deleted = false AND controller_id = $1", _myAddressStr); From fd85f87adec7bfa4b3c8ea0af662400838839116 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Fri, 4 Jun 2021 15:15:42 -0700 Subject: [PATCH 3/4] handle null in result set --- controller/PostgreSQL.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controller/PostgreSQL.cpp b/controller/PostgreSQL.cpp index beb8f3bbc..e7d23e4b7 100644 --- a/controller/PostgreSQL.cpp +++ b/controller/PostgreSQL.cpp @@ -696,7 +696,7 @@ void PostgreSQL::initializeMembers() "WHERE e.network_id = $1 AND e.member_id = $2 AND n.sso_enabled = TRUE " "ORDER BY e.authentication_expiry_time LIMIT 1", networkId, memberId); - if (authRes.size() == 1) { + if (authRes.size() == 1 && !authRes.at(0)[0].is_null()) { // there is an expiry time record config["authenticationExpiryTime"] = authRes.at(0)[0].as(); } From b16f40c0de76192642e13f5993946c534375a9c6 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Fri, 4 Jun 2021 15:18:18 -0700 Subject: [PATCH 4/4] . --- controller/PostgreSQL.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/controller/PostgreSQL.cpp b/controller/PostgreSQL.cpp index e7d23e4b7..e035d4aab 100644 --- a/controller/PostgreSQL.cpp +++ b/controller/PostgreSQL.cpp @@ -699,6 +699,8 @@ void PostgreSQL::initializeMembers() if (authRes.size() == 1 && !authRes.at(0)[0].is_null()) { // there is an expiry time record config["authenticationExpiryTime"] = authRes.at(0)[0].as(); + } else { + config["authenticationExpiryTime"] = 0; } config["objtype"] = "member";