From a535319f1d831051bf6999b61d0016c5378fa52b Mon Sep 17 00:00:00 2001 From: Gmer4Lfe Date: Sat, 6 Jun 2026 18:02:37 -0400 Subject: [PATCH] =?UTF-8?q?API=20key=20button=20delegates=20to=20unraid=5F?= =?UTF-8?q?api=5Fkey=5Frenew.sh=20=E2=80=94=20checks=20registry=20before?= =?UTF-8?q?=20creating?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Plugin/unraid/include/config.php | 39 +++++++++++--------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/Plugin/unraid/include/config.php b/Plugin/unraid/include/config.php index 4c0f57c..c43c2f6 100644 --- a/Plugin/unraid/include/config.php +++ b/Plugin/unraid/include/config.php @@ -360,33 +360,20 @@ function vv_known_hosts(): array { // Create (or overwrite) the Varaverk Unraid API key and write it into host conf. // Returns ['ok'=>true,'key_preview'=>'...'] or ['ok'=>false,'error'=>'...']. function vv_auto_create_api_key(string $hostId, string $confFile): array { + $script = SCRIPTS_DIR . '/Plugin/unraid/System_Essentials/unraid_api_key_renew.sh'; + if (!file_exists($script)) { + return ['ok' => false, 'error' => 'unraid_api_key_renew.sh not found']; + } + exec('bash ' . escapeshellarg($script) . ' 2>&1', $out, $rc); + if ($rc !== 0) { + $msg = implode(' ', array_filter(array_map('trim', $out))); + return ['ok' => false, 'error' => $msg ?: 'Script failed']; + } $varName = strtoupper($hostId) . '_UNRAID_API_KEY'; - $output = shell_exec('timeout 10 /usr/local/sbin/unraid-api apikey --name "Varaverk" --create --overwrite --description "Varaverk plugin" --roles ADMIN --json &1'); - if (!$output) { - return ['ok' => false, 'error' => 'unraid-api returned no output']; - } - $data = json_decode(trim($output), true); - $key = $data['key'] ?? null; - if (!$key) { - return ['ok' => false, 'error' => 'No key in response']; - } - $raw = vv_read_conf_raw($confFile); - if ($raw === '') { - return ['ok' => false, 'error' => 'Cannot read ' . $confFile]; - } - if (!str_contains($raw, $varName)) { - foreach ([strtoupper($hostId) . '_OWNER_EMAIL', strtoupper($hostId) . '_SSH_KEY'] as $anchor) { - if (str_contains($raw, $anchor)) { - $raw = preg_replace('/^(\s*' . preg_quote($anchor, '/') . '\s*=.*$)/m', - '$1' . "\n " . $varName . '=""', $raw, 1); - break; - } - } - } - $raw = preg_replace('/^(\s*' . preg_quote($varName, '/') . '\s*=\s*)"[^"]*"/m', - '${1}"' . $key . '"', $raw); - vv_write_conf_raw($confFile, $raw); - return ['ok' => true, 'key_preview' => substr($key, 0, 8) . '...' . substr($key, -4)]; + $raw = vv_read_conf_raw($confFile); + preg_match('/^\s*' . preg_quote($varName, '/') . '\s*=\s*"([^"]+)"/m', $raw, $m); + $key = $m[1] ?? ''; + return ['ok' => true, 'key_preview' => $key ? substr($key, 0, 8) . '...' . substr($key, -4) : 'registered']; } // Local LAN IP via routing table — static-cached per request.