Skip to content

Commit

Permalink
supoort native encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
H-Shay committed Sep 17, 2024
1 parent 9e85463 commit 63510e9
Show file tree
Hide file tree
Showing 80 changed files with 436 additions and 109 deletions.
13 changes: 13 additions & 0 deletions config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ rawHomeserverUrl: "https://matrix.org"
# Matrix Access Token to use, Mjolnir will only use this if pantalaimon.use is false.
accessToken: "YOUR_TOKEN_HERE"

# Options related to native encryption
encryption:
# whether to use native encryption in mjolnir, rather than using pantalaimon as a proxy
# note that if encryption is enabled here, pantaliamon must be disabled, and vice versa
use: true

# the username to log in with
username: "mjolnir"

# the password to log in with
password: "password"


# Options related to Pantalaimon (https://github.com/matrix-org/pantalaimon)
pantalaimon:
# Whether or not Mjolnir will use pantalaimon to access the matrix homeserver,
Expand Down
7 changes: 6 additions & 1 deletion config/harness.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ homeserverUrl: "http://localhost:8081"
# Where the homeserver is located (client-server URL). NOT pantalaimon.
rawHomeserverUrl: "http://localhost:8081"

encryption:
use: true
username: test
password: testPassword

# README: We use the Pantalaimon client WITHOUT Pantalaimon itself in tests (and the manual test)
# as an easy way to login with passwords from the config without having
# to resolve a chicken-vs-egg problem in regards to access tokens.
# Pantalaimon options (https://github.com/matrix-org/pantalaimon)
pantalaimon:
# If true, accessToken above is ignored and the username/password below will be
# used instead. The access token of the bot will be stored in the dataPath.
use: true
use: false

