Skip to content

Commit

Permalink
Share main appservice config with feed bots
Browse files Browse the repository at this point in the history
This is required to safely enable encryption for the bots that post
GenericHook messages.
  • Loading branch information
AndrewFerr committed Nov 24, 2022
1 parent 4ebd67a commit 6b0e48b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 29 deletions.
7 changes: 5 additions & 2 deletions src/App/BridgeApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { UserNotificationWatcher } from "../Notifications/UserNotificationWatche
import { ListenerService } from "../ListenerService";
import { Logger } from "matrix-appservice-bridge";
import { LogService } from "matrix-bot-sdk";
import { getAppservice } from "../appservice";

Logger.configure({console: "info"});
const log = new Logger("App");
Expand All @@ -25,14 +26,16 @@ async function start() {
});
LogService.setLogger(Logger.botSdkLogger);

const {appservice, storage} = getAppservice(config, registration);

if (config.queue.monolithic) {
const matrixSender = new MatrixSender(config, registration);
const matrixSender = new MatrixSender(config, appservice);
matrixSender.listen();
const userNotificationWatcher = new UserNotificationWatcher(config);
userNotificationWatcher.start();
}

const bridgeApp = new Bridge(config, registration, listener);
const bridgeApp = new Bridge(config, listener, appservice, storage);

