protect secret
This commit is contained in:
2
deploy-secret.php
Normal file
2
deploy-secret.php
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
<?php
|
||||||
|
return 'jX25kNTa@K1e4;jX25kNTa@K1e4;jX25kNTa@K1e4;';
|
||||||
48
deploy.php
48
deploy.php
@@ -1,23 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
/*
|
const SECRET_FILE = '/home/javi/.deploy-secret.php';
|
||||||
* deploy.php
|
|
||||||
* Webhook receptor para Gitea -> cPanel
|
|
||||||
*
|
|
||||||
* Cambia SOLO estos valores:
|
|
||||||
*/
|
|
||||||
const WEBHOOK_SECRET = 'jX25kNTa@K1e4;jX25kNTa@K1e4;jX25kNTa@K1e4;';
|
|
||||||
const ALLOWED_BRANCH = 'master';
|
const ALLOWED_BRANCH = 'master';
|
||||||
const REPO_PATH = '/home/javi/hello-world.ai1.ovh';
|
const REPO_PATH = '/home/javi/hello-world.ai1.ovh';
|
||||||
const GIT_BIN = '/usr/bin/git';
|
const GIT_BIN = '/usr/bin/git';
|
||||||
const LOG_FILE = __DIR__ . '/deploy.log';
|
const LOG_FILE = __DIR__ . '/deploy.log';
|
||||||
|
|
||||||
/*
|
|
||||||
* Si tu repo NO está en /home/javi/repositories/hello-world,
|
|
||||||
* pon aquí la ruta exacta del repo gestionado por cPanel.
|
|
||||||
*/
|
|
||||||
|
|
||||||
function respond(int $code, string $message): void {
|
function respond(int $code, string $message): void {
|
||||||
http_response_code($code);
|
http_response_code($code);
|
||||||
header('Content-Type: text/plain; charset=utf-8');
|
header('Content-Type: text/plain; charset=utf-8');
|
||||||
@@ -39,6 +28,17 @@ if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|||||||
respond(405, 'Method Not Allowed');
|
respond(405, 'Method Not Allowed');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!file_exists(SECRET_FILE)) {
|
||||||
|
log_line('ERROR: No existe SECRET_FILE: ' . SECRET_FILE);
|
||||||
|
respond(500, 'Secret file not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
$secret = require SECRET_FILE;
|
||||||
|
if (!$secret || !is_string($secret)) {
|
||||||
|
log_line('ERROR: SECRET_FILE inválido');
|
||||||
|
respond(500, 'Secret file error');
|
||||||
|
}
|
||||||
|
|
||||||
$contentType = $_SERVER['CONTENT_TYPE'] ?? '';
|
$contentType = $_SERVER['CONTENT_TYPE'] ?? '';
|
||||||
if (stripos($contentType, 'application/json') === false) {
|
if (stripos($contentType, 'application/json') === false) {
|
||||||
log_line('ERROR: Content-Type inválido: ' . $contentType);
|
log_line('ERROR: Content-Type inválido: ' . $contentType);
|
||||||
@@ -57,13 +57,9 @@ if (!$signatureHeader) {
|
|||||||
respond(403, 'Missing signature');
|
respond(403, 'Missing signature');
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
$expected = hash_hmac('sha256', $payload, $secret);
|
||||||
* Compatibilidad:
|
|
||||||
* - algunos ejemplos usan solo el hash
|
|
||||||
* - otros usan "sha256=HASH"
|
|
||||||
*/
|
|
||||||
$expected = hash_hmac('sha256', $payload, WEBHOOK_SECRET);
|
|
||||||
$provided = trim($signatureHeader);
|
$provided = trim($signatureHeader);
|
||||||
|
|
||||||
if (stripos($provided, 'sha256=') === 0) {
|
if (stripos($provided, 'sha256=') === 0) {
|
||||||
$provided = substr($provided, 7);
|
$provided = substr($provided, 7);
|
||||||
}
|
}
|
||||||
@@ -98,11 +94,10 @@ if (!is_dir(REPO_PATH)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$commands = [
|
$commands = [
|
||||||
'cd ' . escapeshellarg(REPO_PATH),
|
'cd ' . escapeshellarg(REPO_PATH) . ' && ' . GIT_BIN . ' rev-parse --is-inside-work-tree 2>&1',
|
||||||
GIT_BIN . ' rev-parse --is-inside-work-tree',
|
'cd ' . escapeshellarg(REPO_PATH) . ' && ' . GIT_BIN . ' fetch origin 2>&1',
|
||||||
GIT_BIN . ' fetch origin',
|
'cd ' . escapeshellarg(REPO_PATH) . ' && ' . GIT_BIN . ' checkout ' . escapeshellarg(ALLOWED_BRANCH) . ' 2>&1',
|
||||||
GIT_BIN . ' checkout ' . escapeshellarg(ALLOWED_BRANCH),
|
'cd ' . escapeshellarg(REPO_PATH) . ' && ' . GIT_BIN . ' pull origin ' . escapeshellarg(ALLOWED_BRANCH) . ' 2>&1',
|
||||||
GIT_BIN . ' pull origin ' . escapeshellarg(ALLOWED_BRANCH) . ' 2>&1',
|
|
||||||
];
|
];
|
||||||
|
|
||||||
$outputAll = [];
|
$outputAll = [];
|
||||||
@@ -112,6 +107,7 @@ foreach ($commands as $cmd) {
|
|||||||
$output = [];
|
$output = [];
|
||||||
$cmdReturn = 0;
|
$cmdReturn = 0;
|
||||||
exec($cmd, $output, $cmdReturn);
|
exec($cmd, $output, $cmdReturn);
|
||||||
|
|
||||||
$outputAll[] = '$ ' . $cmd;
|
$outputAll[] = '$ ' . $cmd;
|
||||||
$outputAll[] = implode("\n", $output);
|
$outputAll[] = implode("\n", $output);
|
||||||
$outputAll[] = 'exit_code=' . $cmdReturn;
|
$outputAll[] = 'exit_code=' . $cmdReturn;
|
||||||
@@ -122,7 +118,11 @@ foreach ($commands as $cmd) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$logBlock = "Webhook OK | event=push | ref={$ref}\n" . implode("\n", $outputAll) . "\n---";
|
$after = $data['after'] ?? '';
|
||||||
|
$pusher = $data['pusher']['login'] ?? ($data['sender']['login'] ?? 'unknown');
|
||||||
|
|
||||||
|
$logBlock = "Webhook OK | event=push | ref={$ref} | after={$after} | pusher={$pusher}\n" .
|
||||||
|
implode("\n", $outputAll) . "\n---";
|
||||||
log_line($logBlock);
|
log_line($logBlock);
|
||||||
|
|
||||||
if ($returnCode !== 0) {
|
if ($returnCode !== 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user