-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.js
97 lines (92 loc) · 2.33 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// @flow
import 'babel-polyfill';
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import morgan from 'morgan';
import basicAuth from 'express-basic-auth';
import prometheusBundle from 'express-prom-bundle';
import {Client as Connection} from 'ldapts';
import {config, getConfig} from './config';
import logger from './logger';
import {Client, Authenticator, Mapping} from './ldap';
import {Healthz, UserAuthentication, TokenAuthentication} from './api';
// setup basic dependencies
const connectionFactory = () => {
return new Connection({
url: config.ldap.uri,
timeout: config.ldap.timeout * 1000,
connectTimeout: config.ldap.timeout * 1000,
});
};
let ldapClient = new Client(
connectionFactory,
config.ldap.baseDn,
config.ldap.bindDn,
config.ldap.bindPw,
);
let authenticator = new Authenticator(ldapClient, config.ldap.filter, logger);
// setup api dependencies
let healthz = new Healthz();
let userAuthentication = new UserAuthentication(
authenticator,
config.jwt.tokenLifetime,
config.jwt.key,
logger,
);
let tokenAuthentication = new TokenAuthentication(
authenticator,
new Mapping(
config.mapping.username,
config.mapping.uid,
config.mapping.groups,
config.mapping.extraFields,
),
config.jwt.key,
logger
);
// setup prometheus exporter
let prometheusExporter = prometheusBundle({
includeMethod: true,
includePath: true,
promClient: {
collectDefaultMetrics: {
timeout: config.prometheus.nodejsProbeInterval,
},
},
});
let prometheusBasicAuth = (req, res, next) => {
let config = getConfig();
if (
Boolean(config.prometheus.username) &&
Boolean(config.prometheus.password)
) {
basicAuth({
users: {
[config.prometheus.username]: config.prometheus.password,
},
})(req, res, next);
} else {
next();
}
};
// setup express
const app = express();
app.use(cors());
app.use(morgan('combined', {
stream: {
write: (message, encoding) => {
logger.info(message);
},
},
}));
app.use('/metrics', prometheusBasicAuth);
app.use(prometheusExporter);
app.get('/healthz', healthz.run);
app.get('/auth', userAuthentication.run);
app.post('/token', bodyParser.json(), tokenAuthentication.run);
app.use((err, req, res, next) => {
logger.error(err);
res.sendStatus(500);
});
export default app;