diff options
author | Marcin Siodelski <marcin@isc.org> | 2023-01-12 18:25:16 +0100 |
---|---|---|
committer | Marcin Siodelski <marcin@isc.org> | 2023-01-17 11:26:51 +0100 |
commit | fe26216af8fd94779f5a1b6f6baed5f9e027c7ea (patch) | |
tree | 91996801720e77663012c791ec95aac42475d381 /src/lib/database | |
parent | [#2688] New tests for invalid port number (diff) | |
download | kea-fe26216af8fd94779f5a1b6f6baed5f9e027c7ea.tar.xz kea-fe26216af8fd94779f5a1b6f6baed5f9e027c7ea.zip |
[#2688] Strict checking timeouts against backend type
The read- and write- timeouts are only allowed for the MySQL backend. The
tcp-user-timeout is only allowed for the postgresql backend.
Diffstat (limited to 'src/lib/database')
-rw-r--r-- | src/lib/database/dbaccess_parser.cc | 15 | ||||
-rw-r--r-- | src/lib/database/dbaccess_parser.h | 7 | ||||
-rw-r--r-- | src/lib/database/tests/dbaccess_parser_unittest.cc | 143 |
3 files changed, 137 insertions, 28 deletions
diff --git a/src/lib/database/dbaccess_parser.cc b/src/lib/database/dbaccess_parser.cc index 3f54098b46..e60fd91d5a 100644 --- a/src/lib/database/dbaccess_parser.cc +++ b/src/lib/database/dbaccess_parser.cc @@ -183,6 +183,11 @@ DbAccessParser::parse(std::string& access_string, << std::numeric_limits<uint32_t>::max() << " (" << value->getPosition() << ")"); } + if (read_timeout > 0 && (dbtype != "mysql")) { + ConstElementPtr value = database_config->get("read-timeout"); + isc_throw(DbConfigError, "read-timeout value is only supported by the mysql backend" + << " (" << value->getPosition() << ")"); + } if ((write_timeout < 0) || (write_timeout > std::numeric_limits<uint32_t>::max())) { ConstElementPtr value = database_config->get("write-timeout"); @@ -191,6 +196,11 @@ DbAccessParser::parse(std::string& access_string, << std::numeric_limits<uint32_t>::max() << " (" << value->getPosition() << ")"); } + if (write_timeout > 0 && (dbtype != "mysql")) { + ConstElementPtr value = database_config->get("write-timeout"); + isc_throw(DbConfigError, "write-timeout value is only supported by the mysql backend" + << " (" << value->getPosition() << ")"); + } if ((tcp_user_timeout < 0) || (tcp_user_timeout > std::numeric_limits<uint32_t>::max())) { ConstElementPtr value = database_config->get("tcp-user-timeout"); @@ -199,6 +209,11 @@ DbAccessParser::parse(std::string& access_string, << std::numeric_limits<uint32_t>::max() << " (" << value->getPosition() << ")"); } + if (tcp_user_timeout > 0 && (dbtype != "postgresql")) { + ConstElementPtr value = database_config->get("tcp-user-timeout"); + isc_throw(DbConfigError, "tcp-user-timeout value is only supported by the mysql backend" + << " (" << value->getPosition() << ")"); + } // e. Check that the port is within a reasonable range. if ((port < 0) || diff --git a/src/lib/database/dbaccess_parser.h b/src/lib/database/dbaccess_parser.h index ce0c9e2e88..30a4ec9163 100644 --- a/src/lib/database/dbaccess_parser.h +++ b/src/lib/database/dbaccess_parser.h @@ -1,4 +1,4 @@ -// Copyright (C) 2012-2021 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 @@ -47,9 +47,8 @@ public: /// @param database_config The configuration value for the "*-database" /// identifier. /// - /// @throw isc::dhcp::DbConfigError The 'type' keyword contains an - /// unknown database type or is missing from the list of - /// database access keywords. + /// @throw isc::dhcp::DbConfigError The connection parameters or their + /// combination is invalid. void parse(std::string& access_string, isc::data::ConstElementPtr database_config); diff --git a/src/lib/database/tests/dbaccess_parser_unittest.cc b/src/lib/database/tests/dbaccess_parser_unittest.cc index ec6f1a9a6d..29322e0727 100644 --- a/src/lib/database/tests/dbaccess_parser_unittest.cc +++ b/src/lib/database/tests/dbaccess_parser_unittest.cc @@ -359,8 +359,8 @@ TEST_F(DbAccessParserTest, largeLFCInterval) { // This test checks that the parser accepts the valid value of the // connect-timeout parameter. TEST_F(DbAccessParserTest, validConnectTimeout) { - const char* config[] = {"type", "memfile", - "name", "/opt/var/lib/kea/kea-leases6.csv", + const char* config[] = {"type", "mysql", + "name", "keatest", "connect-timeout", "3600", NULL}; @@ -377,8 +377,8 @@ TEST_F(DbAccessParserTest, validConnectTimeout) { // This test checks that the parser rejects the negative value of the // connect-timeout parameter. TEST_F(DbAccessParserTest, negativeConnectTimeout) { - const char* config[] = {"type", "memfile", - "name", "/opt/var/lib/kea/kea-leases6.csv", + const char* config[] = {"type", "mysql", + "name", "keatest", "connect-timeout", "-1", NULL}; @@ -393,8 +393,8 @@ TEST_F(DbAccessParserTest, negativeConnectTimeout) { // This test checks that the parser rejects a too large (greater than // the max uint32_t) value of the connecttimeout parameter. TEST_F(DbAccessParserTest, largeConnectTimeout) { - const char* config[] = {"type", "memfile", - "name", "/opt/var/lib/kea/kea-leases6.csv", + const char* config[] = {"type", "mysql", + "name", "keatest", "connect-timeout", "4294967296", NULL}; @@ -409,8 +409,8 @@ TEST_F(DbAccessParserTest, largeConnectTimeout) { // This test checks that the parser accepts the valid value of the // read-timeout parameter. TEST_F(DbAccessParserTest, validReadTimeout) { - const char* config[] = {"type", "memfile", - "name", "/opt/var/lib/kea/kea-leases6.csv", + const char* config[] = {"type", "mysql", + "name", "keatest", "read-timeout", "3600", NULL}; @@ -427,8 +427,8 @@ TEST_F(DbAccessParserTest, validReadTimeout) { // This test checks that the parser rejects the negative value of the // read-timeout parameter. TEST_F(DbAccessParserTest, negativeReadTimeout) { - const char* config[] = {"type", "memfile", - "name", "/opt/var/lib/kea/kea-leases6.csv", + const char* config[] = {"type", "mysql", + "name", "keatest", "read-timeout", "-1", NULL}; @@ -443,8 +443,8 @@ TEST_F(DbAccessParserTest, negativeReadTimeout) { // This test checks that the parser rejects a too large (greater than // the max uint32_t) value of the read-timeout parameter. TEST_F(DbAccessParserTest, largeReadTimeout) { - const char* config[] = {"type", "memfile", - "name", "/opt/var/lib/kea/kea-leases6.csv", + const char* config[] = {"type", "mysql", + "name", "keatest", "read-timeout", "4294967296", NULL}; @@ -459,8 +459,8 @@ TEST_F(DbAccessParserTest, largeReadTimeout) { // This test checks that the parser accepts the valid value of the // write-timeout parameter. TEST_F(DbAccessParserTest, validWriteTimeout) { - const char* config[] = {"type", "memfile", - "name", "/opt/var/lib/kea/kea-leases6.csv", + const char* config[] = {"type", "mysql", + "name", "keatest", "write-timeout", "3600", NULL}; @@ -477,8 +477,8 @@ TEST_F(DbAccessParserTest, validWriteTimeout) { // This test checks that the parser rejects the negative value of the // write-timeout parameter. TEST_F(DbAccessParserTest, negativeWriteTimeout) { - const char* config[] = {"type", "memfile", - "name", "/opt/var/lib/kea/kea-leases6.csv", + const char* config[] = {"type", "mysql", + "name", "keatest", "write-timeout", "-1", NULL}; @@ -493,8 +493,8 @@ TEST_F(DbAccessParserTest, negativeWriteTimeout) { // This test checks that the parser rejects a too large (greater than // the max uint32_t) value of the write-timeout parameter. TEST_F(DbAccessParserTest, largeWriteTimeout) { - const char* config[] = {"type", "memfile", - "name", "/opt/var/lib/kea/kea-leases6.csv", + const char* config[] = {"type", "mysql", + "name", "keatest", "write-timeout", "4294967296", NULL}; @@ -509,8 +509,8 @@ TEST_F(DbAccessParserTest, largeWriteTimeout) { // This test checks that the parser accepts the valid value of the // tcp-user-timeout parameter. TEST_F(DbAccessParserTest, validTcpUserTimeout) { - const char* config[] = {"type", "memfile", - "name", "/opt/var/lib/kea/kea-leases6.csv", + const char* config[] = {"type", "postgresql", + "name", "keatest", "tcp-user-timeout", "3600", NULL}; @@ -527,8 +527,8 @@ TEST_F(DbAccessParserTest, validTcpUserTimeout) { // This test checks that the parser rejects the negative value of the // tcp-user-timeout parameter. TEST_F(DbAccessParserTest, negativeTcpUserTimeout) { - const char* config[] = {"type", "memfile", - "name", "/opt/var/lib/kea/kea-leases6.csv", + const char* config[] = {"type", "postgresql", + "name", "keatest", "tcp-user-timeout", "-1", NULL}; @@ -543,8 +543,8 @@ TEST_F(DbAccessParserTest, negativeTcpUserTimeout) { // This test checks that the parser rejects a too large (greater than // the max uint32_t) value of the tcp-user-timeout parameter. TEST_F(DbAccessParserTest, largeTcpUserTimeout) { - const char* config[] = {"type", "memfile", - "name", "/opt/var/lib/kea/kea-leases6.csv", + const char* config[] = {"type", "postgresql", + "name", "keatest", "tcp-user-timeout", "4294967296", NULL}; @@ -556,6 +556,101 @@ TEST_F(DbAccessParserTest, largeTcpUserTimeout) { EXPECT_THROW(parser.parse(json_elements), DbConfigError); } +// This test verifies that specifying the tcp-user-timeout for the +// memfile backend is not allowed. +TEST_F(DbAccessParserTest, memfileTcpUserTimeout) { + const char* config[] = {"type", "memfile", + "name", "keatest", + "tcp-user-timeout", "10", + NULL}; + + string json_config = toJson(config); + ConstElementPtr json_elements = Element::fromJSON(json_config); + EXPECT_TRUE(json_elements); + + TestDbAccessParser parser; + EXPECT_THROW(parser.parse(json_elements), DbConfigError); +} + +// This test verifies that specifying the tcp-user-timeout for the +// mysql backend is not allowed. +TEST_F(DbAccessParserTest, mysqlTcpUserTimeout) { + const char* config[] = {"type", "mysql", + "name", "keatest", + "tcp-user-timeout", "10", + NULL}; + + string json_config = toJson(config); + ConstElementPtr json_elements = Element::fromJSON(json_config); + EXPECT_TRUE(json_elements); + + TestDbAccessParser parser; + EXPECT_THROW(parser.parse(json_elements), DbConfigError); +} + +// This test verifies that specifying the read-timeout for the +// memfile backend is not allowed. +TEST_F(DbAccessParserTest, memfileReadTimeout) { + const char* config[] = {"type", "memfile", + "name", "keatest", + "read-timeout", "10", + NULL}; + + string json_config = toJson(config); + ConstElementPtr json_elements = Element::fromJSON(json_config); + EXPECT_TRUE(json_elements); + + TestDbAccessParser parser; + EXPECT_THROW(parser.parse(json_elements), DbConfigError); +} + +// This test verifies that specifying the read-timeout for the +// postgresql backend is not allowed. +TEST_F(DbAccessParserTest, postgresqlReadTimeout) { + const char* config[] = {"type", "postgresql", + "name", "keatest", + "read-timeout", "10", + NULL}; + + string json_config = toJson(config); + ConstElementPtr json_elements = Element::fromJSON(json_config); + EXPECT_TRUE(json_elements); + + TestDbAccessParser parser; + EXPECT_THROW(parser.parse(json_elements), DbConfigError); +} + +// This test verifies that specifying the write-timeout for the +// memfile backend is not allowed. +TEST_F(DbAccessParserTest, memfileWriteTimeout) { + const char* config[] = {"type", "memfile", + "name", "keatest", + "write-timeout", "10", + NULL}; + + string json_config = toJson(config); + ConstElementPtr json_elements = Element::fromJSON(json_config); + EXPECT_TRUE(json_elements); + + TestDbAccessParser parser; + EXPECT_THROW(parser.parse(json_elements), DbConfigError); +} + +// This test verifies that specifying the write-timeout for the +// postgresql backend is not allowed. +TEST_F(DbAccessParserTest, postgresqlWriteTimeout) { + const char* config[] = {"type", "postgresql", + "name", "keatest", + "write-timeout", "10", + NULL}; + + string json_config = toJson(config); + ConstElementPtr json_elements = Element::fromJSON(json_config); + EXPECT_TRUE(json_elements); + + TestDbAccessParser parser; + EXPECT_THROW(parser.parse(json_elements), DbConfigError); +} // This test checks that the parser accepts the valid value of the // port parameter. |