Skip to content

Commit 258a311

Browse files
dependabot[bot]aatunyammsalmenatlibfi-jonollil
authored
Bump the development-dependencies group across 1 directory with 2 updates (#183)
* Feature url webhook handler (#155) * Bump @natlibfi/melinda-backend-commons (#143) Bumps the production-dependencies group with 1 update: [@natlibfi/melinda-backend-commons](https://github.com/natlibfi/melinda-backend-commons-js). Updates `@natlibfi/melinda-backend-commons` from 2.3.1 to 2.3.2 - [Release notes](https://github.com/natlibfi/melinda-backend-commons-js/releases) - [Commits](NatLibFi/melinda-backend-commons-js@v2.3.1...v2.3.2) --- updated-dependencies: - dependency-name: "@natlibfi/melinda-backend-commons" dependency-type: direct:production update-type: version-update:semver-patch dependency-group: production-dependencies ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Update deps * 1.0.5-alpha.5 * init url route * init url route * fix-lint * merge routes * moar logs * moar logs * moar logs * less logs * less logs * move ip whitelist to route * move ip whitelist to route * Add newest package-lock --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Minttu Hurme <[email protected]> Co-authored-by: natlibfi-jonollil <[email protected]> * Bump the development-dependencies group across 1 directory with 2 updates Bumps the development-dependencies group with 2 updates in the / directory: [@babel/core](https://github.com/babel/babel/tree/HEAD/packages/babel-core) and [@natlibfi/eslint-config-melinda-backend](https://github.com/natlibfi/eslint-config-melinda-backend). Updates `@babel/core` from 7.27.4 to 7.27.7 - [Release notes](https://github.com/babel/babel/releases) - [Changelog](https://github.com/babel/babel/blob/main/CHANGELOG.md) - [Commits](https://github.com/babel/babel/commits/v7.27.7/packages/babel-core) Updates `@natlibfi/eslint-config-melinda-backend` from 3.0.5 to 3.0.6 - [Release notes](https://github.com/natlibfi/eslint-config-melinda-backend/releases) - [Commits](NatLibFi/eslint-config-melinda-backend@v3.0.5...v3.0.6) --- updated-dependencies: - dependency-name: "@babel/core" dependency-version: 7.27.7 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: development-dependencies - dependency-name: "@natlibfi/eslint-config-melinda-backend" dependency-version: 3.0.6 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: development-dependencies ... Signed-off-by: dependabot[bot] <[email protected]> --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: Aatu Nykänen <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Minttu Hurme <[email protected]> Co-authored-by: natlibfi-jonollil <[email protected]>
1 parent cb39b55 commit 258a311

File tree

4 files changed

+62
-34
lines changed

4 files changed

+62
-34
lines changed

package-lock.json

Lines changed: 26 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import createWebhookRoute from './routes/webhookRoute';
99

1010

1111
export default async function ({
12-
httpPort, githubMetaUrl, openshiftWebhookUrl, ipWhiteList
12+
httpPort, githubMetaUrl, openshiftWebhookUrl, ipWhiteList, urlWhiteList
1313
}) {
1414
const logger = createLogger();
1515
const server = await initExpress();
@@ -29,8 +29,7 @@ export default async function ({
2929
const app = express();
3030
app.set('trust proxy', true);
3131
app.use(createExpressLogger());
32-
app.use(whiteListMiddleware);
33-
app.use('/webhooks', createWebhookRoute(openshiftWebhookUrl));
32+
app.use('/webhooks', createWebhookRoute(whiteListMiddleware, openshiftWebhookUrl, urlWhiteList));
3433

3534
app.use(handleError);
3635

src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export const httpPort = readEnvironmentVariable('HTTP_PORT', {defaultValue: '808
55
export const githubMetaUrl = readEnvironmentVariable('GITHUB_META_URL', {defaultValue: 'https://api.github.com/meta'});
66
export const openshiftWebhookUrl = readEnvironmentVariable('OPENSHIFT_WEBHOOK_URL');
77
export const ipWhiteList = readEnvironmentVariable('IP_WHITELIST', {defaultValue: [], format: v => JSON.parse(v)});
8+
export const urlWhiteList = readEnvironmentVariable('URL_WHITELIST', {defaultValue: [], format: v => JSON.parse(v)});

src/routes/webhookRoute.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,26 @@ import fetch from 'node-fetch';
44
import httpStatus from 'http-status';
55
import bodyParser from 'body-parser';
66

7-
export default function (openshiftWebhookUrl) { // eslint-disable-line no-unused-vars
7+
export default function (whiteListMiddleware, openshiftWebhookUrl, urlWhiteList) { // eslint-disable-line no-unused-vars
88
const logger = createLogger();
99

1010
return new Router()
11-
.post('/:project/:buildConfig/:id', bodyParser.json(), handleHook)
12-
.post('/namespaces/:project/buildconfigs/:buildConfig/webhooks/:id/generic', bodyParser.json(), handleHook)
13-
.post('/apis/build.openshift.io/v1/namespaces/:project/buildconfigs/:buildConfig/webhooks/:id/generic', bodyParser.json(), handleHook)
11+
.post('/:project/:buildConfig/:id', whiteListMiddleware, bodyParser.json(), handleHook)
12+
.post('/namespaces/:project/buildconfigs/:buildConfig/webhooks/:id/generic', whiteListMiddleware, bodyParser.json(), handleHook)
13+
.post('/apis/build.openshift.io/v1/namespaces/:project/buildconfigs/:buildConfig/webhooks/:id/generic', whiteListMiddleware, bodyParser.json(), handleHook)
14+
.post('/url', bodyParser.json(), handleUrlHook)
1415
.use(handleError);
1516

1617
function handleHook(req, res) {
1718
logger.debug('webhookRoute/handleHook');
1819
const {project, buildConfig, id} = req.params;
1920
const data = req.body;
20-
logger.debug('data: ', data);
21+
22+
if ('repository' in data && 'branch' in data) { // eslint-disable-line functional/no-conditional-statements
23+
logger.debug('Repository: ', data.repository);
24+
logger.debug('Branch: ', data.branch);
25+
}
26+
2127
const triggerUrl = `${openshiftWebhookUrl}/${project}/buildconfigs/${buildConfig}/webhooks/${id}/generic`;
2228
fetch(
2329
triggerUrl,
@@ -32,6 +38,28 @@ export default function (openshiftWebhookUrl) { // eslint-disable-line no-unused
3238
res.status(httpStatus.OK).json({status: 200});
3339
}
3440

41+
function handleUrlHook(req, res) {
42+
const {triggerUrl} = req.query;
43+
44+
if (!urlWhiteList.some(urlRegexp => new RegExp(urlRegexp, 'u').test(triggerUrl))) {
45+
return res.status(httpStatus.FORBIDDEN).json({status: 403});
46+
}
47+
48+
const data = req.body;
49+
50+
fetch(
51+
triggerUrl,
52+
{
53+
method: 'post',
54+
headers: {
55+
'Content-Type': 'application/json'
56+
},
57+
body: JSON.stringify(data)
58+
}
59+
);
60+
res.status(httpStatus.OK).json({status: 200});
61+
}
62+
3563
function handleError(err, req, res, next) {
3664
logger.debug('webhookRoute/handleError');
3765
logger.error('Error: ', err);

0 commit comments

Comments
 (0)