]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2869] Moved to inet in PostgreSQL
authorFrancis Dupont <fdupont@isc.org>
Wed, 5 Jul 2023 22:28:41 +0000 (00:28 +0200)
committerFrancis Dupont <fdupont@isc.org>
Thu, 6 Jul 2023 20:17:20 +0000 (22:17 +0200)
src/bin/admin/tests/pgsql_tests.sh.in
src/lib/dhcpsrv/pgsql_lease_mgr.cc
src/share/database/scripts/pgsql/dhcpdb_create.pgsql
src/share/database/scripts/pgsql/upgrade_018_to_019.sh.in

index 08d6b0deb09a32b661ef326c8fe3f97c05196346..32d37e6ba7a5252ac711fc1815cccbfc73c75e2b 100644 (file)
@@ -869,11 +869,26 @@ pgsql_upgrade_17_to_18_test() {
 }
 
 pgsql_upgrade_18_to_19_test() {
-    # The function, pgsql_ddns_conflict_resolution_mode_update_test, is called explicitly
-    # and tests conversion of ddns-use-conflict-resolution to ddns-conflict-resolution-mode.
+    # The function, pgsql_ddns_conflict_resolution_mode_update_test,
+    # is called explicitly and tests conversion of
+    # ddns-use-conflict-resolution to ddns-conflict-resolution-mode.
     # For now this function only verifies version number.
     version=$("${kea_admin}" db-version pgsql -u "${db_user}" -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}")
     assert_str_eq "19.0" "${version}" 'Expected kea-admin to return %s, returned value was %s'
+
+    # Verify that lease6_relay_id lease_addr is binary.
+    qry="insert into lease6_relay_id(relay_id,lease_addr) values('18219',cast('3001::99' as inet));"
+    run_statement "lease6_relay_id_insert" "$qry"
+
+    qry="select host(lease_addr) from lease6_relay_id where relay_id = '18219';"
+    run_statement "lease6_relay_id_get" "$qry" "3001::99"
+
+    # Verify that lease6_remote_id lease_addr is binary.
+    qry="insert into lease6_remote_id(remote_id,lease_addr) values('18219',cast('3001::99' as inet));"
+    run_statement "lease6_remote_id_insert" "$qry"
+
+    qry="select host(lease_addr) from lease6_remote_id where remote_id = '18219';"
+    run_statement "lease6_remote_id_get" "$qry" "3001::99"
 }
 
 pgsql_upgrade_test() {
index a52f5d7355064a20144b8ec25195d01fb1401789..93d282fd734aefc0bb437716c961c7def530bac9 100644 (file)
@@ -576,56 +576,58 @@ PgSqlTaggedStatement tagged_statements[] = {
       "DELETE FROM lease6_remote_id" },
 
     // DELETE_RELAY_ID6
-    { 1, { OID_BYTEA },
+    { 1, { OID_VARCHAR },
       "delete_relay_id6",
-      "DELETE FROM lease6_relay_id WHERE lease_addr = $1" },
+      "DELETE FROM lease6_relay_id WHERE lease_addr = cast($1 as inet)" },
 
     // DELETE_REMOTE_ID6
-    { 1, { OID_BYTEA },
+    { 1, { OID_VARCHAR },
       "delete_remote_id6",
-      "DELETE FROM lease6_remote_id WHERE lease_addr = $1" },
+      "DELETE FROM lease6_remote_id WHERE lease_addr = cast($1 as inet)" },
 
     // ADD_RELAY_ID6
-    { 2, { OID_BYTEA, OID_BYTEA },
+    { 2, { OID_BYTEA, OID_VARCHAR },
       "add_relay_id6",
       "INSERT INTO lease6_relay_id(relay_id, lease_addr) "
-      "VALUES ($1, $2)" },
+      "VALUES ($1, cast($2 as inet))" },
 
     // ADD_REMOTE_ID6
-    { 2, { OID_BYTEA, OID_BYTEA },
+    { 2, { OID_BYTEA, OID_VARCHAR },
       "add_remote_id6",
       "INSERT INTO lease6_remote_id(remote_id, lease_addr) "
-      "VALUES ($1, $2)" },
+      "VALUES ($1, cast($2 as inet))" },
 
     // GET_RELAY_ID6
-    { 3, { OID_BYTEA, OID_BYTEA, OID_INT8 },
+    { 3, { OID_BYTEA, OID_VARCHAR, OID_INT8 },
       "get_relay_id6",
-      "SELECT lease_addr FROM lease6_relay_id "
-      "WHERE relay_id = $1 AND lease_addr > $2 "
+      "SELECT host(lease_addr) FROM lease6_relay_id "
+      "WHERE relay_id = $1 AND lease_addr > cast($2 as inet) "
       "ORDER BY lease_addr "
       "LIMIT $3" },
 
     // GET_REMOTE_ID6
-    { 3, { OID_BYTEA, OID_BYTEA, OID_INT8 },
+    { 3, { OID_BYTEA, OID_VARCHAR, OID_INT8 },
       "get_remote_id6",
-      "SELECT lease_addr FROM lease6_remote_id "
-      "WHERE remote_id = $1 AND lease_addr > $2 "
+      "SELECT host(lease_addr) FROM lease6_remote_id "
+      "WHERE remote_id = $1 AND lease_addr > cast($2 as inet) "
       "ORDER BY lease_addr "
       "LIMIT $3" },
 
     // GET_RELAY_ID6_LINK
-    { 4, { OID_BYTEA, OID_BYTEA, OID_BYTEA, OID_INT8 },
+    { 4, { OID_BYTEA, OID_VARCHAR, OID_VARCHAR, OID_INT8 },
       "get_relay_id6_link",
-      "SELECT lease_addr FROM lease6_relay_id "
-      "WHERE relay_id = $1 AND lease_addr BETWEEN $2 and $3 "
+      "SELECT host(lease_addr) FROM lease6_relay_id "
+      "WHERE relay_id = $1 AND lease_addr "
+        "BETWEEN cast($2 as inet) and cast($3 as inet) "
       "ORDER BY lease_addr "
       "LIMIT $4" },
 
     // GET_REMOTE_ID6_LINK
-    { 4, { OID_BYTEA, OID_BYTEA, OID_BYTEA, OID_INT8 },
+    { 4, { OID_BYTEA, OID_VARCHAR, OID_VARCHAR, OID_INT8 },
       "get_remote_id6_link",
-      "SELECT lease_addr FROM lease6_remote_id "
-      "WHERE remote_id = $1 AND lease_addr BETWEEN $2 and $3 "
+      "SELECT host(lease_addr) FROM lease6_remote_id "
+      "WHERE remote_id = $1 AND lease_addr "
+        "BETWEEN cast($2 as inet) and cast($3 as inet) "
       "ORDER BY lease_addr "
       "LIMIT $4" },
 
@@ -3029,8 +3031,7 @@ PgSqlLeaseMgr::deleteRelayId6(const IOAddress& addr) {
     // Set up the WHERE clause value.
     PsqlBindArray bind_array;
 
-    std::vector<uint8_t> addr_data = addr.toBytes();
-    // Do not check the address length as it does not really matter.
+    std::string addr_data = addr.toText();
     bind_array.add(addr_data);
 
     // Get a context.
@@ -3058,8 +3059,7 @@ PgSqlLeaseMgr::deleteRemoteId6(const IOAddress& addr) {
     // Set up the WHERE clause value.
     PsqlBindArray bind_array;
 
-    std::vector<uint8_t> addr_data = addr.toBytes();
-    // Do not check the address length as it does not really matter.
+    std::string addr_data = addr.toText();
     bind_array.add(addr_data);
 
     // Get a context.
@@ -3095,10 +3095,7 @@ PgSqlLeaseMgr::addRelayId6(const IOAddress& lease_addr,
     bind_array.add(relay_id);
 
     // Bind the lease address.
-    std::vector<uint8_t> lease_addr_data = lease_addr.toBytes();
-    if (lease_addr_data.size() != 16) {
-        isc_throw(DbOperationError, "lease6 address is not 16 bytes long");
-    }
+    std::string lease_addr_data = lease_addr.toText();
     bind_array.add(lease_addr_data);
 
     // Get a context.
@@ -3134,10 +3131,7 @@ PgSqlLeaseMgr::addRemoteId6(const IOAddress& lease_addr,
     bind_array.add(remote_id);
 
     // Bind the lease address.
-    std::vector<uint8_t> lease_addr_data = lease_addr.toBytes();
-    if (lease_addr_data.size() != 16) {
-        isc_throw(DbOperationError, "lease6 address is not 16 bytes long");
-    }
+    std::string lease_addr_data = lease_addr.toText();
     bind_array.add(lease_addr_data);
 
     // Get a context.
@@ -3459,12 +3453,8 @@ PgSqlLeaseMgr::getExtendedInfo6Common(PgSqlLeaseContextPtr& ctx,
     int rows = PQntuples(r);
     std::list<IOAddress> result;
     for (int i = 0; i < rows; ++i) {
-        std::vector<uint8_t> addr_data;
-        PgSqlLeaseExchange::convertFromBytea(r, i, 0, addr_data);
-        if (addr_data.size() != 16) {
-            isc_throw(BadValue, "received lease6 address is not 16 bytes long");
-        }
-        result.push_back(IOAddress::fromBytes(AF_INET6, &addr_data[0]));
+        IOAddress addr(PgSqlLeaseExchange::getIPv6Value(r, i, 0));
+        result.push_back(addr);
     }
     return (result);
 }
@@ -3519,10 +3509,7 @@ PgSqlLeaseMgr::getLeases6ByRelayId(const DUID& relay_id,
         bind_array.add(relay_id_data);
 
         // Bind the lower bound address.
-        std::vector<uint8_t> lb_addr_data = lower_bound_address.toBytes();
-        if (lb_addr_data.size() != 16) {
-            isc_throw(DbOperationError, "lower bound address is not 16 bytes long");
-        }
+        std::string lb_addr_data = lower_bound_address.toText();
         bind_array.add(lb_addr_data);
 
         // Bind page size value.
@@ -3552,17 +3539,11 @@ PgSqlLeaseMgr::getLeases6ByRelayId(const DUID& relay_id,
         bind_array.add(relay_id_data);
 
         // Bind the start address.
-        std::vector<uint8_t> start_addr_data = start_addr.toBytes();
-        if (start_addr_data.size() != 16) {
-            isc_throw(DbOperationError, "start address is not 16 bytes long");
-        }
+        std::string start_addr_data = start_addr.toText();
         bind_array.add(start_addr_data);
 
         // Bind the last address.
-        std::vector<uint8_t> last_addr_data = last_addr.toBytes();
-        if (last_addr_data.size() != 16) {
-            isc_throw(DbOperationError, "last address is not 16 bytes long");
-        }
+        std::string last_addr_data = last_addr.toText();
         bind_array.add(last_addr_data);
 
         // Bind page size value.
@@ -3654,10 +3635,7 @@ PgSqlLeaseMgr::getLeases6ByRemoteId(const OptionBuffer& remote_id,
         bind_array.add(remote_id);
 
         // Bind the lower bound address.
-        std::vector<uint8_t> lb_addr_data = lower_bound_address.toBytes();
-        if (lb_addr_data.size() != 16) {
-            isc_throw(DbOperationError, "lower bound address is not 16 bytes long");
-        }
+        std::string lb_addr_data = lower_bound_address.toText();
         bind_array.add(lb_addr_data);
 
         // Bind page size value.
@@ -3687,17 +3665,11 @@ PgSqlLeaseMgr::getLeases6ByRemoteId(const OptionBuffer& remote_id,
         bind_array.add(remote_id);
 
         // Bind the start address.
-        std::vector<uint8_t> start_addr_data = start_addr.toBytes();
-        if (start_addr_data.size() != 16) {
-            isc_throw(DbOperationError, "start address is not 16 bytes long");
-        }
+        std::string start_addr_data = start_addr.toText();
         bind_array.add(start_addr_data);
 
         // Bind the last address.
-        std::vector<uint8_t> last_addr_data = last_addr.toBytes();
-        if (last_addr_data.size() != 16) {
-            isc_throw(DbOperationError, "last address is not 16 bytes long");
-        }
+        std::string last_addr_data = last_addr.toText();
         bind_array.add(last_addr_data);
 
         // Bind page size value.
index c5f488329b81588318e13b673ce2eb4f40e6b955..843893741246d3a821d16fd18ff4b22556cbd1a7 100644 (file)
@@ -6284,6 +6284,18 @@ UPDATE dhcp6_global_parameter
 SET name = 'ddns-conflict-resolution-mode', value = 'no-check-with-dhcid', parameter_type = 4
 WHERE name = 'ddns-use-conflict-resolution' and value = 'false';
 
+-- Clear lease6_relay_id table.
+DELETE FROM lease6_relay_id;
+
+-- Change lease6_relay_id:lease_addr to INET.
+ALTER TABLE lease6_relay_id ALTER COLUMN lease_addr TYPE INET USING '::';
+
+-- Clear lease6_remote_id table.
+DELETE FROM lease6_remote_id;
+
+-- Change lease6_remote_id:lease_addr to INET.
+ALTER TABLE lease6_remote_id ALTER COLUMN lease_addr TYPE INET USING '::';
+
 -- Update the schema version number.
 UPDATE schema_version
     SET version = '19', minor = '0';
index 088fcd2cf6c46eab1d41e2c59de68f6dbf6b06a6..8f5ded834ae041565597101b7c88b95667ba48ce 100644 (file)
@@ -34,6 +34,7 @@ if [ "$VERSION" != "18.0" ]; then
 fi
 
 psql "$@" >/dev/null <<EOF
+START TRANSACTION;
 
 -- This line starts the schema upgrade to version 19.0.
 
@@ -55,6 +56,20 @@ UPDATE dhcp6_global_parameter
 SET name = 'ddns-conflict-resolution-mode', value = 'no-check-with-dhcid', parameter_type = 4
 WHERE name = 'ddns-use-conflict-resolution' and value = 'false';
 
+-- This line starts the schema upgrade to version 19.0.
+
+-- Clear lease6_relay_id table.
+DELETE FROM lease6_relay_id;
+
+-- Change lease6_relay_id:lease_addr to INET.
+ALTER TABLE lease6_relay_id ALTER COLUMN lease_addr TYPE INET USING '::';
+
+-- Clear lease6_remote_id table.
+DELETE FROM lease6_remote_id;
+
+-- Change lease6_remote_id:lease_addr to INET.
+ALTER TABLE lease6_remote_id ALTER COLUMN lease_addr TYPE INET USING '::';
+
 -- Update the schema version number.
 UPDATE schema_version
     SET version = '19', minor = '0';