Remove orphaned API key code — correct key name format, collapse duplicate buttons

This commit is contained in:
Gmer4Lfe
2026-06-06 18:17:30 -04:00
parent dab8d62e95
commit a24f9db2ff
2 changed files with 24 additions and 48 deletions
+4 -9
View File
@@ -96,15 +96,14 @@ if ($action === 'detect' && $_SERVER['REQUEST_METHOD'] === 'POST') {
} }
// ── Unraid API key status ───────────────────────────────────────────────────── // ── Unraid API key status ─────────────────────────────────────────────────────
// Keys live in host*.conf (private, not master.conf):
// host1.conf: HOST1_UNRAID_API_KEY (own) + HOST2_UNRAID_API_KEY (HOST1's access to HOST2)
// host2.conf: HOST2_UNRAID_API_KEY (own) + HOST1_UNRAID_API_KEY (HOST2's access to HOST1)
if ($action === 'api_status') { if ($action === 'api_status') {
require_once dirname(__DIR__) . '/include/unraid_api.php'; require_once dirname(__DIR__) . '/include/unraid_api.php';
$localStatus = vv_api_get_status(); $localStatus = vv_api_get_status();
$vars = vv_conf_vars(); $vars = vv_conf_vars();
$myHost = vv_detect_host(); $myHost = vv_detect_host();
$myId = strtoupper($myHost); $myId = strtoupper($myHost);
$hn = trim((string)shell_exec("hostname -s 2>/dev/null | sed 's/^[Uu][Nn][Rr][Aa][Ii][Dd]-//'")) ?: 'Varaverk';
$localKeyName = 'Varaverk ' . $hn;
$hosts = []; $hosts = [];
foreach ($vars as $k => $v) { foreach ($vars as $k => $v) {
@@ -113,14 +112,11 @@ if ($action === 'api_status') {
$keyVar = $id . '_UNRAID_API_KEY'; $keyVar = $id . '_UNRAID_API_KEY';
$key = $vars[$keyVar] ?? ''; $key = $vars[$keyVar] ?? '';
$isLocal = ($id === $myId); $isLocal = ($id === $myId);
// For local: key is Varaverk_HOST1 registered on own machine
// For remote: key is Varaverk_HOST1 registered on HOST2's machine (stored in host1.conf)
$hosts[] = [ $hosts[] = [
'host_id' => $id, 'host_id' => $id,
'hostname' => $v, 'hostname' => $v,
'is_local' => $isLocal, 'is_local' => $isLocal,
'key_var' => $keyVar, 'key_var' => $keyVar,
'key_name' => 'Varaverk_' . ($isLocal ? $myId : $myId), // Varaverk_HOST1 on that registry
'key_present' => !empty($key), 'key_present' => !empty($key),
'key_preview' => $key ? substr($key, 0, 8) . '...' . substr($key, -4) : null, 'key_preview' => $key ? substr($key, 0, 8) . '...' . substr($key, -4) : null,
'api_ok' => $isLocal 'api_ok' => $isLocal
@@ -133,6 +129,7 @@ if ($action === 'api_status') {
echo json_encode([ echo json_encode([
'ok' => true, 'ok' => true,
'my_id' => $myId, 'my_id' => $myId,
'key_name' => $localKeyName,
'hosts' => $hosts, 'hosts' => $hosts,
'fallbacks' => $localStatus['fallbacks'], 'fallbacks' => $localStatus['fallbacks'],
]); ]);
@@ -145,11 +142,9 @@ if ($action === 'setup_apikeys' && $_SERVER['REQUEST_METHOD'] === 'POST') {
if (!file_exists($script)) { if (!file_exists($script)) {
echo json_encode(['ok' => false, 'error' => 'unraid_api_key_renew.sh not found']); exit; echo json_encode(['ok' => false, 'error' => 'unraid_api_key_renew.sh not found']); exit;
} }
$allHosts = ($_POST['all_hosts'] ?? '0') === '1';
$flags = $allHosts ? ' --all-hosts' : '';
set_time_limit(60); set_time_limit(60);
$output = []; $exit = 0; $output = []; $exit = 0;
exec('bash ' . escapeshellarg($script) . $flags . ' 2>&1', $output, $exit); exec('bash ' . escapeshellarg($script) . ' 2>&1', $output, $exit);
echo json_encode(['ok' => $exit === 0, 'exit' => $exit, 'output' => implode("\n", $output)]); echo json_encode(['ok' => $exit === 0, 'exit' => $exit, 'output' => implode("\n", $output)]);
exit; exit;
} }
+10 -29
View File
@@ -13,10 +13,6 @@ $_enableLogging = ($_vars['ENABLE_LOGGING'] ?? 'false') === 'true';
$_discordKey = $_myId . '_DISCORD_WEBHOOK'; $_discordKey = $_myId . '_DISCORD_WEBHOOK';
$_discordHook = $_vars[$_discordKey] ?? ''; $_discordHook = $_vars[$_discordKey] ?? '';
// API key
$_apiKeyVar = $_myId . '_UNRAID_API_KEY';
$_apiKey = $_vars[$_apiKeyVar] ?? '';
$_apiPreview = $_apiKey ? substr($_apiKey, 0, 8) . '...' . substr($_apiKey, -4) : '';
?> ?>
<style> <style>
.vv-set-card { background:#161616;border:1px solid #2a2a2a;border-radius:6px;padding:14px 16px;margin-bottom:14px; } .vv-set-card { background:#161616;border:1px solid #2a2a2a;border-radius:6px;padding:14px 16px;margin-bottom:14px; }
@@ -152,18 +148,15 @@ $_apiPreview = $_apiKey ? substr($_apiKey, 0, 8) . '...' . substr($_apiKey, -4)
<div id="vv-api-hosts" style="margin-bottom:12px;"></div> <div id="vv-api-hosts" style="margin-bottom:12px;"></div>
<div style="font-size:11px;color:#444;margin-bottom:12px;line-height:1.6;"> <div style="font-size:11px;color:#444;margin-bottom:12px;line-height:1.6;">
Each host registers Each host registers its key in the local Unraid registry and pushes it to all partners over SSH.
<code style="font-size:10px;color:#4a7a4a;background:#080808;padding:1px 4px;border-radius:2px;">Varaverk_HOST1</code> Key values are stored in the private <code style="font-size:10px;color:#4a7a4a;background:#080808;padding:1px 4px;border-radius:2px;">host*.conf</code>
in <em>every</em> machine's local Unraid registry — own machine plus all partners.
The key values are stored in the private <code style="font-size:10px;color:#4a7a4a;background:#080808;padding:1px 4px;border-radius:2px;">host*.conf</code>
so each host can query every other host's GraphQL API directly — real-time monitoring, no SSH. so each host can query every other host's GraphQL API directly — real-time monitoring, no SSH.
During offboard, partner keys are removed from each registry automatically. During offboard, partner keys are removed from each registry automatically.
</div> </div>
<div style="display:flex;gap:8px;align-items:center;flex-wrap:wrap;"> <div style="display:flex;gap:8px;align-items:center;flex-wrap:wrap;">
<button class="vv-set-btn" onclick="vvApiCheck()" id="vv-api-check-btn">↻ Check status</button> <button class="vv-set-btn" onclick="vvApiCheck()" id="vv-api-check-btn">↻ Check status</button>
<button class="vv-set-btn" onclick="vvApiRenew(false)" id="vv-api-local-btn">Renew local</button> <button class="vv-set-btn primary" onclick="vvApiRenew()" id="vv-api-renew-btn">Renew</button>
<button class="vv-set-btn primary" onclick="vvApiRenew(true)" id="vv-api-all-btn">Setup all host keys</button>
<span id="vv-api-fb" style="font-size:11px;color:#444;"></span> <span id="vv-api-fb" style="font-size:11px;color:#444;"></span>
</div> </div>
<pre class="vv-set-out" id="vv-api-out"></pre> <pre class="vv-set-out" id="vv-api-out"></pre>
@@ -356,16 +349,12 @@ function vvApiCheck() {
: `<span style="color:#2a2a3a;font-size:9px;margin-left:4px;">${d.my_id}'s key on ${h.host_id}</span>`; : `<span style="color:#2a2a3a;font-size:9px;margin-left:4px;">${d.my_id}'s key on ${h.host_id}</span>`;
const keyBit = h.key_present const keyBit = h.key_present
? `<code style="font-size:9px;color:#4a6a4a;">${h.key_preview}</code>` ? `<code style="font-size:9px;color:#4a6a4a;">${h.key_preview}</code>`
: `<span style="color:#8b2a2a;font-size:10px;">not set — click Setup</span>`; : `<span style="color:#8b2a2a;font-size:10px;">not set — click Renew</span>`;
const dot = h.api_ok ? '#4caf50' : (h.key_present ? '#ffb74d' : '#444'); const dot = h.api_ok ? '#4caf50' : (h.key_present ? '#ffb74d' : '#444');
// Key name in that machine's registry: always Varaverk_<MY_ID>
const keyName = `Varaverk_${d.my_id}`;
const registry = h.is_local ? h.host_id : h.host_id;
return `<div style="display:flex;align-items:center;gap:8px;padding:4px 0;border-bottom:1px solid #111;"> return `<div style="display:flex;align-items:center;gap:8px;padding:4px 0;border-bottom:1px solid #111;">
<span style="width:6px;height:6px;border-radius:50%;background:${dot};flex-shrink:0;display:inline-block;"></span> <span style="width:6px;height:6px;border-radius:50%;background:${dot};flex-shrink:0;display:inline-block;"></span>
<span style="font-size:11px;color:#666;flex-shrink:0;min-width:50px;">${h.host_id}</span> <span style="font-size:11px;color:#666;flex-shrink:0;min-width:50px;">${h.host_id}</span>
<code style="font-size:9px;color:#2a3a2a;flex-shrink:0;">${keyName}</code> <code style="font-size:9px;color:#2a3a2a;flex-shrink:0;">${h.is_local ? d.key_name : h.key_var}</code>
<span style="font-size:9px;color:#2a2a2a;flex-shrink:0;">on ${registry}</span>
${tag} ${tag}
<span style="flex:1;text-align:right;">${keyBit}</span> <span style="flex:1;text-align:right;">${keyBit}</span>
</div>`; </div>`;
@@ -374,27 +363,20 @@ function vvApiCheck() {
.catch(() => { btn.disabled = false; btn.textContent = '↻ Check status'; }); .catch(() => { btn.disabled = false; btn.textContent = '↻ Check status'; });
} }
function vvApiRenew(allHosts) { function vvApiRenew() {
const localBtn = document.getElementById('vv-api-local-btn'); const btn = document.getElementById('vv-api-renew-btn');
const allBtn = document.getElementById('vv-api-all-btn');
const out = document.getElementById('vv-api-out'); const out = document.getElementById('vv-api-out');
const fb = document.getElementById('vv-api-fb'); const fb = document.getElementById('vv-api-fb');
const badge = document.getElementById('vv-api-badge'); const badge = document.getElementById('vv-api-badge');
const activeBtn = allHosts ? allBtn : localBtn; btn.disabled = true; btn.textContent = 'Renewing…';
[localBtn, allBtn].forEach(b => { if(b) b.disabled = true; });
activeBtn.textContent = allHosts ? 'Setting up…' : 'Renewing…';
fb.textContent = ''; out.textContent = ''; out.style.display = ''; fb.textContent = ''; out.textContent = ''; out.style.display = '';
const fd = new FormData(); const fd = new FormData();
fd.append('action', 'setup_apikeys'); fd.append('action', 'setup_apikeys');
fd.append('all_hosts', allHosts ? '1' : '0');
fetch('/plugins/varaverk/api/storage.php', { method: 'POST', body: fd }) fetch('/plugins/varaverk/api/storage.php', { method: 'POST', body: fd })
.then(r => r.json()) .then(r => r.json())
.then(d => { .then(d => {
localBtn.disabled = false; localBtn.textContent = 'Renew local'; btn.disabled = false; btn.textContent = 'Renew';
allBtn.disabled = false; allBtn.textContent = 'Setup all host keys';
out.textContent = d.output || '(no output)'; out.textContent = d.output || '(no output)';
out.scrollTop = out.scrollHeight; out.scrollTop = out.scrollHeight;
fb.style.color = d.ok ? '#4caf50' : '#ef5350'; fb.style.color = d.ok ? '#4caf50' : '#ef5350';
@@ -402,8 +384,7 @@ function vvApiRenew(allHosts) {
if (d.ok) { badge.textContent = ''; vvApiCheck(); } if (d.ok) { badge.textContent = ''; vvApiCheck(); }
}) })
.catch(() => { .catch(() => {
localBtn.disabled = false; localBtn.textContent = 'Renew local'; btn.disabled = false; btn.textContent = 'Renew';
allBtn.disabled = false; allBtn.textContent = 'Setup all host keys';
fb.style.color = '#ef5350'; fb.textContent = 'Request failed'; fb.style.color = '#ef5350'; fb.textContent = 'Request failed';
}); });
} }