From 2ff46b2dd59e37fb0a7bf02107bbfd5e1df7eeb4 Mon Sep 17 00:00:00 2001 From: mayya Date: Mon, 16 Jul 2018 23:55:03 +0200 Subject: [PATCH] [extend_host_struct] Fix review comments --- AUTHORS | 1 + ChangeLog | 5 ++++ src/bin/admin/tests/mysql_tests.sh.in | 6 ++--- src/bin/admin/tests/pgsql_tests.sh.in | 2 +- src/lib/dhcpsrv/dhcpsrv_messages.mes | 4 +++ src/lib/dhcpsrv/host.cc | 11 ++++---- src/lib/dhcpsrv/host.h | 26 ++++++++----------- src/lib/dhcpsrv/memfile_lease_mgr.cc | 14 ++++++++++ src/lib/dhcpsrv/mysql_host_data_source.cc | 3 +-- src/lib/dhcpsrv/mysql_lease_mgr.cc | 2 +- src/lib/dhcpsrv/pgsql_host_data_source.cc | 2 +- .../tests/alloc_engine_expiration_unittest.cc | 2 +- src/lib/dhcpsrv/tests/host_unittest.cc | 2 ++ .../testutils/host_data_source_utils.cc | 12 ++++----- .../testutils/host_data_source_utils.h | 2 +- 15 files changed, 57 insertions(+), 37 deletions(-) diff --git a/AUTHORS b/AUTHORS index 3dde830c38..f76dd4fbda 100644 --- a/AUTHORS +++ b/AUTHORS @@ -171,6 +171,7 @@ We have received the following contributions: - Sunil Mayya 2018-07: support for Authentication option in DHCPv6 + 2018-07: support storage of Authentication keys in host structure Kea uses log4cplus (http://sourceforge.net/projects/log4cplus/) for logging, Boost (http://www.boost.org/) library for almost everything, and can use Botan diff --git a/ChangeLog b/ChangeLog index 55a15670cc..8ab7e2285d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +1434. [func] MayyaSunil + Code added to support storage of Authentication key host reservation. + There is no way to use the code yet. + (Github #88, git tbd) + 1433. [func] marcin Implemented state HA state machine pausing in the high availability hooks library. diff --git a/src/bin/admin/tests/mysql_tests.sh.in b/src/bin/admin/tests/mysql_tests.sh.in index 259291dec2..a8dcf2b7cd 100644 --- a/src/bin/admin/tests/mysql_tests.sh.in +++ b/src/bin/admin/tests/mysql_tests.sh.in @@ -165,7 +165,7 @@ EOF # Second table: hosts mysql -u$db_user -p$db_password $db_name >/dev/null 2>&1 < 5.0) # verify that hosts table has columns holding values for DHCPv4 fixed fields - qry="select dhcp4_next_server, dhcp4_server_hostname, dhcp4_boot_file_name from hosts"; + qry="select dhcp4_next_server, dhcp4_server_hostname, dhcp4_boot_file_name, auth_key from hosts"; count=`mysql_execute "${qry}"` ERRCODE=$? - assert_eq 0 $ERRCODE "select dhcp4_next_server, dhcp4_server_hostname, dhcp4_boot_file_name failed. (expected status code %d, returned %d)" + assert_eq 0 $ERRCODE "select dhcp4_next_server, dhcp4_server_hostname, dhcp4_boot_file_name, auth_key failed. (expected status code %d, returned %d)" # verify that dhcp4_subnet_id is unsigned qry="show columns from hosts like 'dhcp4_subnet_id'" diff --git a/src/bin/admin/tests/pgsql_tests.sh.in b/src/bin/admin/tests/pgsql_tests.sh.in index 553cc0699f..0584ff2012 100644 --- a/src/bin/admin/tests/pgsql_tests.sh.in +++ b/src/bin/admin/tests/pgsql_tests.sh.in @@ -165,7 +165,7 @@ pgsql_upgrade_2_0_to_3_0() { assert_eq 0 $ERRCODE "lease_hwaddr_source table is missing or broken. (expected status code %d, returned %d)" # Added hosts table - output=`pgsql_execute "select host_id, dhcp_identifier, dhcp_identifier_type, dhcp4_subnet_id, dhcp6_subnet_id, ipv4_address, hostname, dhcp4_client_classes, dhcp6_client_classes, dhcp4_next_server, dhcp4_server_hostname, dhcp4_boot_file_name from hosts;"` + output=`pgsql_execute "select host_id, dhcp_identifier, dhcp_identifier_type, dhcp4_subnet_id, dhcp6_subnet_id, ipv4_address, hostname, dhcp4_client_classes, dhcp6_client_classes, dhcp4_next_server, dhcp4_server_hostname, dhcp4_boot_file_name, auth_key from hosts;"` ERRCODE=$? assert_eq 0 $ERRCODE "hosts table is missing or broken. (expected status code %d, returned %d)" diff --git a/src/lib/dhcpsrv/dhcpsrv_messages.mes b/src/lib/dhcpsrv/dhcpsrv_messages.mes index 5801d9825f..edf22403c7 100644 --- a/src/lib/dhcpsrv/dhcpsrv_messages.mes +++ b/src/lib/dhcpsrv/dhcpsrv_messages.mes @@ -497,6 +497,10 @@ of leases beginning with the specified address. A debug message issued when the server is attempting to obtain all IPv6 leases from the memory file database. +% DHCPSRV_MEMFILE_GET6_DUID obtaining IPv6 leases for DUID %1 +A debug message issued when the server is attempting to obtain IPv6 +leases from the memory file database for the DUID. + % DHCPSRV_MEMFILE_GET_ADDR4 obtaining IPv4 lease for address %1 A debug message issued when the server is attempting to obtain an IPv4 lease from the memory file database for the specified address. diff --git a/src/lib/dhcpsrv/host.cc b/src/lib/dhcpsrv/host.cc index 4ec23c0d81..0244f23aea 100644 --- a/src/lib/dhcpsrv/host.cc +++ b/src/lib/dhcpsrv/host.cc @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -47,9 +48,9 @@ AuthKey::getRandomKeyString() { std::string AuthKey::ToText() const { - std::ostringstream s; - //s << authKey_; - return (authKey_); + //this will need enhancement if the stored container is not + //string + return authKey_; } void @@ -146,7 +147,7 @@ Host::Host(const uint8_t* identifier, const size_t identifier_len, server_host_name_(server_host_name), boot_file_name_(boot_file_name), host_id_(0), cfg_option4_(new CfgOption()), cfg_option6_(new CfgOption()), negative_(false), - key_(auth_key){ + key_(auth_key) { // Initialize host identifier. setIdentifier(identifier, identifier_len, identifier_type); @@ -182,7 +183,7 @@ Host::Host(const std::string& identifier, const std::string& identifier_name, server_host_name_(server_host_name), boot_file_name_(boot_file_name), host_id_(0), cfg_option4_(new CfgOption()), cfg_option6_(new CfgOption()), negative_(false), - key_(auth_key){ + key_(auth_key) { // Initialize host identifier. setIdentifier(identifier, identifier_name); diff --git a/src/lib/dhcpsrv/host.h b/src/lib/dhcpsrv/host.h index 065df92400..06a72795fd 100644 --- a/src/lib/dhcpsrv/host.h +++ b/src/lib/dhcpsrv/host.h @@ -35,13 +35,11 @@ class AuthKey { public: /// @brief Length of the key - 128 bits const static uint8_t KEY_LEN = 16; - + /// @brief Constructor /// /// Constructor for assigning auth keys in host reservation - /// Throws if the key length is greater than 16 bytes - /// appends 0 to the the input if the key value is less - /// than 16 bytes. Provide flexibility to store initialise the value as a string or a vector of bytes. + /// Ensures the key length is not greater than 16 bytes /// @param string auth key to be stored AuthKey(const std::string key); @@ -50,20 +48,20 @@ public: /// Constructor for generating auth keys, with no argument /// shall use the internal function for generationg random keys AuthKey(void); - + // @brief get random string /// - /// Random string is generated by default will be used for + /// Random string is generated by default will be used for /// the keys to be used for signing Reconfigure Message. /// @return auth keys /// @todo Move randomization function to cryptolink - static std::string getRandomKeyString(); + static std::string getRandomKeyString(); /// @brief set auth key value /// - /// Set the key value. If the value is less than 16 bytes - /// append 0. If the size is greater than 16 bytes, we shall - /// throw an exception + /// Set the key value. + // If the size is greater than 16 bytes, we resize to 16 Bytes + /// Doesnt throw an exception /// @param string key to be stored void setAuthKey(const std::string& key); @@ -73,13 +71,13 @@ public: std::string getAuthKey() { return authKey_; } - + /// @brief return text format for keys /// /// Although returning member would have sufficed /// this is added incase in future authkey is no longer std::string std::string ToText() const; - + /// /// @brief equality operator /// @@ -645,7 +643,6 @@ public: /// @brief sets key. /// /// Keys are used for signing the Reconfigure Message. - /// The accessor should ensure 128 characters void setKey(const AuthKey& key) { key_ = key; } @@ -696,7 +693,7 @@ private: std::string server_host_name_; /// @brief Boot file name (a.k.a. file, carried in DHCPv4 message) std::string boot_file_name_; - + /// @brief HostID (a unique identifier assigned when the host is stored in /// MySQL, PostgreSQL or Cassandra) uint64_t host_id_; @@ -719,7 +716,6 @@ private: // While sending reconfigure message authentication field /// shall contain MD5 hash computed using this key. AuthKey key_; - }; /// @brief Pointer to the @c Host object. diff --git a/src/lib/dhcpsrv/memfile_lease_mgr.cc b/src/lib/dhcpsrv/memfile_lease_mgr.cc index c1c84b8aea..dd04ef3dac 100644 --- a/src/lib/dhcpsrv/memfile_lease_mgr.cc +++ b/src/lib/dhcpsrv/memfile_lease_mgr.cc @@ -1015,6 +1015,20 @@ Memfile_LeaseMgr::getLeases6() const { return (collection); } +Lease6Collection +Memfile_LeaseMgr::getLeases6(const DUID& duid) const { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_MEMFILE_GET6_DUID) + .arg(duid.toText()); + + Lease6Collection collection; + for (auto lease = storage6_.begin(); lease != storage6_.end(); ++lease ) { + if ( (**lease).duid_->getDuid() == duid.getDuid() ) + collection.push_back(Lease6Ptr(new Lease6(**lease))); + } + + return (collection); +} + Lease6Collection Memfile_LeaseMgr::getLeases6(const asiolink::IOAddress& lower_bound_address, const LeasePageSize& page_size) const { diff --git a/src/lib/dhcpsrv/mysql_host_data_source.cc b/src/lib/dhcpsrv/mysql_host_data_source.cc index e4bc90aaf8..adb6b3f651 100644 --- a/src/lib/dhcpsrv/mysql_host_data_source.cc +++ b/src/lib/dhcpsrv/mysql_host_data_source.cc @@ -145,7 +145,7 @@ public: dhcp4_next_server_null_(MLM_FALSE), dhcp4_server_hostname_null_(MLM_FALSE), dhcp4_boot_file_name_null_(MLM_FALSE), - auth_key_null_(MLM_FALSE) { + auth_key_null_(MLM_FALSE) { // Fill arrays with 0 so as they don't include any garbage. memset(dhcp_identifier_buffer_, 0, sizeof(dhcp_identifier_buffer_)); @@ -1430,7 +1430,6 @@ public: ipv6_address_buffer_[ipv6_address_buffer_len_] = '\0'; std::string address = ipv6_address_buffer_; - IPv6Resrv r(type, IOAddress(address), prefix_len_); return (r); }; diff --git a/src/lib/dhcpsrv/mysql_lease_mgr.cc b/src/lib/dhcpsrv/mysql_lease_mgr.cc index 663bc0c8c9..83a2710ca5 100644 --- a/src/lib/dhcpsrv/mysql_lease_mgr.cc +++ b/src/lib/dhcpsrv/mysql_lease_mgr.cc @@ -184,7 +184,7 @@ tagged_statements = { { "state, user_context " "FROM lease6 " "WHERE address = ? AND lease_type = ?"}, - {MySqlLeaseMgr::GET_LEASE6_DUID_IAID, + {MySqlLeaseMgr::GET_LEASE6_DUID_IAID, "SELECT address, duid, valid_lifetime, " "expire, subnet_id, pref_lifetime, " "lease_type, iaid, prefix_len, " diff --git a/src/lib/dhcpsrv/pgsql_host_data_source.cc b/src/lib/dhcpsrv/pgsql_host_data_source.cc index efa2c00b0c..02e2048601 100644 --- a/src/lib/dhcpsrv/pgsql_host_data_source.cc +++ b/src/lib/dhcpsrv/pgsql_host_data_source.cc @@ -1646,7 +1646,7 @@ TaggedStatementArray tagged_statements = { { //PgSqlHostDataSourceImpl::INSERT_V6_RESRV // Inserts a single IPv6 reservation into 'reservations' table. {5, - { OID_VARCHAR, OID_INT2, OID_INT4, OID_INT4, OID_INT4}, + { OID_VARCHAR, OID_INT2, OID_INT4, OID_INT4, OID_INT4 }, "insert_v6_resrv", "INSERT INTO ipv6_reservations(address, prefix_len, type, " " dhcp6_iaid, host_id) " diff --git a/src/lib/dhcpsrv/tests/alloc_engine_expiration_unittest.cc b/src/lib/dhcpsrv/tests/alloc_engine_expiration_unittest.cc index 49d471b527..997fe9549b 100644 --- a/src/lib/dhcpsrv/tests/alloc_engine_expiration_unittest.cc +++ b/src/lib/dhcpsrv/tests/alloc_engine_expiration_unittest.cc @@ -1590,7 +1590,7 @@ TEST_F(ExpirationAllocEngine6Test, reclaimDeclinedStats) { // This test verifies that expired leases are reclaimed before they are // allocated to another client sending a Request message. -/TEST_F(ExpirationAllocEngine6Test, reclaimReusedLeases) { +TEST_F(ExpirationAllocEngine6Test, reclaimReusedLeases) { testReclaimReusedLeases(DHCPV6_REQUEST, false); } diff --git a/src/lib/dhcpsrv/tests/host_unittest.cc b/src/lib/dhcpsrv/tests/host_unittest.cc index be5af1bc58..4847e2a779 100644 --- a/src/lib/dhcpsrv/tests/host_unittest.cc +++ b/src/lib/dhcpsrv/tests/host_unittest.cc @@ -1277,10 +1277,12 @@ TEST(AuthKeyTest, basicTest) { defaultTestKey.setAuthKey(key16ByteStr); ASSERT_EQ(16, defaultTestKey.getAuthKey().size()); ASSERT_EQ(key16ByteStr, defaultTestKey.getAuthKey()); + ASSERT_EQ(key16ByteStr, defaultTestKey.ToText()); defaultTestKey.setAuthKey(key18ByteStr); ASSERT_EQ(16, defaultTestKey.getAuthKey().size()); ASSERT_EQ(key16ByteStr, defaultTestKey.getAuthKey()); + ASSERT_EQ(key16ByteStr, defaultTestKey.ToText()); } diff --git a/src/lib/dhcpsrv/testutils/host_data_source_utils.cc b/src/lib/dhcpsrv/testutils/host_data_source_utils.cc index 85b2cad620..d76038b167 100644 --- a/src/lib/dhcpsrv/testutils/host_data_source_utils.cc +++ b/src/lib/dhcpsrv/testutils/host_data_source_utils.cc @@ -81,7 +81,7 @@ HostDataSourceUtils::initializeHost4(const std::string& address, } HostPtr -HostDataSourceUtils::initializeHost6(const std::string address, +HostDataSourceUtils::initializeHost6(std::string address, Host::IdentifierType identifier, bool prefix, bool new_identifier, @@ -107,13 +107,11 @@ HostDataSourceUtils::initializeHost6(const std::string address, ++subnet4; ++subnet6; - std::string default_string; HostPtr host(new Host(&ident[0], ident.size(), identifier, subnet4, subnet6, - IOAddress("0.0.0.0"), default_string, - default_string, default_string, - asiolink:: IOAddress::IPV4_ZERO_ADDRESS(), default_string, default_string, - AuthKey(auth_key))); - + IOAddress("0.0.0.0"))); + + host->setKey(AuthKey(auth_key)); + if (!prefix) { // Create IPv6 reservation (for an address) IPv6Resrv resv(IPv6Resrv::TYPE_NA, IOAddress(address), 128); diff --git a/src/lib/dhcpsrv/testutils/host_data_source_utils.h b/src/lib/dhcpsrv/testutils/host_data_source_utils.h index 7b1b7789db..a252eeb0dc 100644 --- a/src/lib/dhcpsrv/testutils/host_data_source_utils.h +++ b/src/lib/dhcpsrv/testutils/host_data_source_utils.h @@ -38,7 +38,7 @@ public: /// identifier should be generated or the same as previously. /// /// @return generated Host object - static HostPtr initializeHost6(const std::string address, Host::IdentifierType id, + static HostPtr initializeHost6(std::string address, Host::IdentifierType id, bool prefix, bool new_identifier = true, const std::string key = ""); /// @brief Generates a hardware address in text version. -- 2.47.2