...
 
Commits (3)
......@@ -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'}));
......
......@@ -8,10 +8,20 @@ docker_group_members:
nodejs_version: "6.x"
gitlab_bot_webhook_token: "a153b0d18961a9947a63888de405eb40"
gitlab_bot_user:
email: "duralex.sedlex@{{ hostname }}"
password: "duralexsedlex" # FIXME
username: "duralexsedlex"
name: "DuraLex SedLex"
gitlab_admin_email: "admin@{{ hostname }}"
gitlab_admin_password: "adminadmin"
gitlab_bot_webhook_token: "a153b0d18961a9947a63888de405eb40"
gitlab_users:
- email: "{{ gitlab_bot_user.email }}"
password: "{{ gitlab_bot_user.password }}"
username: "{{ gitlab_bot_user.username }}"
name: "{{ gitlab_bot_user.name }}"
pm2_service_enabled: yes
pm2_service_state: started
......
......@@ -4,7 +4,7 @@
- all
roles:
- { role: config, tags: ["config", "api", "gitlab-bot"] }
- { role: gitlab, tags: ["api", "gitlab"] }
- { role: gitlab, tags: ["api", "gitlab", "gitlab-bot"] }
- { role: reverse-proxy, tags: ["api", "doc", "gitlab"] }
- { role: api, tags: ["api"] }
- { role: app, tags: ["app"] }
......
......@@ -4,8 +4,12 @@
"adminEmail": "{{ gitlab_admin_email }}",
"adminPassword": "{{ gitlab_admin_password }}"
},
"webhook": {
"gitlabBot": {
"url": "http://172.17.0.1:3002",
"token": "{{ gitlab_bot_webhook_token }}"
"token": "{{ gitlab_bot_webhook_token }}",
"email": "{{ gitlab_bot_user.email }}",
"password": "{{ gitlab_bot_user.password }}",
"username": "{{ gitlab_bot_user.username }}",
"name": "{{ gitlab_bot_user.name }}"
}
}
---
gitlab_version: 10.1.0-ce.0
gitlab_users:
......@@ -17,3 +17,49 @@
GITLAB_OMNIBUS_CONFIG: "external_url 'https://{{ hostname }}/admin/'; nginx['listen_port'] = 80; nginx['listen_https'] = false"
GITLAB_ROOT_EMAIL: "{{ gitlab_admin_email }}"
GITLAB_ROOT_PASSWORD: "{{ gitlab_admin_password }}"
- name: Check if there is an existing OAuth2 token
stat:
path: /etc/amenda/gitlab_token.json
register: gitlab_oauth2_token_json_file
- name: Get a new OAuth2 token
changed_when: no
uri:
url: "http://127.0.0.1:8000/admin/oauth/token"
body_format: "json"
validate_certs: no
method: POST
body:
grant_type: "password"
username: "{{ gitlab_admin_email }}"
password: "{{ gitlab_admin_password }}"
register: gitlab_oauth2_token_res
when: not gitlab_oauth2_token_json_file.stat.exists
- name: Save the GitLab OAuth2 token in /etc/amenda/gitlab_token.json
copy: content="{{ gitlab_oauth2_token_res.json | to_nice_json }}\n" dest=/etc/amenda/gitlab_token.json
when: not gitlab_oauth2_token_json_file.stat.exists
- name: Set the gitlab_oauth2_token fact
set_fact:
gitlab_oauth2_token: "{{ gitlab_oauth2_token_json_file.stat.exists | ternary(lookup('file', '/etc/amenda/gitlab_token.json') | from_json, gitlab_oauth2_token_res.json) }}"
- name: Create GitLab users
uri:
url: "http://127.0.0.1:8000/admin/api/v4/users"
body_format: "json"
validate_certs: no
method: POST
status_code: [200, 201, 409]
body:
email: "{{ item.email }}"
password: "{{ item.password }}"
username: "{{ item.username }}"
name: "{{ item.name }}"
skip_confirmation: "true"
headers:
Authorization: 'Bearer {{ gitlab_oauth2_token.access_token }}'
with_items: "{{ gitlab_users }}"
register: gitlab_created_user
changed_when: gitlab_created_user.status == 201