Skip to content

Commit

Permalink
extract prometheus basic auth into function
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Jucker committed Jun 12, 2019
1 parent daba70f commit e745e79
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import morgan from 'morgan';
import basicAuth from 'express-basic-auth';
import prometheusBundle from 'express-prom-bundle';
import {Client as Connection} from 'ldapts';
import {config} from './config';
import {config, getConfig} from './config';
import logger from './logger';
import {Client, Authenticator, Mapping} from './ldap';
import {Healthz, UserAuthentication, TokenAuthentication} from './api';
Expand Down Expand Up @@ -55,6 +55,21 @@ let prometheusExporter = prometheusBundle({
},
},
});
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();
Expand All @@ -66,16 +81,7 @@ app.use(morgan('combined', {
},
},
}));
if (
Boolean(config.prometheus.username) &&
Boolean(config.prometheus.password)
) {
app.use('/metrics', basicAuth({
users: {
[config.prometheus.username]: config.prometheus.password,
},
}));
};
app.use('/metrics', prometheusBasicAuth);
app.use(prometheusExporter);
app.get('/healthz', healthz.run);
app.get('/auth', userAuthentication.run);
Expand Down

0 comments on commit e745e79

Please sign in to comment.