# The username to login with.
username: mjolnir
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
"prom-client": "^14.1.0",
"shell-quote": "^1.7.3",
"ulidx": "^0.3.0",
"yaml": "^2.2.2"
"yaml": "^2.2.2",
"@vector-im/matrix-bot-sdk": "^0.7.1-element.0"
},
"engines": {
"node": ">=18.0.0"
Expand Down
2 changes: 1 addition & 1 deletion src/ManagementRoomOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import * as Sentry from "@sentry/node";
import { extractRequestError, LogLevel, LogService, MessageType, Permalinks, TextualMessageEventContent, UserID } from "matrix-bot-sdk";
import { extractRequestError, LogLevel, LogService, MessageType, Permalinks, TextualMessageEventContent, UserID } from "@vector-im/matrix-bot-sdk";
import { IConfig } from "./config";
import { MatrixSendClient } from "./MatrixEmitter";
import { htmlEscape } from "./utils";
Expand Down
2 changes: 1 addition & 1 deletion src/MatrixEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import EventEmitter from "events";
import { MatrixClient } from "matrix-bot-sdk";
import { MatrixClient } from "@vector-im/matrix-bot-sdk";

/**
* This is an interface created in order to keep the event listener
Expand Down
7 changes: 4 additions & 3 deletions src/Mjolnir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {
LogLevel,
LogService,
MembershipEvent,
} from "matrix-bot-sdk";
MatrixClient
} from "@vector-im/matrix-bot-sdk";

import { ALL_RULE_TYPES as ALL_BAN_LIST_RULE_TYPES } from "./models/ListRule";
import { COMMAND_PREFIX, handleCommand } from "./commands/CommandHandler";
Expand Down Expand Up @@ -136,10 +137,10 @@ export class Mjolnir {

/**
* Create a new Mjolnir instance from a client and the options in the configuration file, ready to be started.
* @param {MatrixSendClient} client The client for Mjolnir to use.
* @param {MatrixClient} client The client for Mjolnir to use.
* @returns A new Mjolnir instance that can be started without further setup.
*/
static async setupMjolnirFromConfig(client: MatrixSendClient, matrixEmitter: MatrixEmitter, config: IConfig): Promise<Mjolnir> {
static async setupMjolnirFromConfig(client: MatrixClient, matrixEmitter: MatrixEmitter, config: IConfig): Promise<Mjolnir> {
if (!config.autojoinOnlyIfManager && config.acceptInvitesFromSpace === getDefaultConfig().acceptInvitesFromSpace) {
throw new TypeError("`autojoinOnlyIfManager` has been disabled but you have not set `acceptInvitesFromSpace`. Please make it empty to accept invites from everywhere or give it a namespace alias or room id.");
}
Expand Down
2 changes: 1 addition & 1 deletion src/ProtectedRoomsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import AwaitLock from 'await-lock';
import { extractRequestError, LogService, Permalinks } from "matrix-bot-sdk";
import { extractRequestError, LogService, Permalinks } from "@vector-im/matrix-bot-sdk";
import { IConfig } from "./config";
import { MatrixSendClient } from './MatrixEmitter';
const PROTECTED_ROOMS_EVENT_TYPE = "org.matrix.mjolnir.protected_rooms";
Expand Down
2 changes: 1 addition & 1 deletion src/ProtectedRoomsSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { LogLevel, LogService, MatrixGlob, Permalinks, UserID } from "matrix-bot-sdk";
import { LogLevel, LogService, MatrixGlob, Permalinks, UserID } from "@vector-im/matrix-bot-sdk";
import { IConfig } from "./config";
import ErrorCache, { ERROR_KIND_FATAL, ERROR_KIND_PERMISSION } from "./ErrorCache";
import ManagementRoomOutput from "./ManagementRoomOutput";
Expand Down
3 changes: 2 additions & 1 deletion src/appservice/AccessControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
import { Bridge } from "matrix-appservice-bridge";
import AccessControlUnit, { EntityAccess } from "../models/AccessControlUnit";
import PolicyList from "../models/PolicyList";
import { Permalinks } from "matrix-bot-sdk";
import { Permalinks } from "@vector-im/matrix-bot-sdk";

/**
* Utility to manage which users have access to the application service,
Expand Down Expand Up @@ -47,6 +47,7 @@ export class AccessControl {
const accessControlList = new PolicyList(
accessControlListId,
Permalinks.forRoom(accessControlListId),
// @ts-ignore
bridge.getBot().getClient()
);
const accessControlUnit = new AccessControlUnit([accessControlList]);
Expand Down
5 changes: 3 additions & 2 deletions src/appservice/MjolnirManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Request, WeakEvent, BridgeContext, Bridge, Intent, Logger } from "matri
import { getProvisionedMjolnirConfig } from "../config";
import { IConfig as IAppserviceConfig } from "./config/config";
import PolicyList from "../models/PolicyList";
import { Permalinks, MatrixClient } from "matrix-bot-sdk";
import { Permalinks, MatrixClient } from "@vector-im/matrix-bot-sdk";
import { DataStore } from "./datastore";
import { AccessControl } from "./AccessControl";
import { Access } from "../models/AccessControlUnit";
Expand Down Expand Up @@ -140,7 +140,7 @@ export class MjolnirManager {
invite: [requestingUserId],
name: `${requestingUserId}'s mjolnir`
});

// @ts-ignore
const mjolnir = await this.makeInstance(requestingUserId, managementRoomId, mjIntent.matrixClient);
await mjolnir.createFirstList(requestingUserId, "list");

Expand Down Expand Up @@ -183,6 +183,7 @@ export class MjolnirManager {
await this.makeInstance(
mjolnirRecord.owner,
mjolnirRecord.management_room,
// @ts-ignore
mjIntent.matrixClient,
).catch((e: any) => {
log.error(`Could not start mjolnir ${mjolnirRecord.local_part} for ${mjolnirRecord.owner}:`, e);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/AddRemoveProtectedRoomsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import { Mjolnir } from "../Mjolnir";
import { extractRequestError, LogLevel, LogService } from "matrix-bot-sdk";
import { extractRequestError, LogLevel, LogService } from "@vector-im/matrix-bot-sdk";

// !mjolnir rooms add <room alias/ID>
export async function execAddProtectedRoom(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/AddRemoveRoomFromDirectoryCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import { Mjolnir } from "../Mjolnir";
import { RichReply } from "matrix-bot-sdk";
import { RichReply } from "@vector-im/matrix-bot-sdk";

async function addRemoveFromDirectory(inRoomId: string, event: any, mjolnir: Mjolnir, roomRef: string, visibility: "public" | "private") {
const isAdmin = await mjolnir.isSynapseAdmin();
Expand Down
2 changes: 1 addition & 1 deletion src/commands/AliasCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import { Mjolnir } from "../Mjolnir";
import { RichReply } from "matrix-bot-sdk";
import { RichReply } from "@vector-im/matrix-bot-sdk";
import { htmlEscape } from "../utils";

// !mjolnir move <alias> <new room ID>
Expand Down
2 changes: 1 addition & 1 deletion src/commands/CommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Mjolnir } from "../Mjolnir";
import { execStatusCommand } from "./StatusCommand";
import { execBanCommand, execUnbanCommand } from "./UnbanBanCommand";
import { execDumpRulesCommand, execRulesMatchingCommand } from "./DumpRulesCommand";
import { extractRequestError, LogService, RichReply } from "matrix-bot-sdk";
import { extractRequestError, LogService, RichReply } from "@vector-im/matrix-bot-sdk";
import { htmlEscape } from "../utils";
import { execSyncCommand } from "./SyncCommand";
import { execPermissionCheckCommand } from "./PermissionCheckCommand";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/CreateBanListCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

import { Mjolnir } from "../Mjolnir";
import PolicyList from "../models/PolicyList";
import { Permalinks, RichReply } from "matrix-bot-sdk";
import { Permalinks, RichReply } from "@vector-im/matrix-bot-sdk";

// !mjolnir list create <shortcode> <alias localpart>
export async function execCreateListCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/DeactivateCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import { Mjolnir } from "../Mjolnir";
import { RichReply } from "matrix-bot-sdk";
import { RichReply } from "@vector-im/matrix-bot-sdk";

// !mjolnir deactivate <user ID>
export async function execDeactivateCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/DumpRulesCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { RichReply } from "matrix-bot-sdk";
import { RichReply } from "@vector-im/matrix-bot-sdk";
import { Mjolnir } from "../Mjolnir";
import { EntityType } from "../models/ListRule";
import { htmlEscape } from "../utils";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/ImportCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import { Mjolnir } from "../Mjolnir";
import { RichReply } from "matrix-bot-sdk";
import { RichReply } from "@vector-im/matrix-bot-sdk";
import { EntityType } from "../models/ListRule";
import PolicyList from "../models/PolicyList";

Expand Down
2 changes: 1 addition & 1 deletion src/commands/KickCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import { Mjolnir } from "../Mjolnir";
import { LogLevel, MatrixGlob, RichReply } from "matrix-bot-sdk";
import { LogLevel, MatrixGlob, RichReply } from "@vector-im/matrix-bot-sdk";

// !mjolnir kick <user|filter> [room] [reason]
export async function execKickCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/ListProtectedRoomsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import { Mjolnir } from "../Mjolnir";
import { Permalinks, RichReply } from "matrix-bot-sdk";
import { Permalinks, RichReply } from "@vector-im/matrix-bot-sdk";

// !mjolnir rooms
export async function execListProtectedRooms(roomId: string, event: any, mjolnir: Mjolnir) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/MakeRoomAdminCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import { Mjolnir } from "../Mjolnir";
import { RichReply } from "matrix-bot-sdk";
import { RichReply } from "@vector-im/matrix-bot-sdk";

// !mjolnir make admin <room> [<user ID>]
export async function execMakeRoomAdminCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/ProtectionsCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

import { htmlEscape } from "../utils";
import { Mjolnir } from "../Mjolnir";
import { extractRequestError, LogService, RichReply } from "matrix-bot-sdk";
import { extractRequestError, LogService, RichReply } from "@vector-im/matrix-bot-sdk";
import { isListSetting } from "../protections/ProtectionSettings";

// !mjolnir enable <protection>
Expand Down
2 changes: 1 addition & 1 deletion src/commands/RedactCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

import { Mjolnir } from "../Mjolnir";
import { redactUserMessagesIn } from "../utils";
import { Permalinks } from "matrix-bot-sdk";
import { Permalinks } from "@vector-im/matrix-bot-sdk";

// !mjolnir redact <user ID> [room alias] [limit]
export async function execRedactCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/SetDefaultBanListCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import { Mjolnir } from "../Mjolnir";
import { RichReply } from "matrix-bot-sdk";
import { RichReply } from "@vector-im/matrix-bot-sdk";

export const DEFAULT_LIST_EVENT_TYPE = "org.matrix.mjolnir.default_list";

Expand Down
2 changes: 1 addition & 1 deletion src/commands/SetPowerLevelCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import { Mjolnir } from "../Mjolnir";
import { extractRequestError, LogLevel, LogService } from "matrix-bot-sdk";
import { extractRequestError, LogLevel, LogService } from "@vector-im/matrix-bot-sdk";

// !mjolnir powerlevel <user ID> <level> [room]
export async function execSetPowerLevelCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/SetupDecentralizedReportingCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Mjolnir } from "../Mjolnir";
import { LogLevel } from "matrix-bot-sdk";
import { LogLevel } from "@vector-im/matrix-bot-sdk";

const EVENT_MODERATED_BY = "org.matrix.msc3215.room.moderation.moderated_by";
const EVENT_MODERATOR_OF = "org.matrix.msc3215.room.moderation.moderator_of";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/ShutdownRoomCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import { Mjolnir } from "../Mjolnir";
import { RichReply } from "matrix-bot-sdk";
import { RichReply } from "@vector-im/matrix-bot-sdk";

// !mjolnir shutdown room <room> [<message>]
export async function execShutdownRoomCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/SinceCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import { Mjolnir } from "../Mjolnir";
import { LogLevel, LogService, RichReply } from "matrix-bot-sdk";
import { LogLevel, LogService, RichReply } from "@vector-im/matrix-bot-sdk";
import { htmlEscape, parseDuration } from "../utils";
import { ParseEntry } from "shell-quote";
import { HumanizeDurationLanguage, HumanizeDuration } from "humanize-duration-ts";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/StatusCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import { Mjolnir, STATE_CHECKING_PERMISSIONS, STATE_NOT_STARTED, STATE_RUNNING, STATE_SYNCING } from "../Mjolnir";
import { RichReply } from "matrix-bot-sdk";
import { RichReply } from "@vector-im/matrix-bot-sdk";
import { htmlEscape, parseDuration } from "../utils";
import { HumanizeDurationLanguage, HumanizeDuration } from "humanize-duration-ts";
import PolicyList from "../models/PolicyList";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/SuspendCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import {Mjolnir} from "../Mjolnir";
import {RichReply} from "matrix-bot-sdk";
import {RichReply} from "@vector-im/matrix-bot-sdk";

export async function execSuspendCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
const target = parts[2];
Expand Down
2 changes: 1 addition & 1 deletion src/commands/UnbanBanCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

import { Mjolnir } from "../Mjolnir";
import PolicyList from "../models/PolicyList";
import { extractRequestError, LogLevel, LogService, MatrixGlob, RichReply } from "matrix-bot-sdk";
import { extractRequestError, LogLevel, LogService, MatrixGlob, RichReply } from "@vector-im/matrix-bot-sdk";
import { RULE_ROOM, RULE_SERVER, RULE_USER, USER_RULE_TYPES } from "../models/ListRule";
import { DEFAULT_LIST_EVENT_TYPE } from "./SetDefaultBanListCommand";

Expand Down
2 changes: 1 addition & 1 deletion src/commands/UnsuspendCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import {Mjolnir} from "../Mjolnir";
import {RichReply} from "matrix-bot-sdk";
import {RichReply} from "@vector-im/matrix-bot-sdk";

export async function execUnsuspendCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
const target = parts[2];
Expand Down
2 changes: 1 addition & 1 deletion src/commands/WatchUnwatchCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import { Mjolnir } from "../Mjolnir";
import { Permalinks, RichReply } from "matrix-bot-sdk";
import { Permalinks, RichReply } from "@vector-im/matrix-bot-sdk";

// !mjolnir watch <room alias or ID>
export async function execWatchCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
Expand Down
12 changes: 11 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

import * as fs from "fs";
import { load } from "js-yaml";
import { MatrixClient, LogService } from "matrix-bot-sdk";
import { MatrixClient, LogService } from "@vector-im/matrix-bot-sdk";
import Config from "config";

export interface IHealthConfig {
Expand Down Expand Up @@ -70,6 +70,11 @@ export interface IConfig {
homeserverUrl: string;
rawHomeserverUrl: string;
accessToken: string;
encryption: {
use: boolean;
username: string;
password: string;
}
pantalaimon: {
use: boolean;
username: string;
Expand Down Expand Up @@ -189,6 +194,11 @@ const defaultConfig: IConfig = {
homeserverUrl: "http://localhost:8008",
rawHomeserverUrl: "http://localhost:8008",
accessToken: "NONE_PROVIDED",
encryption: {
use: true,
username: "",
password: "",
},
pantalaimon: {
use: false,
username: "",
Expand Down
2 changes: 1 addition & 1 deletion src/health/healthz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import * as http from "http";
import { LogService } from "matrix-bot-sdk";
import { LogService } from "@vector-im/matrix-bot-sdk";
import { IConfig } from "../config";
// allowed to use the global configuration since this is only intended to be used by `src/index.ts`.

Expand Down
Loading

0 comments on commit 63510e9

Please sign in to comment.