From 366438b4b8698cbaeb10bbde9f3fddfb732d3b6a Mon Sep 17 00:00:00 2001 From: Marcin Siodelski Date: Fri, 3 Feb 2023 13:06:51 +0100 Subject: [PATCH] [#2741] Fixed getting configured timeout parameters --- src/lib/database/database_connection.cc | 17 +++--- src/lib/database/db_messages.cc | 8 ++- src/lib/database/db_messages.h | 4 +- src/lib/database/db_messages.mes | 18 +++++-- .../tests/database_connection_unittest.cc | 53 ++++++++++++++++++- 5 files changed, 85 insertions(+), 15 deletions(-) diff --git a/src/lib/database/database_connection.cc b/src/lib/database/database_connection.cc index 067a1e03d9..50175c6aa8 100644 --- a/src/lib/database/database_connection.cc +++ b/src/lib/database/database_connection.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2022 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2015-2023 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -225,6 +225,9 @@ DatabaseConnection::toElement(const ParameterMap& params) { if ((keyword == "lfc-interval") || (keyword == "connect-timeout") || + (keyword == "read-timeout") || + (keyword == "write-timeout") || + (keyword == "tcp-user-timeout") || (keyword == "reconnect-wait-time") || (keyword == "max-reconnect-tries") || (keyword == "port") || @@ -235,8 +238,8 @@ DatabaseConnection::toElement(const ParameterMap& params) { int_value = boost::lexical_cast(value); result->set(keyword, isc::data::Element::create(int_value)); } catch (...) { - LOG_ERROR(database_logger, DATABASE_TO_JSON_ERROR) - .arg("integer").arg(keyword).arg(value); + LOG_ERROR(database_logger, DATABASE_TO_JSON_INTEGER_ERROR) + .arg(keyword).arg(value); } } else if ((keyword == "persist") || (keyword == "readonly")) { @@ -245,8 +248,8 @@ DatabaseConnection::toElement(const ParameterMap& params) { } else if (value == "false") { result->set(keyword, isc::data::Element::create(false)); } else { - LOG_ERROR(database_logger, DATABASE_TO_JSON_ERROR) - .arg("boolean").arg(keyword).arg(value); + LOG_ERROR(database_logger, DATABASE_TO_JSON_BOOLEAN_ERROR) + .arg(keyword).arg(value); } } else if ((keyword == "type") || (keyword == "user") || @@ -260,8 +263,8 @@ DatabaseConnection::toElement(const ParameterMap& params) { (keyword == "cipher-list")) { result->set(keyword, isc::data::Element::create(value)); } else { - LOG_ERROR(database_logger, DATABASE_TO_JSON_ERROR) - .arg("unknown").arg(keyword).arg(value); + LOG_ERROR(database_logger, DATABASE_TO_JSON_UNKNOWN_TYPE_ERROR) + .arg(keyword).arg(value); } } diff --git a/src/lib/database/db_messages.cc b/src/lib/database/db_messages.cc index 6a25cf5b95..5012849c3a 100644 --- a/src/lib/database/db_messages.cc +++ b/src/lib/database/db_messages.cc @@ -19,7 +19,9 @@ extern const isc::log::MessageID DATABASE_PGSQL_FATAL_ERROR = "DATABASE_PGSQL_FA extern const isc::log::MessageID DATABASE_PGSQL_ROLLBACK = "DATABASE_PGSQL_ROLLBACK"; extern const isc::log::MessageID DATABASE_PGSQL_ROLLBACK_SAVEPOINT = "DATABASE_PGSQL_ROLLBACK_SAVEPOINT"; extern const isc::log::MessageID DATABASE_PGSQL_START_TRANSACTION = "DATABASE_PGSQL_START_TRANSACTION"; -extern const isc::log::MessageID DATABASE_TO_JSON_ERROR = "DATABASE_TO_JSON_ERROR"; +extern const isc::log::MessageID DATABASE_TO_JSON_BOOLEAN_ERROR = "DATABASE_TO_JSON_BOOLEAN_ERROR"; +extern const isc::log::MessageID DATABASE_TO_JSON_INTEGER_ERROR = "DATABASE_TO_JSON_INTEGER_ERROR"; +extern const isc::log::MessageID DATABASE_TO_JSON_UNKNOWN_TYPE_ERROR = "DATABASE_TO_JSON_UNKNOWN_TYPE_ERROR"; } // namespace db } // namespace isc @@ -39,7 +41,9 @@ const char* values[] = { "DATABASE_PGSQL_ROLLBACK", "rolling back PostgreSQL database", "DATABASE_PGSQL_ROLLBACK_SAVEPOINT", "rolling back PostgreSQL database to savepoint: $1", "DATABASE_PGSQL_START_TRANSACTION", "starting a new PostgreSQL transaction", - "DATABASE_TO_JSON_ERROR", "Internal logic error: unknown %1 element found in state: %2", + "DATABASE_TO_JSON_BOOLEAN_ERROR", "Internal logic error: invalid boolean value found in database connection parameters: %1=%2", + "DATABASE_TO_JSON_INTEGER_ERROR", "Internal logic error: invalid integer value found in database connection parameters: %1=%2", + "DATABASE_TO_JSON_UNKNOWN_TYPE_ERROR", "Internal logic error: unknown element found in database connection parameters: %1=%2", NULL }; diff --git a/src/lib/database/db_messages.h b/src/lib/database/db_messages.h index 6cfad3189a..3dddafcd2e 100644 --- a/src/lib/database/db_messages.h +++ b/src/lib/database/db_messages.h @@ -20,7 +20,9 @@ extern const isc::log::MessageID DATABASE_PGSQL_FATAL_ERROR; extern const isc::log::MessageID DATABASE_PGSQL_ROLLBACK; extern const isc::log::MessageID DATABASE_PGSQL_ROLLBACK_SAVEPOINT; extern const isc::log::MessageID DATABASE_PGSQL_START_TRANSACTION; -extern const isc::log::MessageID DATABASE_TO_JSON_ERROR; +extern const isc::log::MessageID DATABASE_TO_JSON_BOOLEAN_ERROR; +extern const isc::log::MessageID DATABASE_TO_JSON_INTEGER_ERROR; +extern const isc::log::MessageID DATABASE_TO_JSON_UNKNOWN_TYPE_ERROR; } // namespace db } // namespace isc diff --git a/src/lib/database/db_messages.mes b/src/lib/database/db_messages.mes index d919320948..4ef3cf949f 100644 --- a/src/lib/database/db_messages.mes +++ b/src/lib/database/db_messages.mes @@ -1,4 +1,4 @@ -# Copyright (C) 2012-2022 Internet Systems Consortium, Inc. ("ISC") +# Copyright (C) 2012-2023 Internet Systems Consortium, Inc. ("ISC") # # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this @@ -80,8 +80,20 @@ inserted into multiple tables with multiple INSERT statements and there may be a need to rollback the whole transaction if any of these INSERT statements fail. -% DATABASE_TO_JSON_ERROR Internal logic error: unknown %1 element found in state: %2 +% DATABASE_TO_JSON_BOOLEAN_ERROR Internal logic error: invalid boolean value found in database connection parameters: %1=%2 This error message is printed when conversion to JSON of the internal state is requested, -but the connection string contains unrecognized parameter. This is a programming error. +but the connection string contains a boolean parameter with invalid value. It is a programming +error. The software will continue operation, but the returned JSON data will be syntactically +valid, but incomplete. The culprit parameter will not be converted. + +% DATABASE_TO_JSON_INTEGER_ERROR Internal logic error: invalid integer value found in database connection parameters: %1=%2 +This error message is printed when conversion to JSON of the internal state is requested, +but the connection string contains the integer parameter with a wrong value. It is a programming +error. The software will continue operation, but the returned JSON data will be syntactically +valid, but incomplete. The culprit parameter will not be converted. + +% DATABASE_TO_JSON_UNKNOWN_TYPE_ERROR Internal logic error: unknown element found in database connection parameters: %1=%2 +This error message is printed when conversion to JSON of the internal state is requested, +but the connection string contains unrecognized parameter. It is a programming error. The software will continue operation, but the returned JSON data will be syntactically valid, but incomplete. The unknown parameter will not be converted. diff --git a/src/lib/database/tests/database_connection_unittest.cc b/src/lib/database/tests/database_connection_unittest.cc index 42dda26093..dc8c8c30ed 100644 --- a/src/lib/database/tests/database_connection_unittest.cc +++ b/src/lib/database/tests/database_connection_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2022 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2015-2023 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -525,7 +525,7 @@ TEST(DatabaseConnectionTest, redactAccessStringNoPassword) { TEST(DatabaseConnection, toElementDbAccessStringValid) { const char* configs[] = { "{\n" - "\"connect-timeout\" : 200, \n" + "\"connect-timeout\": 200, \n" "\"on-fail\": \"stop-retry-exit\", \n" "\"lfc-interval\" : 100, \n" "\"host\": \"whatevah\", \n" @@ -558,6 +558,55 @@ TEST(DatabaseConnection, toElementDbAccessStringValid) { ASSERT_TRUE(json_elements->equals(*round_trip)); } +// Check that the toElementDbAccessString() handles Postgres backend +// specific paramateres. +TEST(DatabaseConnection, toElementDbAccessStringValidPostgresql) { + const char* configs[] = { + "{\n" + "\"connect-timeout\": 200, \n" + "\"tcp-user-timeout\": 10, \n" + "\"type\": \"postgresql\", \n" + "\"user\": \"user_str\" \n" + "}\n" + }; + + DbAccessParser parser; + std::string access_str; + ConstElementPtr json_elements; + + ASSERT_NO_THROW(json_elements = Element::fromJSON(configs[0])); + ASSERT_NO_THROW(parser.parse(access_str, json_elements)); + + ElementPtr round_trip = DatabaseConnection::toElementDbAccessString(access_str); + + ASSERT_TRUE(json_elements->equals(*round_trip)); +} + +// Check that the toElementDbAccessString() handles MySQL backend +// specific paramateres. +TEST(DatabaseConnection, toElementDbAccessStringValidMySql) { + const char* configs[] = { + "{\n" + "\"connect-timeout\": 200, \n" + "\"read-timeout\": 10, \n" + "\"write-timeout\": 10, \n" + "\"type\": \"mysql\", \n" + "\"user\": \"user_str\" \n" + "}\n" + }; + + DbAccessParser parser; + std::string access_str; + ConstElementPtr json_elements; + + ASSERT_NO_THROW(json_elements = Element::fromJSON(configs[0])); + ASSERT_NO_THROW(parser.parse(access_str, json_elements)); + + ElementPtr round_trip = DatabaseConnection::toElementDbAccessString(access_str); + + ASSERT_TRUE(json_elements->equals(*round_trip)); +} + // Check that toElementDbAccessString() catches invalid parameters. // Note that because toElementDbAccessString() utilizes // toElement() this tests both. -- 2.47.2