Skip to content

Commit e35b855

Browse files
authored
Gnuxie/appservice logging (#441)
* upgrade to matrix-appservice-bridge 8.0.0 this is so we can use their new logger * Configure and use matrix-appservice-bridge's `Logger` #422 Haven't changed all of the mjolnir components to use this, just the appservice. The fact that we've configured this properly means we get logging from matrix-appservice-bridge components too (we didn't before). * use try/catch instead
1 parent 38c5714 commit e35b855

File tree

5 files changed

+66
-31
lines changed

5 files changed

+66
-31
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"humanize-duration-ts": "^2.1.1",
5454
"js-yaml": "^4.1.0",
5555
"jsdom": "^16.6.0",
56-
"matrix-appservice-bridge": "^5.0.0",
56+
"matrix-appservice-bridge": "8.0.0",
5757
"parse-duration": "^1.0.2",
5858
"pg": "^8.8.0",
5959
"shell-quote": "^1.7.3",

src/appservice/Api.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import express from "express";
33
import * as bodyParser from "body-parser";
44
import { MjolnirManager } from "./MjolnirManager";
55
import * as http from "http";
6+
import { Logger } from "matrix-appservice-bridge";
67

8+
const log = new Logger("Api");
79
/**
810
* This provides a web api that is designed to power the mjolnir widget https://github.com/matrix-org/mjolnir-widget.
911
*/
@@ -28,15 +30,15 @@ export class Api {
2830
qs: { access_token: accessToken },
2931
}, (err, homeserver_response, body) => {
3032
if (err) {
31-
console.error(`Error resolving openID token from ${this.homeserver}`, err);
33+
log.error(`Error resolving openID token from ${this.homeserver}`, err);
3234
reject(null);
3335
}
3436

3537
let response: { sub: string};
3638
try {
3739
response = JSON.parse(body);
3840
} catch (e) {
39-
console.error(`Received ill formed response from ${this.homeserver} when resolving an openID token`, e);
41+
log.error(`Received ill formed response from ${this.homeserver} when resolving an openID token`, e);
4042
reject(null);
4143
return;
4244
}

src/appservice/AppService.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import { AppServiceRegistration, Bridge, Request, WeakEvent, BridgeContext, MatrixUser } from "matrix-appservice-bridge";
17+
import { AppServiceRegistration, Bridge, Request, WeakEvent, BridgeContext, MatrixUser, Logger } from "matrix-appservice-bridge";
1818
import { MjolnirManager } from ".//MjolnirManager";
1919
import { DataStore, PgDataStore } from ".//datastore";
2020
import { Api } from "./Api";
2121
import { IConfig } from "./config/config";
2222
import { AccessControl } from "./AccessControl";
2323

24+
const log = new Logger("AppService");
2425
/**
2526
* Responsible for setting up listeners and delegating functionality to a matrix-appservice-bridge `Bridge` for
2627
* the entrypoint of the application.
@@ -64,7 +65,7 @@ export class MjolnirAppService {
6465
},
6566
suppressEcho: false,
6667
});
67-
await bridge.initalise();
68+
await bridge.initialise();
6869
const accessControlListId = await bridge.getBot().getClient().resolveRoom(config.accessControlList);
6970
const accessControl = await AccessControl.setupAccessControl(accessControlListId, bridge);
7071
const mjolnirManager = await MjolnirManager.makeMjolnirManager(dataStore, bridge, accessControl);
@@ -89,6 +90,7 @@ export class MjolnirAppService {
8990
* @param registrationFilePath A path to their homeserver registration file.
9091
*/
9192
public static async run(port: number, config: IConfig, registrationFilePath: string): Promise<MjolnirAppService> {
93+
Logger.configure(config.logging ?? { console: "debug" });
9294
const dataStore = new PgDataStore(config.db.connectionString);
9395
await dataStore.init();
9496
const service = await MjolnirAppService.makeMjolnirAppService(config, dataStore, registrationFilePath);
@@ -114,11 +116,19 @@ export class MjolnirAppService {
114116
// Acts as an alternative to the web api provided for the widget.
115117
if ('m.room.member' === mxEvent.type) {
116118
if ('invite' === mxEvent.content['membership'] && mxEvent.state_key === this.bridge.botUserId) {
117-
await this.mjolnirManager.provisionNewMjolnir(mxEvent.sender);
118-
// reject the invite to keep the room clean and make sure the invetee doesn't get confused and think this is their mjolnir.
119-
this.bridge.getBot().getClient().leaveRoom(mxEvent.room_id).catch(e => {
120-
console.warn("Unable to reject an invite to a room", e)
121-
});
119+
log.info(`${mxEvent.sender} has sent an invitation to the appservice bot ${this.bridge.botUserId}, attempting to provision them a mjolnir`);
120+
try {
121+
await this.mjolnirManager.provisionNewMjolnir(mxEvent.sender)
122+
} catch (e: any) {
123+
log.error(`Failed to provision a mjolnir for ${mxEvent.sender} after they invited ${this.bridge.botUserId}:`, e);
124+
// continue, we still want to reject this invitation.
125+
}
126+
try {
127+
// reject the invite to keep the room clean and make sure the invetee doesn't get confused and think this is their mjolnir.
128+
await this.bridge.getBot().getClient().leaveRoom(mxEvent.room_id);
129+
} catch (e: any) {
130+
log.warn("Unable to reject an invite to a room", e);
131+
}
122132
}
123133
}
124134
this.accessControl.handleEvent(mxEvent['room_id'], mxEvent);
@@ -130,10 +140,10 @@ export class MjolnirAppService {
130140
* @param port The port that the appservice should listen on to receive transactions from the homeserver.
131141
*/
132142
private async start(port: number) {
133-
console.log("Starting MjolnirAppService, Matrix-side to listen on port %s", port);
143+
log.info("Starting MjolnirAppService, Matrix-side to listen on port", port);
134144
this.api.start(this.config.webAPI.port);
135145
await this.bridge.listen(port);
136-
console.log("MjolnirAppService started successfully");
146+
log.info("MjolnirAppService started successfully");
137147
}
138148

139149
/**

src/appservice/config/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
import * as fs from "fs";
1818
import { load } from "js-yaml";
19+
import { LoggingOpts } from "matrix-appservice-bridge";
1920

2021
export interface IConfig {
2122
/** Details for the homeserver the appservice will be serving */
@@ -36,6 +37,8 @@ export interface IConfig {
3637
},
3738
/** A policy room for controlling access to the appservice */
3839
accessControlList: string,
40+
/** configuration for matrix-appservice-bridge's Logger */
41+
logging?: LoggingOpts,
3942
}
4043

4144
export function read(configPath: string): IConfig {

yarn.lock

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,13 @@
247247
pg-protocol "*"
248248
pg-types "^2.2.0"
249249

250+
"@types/pkginfo@^0.4.0":
251+
version "0.4.0"
252+
resolved "https://registry.yarnpkg.com/@types/pkginfo/-/pkginfo-0.4.0.tgz#00143b97e98aa7c9391943266d2e4aebd8f44c35"
253+
integrity sha512-4DGKkOlWkMuVDZQvytWzzWWAjyqDmlLKRYE4lzeA8t0s7fK0aF25uPbX9eBVermUjLJdeLHu9k1WmNiAssqCcg==
254+
dependencies:
255+
"@types/node" "*"
256+
250257
"@types/qs@*":
251258
version "6.9.7"
252259
resolved "https://registry.npmjs.org/@types/qs/-/qs-6.9.7.tgz"
@@ -2201,12 +2208,13 @@ make-error@^1.1.1:
22012208
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
22022209
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
22032210

2204-
matrix-appservice-bridge@^5.0.0:
2205-
version "5.0.0"
2206-
resolved "https://registry.yarnpkg.com/matrix-appservice-bridge/-/matrix-appservice-bridge-5.0.0.tgz#e3b42f9c1bef9c2a5fe51c47e2232ea3040852d0"
2207-
integrity sha512-d+F2RN/6o4TtBHOgcNECXX/UGjqM4zuWK14hmnzHHdrQi/Xuq9GSO14jMKsl1Jc0Kbkpv9cFMfhy27yo/Ju7/w==
2211+
matrix-appservice-bridge@8.0.0:
2212+
version "8.0.0"
2213+
resolved "https://registry.yarnpkg.com/matrix-appservice-bridge/-/matrix-appservice-bridge-8.0.0.tgz#6849ac05c281399b2c2b35daba784f8291d3b35d"
2214+
integrity sha512-XFo3avVfKb34d7kalXcsi0vThlnqmrwvewcfhjintmpbFlwu54/lvdbykFSyu2kT8BY1zUtDz7iQ3Q3RAyaN1g==
22082215
dependencies:
22092216
"@alloc/quick-lru" "^5.2.0"
2217+
"@types/pkginfo" "^0.4.0"
22102218
axios "^0.27.2"
22112219
chalk "^4.1.0"
22122220
express "^4.18.1"
@@ -2215,31 +2223,33 @@ matrix-appservice-bridge@^5.0.0:
22152223
ip-cidr "^3.0.4"
22162224
is-my-json-valid "^2.20.5"
22172225
js-yaml "^4.0.0"
2218-
matrix-appservice "^1.0.0"
2219-
matrix-bot-sdk "^0.6.1"
2226+
matrix-appservice "^1.1.0"
2227+
matrix-bot-sdk "^0.6.2"
22202228
nedb "^1.8.0"
22212229
nopt "^5.0.0"
22222230
p-queue "^6.6.2"
2223-
prom-client "^14.0.0"
2231+
pkginfo "^0.4.1"
2232+
postgres "^3.3.1"
2233+
prom-client "^14.1.0"
22242234
uuid "^8.3.2"
22252235
winston "^3.3.3"
22262236
winston-daily-rotate-file "^4.5.1"
22272237

2228-
matrix-appservice@^1.0.0:
2229-
version "1.0.0"
2230-
resolved "https://registry.yarnpkg.com/matrix-appservice/-/matrix-appservice-1.0.0.tgz#8a1225b248131eb8d37618bd88f76d705c647c28"
2231-
integrity sha512-SoWTp92xKsKiC6T8147gTxChyp0aNisxLWtX15vGMIeIdV3ZsUzoQsLVhABzuVzW1ExZ5FAoD1HpYz1Du9lk6A==
2238+
matrix-appservice@^1.1.0:
2239+
version "1.1.0"
2240+
resolved "https://registry.yarnpkg.com/matrix-appservice/-/matrix-appservice-1.1.0.tgz#e567945042000485e4ea4bfeef92246e93296f01"
2241+
integrity sha512-6hJdmo9YIbh6dS9MfMHCpHMhklN/+NOcfGQ/3UbbEEfIE8dt0bHqi1nnIiias5IqDFl6ED9y+YQdtyqnIXx+Ww==
22322242
dependencies:
22332243
"@types/express" "^4.17.8"
22342244
body-parser "^1.19.0"
22352245
express "^4.17.1"
22362246
js-yaml "^4.1.0"
22372247
morgan "^1.10.0"
22382248

2239-
matrix-bot-sdk@^0.6.1:
2240-
version "0.6.1"
2241-
resolved "https://registry.yarnpkg.com/matrix-bot-sdk/-/matrix-bot-sdk-0.6.1.tgz#29c5d92fd6bc2eb0f6be2540ed133d8ffadaed93"
2242-
integrity sha512-xbSQUpbuQq9Oj5f5GeprKndJZqdB9N9majg+VwIEcBGBscpWCskX32kTaoNhTHAE92XlM+qnwFME45TYElf4tA==
2249+
matrix-bot-sdk@^0.6.2:
2250+
version "0.6.2"
2251+
resolved "https://registry.yarnpkg.com/matrix-bot-sdk/-/matrix-bot-sdk-0.6.2.tgz#c9334b39f62a9742d74e46312def566429dfef26"
2252+
integrity sha512-+kXlXkQBQgWC6oUwYEosJlXjceaj7jQUnPlALFhGeAabgVm8tmuvFNVKqClwvrrjj+0Gzsmt+rcJHmkvqymFXA==
22432253
dependencies:
22442254
"@matrix-org/matrix-sdk-crypto-nodejs" "^0.1.0-beta.1"
22452255
"@types/express" "^4.17.13"
@@ -2718,6 +2728,11 @@ pify@^3.0.0:
27182728
resolved "https://registry.yarnpkg.com/pify/-/pify-3.0.0.tgz#e5a4acd2c101fdf3d9a4d07f0dbc4db49dd28176"
27192729
integrity sha512-C3FsVNH1udSEX48gGX1xfvwTWfsYWj5U+8/uK15BGzIGrKoUpghX8hWZwa/OFnakBiiVNmBvemTJR5mcy7iPcg==
27202730

2731+
pkginfo@^0.4.1:
2732+
version "0.4.1"
2733+
resolved "https://registry.yarnpkg.com/pkginfo/-/pkginfo-0.4.1.tgz#b5418ef0439de5425fc4995042dced14fb2a84ff"
2734+
integrity sha512-8xCNE/aT/EXKenuMDZ+xTVwkT8gsoHN2z/Q29l80u0ppGEXVvsKRzNMbtKhg8LS8k1tJLAHHylf6p4VFmP6XUQ==
2735+
27212736
postcss@^8.3.11:
27222737
version "8.4.16"
27232738
resolved "https://registry.npmjs.org/postcss/-/postcss-8.4.16.tgz"
@@ -2749,6 +2764,11 @@ postgres-interval@^1.1.0:
27492764
dependencies:
27502765
xtend "^4.0.0"
27512766

2767+
postgres@^3.3.1:
2768+
version "3.3.2"
2769+
resolved "https://registry.yarnpkg.com/postgres/-/postgres-3.3.2.tgz#91f2e209e4a08ca7101eb7178734e4c0e4d23eb3"
2770+
integrity sha512-NaPqFpUC6C7aCQkJXLvuO/3RKNKL4en8opY53YrcXK3//xXra6CZ2qX6290lxuQ1dW1LbRGYCmsawRlCxSBonQ==
2771+
27522772
prelude-ls@^1.2.1:
27532773
version "1.2.1"
27542774
resolved "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.2.1.tgz"
@@ -2774,10 +2794,10 @@ progress@^2.0.0:
27742794
resolved "https://registry.npmjs.org/progress/-/progress-2.0.3.tgz"
27752795
integrity sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA==
27762796

2777-
prom-client@^14.0.0:
2778-
version "14.0.1"
2779-
resolved "https://registry.npmjs.org/prom-client/-/prom-client-14.0.1.tgz"
2780-
integrity sha512-HxTArb6fkOntQHoRGvv4qd/BkorjliiuO2uSWC2KC17MUTKYttWdDoXX/vxOhQdkoECEM9BBH0pj2l8G8kev6w==
2797+
prom-client@^14.1.0:
2798+
version "14.1.0"
2799+
resolved "https://registry.yarnpkg.com/prom-client/-/prom-client-14.1.0.tgz#049609859483d900844924df740722c76ed1fdbb"
2800+
integrity sha512-iFWCchQmi4170omLpFXbzz62SQTmPhtBL35v0qGEVRHKcqIeiexaoYeP0vfZTujxEq3tA87iqOdRbC9svS1B9A==
27812801
dependencies:
27822802
tdigest "^0.1.1"
27832803

0 commit comments

Comments
 (0)