From 868996c8c5e6df24e9d0cf6e30c531fb03d41056 Mon Sep 17 00:00:00 2001 From: Thomas Markwalder Date: Thu, 31 May 2018 14:30:49 -0400 Subject: [PATCH] [5629] Cql lease and host now validate schema version src/lib/dhcpsrv/cql_host_data_source.cc CqlHostDataSourceImpl::CqlHostDataSourceImpl() - added schema version validation src/lib/dhcpsrv/cql_lease_mgr.cc CqlLeaseMgr::CqlLeaseMgr() - added schema version validation --- src/lib/dhcpsrv/cql_host_data_source.cc | 14 +++++++++++++- src/lib/dhcpsrv/cql_host_data_source.h | 3 ++- src/lib/dhcpsrv/cql_lease_mgr.cc | 17 ++++++++++++++++- src/lib/dhcpsrv/cql_lease_mgr.h | 3 ++- src/lib/dhcpsrv/mysql_host_data_source.cc | 2 +- src/lib/dhcpsrv/pgsql_host_data_source.cc | 7 +++++-- src/lib/dhcpsrv/pgsql_lease_mgr.cc | 7 +++++-- 7 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/lib/dhcpsrv/cql_host_data_source.cc b/src/lib/dhcpsrv/cql_host_data_source.cc index 5499848024..2ba860bd3b 100644 --- a/src/lib/dhcpsrv/cql_host_data_source.cc +++ b/src/lib/dhcpsrv/cql_host_data_source.cc @@ -1612,9 +1612,21 @@ CqlHostDataSourceImpl::CqlHostDataSourceImpl(const CqlConnection::ParameterMap& // Open the database. dbconn_.openDatabase(); + // Prepare the version exchange first. + dbconn_.prepareStatements(CqlVersionExchange::tagged_statements_); + + // Validate the schema version. + std::pair code_version(CQL_SCHEMA_VERSION_MAJOR, + CQL_SCHEMA_VERSION_MINOR); + std::pair db_version = getVersion(); + if (code_version != db_version) { + isc_throw(DbOpenError, "Cassandra schema version mismatch: need version: " + << code_version.first << "." << code_version.second + << " found version: " << db_version.first << "."); + } + // Prepare all possible statements. dbconn_.prepareStatements(CqlHostExchange::tagged_statements_); - dbconn_.prepareStatements(CqlVersionExchange::tagged_statements_); } CqlHostDataSourceImpl::~CqlHostDataSourceImpl() { diff --git a/src/lib/dhcpsrv/cql_host_data_source.h b/src/lib/dhcpsrv/cql_host_data_source.h index 75d4d45a1d..6208eae528 100644 --- a/src/lib/dhcpsrv/cql_host_data_source.h +++ b/src/lib/dhcpsrv/cql_host_data_source.h @@ -78,7 +78,8 @@ public: /// concerned with the database. /// /// @throw isc::dhcp::NoDatabaseName Mandatory database name not given - /// @throw isc::dhcp::DbOpenError Error opening the database + /// @throw isc::dhcp::DbOpenError Error opening the database or if the + /// schema version is invalid. /// @throw isc::dhcp::DbOperationError An operation on the open database has /// failed. explicit CqlHostDataSource(const DatabaseConnection::ParameterMap& parameters); diff --git a/src/lib/dhcpsrv/cql_lease_mgr.cc b/src/lib/dhcpsrv/cql_lease_mgr.cc index 0b10becfe1..b0ddeca159 100644 --- a/src/lib/dhcpsrv/cql_lease_mgr.cc +++ b/src/lib/dhcpsrv/cql_lease_mgr.cc @@ -1854,9 +1854,24 @@ CqlLeaseStatsQuery::executeSelect(const CqlConnection& connection, const AnyArra CqlLeaseMgr::CqlLeaseMgr(const DatabaseConnection::ParameterMap ¶meters) : LeaseMgr(), dbconn_(parameters) { dbconn_.openDatabase(); + + // Prepare the version exchange first. + dbconn_.prepareStatements(CqlVersionExchange::tagged_statements_); + + // Validate the schema version. + std::pair code_version(CQL_SCHEMA_VERSION_MAJOR, + CQL_SCHEMA_VERSION_MINOR); + std::pair db_version = getVersion(); + if (code_version != db_version) { + isc_throw(DbOpenError, + "Cassandra schema version mismatch: need version: " + << code_version.first << "." << code_version.second + << " found version: " << db_version.first << "."); + } + + // Now prepare the rest of the exchanges. dbconn_.prepareStatements(CqlLease4Exchange::tagged_statements_); dbconn_.prepareStatements(CqlLease6Exchange::tagged_statements_); - dbconn_.prepareStatements(CqlVersionExchange::tagged_statements_); dbconn_.prepareStatements(CqlLeaseStatsQuery::tagged_statements_); } diff --git a/src/lib/dhcpsrv/cql_lease_mgr.h b/src/lib/dhcpsrv/cql_lease_mgr.h index ecd47864a7..69adf64500 100644 --- a/src/lib/dhcpsrv/cql_lease_mgr.h +++ b/src/lib/dhcpsrv/cql_lease_mgr.h @@ -66,7 +66,8 @@ public: /// concerned with the database. /// /// @throw isc::dhcp::NoDatabaseName Mandatory database name not given - /// @throw isc::dhcp::DbOpenError Error opening the database + /// @throw isc::dhcp::DbOpenError Error opening the database or the schema + /// version is invalid. /// @throw isc::dhcp::DbOperationError An operation on the open database has /// failed. explicit CqlLeaseMgr(const DatabaseConnection::ParameterMap& parameters); diff --git a/src/lib/dhcpsrv/mysql_host_data_source.cc b/src/lib/dhcpsrv/mysql_host_data_source.cc index c4dfae044b..537c0cb637 100644 --- a/src/lib/dhcpsrv/mysql_host_data_source.cc +++ b/src/lib/dhcpsrv/mysql_host_data_source.cc @@ -2305,7 +2305,7 @@ MySqlHostDataSourceImpl(const MySqlConnection::ParameterMap& parameters) // Test schema version before we try to prepare statements. std::pair code_version(MYSQL_SCHEMA_VERSION_MAJOR, - MYSQL_SCHEMA_VERSION_MINOR); + MYSQL_SCHEMA_VERSION_MINOR); std::pair db_version = getVersion(); if (code_version != db_version) { isc_throw(DbOpenError, "MySQL schema version mismatch: need version: " diff --git a/src/lib/dhcpsrv/pgsql_host_data_source.cc b/src/lib/dhcpsrv/pgsql_host_data_source.cc index 91a207efbf..5213f86367 100644 --- a/src/lib/dhcpsrv/pgsql_host_data_source.cc +++ b/src/lib/dhcpsrv/pgsql_host_data_source.cc @@ -1700,8 +1700,10 @@ PgSqlHostDataSourceImpl(const PgSqlConnection::ParameterMap& parameters) // Open the database. conn_.openDatabase(); - pair code_version(PG_SCHEMA_VERSION_MAJOR, PG_SCHEMA_VERSION_MINOR); - pair db_version = getVersion(); + // Validate the schema version first. + std::pair code_version(PG_SCHEMA_VERSION_MAJOR, + PG_SCHEMA_VERSION_MINOR); + std::pair db_version = getVersion(); if (code_version != db_version) { isc_throw(DbOpenError, "PostgreSQL schema version mismatch: need version: " @@ -1710,6 +1712,7 @@ PgSqlHostDataSourceImpl(const PgSqlConnection::ParameterMap& parameters) << db_version.second); } + // Now prepare the SQL statements. conn_.prepareStatements(tagged_statements.begin(), tagged_statements.begin() + WRITE_STMTS_BEGIN); diff --git a/src/lib/dhcpsrv/pgsql_lease_mgr.cc b/src/lib/dhcpsrv/pgsql_lease_mgr.cc index 7e368db778..f6e2a18434 100644 --- a/src/lib/dhcpsrv/pgsql_lease_mgr.cc +++ b/src/lib/dhcpsrv/pgsql_lease_mgr.cc @@ -996,8 +996,10 @@ PgSqlLeaseMgr::PgSqlLeaseMgr(const DatabaseConnection::ParameterMap& parameters) exchange6_(new PgSqlLease6Exchange()), conn_(parameters) { conn_.openDatabase(); - pair code_version(PG_SCHEMA_VERSION_MAJOR, PG_SCHEMA_VERSION_MINOR); - pair db_version = getVersion(); + // Validate schema version first. + std::pair code_version(PG_SCHEMA_VERSION_MAJOR, + PG_SCHEMA_VERSION_MINOR); + std::pair db_version = getVersion(); if (code_version != db_version) { isc_throw(DbOpenError, "PostgreSQL schema version mismatch: need version: " @@ -1006,6 +1008,7 @@ PgSqlLeaseMgr::PgSqlLeaseMgr(const DatabaseConnection::ParameterMap& parameters) << db_version.second); } + // Now prepare the SQL statements. int i = 0; for( ; tagged_statements[i].text != NULL ; ++i) { conn_.prepareStatement(tagged_statements[i]); -- 2.47.2