Skip to content

Commit f0def93

Browse files
Merge pull request #3 from beeper/rr-bearer-secret
Allow authenticating with bearer token
2 parents 673cb7f + e5276ee commit f0def93

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ Main features:
1919

2020
### Configuration
2121

22-
Whether running manually or via the Docker image, the configuration is set
22+
Whether running manually or via the Docker image, the configuration is set
2323
via environment variables. When running manually, copy `.env.default`
24-
into `.env`, set the values and they will be loaded automatically.
25-
When using the Docker image, set the environment variables when running
24+
into `.env`, set the values and they will be loaded automatically.
25+
When using the Docker image, set the environment variables when running
2626
the container.
2727

2828
### Docker
@@ -37,11 +37,20 @@ You will need to configure a webhook receiver in Alertmanager. It should looks s
3737
receivers:
3838
- name: 'myreceiver'
3939
webhook_configs:
40-
- url: 'https://my-matrix-alertmanager.tld/alerts?secret=veryverysecretkeyhere'
40+
- url: 'https://my-matrix-alertmanager.tld/alerts'
41+
http_config:
42+
authorization:
43+
type: Bearer
44+
credentials: 'veryverysecretkeyhere'
4145
```
4246
4347
The secret key obviously should match the one in the alertmanager configuration.
4448
49+
The configuration above will pass the secret as an Authorization
50+
header bearer token, alternatively you can pass it as a query
51+
parameter `secret`, but if you do it that way then it is not redacted
52+
from the Alertmanager web UI so this is not really recommended.
53+
4554
### Prometheus rules
4655

4756
Add some styling to your prometheus rules

src/routes.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
11
const client = require('./client')
22
const utils = require('./utils')
33

4+
const crypto = require('crypto')
5+
6+
const passwordsEqual = (a, b) => {
7+
return a && b && a.length === b.length && crypto.timingSafeEqual(Buffer.from(a), Buffer.from(b))
8+
}
9+
410
const routes = {
511
getRoot: (req, res) => {
612
res.send('Hey 👋')
713
},
814
postAlerts: async (req, res) => {
9-
const secret = req.query.secret
10-
if (secret !== process.env.APP_ALERTMANAGER_SECRET) {
15+
let authorized = false
16+
let expectedSecret = process.env.APP_ALERTMANAGER_SECRET
17+
18+
if (!expectedSecret) {
19+
console.error("APP_ALERTMANAGER_SECRET is not configured, unable to authenticate requests")
20+
res.status(500).end()
21+
return
22+
}
23+
24+
if (passwordsEqual(req.query.secret, expectedSecret)) {
25+
authorized = true
26+
}
27+
28+
if (passwordsEqual(req.get('authorization'), `Bearer ${expectedSecret}`)) {
29+
authorized = true
30+
}
31+
32+
if (!authorized) {
1133
res.status(403).end()
1234
return
1335
}
36+
1437
const alerts = utils.parseAlerts(req.body)
1538

1639
if (!alerts) {

0 commit comments

Comments
 (0)