opnsense-nordvpn-plugin/tests/ui/guest-setup.php

50 lines
1.6 KiB
PHP

<?php
require_once '/usr/local/etc/inc/config.inc';
require_once '/usr/local/etc/inc/auth.inc';
use OPNsense\Core\Config;
use OPNsense\Firewall\Alias;
$password = trim((string)file_get_contents('/mnt/ui-password'));
if (!preg_match('/^[a-f0-9]{64}$/', $password)) {
throw new RuntimeException('The UI test password is invalid.');
}
$root =& getUserEntry('root');
if ($root === false) {
throw new RuntimeException('The root user is unavailable.');
}
local_user_set_password($root, $password);
local_user_set($root, true);
write_config('Set disposable WebGUI test password');
$users = Config::getInstance()->object()->system->user;
foreach ($users as $user) {
if ((string)$user->name === 'root' && password_verify($password, (string)$user->password)) {
break;
}
}
if (!isset($user) || (string)$user->name !== 'root' || !password_verify($password, (string)$user->password)) {
throw new RuntimeException('The disposable WebGUI password was not saved.');
}
exec('/usr/local/sbin/configctl webgui restart', $output, $status);
if ($status !== 0) {
throw new RuntimeException('The WebGUI did not restart.');
}
$aliases = new Alias();
foreach ($aliases->aliases->alias->iterateItems() as $alias) {
if ((string)$alias->name === 'UI_TEST_NETWORK') {
return;
}
}
$alias = $aliases->aliases->alias->add();
$alias->setNodes([
'enabled' => '1',
'name' => 'UI_TEST_NETWORK',
'type' => 'network',
'proto' => 'IPv4',
'content' => '192.0.2.0/24',
]);
$aliases->serializeToConfig();
Config::getInstance()->save(['description' => 'Add synthetic UI test alias']);