-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest03RegionalSpokeSecurity.js
More file actions
180 lines (158 loc) · 5.37 KB
/
Copy pathtest03RegionalSpokeSecurity.js
File metadata and controls
180 lines (158 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import fetch from 'node-fetch';
import {
parseArgs,
signInternalWebhook
} from './dummyRtms.js';
const args = parseArgs(process.argv.slice(2));
const spokeUrl = args.spokeUrl || process.env.SPOKE_TEST_URL || 'http://127.0.0.1:4611/spoke/webhook';
const eventsUrl = args.eventsUrl || process.env.SPOKE_EVENTS_URL || 'http://127.0.0.1:4611/local/events';
const secret = args.secret || process.env.INTERNAL_WEBHOOK_TEST_SECRET || process.env.INTERNAL_WEBHOOK_SECRET || 'testsecrettoken';
const wrongSecret = args.wrongSecret || 'wronginternalsecret';
const timeoutMs = Number(args.timeoutMs || 5000);
const pollIntervalMs = Number(args.pollIntervalMs || 200);
const staleSeconds = Number(args.staleSeconds || 1000);
const results = [];
await testSpokeHealth();
await testMissingSignatureRejected();
await testInvalidSignatureRejected();
await testStaleSignatureRejected();
await testValidSignedEnvelopeAccepted();
const failed = results.filter((result) => !result.ok);
for (const result of results) {
const marker = result.ok ? 'PASS' : 'FAIL';
console.log(`${marker} ${result.name} status=${result.status}${result.reason ? ` reason=${result.reason}` : ''}`);
}
if (failed.length > 0) {
console.error(`03 regional spoke security tester failed: ${failed.length}/${results.length}`);
process.exit(1);
}
console.log(`03 regional spoke security tester passed: ${results.length}/${results.length}`);
async function testSpokeHealth() {
const healthUrl = new URL('/health', spokeUrl).toString();
const response = await fetch(healthUrl);
const body = await readJson(response);
record({
name: 'spoke_health_signature_required',
status: response.status,
ok: response.status === 200 &&
body?.ok === true &&
body?.service === 'regional-webhook-spoke' &&
body?.internalSignatureVerification === 'required',
reason: body?.internalSignatureVerification
});
}
async function testMissingSignatureRejected() {
const response = await postEnvelope(buildEnvelope('missing-signature'), null);
const body = await readJson(response);
record({
name: 'missing_internal_signature_rejected',
status: response.status,
reason: body?.reason,
ok: response.status === 401 && body?.reason === 'missing_x-rtms-signature'
});
}
async function testInvalidSignatureRejected() {
const response = await postEnvelope(buildEnvelope('invalid-signature'), wrongSecret);
const body = await readJson(response);
record({
name: 'invalid_internal_signature_rejected',
status: response.status,
reason: body?.reason,
ok: response.status === 401 && body?.reason === 'invalid_x-rtms-signature'
});
}
async function testStaleSignatureRejected() {
const staleTimestamp = Math.floor(Date.now() / 1000) - staleSeconds;
const response = await postEnvelope(buildEnvelope('stale-signature'), secret, staleTimestamp);
const body = await readJson(response);
record({
name: 'stale_internal_signature_rejected',
status: response.status,
reason: body?.reason,
ok: response.status === 401 && body?.reason === 'stale_x-rtms-request-timestamp'
});
}
async function testValidSignedEnvelopeAccepted() {
const streamId = `test03-valid-${Date.now()}`;
const envelope = buildEnvelope(streamId);
const response = await postEnvelope(envelope, secret);
const seen = response.status === 202 && await waitForSpokeEvent(streamId);
record({
name: 'valid_internal_signature_accepted',
status: response.status,
ok: seen,
reason: seen ? undefined : 'stream_not_seen_at_spoke'
});
}
async function postEnvelope(envelope, signingSecret, timestamp) {
const bodyText = JSON.stringify(envelope);
const headers = { 'content-type': 'application/json' };
if (signingSecret) {
Object.assign(headers, signInternalWebhook(bodyText, signingSecret, timestamp));
}
return fetch(spokeUrl, {
method: 'POST',
headers,
body: bodyText
});
}
async function waitForSpokeEvent(streamId) {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
try {
const response = await fetch(eventsUrl);
const body = await response.json();
const found = Array.isArray(body.events) && body.events.some((event) => event.streamId === streamId);
if (found) return true;
} catch {
// Keep polling until timeout; the caller reports the failed route.
}
await sleep(pollIntervalMs);
}
return false;
}
function buildEnvelope(streamId) {
return {
schemaVersion: 1,
source: 'test03',
event: 'meeting.rtms_started',
eventType: 'start',
productType: 'meeting',
rtmsId: `meeting-${streamId}`,
streamId,
regionCode: 'IAD',
idempotencyKey: `test03:${streamId}`,
receivedAt: new Date().toISOString(),
eventTs: Date.now(),
webhook: {
event: 'meeting.rtms_started',
payload: {
meeting_uuid: `meeting-${streamId}`,
rtms_stream_id: streamId,
event_ts: Date.now(),
server_urls: {
signaling: 'wss://iad.zoom.us/rtms/signaling'
}
}
},
payload: {
meeting_uuid: `meeting-${streamId}`,
rtms_stream_id: streamId
}
};
}
function record(result) {
results.push(result);
}
async function readJson(response) {
const text = await response.text();
if (!text) return null;
try {
return JSON.parse(text);
} catch {
return null;
}
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}