2023-11-22 19:49:51 +08:00
|
|
|
/*
|
|
|
|
Copyright 2023 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-11-27 22:40:45 +08:00
|
|
|
import * as path from "node:path";
|
|
|
|
import * as os from "node:os";
|
|
|
|
import * as crypto from "node:crypto";
|
2023-11-22 19:49:51 +08:00
|
|
|
import * as fse from "fs-extra";
|
|
|
|
import { APIRequestContext } from "@playwright/test";
|
|
|
|
|
2023-11-27 22:40:45 +08:00
|
|
|
import { getFreePort } from "../../utils/port";
|
|
|
|
import { Docker } from "../../docker";
|
|
|
|
import { HomeserverConfig, HomeserverInstance, Homeserver, StartHomeserverOpts, Credentials } from "..";
|
|
|
|
import { randB64Bytes } from "../../utils/rand";
|
2023-11-22 19:49:51 +08:00
|
|
|
|
2024-08-07 01:02:50 +08:00
|
|
|
// Docker tag to use for synapse docker image.
|
2024-05-30 18:34:34 +08:00
|
|
|
// We target a specific digest as every now and then a Synapse update will break our CI.
|
|
|
|
// This digest is updated by the playwright-image-updates.yaml workflow periodically.
|
2024-09-06 17:38:36 +08:00
|
|
|
const DOCKER_TAG = "develop@sha256:c826fb82717a26e6dbb42b566c864064e2b67d28e1dae092908ce01ab84233c5";
|
2024-05-30 18:34:34 +08:00
|
|
|
|
2023-12-16 18:01:26 +08:00
|
|
|
async function cfgDirFromTemplate(opts: StartHomeserverOpts): Promise<Omit<HomeserverConfig, "dockerUrl">> {
|
2023-11-22 19:49:51 +08:00
|
|
|
const templateDir = path.join(__dirname, "templates", opts.template);
|
|
|
|
|
|
|
|
const stats = await fse.stat(templateDir);
|
|
|
|
if (!stats?.isDirectory) {
|
|
|
|
throw new Error(`No such template: ${opts.template}`);
|
|
|
|
}
|
|
|
|
const tempDir = await fse.mkdtemp(path.join(os.tmpdir(), "react-sdk-synapsedocker-"));
|
|
|
|
|
|
|
|
// copy the contents of the template dir, omitting homeserver.yaml as we'll template that
|
|
|
|
console.log(`Copy ${templateDir} -> ${tempDir}`);
|
|
|
|
await fse.copy(templateDir, tempDir, { filter: (f) => path.basename(f) !== "homeserver.yaml" });
|
|
|
|
|
|
|
|
const registrationSecret = randB64Bytes(16);
|
|
|
|
const macaroonSecret = randB64Bytes(16);
|
|
|
|
const formSecret = randB64Bytes(16);
|
|
|
|
|
|
|
|
const port = await getFreePort();
|
|
|
|
const baseUrl = `http://localhost:${port}`;
|
|
|
|
|
|
|
|
// now copy homeserver.yaml, applying substitutions
|
|
|
|
const templateHomeserver = path.join(templateDir, "homeserver.yaml");
|
|
|
|
const outputHomeserver = path.join(tempDir, "homeserver.yaml");
|
|
|
|
console.log(`Gen ${templateHomeserver} -> ${outputHomeserver}`);
|
|
|
|
let hsYaml = await fse.readFile(templateHomeserver, "utf8");
|
|
|
|
hsYaml = hsYaml.replace(/{{REGISTRATION_SECRET}}/g, registrationSecret);
|
|
|
|
hsYaml = hsYaml.replace(/{{MACAROON_SECRET_KEY}}/g, macaroonSecret);
|
|
|
|
hsYaml = hsYaml.replace(/{{FORM_SECRET}}/g, formSecret);
|
|
|
|
hsYaml = hsYaml.replace(/{{PUBLIC_BASEURL}}/g, baseUrl);
|
|
|
|
if (opts.oAuthServerPort) {
|
|
|
|
hsYaml = hsYaml.replace(/{{OAUTH_SERVER_PORT}}/g, opts.oAuthServerPort.toString());
|
|
|
|
}
|
|
|
|
if (opts.variables) {
|
|
|
|
for (const key in opts.variables) {
|
2024-02-20 22:21:10 +08:00
|
|
|
hsYaml = hsYaml.replace(new RegExp("%" + key + "%", "g"), String(opts.variables[key]));
|
2023-11-22 19:49:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await fse.writeFile(outputHomeserver, hsYaml);
|
|
|
|
|
|
|
|
// now generate a signing key (we could use synapse's config generation for
|
|
|
|
// this, or we could just do this...)
|
|
|
|
// NB. This assumes the homeserver.yaml specifies the key in this location
|
|
|
|
const signingKey = randB64Bytes(32);
|
|
|
|
const outputSigningKey = path.join(tempDir, "localhost.signing.key");
|
|
|
|
console.log(`Gen -> ${outputSigningKey}`);
|
|
|
|
await fse.writeFile(outputSigningKey, `ed25519 x ${signingKey}`);
|
|
|
|
|
2023-12-15 22:30:59 +08:00
|
|
|
// Allow anyone to read, write and execute in the /temp/react-sdk-synapsedocker-xxx directory
|
|
|
|
// so that the DIND setup that we use to update the playwright screenshots work without any issues.
|
|
|
|
await fse.chmod(tempDir, 0o757);
|
|
|
|
|
2023-11-22 19:49:51 +08:00
|
|
|
return {
|
|
|
|
port,
|
|
|
|
baseUrl,
|
|
|
|
configDir: tempDir,
|
|
|
|
registrationSecret,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Synapse implements Homeserver, HomeserverInstance {
|
2023-11-27 22:40:45 +08:00
|
|
|
protected docker: Docker = new Docker();
|
|
|
|
public config: HomeserverConfig & { serverId: string };
|
2023-11-22 19:49:51 +08:00
|
|
|
|
2024-07-05 17:51:21 +08:00
|
|
|
private adminToken?: string;
|
|
|
|
|
2023-11-22 19:49:51 +08:00
|
|
|
public constructor(private readonly request: APIRequestContext) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start a synapse instance: the template must be the name of
|
|
|
|
* one of the templates in the playwright/plugins/synapsedocker/templates
|
|
|
|
* directory.
|
|
|
|
*/
|
|
|
|
public async start(opts: StartHomeserverOpts): Promise<HomeserverInstance> {
|
|
|
|
if (this.config) await this.stop();
|
|
|
|
|
|
|
|
const synCfg = await cfgDirFromTemplate(opts);
|
|
|
|
console.log(`Starting synapse with config dir ${synCfg.configDir}...`);
|
2024-02-20 22:21:10 +08:00
|
|
|
const dockerSynapseParams = ["-v", `${synCfg.configDir}:/data`, "-p", `${synCfg.port}:8008/tcp`];
|
2023-11-22 19:49:51 +08:00
|
|
|
const synapseId = await this.docker.run({
|
2024-08-07 01:02:50 +08:00
|
|
|
image: `ghcr.io/element-hq/synapse:${DOCKER_TAG}`,
|
2023-11-22 19:49:51 +08:00
|
|
|
containerName: `react-sdk-playwright-synapse`,
|
|
|
|
params: dockerSynapseParams,
|
|
|
|
cmd: ["run"],
|
|
|
|
});
|
|
|
|
console.log(`Started synapse with id ${synapseId} on port ${synCfg.port}.`);
|
|
|
|
// Await Synapse healthcheck
|
|
|
|
await this.docker.exec([
|
|
|
|
"curl",
|
|
|
|
"--connect-timeout",
|
|
|
|
"30",
|
|
|
|
"--retry",
|
|
|
|
"30",
|
|
|
|
"--retry-delay",
|
|
|
|
"1",
|
|
|
|
"--retry-all-errors",
|
|
|
|
"--silent",
|
|
|
|
"http://localhost:8008/health",
|
|
|
|
]);
|
2023-12-16 18:01:26 +08:00
|
|
|
const dockerUrl = `http://${await this.docker.getContainerIp()}:8008`;
|
2023-11-22 19:49:51 +08:00
|
|
|
this.config = {
|
|
|
|
...synCfg,
|
|
|
|
serverId: synapseId,
|
2023-12-16 18:01:26 +08:00
|
|
|
dockerUrl,
|
2023-11-22 19:49:51 +08:00
|
|
|
};
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2023-12-14 21:49:35 +08:00
|
|
|
public async stop(): Promise<string[]> {
|
2023-11-22 19:49:51 +08:00
|
|
|
if (!this.config) throw new Error("Missing existing synapse instance, did you call stop() before start()?");
|
|
|
|
const id = this.config.serverId;
|
2024-02-21 18:43:47 +08:00
|
|
|
const synapseLogsPath = path.join("playwright", "logs", "synapse", id);
|
2023-11-22 19:49:51 +08:00
|
|
|
await fse.ensureDir(synapseLogsPath);
|
|
|
|
await this.docker.persistLogsToFile({
|
|
|
|
stdoutFile: path.join(synapseLogsPath, "stdout.log"),
|
|
|
|
stderrFile: path.join(synapseLogsPath, "stderr.log"),
|
|
|
|
});
|
|
|
|
await this.docker.stop();
|
|
|
|
await fse.remove(this.config.configDir);
|
|
|
|
console.log(`Stopped synapse id ${id}.`);
|
2023-12-14 21:49:35 +08:00
|
|
|
|
|
|
|
return [path.join(synapseLogsPath, "stdout.log"), path.join(synapseLogsPath, "stderr.log")];
|
2023-11-22 19:49:51 +08:00
|
|
|
}
|
|
|
|
|
2024-07-05 17:51:21 +08:00
|
|
|
private async registerUserInternal(
|
|
|
|
username: string,
|
|
|
|
password: string,
|
|
|
|
displayName?: string,
|
|
|
|
admin = false,
|
|
|
|
): Promise<Credentials> {
|
2023-11-22 19:49:51 +08:00
|
|
|
const url = `${this.config.baseUrl}/_synapse/admin/v1/register`;
|
|
|
|
const { nonce } = await this.request.get(url).then((r) => r.json());
|
|
|
|
const mac = crypto
|
|
|
|
.createHmac("sha1", this.config.registrationSecret)
|
2024-07-05 17:51:21 +08:00
|
|
|
.update(`${nonce}\0${username}\0${password}\0${admin ? "" : "not"}admin`)
|
2023-11-22 19:49:51 +08:00
|
|
|
.digest("hex");
|
|
|
|
const res = await this.request.post(url, {
|
|
|
|
data: {
|
|
|
|
nonce,
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
mac,
|
2024-07-05 17:51:21 +08:00
|
|
|
admin,
|
2023-11-22 19:49:51 +08:00
|
|
|
displayname: displayName,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-11-27 20:11:00 +08:00
|
|
|
if (!res.ok()) {
|
|
|
|
throw await res.json();
|
|
|
|
}
|
|
|
|
|
2023-11-22 19:49:51 +08:00
|
|
|
const data = await res.json();
|
|
|
|
return {
|
|
|
|
homeServer: data.home_server,
|
|
|
|
accessToken: data.access_token,
|
|
|
|
userId: data.user_id,
|
|
|
|
deviceId: data.device_id,
|
|
|
|
password,
|
2023-11-28 19:02:50 +08:00
|
|
|
displayName,
|
2023-11-22 19:49:51 +08:00
|
|
|
};
|
|
|
|
}
|
2023-11-29 21:50:13 +08:00
|
|
|
|
2024-07-05 17:51:21 +08:00
|
|
|
public registerUser(username: string, password: string, displayName?: string): Promise<Credentials> {
|
|
|
|
return this.registerUserInternal(username, password, displayName, false);
|
|
|
|
}
|
|
|
|
|
2023-11-29 21:50:13 +08:00
|
|
|
public async loginUser(userId: string, password: string): Promise<Credentials> {
|
|
|
|
const url = `${this.config.baseUrl}/_matrix/client/v3/login`;
|
|
|
|
const res = await this.request.post(url, {
|
|
|
|
data: {
|
|
|
|
type: "m.login.password",
|
|
|
|
identifier: {
|
|
|
|
type: "m.id.user",
|
|
|
|
user: userId,
|
|
|
|
},
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const json = await res.json();
|
|
|
|
|
|
|
|
return {
|
|
|
|
password,
|
|
|
|
accessToken: json.access_token,
|
|
|
|
userId: json.user_id,
|
|
|
|
deviceId: json.device_id,
|
|
|
|
homeServer: json.home_server,
|
|
|
|
};
|
|
|
|
}
|
2024-07-05 17:51:21 +08:00
|
|
|
|
|
|
|
public async setThreepid(userId: string, medium: string, address: string): Promise<void> {
|
|
|
|
if (this.adminToken === undefined) {
|
|
|
|
const result = await this.registerUserInternal("admin", "totalyinsecureadminpassword", undefined, true);
|
|
|
|
this.adminToken = result.accessToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
const url = `${this.config.baseUrl}/_synapse/admin/v2/users/${userId}`;
|
|
|
|
const res = await this.request.put(url, {
|
|
|
|
data: {
|
|
|
|
threepids: [
|
|
|
|
{
|
|
|
|
medium,
|
|
|
|
address,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${this.adminToken}`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!res.ok()) {
|
|
|
|
throw await res.json();
|
|
|
|
}
|
|
|
|
}
|
2023-11-22 19:49:51 +08:00
|
|
|
}
|