Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 3bdca6c

Browse files
committed
1 parent 6b41965 commit 3bdca6c

File tree

6 files changed

+42
-85
lines changed

6 files changed

+42
-85
lines changed

._README.md

-4 KB
Binary file not shown.

README.md

Lines changed: 20 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ The following config parameters are supported, they are defined in `config/defau
3232
| :----------------------------- | :----------------------------------------: | :------------------------------: |
3333
| PORT | the port the application will listen on | 3000 |
3434
| LOG_LEVEL | the log level | info |
35-
| TOPIC | the kafka subscribe topic name | events_topic |
35+
| TOPIC | the kafka subscribe topic name | tc-x-events |
36+
| WEBHOOK_SECRET_TOKEN | the webhook security token for githost, it must be same as `WEBHOOK_SECRET_TOKEN` configured for Topcoder-X-backend| `ka75hsrq65cFEr61Hd4x`|
3637
|KAFKA_OPTIONS | the connection option for kafka | see below about KAFKA options |
37-
| GITHUB_SECRET_TOKEN | the webhook security token for github | |
38-
| GITLAB_SECRET_TOKEN | the webhook security token for gitlab | |
39-
| WATCH_REPOS | the repos we want to watch | |
38+
| MONGODB_URL | the MongoDB URL which must be same as Ragnar tool | mongodb://127.0.0.1:27017/ragnar|
4039

4140
KAFKA_OPTIONS should be object as described in https://github.com/SOHU-Co/kafka-node#kafkaclient
4241
For using with SSL, the options should be as
@@ -50,71 +49,41 @@ For using with SSL, the options should be as
5049
}
5150
```
5251

53-
To change the WATCH_REPOS, you'd better create a `config/local.js` file to override the WATCH_REPOS, see `config/sample-local.js` for example.
54-
5552
`config/local.js` will not tracked by git.
5653

57-
Normally you just need config the GITHUB_SECRET_TOKEN and GITLAB_SECRET_TOKEN (optional in this challenge):
54+
## Local Setup
5855

5956
```shell
60-
export GITHUB_SECRET_TOKEN=...
61-
export GITLAB_SECRET_TOKEN=...
57+
npm start
6258
```
6359

64-
Or on windows:
60+
Server should be started at port 3002.
6561

62+
use `ngrok` to make your local deploy accessible by internet:
6663
```shell
67-
set GITHUB_SECRET_TOKEN=...
68-
set GITLAB_SECRET_TOKEN=...
64+
ngrok http 3002
6965
```
7066

67+
Copy the forwarding URL to set in `HOOK_BASE_URL` of topcoder-x-ui in config.json
7168

72-
## GitHub Webhook Setup
69+
## Setup for verification
70+
Before verifying the tool, 4 service needs be configured and run them
71+
- processor
72+
- receiver
73+
- Ragnar Tool
74+
- Topcoder X (both backend and UI)
7375

74-
- login into github.com
75-
- go to the repository you want to watch
76-
- click: Settings -> Options(in the left panel) -> Webhooks
77-
- click: 'Add Webhook' button
78-
- fill the form:
79-
- Payload URL: `https://<YOUR_HOST>/webhooks/github`,
80-
for example: `https://4bb6c860.ngrok.io/webhooks/github`
81-
- Content Type: application/json
82-
- Secret: type your secret and remember it to set into GITHUB_SECRET_TOKEN
83-
- Check: Send me everything
84-
- Check: Active
85-
- click: 'Add Webhook' button
76+
First login in Ragnar tool with admin and Add owner for which requires topcoder handle, git host's username and type of git host.
8677

87-
## GitLab Webhook Setup (optional for this challenge)
78+
Go to Topcoder X UI login with above used topcoder username and
79+
- go to settings and make sure git hosts are correctly setup, if not click setup and authorize to setup.
8880

89-
- login into gitlab.com
90-
- go to the repository you want to watch
91-
- click: Settings -> Integrations
92-
- fill the form:
93-
- URL: `https://<YOUR_HOST>/webhooks/gitlab`,
94-
for example: `https://4bb6c860.ngrok.io/webhooks/gitlab`
95-
- Secret Token: type your secret and remember it to set into GITLAB_SECRET_TOKEN
96-
- Trigger: Check all the events
97-
- Check: Enable SSL Verifications
98-
- click: 'Add Webhook' button
81+
- Go to Topcoder X UI and go to project management and add a project from git account and click save, and edit the same project and click 'Add Webhooks' button (you need to add personnel access token), verify that webhooks are set up correctly on git host's project.
9982

100-
101-
## Local Setup
102-
103-
```shell
104-
npm start
105-
```
106-
107-
Server should be started at port 3000.
108-
109-
use `ngrok` to make your local deploy accessible by internet:
110-
```shell
111-
ngrok http 3000
112-
```
83+
Now, receiver service can receive the webhooks from git host's project. Now you can verify this service by following the verfication steps below
11384

11485
## GitHub Verification
11586

116-
- properly config and run the `receiver` app.
117-
- properly config and run the `processor` app.
11887
- create an issue in the repo, you can see the logs in `receiver` and `processor`, the `issue.created` event is generated.
11988
- update an issue in the repo, you can see the logs in `receiver` and `processor`, the `issue.updated` event is generated.
12089
- create a comment on an issue, you can see the logs in `receiver` and `processor`, the `comment.created` event is generated.
@@ -128,8 +97,6 @@ ngrok http 3000
12897

12998
## Gitlab Verification
13099

