2017-10-29 21:10:26 +00:00
|
|
|
<?php
|
|
|
|
|
2018-01-29 03:24:44 +00:00
|
|
|
ini_set("display_errors", 0);
|
|
|
|
error_reporting(-1);
|
|
|
|
|
2017-12-21 13:08:23 +00:00
|
|
|
ob_start();
|
|
|
|
|
2017-10-29 21:10:26 +00:00
|
|
|
require_once __DIR__ . '/../config.php';
|
|
|
|
use PhpAmqpLib\Message\AMQPMessage;
|
|
|
|
|
2017-12-21 13:08:23 +00:00
|
|
|
class DumpableException extends \Exception{}
|
|
|
|
class InvalidPayloadException extends DumpableException {}
|
|
|
|
class InvalidSignatureException extends DumpableException {}
|
|
|
|
class InvalidEventTypeException extends DumpableException {}
|
|
|
|
class ValidationFailureException extends DumpableException {}
|
|
|
|
class ExecutionFailureException extends DumpableException {}
|
|
|
|
|
2018-01-29 03:24:44 +00:00
|
|
|
function retry_rabbitmq_conn() {
|
|
|
|
$maximum_time = 25;
|
|
|
|
$delay = 1;
|
|
|
|
$timeout = 0.5;
|
|
|
|
|
|
|
|
for ($i = 0.0; $i < $maximum_time; $i += ($timeout + $delay)) {
|
|
|
|
try {
|
|
|
|
return rabbitmq_conn($timeout);
|
|
|
|
} catch (ErrorException $e) {
|
|
|
|
trigger_error(print_r($e, true), E_USER_WARNING);
|
|
|
|
}
|
|
|
|
sleep($delay);
|
|
|
|
}
|
|
|
|
trigger_error("Failed to connect to RabbitMQ", E_USER_WARNING);
|
|
|
|
echo "rabbit failure";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-21 13:08:23 +00:00
|
|
|
function payload() {
|
|
|
|
if (!isset($_SERVER)) {
|
|
|
|
throw new InvalidPayloadException('_SERVER undefined');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($_SERVER['CONTENT_TYPE'])) {
|
|
|
|
throw new InvalidPayloadException('CONTENT_TYPE not set in _SERVER');
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($_SERVER['CONTENT_TYPE']) {
|
|
|
|
case 'application/json':
|
|
|
|
$input = file_get_contents('php://input');
|
|
|
|
if ($input === false) {
|
|
|
|
throw new InvalidPayloadException('Failed to read php://input for application/json');
|
|
|
|
} else {
|
|
|
|
return $input;
|
|
|
|
}
|
|
|
|
default:
|
2017-12-21 13:28:48 +00:00
|
|
|
throw new InvalidPayloadException('Unsupported content type: ' . $_SERVER['CONTENT_TYPE']);
|
2017-12-21 13:08:23 +00:00
|
|
|
}
|
2017-10-29 21:10:26 +00:00
|
|
|
}
|
|
|
|
|
2017-12-21 13:08:23 +00:00
|
|
|
function signature() {
|
|
|
|
if (!isset($_SERVER)) {
|
|
|
|
throw new InvalidSignatureException('_SERVER undefined');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($_SERVER['HTTP_X_HUB_SIGNATURE'])) {
|
|
|
|
throw new InvalidSignatureException('HTTP_X_HUB_SIGNATURE absent from _SERVER');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $_SERVER['HTTP_X_HUB_SIGNATURE'];
|
2017-10-31 08:09:06 +00:00
|
|
|
}
|
|
|
|
|
2017-12-21 13:08:23 +00:00
|
|
|
function event_type() {
|
|
|
|
if (!isset($_SERVER)) {
|
|
|
|
throw new InvalidEventTypeException('_SERVER undefined');
|
|
|
|
}
|
2017-12-21 03:40:24 +00:00
|
|
|
|
2017-12-21 13:08:23 +00:00
|
|
|
if (!isset($_SERVER['HTTP_X_GITHUB_EVENT'])) {
|
|
|
|
throw new InvalidEventTypeException('HTTP_X_GITHUB_EVENT absent from _SERVER');
|
|
|
|
}
|
2017-12-21 03:40:24 +00:00
|
|
|
|
2017-12-21 13:08:23 +00:00
|
|
|
$type = trim($_SERVER['HTTP_X_GITHUB_EVENT']);
|
|
|
|
|
|
|
|
if (strlen($type) === 0) {
|
|
|
|
throw new InvalidEventTypeException('After trimming, event type is zero-length');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $type;
|
2017-12-21 12:36:19 +00:00
|
|
|
}
|
2017-12-21 03:40:24 +00:00
|
|
|
|
2017-12-21 13:08:23 +00:00
|
|
|
function validate_payload_signature($secret, $payload, $signature) {
|
|
|
|
if (!extension_loaded('hash')) {
|
|
|
|
throw new ValidationFailureException('Missing hash extension');
|
|
|
|
}
|
|
|
|
|
|
|
|
$components = explode('=', $signature, 2);
|
|
|
|
if (count($components) != 2) {
|
|
|
|
throw new ValidationFailureException('Provided signature seems invalid after splitting on =');
|
|
|
|
}
|
|
|
|
|
|
|
|
$algo = $components[0];
|
|
|
|
$provided_hash = $components[1];
|
|
|
|
|
|
|
|
if (!in_array($algo, hash_algos(), true)) {
|
|
|
|
throw new ValidationFailureException("Hash algorithm '$algo' is not supported by the extension.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$ok_algos = [
|
|
|
|
'sha1',
|
|
|
|
'sha256',
|
|
|
|
'sha512',
|
|
|
|
];
|
|
|
|
if (!in_array($algo, $ok_algos, true)) {
|
|
|
|
throw new ValidationFailureException("Hash algorithm '$algo' is not considered okay");
|
|
|
|
}
|
|
|
|
|
|
|
|
$calculated_hash = hash_hmac($algo, $payload, $secret);
|
|
|
|
|
|
|
|
return hash_equals($provided_hash, $calculated_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$raw = payload();
|
|
|
|
if (!validate_payload_signature(gh_secret(), $raw, signature())) {
|
|
|
|
throw new ExecutionFailureException('Failed to validate signature');
|
|
|
|
}
|
|
|
|
|
|
|
|
$input = json_decode($raw);
|
|
|
|
if ($input === null) {
|
|
|
|
throw new ExecutionFailureException('Failed to decode the JSON');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($input->repository)) {
|
|
|
|
throw new\ExecutionFailureException('Dataset does not have a repository');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($input->repository->full_name)) {
|
|
|
|
throw new ExecutionFailureException('Dataset repository does not have a name');
|
|
|
|
}
|
|
|
|
|
|
|
|
$name = strtolower($input->repository->full_name);
|
|
|
|
$eventtype = event_type();
|
|
|
|
|
2018-01-29 03:24:44 +00:00
|
|
|
$connection = retry_rabbitmq_conn();
|
2017-12-21 13:08:23 +00:00
|
|
|
$channel = $connection->channel();
|
|
|
|
|
|
|
|
$dec = $channel->exchange_declare('github-events', 'topic', false, true, false);
|
|
|
|
|
|
|
|
$message = new AMQPMessage(json_encode($input),
|
|
|
|
array(
|
|
|
|
'content_type' => 'application/json',
|
|
|
|
'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT,
|
|
|
|
));
|
|
|
|
|
|
|
|
$routing_key = "$eventtype.$name";
|
|
|
|
$rec = $channel->basic_publish($message, 'github-events', $routing_key);
|
2017-10-29 21:10:26 +00:00
|
|
|
|
2017-12-21 13:08:23 +00:00
|
|
|
echo "ok";
|
|
|
|
} catch (DumpableException $e) {
|
2017-12-21 13:28:48 +00:00
|
|
|
trigger_error(print_r($e, true), E_USER_WARNING);
|
|
|
|
header("HTTP/1.1 400 Eh", true, 400);
|
2017-12-21 13:08:23 +00:00
|
|
|
var_dump($e);
|
|
|
|
echo ob_get_clean();
|
|
|
|
} catch (\Exception $e) {
|
2017-12-21 13:28:48 +00:00
|
|
|
trigger_error(print_r($e, true), E_USER_WARNING);
|
|
|
|
header("HTTP/1.1 400 Meh", true, 400);
|
2017-12-21 13:08:23 +00:00
|
|
|
var_dump(get_class($e));
|
|
|
|
echo ob_get_clean();
|
|
|
|
}
|