增强Salon Nacos回退读回校验

This commit is contained in:
2026-11-02 21:57:20 +08:00
parent 506fd8273f
commit e7e340b780
5 changed files with 76 additions and 4 deletions
+25
View File
@@ -0,0 +1,25 @@
import { createHash } from 'node:crypto';
export async function verifyConfigReadback(url, expectedContent, expectExists, options = {}) {
const request = options.request || fetch;
const attempts = options.attempts || 3;
const delayMs = options.delayMs ?? 1000;
const expectedHash = expectExists ? sha256(expectedContent) : null;
for (let attempt = 0; attempt < attempts; attempt += 1) {
const response = await request(url);
const actual = await response.text();
const matches = expectExists
? response.ok && sha256(actual) === expectedHash
: response.status === 404;
if (matches) return true;
if (attempt + 1 < attempts && delayMs > 0) {
await new Promise((resolve) => setTimeout(resolve, delayMs));
}
}
return false;
}
function sha256(value) {
return createHash('sha256').update(value ?? '', 'utf8').digest('hex');
}
+45
View File
@@ -0,0 +1,45 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { verifyConfigReadback } from './nacos-readback.mjs';
test('retries a stale read until the restored config is visible', async () => {
const responses = [
new Response('old config', { status: 200 }),
new Response('restored config', { status: 200 })
];
let calls = 0;
const verified = await verifyConfigReadback('http://nacos/config', 'restored config', true, {
request: async () => responses[calls++],
attempts: 3,
delayMs: 0
});
assert.equal(verified, true);
assert.equal(calls, 2);
});
test('fails closed when the config does not match after bounded retries', async () => {
let calls = 0;
const verified = await verifyConfigReadback('http://nacos/config', 'restored config', true, {
request: async () => {
calls += 1;
return new Response('different config', { status: 200 });
},
attempts: 3,
delayMs: 0
});
assert.equal(verified, false);
assert.equal(calls, 3);
});
test('confirms deletion only after the config is absent', async () => {
const verified = await verifyConfigReadback('http://nacos/config', null, false, {
request: async () => new Response(null, { status: 404 }),
attempts: 1,
delayMs: 0
});
assert.equal(verified, true);
});
+3 -3
View File
@@ -2,6 +2,7 @@ import { createHash } from 'node:crypto';
import { readFile, readdir, rm } from 'node:fs/promises';
import { join } from 'node:path';
import { envName, recordsRoot, snapshotRoot, writeJson } from './release-store.mjs';
import { verifyConfigReadback } from './nacos-readback.mjs';
const environment = envName(process.env.NACOS_ENVIRONMENT);
const group = 'DEFAULT_GROUP';
@@ -40,9 +41,8 @@ for (const target of targets) {
? await fetch(`${base}/nacos/v1/cs/configs?accessToken=${encodeURIComponent(target.token)}`, { method: 'POST', body: new URLSearchParams({ dataId: entry.dataId, group, tenant: entry.namespace, type: 'yaml', content }) })
: await fetch(`${base}/nacos/v1/cs/configs?${query}`, { method: 'DELETE' });
if (!response.ok || (exists && (await response.text()).trim() !== 'true')) throw new Error(`回退失败:${target.id}/${entry.namespace}/${entry.dataId}`);
const check = await fetch(`${base}/nacos/v1/cs/configs?${new URLSearchParams({ dataId: entry.dataId, group, tenant: entry.namespace, accessToken: target.token })}`);
const actual = await check.text();
if (exists ? (!check.ok || sha256(actual) !== sha256(content)) : check.status !== 404) throw new Error(`回退摘要校验失败:${target.id}/${entry.namespace}/${entry.dataId}`);
const checkUrl = `${base}/nacos/v1/cs/configs?${new URLSearchParams({ dataId: entry.dataId, group, tenant: entry.namespace, accessToken: target.token })}`;
if (!(await verifyConfigReadback(checkUrl, content, exists))) throw new Error(`回退摘要校验失败:${target.id}/${entry.namespace}/${entry.dataId}`);
}
}
}