Skip to content

Commit a36644b

Browse files
committed
Add required authorization header to queue-status endpoint, using new shared secret.
1 parent 908746b commit a36644b

15 files changed

+120
-46
lines changed

packages/matrix/helpers/isolated-realm-server.ts

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export async function startServer() {
2727
process.env.PGPORT = '5435';
2828
process.env.PGDATABASE = `test_db_${Math.floor(10000000 * Math.random())}`;
2929
process.env.NODE_NO_WARNINGS = '1';
30+
process.env.REALM_SERVER_SECRET_SEED = "mum's the word";
3031
process.env.REALM_SECRET_SEED = "shhh! it's a secret";
3132
process.env.MATRIX_URL = 'http://localhost:8008';
3233
process.env.REALM_SERVER_MATRIX_USERNAME = 'realm_server';

packages/realm-server/handlers/handle-create-session.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ const log = logger('realm-server');
1616

1717
export default function handleCreateSessionRequest({
1818
matrixClient,
19-
secretSeed,
19+
realmSecretSeed,
2020
}: CreateRoutesArgs): (ctxt: Koa.Context, next: Koa.Next) => Promise<void> {
2121
let matrixBackendAuthentication = new MatrixBackendAuthentication(
2222
matrixClient,
23-
secretSeed,
23+
realmSecretSeed,
2424
{
2525
badRequest: function (message: string) {
2626
return new Response(JSON.stringify({ errors: message }), {
@@ -36,7 +36,7 @@ export default function handleCreateSessionRequest({
3636
return new Response(body, init);
3737
},
3838
createJWT: async (user: string, sessionRoom: string) =>
39-
createJWT({ user, sessionRoom }, secretSeed),
39+
createJWT({ user, sessionRoom }, realmSecretSeed),
4040
} as Utils,
4141
);
4242

packages/realm-server/handlers/handle-queue-status.ts

+19
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,30 @@ import Koa from 'koa';
22
import { query, SupportedMimeType } from '@cardstack/runtime-common';
33
import { setContextResponse } from '../middleware';
44
import { CreateRoutesArgs } from '../routes';
5+
import { monitoringAuthToken } from '../utils/monitoring';
56

7+
function isAuthorizedToViewMonitoring(
8+
request: Koa.Request,
9+
realmServerSecretSeed: string,
10+
): boolean {
11+
return (
12+
request.headers['authorization'] ===
13+
`Bearer ${monitoringAuthToken(realmServerSecretSeed)}`
14+
);
15+
}
616
export default function handleQueueStatusRequest({
717
dbAdapter,
18+
realmServerSecretSeed,
819
}: CreateRoutesArgs): (ctxt: Koa.Context, next: Koa.Next) => Promise<void> {
920
return async function (ctxt: Koa.Context, _next: Koa.Next) {
21+
if (!isAuthorizedToViewMonitoring(ctxt.request, realmServerSecretSeed)) {
22+
return setContextResponse(
23+
ctxt,
24+
new Response('Unauthorized', {
25+
status: 401,
26+
}),
27+
);
28+
}
1029
let [{ pending_job_count }] = (await query(dbAdapter, [
1130
`SELECT COUNT(*) as pending_job_count FROM jobs WHERE status='unfulfilled'`,
1231
])) as {

packages/realm-server/main.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ if (process.env.NODE_ENV === 'test') {
2323
(globalThis as any).__environment = 'test';
2424
}
2525

26+
const REALM_SERVER_SECRET_SEED = process.env.REALM_SERVER_SECRET_SEED;
27+
if (!REALM_SERVER_SECRET_SEED) {
28+
console.error(
29+
`The REALM_SERVER_SECRET_SEED environment variable is not set. Please make sure this env var has a value`,
30+
);
31+
process.exit(-1);
32+
}
33+
2634
const REALM_SECRET_SEED = process.env.REALM_SECRET_SEED;
2735
if (!REALM_SECRET_SEED) {
2836
console.error(
@@ -253,7 +261,8 @@ let autoMigrate = migrateDB || undefined;
253261
virtualNetwork,
254262
matrixClient,
255263
realmsRootPath,
256-
secretSeed: REALM_SECRET_SEED,
264+
realmServerSecretSeed: REALM_SERVER_SECRET_SEED,
265+
realmSecretSeed: REALM_SECRET_SEED,
257266
dbAdapter,
258267
queue,
259268
assetsURL: dist,

packages/realm-server/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.0.0",
44
"license": "MIT",
55
"devDependencies": {
6+
"@aws-crypto/sha256-js": "^5.2.0",
67
"@cardstack/billing": "workspace:*",
78
"@cardstack/boxel-icons": "workspace:*",
89
"@cardstack/boxel-ui": "workspace:*",

packages/realm-server/routes.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import handleQueueStatusRequest from './handlers/handle-queue-status';
1515
export type CreateRoutesArgs = {
1616
dbAdapter: DBAdapter;
1717
matrixClient: MatrixClient;
18-
secretSeed: string;
18+
realmServerSecretSeed: string;
19+
realmSecretSeed: string;
1920
virtualNetwork: VirtualNetwork;
2021
createRealm: ({
2122
ownerUserId,
@@ -45,20 +46,20 @@ export function createRoutes(args: CreateRoutesArgs) {
4546
router.post('/_server-session', handleCreateSessionRequest(args));
4647
router.post(
4748
'/_create-realm',
48-
jwtMiddleware(args.secretSeed),
49+
jwtMiddleware(args.realmSecretSeed),
4950
handleCreateRealmRequest(args),
5051
);
5152
router.get('/_catalog-realms', handleFetchCatalogRealmsRequest(args));
5253
router.get('/_queue-status', handleQueueStatusRequest(args));
5354
router.post('/_stripe-webhook', handleStripeWebhookRequest(args));
5455
router.get(
5556
'/_user',
56-
jwtMiddleware(args.secretSeed),
57+
jwtMiddleware(args.realmSecretSeed),
5758
handleFetchUserRequest(args),
5859
);
5960
router.post(
6061
'/_user',
61-
jwtMiddleware(args.secretSeed),
62+
jwtMiddleware(args.realmSecretSeed),
6263
handleCreateUserRequest(args),
6364
);
6465
router.get('/_stripe-links', handleStripeLinksRequest());

packages/realm-server/scripts/start-base.sh

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ NODE_ENV=development \
1313
NODE_NO_WARNINGS=1 \
1414
PGPORT=5435 \
1515
PGDATABASE=boxel_base \
16+
REALM_SERVER_SECRET_SEED="mum's the word" \
1617
REALM_SECRET_SEED="shhh! it's a secret" \
1718
MATRIX_URL=http://localhost:8008 \
1819
REALM_SERVER_MATRIX_USERNAME=realm_server \

packages/realm-server/scripts/start-development.sh

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ NODE_ENV=development \
1414
PGPORT=5435 \
1515
PGDATABASE=boxel \
1616
LOG_LEVELS='*=info' \
17+
REALM_SERVER_SECRET_SEED="mum's the word" \
1718
REALM_SECRET_SEED="shhh! it's a secret" \
1819
MATRIX_URL=http://localhost:8008 \
1920
REALM_SERVER_MATRIX_USERNAME=realm_server \

packages/realm-server/scripts/start-test-realms.sh

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ NODE_ENV=test \
1313
PGPORT=5435 \
1414
PGDATABASE=boxel_test \
1515
NODE_NO_WARNINGS=1 \
16+
REALM_SERVER_SECRET_SEED="mum's the word" \
1617
REALM_SECRET_SEED="shhh! it's a secret" \
1718
MATRIX_URL=http://localhost:8008 \
1819
REALM_SERVER_MATRIX_USERNAME=realm_server \

packages/realm-server/server.ts

+13-8
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ export class RealmServer {
5959
private realms: Realm[];
6060
private virtualNetwork: VirtualNetwork;
6161
private matrixClient: MatrixClient;
62-
private secretSeed: string;
62+
private realmServerSecretSeed: string;
63+
private realmSecretSeed: string;
6364
private realmsRootPath: string;
6465
private dbAdapter: DBAdapter;
6566
private queue: QueuePublisher;
@@ -79,7 +80,8 @@ export class RealmServer {
7980
realms,
8081
virtualNetwork,
8182
matrixClient,
82-
secretSeed,
83+
realmServerSecretSeed,
84+
realmSecretSeed,
8385
realmsRootPath,
8486
dbAdapter,
8587
queue,
@@ -94,7 +96,8 @@ export class RealmServer {
9496
realms: Realm[];
9597
virtualNetwork: VirtualNetwork;
9698
matrixClient: MatrixClient;
97-
secretSeed: string;
99+
realmServerSecretSeed: string;
100+
realmSecretSeed: string;
98101
realmsRootPath: string;
99102
dbAdapter: DBAdapter;
100103
queue: QueuePublisher;
@@ -116,7 +119,8 @@ export class RealmServer {
116119
this.serverURL = serverURL;
117120
this.virtualNetwork = virtualNetwork;
118121
this.matrixClient = matrixClient;
119-
this.secretSeed = secretSeed;
122+
this.realmSecretSeed = realmSecretSeed;
123+
this.realmServerSecretSeed = realmServerSecretSeed;
120124
this.realmsRootPath = realmsRootPath;
121125
this.seedPath = seedPath;
122126
this.seedRealmURL = seedRealmURL;
@@ -164,7 +168,8 @@ export class RealmServer {
164168
createRoutes({
165169
dbAdapter: this.dbAdapter,
166170
matrixClient: this.matrixClient,
167-
secretSeed: this.secretSeed,
171+
realmServerSecretSeed: this.realmServerSecretSeed,
172+
realmSecretSeed: this.realmSecretSeed,
168173
virtualNetwork: this.virtualNetwork,
169174
createRealm: this.createRealm,
170175
serveIndex: this.serveIndex,
@@ -326,7 +331,7 @@ export class RealmServer {
326331
matrixURL: this.matrixClient.matrixURL,
327332
displayname: username,
328333
username,
329-
password: await passwordFromSeed(username, this.secretSeed),
334+
password: await passwordFromSeed(username, this.realmSecretSeed),
330335
registrationSecret: await this.getMatrixRegistrationSecret(),
331336
});
332337
this.log.debug(`created realm bot user '${userId}' for new realm ${url}`);
@@ -370,7 +375,7 @@ export class RealmServer {
370375
{
371376
url,
372377
adapter,
373-
secretSeed: this.secretSeed,
378+
secretSeed: this.realmSecretSeed,
374379
virtualNetwork: this.virtualNetwork,
375380
dbAdapter: this.dbAdapter,
376381
queue: this.queue,
@@ -430,7 +435,7 @@ export class RealmServer {
430435
let realm = new Realm({
431436
url,
432437
adapter,
433-
secretSeed: this.secretSeed,
438+
secretSeed: this.realmSecretSeed,
434439
virtualNetwork: this.virtualNetwork,
435440
dbAdapter: this.dbAdapter,
436441
queue: this.queue,

packages/realm-server/tests/helpers/index.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ export const realmServerTestMatrix: MatrixConfig = {
6161
url: matrixURL,
6262
username: 'node-test_realm-server',
6363
};
64-
export const secretSeed = `shhh! it's a secret`;
64+
export const realmServerSecretSeed = "mum's the word";
65+
export const realmSecretSeed = `shhh! it's a secret`;
6566
export const matrixRegistrationSecret: string =
6667
getSynapseConfig()!.registration_shared_secret; // as long as synapse has been started at least once, this will always exist
6768

@@ -236,14 +237,14 @@ export async function createRealm({
236237
indexRunner,
237238
virtualNetwork,
238239
matrixURL: matrixConfig.url,
239-
secretSeed,
240+
secretSeed: realmSecretSeed,
240241
});
241242
}
242243
let realm = new Realm({
243244
url: realmURL,
244245
adapter,
245246
matrix: matrixConfig,
246-
secretSeed,
247+
secretSeed: realmSecretSeed,
247248
virtualNetwork,
248249
dbAdapter,
249250
queue: publisher,
@@ -299,7 +300,7 @@ export async function runBaseRealmServer(
299300
indexRunner,
300301
virtualNetwork,
301302
matrixURL,
302-
secretSeed,
303+
secretSeed: realmSecretSeed,
303304
});
304305
let testBaseRealm = await createRealm({
305306
dir: basePath,
@@ -316,13 +317,14 @@ export async function runBaseRealmServer(
316317
let matrixClient = new MatrixClient({
317318
matrixURL: realmServerTestMatrix.url,
318319
username: realmServerTestMatrix.username,
319-
seed: secretSeed,
320+
seed: realmSecretSeed,
320321
});
321322
let testBaseRealmServer = new RealmServer({
322323
realms: [testBaseRealm],
323324
virtualNetwork,
324325
matrixClient,
325-
secretSeed,
326+
realmServerSecretSeed,
327+
realmSecretSeed,
326328
matrixRegistrationSecret,
327329
realmsRootPath,
328330
dbAdapter,
@@ -367,7 +369,7 @@ export async function runTestRealmServer({
367369
indexRunner,
368370
virtualNetwork,
369371
matrixURL,
370-
secretSeed,
372+
secretSeed: realmSecretSeed,
371373
});
372374
await worker.run();
373375
let testRealm = await createRealm({
@@ -402,13 +404,14 @@ export async function runTestRealmServer({
402404
let matrixClient = new MatrixClient({
403405
matrixURL: realmServerTestMatrix.url,
404406
username: realmServerTestMatrix.username,
405-
seed: secretSeed,
407+
seed: realmSecretSeed,
406408
});
407409
let testRealmServer = new RealmServer({
408410
realms,
409411
virtualNetwork,
410412
matrixClient,
411-
secretSeed,
413+
realmServerSecretSeed,
414+
realmSecretSeed,
412415
matrixRegistrationSecret,
413416
realmsRootPath,
414417
dbAdapter,

packages/realm-server/tests/realm-endpoints-test.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ import {
3939
setupDB,
4040
createRealm,
4141
realmServerTestMatrix,
42-
secretSeed,
42+
realmServerSecretSeed,
43+
realmSecretSeed,
4344
createVirtualNetwork,
4445
createVirtualNetworkAndLoader,
4546
matrixURL,
@@ -3756,14 +3757,15 @@ module(basename(__filename), function () {
37563757
let matrixClient = new MatrixClient({
37573758
matrixURL: realmServerTestMatrix.url,
37583759
username: realmServerTestMatrix.username,
3759-
seed: secretSeed,
3760+
seed: realmSecretSeed,
37603761
});
37613762
let getIndexHTML = (await getFastbootState()).getIndexHTML;
37623763
testRealmServer = new RealmServer({
37633764
realms: [base, testRealm],
37643765
virtualNetwork,
37653766
matrixClient,
3766-
secretSeed,
3767+
realmServerSecretSeed,
3768+
realmSecretSeed,
37673769
matrixRegistrationSecret,
37683770
realmsRootPath: dir.name,
37693771
dbAdapter,

0 commit comments

Comments
 (0)