gateways: fix migration issue causing gateways to be skipped

Properties should be copied 1-to-1 before we apply the required
defaults if necessary. In the previous situation this caused
required properties to be set to an empty value after the default
value had already been written to it. In the failure case we
attempt to be a bit more explicit and refer to the crash reporter.

While here, the master branch has dropped the Phalcon Messages class,
so switch to count() since this seems to inherit array type
and is therefore backwards compatible:

$msgs = new \Phalcon\Messages\Messages();
$count = count($msgs);
// $count == 0

See https://github.com/opnsense/core/issues/6389
This commit is contained in:
Stephan de Wit 2024-02-14 14:26:50 +01:00
parent 0873d89f3c
commit fdc8a8fd2c

View File

@ -44,28 +44,12 @@ class M1_0_0 extends BaseModelMigration
$config = Config::getInstance()->object();
// create logger to save possible consistency issues to
$logger = new Syslog('config', null, LOG_LOCAL2);
$logger = new Syslog('config', null, LOG_LOCAL2);
if (!empty($config->gateways) && count($config->gateways->children()) > 0) {
foreach ($config->gateways->gateway_item as $gateway) {
$node = $model->gateway_item->Add();
// special handling of implied booleans
$node->defaultgw = !empty((string)$gateway->defaultgw) ? '1' : '0';
$node->disabled = !empty((string)$gateway->disabled) ? '1' : '0';
$node->fargw = !empty((string)$gateway->fargw) ? '1' : '0';
$node->force_down = !empty((string)$gateway->force_down) ? '1' : '0';
$node->monitor_disable = !empty((string)$gateway->monitor_disable) ? '1' : '0';
$node->monitor_noroute = !empty((string)$gateway->monitor_noroute) ? '1' : '0';
if (empty((string)$gateway->priority)) {
$node->priority = '255';
}
if (empty((string)$gateway->ipprotocol)) {
$node->ipprotocol = 'inet';
}
// migrate set nodes
$node_properties = iterator_to_array($node->iterateItems());
foreach ($gateway as $key => $value) {
@ -83,6 +67,26 @@ class M1_0_0 extends BaseModelMigration
$node->$key = (string)$value;
}
// special handling of implied booleans
$node->defaultgw = !empty((string)$gateway->defaultgw) ? '1' : '0';
$node->disabled = !empty((string)$gateway->disabled) ? '1' : '0';
$node->fargw = !empty((string)$gateway->fargw) ? '1' : '0';
$node->force_down = !empty((string)$gateway->force_down) ? '1' : '0';
$node->monitor_disable = !empty((string)$gateway->monitor_disable) ? '1' : '0';
$node->monitor_noroute = !empty((string)$gateway->monitor_noroute) ? '1' : '0';
if (empty((string)$gateway->priority)) {
$node->priority = '255';
}
if (empty((string)$gateway->ipprotocol)) {
$node->ipprotocol = 'inet';
}
if (empty((string)$gateway->weight)) {
$node->weight = '1';
}
$model->gateway_item->calculateCurrent($node);
// increase time period if old model had it set too low
$min_time_period = 2 * (
@ -91,8 +95,12 @@ class M1_0_0 extends BaseModelMigration
if ((string)$node->current_time_period < $min_time_period) {
$node->time_period = $min_time_period;
}
if ($model->performValidation()->count() > 0) {
$logger->error(sprintf("Migration skipped gateway %s (%s)", $gateway->name, $gateway->gateway));
$result = $model->performValidation();
if (count($result) > 0) {
// save details of validation error
error_log(print_r($result, true));
$logger->error(sprintf("Migration skipped gateway %s (%s). See crash reporter for details",
$gateway->name, $gateway->gateway));
$model->gateway_item->del($node->getAttribute('uuid'));
}
}