From 25e47c7f34f90f5393958f998c1861efa966d702 Mon Sep 17 00:00:00 2001 From: Shobhit Adlakha Date: Tue, 15 Mar 2022 17:21:06 -0400 Subject: Fix/Clear endpoint_properties in policy DB if empty properties recieved in PTU (#3880) * Clear endpoint_properties from policy DB if empty endpoint_properties are recieved in a PTU * Add check to only overwrite endpoint_properties if parameter is defined and empty * Add is_initialized check for external proprietary --- .../policy/policy_external/include/policy/sql_pt_queries.h | 1 + src/components/policy/policy_external/src/sql_pt_queries.cc | 3 +++ .../policy/policy_external/src/sql_pt_representation.cc | 12 ++++++++++++ .../policy/policy_regular/include/policy/sql_pt_queries.h | 1 + src/components/policy/policy_regular/src/sql_pt_queries.cc | 3 +++ .../policy/policy_regular/src/sql_pt_representation.cc | 12 ++++++++++++ 6 files changed, 32 insertions(+) (limited to 'src') diff --git a/src/components/policy/policy_external/include/policy/sql_pt_queries.h b/src/components/policy/policy_external/include/policy/sql_pt_queries.h index aba08dcca1..18c4468703 100644 --- a/src/components/policy/policy_external/include/policy/sql_pt_queries.h +++ b/src/components/policy/policy_external/include/policy/sql_pt_queries.h @@ -99,6 +99,7 @@ extern const std::string kInsertDeviceData; extern const std::string kInsertAppLevel; extern const std::string kDeleteSecondsBetweenRetries; extern const std::string kDeleteEndpoint; +extern const std::string kDeleteEndpointProperties; extern const std::string kDeleteAppLevel; extern const std::string kDeleteMessageString; extern const std::string kDeleteFunctionalGroup; diff --git a/src/components/policy/policy_external/src/sql_pt_queries.cc b/src/components/policy/policy_external/src/sql_pt_queries.cc index 91dd6fb713..8414c2feda 100644 --- a/src/components/policy/policy_external/src/sql_pt_queries.cc +++ b/src/components/policy/policy_external/src/sql_pt_queries.cc @@ -918,6 +918,9 @@ const std::string kDeleteSecondsBetweenRetries = const std::string kDeleteEndpoint = "DELETE FROM `endpoint`"; +const std::string kDeleteEndpointProperties = + "DELETE FROM `endpoint_properties`"; + const std::string kDeleteAppLevel = "DELETE FROM `app_level`"; const std::string kDeleteMessageString = "DELETE FROM `message`"; diff --git a/src/components/policy/policy_external/src/sql_pt_representation.cc b/src/components/policy/policy_external/src/sql_pt_representation.cc index 5bee09773d..0436e82bbb 100644 --- a/src/components/policy/policy_external/src/sql_pt_representation.cc +++ b/src/components/policy/policy_external/src/sql_pt_representation.cc @@ -1575,6 +1575,18 @@ bool SQLPTRepresentation::SaveServiceEndpoints( bool SQLPTRepresentation::SaveServiceEndpointProperties( const policy_table::ServiceEndpointProperties& endpoint_properties) { utils::dbms::SQLQuery query(db()); + + if (endpoint_properties.is_initialized() && endpoint_properties.empty()) { + bool delete_query_exec_result = + query.Exec(sql_pt::kDeleteEndpointProperties); + + if (!delete_query_exec_result) { + SDL_LOG_WARN("Failed to delete endpoint properties from DB."); + return false; + } + return true; + } + if (!query.Prepare(sql_pt::kInsertEndpointVersion)) { SDL_LOG_WARN( "Incorrect insert of endpoint property to endpoint_properties."); diff --git a/src/components/policy/policy_regular/include/policy/sql_pt_queries.h b/src/components/policy/policy_regular/include/policy/sql_pt_queries.h index fd7f879abf..492db56204 100644 --- a/src/components/policy/policy_regular/include/policy/sql_pt_queries.h +++ b/src/components/policy/policy_regular/include/policy/sql_pt_queries.h @@ -99,6 +99,7 @@ extern const std::string kInsertDeviceData; extern const std::string kInsertAppLevel; extern const std::string kDeleteSecondsBetweenRetries; extern const std::string kDeleteEndpoint; +extern const std::string kDeleteEndpointProperties; extern const std::string kDeleteAppLevel; extern const std::string kDeleteMessageString; extern const std::string kDeleteFunctionalGroup; diff --git a/src/components/policy/policy_regular/src/sql_pt_queries.cc b/src/components/policy/policy_regular/src/sql_pt_queries.cc index 6e0bfd8c89..c86e934823 100644 --- a/src/components/policy/policy_regular/src/sql_pt_queries.cc +++ b/src/components/policy/policy_regular/src/sql_pt_queries.cc @@ -868,6 +868,9 @@ const std::string kDeleteSecondsBetweenRetries = const std::string kDeleteEndpoint = "DELETE FROM `endpoint`"; +const std::string kDeleteEndpointProperties = + "DELETE FROM `endpoint_properties`"; + const std::string kDeleteAppLevel = "DELETE FROM `app_level`"; const std::string kDeleteMessageString = "DELETE FROM `message`"; diff --git a/src/components/policy/policy_regular/src/sql_pt_representation.cc b/src/components/policy/policy_regular/src/sql_pt_representation.cc index f2254d7752..9dcde34588 100644 --- a/src/components/policy/policy_regular/src/sql_pt_representation.cc +++ b/src/components/policy/policy_regular/src/sql_pt_representation.cc @@ -1539,6 +1539,18 @@ bool SQLPTRepresentation::SaveServiceEndpoints( bool SQLPTRepresentation::SaveServiceEndpointProperties( const policy_table::ServiceEndpointProperties& endpoint_properties) { utils::dbms::SQLQuery query(db()); + + if (endpoint_properties.is_initialized() && endpoint_properties.empty()) { + bool delete_query_exec_result = + query.Exec(sql_pt::kDeleteEndpointProperties); + + if (!delete_query_exec_result) { + SDL_LOG_WARN("Failed to delete endpoint properties from DB."); + return false; + } + return true; + } + if (!query.Prepare(sql_pt::kInsertEndpointVersion)) { SDL_LOG_WARN( "Incorrect insert of endpoint property to endpoint_properties."); -- cgit v1.2.1