Files
portal/app/tests/Unit/Services/Audit/AuditChainConfigTest.php
T
Дмитрий 1495cd643d
Accessibility (Pa11y live) / a11y (push) Has been cancelled
SAST — Semgrep / Semgrep SAST scan (push) Has been cancelled
fix(audit): hash-chain canonical → global-within-partition (ADR-021, supersedes ADR-018)
Ночной audit:verify-chains падал на pd_processing_log (ложный «high»-инцидент + письма
kdv1@bk.ru). Проверка на живом кластере (read-only, хеши server-side) показала: ВСЕ 4
«per-tenant» audit-таблицы (activity_log, tenant_operations_log, balance_transactions,
pd_processing_log) записаны ГЛОБАЛЬНОЙ цепочкой (0 global-mismatch), per-tenant верификация
ложно ругается на стыках клиентов. Предпосылка ADR-018 (триггер даёт per-tenant через RLS)
эмпирически неверна — изоляция никогда не была реализована. Данные целостны.

Решение владельца (с учётом что задет и денежный balance_transactions): канон — global.
AuditChainConfig: partition='' для 4 таблиц (verify/rebuild читают конфиг → авто-global).
Данные/схему/триггер/деньги НЕ трогаем (0 mismatch под global, rebuild не нужен).

TDD: новый тест «cross-tenant rows chained by trigger (global) verify intact»; обновлены 3
теста старого per-tenant поведения (config-mirror, per-tenant regression → supersede,
rebuild per-tenant → global). Тесты: 30 audit + 23 Pd/Scheduler зелёные, pint + PHPStan(0) чисто.

Решение задокументировано в docs/adr/ADR-021; ADR-018 → Superseded.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-09 08:54:10 +03:00

38 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
use App\Services\Audit\AuditChainConfig;
it('exposes all 6 audit tables', function (): void {
expect(array_keys(AuditChainConfig::TABLES))->toEqual([
'auth_log',
'activity_log',
'tenant_operations_log',
'balance_transactions',
'pd_processing_log',
'saas_admin_audit_log',
]);
});
it('all 6 audit tables use global-within-partition chain (ADR-021, supersedes ADR-018)', function (): void {
// ADR-021: триггер де-факто пишет глобальную цепочку для ВСЕХ таблиц (проверено на
// проде 2026-07-09, включая balance_transactions). per-tenant scope ADR-018 никогда
// не был реализован → все partition-clause пустые (global-within-partition).
foreach (AuditChainConfig::TABLES as $table => $cfg) {
expect($cfg['partition'])->toEqual('', "table {$table} must use global chain");
}
});
it('rowExpression builds ROW(...) with NULL::bytea at __log_hash__ position', function (): void {
$expr = AuditChainConfig::rowExpression('activity_log');
expect($expr)->toEqual(
'ROW(t.id, t.tenant_id, t.user_id, t.deal_id, t.event, t.old_value, '
.'t.new_value, t.context, t.ip_address, t.user_agent, NULL::bytea, t.created_at)'
);
});
it('rowExpression throws on unknown table', function (): void {
AuditChainConfig::rowExpression('unknown_table');
})->throws(InvalidArgumentException::class);