From 2d273e3841ef4f1b874fe82b8883f62f8f412044 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Thu, 22 Jan 2015 20:57:38 -0800 Subject: Initial commit of insomniaircd_webhook --- README.md | 17 ++++++++++++++ lib/insomniaircd_webhook.rb | 50 ++++++++++++++++++++++++++++++++++++++++ lib/insomniaircd_webhook/util.rb | 13 +++++++++++ 3 files changed, 80 insertions(+) create mode 100644 README.md create mode 100644 lib/insomniaircd_webhook.rb create mode 100644 lib/insomniaircd_webhook/util.rb diff --git a/README.md b/README.md new file mode 100644 index 0000000..fdbba12 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +insomniaircd_webhook +==================== + +This is a simple GitHub webhook receiver to regenerate the InsomniaIRC network configs. + +### Environment Variables + +* `HOSTNAME` should be set to the IRCD hostname to generate the config for. +* `SECRET_KEY` should be set to the AES key to decrypt secrets in the config. +* `SECRET_TOKEN` should be set to the secret token in the GitHub webhook configuration. + +Example command: + + HOSTNAME=hypocrisy.insomniairc.net \ + SECRET_KEY=SUPER_SECRET_KEY \ + SECRET_TOKEN=DIFFERENT_SECRET \ + ruby lib/insomniaircd_webhook.rb diff --git a/lib/insomniaircd_webhook.rb b/lib/insomniaircd_webhook.rb new file mode 100644 index 0000000..1007607 --- /dev/null +++ b/lib/insomniaircd_webhook.rb @@ -0,0 +1,50 @@ +require 'json' +require 'rack/utils' +require 'sinatra' + +FileUtils.mkdir_p 'repos' + +post '/webhook' do + payload = request.body.read + verify_signature(payload) + data = JSON.parse(payload) + + event = request.env['HTTP_X_GITHUB_EVENT'] + if event == "ping" + ping data + + elsif event == "push" + push data + + else + 404 + end +end + +def ping(data) + "pong" +end + +def push(data) + ref = data["ref"] + who = data["sender"]["login"] + repo = data["repository"]["name"] + url = data["repository"]["clone_url"] + + puts "#{who} pushed to #{ref} on repository #{repo}" + + if ref == 'refs/heads/master' + if generate_config repo, url + 200 + else + 500 + end + end +end + +def verify_signature(payload_body) + if ENV['SECRET_TOKEN'] + signature = 'sha1=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), ENV['SECRET_TOKEN'], payload_body) + return halt 500, "Signatures didn't match!" unless Rack::Utils.secure_compare(signature, request.env['HTTP_X_HUB_SIGNATURE']) + end +end diff --git a/lib/insomniaircd_webhook/util.rb b/lib/insomniaircd_webhook/util.rb new file mode 100644 index 0000000..9b647b7 --- /dev/null +++ b/lib/insomniaircd_webhook/util.rb @@ -0,0 +1,13 @@ + +def generate_config(repo, url) + if File.directory? "repos/#{repo}" + clone_success = system "cd repos/#{repo} && git pull" + else + clone_success = system "git clone #{url} repos/#{repo}" + end + + if clone_success + system "cd repos/#{repo} && rake" + end +end + -- cgit v1.2.3