131-
- properly config and run the `receiver` app.
132-
- properly config and run the `processor` app.
133100
- create an issue in the repo, you can see the logs in `receiver` and `processor`, the `issue.created` event is generated.
134101
- update an issue in the repo, you can see the logs in `receiver` and `processor`, the `issue.updated` event is generated.
135102
- create a comment on an issue, you can see the logs in `receiver` and `processor`, the `comment.created` event is generated.

config/default.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ module.exports = {
1717
PORT: process.env.PORT || 3002, // eslint-disable-line no-magic-numbers
1818
LOG_LEVEL: process.env.LOG_LEVEL || 'info',
1919
TOPIC: process.env.TOPIC || 'tc-x-events',
20-
GITHUB_SECRET_TOKEN: process.env.GITHUB_SECRET_TOKEN || 'ghostar',
21-
GITLAB_SECRET_TOKEN: process.env.GITLAB_SECRET_TOKEN || 'ghostar',
20+
WEBHOOK_SECRET_TOKEN: process.env.WEBHOOK_SECRET_TOKEN || 'ka75hsrq65cFEr61Hd4x',
2221
KAFKA_OPTIONS: {
2322
kafkaHost: process.env.KAFKA_HOST || 'localhost:9092',
2423
sslOptions: {
25-
cert: process.env.KAFKA_CLIENT_CERT || fs.readFileSync('./kafka_client.cer'),
26-
key: process.env.KAFKA_CLIENT_CERT_KEY || fs.readFileSync('./kafka_client.key')
27-
}
24+
cert: process.env.KAFKA_CLIENT_CERT || fs.readFileSync('./kafka_client.cer'), // eslint-disable-line no-sync
25+
key: process.env.KAFKA_CLIENT_CERT_KEY || fs.readFileSync('./kafka_client.key') // eslint-disable-line no-sync
26+
}
2827
},
29-
MONGODB_URL: process.env.MONGODB_URI || 'mongodb://heroku_mx614sjn:[email protected]:41464/heroku_mx614sjn'
28+
MONGODB_URL: process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/ragnar'
3029
};

models/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
const config = require('config');
1414
const mongoose = require('mongoose');
15+
1516
mongoose.Promise = global.Promise;
1617
const connection = mongoose.createConnection(config.MONGODB_URL);
1718
const IssueCreatedEvent = require('./IssueCreatedEvent');
@@ -23,6 +24,7 @@ const UserUnassignedEvent = require('./UserUnassignedEvent');
2324
const PullRequestCreatedEvent = require('./PullRequestCreatedEvent');
2425
const PullRequestClosedEvent = require('./PullRequestClosedEvent');
2526
const LabelUpdatedEvent = require('./LabelUpdatedEvent');
27+
const Challenge = require('./Challenge');
2628

2729
module.exports = {
2830
IssueCreatedEvent,
@@ -34,5 +36,5 @@ module.exports = {
3436
PullRequestCreatedEvent,
3537
PullRequestClosedEvent,
3638
LabelUpdatedEvent,
37-
Challenge: connection.model('Challenge', require('./Challenge')),
39+
Challenge: connection.model('Challenge', Challenge)
3840
};

routes/middlewares/RepositoryFilter.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010
'use strict';
1111
const _ = require('lodash');
12-
const config = require('config');
1312

1413
const logger = require('../../utils/logger');
1514
const Challenge = require('../../models').Challenge;
@@ -24,15 +23,8 @@ module.exports = (provider) => async (req, res, next) => {
2423
repoNames = [repo.homepage, repo.http_url, repo.url, repo.ssh_url];
2524
}
2625
let found = false;
27-
var challenges = await Challenge.find({archieved: false});
28-
_.forEach(repoNames, (r) => {
29-
_.forEach(challenges, (challenge) => {
30-
if (challenge.repoUrl === r) {
31-
found = true;
32-
}
33-
});
34-
});
35-
26+
const challenges = await Challenge.find({archieved: false});
27+
found = _.some(challenges, (challenge) => _.includes(repoNames, challenge.repoUrl));
3628
if (found) {
3729
return next();
3830
}

routes/middlewares/SecurityChecker.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,22 @@ const config = require('config');
1313
const logger = require('../../utils/logger');
1414

1515
module.exports = (provider) => (req, res, next) => {
16+
let isValid = false;
1617
if (provider === 'github') {
17-
const hash = crypto.createHmac('sha1', config.GITHUB_SECRET_TOKEN).update(req.rawBody).digest('hex');
18-
if (`sha1=${hash}` !== req.header('X-Hub-Signature')) {
19-
logger.info('Invalid Security Check. Make sure you set the secret token in webhook.');
20-
const err = new Error('Invalid secret token');
21-
err.status = 400;
22-
return next(err);
23-
}
24-
return next();
18+
const hash = crypto.createHmac('sha1', config.WEBHOOK_SECRET_TOKEN).update(req.rawBody).digest('hex');
19+
isValid = `sha1=${hash}` === req.header('X-Hub-Signature');
2520
} else if (provider === 'gitlab') {
26-
if (config.GITLAB_SECRET_TOKEN !== req.header('X-Gitlab-Token')) {
27-
logger.info('Invalid Security Check. Make sure you set the secret token in webhook.');
28-
const err = new Error('Invalid secret token');
29-
err.status = 400;
30-
return next(err);
31-
}
21+
isValid = config.WEBHOOK_SECRET_TOKEN === req.header('X-Gitlab-Token');
22+
} else {
23+
// unknown provider
3224
return next();
3325
}
3426

35-
// unknown provider
27+
if (!isValid) {
28+
logger.info('Invalid Security Check. Make sure you set the secret token in webhook.');
29+
const err = new Error('Invalid secret token');
30+
err.status = 400;
31+
return next(err);
32+
}
3633
return next();
3734
};

0 commit comments

Comments
 (0)