feat(federation): POST /peers/:id/recheck — manual reachability probe
This commit is contained in:
@@ -0,0 +1,154 @@
|
|||||||
|
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||||
|
import Fastify, { type FastifyInstance } from 'fastify';
|
||||||
|
import Database from 'better-sqlite3';
|
||||||
|
import { drizzle } from 'drizzle-orm/better-sqlite3';
|
||||||
|
import { eq } from 'drizzle-orm';
|
||||||
|
import fs from 'node:fs';
|
||||||
|
import path from 'node:path';
|
||||||
|
import { fileURLToPath } from 'node:url';
|
||||||
|
import * as schema from '../db/schema.js';
|
||||||
|
import { setWorkerId } from '../utils/snowflake.js';
|
||||||
|
|
||||||
|
setWorkerId(1);
|
||||||
|
|
||||||
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||||
|
|
||||||
|
type TestDb = ReturnType<typeof drizzle<typeof schema>>;
|
||||||
|
let sqlite: Database.Database;
|
||||||
|
let testDb: TestDb;
|
||||||
|
|
||||||
|
vi.mock('../db/index.js', () => ({
|
||||||
|
getDb: () => testDb,
|
||||||
|
getRawDb: () => sqlite,
|
||||||
|
schema,
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock('../config.js', () => ({
|
||||||
|
config: {
|
||||||
|
domain: 'local.example',
|
||||||
|
port: 3000,
|
||||||
|
host: '0.0.0.0',
|
||||||
|
jwtSecret: 'test-secret-12345678901234567890123456789012',
|
||||||
|
maxUploadSize: 100 * 1024 * 1024,
|
||||||
|
registrationOpen: true,
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock('../utils/auth.js', () => ({
|
||||||
|
authenticate: async (req: { userId?: string }) => {
|
||||||
|
req.userId = 'admin-user';
|
||||||
|
},
|
||||||
|
requireAdmin: async () => {},
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock('../utils/federationAuth.js', async () => {
|
||||||
|
const actual = await vi.importActual<typeof import('../utils/federationAuth.js')>('../utils/federationAuth.js');
|
||||||
|
return {
|
||||||
|
...actual,
|
||||||
|
getOurOrigin: () => 'https://local.example',
|
||||||
|
generateHmacSecret: () => 'mock-generated-secret',
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
vi.mock('../ws/handler.js', () => ({
|
||||||
|
connectionManager: {
|
||||||
|
sendToAdmins: vi.fn(),
|
||||||
|
getAllOnlineUserIds: () => [],
|
||||||
|
sendToUser: vi.fn(),
|
||||||
|
sendToDmMembers: vi.fn(),
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock('../utils/federationPeerActivation.js', () => ({
|
||||||
|
onPeerActivated: vi.fn(async () => undefined),
|
||||||
|
onPeerDeactivated: vi.fn(async () => undefined),
|
||||||
|
}));
|
||||||
|
|
||||||
|
function applyMigrations(db: Database.Database): void {
|
||||||
|
const migrationsDir = path.resolve(__dirname, '../../drizzle');
|
||||||
|
const files = fs.readdirSync(migrationsDir).filter(f => f.endsWith('.sql')).sort();
|
||||||
|
for (const f of files) {
|
||||||
|
const sqlText = fs.readFileSync(path.join(migrationsDir, f), 'utf8');
|
||||||
|
const statements = sqlText.split(/-->\s*statement-breakpoint/);
|
||||||
|
for (const stmt of statements) {
|
||||||
|
const clean = stmt.trim();
|
||||||
|
if (clean) db.exec(clean);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function seedInstanceSettings(): void {
|
||||||
|
testDb.insert(schema.instanceSettings).values({
|
||||||
|
id: 1,
|
||||||
|
instanceName: 'Local Backspace',
|
||||||
|
autoAcceptPeering: 0,
|
||||||
|
registrationOpen: 1,
|
||||||
|
updatedAt: Date.now(),
|
||||||
|
}).run();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function buildApp(): Promise<FastifyInstance> {
|
||||||
|
const app = Fastify({ logger: false });
|
||||||
|
const { federationRoutes } = await import('./federation.js');
|
||||||
|
await app.register(federationRoutes);
|
||||||
|
await app.ready();
|
||||||
|
return app;
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('POST /api/federation/peers/:id/recheck', () => {
|
||||||
|
let app: FastifyInstance;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
sqlite = new Database(':memory:');
|
||||||
|
testDb = drizzle(sqlite, { schema });
|
||||||
|
applyMigrations(sqlite);
|
||||||
|
seedInstanceSettings();
|
||||||
|
app = await buildApp();
|
||||||
|
});
|
||||||
|
afterEach(async () => { await app.close(); });
|
||||||
|
|
||||||
|
function seedUnreachable(id: string): void {
|
||||||
|
testDb.insert(schema.federationPeers).values({
|
||||||
|
id, origin: 'https://peer.example', hmacSecret: 'secret',
|
||||||
|
status: 'unreachable', consecutiveFailures: 10, probeAttempts: 2, lastProbeAt: 1,
|
||||||
|
lastSyncedAt: Date.now(), createdAt: Date.now(),
|
||||||
|
}).run();
|
||||||
|
}
|
||||||
|
|
||||||
|
it('returns 400 for a non-unreachable peer', async () => {
|
||||||
|
testDb.insert(schema.federationPeers).values({
|
||||||
|
id: 'p-active', origin: 'https://peer.example', hmacSecret: 'secret',
|
||||||
|
status: 'active', lastSyncedAt: Date.now(), createdAt: Date.now(),
|
||||||
|
}).run();
|
||||||
|
const res = await app.inject({ method: 'POST', url: '/api/federation/peers/p-active/recheck' });
|
||||||
|
expect(res.statusCode).toBe(400);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns 404 for an unknown peer', async () => {
|
||||||
|
const res = await app.inject({ method: 'POST', url: '/api/federation/peers/nope/recheck' });
|
||||||
|
expect(res.statusCode).toBe(404);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('recovers the peer when the probe succeeds', async () => {
|
||||||
|
seedUnreachable('p-rec');
|
||||||
|
vi.spyOn(globalThis, 'fetch').mockResolvedValue(new Response('{}', { status: 200 }));
|
||||||
|
const res = await app.inject({ method: 'POST', url: '/api/federation/peers/p-rec/recheck' });
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(res.json()).toEqual({ recovered: true, status: 'active' });
|
||||||
|
const row = testDb.select().from(schema.federationPeers)
|
||||||
|
.where(eq(schema.federationPeers.id, 'p-rec')).get()!;
|
||||||
|
expect(row.status).toBe('active');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('stays unreachable and advances pacing when the probe fails', async () => {
|
||||||
|
seedUnreachable('p-fail');
|
||||||
|
vi.spyOn(globalThis, 'fetch').mockRejectedValue(new Error('down'));
|
||||||
|
const res = await app.inject({ method: 'POST', url: '/api/federation/peers/p-fail/recheck' });
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(res.json()).toEqual({ recovered: false, status: 'unreachable' });
|
||||||
|
const row = testDb.select().from(schema.federationPeers)
|
||||||
|
.where(eq(schema.federationPeers.id, 'p-fail')).get()!;
|
||||||
|
expect(row.probeAttempts).toBe(3);
|
||||||
|
expect(row.lastProbeAt).toBeGreaterThan(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -19,6 +19,7 @@ import { deleteAttachmentFiles, deleteUploadFile } from '../utils/fileCleanup.js
|
|||||||
import { tombstoneUser, collectDeletionBroadcastTargets, collectProfileBroadcastTargetIds } from '../utils/userDeletion.js';
|
import { tombstoneUser, collectDeletionBroadcastTargets, collectProfileBroadcastTargetIds } from '../utils/userDeletion.js';
|
||||||
import { computeFederatedId, getDmParticipants, sendCallRelay } from '../utils/federationOutbox.js';
|
import { computeFederatedId, getDmParticipants, sendCallRelay } from '../utils/federationOutbox.js';
|
||||||
import { onPeerActivated, onPeerDeactivated } from '../utils/federationPeerActivation.js';
|
import { onPeerActivated, onPeerDeactivated } from '../utils/federationPeerActivation.js';
|
||||||
|
import { probePeerReachable, markPeerRecovered } from '../utils/federationRecovery.js';
|
||||||
import { getDmMessageWithUser } from './dm.js';
|
import { getDmMessageWithUser } from './dm.js';
|
||||||
import type { FederationRelayRequest, FederationRelayResponse, FederationRelayEvent, FederationRelayAttachment, FederationSyncRequest, FederationSyncResponse, DmMessageWithUser, DmChannel, FederationRelayProfileSnapshot, FederationIdentityDeleteS2SRequest, FederationProfileUpdatePayload, ServerEvent, ApprovalRequestSubscriberSummary, PeeringTriggerReason } from '@backspace/shared';
|
import type { FederationRelayRequest, FederationRelayResponse, FederationRelayEvent, FederationRelayAttachment, FederationSyncRequest, FederationSyncResponse, DmMessageWithUser, DmChannel, FederationRelayProfileSnapshot, FederationIdentityDeleteS2SRequest, FederationProfileUpdatePayload, ServerEvent, ApprovalRequestSubscriberSummary, PeeringTriggerReason } from '@backspace/shared';
|
||||||
import { GROUP_DM_NAME_MIN_LENGTH, GROUP_DM_NAME_MAX_LENGTH } from '@backspace/shared/src/constants.js';
|
import { GROUP_DM_NAME_MIN_LENGTH, GROUP_DM_NAME_MAX_LENGTH } from '@backspace/shared/src/constants.js';
|
||||||
@@ -1565,6 +1566,51 @@ export async function federationRoutes(app: FastifyInstance): Promise<void> {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// ─── POST /api/federation/peers/:id/recheck ────────────────────────────────
|
||||||
|
// Admin-only: run an immediate reachability probe on an unreachable peer.
|
||||||
|
// On success the peer transitions to active (outbox flushes on the next tick).
|
||||||
|
app.post<{ Params: { id: string } }>(
|
||||||
|
'/api/federation/peers/:id/recheck',
|
||||||
|
{ preHandler: [authenticate, requireAdmin] },
|
||||||
|
async (request, reply) => {
|
||||||
|
const { id } = request.params;
|
||||||
|
const db = getDb();
|
||||||
|
|
||||||
|
const peer = db
|
||||||
|
.select()
|
||||||
|
.from(schema.federationPeers)
|
||||||
|
.where(eq(schema.federationPeers.id, id))
|
||||||
|
.get();
|
||||||
|
|
||||||
|
if (!peer) {
|
||||||
|
return reply.code(404).send({ error: 'Peer not found', statusCode: 404 });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (peer.status !== 'unreachable') {
|
||||||
|
return reply.code(400).send({
|
||||||
|
error: 'Recheck is only available for unreachable peers.',
|
||||||
|
statusCode: 400,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const reachable = await probePeerReachable(peer.origin);
|
||||||
|
|
||||||
|
if (reachable) {
|
||||||
|
await markPeerRecovered(peer.id);
|
||||||
|
return reply.code(200).send({ recovered: true, status: 'active' });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Probe failed — advance pacing so a manual attempt stays consistent with
|
||||||
|
// the recovery worker's schedule.
|
||||||
|
db.update(schema.federationPeers)
|
||||||
|
.set({ probeAttempts: peer.probeAttempts + 1, lastProbeAt: Date.now() })
|
||||||
|
.where(eq(schema.federationPeers.id, peer.id))
|
||||||
|
.run();
|
||||||
|
|
||||||
|
return reply.code(200).send({ recovered: false, status: 'unreachable' });
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
// ─── PATCH /api/federation/peers/:id ────────────────────────────────────────
|
// ─── PATCH /api/federation/peers/:id ────────────────────────────────────────
|
||||||
// Admin-only: update peer settings (e.g. auto-rotation interval).
|
// Admin-only: update peer settings (e.g. auto-rotation interval).
|
||||||
app.patch<{ Params: { id: string }; Body: { autoRotateIntervalDays?: number } }>(
|
app.patch<{ Params: { id: string }; Body: { autoRotateIntervalDays?: number } }>(
|
||||||
|
|||||||
Reference in New Issue
Block a user