diff --git a/cloud_health.php b/cloud_health.php index 8163a8b..bad9438 100644 --- a/cloud_health.php +++ b/cloud_health.php @@ -30,9 +30,11 @@ exit; } -$cloud_clock = new \Somethingwithproof\Cloud\SystemClock(); -$cloud_page = new \Somethingwithproof\Cloud\HealthPage( - cloud_accounts(), +// Without Cloud Admin, Cacti's device permissions decide which accounts show. +$cloud_admin = api_user_realm_auth('cloud_accounts.php'); +$cloud_accounts = $cloud_admin ? cloud_accounts()->all() : cloud_device_allowed_accounts(); +$cloud_clock = new \Somethingwithproof\Cloud\SystemClock(); +$cloud_page = new \Somethingwithproof\Cloud\HealthPage( new \Somethingwithproof\Cloud\HealthRepository(false), new \Somethingwithproof\Cloud\ResourceRepository(false, $cloud_clock), new \Somethingwithproof\Cloud\ObservationRepository(false), @@ -44,8 +46,9 @@ top_header(); $cloud_page->render( cloud_settings(), - api_user_realm_auth('cloud_accounts.php'), - (int) read_config_option('poller_interval') + $cloud_admin, + (int) read_config_option('poller_interval'), + $cloud_accounts ); -cloud_estimate_view()->render(cloud_accounts()->all()); +cloud_estimate_view()->render($cloud_accounts); bottom_footer(); diff --git a/include/operator.php b/include/operator.php index 1367b6d..6179886 100644 --- a/include/operator.php +++ b/include/operator.php @@ -18,24 +18,15 @@ */ use Somethingwithproof\Cloud\HealthRepository; -use Somethingwithproof\Cloud\DeviceRepository; -use Somethingwithproof\Cloud\SystemClock; use Somethingwithproof\Cloud\Verifier; use Somethingwithproof\Cloud\Drivers; /** @return list */ function cloud_operator_rows(): array { - $rows = []; - $accounts = cloud_accounts(); - $devices = new DeviceRepository(false, new SystemClock()); - $health = new HealthRepository(false); - - foreach ($accounts->all() as $account) { - $host = $devices->hostFor($account->id); + $rows = []; + $health = new HealthRepository(false); - if ($host === null || !is_device_allowed($host)) { - continue; - } + foreach (cloud_device_allowed_accounts() as $account) { $h = $health->find($account->id); $rows[] = [ 'id' => (string) $account->id, diff --git a/lib/HealthPage.php b/lib/HealthPage.php index a34125c..87b75e6 100644 --- a/lib/HealthPage.php +++ b/lib/HealthPage.php @@ -32,7 +32,6 @@ final class HealthPage { public const FILE = 'cloud_health.php'; public function __construct( - private readonly AccountRepository $accounts, private readonly HealthRepository $health, private readonly ResourceRepository $resources, private readonly ObservationRepository $observations, @@ -43,12 +42,12 @@ public function __construct( } /** - * @param bool $admin whether the viewer has Cloud Admin, which alone sees - * credential state and credential warnings + * @param bool $admin whether the viewer has Cloud Admin, which alone sees + * credential state and credential warnings + * @param list $accounts the accounts the viewer may see; the caller + * applies Cacti's device permissions */ - public function render(Settings $settings, bool $admin, int $pollerInterval): void { - $accounts = $this->accounts->all(); - + public function render(Settings $settings, bool $admin, int $pollerInterval, array $accounts): void { html_start_box(__('Cloud Warnings', 'cloud'), '100%', '', '3', 'center', ''); $shown = 0; diff --git a/setup.php b/setup.php index e4cc700..6a41315 100644 --- a/setup.php +++ b/setup.php @@ -196,6 +196,28 @@ function cloud_accounts(): \Somethingwithproof\Cloud\AccountRepository { return new \Somethingwithproof\Cloud\AccountRepository(false, new \Somethingwithproof\Cloud\SystemClock()); } +/** + * The accounts whose scope device the current user may see under Cacti's + * device permissions. An account without a scope device is left out: only + * Cloud Admin sees unmapped accounts. + * + * @return list<\Somethingwithproof\Cloud\Account> + */ +function cloud_device_allowed_accounts(): array { + $devices = new \Somethingwithproof\Cloud\DeviceRepository(false, new \Somethingwithproof\Cloud\SystemClock()); + $accounts = array(); + + foreach (cloud_accounts()->all() as $account) { + $host = $devices->hostFor($account->id); + + if ($host !== null && is_device_allowed($host)) { + $accounts[] = $account; + } + } + + return $accounts; +} + function cloud_settings(): \Somethingwithproof\Cloud\Settings { return (new \Somethingwithproof\Cloud\SettingsRepository(false))->load(); } @@ -515,7 +537,8 @@ function cloud_graphs_new_top_links(): void { $host = get_request_var('host_id'); - if (!is_numeric($host) || !cloud_boot() || !api_user_realm_auth('cloud_health.php')) { + if (!is_numeric($host) || !cloud_boot() || !api_user_realm_auth('cloud_health.php') + || (!api_user_realm_auth('cloud_accounts.php') && !is_device_allowed((int) $host))) { return; } diff --git a/tests/Unit/RequestBudgetTest.php b/tests/Unit/RequestBudgetTest.php index 62e2fe9..6c64b7c 100644 --- a/tests/Unit/RequestBudgetTest.php +++ b/tests/Unit/RequestBudgetTest.php @@ -171,7 +171,6 @@ public function estimate(array $requests, int $intervalSeconds): \Somethingwithp ]; $page = new \Somethingwithproof\Cloud\HealthPage( - new \Somethingwithproof\Cloud\AccountRepository(false, $clock), new \Somethingwithproof\Cloud\HealthRepository(false), new \Somethingwithproof\Cloud\ResourceRepository(false, $clock), new \Somethingwithproof\Cloud\ObservationRepository(false), diff --git a/tests/bin/page-permissions.php b/tests/bin/page-permissions.php index c8d7279..0c9bf9a 100644 --- a/tests/bin/page-permissions.php +++ b/tests/bin/page-permissions.php @@ -30,8 +30,10 @@ * Then it saves an account with a vault credential through the form and * searches every response, the Cacti log, and every plugin table for the * credential (#32). Last (#45), it views a cloud graph as users allowed and - * denied by Cacti's graph and device permissions, checks that ss_cloud.php - * serves nothing over HTTP, and revokes each realm from a logged-in user. + * denied by Cacti's graph and device permissions, checks that Cloud Health + * lists the graph's account only to viewers allowed its device, checks that + * ss_cloud.php serves nothing over HTTP, and revokes each realm from a + * logged-in user. * Disposable databases only: it resets the admin password and adds users. * Must parse on PHP 8.0. * @@ -365,6 +367,29 @@ function perm_login($user) { unlink($graphJar); } +// Cloud Health, its warnings and its estimate list an account to a viewer +// only when Cacti's device permissions allow its scope device. +perm_user('healthdenied', array(8, $view), 2); +db_execute_prepared( + 'INSERT INTO user_auth_perms (user_id, item_id, type) VALUES (?, ?, 3)', + array(perm_user('healthdevice', array(8, $view), 2), $host) +); + +foreach (array('admin' => true, 'viewer' => true, 'healthdevice' => true, 'healthdenied' => false) as $user => $allowed) { + $healthJar = $user === 'admin' ? $jar : perm_login($user); + list($code, $body) = perm_http($base . 'cloud_health.php', $healthJar); + + perm_expect( + strpos($body, 'Cloud Warnings') !== false && strpos($body, 'Cloud Request Estimate') !== false + && (strpos($body, 'Graph probe') !== false) === $allowed, + "$user " . ($allowed ? 'sees' : 'does not see') . " the scope device's account on cloud_health.php (HTTP $code)" + ); + + if ($healthJar !== $jar) { + unlink($healthJar); + } +} + // The script server entry point serves nothing over the web, even to an administrator. list($code, $body) = perm_http($base . 'ss_cloud.php?argv[]=' . $host . '&argv[]=droplet&argv[]=index', $jar); // Cacti's cli_check.php answers 404, so the file must exist for the 404 to count.