firmware: actually extract the subscription from the mirror #4881

We do not use it very often and almost all mandatory reads are done
via opnsense-update which figures this out differently.
This commit is contained in:
Franco Fichtner 2023-03-09 10:24:25 +01:00
parent ac0f58f291
commit 84f0bcdbdb
8 changed files with 29 additions and 25 deletions

View File

@ -763,9 +763,21 @@ function system_firmware_configure($verbose = false)
/* calculate the effective ABI */
$args = [ exec_safe('-A %s', shell_safe('opnsense-version -x')) ];
$url_sub = '';
if (!empty($config['system']['firmware']['subscription'])) {
/*
* Append the url now that it is not in the mirror anymore.
* This only ever works if the mirror is set to a non-default.
*/
$url_sub = '/' . $config['system']['firmware']['subscription'];
} else {
/* clear the license file when no subscription key is set */
@unlink('/usr/local/opnsense/version/core.license');
}
if (!empty($config['system']['firmware']['mirror'])) {
$args[] = exec_safe('-m %s', str_replace('/', '\/', $config['system']['firmware']['mirror']));
$args[] = exec_safe('-m %s', str_replace('/', '\/', $config['system']['firmware']['mirror'] . $url_sub));
}
if (!empty($config['system']['firmware']['flavour'])) {
@ -775,11 +787,6 @@ function system_firmware_configure($verbose = false)
/* rewrite the config via the defaults and possible arguments */
mwexec('/usr/local/sbin/opnsense-update -sd ' . join(' ', $args));
/* clear the license file when no subscription key is set */
if (empty(shell_safe('/usr/local/sbin/opnsense-update -K'))) {
@unlink('/usr/local/opnsense/version/core.license');
}
service_log("done.\n", $verbose);
}

View File

@ -1019,11 +1019,6 @@ class FirmwareController extends ApiMutableModelControllerBase
return $response;
}
if (!empty((string)$mdl->subscription)) {
/* append subscription */
$mdl->mirror = (string)$mdl->mirror . '/' . (string)$mdl->subscription;
}
$response['status'] = 'ok';
$this->save();

View File

@ -113,17 +113,11 @@ class Firmware extends BaseModel
{
$validOptions = $this->getRepositoryOptions();
/* XXX for now make sure the subscription is removed if given */
$mirror_stripped = (string)$this->mirror;
if (!empty((string)$this->subscription)) {
$mirror_stripped = str_replace('/' . (string)$this->subscription, '', $mirror_stripped);
}
/* standard model validations */
$messages = parent::performValidation($validateFullModel);
/* extended validations */
if (!$validOptions['mirrors_allow_custom'] && !isset($validOptions['mirrors'][$mirror_stripped])) {
if (!$validOptions['mirrors_allow_custom'] && !isset($validOptions['mirrors'][(string)$this->mirror])) {
$messages->appendMessage(new Message(gettext('Unable to set invalid firmware mirror'), 'mirror'));
}
if (!$validOptions['flavours_allow_custom'] && !isset($validOptions['flavours'][(string)$this->flavour])) {
@ -132,7 +126,7 @@ class Firmware extends BaseModel
if (!isset($validOptions['families'][(string)$this->type])) {
$messages->appendMessage(new Message(gettext('Unable to set invalid firmware release type'), 'type'));
}
if (in_array($mirror_stripped, $validOptions['mirrors_has_subscription'])) {
if (in_array((string)$this->mirror, $validOptions['mirrors_has_subscription'])) {
if (!preg_match('/^[a-z0-9]{8}(-[a-z0-9]{4}){3}-[a-z0-9]{12}$/i', (string)$this->subscription)) {
$messages->appendMessage(new Message(gettext('A valid subscription is required for this firmware mirror'), 'subscription'));
}

View File

@ -29,6 +29,7 @@
namespace OPNsense\Core\Migrations;
use OPNsense\Base\BaseModelMigration;
use OPNsense\Core\Firmware;
class M1_0_0 extends BaseModelMigration
{
@ -38,6 +39,9 @@ class M1_0_0 extends BaseModelMigration
*/
public function run($model)
{
if (!($model instanceof Firmware)) {
return;
}
if ((empty((string)$model->type) || (string)$model->type == 'devel') && !empty((string)$model->mirror)) {
$is_business = strpos((string)$model->mirror, 'opnsense-update.deciso.com') !== false;
if ($is_business) {

View File

@ -29,15 +29,19 @@
namespace OPNsense\Core\Migrations;
use OPNsense\Base\BaseModelMigration;
use OPNsense\Core\Firmware;
class M1_0_1 extends BaseModelMigration
{
/**
* Migrate BE release type
* Migrate subscription and remove old flavour types
* @param $model
*/
public function run($model)
{
if (!($model instanceof Firmware)) {
return;
}
if (in_array((string)$model->flavour, ['latest', 'libressl'])) {
$model->flavour = null;
}
@ -46,6 +50,7 @@ class M1_0_1 extends BaseModelMigration
if ($is_business) {
$url = explode('/', (string)$model->mirror);
$model->subscription = array_pop($url);
$model->mirror = implode('/', $url);
}
}
}

View File

@ -659,7 +659,7 @@
var custom_selected = true;
$.each(firmwareoptions.mirrors, function(key, value) {
var selected = false;
if ((key != "" && firmwareconfig['mirror'].indexOf(key) != -1) || key == firmwareconfig['mirror']) {
if (key == firmwareconfig['mirror']) {
selected = true;
custom_selected = false;
}

View File

@ -1,8 +1,8 @@
[settings]
{% if not helpers.empty('OPNsense.Firewall.Alias.geoip.url') %}
url={{OPNsense.Firewall.Alias.geoip.url}}
{% elif not helpers.empty('system.firmware.mirror') and system.firmware.mirror.find('opnsense-update.deciso.com') > -1 and system.firmware.mirror.count('-') > 3 %}
url={{system.firmware.mirror}}/GeopIPAlias.zip
{% elif not helpers.empty('system.firmware.mirror') and system.firmware.mirror.find('opnsense-update.deciso.com') > -1 and no helpers.empty('system.firmware.subscription') %}
url={{system.firmware.mirror}}/{{system.firmware.subscription}}/GeopIPAlias.zip
{% else %}
url=
{% endif %}

View File

@ -141,8 +141,7 @@ core)
target_abi_minor=${product_abi#*.}
IS_BUSINESS=$(echo ${product_id} | grep -c -- '-business')
WANT_BUSINESS=$(${PLUGINCTL} -g system.firmware.mirror | \
grep -c 'opnsense-update\.deciso\.com')
WANT_BUSINESS=$(${PLUGINCTL} -g system.firmware.subscription | grep -c '.')
if [ "${IS_BUSINESS}${WANT_BUSINESS}" = "01" -o ]; then
case ${target_abi_minor} in