File tree 5 files changed +30
-3
lines changed
5 files changed +30
-3
lines changed Original file line number Diff line number Diff line change @@ -42,7 +42,12 @@ docker build -t docbot .
42
42
Then run it like this:
43
43
44
44
``` sh
45
- docker run -d -p5000:5000 -e GITHUB_TOKEN=< token> -e GITHUB_SIGN_KEY=< sign_key> --name docbot docbot
45
+ docker run \
46
+ -d -p5000:5000 \
47
+ -e GITHUB_TOKEN=< token> \
48
+ -e GITHUB_SIGN_KEY=< sign_key> \
49
+ -e PROMETHEUS_TOKEN=< token> \
50
+ --name docbot docbot
46
51
```
47
52
48
53
To check that it works try to get ` localhost:5000 ` - it will print the
Original file line number Diff line number Diff line change 3
3
from elasticapm .contrib .flask import ElasticAPM
4
4
from prometheus_flask_exporter import PrometheusMetrics
5
5
from flask import Flask , Response , request
6
+ from flask_httpauth import HTTPTokenAuth
6
7
7
8
from .handlers import webhook_handler , list_events_handler
8
9
from .logging_config import LOGGING_CONFIG
9
- from .utils import is_verified_signature
10
+ from .utils import is_verified_signature , is_verified_prometheus_token
10
11
11
12
12
13
logging .config .dictConfig (LOGGING_CONFIG )
15
16
16
17
17
18
app = Flask (__name__ )
19
+ auth = HTTPTokenAuth ()
18
20
19
21
app .config ['ELASTIC_APM' ] = {
20
22
'SERVICE_NAME' : 'docbot' ,
21
23
}
22
24
apm = ElasticAPM (app , logging = True )
23
25
24
- metrics = PrometheusMetrics (app , group_by = 'endpoint' )
26
+ metrics = PrometheusMetrics (app , group_by = 'endpoint' , metrics_decorator = auth .login_required )
27
+
28
+
29
+ @auth .verify_token
30
+ def verify_token (token ):
31
+ return is_verified_prometheus_token (token )
25
32
26
33
@app .route ("/" , methods = ['GET' ])
27
34
@metrics .do_not_track ()
Original file line number Diff line number Diff line change 2
2
3
3
token = os .environ .get ('GITHUB_TOKEN' )
4
4
github_signature = os .environ .get ('GITHUB_SIGN_KEY' )
5
+ prometheus_token = os .environ .get ('PROMETHEUS_TOKEN' )
5
6
assert token is not None
6
7
assert github_signature is not None
8
+ assert prometheus_token is not None
7
9
doc_requests = [' document\r \n ' , ' document\n ' ]
8
10
bot_name = '@TarantoolBot'
9
11
title_header = 'Title:'
Original file line number Diff line number Diff line change @@ -39,3 +39,15 @@ def is_verified_signature(body, signature):
39
39
if not hmac .compare_digest (expected_signature , signature ):
40
40
return False
41
41
return True
42
+
43
+ def is_verified_prometheus_token (token ):
44
+ """Verify that the payload was sent from Prometheus via Bearer token.
45
+
46
+ Returns True if the request is authorized, otherwise False.
47
+
48
+ Args:
49
+ token: original request token to verify
50
+ """
51
+ if token == settings .prometheus_token :
52
+ return True
53
+ return False
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ click==8.1.3
5
5
ecs-logging == 2.0.0
6
6
elastic-apm == 6.9.1
7
7
Flask == 2.2.5
8
+ Flask-HTTPAuth == 4.8.0
8
9
gunicorn == 20.1.0
9
10
idna == 3.3
10
11
itsdangerous == 2.1.2
You can’t perform that action at this time.
0 commit comments