process.once("SIGTERM", () => {
log.error("Got SIGTERM");
Expand Down
3 changes: 2 additions & 1 deletion src/App/MatrixSenderApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Logger } from "matrix-appservice-bridge";
import Metrics from "../Metrics";
import { ListenerService } from "../ListenerService";
import { LogService } from "matrix-bot-sdk";
import { getAppservice } from "../appservice";


const log = new Logger("App");
Expand All @@ -21,7 +22,7 @@ async function start() {
});
LogService.setLogger(Logger.botSdkLogger);
const listener = new ListenerService(config.listeners);
const sender = new MatrixSender(config, registration);
const sender = new MatrixSender(config, getAppservice(config, registration).appservice);
if (config.metrics) {
if (!config.metrics.port) {
log.warn(`Not running metrics for service, no port specified`);
Expand Down
23 changes: 7 additions & 16 deletions src/Bridge.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AdminAccountData } from "./AdminRoomCommandHandler";
import { AdminRoom, BRIDGE_ROOM_TYPE, LEGACY_BRIDGE_ROOM_TYPE } from "./AdminRoom";
import { Appservice, IAppserviceRegistration, RichRepliesPreprocessor, IRichReplyMetadata, StateEvent, MatrixClient, EventKind, PowerLevelsEvent } from "matrix-bot-sdk";
import { Appservice, RichRepliesPreprocessor, IRichReplyMetadata, StateEvent, EventKind, PowerLevelsEvent } from "matrix-bot-sdk";
import { BridgeConfig, BridgePermissionLevel, GitLabInstance } from "./Config/Config";
import { BridgeWidgetApi } from "./Widgets/BridgeWidgetApi";
import { CommentProcessor } from "./CommentProcessor";
Expand All @@ -14,14 +14,12 @@ import { IGitLabWebhookIssueStateEvent, IGitLabWebhookMREvent, IGitLabWebhookNot
import { JiraIssueEvent, JiraIssueUpdatedEvent, JiraVersionEvent } from "./Jira/WebhookTypes";
import { JiraOAuthResult } from "./Jira/Types";
import { MatrixEvent, MatrixMemberContent, MatrixMessageContent } from "./MatrixEvent";
import { MemoryStorageProvider } from "./Stores/MemoryStorageProvider";
import { MessageQueue, createMessageQueue } from "./MessageQueue";
import { MessageSenderClient } from "./MatrixSender";
import { NotifFilter, NotificationFilterStateContent } from "./NotificationFilters";
import { NotificationProcessor } from "./NotificationsProcessor";
import { NotificationsEnableEvent, NotificationsDisableEvent } from "./Webhooks";
import { GitHubOAuthToken, GitHubOAuthTokenResponse, ProjectsGetResponseData } from "./Github/Types";
import { RedisStorageProvider } from "./Stores/RedisStorageProvider";
import { retry } from "./PromiseUtil";
import { UserNotificationsEvent } from "./Notifications/UserNotificationWatcher";
import { UserTokenStore } from "./UserTokenStore";
Expand All @@ -36,16 +34,13 @@ import Metrics from "./Metrics";
import { FigmaEvent, ensureFigmaWebhooks } from "./figma";
import { ListenerService } from "./ListenerService";
import { SetupConnection } from "./Connections/SetupConnection";
import { getAppservice } from "./appservice";
import { JiraOAuthRequestCloud, JiraOAuthRequestOnPrem, JiraOAuthRequestResult } from "./Jira/OAuth";
import { GenericWebhookEvent, GenericWebhookEventResult } from "./generic/types";
import { SetupWidget } from "./Widgets/SetupWidget";
import { FeedEntry, FeedError, FeedReader, FeedSuccess } from "./feeds/FeedReader";
const log = new Logger("Bridge");

export class Bridge {
private readonly as: Appservice;
private readonly storage: IBridgeStorageProvider;
private readonly messageClient: MessageSenderClient;
private readonly queue: MessageQueue;
private readonly commentProcessor: CommentProcessor;
Expand All @@ -61,16 +56,12 @@ export class Bridge {

private ready = false;

constructor(private config: BridgeConfig, private registration: IAppserviceRegistration, private readonly listener: ListenerService) {
if (this.config.queue.host && this.config.queue.port) {
log.info(`Initialising Redis storage (on ${this.config.queue.host}:${this.config.queue.port})`);
this.storage = new RedisStorageProvider(this.config.queue.host, this.config.queue.port);
} else {
log.info('Initialising memory storage');
this.storage = new MemoryStorageProvider();
}
this.as = getAppservice(this.config, this.registration, this.storage);
Metrics.registerMatrixSdkMetrics(this.as);
constructor(
private config: BridgeConfig,
private readonly listener: ListenerService,
private readonly as: Appservice,
private readonly storage: IBridgeStorageProvider,
) {
this.queue = createMessageQueue(this.config.queue);
this.messageClient = new MessageSenderClient(this.queue);
this.commentProcessor = new CommentProcessor(this.as, this.config.bridge.mediaUrl || this.config.bridge.url);
Expand Down
9 changes: 2 additions & 7 deletions src/MatrixSender.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { BridgeConfig } from "./Config/Config";
import { MessageQueue, createMessageQueue } from "./MessageQueue";
import { Appservice, IAppserviceRegistration, MemoryStorageProvider } from "matrix-bot-sdk";
import { Appservice, Intent } from "matrix-bot-sdk";
import { Logger } from "matrix-appservice-bridge";
import { v4 as uuid } from "uuid";
import { getAppservice } from "./appservice";
import Metrics from "./Metrics";

export interface IMatrixSendMessage {
sender: string|null;
Expand All @@ -26,11 +24,8 @@ const log = new Logger("MatrixSender");

export class MatrixSender {
private mq: MessageQueue;
private as: Appservice;
constructor(private config: BridgeConfig, registration: IAppserviceRegistration) {
constructor(private config: BridgeConfig, private readonly as: Appservice) {
this.mq = createMessageQueue(this.config.queue);
this.as = getAppservice(config, registration, new MemoryStorageProvider());
Metrics.registerMatrixSdkMetrics(this.as);
}

public listen() {
Expand Down
25 changes: 22 additions & 3 deletions src/appservice.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import { Appservice, IAppserviceRegistration, IAppserviceStorageProvider, RustSdkAppserviceCryptoStorageProvider } from "matrix-bot-sdk";
import { Logger } from "matrix-appservice-bridge";
import { Appservice, IAppserviceRegistration, RustSdkAppserviceCryptoStorageProvider } from "matrix-bot-sdk";
import { BridgeConfig } from "./Config/Config";
import Metrics from "./Metrics";
import { MemoryStorageProvider } from "./Stores/MemoryStorageProvider";
import { RedisStorageProvider } from "./Stores/RedisStorageProvider";
import { IBridgeStorageProvider } from "./Stores/StorageProvider";
const log = new Logger("Appservice");

export function getAppservice(config: BridgeConfig, registration: IAppserviceRegistration) {
let storage: IBridgeStorageProvider;
if (config.queue.host && config.queue.port) {
log.info(`Initialising Redis storage (on ${config.queue.host}:${config.queue.port})`);
storage = new RedisStorageProvider(config.queue.host, config.queue.port);
} else {
log.info('Initialising memory storage');
storage = new MemoryStorageProvider();
}

export function getAppservice(config: BridgeConfig, registration: IAppserviceRegistration, storage: IAppserviceStorageProvider) {
const cryptoStorage = config.encryption?.storagePath ? new RustSdkAppserviceCryptoStorageProvider(config.encryption.storagePath) : undefined;

return new Appservice({
const appservice = new Appservice({
homeserverName: config.bridge.domain,
homeserverUrl: config.bridge.url,
port: config.bridge.port,
Expand All @@ -27,4 +42,8 @@ export function getAppservice(config: BridgeConfig, registration: IAppserviceReg
},
cryptoStorage: cryptoStorage,
});

Metrics.registerMatrixSdkMetrics(appservice);

return {appservice, storage};
}

0 comments on commit 6b0e48b

Please sign in to comment.