diff --git a/lib/TargetsPage.php b/lib/TargetsPage.php index 5e575c5..9cef5ce 100644 --- a/lib/TargetsPage.php +++ b/lib/TargetsPage.php @@ -25,9 +25,10 @@ * reload is never a measurement (REQ-STATE-006). * * Visibility follows Cacti, not a plugin permission layer (REQ-SEC-011, - * REQ-SEC-012): a filesystem mapped to a device is shown to users Cacti - * allows to see that device, checked on every request; an unmapped - * filesystem and every node name are shown to administrators only. + * REQ-SEC-012): a filesystem mapped to a device is shown only to users Cacti + * allows to see that device, administrators included, checked on every + * request (DeviceAccess); an unmapped filesystem and every node name are + * shown to administrators only. */ final class TargetsPage { public const FILE = 'pfs.php'; @@ -39,8 +40,8 @@ public function __construct( private readonly ObservationRepository $observations, private readonly FreshnessEvaluator $freshness, private readonly Clock $clock, - private readonly bool $admin = true, - private readonly ?\Closure $deviceAllowed = null, + private readonly DeviceAccess $access, + private readonly bool $admin = false, ) { } @@ -50,7 +51,9 @@ public function __construct( * @return list */ public function visible(): array { - return array_values(array_filter($this->filesystems->all(), fn (Filesystem $f): bool => $this->admin || ($f->hostId > 0 && $this->deviceAllowed !== null && ($this->deviceAllowed)($f->hostId) === true))); + $allowed = $this->access->filesystems($this->filesystems->all()); + + return $this->admin ? $allowed : array_values(array_filter($allowed, static fn (Filesystem $f): bool => $f->hostId > 0)); } public function render(): void { diff --git a/pfs.php b/pfs.php index 30f6a42..4882d36 100644 --- a/pfs.php +++ b/pfs.php @@ -20,8 +20,9 @@ /* * Parallel Filesystems (Parallel Filesystem View realm). Parses on PHP 8.0; * pfs_page_start() refuses an unsupported runtime before any plugin class - * loads. Reads the cache only. A user without the admin realm sees only - * filesystems whose device Cacti lets them see, checked on this request. + * loads. Reads the cache only. Every user, plugin administrators included, + * sees only filesystems whose device Cacti lets them see, checked on this + * request; the admin realm adds unmapped filesystems and node names. */ chdir('../../'); @@ -40,10 +41,10 @@ new \Somethingwithproof\Pfs\ObservationRepository(false), new \Somethingwithproof\Pfs\FreshnessEvaluator($pfs_clock), $pfs_clock, - api_plugin_user_realm_auth('pfs_admin.php'), - function ($host_id) { + new \Somethingwithproof\Pfs\DeviceAccess(function ($host_id) { return is_device_allowed($host_id); - } + }), + api_plugin_user_realm_auth('pfs_admin.php') ); top_header(); diff --git a/tests/Unit/PagesTest.php b/tests/Unit/PagesTest.php index 73eb53a..ffd2f30 100644 --- a/tests/Unit/PagesTest.php +++ b/tests/Unit/PagesTest.php @@ -45,10 +45,30 @@ use Somethingwithproof\Pfs\TargetsPage; use Somethingwithproof\Pfs\HealthWarnings; -function pfs_targets_page(FixedClock $clock): TargetsPage { - return new TargetsPage(new FilesystemRepository(false, $clock), new NodeRepository(false, $clock), new TargetRepository(false), new ObservationRepository(false), new FreshnessEvaluator($clock), $clock); +function pfs_targets_page(FixedClock $clock, ?DeviceAccess $access = null, bool $admin = true): TargetsPage { + return new TargetsPage(new FilesystemRepository(false, $clock), new NodeRepository(false, $clock), new TargetRepository(false), new ObservationRepository(false), new FreshnessEvaluator($clock), $clock, $access ?? new DeviceAccess(fn (int $id): bool => true), $admin); } +it('shows an administrator only filesystems on devices Cacti allows, plus unmapped ones', function () { + $clock = new FixedClock(10000); + $GLOBALS['__pfs_db_rows'] = [ + 'FROM plugin_pfs_filesystem' => [ + ['id' => 1, 'name' => 'allowed', 'backend' => 'lustre', 'poller_id' => 1, 'expected_interval' => 300, 'admin_state' => 'enabled', 'host_id' => 7], + ['id' => 2, 'name' => 'denied', 'backend' => 'lustre', 'poller_id' => 1, 'expected_interval' => 300, 'admin_state' => 'enabled', 'host_id' => 8], + ['id' => 3, 'name' => 'unmapped', 'backend' => 'lustre', 'poller_id' => 1, 'expected_interval' => 300, 'admin_state' => 'enabled', 'host_id' => 0], + ], + ]; + $access = new DeviceAccess(fn (int $id): bool => $id === 7); + $names = static fn (TargetsPage $page): array => array_map(static fn (Filesystem $f): string => $f->name, $page->visible()); + + $admin = $names(pfs_targets_page($clock, $access, true)); + $viewer = $names(pfs_targets_page($clock, $access, false)); + $GLOBALS['__pfs_db_rows'] = []; + + expect($admin)->toBe(['allowed', 'unmapped']) + ->and($viewer)->toBe(['allowed']); +}); + it('shows a present target with unknown values as unknown, not zero', function () { $clock = new FixedClock(10000); $target = new Target(7, 1, TargetId::parse('scratch-OST0001') ?? throw new LogicException(), Review::Accepted, Presence::Present, Condition::Unknown, null, 3, 100, 9900); diff --git a/tests/bin/device-permissions.php b/tests/bin/device-permissions.php index 8efd8dc..9044e98 100644 --- a/tests/bin/device-permissions.php +++ b/tests/bin/device-permissions.php @@ -28,8 +28,9 @@ * pfsrevoked View realm, device and graphs denied by Cacti: nothing * pfsnorealm Console and graphs, no plugin realm: no plugin page or link * pfsadmin2 both realms, but Cacti grants only the "work" filesystem's - * device: the registry, export, and health page hide the - * other device's records, and every change to them is refused + * device: the view page, registry, export, and health page + * hide the other device's records, and every change to them + * is refused * * Graph and data source access is Cacti's own; the plugin adds no layer, so * this shows that the plugin's objects follow it. Disposable databases only: @@ -217,6 +218,9 @@ function dp_token($body) { $data = json_decode($export, true); dp_expect(is_array($data) && !in_array('scratch', array_column($data['filesystems'], 'name'), true) && $data['nodes'] === array() && $data['targets'] === array(), 'a restricted administrator\'s export leaves out records on denied devices'); +list(, $view) = dp_http($base . 'pfs.php', $jar); +dp_expect(strpos($view, 'Filesystem work') !== false && strpos($view, 'Filesystem orphan') !== false && strpos($view, 'Filesystem scratch') === false && strpos($view, 'oss-probe-name') === false, 'a restricted administrator\'s view page shows only filesystems on allowed devices, and unmapped ones'); + list(, $health) = dp_http($base . 'pfs_health.php', $jar); dp_expect(strpos($health, 'Parallel Filesystem Warnings') !== false && strpos($health, 'oss-probe-name') === false, 'a restricted administrator\'s health page leaves out nodes on denied devices');