Introduce a dedicated `audit` log channel (daily rotation, configurable retention via LOG_AUDIT_DAYS) and a small `auditLog()` / `auditLogWebhookFailure()` helper used to record state-changing API operations and webhook events. Instrumented: - API mutation endpoints (create / update / delete / start / stop / restart) across applications, services, databases (incl. backups, env vars, storage), servers, projects + environments, scheduled tasks, private keys, GitHub apps, cloud provider tokens, Hetzner server provisioning, instance enable/disable. - Webhook signature verification outcomes for GitHub, GitLab, Bitbucket, Gitea and Stripe, plus the Sentinel push endpoint. - Authentication and authorization outcomes via the global exception handler and the `ApiAbility` middleware (unauthenticated, ability-denied, policy-denied). The helper is wrapped in try/catch so logging failures never affect the request path. Successful operations log at `info`; suspicious/denied requests log at `warning`. Operators wanting a failures-only feed can set `LOG_AUDIT_LEVEL=warning`. Includes a feature test suite covering the helper, the webhook providers and the new auth/authorization log paths. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
81 lines
3 KiB
PHP
81 lines
3 KiB
PHP
<?php
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
if (! function_exists('auditLog')) {
|
|
/**
|
|
* Write a security-relevant audit entry to the dedicated `audit` log channel.
|
|
*
|
|
* Never include secrets (private keys, passwords, tokens, webhook secrets,
|
|
* signature header values, env-var values) in $context.
|
|
*
|
|
* @param string $event Dot-namespaced event name, e.g. `api.private_key.created`.
|
|
* @param array<string, mixed> $context Identifiers + outcome details.
|
|
* @param string $level Log level: info | warning | error.
|
|
*/
|
|
function auditLog(string $event, array $context = [], string $level = 'info'): void
|
|
{
|
|
try {
|
|
$request = app()->bound('request') ? request() : null;
|
|
$user = auth()->check() ? auth()->user() : null;
|
|
$token = $user?->currentAccessToken();
|
|
|
|
$base = [
|
|
'event' => $event,
|
|
'ip' => $request?->ip(),
|
|
'ua' => substr((string) $request?->userAgent(), 0, 200),
|
|
'user_id' => $user?->id,
|
|
'user_email' => $user?->email,
|
|
'team_id' => $token ? data_get($token, 'team_id') : null,
|
|
'token_id' => $token?->id ?? null,
|
|
'token_name' => $token?->name ?? null,
|
|
'method' => $request?->method(),
|
|
'path' => $request?->path(),
|
|
];
|
|
|
|
$payload = array_merge($base, $context);
|
|
|
|
Log::channel('audit')->{$level}($event, $payload);
|
|
} catch (Throwable $e) {
|
|
// Audit logging must never break the request path.
|
|
try {
|
|
Log::warning('auditLog failed: '.$e->getMessage(), ['event' => $event]);
|
|
} catch (Throwable) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (! function_exists('auditLogWebhookFailure')) {
|
|
/**
|
|
* Record a webhook signature/auth verification failure to the `audit` channel.
|
|
*/
|
|
function auditLogWebhookFailure(string $provider, string $reason, array $context = []): void
|
|
{
|
|
try {
|
|
$request = app()->bound('request') ? request() : null;
|
|
|
|
$event = "webhook.{$provider}.signature_failed";
|
|
|
|
$base = [
|
|
'event' => $event,
|
|
'reason' => $reason,
|
|
'ip' => $request?->ip(),
|
|
'ua' => substr((string) $request?->userAgent(), 0, 200),
|
|
'method' => $request?->method(),
|
|
'path' => $request?->path(),
|
|
'event_header' => $request?->header('X-GitHub-Event')
|
|
?? $request?->header('X-Gitlab-Event')
|
|
?? $request?->header('X-Gitea-Event')
|
|
?? $request?->header('X-Event-Key'),
|
|
];
|
|
|
|
Log::channel('audit')->warning($event, array_merge($base, $context));
|
|
} catch (Throwable $e) {
|
|
try {
|
|
Log::warning('auditLogWebhookFailure failed: '.$e->getMessage(), ['provider' => $provider]);
|
|
} catch (Throwable) {
|
|
}
|
|
}
|
|
}
|
|
}
|