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

54 lines
1.8 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');
$aliases = new Alias();
$existing = [];
foreach ($aliases->aliases->alias->iterateItems() as $alias) {
$existing[(string)$alias->name] = true;
}
foreach (['UI_TEST_NETWORK', 'UI_TEST_NETWORK_TWO'] as $name) {
if (isset($existing[$name])) {
continue;
}
$alias = $aliases->aliases->alias->add();
$alias->setNodes([
'enabled' => '1',
'name' => $name,
'type' => 'network',
'proto' => 'IPv4',
'content' => $name === 'UI_TEST_NETWORK' ? '192.0.2.0/24' : '198.51.100.0/24',
]);
}
$aliases->serializeToConfig();
Config::getInstance()->save(['description' => 'Add synthetic UI test aliases']);
$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.');
}