Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Github shared secret #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/deploys-with-secret.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"secret": "secret",
"deploys": [{
"name": "Webhook Deployer",
"type": "github",
"repo": "https://github.com/Camme/webhook-deployer",
"basepath": "/Users/camilo/Projects/24hr-deployer",
"command": "git pull",
"branch": "master"
}]
}
96 changes: 64 additions & 32 deletions lib/incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var webdep = require('./webdep');
var fs = require("fs");
var path = require("path");
var exec = require('child_process').exec;
var crypto = require('crypto');

exports.control = function(options) {

Expand All @@ -22,55 +23,66 @@ exports.control = function(options) {
error = true;
}

deploysList.forEach(function(deploy) {
if (repoData.hasOwnProperty('signature') && config.hasOwnProperty('signature')) {

if (deploy.type == "github") {
error = true;
}

if (!repoData.error && !repoData.repository) {
webdep.log("Webhook payload was not for a push.");
return;
}
if (validateSignature(req)) {
deploysList.forEach(function(deploy) {

if (deploy.type == "github") {

if (!repoData.error && !repoData.repository) {
webdep.log("Webhook payload was not for a push.");
return;
}

if (!repoData.error) {
if (!repoData.error) {

webdep.log("Checking incoming repo " + repoData.repository.url + "...");
webdep.log("Checking incoming repo " + repoData.repository.url + "...");

// remove .git from the repo string, since the hook will not have that address when it comes from github
// so instead of explaining that you shouldnt enter ".git", we just remove it here.
if (repoData.repository.url == deploy.repo.replace(".git$", "")) {
// remove .git from the repo string, since the hook will not have that address when it comes from github
// so instead of explaining that you shouldnt enter ".git", we just remove it here.
if (repoData.repository.url == deploy.repo.replace(".git$", "")) {

var branch = repoData.ref.split("/").pop();
var branch = repoData.ref.split("/").pop();

webdep.log("> Checking branch '" + branch + "' ...");
webdep.log("> Checking branch '" + branch + "' ...");

if (branch == deploy.branch) {
if (branch == deploy.branch) {

runDeploy(deploy);
runDeploy(deploy);

}
else {
webdep.log("> No, wrong branch, nothing to do");
webdep.log("> Got:");
webdep.log("> " + branch);
webdep.log("> But expected:");
webdep.log("> " + deploy.branch);
}
}
else {
webdep.log("> No, wrong branch, nothing to do");
webdep.log("> Got:");
webdep.log("> " + branch);
webdep.log("> But expected:");
webdep.log("> " + deploy.branch);
webdep.log("No, wrong repo, nothing to do");
webdep.log("Got:");
webdep.log(repoData.repository.url);
webdep.log("But expected:");
webdep.log(deploy.repo.replace(".git", ""));
}

}
else {
webdep.log("No, wrong repo, nothing to do");
webdep.log("Got:");
webdep.log(repoData.repository.url);
webdep.log("But expected:");
webdep.log(deploy.repo.replace(".git", ""));
webdep.log("");
webdep.log("Error while parsing post body");
webdep.log(repoData.error);
}

}
else {
webdep.log("");
webdep.log("Error while parsing post body");
}
}
});
});
} else {
error = true;
webdep.log("Signatures don't match");
}

if (error) {
res.statusCode = 500;
Expand All @@ -88,6 +100,26 @@ exports.control = function(options) {

};

function signBlob(key, blob) {
return 'sha1=' + crypto.createHmac('sha1', key).update(blob).digest('hex')
}

function validateSignature(req) {
var headers = req.headers;
var secret = webdep.getSecret();
var signature;

if (secret || headers.hasOwnProperty('x-hub-signature')) {
if (!secret || !headers.hasOwnProperty('x-hub-signature')) {
return false;
}

signature = signBlob(secret, req.rawBody);
return signature === headers['x-hub-signature'];
}

return true;
}

function runDeploy(deploy) {

Expand Down
5 changes: 5 additions & 0 deletions lib/webdep.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ exports.init = function(newOptions, callback) {
req.on("end", function() {

req.params = {};
req.rawBody = data;

if (req.method == "POST") {
req.body = querystring.parse(data);
Expand Down Expand Up @@ -335,6 +336,10 @@ function getDeployFromId(id) {
return null;
}

exports.getSecret = getSecret = function() {
return options.hasOwnProperty('secret') ? options.secret : false;
}

exports.getDeployList = getDeployList = function() {

var deploysList = {};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"dependencies": {
"commander": "~1.1.1",
"cookies": "~0.3.6",
"crypto": "0.0.3",
"keygrip": "~0.2.2",
"mustache": "~0.7.2",
"node-github": "0.0.3",
Expand Down