Skip to content

Commit

Permalink
add reportMatrix module (#1043)
Browse files Browse the repository at this point in the history
* add reportMatrix module

* add friends

* fix identation in config.yml.example

* use uuidv4 instead of Math.random()

* add documentation on how to find room id and access token

* added acknowledgement

---------

Co-authored-by: Massimo Candela <[email protected]>
  • Loading branch information
NickBouwhuis and massimocandela authored Feb 23, 2023
1 parent 974bed4 commit 8be8ba1
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 1 deletion.
18 changes: 18 additions & 0 deletions config.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,24 @@ reports:
# params:
# maxAlertsAmount: 25

# - file: reportMatrix
# channels:
# - hijack
# - newprefix
# - visibility
# - path
# - misconfiguration
# - rpki
# - roa
# params:
# showPaths: 0 # Amount of AS_PATHs to report in the alert
# homeserverUrl: https://matrix.org
# accessToken: _ACCESS_TOKEN_
# roomIds:
# default: "_ROOM_ID_"
# noc: "_ROOM_ID_"


############################
# Notification settings:
# - notificationIntervalSeconds
Expand Down
2 changes: 2 additions & 0 deletions docs/friends.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,5 @@ Please, let me know so I can add your company name here.
* Rechenzentrum Haßfurt GmbH (AS44973)
* obe.net (AS3399)
* IT-Total (AS8769)
* Speakup (AS49627)
* Nick Bouwhuis (AS202585)
24 changes: 23 additions & 1 deletion docs/reports.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This will generate fake alerts. [Read more here](installation.md#bgpalerter-para
- [reportHTTP](reports.md#reportHTTP)
- [reportTelegram](reports.md#reportTelegram)
- [reportPullAPI](reports.md#reportPullAPI)
- [reportMatrix](reports.md#reportMatrix)

## reportFile

Expand Down Expand Up @@ -172,10 +173,31 @@ This report module creates a REST API reachable at `http://host:port/alerts/`. T

The REST API uses the generic `rest` configuration in `config.yml`. Read [here](configuration.md) or see `config.yml.example` for more information.


Parameters for this report module:

|Parameter| Description|
|---|---|
|maxAlertsAmount| The maximum amount of alerts the API will return. By default set to 100. Don't exagerate with the number, the greater this value is the more memory BGPalerter will use. |
|noProxy| If there is a global proxy configuration (see [here](http-proxy.md)), this parameter if set to true allows the single module to bypass the proxy. |

## reportMatrix

This report module sends alerts directly to a specific Matrix room.
To send alert to Matrix you need an access token and a room ID.

You can find your access token in the Element client by going to All Settings > Help & About > Advanced. Read more about access tokens [here](https://spec.matrix.org/v1.6/client-server-api/#using-access-tokens).

You can find the room ID in the Element client by going to Room info > Room Settings > Advanced. Read more about Room ID [here](https://spec.matrix.org/latest/#room-structure).

Parameters for this report module:

|Parameter| Description|
|---|---|
|showPaths| Amount of AS_PATHs to report in the alert (0 to disable). |
|homeserverUrl| URL of your Matrix homeserver (for example: `https://matrix.org`. |
|noProxy| If there is a global proxy configuration (see [here](http-proxy.md)), this parameter if set to true allows the single module to bypass the proxy. |
|accessToken| The access token for authentication yourself to the Matrix API. |
|roomIds| A dictionary containing chat IDs grouped by user group (key: group, value: room ID). |
|roomIds.default| The room ID of the default room.|

Thanks to [@nickbouwhuis](https://github.com/nttgin/BGPalerter/pull/1043).
88 changes: 88 additions & 0 deletions src/reports/reportMatrix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* BSD 3-Clause License
*
* Copyright (c) 2019, NTT Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import ReportHTTP from "./reportHTTP";
import { v4 as uuidv4 } from 'uuid';

export default class reportMatrix extends ReportHTTP {

constructor(channels, params, env) {
const hooks = {};
const transactionId = uuidv4();

for (let userGroup in params.roomIds) {
hooks[userGroup] = params.homeserverUrl + "/_matrix/client/v3/rooms/" + encodeURIComponent(params.roomIds[userGroup]) + "/send/m.room.message/" + transactionId;
}
hooks["default"] = params.homeserverUrl + "/_matrix/client/v3/rooms/" + encodeURIComponent(params.roomIds["default"]) + "/send/m.room.message/" + transactionId;


const matrixParams = {
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + params.accessToken
},
isTemplateJSON: true,
showPaths: params.showPaths,
hooks: hooks,
name: "reportMatrix",
method: "put",
templates: {}
};

super(channels, matrixParams, env);
this.roomIds = params.roomIds;

if (!params.homeserverUrl || !params.accessToken) {
this.logger.log({
level: 'error',
message: `${this.name} reporting is not enabled: homeserverUrl and accessToken are required`
});
this.enabled = false;
}

if (!params.roomIds || !params.roomIds["default"]) {
this.logger.log({
level: 'error',
message: `${this.name} reporting is not enabled: no default room id provided`
});
this.enabled = false;
}
};

getTemplate = (group, channel, content) => {
return JSON.stringify({
"msgtype": "m.text",
"body": "${summary}${markDownUrl}",
});
};
}

0 comments on commit 8be8ba1

Please sign in to comment.