Skip to content
This repository has been archived by the owner on Apr 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1497 from DashboardHub/issue-1465-v2
Browse files Browse the repository at this point in the history
feat(repositories)#1465 secure webhooks
  • Loading branch information
eddiejaoude authored Sep 12, 2019
2 parents 470dd8a + 6d34857 commit bc13a5e
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Please get in touch via [@DashboardHub](https://twitter.com/DashboardHub) and le
2. Enter the 2 OAuth private keys from GitHub into the Firebase Authentication
3. Click **Databases** and create an empty `firestore` database (indexes, security, collections and rules will all be automatically created later on as part of the deployment)
4. Update `{{ FIREBASE_FUNCTIONS_URL }}` in file `functions/src/environments/environment.ts` with your function subdomain, for example `us-central1-pipelinedashboard-test`
4. Update `{{ GITHUB_WEBHOOK_SECRET }}` in file `functions/src/environments/environment.ts` with your private secret key (random string), this is used to protect your webhook function, for example `pipelinedashboard-test-123`

#### Angular

Expand Down
21 changes: 15 additions & 6 deletions functions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions functions/src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
export const enviroment: Config = {
githubWebhook: {
url: 'https://{{ FIREBASE_FUNCTIONS_URL }}.cloudfunctions.net/responseGitWebhookRepository',
secret: '{{ GITHUB_WEBHOOK_SECRET }}',
content_type: 'json',
insecure_ssl: '0',
events: [
// IMPLEMENTED
'create',
Expand Down Expand Up @@ -61,6 +64,9 @@ export const enviroment: Config = {
interface Config {
githubWebhook: {
url: string,
secret: string,
content_type: 'json' | 'form',
insecure_ssl: '0' | '1',
events: string[],
}
}
13 changes: 10 additions & 3 deletions functions/src/repository/create-git-webhook-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ export function createWebhook(repositoryFullName: string, token: string): Promis
events: enviroment.githubWebhook.events,
config: {
url: enviroment.githubWebhook.url,
content_type: 'json',
insecure_ssl: '0',
secret: enviroment.githubWebhook.secret,
content_type: enviroment.githubWebhook.content_type,
insecure_ssl: enviroment.githubWebhook.insecure_ssl,
},
}
return GitHubClientPost<GitHubRepositoryWebhookResponse>(`/repos/${repositoryFullName}/hooks`, token, body);
Expand All @@ -53,7 +54,10 @@ export async function getWebhook(repositoryFullName: string, token: string): Pro
const exist: GitHubRepositoryWebhookResponse = await findWebhook(repositoryFullName, token);

if (exist) {
let isEqual: boolean = exist.events.length === enviroment.githubWebhook.events.length;
let isEqual: boolean = exist.events.length === enviroment.githubWebhook.events.length
&& exist.config.content_type === enviroment.githubWebhook.content_type
&& exist.config.insecure_ssl === enviroment.githubWebhook.insecure_ssl
&& ((!exist.config.secret && !enviroment.githubWebhook.secret) || (!!exist.config.secret && !!enviroment.githubWebhook.secret));

if (isEqual) {

Expand All @@ -65,11 +69,14 @@ export async function getWebhook(repositoryFullName: string, token: string): Pro
}

if (isEqual) {
Logger.info('Webhook is exist');
return GitHubRepositoryWebhookMapper.import(exist);
}
}
Logger.info('Webhook is deleting');
await deleteWebhook(repositoryFullName, exist.id, token);
}

Logger.info('Webhook is creating');
return GitHubRepositoryWebhookMapper.import(await createWebhook(repositoryFullName, token));
}
12 changes: 11 additions & 1 deletion functions/src/repository/response-git-webhook-repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// Third party modules
import * as CORS from 'cors';
import * as crypto from "crypto";
import { https, HttpsFunction, Response } from 'firebase-functions';

import { enviroment } from '../environments/environment';

// Dashboard hub firebase functions models/mappers
import { GitHubClient } from '../client/github';
import { Logger } from '../client/logger';
import { GitHubContributorInput, GitHubContributorMapper } from '../mappers/github/index.mapper';
Expand Down Expand Up @@ -33,7 +38,12 @@ export interface ResponseGitWebhookRepositoryInput {

export const onResponseGitWebhookRepository: HttpsFunction = https.onRequest((req: https.Request, res: Response) => {
return cors(req, res, () => {
Logger.info(`${req.protocol}://${req.hostname} ; onResponseGitWebhookRepository: success!`);
const sig: string = 'sha1=' + crypto.createHmac('sha1', enviroment.githubWebhook.secret).update(req.rawBody).digest('hex');

if (sig !== req.headers['x-hub-signature']) {
res.status(401).send('Error secret token!');
return;
}

const inputData: any = req.body;
let result: Promise<any>;
Expand Down
1 change: 1 addition & 0 deletions scripts/deployment/dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# FUNCTIONS
(cd functions; npm install)
(cd functions/src/environments; sed -i 's/{{ GITHUB_WEBHOOK_SECRET }}/'$GITHUB_WEBHOOK_SECRET'/g' environment.ts)
(cd functions/src/environments; sed -i 's/{{ FIREBASE_FUNCTIONS_URL }}/us-central1-pipelinedashboard-dev/g' environment.ts)

# WEB
Expand Down
1 change: 1 addition & 0 deletions scripts/deployment/eddie.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# FUNCTIONS
(cd functions; npm install)
(cd functions/src/environments; sed -i 's/{{ GITHUB_WEBHOOK_SECRET }}/'$GITHUB_WEBHOOK_SECRET'/g' environment.ts)
(cd functions/src/environments; sed -i 's/{{ FIREBASE_FUNCTIONS_URL }}/us-central1-pipelinedashboard-eddie/g' environment.ts)

# WEB
Expand Down
1 change: 1 addition & 0 deletions scripts/deployment/khush.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# FUNCTIONS
(cd functions; npm install)
(cd functions/src/environments; sed -i 's/{{ GITHUB_WEBHOOK_SECRET }}/'$GITHUB_WEBHOOK_SECRET'/g' environment.ts)
(cd functions/src/environments; sed -i 's/{{ FIREBASE_FUNCTIONS_URL }}/us-central1-pipelinedashboard-khush/g' environment.ts)

# WEB
Expand Down
1 change: 1 addition & 0 deletions scripts/deployment/prod.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# FUNCTIONS
(cd functions; npm install)
(cd functions/src/environments; sed -i 's/{{ GITHUB_WEBHOOK_SECRET }}/'$GITHUB_WEBHOOK_SECRET'/g' environment.ts)
(cd functions/src/environments; sed -i 's/{{ FIREBASE_FUNCTIONS_URL }}/us-central1-pipelinedashboard/g' environment.ts)

# WEB
Expand Down

0 comments on commit bc13a5e

Please sign in to comment.