Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions lib/TargetsPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -39,8 +40,8 @@
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,
) {
}

Expand All @@ -50,7 +51,9 @@
* @return list<Filesystem>
*/
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));

Check warning on line 56 in lib/TargetsPage.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Split this 123 characters long line (which is greater than 120 authorized).

See more on https://sonarcloud.io/project/issues?id=somethingwithproof_plugin_pfs&issues=AaH3IAwwUgvRZIAyoK7q&open=AaH3IAwwUgvRZIAyoK7q&pullRequest=81
}

public function render(): void {
Expand Down
11 changes: 6 additions & 5 deletions pfs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('../../');
Expand All @@ -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();
Expand Down
24 changes: 22 additions & 2 deletions tests/Unit/PagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions tests/bin/device-permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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');

Expand Down