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 cloud_health.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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();
15 changes: 3 additions & 12 deletions include/operator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<array{id: string, name: string, state: string, observed: int, interval: int, detail: string}> */
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,
Expand Down
11 changes: 5 additions & 6 deletions lib/HealthPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<Account> $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;
Expand Down
25 changes: 24 additions & 1 deletion setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
}

Expand Down
1 change: 0 additions & 1 deletion tests/Unit/RequestBudgetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
29 changes: 27 additions & 2 deletions tests/bin/page-permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
Expand Down