diff --git a/web/app/Services/Cloud/CloudAgentSettingsService.php b/web/app/Services/Cloud/CloudAgentSettingsService.php index b3e12d1c..cfb1ae40 100644 --- a/web/app/Services/Cloud/CloudAgentSettingsService.php +++ b/web/app/Services/Cloud/CloudAgentSettingsService.php @@ -132,6 +132,7 @@ final class CloudAgentSettingsService if ($actualRevision !== $expectedRevision) { throw new CloudError('Agent 配置已被修改,请刷新页面后重试', 409); } + $isNew = $setting === null; if ($setting === null) { if ($secret === '') { throw new CloudError('首次保存 Agent 配置必须填写 API 密钥'); @@ -142,6 +143,12 @@ final class CloudAgentSettingsService } elseif ($secret === '' && trim((string) $setting->sealed_api_secret) === '') { throw new CloudError('启用 Agent 前必须配置 API 密钥'); } + $enabledChanged = $isNew + || (bool) $setting->enabled !== $enabled; + $secretChanged = $secret !== ''; + if (!$enabledChanged && !$secretChanged) { + return $setting; + } $setting->enabled = $enabled; if ($secret !== '') { $setting->sealed_api_secret = CloudAgentSecretCipher::encrypt($secret, $hostId); diff --git a/web/public/cloud/admin.css b/web/public/cloud/admin.css index c481d5df..05077a74 100644 --- a/web/public/cloud/admin.css +++ b/web/public/cloud/admin.css @@ -1345,6 +1345,30 @@ margin-top: 10px; } +.cloud-agent-rotation-confirm { + display: flex; + align-items: flex-start; + gap: 9px; + margin-top: 12px; + padding: 11px 12px; + border: 1px solid #f4c7a1; + border-radius: 8px; + color: #9a4f14; + background: #fff8ef; + font-size: 12px; + font-weight: 750; + line-height: 1.55; +} + +.cloud-agent-rotation-confirm[hidden] { + display: none; +} + +.cloud-agent-rotation-confirm .form-check-input { + flex: 0 0 auto; + margin-top: 2px; +} + .cloud-agent-config-box { display: grid; gap: 10px; diff --git a/web/public/cloud/agent-settings.js b/web/public/cloud/agent-settings.js index 2f4da8d1..031adc91 100644 --- a/web/public/cloud/agent-settings.js +++ b/web/public/cloud/agent-settings.js @@ -6,6 +6,7 @@ const state = { selected: null, busy: false, expandedRegions: new Set(), regionsInitialized: false, drawerOpener: null, drawerScroll: null, secretDraft: '', secretDraftHostId: null, secretVisible: false, + rotationConfirmed: false, }; function notice(message, error = false) { @@ -245,6 +246,7 @@ function clearSecretDraft(update = true) { state.secretDraft = ''; state.secretDraftHostId = null; state.secretVisible = false; + state.rotationConfirmed = false; if (update) renderSecretDraft(selectedAgent()); } @@ -262,6 +264,12 @@ function renderSecretDraft(agent) { byId('agent-settings-copy-secret').disabled = !canManage || !hasDraft; byId('agent-settings-clear-secret').disabled = !canManage || !hasDraft; byId('agent-settings-copy-config').disabled = !canManage || !hasDraft; + const rotating = hasDraft && agent?.secret_configured === true; + const confirmation = byId('agent-settings-rotation-confirm-box'); + confirmation.hidden = !rotating; + byId('agent-settings-rotation-confirm').checked = rotating && state.rotationConfirmed; + byId('agent-settings-save').disabled = !canManage || !agent || state.busy + || (rotating && !state.rotationConfirmed); const box = byId('agent-settings-config-box'); box.hidden = !hasDraft; byId('agent-settings-config-preview').textContent = hasDraft ? configFragment(agent, secret) : ''; @@ -360,6 +368,7 @@ async function save(event) { }; if (!Number.isSafeInteger(body.expected_revision) || body.expected_revision < 0) return notice('配置版本无效,请刷新页面。', true); if (body.api_secret && (body.api_secret.length < 32 || body.api_secret.length > 256 || /\s/.test(body.api_secret))) return notice('API 密钥需为 32 至 256 位无空格字符。', true); + if (body.api_secret && selectedAgent()?.secret_configured && !state.rotationConfirmed) return notice('请先确认密钥轮换会立即使旧密钥失效。', true); state.busy = true; fill(selectedAgent(), { preserveDraft: true }); notice('正在保存 Agent API 配置…'); let savedSuccessfully = false; try { @@ -397,6 +406,7 @@ if (root) { byId('agent-settings-secret').addEventListener('input', event => { state.secretDraft = event.currentTarget.value; state.secretDraftHostId = state.selected; + state.rotationConfirmed = false; renderSecretDraft(selectedAgent()); }); byId('agent-settings-generate').addEventListener('click', () => { @@ -404,11 +414,13 @@ if (root) { state.secretDraft = generateAgentSecret(); state.secretDraftHostId = state.selected; state.secretVisible = true; + state.rotationConfirmed = false; renderSecretDraft(selectedAgent()); notice('已在本页生成一次性安全密钥;请复制到宿主机并保存面板配置。'); } catch (error) { notice(error.message, true); } }); byId('agent-settings-reveal').addEventListener('click', () => { state.secretVisible = !state.secretVisible; renderSecretDraft(selectedAgent()); }); + byId('agent-settings-rotation-confirm').addEventListener('change', event => { state.rotationConfirmed = event.currentTarget.checked; renderSecretDraft(selectedAgent()); }); byId('agent-settings-clear-secret').addEventListener('click', () => clearSecretDraft()); byId('agent-settings-copy-secret').addEventListener('click', () => { void copyDraft(state.secretDraft, '密钥').catch(error => notice(error.message, true)); }); byId('agent-settings-copy-config').addEventListener('click', () => { void copyDraft(configFragment(selectedAgent(), state.secretDraft), '配置片段').catch(error => notice(error.message, true)); }); diff --git a/web/resources/views/venix/admin/cloud/agents.tpl b/web/resources/views/venix/admin/cloud/agents.tpl index 4aa6a69d..cebc1b34 100644 --- a/web/resources/views/venix/admin/cloud/agents.tpl +++ b/web/resources/views/venix/admin/cloud/agents.tpl @@ -75,6 +75,7 @@ +
config.json;关闭抽屉后不再保留。