Commit 592b032d authored by Jean-Marc Le Roux's avatar Jean-Marc Le Roux

Fix the GitLab token/credentials retrieval from the config file.

parent 727dbeb0
......@@ -10,6 +10,14 @@ export default class GitLabAPI {
this.url = url;
}
setOAuth2TokenFromFile(filename) {
this.setOAuth2Token(JSON.parse(fs.readFileSync(filename, 'utf-8')));
}
setOAuth2Token(token) {
request.use((req) => req.set('Authorization', 'Bearer ' + token.access_token));
}
async oauth2Connect(username, password) {
var token = null;
try {
......@@ -36,7 +44,7 @@ export default class GitLabAPI {
fs.writeFileSync('gitlab_token.json', JSON.stringify(token));
}
request.use((req) => req.set('Authorization', 'Bearer ' + token.access_token));
this.setOAuth2Token(token);
return token;
}
......
......@@ -5,14 +5,14 @@ import logger from './logger';
export default class GitLabWebhook {
constructor(route, options, eventHeader = false, eventNames = null) {
this.route = route;
this.url = config.webhook.url + route;
this.url = config.gitlabBot.url + route;
this.options = options;
this.eventHeader = eventHeader;
this.eventNames = eventNames;
}
middleware(req, res, next) {
if (req.headers['x-gitlab-token'] != config.webhook.token) {
if (req.headers['x-gitlab-token'] != config.gitlabBot.token) {
res.sendStatus(400);
return;
}
......@@ -37,7 +37,7 @@ export default class GitLabWebhook {
logger.info('webhook setup on %s', this.route, {webhook: this});
}
static async addMissingProjectHooks(gitlab, token) {
static async addMissingProjectHooks(gitlab) {
const requiredProjectHooks = require('./webhook/project').required;
const projects = await gitlab.getProjects();
......@@ -46,19 +46,19 @@ export default class GitLabWebhook {
const missingProjectHooks = requiredProjectHooks.filter((rh) => projectHooks.filter(hook => hook.url == rh.url).length == 0);
for (var missingHook of missingProjectHooks) {
logger.info("adding missing project webhook", {project: project.id, webhook: missingHook});
await gitlab.addProjectHook(project.id, missingHook.url, token, false, missingHook.options);
await gitlab.addProjectHook(project.id, missingHook.url, config.gitlabBot.oken, false, missingHook.options);
}
}
}
static async addMissingSystemHooks(gitlab, token) {
static async addMissingSystemHooks(gitlab) {
const requiredSystemHooks = require('./webhook/system').required;
const hooks = await gitlab.getSystemHooks();
const missingSystemHooks = requiredSystemHooks.filter((rh) => hooks.filter(hook => hook.url == rh.url).length == 0);
for (var missingHook of missingSystemHooks) {
logger.info("adding missing system webhook", {webhook: missingHook});
await gitlab.addSystemHook(missingHook.url, token, false, missingHook.options);
await gitlab.addSystemHook(missingHook.url, config.gitlabBot.token, false, missingHook.options);
}
}
......
......@@ -27,10 +27,11 @@ import GitLabWebhook from './GitLabWebhook';
bodyWhitelist: ['event_name', 'object_kind', 'object_attributes'],
}));
await gitlab.oauth2Connect(config.gitlab.adminEmail, config.gitlab.adminPassword);
gitlab.setOAuth2TokenFromFile('/etc/amenda/gitlab_token.json');
// await gitlab.oauth2Connect(config.gitlabBot.email, config.gitlabBot.password);
await GitLabWebhook.addMissingProjectHooks(gitlab, config.webhook.token);
await GitLabWebhook.addMissingSystemHooks(gitlab, config.webhook.token);
await GitLabWebhook.addMissingProjectHooks(gitlab);
await GitLabWebhook.addMissingSystemHooks(gitlab);
GitLabWebhook.setupAllWebhooks(app);
app.get('/ping', (req, res) => res.json({ping: 'pong'}));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment