formations/developpement/securite_web/ressources/prometheus-demo/app/app.js

37 lines
1.1 KiB
JavaScript

// Voir https://github.com/siimon/prom-client
const client = require('prom-client');
// Voir http://expressjs.com/
const express = require('express')
const basicAuth = require('basic-auth-connect');
// Création de notre application Web
const app = express()
// Définition des métriques
const authFailureCounter = new client.Counter({
name: 'auth_failure_counter',
help: 'Tentatives \'authentification échouées',
labelNames: ['username', 'ip'],
});
// On expose les métriques de notre récolteur sur /metrics
app.get('/metrics', (req, res) => res.send(client.register.metrics()))
app.use(function(req, res, next) {
const basicUserMiddleware = basicAuth(function(username, password) {
const isAuthenticated = username == 'admin' && password == 'admin';
if (!isAuthenticated) {
authFailureCounter.inc({ip: req.connection.remoteAddress, username: username})
}
return isAuthenticated;
})
return basicUserMiddleware(req, res, next);
});
app.get('/', (req, res) => {
res.send(JSON.stringify(authFailureCounter.get(), null, 2))
})
// On écoute sur le port 0.0.0.0:3000
app.listen(3000, () => console.log('listening on port 3000'))