Add PROMETHEUS_BEARER_TOKEN env var support

This commit is contained in:
Lauri Võsandi 2021-06-09 22:58:21 +03:00
parent 6feef9f9e0
commit b13952b161

View File

@ -4,11 +4,14 @@ import asyncio
import os import os
from collections import Counter from collections import Counter
from motor.motor_asyncio import AsyncIOMotorClient from motor.motor_asyncio import AsyncIOMotorClient
from sanic import Sanic, response from sanic import Sanic, response, exceptions
app = Sanic("exporter") app = Sanic("exporter")
MONGO_URI = os.getenv("MONGO_URI", "mongodb://127.0.0.1:27017/default") MONGO_URI = os.getenv("MONGO_URI", "mongodb://127.0.0.1:27017/default")
PROMETHEUS_BEARER_TOKEN = os.getenv("PROMETHEUS_BEARER_TOKEN")
if not PROMETHEUS_BEARER_TOKEN:
raise ValueError("No PROMETHEUS_BEARER_TOKEN specified")
@app.listener("before_server_start") @app.listener("before_server_start")
@ -68,6 +71,8 @@ async def openvpn_stats(port, service):
@app.route("/metrics") @app.route("/metrics")
async def view_export(request): async def view_export(request):
if request.token != PROMETHEUS_BEARER_TOKEN:
raise exceptions.Forbidden("Invalid bearer token")
coll = app.ctx.db["certidude_certificates"] coll = app.ctx.db["certidude_certificates"]
async def streaming_fn(response): async def streaming_fn(response):