refactor(abuse): remove legacy telemetry pipeline
parent
300e4c8ba8
commit
0991bf9efa
@ -1,301 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cloud;
|
||||
|
||||
/**
|
||||
* Persists immutable finding-transition facts in the caller's main-database
|
||||
* transaction. This coordinator intentionally performs no external delivery.
|
||||
*/
|
||||
final class CloudAbuseAlertEventCoordinator
|
||||
{
|
||||
/**
|
||||
* @param array<string,mixed> $claim
|
||||
* @param list<array<string,mixed>> $transitions
|
||||
*/
|
||||
public function record(
|
||||
object $main,
|
||||
array $claim,
|
||||
array $transitions,
|
||||
int $now
|
||||
): void {
|
||||
if ($transitions === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
$items = self::canonicalTransitions($transitions);
|
||||
foreach ($items as $item) {
|
||||
if ($item['occurred_at'] !== (int) $claim['bucket_start']) {
|
||||
throw new \LogicException(
|
||||
'Abuse alert transition bucket is inconsistent'
|
||||
);
|
||||
}
|
||||
}
|
||||
$canonicalJson = self::json($items);
|
||||
$digest = hash('sha256', $canonicalJson);
|
||||
$eventKey = hash('sha256', implode("\n", [
|
||||
'cloud-abuse-alert-event-v1',
|
||||
(string) $claim['sample_key'],
|
||||
(string) $claim['instance_id'],
|
||||
(string) $claim['bucket_start'],
|
||||
(string) $claim['rule_version'],
|
||||
(string) $claim['policy_revision'],
|
||||
(string) $claim['policy_hash'],
|
||||
$digest,
|
||||
]));
|
||||
$highestSeverity = self::highestSeverity($items);
|
||||
$eventValues = [
|
||||
'event_key' => $eventKey,
|
||||
'sample_key' => (string) $claim['sample_key'],
|
||||
'instance_id' => (int) $claim['instance_id'],
|
||||
'user_id' => (int) $claim['user_id'],
|
||||
'host_id' => (int) $claim['host_id'],
|
||||
'bucket_start' => (int) $claim['bucket_start'],
|
||||
'rule_version' => (int) $claim['rule_version'],
|
||||
'policy_revision' => (int) $claim['policy_revision'],
|
||||
'policy_hash' => (string) $claim['policy_hash'],
|
||||
'transition_digest' => $digest,
|
||||
'transition_count' => count($items),
|
||||
'highest_severity' => $highestSeverity,
|
||||
'created_at' => $now,
|
||||
];
|
||||
|
||||
$existing = $main->table('cloud_abuse_alert_events')
|
||||
->where('sample_key', $eventValues['sample_key'])
|
||||
->lockForUpdate()->first();
|
||||
if ($existing === null) {
|
||||
$eventId = (int) $main->table('cloud_abuse_alert_events')
|
||||
->insertGetId($eventValues);
|
||||
if ($eventId < 1) {
|
||||
throw new \RuntimeException(
|
||||
'Abuse alert event insert did not return an id'
|
||||
);
|
||||
}
|
||||
foreach ($items as $item) {
|
||||
$main->table('cloud_abuse_alert_event_items')->insert(
|
||||
self::itemValues($eventId, $eventKey, $item, $now)
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
self::assertEventReplay($existing, $eventValues);
|
||||
$eventId = (int) $existing->id;
|
||||
$storedItems = $main->table('cloud_abuse_alert_event_items')
|
||||
->where('event_id', $eventId)->orderBy('item_key')->get();
|
||||
if (count($storedItems) !== count($items)) {
|
||||
throw new \RuntimeException(
|
||||
'Abuse alert event item count conflicts with replay'
|
||||
);
|
||||
}
|
||||
$byKey = [];
|
||||
foreach ($storedItems as $stored) {
|
||||
$byKey[(string) $stored->item_key] = $stored;
|
||||
}
|
||||
foreach ($items as $item) {
|
||||
$expected = self::itemValues(
|
||||
$eventId,
|
||||
$eventKey,
|
||||
$item,
|
||||
$now
|
||||
);
|
||||
$stored = $byKey[$expected['item_key']] ?? null;
|
||||
if ($stored === null) {
|
||||
throw new \RuntimeException(
|
||||
'Abuse alert event item is missing during replay'
|
||||
);
|
||||
}
|
||||
self::assertItemReplay($stored, $expected);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array<string,mixed>> $transitions
|
||||
* @return list<array<string,mixed>>
|
||||
*/
|
||||
private static function canonicalTransitions(array $transitions): array
|
||||
{
|
||||
$canonical = [];
|
||||
foreach ($transitions as $transition) {
|
||||
$type = (string) ($transition['transition_type'] ?? '');
|
||||
$from = (string) ($transition['from_severity'] ?? '');
|
||||
$to = (string) ($transition['to_severity'] ?? '');
|
||||
$reason = (string) ($transition['resolution_reason'] ?? '');
|
||||
$validShape = ($type === 'opened' && $from === 'none'
|
||||
&& in_array($to, ['suspicious', 'high'], true)
|
||||
&& $reason === '')
|
||||
|| ($type === 'escalated' && $from === 'suspicious'
|
||||
&& $to === 'high' && $reason === '')
|
||||
|| ($type === 'resolved'
|
||||
&& in_array($from, ['suspicious', 'high'], true)
|
||||
&& $to === 'none' && $reason === 'evidence_clear');
|
||||
$findingKey = (string) ($transition['finding_key'] ?? '');
|
||||
$direction = (string) ($transition['direction'] ?? '');
|
||||
$family = (string) ($transition['ip_family'] ?? '');
|
||||
$ruleCode = (string) ($transition['rule_code'] ?? '');
|
||||
$generation = (int) ($transition['occurrence_generation'] ?? 0);
|
||||
$revision = (int) ($transition['transition_revision'] ?? 0);
|
||||
$occurredAt = (int) ($transition['occurred_at'] ?? 0);
|
||||
if (!$validShape
|
||||
|| preg_match('/^[a-f0-9]{64}$/D', $findingKey) !== 1
|
||||
|| !in_array($direction, ['inbound', 'outbound'], true)
|
||||
|| !in_array($family, ['ipv4', 'ipv6', 'mixed'], true)
|
||||
|| preg_match('/^[a-z][a-z0-9_]{0,47}$/D',
|
||||
$ruleCode) !== 1
|
||||
|| $generation < 1 || $revision < 1 || $occurredAt < 1
|
||||
|| !self::validEvidence($transition['evidence'] ?? null)) {
|
||||
throw new \LogicException(
|
||||
'Abuse alert transition is invalid'
|
||||
);
|
||||
}
|
||||
$canonical[] = [
|
||||
'finding_key' => $findingKey,
|
||||
'occurrence_generation' => $generation,
|
||||
'transition_revision' => $revision,
|
||||
'rule_code' => $ruleCode,
|
||||
'direction' => $direction,
|
||||
'ip_family' => $family,
|
||||
'transition_type' => $type,
|
||||
'from_severity' => $from,
|
||||
'to_severity' => $to,
|
||||
'evidence' => self::canonicalEvidence(
|
||||
$transition['evidence']
|
||||
),
|
||||
'resolution_reason' => $reason,
|
||||
'occurred_at' => $occurredAt,
|
||||
];
|
||||
}
|
||||
usort($canonical, static function (array $left, array $right): int {
|
||||
return [
|
||||
$left['finding_key'],
|
||||
$left['occurrence_generation'],
|
||||
$left['transition_revision'],
|
||||
$left['transition_type'],
|
||||
] <=> [
|
||||
$right['finding_key'],
|
||||
$right['occurrence_generation'],
|
||||
$right['transition_revision'],
|
||||
$right['transition_type'],
|
||||
];
|
||||
});
|
||||
return $canonical;
|
||||
}
|
||||
|
||||
/** @param list<array<string,mixed>> $items */
|
||||
private static function highestSeverity(array $items): string
|
||||
{
|
||||
$highest = 'suspicious';
|
||||
foreach ($items as $item) {
|
||||
if ($item['from_severity'] === 'high'
|
||||
|| $item['to_severity'] === 'high') {
|
||||
$highest = 'high';
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $highest;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $item
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
private static function itemValues(
|
||||
int $eventId,
|
||||
string $eventKey,
|
||||
array $item,
|
||||
int $now
|
||||
): array {
|
||||
$itemKey = hash('sha256', implode("\n", [
|
||||
'cloud-abuse-alert-item-v1',
|
||||
$eventKey,
|
||||
$item['finding_key'],
|
||||
(string) $item['occurrence_generation'],
|
||||
(string) $item['transition_revision'],
|
||||
$item['transition_type'],
|
||||
]));
|
||||
return [
|
||||
'event_id' => $eventId,
|
||||
'item_key' => $itemKey,
|
||||
'finding_key' => $item['finding_key'],
|
||||
'occurrence_generation' => $item['occurrence_generation'],
|
||||
'transition_revision' => $item['transition_revision'],
|
||||
'rule_code' => $item['rule_code'],
|
||||
'direction' => $item['direction'],
|
||||
'ip_family' => $item['ip_family'],
|
||||
'transition_type' => $item['transition_type'],
|
||||
'from_severity' => $item['from_severity'],
|
||||
'to_severity' => $item['to_severity'],
|
||||
'evidence_json' => self::json($item['evidence']),
|
||||
'resolution_reason' => $item['resolution_reason'],
|
||||
'occurred_at' => $item['occurred_at'],
|
||||
'created_at' => $now,
|
||||
];
|
||||
}
|
||||
|
||||
/** @param array<string,mixed> $expected */
|
||||
private static function assertEventReplay(
|
||||
object $stored,
|
||||
array $expected
|
||||
): void {
|
||||
foreach ([
|
||||
'event_key', 'sample_key', 'instance_id', 'user_id', 'host_id',
|
||||
'bucket_start', 'rule_version', 'policy_revision', 'policy_hash',
|
||||
'transition_digest', 'transition_count', 'highest_severity',
|
||||
] as $column) {
|
||||
if ((string) $stored->{$column} !== (string) $expected[$column]) {
|
||||
throw new \RuntimeException(
|
||||
'Abuse alert event conflicts with replay'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** @param array<string,mixed> $expected */
|
||||
private static function assertItemReplay(
|
||||
object $stored,
|
||||
array $expected
|
||||
): void {
|
||||
foreach ([
|
||||
'event_id', 'item_key', 'finding_key', 'occurrence_generation',
|
||||
'transition_revision', 'rule_code', 'direction', 'ip_family',
|
||||
'transition_type', 'from_severity', 'to_severity',
|
||||
'evidence_json', 'resolution_reason', 'occurred_at',
|
||||
] as $column) {
|
||||
if ((string) $stored->{$column} !== (string) $expected[$column]) {
|
||||
throw new \RuntimeException(
|
||||
'Abuse alert event item conflicts with replay'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static function validEvidence(mixed $value): bool
|
||||
{
|
||||
if (!is_array($value)) {
|
||||
return false;
|
||||
}
|
||||
foreach ($value as $key => $item) {
|
||||
if (!is_string($key)
|
||||
|| preg_match('/^[a-z][a-z0-9_]{0,63}$/D', $key) !== 1
|
||||
|| !is_int($item) || $item < 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @param array<string,int> $value @return array<string,int> */
|
||||
private static function canonicalEvidence(array $value): array
|
||||
{
|
||||
ksort($value, SORT_STRING);
|
||||
return $value;
|
||||
}
|
||||
|
||||
private static function json(mixed $value): string
|
||||
{
|
||||
return json_encode(
|
||||
$value,
|
||||
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
|
||||
| JSON_PRESERVE_ZERO_FRACTION | JSON_THROW_ON_ERROR
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -1,354 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cloud;
|
||||
|
||||
/**
|
||||
* Compact, durable trend history for canonical abuse observations.
|
||||
*
|
||||
* Raw observation JSON is intentionally short lived. This module is the one
|
||||
* seam that translates a newly accepted immutable sample into bounded
|
||||
* five-minute and hourly numeric summaries. Callers must invoke it only after
|
||||
* a sample was inserted for the first time; replayed samples are not counted.
|
||||
*/
|
||||
final class CloudAbuseObservationRollupStore
|
||||
{
|
||||
public const FIVE_MINUTES = 300;
|
||||
public const ONE_HOUR = 3600;
|
||||
|
||||
private const QUALITY_BITS = [
|
||||
'clock_regression' => 1 << 0,
|
||||
'cold_start' => 1 << 1,
|
||||
'conntrack_unavailable' => 1 << 2,
|
||||
'counter_reset' => 1 << 3,
|
||||
'flow_limit_reached' => 1 << 4,
|
||||
'incomplete' => 1 << 5,
|
||||
'interface_unavailable' => 1 << 6,
|
||||
'local_port_cardinality_saturated' => 1 << 7,
|
||||
'mapping_changed' => 1 << 8,
|
||||
'overflow' => 1 << 9,
|
||||
'packet_accounting_unavailable' => 1 << 10,
|
||||
'relation_cardinality_saturated' => 1 << 11,
|
||||
'remote_ip_cardinality_saturated' => 1 << 12,
|
||||
'remote_port_cardinality_saturated' => 1 << 13,
|
||||
'sampling_gap' => 1 << 14,
|
||||
'panel_ring_gap' => 1 << 15,
|
||||
];
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $metrics canonical observation metrics
|
||||
* @param list<string> $qualityFlags canonical quality flags
|
||||
*/
|
||||
public function record(
|
||||
object $log,
|
||||
int $instanceId,
|
||||
int $userId,
|
||||
int $hostId,
|
||||
int $bucketStart,
|
||||
bool $complete,
|
||||
array $metrics,
|
||||
array $qualityFlags,
|
||||
int $now
|
||||
): void {
|
||||
if ($instanceId < 1 || $userId < 1 || $hostId < 1
|
||||
|| $bucketStart < 1 || $bucketStart % 60 !== 0 || $now < 1) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Abuse observation rollup identity is invalid'
|
||||
);
|
||||
}
|
||||
$summary = self::summary($metrics, $qualityFlags);
|
||||
foreach ([self::FIVE_MINUTES, self::ONE_HOUR] as $interval) {
|
||||
$this->recordInterval(
|
||||
$log,
|
||||
$instanceId,
|
||||
$userId,
|
||||
$hostId,
|
||||
$bucketStart,
|
||||
$interval,
|
||||
$complete,
|
||||
$summary,
|
||||
$now
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** @param array<string,int|null> $summary */
|
||||
private function recordInterval(
|
||||
object $log,
|
||||
int $instanceId,
|
||||
int $userId,
|
||||
int $hostId,
|
||||
int $sampleStart,
|
||||
int $interval,
|
||||
bool $complete,
|
||||
array $summary,
|
||||
int $now
|
||||
): void {
|
||||
$bucketStart = intdiv($sampleStart, $interval) * $interval;
|
||||
$query = $log->table('cloud_vm_network_observation_rollups')
|
||||
->where('instance_id', $instanceId)
|
||||
->where('interval_seconds', $interval)
|
||||
->where('bucket_start', $bucketStart);
|
||||
$existing = $query->lockForUpdate()->first();
|
||||
if ($existing === null) {
|
||||
$log->table('cloud_vm_network_observation_rollups')->insert([
|
||||
'instance_id' => $instanceId,
|
||||
'user_id' => $userId,
|
||||
'host_id' => $hostId,
|
||||
'interval_seconds' => $interval,
|
||||
'bucket_start' => $bucketStart,
|
||||
'sample_count' => 1,
|
||||
'complete_count' => $complete ? 1 : 0,
|
||||
'quality_mask' => $summary['quality_mask'],
|
||||
'rx_bytes_min' => $summary['rx_bytes'],
|
||||
'rx_bytes_max' => $summary['rx_bytes'],
|
||||
'tx_bytes_min' => $summary['tx_bytes'],
|
||||
'tx_bytes_max' => $summary['tx_bytes'],
|
||||
'rx_packets_min' => $summary['rx_packets'],
|
||||
'rx_packets_max' => $summary['rx_packets'],
|
||||
'tx_packets_min' => $summary['tx_packets'],
|
||||
'tx_packets_max' => $summary['tx_packets'],
|
||||
'tcp_active_peak' => $summary['tcp_active'],
|
||||
'tcp_new_total' => $summary['tcp_new'],
|
||||
'tcp_syn_total' => $summary['tcp_syn'],
|
||||
'tcp_unreplied_total' => $summary['tcp_unreplied'],
|
||||
'udp_active_peak' => $summary['udp_active'],
|
||||
'udp_new_total' => $summary['udp_new'],
|
||||
'peer_ip_cardinality_peak' => $summary['remote_ips'],
|
||||
'peer_port_cardinality_peak' => $summary['remote_ports'],
|
||||
'first_sample_at' => $sampleStart,
|
||||
'last_sample_at' => $sampleStart,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
return;
|
||||
}
|
||||
if ((int) $existing->user_id !== $userId) {
|
||||
throw new \RuntimeException(
|
||||
'Abuse observation rollup owner changed inside one bucket'
|
||||
);
|
||||
}
|
||||
|
||||
$query->update([
|
||||
// Host may legitimately change during maintenance migration.
|
||||
'host_id' => $hostId,
|
||||
'sample_count' => (int) $existing->sample_count + 1,
|
||||
'complete_count' => (int) $existing->complete_count
|
||||
+ ($complete ? 1 : 0),
|
||||
'quality_mask' => ((int) $existing->quality_mask)
|
||||
| $summary['quality_mask'],
|
||||
'rx_bytes_min' => self::minimum(
|
||||
$existing->rx_bytes_min,
|
||||
$summary['rx_bytes']
|
||||
),
|
||||
'rx_bytes_max' => self::maximum(
|
||||
$existing->rx_bytes_max,
|
||||
$summary['rx_bytes']
|
||||
),
|
||||
'tx_bytes_min' => self::minimum(
|
||||
$existing->tx_bytes_min,
|
||||
$summary['tx_bytes']
|
||||
),
|
||||
'tx_bytes_max' => self::maximum(
|
||||
$existing->tx_bytes_max,
|
||||
$summary['tx_bytes']
|
||||
),
|
||||
'rx_packets_min' => self::minimum(
|
||||
$existing->rx_packets_min,
|
||||
$summary['rx_packets']
|
||||
),
|
||||
'rx_packets_max' => self::maximum(
|
||||
$existing->rx_packets_max,
|
||||
$summary['rx_packets']
|
||||
),
|
||||
'tx_packets_min' => self::minimum(
|
||||
$existing->tx_packets_min,
|
||||
$summary['tx_packets']
|
||||
),
|
||||
'tx_packets_max' => self::maximum(
|
||||
$existing->tx_packets_max,
|
||||
$summary['tx_packets']
|
||||
),
|
||||
'tcp_active_peak' => max(
|
||||
(int) $existing->tcp_active_peak,
|
||||
$summary['tcp_active']
|
||||
),
|
||||
'tcp_new_total' => self::add(
|
||||
(int) $existing->tcp_new_total,
|
||||
$summary['tcp_new']
|
||||
),
|
||||
'tcp_syn_total' => self::add(
|
||||
(int) $existing->tcp_syn_total,
|
||||
$summary['tcp_syn']
|
||||
),
|
||||
'tcp_unreplied_total' => self::add(
|
||||
(int) $existing->tcp_unreplied_total,
|
||||
$summary['tcp_unreplied']
|
||||
),
|
||||
'udp_active_peak' => max(
|
||||
(int) $existing->udp_active_peak,
|
||||
$summary['udp_active']
|
||||
),
|
||||
'udp_new_total' => self::add(
|
||||
(int) $existing->udp_new_total,
|
||||
$summary['udp_new']
|
||||
),
|
||||
'peer_ip_cardinality_peak' => max(
|
||||
(int) $existing->peer_ip_cardinality_peak,
|
||||
$summary['remote_ips']
|
||||
),
|
||||
'peer_port_cardinality_peak' => max(
|
||||
(int) $existing->peer_port_cardinality_peak,
|
||||
$summary['remote_ports']
|
||||
),
|
||||
'first_sample_at' => min(
|
||||
(int) $existing->first_sample_at,
|
||||
$sampleStart
|
||||
),
|
||||
'last_sample_at' => max(
|
||||
(int) $existing->last_sample_at,
|
||||
$sampleStart
|
||||
),
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
}
|
||||
|
||||
/** @return array<string,int|null> */
|
||||
private static function summary(
|
||||
array $metrics,
|
||||
array $qualityFlags
|
||||
): array {
|
||||
$interface = is_array($metrics['interface'] ?? null)
|
||||
? $metrics['interface'] : [];
|
||||
$result = [
|
||||
'quality_mask' => self::qualityMask($qualityFlags),
|
||||
'rx_bytes' => self::counter($interface['rx_bytes'] ?? null),
|
||||
'tx_bytes' => self::counter($interface['tx_bytes'] ?? null),
|
||||
'rx_packets' => self::counter($interface['rx_packets'] ?? null),
|
||||
'tx_packets' => self::counter($interface['tx_packets'] ?? null),
|
||||
'tcp_active' => 0,
|
||||
'tcp_new' => 0,
|
||||
'tcp_syn' => 0,
|
||||
'tcp_unreplied' => 0,
|
||||
'udp_active' => 0,
|
||||
'udp_new' => 0,
|
||||
'remote_ips' => 0,
|
||||
'remote_ports' => 0,
|
||||
];
|
||||
foreach (['ipv4', 'ipv6'] as $family) {
|
||||
$families = $metrics[$family] ?? null;
|
||||
if (!is_array($families)) {
|
||||
continue;
|
||||
}
|
||||
foreach (['inbound', 'outbound'] as $direction) {
|
||||
$flow = $families[$direction] ?? null;
|
||||
if (!is_array($flow)) {
|
||||
continue;
|
||||
}
|
||||
$result['tcp_active'] = self::add(
|
||||
$result['tcp_active'],
|
||||
self::requiredCounter($flow, 'tcp_active_flows')
|
||||
);
|
||||
$result['tcp_new'] = self::add(
|
||||
$result['tcp_new'],
|
||||
self::requiredCounter($flow, 'tcp_new_flows')
|
||||
);
|
||||
$result['tcp_syn'] = self::add(
|
||||
$result['tcp_syn'],
|
||||
self::requiredCounter($flow, 'tcp_syn_flows')
|
||||
);
|
||||
$result['tcp_unreplied'] = self::add(
|
||||
$result['tcp_unreplied'],
|
||||
self::requiredCounter($flow, 'tcp_unreplied_flows')
|
||||
);
|
||||
$result['udp_active'] = self::add(
|
||||
$result['udp_active'],
|
||||
self::requiredCounter($flow, 'udp_active_flows')
|
||||
);
|
||||
$result['udp_new'] = self::add(
|
||||
$result['udp_new'],
|
||||
self::requiredCounter($flow, 'udp_new_flows')
|
||||
);
|
||||
$result['remote_ips'] = self::add(
|
||||
$result['remote_ips'],
|
||||
self::requiredCounter($flow, 'unique_remote_ips')
|
||||
);
|
||||
$result['remote_ports'] = self::add(
|
||||
$result['remote_ports'],
|
||||
self::requiredCounter($flow, 'unique_remote_ports')
|
||||
);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/** @param list<string> $flags */
|
||||
private static function qualityMask(array $flags): int
|
||||
{
|
||||
$mask = 0;
|
||||
foreach ($flags as $flag) {
|
||||
if (!is_string($flag) || !isset(self::QUALITY_BITS[$flag])) {
|
||||
throw new \RuntimeException(
|
||||
'Abuse observation rollup quality flag is invalid'
|
||||
);
|
||||
}
|
||||
$mask |= self::QUALITY_BITS[$flag];
|
||||
}
|
||||
return $mask;
|
||||
}
|
||||
|
||||
private static function requiredCounter(array $values, string $key): int
|
||||
{
|
||||
$value = self::counter($values[$key] ?? null);
|
||||
if ($value === null) {
|
||||
throw new \RuntimeException(
|
||||
'Abuse observation rollup flow counter is missing'
|
||||
);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
private static function counter(mixed $value): ?int
|
||||
{
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
if (!is_int($value) || $value < 0) {
|
||||
throw new \RuntimeException(
|
||||
'Abuse observation rollup counter is invalid'
|
||||
);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
private static function minimum(mixed $left, ?int $right): ?int
|
||||
{
|
||||
if ($left === null) {
|
||||
return $right;
|
||||
}
|
||||
if ($right === null) {
|
||||
return (int) $left;
|
||||
}
|
||||
return min((int) $left, $right);
|
||||
}
|
||||
|
||||
private static function maximum(mixed $left, ?int $right): ?int
|
||||
{
|
||||
if ($left === null) {
|
||||
return $right;
|
||||
}
|
||||
if ($right === null) {
|
||||
return (int) $left;
|
||||
}
|
||||
return max((int) $left, $right);
|
||||
}
|
||||
|
||||
private static function add(int $left, int $right): int
|
||||
{
|
||||
if ($right > PHP_INT_MAX - $left) {
|
||||
throw new \OverflowException(
|
||||
'Abuse observation rollup counter overflowed'
|
||||
);
|
||||
}
|
||||
return $left + $right;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@ -1,198 +0,0 @@
|
||||
<?php
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Services\Cloud\CloudAbuseAlertEventCoordinator;
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
|
||||
$assert = static function (bool $condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
|
||||
$capsule = new Capsule();
|
||||
$capsule->addConnection([
|
||||
'driver' => 'sqlite', 'database' => ':memory:',
|
||||
], 'default');
|
||||
$capsule->setAsGlobal();
|
||||
$capsule->bootEloquent();
|
||||
$main = $capsule->connection('default');
|
||||
$main->statement('PRAGMA foreign_keys = ON');
|
||||
$schema = $capsule->schema('default');
|
||||
$schema->create('cloud_instances', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
});
|
||||
$schema->create('cloud_abuse_alert_events',
|
||||
static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->string('event_key')->unique();
|
||||
$table->string('sample_key')->unique();
|
||||
$table->integer('instance_id');
|
||||
$table->integer('user_id');
|
||||
$table->integer('host_id');
|
||||
$table->integer('bucket_start');
|
||||
$table->integer('rule_version');
|
||||
$table->integer('policy_revision');
|
||||
$table->string('policy_hash');
|
||||
$table->string('transition_digest');
|
||||
$table->integer('transition_count');
|
||||
$table->string('highest_severity');
|
||||
$table->integer('created_at');
|
||||
});
|
||||
$schema->create('cloud_abuse_alert_event_items',
|
||||
static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('event_id');
|
||||
$table->string('item_key')->unique();
|
||||
$table->string('finding_key');
|
||||
$table->integer('occurrence_generation');
|
||||
$table->integer('transition_revision');
|
||||
$table->string('rule_code');
|
||||
$table->string('direction');
|
||||
$table->string('ip_family');
|
||||
$table->string('transition_type');
|
||||
$table->string('from_severity');
|
||||
$table->string('to_severity');
|
||||
$table->text('evidence_json');
|
||||
$table->string('resolution_reason')->default('');
|
||||
$table->integer('occurred_at');
|
||||
$table->integer('created_at');
|
||||
$table->foreign('event_id')->references('id')
|
||||
->on('cloud_abuse_alert_events')->cascadeOnDelete();
|
||||
});
|
||||
|
||||
$main->table('cloud_instances')->insert(['id' => 41]);
|
||||
$policyHash = str_repeat('a', 64);
|
||||
$claim = [
|
||||
'sample_key' => str_repeat('b', 64),
|
||||
'instance_id' => 41,
|
||||
'user_id' => 9,
|
||||
'host_id' => 3,
|
||||
'bucket_start' => 120,
|
||||
'rule_version' => 1,
|
||||
'policy_revision' => 7,
|
||||
'policy_hash' => $policyHash,
|
||||
];
|
||||
$transitions = [[
|
||||
'finding_key' => str_repeat('c', 64),
|
||||
'occurrence_generation' => 1,
|
||||
'transition_revision' => 1,
|
||||
'rule_code' => 'outbound_tcp_syn_sweep',
|
||||
'direction' => 'outbound',
|
||||
'ip_family' => 'ipv4',
|
||||
'transition_type' => 'opened',
|
||||
'from_severity' => 'none',
|
||||
'to_severity' => 'suspicious',
|
||||
'evidence' => ['tcp_syn_flows' => 900, 'unique_remote_ips' => 140],
|
||||
'resolution_reason' => '',
|
||||
'occurred_at' => 120,
|
||||
], [
|
||||
'finding_key' => str_repeat('d', 64),
|
||||
'occurrence_generation' => 2,
|
||||
'transition_revision' => 4,
|
||||
'rule_code' => 'inbound_udp_flood',
|
||||
'direction' => 'inbound',
|
||||
'ip_family' => 'ipv6',
|
||||
'transition_type' => 'resolved',
|
||||
'from_severity' => 'high',
|
||||
'to_severity' => 'none',
|
||||
'evidence' => ['udp_active_flows' => 0, 'udp_new_flows' => 0],
|
||||
'resolution_reason' => 'evidence_clear',
|
||||
'occurred_at' => 120,
|
||||
]];
|
||||
|
||||
$coordinator = new CloudAbuseAlertEventCoordinator();
|
||||
$main->transaction(static function () use (
|
||||
$coordinator,
|
||||
$main,
|
||||
$claim,
|
||||
$transitions
|
||||
): void {
|
||||
$coordinator->record($main, $claim, array_reverse($transitions), 1000);
|
||||
});
|
||||
$event = $main->table('cloud_abuse_alert_events')->first();
|
||||
$items = $main->table('cloud_abuse_alert_event_items')
|
||||
->orderBy('finding_key')->get();
|
||||
$assert($event !== null
|
||||
&& (int) $event->transition_count === 2
|
||||
&& $event->highest_severity === 'high'
|
||||
&& count($items) === 2
|
||||
&& $items[0]->finding_key === str_repeat('c', 64)
|
||||
&& $items[0]->evidence_json
|
||||
=== '{"tcp_syn_flows":900,"unique_remote_ips":140}',
|
||||
'Multiple transitions were not frozen in one canonical event batch.');
|
||||
|
||||
$coordinator->record($main, $claim, $transitions, 1001);
|
||||
$assert($main->table('cloud_abuse_alert_events')->count() === 1
|
||||
&& $main->table('cloud_abuse_alert_event_items')->count() === 2,
|
||||
'An identical event replay duplicated the durable ledger.');
|
||||
|
||||
$invalidEvidence = $transitions;
|
||||
$invalidEvidence[0]['evidence'] = ['remote_ip' => '203.0.113.4'];
|
||||
$threw = false;
|
||||
try {
|
||||
$coordinator->record($main, $claim + [
|
||||
'sample_key' => str_repeat('e', 64),
|
||||
], $invalidEvidence, 1002);
|
||||
} catch (LogicException) {
|
||||
$threw = true;
|
||||
}
|
||||
$assert($threw && $main->table('cloud_abuse_alert_events')->count() === 1,
|
||||
'Non-aggregate evidence was accepted by the alert ledger.');
|
||||
|
||||
$wrongBucket = $transitions;
|
||||
$wrongBucket[0]['occurred_at'] = 60;
|
||||
$threw = false;
|
||||
try {
|
||||
$coordinator->record($main, $claim + [
|
||||
'sample_key' => str_repeat('f', 64),
|
||||
], $wrongBucket, 1003);
|
||||
} catch (LogicException) {
|
||||
$threw = true;
|
||||
}
|
||||
$assert($threw, 'A transition from another sample bucket was accepted.');
|
||||
|
||||
$rollbackClaim = $claim;
|
||||
$rollbackClaim['sample_key'] = str_repeat('1', 64);
|
||||
$rolledBack = false;
|
||||
try {
|
||||
$main->transaction(static function () use (
|
||||
$coordinator,
|
||||
$main,
|
||||
$rollbackClaim,
|
||||
$transitions
|
||||
): void {
|
||||
$coordinator->record(
|
||||
$main,
|
||||
$rollbackClaim,
|
||||
$transitions,
|
||||
1004
|
||||
);
|
||||
throw new RuntimeException('force rollback');
|
||||
});
|
||||
} catch (RuntimeException $error) {
|
||||
$rolledBack = $error->getMessage() === 'force rollback';
|
||||
}
|
||||
$assert($rolledBack
|
||||
&& !$main->table('cloud_abuse_alert_events')
|
||||
->where('sample_key', $rollbackClaim['sample_key'])->exists(),
|
||||
'A rolled-back alert event left a partial durable ledger.');
|
||||
|
||||
$main->table('cloud_instances')->where('id', 41)->delete();
|
||||
$assert($main->table('cloud_abuse_alert_events')->count() === 1
|
||||
&& $main->table('cloud_abuse_alert_event_items')->count() === 2,
|
||||
'Deleting an instance erased an alert before delivery finality exists.');
|
||||
$main->table('cloud_abuse_alert_events')->where('id', $event->id)->delete();
|
||||
$assert($main->table('cloud_abuse_alert_event_items')->count() === 0,
|
||||
'Deleting an alert event did not cascade its immutable items.');
|
||||
$source = file_get_contents(dirname(__DIR__)
|
||||
. '/app/Services/Cloud/CloudAbuseAlertEventCoordinator.php');
|
||||
$assert(is_string($source)
|
||||
&& !str_contains($source, 'Telegram')
|
||||
&& !str_contains($source, 'TicketTelegram')
|
||||
&& !str_contains($source, 'Http::'),
|
||||
'Phase-6 event recording gained an external delivery dependency.');
|
||||
|
||||
echo "Cloud abuse alert event coordinator tests passed.\n";
|
||||
@ -1,431 +0,0 @@
|
||||
<?php
|
||||
|
||||
$root = dirname(__DIR__);
|
||||
$read = static function (string $path) use ($root): string {
|
||||
$value = file_get_contents($root . '/' . ltrim($path, '/'));
|
||||
if (!is_string($value)) {
|
||||
throw new RuntimeException('Unable to read ' . $path);
|
||||
}
|
||||
return $value;
|
||||
};
|
||||
$assert = static function (bool $condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
$tableSql = static function (string $schema, string $table) use ($assert): string {
|
||||
$matched = preg_match(
|
||||
'/CREATE TABLE ' . preg_quote($table, '/')
|
||||
. ' \(([\s\S]*?)\n\) ENGINE=InnoDB/',
|
||||
$schema,
|
||||
$match
|
||||
) === 1;
|
||||
$assert($matched, 'Missing abuse-monitoring table: ' . $table);
|
||||
return $match[1];
|
||||
};
|
||||
|
||||
$main = $read('sql/cloud.sql');
|
||||
$log = $read('sql/cloud_log.sql');
|
||||
$setup = $read('app/Command/CloudSetup.php');
|
||||
$traffic = $read('app/Services/Cloud/CloudTrafficService.php');
|
||||
$lifecycle = $read('app/Services/Cloud/CloudLifecycleService.php');
|
||||
$deletion = $read('app/Services/UserDataDeletionService.php');
|
||||
$projection = $read('app/Services/Cloud/CloudAbuseProjectionService.php');
|
||||
|
||||
$mainTables = [
|
||||
'cloud_abuse_instance_states' => [
|
||||
'instance_id', 'user_id', 'host_id', 'rule_version',
|
||||
'policy_revision', 'policy_hash', 'risk_state',
|
||||
'collection_state', 'latest_direction', 'latest_rule_code',
|
||||
'latest_severity', 'summary_json', 'last_projection_key',
|
||||
'last_bucket_start', 'last_sample_at', 'last_evaluated_at',
|
||||
'created_at', 'updated_at',
|
||||
],
|
||||
'cloud_abuse_findings' => [
|
||||
'id', 'finding_key', 'instance_id', 'user_id', 'host_id',
|
||||
'rule_code', 'rule_version', 'policy_revision', 'policy_hash',
|
||||
'direction', 'ip_family', 'severity',
|
||||
'state', 'first_seen_at', 'last_seen_at', 'consecutive_windows',
|
||||
'occurrence_generation', 'transition_revision', 'evidence_json',
|
||||
'resolved_at', 'resolution_reason',
|
||||
'created_at', 'updated_at',
|
||||
],
|
||||
'cloud_abuse_alert_events' => [
|
||||
'id', 'event_key', 'sample_key', 'instance_id', 'user_id',
|
||||
'host_id', 'bucket_start', 'rule_version', 'policy_revision',
|
||||
'policy_hash', 'transition_digest', 'transition_count',
|
||||
'highest_severity', 'created_at',
|
||||
],
|
||||
'cloud_abuse_alert_event_items' => [
|
||||
'id', 'event_id', 'item_key', 'finding_key',
|
||||
'occurrence_generation', 'transition_revision', 'rule_code',
|
||||
'direction', 'ip_family', 'transition_type', 'from_severity',
|
||||
'to_severity', 'evidence_json', 'resolution_reason',
|
||||
'occurred_at', 'created_at',
|
||||
],
|
||||
'cloud_abuse_alert_deliveries' => [
|
||||
'id', 'event_id', 'binding_generation', 'bound_admin_id',
|
||||
'chat_id', 'telegram_message_id', 'state', 'message_text',
|
||||
'content_hash', 'error_code', 'skip_reason',
|
||||
'dispatch_started_at', 'sent_at', 'terminal_at',
|
||||
'destination_redacted_at', 'created_at', 'updated_at',
|
||||
],
|
||||
'administrator_telegram_delivery_gate' => [
|
||||
'id', 'next_send_at', 'updated_at',
|
||||
],
|
||||
'cloud_abuse_projection_receipts' => [
|
||||
'sample_key', 'instance_id', 'rule_version', 'policy_revision',
|
||||
'policy_hash', 'bucket_start', 'projected_at',
|
||||
],
|
||||
];
|
||||
$logTables = [
|
||||
'cloud_vm_network_observation_rollups' => [
|
||||
'instance_id', 'user_id', 'host_id', 'interval_seconds',
|
||||
'bucket_start', 'sample_count', 'complete_count', 'quality_mask',
|
||||
'rx_bytes_min', 'rx_bytes_max', 'tx_bytes_min', 'tx_bytes_max',
|
||||
'rx_packets_min', 'rx_packets_max', 'tx_packets_min',
|
||||
'tx_packets_max', 'tcp_active_peak', 'tcp_new_total',
|
||||
'tcp_syn_total', 'tcp_unreplied_total', 'udp_active_peak',
|
||||
'udp_new_total', 'peer_ip_cardinality_peak',
|
||||
'peer_port_cardinality_peak',
|
||||
'first_sample_at', 'last_sample_at', 'created_at', 'updated_at',
|
||||
],
|
||||
'cloud_vm_network_observation_samples' => [
|
||||
'sample_key', 'instance_id', 'user_id', 'host_id', 'vm_uuid',
|
||||
'placement_key', 'collector_generation', 'window_id',
|
||||
'contract_version', 'bucket_start', 'bucket_seconds', 'complete',
|
||||
'metrics_json',
|
||||
'quality_flags_json', 'received_at',
|
||||
],
|
||||
'cloud_vm_network_projection_outbox' => [
|
||||
'sample_key', 'instance_id', 'bucket_start', 'placement_key',
|
||||
'rule_version', 'policy_revision', 'policy_hash', 'state',
|
||||
'attempts', 'available_at', 'lease_token', 'lease_until',
|
||||
'last_error_code', 'projected_at', 'terminal_at',
|
||||
'discard_reason_code', 'created_at', 'updated_at',
|
||||
],
|
||||
'cloud_vm_network_monitor_cursors' => [
|
||||
'host_id', 'collector_generation', 'cursor_value',
|
||||
'oldest_cursor', 'gap_before', 'poll_revision', 'next_poll_at',
|
||||
'poll_lease_token', 'poll_lease_until', 'collection_state',
|
||||
'consecutive_failures', 'last_window_start',
|
||||
'last_success_at', 'last_error_code', 'updated_at',
|
||||
],
|
||||
'cloud_vm_network_baselines' => [
|
||||
'instance_id', 'user_id', 'rule_version', 'policy_revision',
|
||||
'policy_hash', 'sample_count',
|
||||
'warmup_state', 'metrics_json', 'last_projection_key',
|
||||
'last_bucket_start', 'projection_lease_token',
|
||||
'projection_lease_until', 'revision', 'created_at', 'updated_at',
|
||||
],
|
||||
];
|
||||
|
||||
$definitions = [];
|
||||
foreach ($mainTables as $table => $columns) {
|
||||
$definitions[$table] = $tableSql($main, $table);
|
||||
$assert(str_contains($setup, "'" . $table . "'"),
|
||||
'CloudSetup does not require main abuse table: ' . $table);
|
||||
foreach ($columns as $column) {
|
||||
$assert(preg_match('/\b' . preg_quote($column, '/') . '\b/',
|
||||
$definitions[$table]) === 1,
|
||||
'Missing main abuse column: ' . $table . '.' . $column);
|
||||
$assert(str_contains($setup, "'" . $column . "'"),
|
||||
'CloudSetup does not require main abuse column: '
|
||||
. $table . '.' . $column);
|
||||
}
|
||||
}
|
||||
foreach ($logTables as $table => $columns) {
|
||||
$definitions[$table] = $tableSql($log, $table);
|
||||
$assert(str_contains($setup, "'" . $table . "'"),
|
||||
'CloudSetup does not require log abuse table: ' . $table);
|
||||
foreach ($columns as $column) {
|
||||
$assert(preg_match('/\b' . preg_quote($column, '/') . '\b/',
|
||||
$definitions[$table]) === 1,
|
||||
'Missing log abuse column: ' . $table . '.' . $column);
|
||||
$assert(str_contains($setup, "'" . $column . "'"),
|
||||
'CloudSetup does not require log abuse column: '
|
||||
. $table . '.' . $column);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ([
|
||||
'cloud_abuse_state_risk', 'cloud_abuse_state_collection',
|
||||
'cloud_abuse_state_host', 'cloud_abuse_state_owner',
|
||||
] as $index) {
|
||||
$assert(str_contains($definitions['cloud_abuse_instance_states'], $index)
|
||||
&& str_contains($setup, "'" . $index . "'"),
|
||||
'Missing abuse state index: ' . $index);
|
||||
}
|
||||
foreach ([
|
||||
'cloud_abuse_alert_event_key', 'cloud_abuse_alert_event_sample',
|
||||
'cloud_abuse_alert_event_instance', 'cloud_abuse_alert_event_owner',
|
||||
'cloud_abuse_alert_event_host', 'cloud_abuse_alert_event_retention',
|
||||
] as $index) {
|
||||
$assert(str_contains($definitions['cloud_abuse_alert_events'], $index)
|
||||
&& str_contains($setup, "'" . $index . "'"),
|
||||
'Missing abuse alert event index: ' . $index);
|
||||
}
|
||||
foreach ([
|
||||
'cloud_abuse_alert_item_key', 'cloud_abuse_alert_item_finding_once',
|
||||
'cloud_abuse_alert_item_event', 'cloud_abuse_alert_item_finding',
|
||||
] as $index) {
|
||||
$assert(str_contains(
|
||||
$definitions['cloud_abuse_alert_event_items'], $index
|
||||
) && str_contains($setup, "'" . $index . "'"),
|
||||
'Missing abuse alert item index: ' . $index);
|
||||
}
|
||||
foreach ([
|
||||
'cloud_abuse_alert_delivery_event',
|
||||
'cloud_abuse_alert_delivery_message',
|
||||
'cloud_abuse_alert_delivery_due',
|
||||
'cloud_abuse_alert_delivery_admin',
|
||||
'cloud_abuse_alert_delivery_terminal',
|
||||
] as $index) {
|
||||
$assert(str_contains(
|
||||
$definitions['cloud_abuse_alert_deliveries'], $index
|
||||
) && str_contains($setup, "'" . $index . "'"),
|
||||
'Missing abuse alert delivery index: ' . $index);
|
||||
}
|
||||
foreach ([
|
||||
'cloud_abuse_finding_key', 'cloud_abuse_finding_instance',
|
||||
'cloud_abuse_finding_owner', 'cloud_abuse_finding_host',
|
||||
'cloud_abuse_finding_rule', 'cloud_abuse_finding_retention',
|
||||
] as $index) {
|
||||
$assert(str_contains($definitions['cloud_abuse_findings'], $index)
|
||||
&& str_contains($setup, "'" . $index . "'"),
|
||||
'Missing abuse finding index: ' . $index);
|
||||
}
|
||||
foreach ([
|
||||
'cloud_network_rollup_owner',
|
||||
'cloud_network_rollup_host',
|
||||
'cloud_network_rollup_retention',
|
||||
] as $index) {
|
||||
$assert(str_contains(
|
||||
$definitions['cloud_vm_network_observation_rollups'], $index
|
||||
) && str_contains($setup, "'" . $index . "'"),
|
||||
'Missing observation rollup index: ' . $index);
|
||||
}
|
||||
foreach ([
|
||||
'cloud_network_observation_source',
|
||||
'cloud_network_observation_instance',
|
||||
'cloud_network_observation_retention',
|
||||
] as $index) {
|
||||
$assert(str_contains(
|
||||
$definitions['cloud_vm_network_observation_samples'], $index
|
||||
) && str_contains($setup, "'" . $index . "'"),
|
||||
'Missing observation index: ' . $index);
|
||||
}
|
||||
$assert(!str_contains(
|
||||
$definitions['cloud_vm_network_observation_samples'],
|
||||
'cloud_network_observation_placement'
|
||||
) && !str_contains(
|
||||
$definitions['cloud_vm_network_observation_samples'],
|
||||
'cloud_network_observation_host'
|
||||
),
|
||||
'Observation storage retained redundant wide placement/host indexes.');
|
||||
foreach ([
|
||||
'cloud_network_projection_due',
|
||||
'cloud_network_projection_instance',
|
||||
'cloud_network_projection_lease',
|
||||
'cloud_network_projection_retention',
|
||||
] as $index) {
|
||||
$assert(str_contains(
|
||||
$definitions['cloud_vm_network_projection_outbox'], $index
|
||||
) && str_contains($setup, "'" . $index . "'"),
|
||||
'Missing projection index: ' . $index);
|
||||
}
|
||||
foreach ([
|
||||
'cloud_network_monitor_cursor_health',
|
||||
'cloud_network_monitor_cursor_due',
|
||||
'cloud_network_monitor_cursor_lease',
|
||||
] as $index) {
|
||||
$assert(str_contains(
|
||||
$definitions['cloud_vm_network_monitor_cursors'], $index
|
||||
) && str_contains($setup, "'" . $index . "'"),
|
||||
'Missing cursor index: ' . $index);
|
||||
}
|
||||
foreach ([
|
||||
'cloud_network_baseline_owner',
|
||||
'cloud_network_baseline_warmup',
|
||||
'cloud_network_baseline_projection_lease',
|
||||
] as $index) {
|
||||
$assert(str_contains(
|
||||
$definitions['cloud_vm_network_baselines'], $index
|
||||
) && str_contains($setup, "'" . $index . "'"),
|
||||
'Missing baseline index: ' . $index);
|
||||
}
|
||||
foreach ([
|
||||
'chk_cloud_network_rollup_bucket',
|
||||
'chk_cloud_network_rollup_counts',
|
||||
'chk_cloud_network_rollup_counters',
|
||||
] as $check) {
|
||||
$assert(str_contains($log, 'CONSTRAINT ' . $check . ' CHECK')
|
||||
&& str_contains($setup, "'" . $check . "'"),
|
||||
'CloudSetup does not require rollup CHECK: ' . $check);
|
||||
}
|
||||
foreach ([
|
||||
'chk_cloud_network_observation_contract',
|
||||
'chk_cloud_network_observation_bucket',
|
||||
'chk_cloud_network_observation_complete',
|
||||
'chk_cloud_network_projection_key',
|
||||
'chk_cloud_network_projection_policy',
|
||||
'chk_cloud_network_projection_state',
|
||||
'chk_cloud_network_projection_lease',
|
||||
'chk_cloud_network_monitor_cursor_state',
|
||||
'chk_cloud_network_monitor_cursor_generation',
|
||||
'chk_cloud_network_monitor_cursor_lease',
|
||||
'chk_cloud_network_baseline_warmup',
|
||||
'chk_cloud_network_baseline_policy',
|
||||
'chk_cloud_network_baseline_projection',
|
||||
'chk_cloud_network_baseline_projection_lease',
|
||||
] as $check) {
|
||||
$assert(str_contains($log, 'CONSTRAINT ' . $check . ' CHECK')
|
||||
&& str_contains($setup, "'" . $check . "'"),
|
||||
'CloudSetup does not require log CHECK: ' . $check);
|
||||
}
|
||||
|
||||
$assert(str_contains($definitions['cloud_abuse_instance_states'],
|
||||
"risk_state IN ('observing','normal','suspicious','high_risk')")
|
||||
&& str_contains($definitions['cloud_abuse_instance_states'],
|
||||
"'unknown','healthy','degraded','stale','unavailable'")
|
||||
&& str_contains($definitions['cloud_abuse_findings'],
|
||||
"direction IN ('inbound','outbound')")
|
||||
&& str_contains($definitions['cloud_abuse_findings'],
|
||||
"ip_family IN ('ipv4','ipv6','mixed')")
|
||||
&& str_contains($definitions['cloud_abuse_findings'],
|
||||
"state IN ('open','resolved')")
|
||||
&& str_contains($definitions['cloud_abuse_instance_states'],
|
||||
'chk_cloud_abuse_state_policy CHECK')
|
||||
&& str_contains($definitions['cloud_abuse_findings'],
|
||||
'chk_cloud_abuse_finding_policy CHECK')
|
||||
&& str_contains($definitions['cloud_abuse_projection_receipts'],
|
||||
'chk_cloud_abuse_projection_policy CHECK')
|
||||
&& str_contains($definitions['cloud_abuse_findings'],
|
||||
"resolution_reason IN (\n 'evidence_clear','policy_replaced','owner_changed'"),
|
||||
'Abuse-monitoring state constraints are incomplete.');
|
||||
$assert(str_contains($definitions['cloud_abuse_alert_events'],
|
||||
'chk_cloud_abuse_alert_event_identity CHECK')
|
||||
&& str_contains($definitions['cloud_abuse_alert_events'],
|
||||
"highest_severity IN ('suspicious','high')")
|
||||
&& str_contains($definitions['cloud_abuse_alert_event_items'],
|
||||
'chk_cloud_abuse_alert_item_transition CHECK')
|
||||
&& str_contains($definitions['cloud_abuse_alert_event_items'],
|
||||
"transition_type = 'opened'")
|
||||
&& str_contains($definitions['cloud_abuse_alert_event_items'],
|
||||
"transition_type = 'escalated'")
|
||||
&& str_contains($definitions['cloud_abuse_alert_event_items'],
|
||||
"transition_type = 'resolved'"),
|
||||
'Abuse alert event transition constraints are incomplete.');
|
||||
$delivery = $definitions['cloud_abuse_alert_deliveries'];
|
||||
$assert(str_contains($delivery,
|
||||
'chk_cloud_abuse_alert_delivery_destination CHECK')
|
||||
&& str_contains($delivery, 'binding_generation IS NOT NULL')
|
||||
&& str_contains($delivery, 'chat_id IS NOT NULL')
|
||||
&& str_contains($delivery, 'OR telegram_message_id > 0')
|
||||
&& str_contains($delivery,
|
||||
"state = 'failed'\n AND telegram_message_id IS NULL\n AND dispatch_started_at IS NULL")
|
||||
&& str_contains($delivery,
|
||||
"state = 'unknown'\n AND dispatch_started_at IS NOT NULL")
|
||||
&& str_contains($definitions['administrator_telegram_delivery_gate'],
|
||||
'chk_administrator_telegram_delivery_gate CHECK')
|
||||
&& str_contains($setup,
|
||||
"'chk_cloud_abuse_alert_delivery_destination'")
|
||||
&& str_contains($setup,
|
||||
"'chk_administrator_telegram_delivery_gate'"),
|
||||
'Abuse delivery or shared Telegram gate constraints are incomplete.');
|
||||
|
||||
$assert(str_contains($definitions['cloud_abuse_instance_states'],
|
||||
'fk_cloud_abuse_state_instance FOREIGN KEY (instance_id)')
|
||||
&& str_contains($definitions['cloud_abuse_instance_states'],
|
||||
'REFERENCES cloud_instances (id) ON DELETE CASCADE')
|
||||
&& str_contains($definitions['cloud_abuse_findings'],
|
||||
'fk_cloud_abuse_finding_instance FOREIGN KEY (instance_id)')
|
||||
&& str_contains($definitions['cloud_abuse_findings'],
|
||||
'REFERENCES cloud_instances (id) ON DELETE CASCADE')
|
||||
&& str_contains($definitions['cloud_abuse_projection_receipts'],
|
||||
'fk_cloud_abuse_projection_instance FOREIGN KEY (instance_id)')
|
||||
&& str_contains($definitions['cloud_abuse_projection_receipts'],
|
||||
'REFERENCES cloud_instances (id) ON DELETE CASCADE')
|
||||
&& str_contains($setup, "'fk_cloud_abuse_state_instance'")
|
||||
&& str_contains($setup, "'fk_cloud_abuse_finding_instance'")
|
||||
&& str_contains($setup, "'fk_cloud_abuse_projection_instance'"),
|
||||
'Current state or finding rows can outlive a deleted instance.');
|
||||
$assert(!str_contains($definitions['cloud_abuse_alert_events'],
|
||||
'REFERENCES cloud_instances')
|
||||
&& str_contains($definitions['cloud_abuse_alert_event_items'],
|
||||
'fk_cloud_abuse_alert_item_event FOREIGN KEY (event_id)')
|
||||
&& str_contains($definitions['cloud_abuse_alert_event_items'],
|
||||
'ON DELETE CASCADE ON UPDATE RESTRICT'),
|
||||
'Alert events are not independent or item cleanup is not bounded.');
|
||||
$assert(str_contains($delivery,
|
||||
'fk_cloud_abuse_alert_delivery_event FOREIGN KEY (event_id)')
|
||||
&& str_contains($delivery,
|
||||
'ON DELETE CASCADE ON UPDATE RESTRICT')
|
||||
&& str_contains($setup,
|
||||
"'fk_cloud_abuse_alert_delivery_event'"),
|
||||
'Abuse delivery does not cascade only from its immutable event.');
|
||||
|
||||
$outboxDelete = strpos(
|
||||
$deletion, "'cloud_vm_network_projection_outbox'"
|
||||
);
|
||||
$sampleDelete = strrpos(
|
||||
$deletion, "'cloud_vm_network_observation_samples'"
|
||||
);
|
||||
$assert($outboxDelete !== false && $sampleDelete !== false
|
||||
&& $outboxDelete < $sampleDelete
|
||||
&& str_contains($deletion, "'cloud_vm_network_baselines'")
|
||||
&& str_contains($deletion, "'cloud_vm_network_observation_rollups'"),
|
||||
'Account deletion leaves tenant observation history in the log database.');
|
||||
$assert(str_contains($deletion,
|
||||
"['cloud_abuse_alert_events', 'user_id']")
|
||||
&& str_contains($deletion,
|
||||
"['cloud_abuse_findings', 'user_id']")
|
||||
&& str_contains($deletion,
|
||||
"['cloud_abuse_instance_states', 'user_id']"),
|
||||
'Account deletion does not erase frozen old-owner abuse data.');
|
||||
$ownerLock = strpos($projection, "->table('user')");
|
||||
$instanceLock = strpos($projection, "->table('cloud_instances')");
|
||||
$assert($ownerLock !== false && $instanceLock !== false
|
||||
&& $ownerLock < $instanceLock,
|
||||
'Projection does not share the account deletion user-before-instance lock order.');
|
||||
|
||||
$assert(str_contains(
|
||||
$definitions['cloud_vm_network_projection_outbox'],
|
||||
'ON DELETE RESTRICT ON UPDATE RESTRICT'
|
||||
) && !str_contains(
|
||||
$definitions['cloud_vm_network_projection_outbox'],
|
||||
'ON DELETE CASCADE'
|
||||
) && str_contains($setup, "'delete_rule' => 'RESTRICT'")
|
||||
&& str_contains($setup, "'update_rule' => 'RESTRICT'"),
|
||||
'Pending or leased projection samples are not deletion-protected.');
|
||||
|
||||
$assert(str_contains(
|
||||
$definitions['cloud_vm_network_observation_samples'],
|
||||
'contract_version = 1'
|
||||
) && str_contains(
|
||||
$definitions['cloud_vm_network_observation_samples'],
|
||||
'bucket_seconds = 60'
|
||||
) && str_contains(
|
||||
$definitions['cloud_vm_network_monitor_cursors'],
|
||||
"'unknown','healthy','degraded','stale','unavailable'"
|
||||
) && str_contains(
|
||||
$definitions['cloud_vm_network_baselines'],
|
||||
"warmup_state IN ('warming','ready')"
|
||||
), 'Observation quality or baseline constraints are incomplete.');
|
||||
|
||||
$assert(str_contains(
|
||||
$setup,
|
||||
"SCHEMA_VERSION = 'go-agent-wss-v56-daily-traffic-settlement'"
|
||||
), 'CloudSetup schema version was not advanced for abuse observation.');
|
||||
|
||||
foreach (array_keys($mainTables + $logTables) as $table) {
|
||||
$assert(!str_contains($traffic, $table)
|
||||
&& !str_contains($lifecycle, $table),
|
||||
'Abuse monitoring leaked into billing/lifecycle sampling: ' . $table);
|
||||
}
|
||||
$forbiddenIdentityColumn = '/^\s*`?remote_(?:ip|port)[a-z0-9_]*`?\s+/mi';
|
||||
$assert(preg_match($forbiddenIdentityColumn, $main) !== 1
|
||||
&& preg_match($forbiddenIdentityColumn, $log) !== 1,
|
||||
'Schema stores forbidden remote flow identities.');
|
||||
|
||||
echo "Abuse monitoring schema contract passed.\n";
|
||||
@ -1,366 +0,0 @@
|
||||
<?php
|
||||
|
||||
$root = dirname(__DIR__);
|
||||
$read = static function (string $path) use ($root): string {
|
||||
$value = file_get_contents($root . '/' . ltrim($path, '/'));
|
||||
if (!is_string($value)) {
|
||||
throw new RuntimeException('Unable to read ' . $path);
|
||||
}
|
||||
return $value;
|
||||
};
|
||||
$assert = static function (bool $condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
$table = static function (string $schema, string $name) use ($assert): string {
|
||||
$matched = preg_match(
|
||||
'/CREATE TABLE ' . preg_quote($name, '/')
|
||||
. ' \(([\s\S]*?)\n\) ENGINE=InnoDB/',
|
||||
$schema,
|
||||
$match
|
||||
) === 1;
|
||||
$assert($matched, 'Missing projection-ledger table: ' . $name);
|
||||
return $match[1];
|
||||
};
|
||||
|
||||
$main = $read('sql/cloud.sql');
|
||||
$log = $read('sql/cloud_log.sql');
|
||||
$setup = $read('app/Command/CloudSetup.php');
|
||||
$deletion = $read('app/Services/UserDataDeletionService.php');
|
||||
$projection = $read('app/Services/Cloud/CloudAbuseProjectionService.php');
|
||||
$deployment = $read('../docs/DEPLOYMENT.md');
|
||||
$compactSetup = preg_replace('/\s+/', '', $setup);
|
||||
$assert(is_string($compactSetup), 'Unable to normalize CloudSetup source.');
|
||||
$logShapeOffset = strpos($compactSetup, 'REQUIRED_LOG_INDEX_SHAPES');
|
||||
$assert($logShapeOffset !== false, 'Missing CloudSetup log index shapes.');
|
||||
$logShapeSetup = substr($compactSetup, $logShapeOffset);
|
||||
|
||||
$sample = $table($log, 'cloud_vm_network_observation_samples');
|
||||
$outbox = $table($log, 'cloud_vm_network_projection_outbox');
|
||||
$cursor = $table($log, 'cloud_vm_network_monitor_cursors');
|
||||
$baseline = $table($log, 'cloud_vm_network_baselines');
|
||||
$state = $table($main, 'cloud_abuse_instance_states');
|
||||
$finding = $table($main, 'cloud_abuse_findings');
|
||||
$receipt = $table($main, 'cloud_abuse_projection_receipts');
|
||||
|
||||
$assert(str_contains($sample,
|
||||
'collector_generation CHAR(32) CHARACTER SET ascii COLLATE ascii_bin')
|
||||
&& str_contains($sample,
|
||||
'window_id CHAR(32) CHARACTER SET ascii COLLATE ascii_bin')
|
||||
&& str_contains($sample,
|
||||
"collector_generation REGEXP '^[a-f0-9]{32}$'")
|
||||
&& str_contains($sample, "window_id REGEXP '^[a-f0-9]{32}$'")
|
||||
&& str_contains($sample, "sample_key REGEXP '^[a-f0-9]{64}$'")
|
||||
&& str_contains($sample, "placement_key REGEXP '^[a-f0-9]{64}$'")
|
||||
&& str_contains($sample,
|
||||
"'^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$'")
|
||||
&& str_contains($sample, 'bucket_seconds = 60')
|
||||
&& str_contains($sample, 'bucket_start % 60 = 0'),
|
||||
'Agent generation, window, placement or minute-bucket identity is loose.');
|
||||
|
||||
$assert(str_contains($sample, 'metrics_json JSON NOT NULL')
|
||||
&& !str_contains($sample, 'payload_json')
|
||||
&& !str_contains($sample, 'raw_agent')
|
||||
&& str_contains($sample, 'quality_flags_json JSON NOT NULL'),
|
||||
'Observation storage can retain a raw Agent body instead of allowlisted metrics.');
|
||||
|
||||
$assert(str_contains($sample,
|
||||
'placement_key CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
|
||||
&& str_contains($sample,
|
||||
'cloud_network_observation_instance')
|
||||
&& !str_contains($sample, 'cloud_network_observation_placement')
|
||||
&& !str_contains($sample, 'cloud_network_observation_host'),
|
||||
'Observation sample lost placement evidence or retained redundant indexes.');
|
||||
|
||||
$assert(str_contains($outbox, 'PRIMARY KEY (sample_key)')
|
||||
&& str_contains($outbox, 'instance_id BIGINT UNSIGNED NOT NULL')
|
||||
&& str_contains($outbox, 'bucket_start BIGINT UNSIGNED NOT NULL')
|
||||
&& str_contains($outbox,
|
||||
'placement_key CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
|
||||
&& str_contains($outbox, 'rule_version INT UNSIGNED NOT NULL')
|
||||
&& str_contains($outbox, 'policy_revision BIGINT UNSIGNED NOT NULL')
|
||||
&& str_contains($outbox,
|
||||
'policy_hash CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
|
||||
&& str_contains($outbox,
|
||||
'chk_cloud_network_projection_policy CHECK')
|
||||
&& str_contains($outbox,
|
||||
"state IN ('pending','leased','done','discarded')")
|
||||
&& preg_match(
|
||||
'/cloud_network_projection_due \(\s*state, available_at, bucket_start, sample_key\s*\)/',
|
||||
$outbox
|
||||
) === 1
|
||||
&& preg_match(
|
||||
'/cloud_network_projection_instance \(\s*instance_id, state, bucket_start, sample_key\s*\)/',
|
||||
$outbox
|
||||
) === 1
|
||||
&& str_contains($outbox,
|
||||
'cloud_network_projection_lease (state, lease_until, sample_key)')
|
||||
&& str_contains($outbox,
|
||||
'cloud_network_projection_retention (state, terminal_at, sample_key)')
|
||||
&& str_contains($outbox, 'lease_token IS NOT NULL')
|
||||
&& str_contains($outbox,
|
||||
"lease_token REGEXP '^[a-f0-9]{32}$'")
|
||||
&& str_contains($outbox,
|
||||
"state = 'discarded' AND lease_token IS NULL")
|
||||
&& str_contains($outbox, 'projected_at IS NULL AND terminal_at IS NOT NULL')
|
||||
&& str_contains($outbox,
|
||||
"discard_reason_code REGEXP '^[a-z0-9_]{3,64}$'")
|
||||
&& str_contains($outbox,
|
||||
'fk_cloud_network_projection_sample FOREIGN KEY (sample_key)')
|
||||
&& str_contains($outbox,
|
||||
'ON DELETE RESTRICT ON UPDATE RESTRICT')
|
||||
&& !str_contains($outbox, 'ON DELETE CASCADE'),
|
||||
'Log projection outbox is not durable, ordered or RESTRICT-bound.');
|
||||
|
||||
$assert(str_contains($cursor,
|
||||
'collector_generation CHAR(32) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL')
|
||||
&& str_contains($cursor,
|
||||
'cursor_value BIGINT UNSIGNED NOT NULL DEFAULT 0')
|
||||
&& str_contains($cursor,
|
||||
'oldest_cursor BIGINT UNSIGNED NOT NULL DEFAULT 0')
|
||||
&& str_contains($cursor, 'gap_before TINYINT(1) NOT NULL DEFAULT 0')
|
||||
&& str_contains($cursor, 'poll_revision BIGINT UNSIGNED NOT NULL DEFAULT 0')
|
||||
&& str_contains($cursor, 'next_poll_at BIGINT UNSIGNED NOT NULL DEFAULT 0')
|
||||
&& str_contains($cursor,
|
||||
'poll_lease_token CHAR(32) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL')
|
||||
&& str_contains($cursor,
|
||||
'poll_lease_until BIGINT UNSIGNED NOT NULL DEFAULT 0')
|
||||
&& str_contains($cursor,
|
||||
'collector_generation IS NULL AND cursor_value = 0')
|
||||
&& str_contains($cursor,
|
||||
"oldest_cursor = 0 AND gap_before = 0\n"
|
||||
. " AND collection_state = 'unknown' AND consecutive_failures = 0")
|
||||
&& str_contains($cursor,
|
||||
'last_window_start IS NULL AND last_success_at IS NULL')
|
||||
&& str_contains($cursor,
|
||||
"collector_generation REGEXP '^[a-f0-9]{32}$'")
|
||||
&& str_contains($cursor,
|
||||
'oldest_cursor = 0 AND cursor_value = 0 AND gap_before = 0')
|
||||
&& str_contains($cursor,
|
||||
'oldest_cursor > 0 AND oldest_cursor <= cursor_value')
|
||||
&& str_contains($cursor,
|
||||
"poll_lease_token REGEXP '^[a-f0-9]{32}$'")
|
||||
&& preg_match(
|
||||
'/cloud_network_monitor_cursor_due \(\s*next_poll_at, poll_revision, host_id\s*\)/',
|
||||
$cursor
|
||||
) === 1
|
||||
&& preg_match(
|
||||
'/cloud_network_monitor_cursor_lease \(\s*poll_lease_until, host_id\s*\)/',
|
||||
$cursor
|
||||
) === 1,
|
||||
'Host cursor lacks strict initial, CAS, due or lease semantics.');
|
||||
|
||||
$assert(str_contains($baseline, 'last_projection_key CHAR(64)')
|
||||
&& str_contains($baseline, 'rule_version INT UNSIGNED NOT NULL')
|
||||
&& str_contains($baseline, 'policy_revision BIGINT UNSIGNED NOT NULL')
|
||||
&& str_contains($baseline,
|
||||
'policy_hash CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
|
||||
&& str_contains($baseline,
|
||||
'chk_cloud_network_baseline_policy CHECK')
|
||||
&& str_contains($baseline, 'last_bucket_start BIGINT UNSIGNED')
|
||||
&& str_contains($baseline,
|
||||
'last_projection_key IS NULL AND last_bucket_start IS NULL')
|
||||
&& str_contains($baseline,
|
||||
"last_projection_key REGEXP '^[a-f0-9]{64}$'")
|
||||
&& str_contains($baseline, 'last_bucket_start % 60 = 0')
|
||||
&& str_contains($baseline,
|
||||
'projection_lease_token CHAR(32) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL')
|
||||
&& str_contains($baseline,
|
||||
'projection_lease_until BIGINT UNSIGNED NOT NULL DEFAULT 0')
|
||||
&& str_contains($baseline, 'revision BIGINT UNSIGNED NOT NULL DEFAULT 0')
|
||||
&& str_contains($baseline,
|
||||
"projection_lease_token REGEXP '^[a-f0-9]{32}$'")
|
||||
&& preg_match(
|
||||
'/cloud_network_baseline_projection_lease \(\s*projection_lease_until, instance_id\s*\)/',
|
||||
$baseline
|
||||
) === 1,
|
||||
'Baseline lacks a paired projection fence or recoverable instance lease.');
|
||||
|
||||
$assert(str_contains($receipt,
|
||||
'sample_key CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL PRIMARY KEY')
|
||||
&& str_contains($receipt, 'rule_version INT UNSIGNED NOT NULL')
|
||||
&& str_contains($receipt, 'policy_revision BIGINT UNSIGNED NOT NULL')
|
||||
&& str_contains($receipt,
|
||||
'policy_hash CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
|
||||
&& str_contains($receipt, 'chk_cloud_abuse_projection_policy CHECK')
|
||||
&& str_contains($receipt,
|
||||
'cloud_abuse_projection_instance')
|
||||
&& str_contains($receipt,
|
||||
'cloud_abuse_projection_retention (projected_at, sample_key)')
|
||||
&& str_contains($receipt,
|
||||
'fk_cloud_abuse_projection_instance FOREIGN KEY (instance_id)')
|
||||
&& str_contains($receipt,
|
||||
'ON DELETE CASCADE ON UPDATE RESTRICT'),
|
||||
'Main projection receipt is not idempotent, retained or owner-bound.');
|
||||
|
||||
$assert(str_contains($state, 'last_projection_key CHAR(64)')
|
||||
&& str_contains($state, 'rule_version INT UNSIGNED NOT NULL')
|
||||
&& str_contains($state, 'policy_revision BIGINT UNSIGNED NOT NULL')
|
||||
&& str_contains($state,
|
||||
'policy_hash CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
|
||||
&& str_contains($state, 'chk_cloud_abuse_state_policy CHECK')
|
||||
&& str_contains($state, 'last_bucket_start BIGINT UNSIGNED')
|
||||
&& str_contains($state,
|
||||
'last_projection_key IS NOT NULL')
|
||||
&& str_contains($state,
|
||||
"last_projection_key REGEXP '^[a-f0-9]{64}$'")
|
||||
&& str_contains($state,
|
||||
'last_projection_key IS NULL AND last_bucket_start IS NULL'),
|
||||
'Current state lacks the projection ordering fence.');
|
||||
|
||||
$assert(str_contains($finding,
|
||||
'cloud_abuse_finding_retention (')
|
||||
&& str_contains($finding, 'policy_revision BIGINT UNSIGNED NOT NULL')
|
||||
&& str_contains($finding,
|
||||
'policy_hash CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
|
||||
&& str_contains($finding, 'chk_cloud_abuse_finding_policy CHECK')
|
||||
&& str_contains($finding,
|
||||
"resolution_reason IN (\n 'evidence_clear','policy_replaced','owner_changed'")
|
||||
&& str_contains($finding,
|
||||
'state, resolved_at, last_seen_at, id')
|
||||
&& str_contains($finding,
|
||||
"finding_key REGEXP '^[a-f0-9]{64}$'")
|
||||
&& str_contains($finding,
|
||||
"state = 'resolved' AND resolved_at IS NOT NULL")
|
||||
&& str_contains($finding,
|
||||
'resolved_at >= last_seen_at'),
|
||||
'Finding retention or terminal-time contract is incomplete.');
|
||||
|
||||
$findingIdentityStart = strpos(
|
||||
$projection,
|
||||
'$key = hash(\'sha256\', implode("\\n", ['
|
||||
);
|
||||
$findingIdentityEnd = $findingIdentityStart === false
|
||||
? false
|
||||
: strpos($projection, ']));', $findingIdentityStart);
|
||||
$findingIdentity = $findingIdentityStart === false
|
||||
|| $findingIdentityEnd === false
|
||||
? ''
|
||||
: substr(
|
||||
$projection,
|
||||
$findingIdentityStart,
|
||||
$findingIdentityEnd - $findingIdentityStart
|
||||
);
|
||||
$lastIdentityOffset = -1;
|
||||
foreach ([
|
||||
'$claim[\'instance_id\']',
|
||||
'$claim[\'rule_version\']',
|
||||
'$claim[\'policy_hash\']',
|
||||
'$finding[\'rule_code\']',
|
||||
'$finding[\'ip_family\']',
|
||||
'$finding[\'direction\']',
|
||||
] as $identityPart) {
|
||||
$identityOffset = strpos($findingIdentity, $identityPart);
|
||||
$assert($identityOffset !== false && $identityOffset > $lastIdentityOffset,
|
||||
'Finding identity does not include ordered part: ' . $identityPart);
|
||||
$lastIdentityOffset = $identityOffset;
|
||||
}
|
||||
$assert(str_contains($projection, '->snapshotByIdentity($revision, $hash)')
|
||||
&& !str_contains($projection, '->activeSnapshot()'),
|
||||
'Projection can drift from the outbox identity to the current policy.');
|
||||
$assert(str_contains($deployment,
|
||||
'`go-agent-wss-v56-daily-traffic-settlement`')
|
||||
&& str_contains($deployment,
|
||||
'`occurrence_generation = 1` 与 `transition_revision = 1`')
|
||||
&& str_contains($deployment,
|
||||
'历史 finding 不反向合成事件')
|
||||
&& str_contains($deployment,
|
||||
'`policy_replaced` 与 `owner_changed` 只关闭 finding'),
|
||||
'Deployment contract does not define the v45 event-ledger cutover.');
|
||||
|
||||
foreach ([
|
||||
'cloud_vm_network_projection_outbox',
|
||||
'cloud_abuse_projection_receipts',
|
||||
] as $requiredTable) {
|
||||
$assert(str_contains($setup, "'" . $requiredTable . "'"),
|
||||
'CloudSetup does not require projection table: ' . $requiredTable);
|
||||
}
|
||||
foreach ([
|
||||
'cloud_abuse_projection_retention',
|
||||
'fk_cloud_abuse_projection_instance',
|
||||
'chk_cloud_abuse_state_projection',
|
||||
'chk_cloud_abuse_state_policy',
|
||||
'chk_cloud_abuse_finding_key',
|
||||
'chk_cloud_abuse_finding_policy',
|
||||
'chk_cloud_abuse_projection_policy',
|
||||
'chk_cloud_network_projection_lease',
|
||||
'chk_cloud_network_projection_policy',
|
||||
'chk_cloud_network_monitor_cursor_generation',
|
||||
'chk_cloud_network_baseline_projection_lease',
|
||||
'chk_cloud_network_baseline_policy',
|
||||
'cloud_network_projection_due',
|
||||
'cloud_network_projection_instance',
|
||||
'cloud_network_projection_retention',
|
||||
'cloud_network_monitor_cursor_due',
|
||||
'cloud_network_monitor_cursor_lease',
|
||||
'cloud_network_baseline_projection_lease',
|
||||
'fk_cloud_network_projection_sample',
|
||||
] as $requiredObject) {
|
||||
$assert(str_contains($setup, "'" . $requiredObject . "'"),
|
||||
'CloudSetup does not verify projection object: ' . $requiredObject);
|
||||
}
|
||||
|
||||
foreach ([
|
||||
"'cloud_network_observation_source'=>['non_unique'=>0,'columns'=>['host_id','collector_generation','window_id','vm_uuid',]",
|
||||
"'cloud_network_observation_instance'=>['non_unique'=>1,'columns'=>['instance_id','bucket_start'],]",
|
||||
"'cloud_network_observation_retention'=>['non_unique'=>1,'columns'=>['bucket_start'],]",
|
||||
"'cloud_network_projection_due'=>['non_unique'=>1,'columns'=>['state','available_at','bucket_start','sample_key',]",
|
||||
"'cloud_network_projection_instance'=>['non_unique'=>1,'columns'=>['instance_id','state','bucket_start','sample_key',]",
|
||||
"'cloud_network_projection_lease'=>['non_unique'=>1,'columns'=>['state','lease_until','sample_key'],]",
|
||||
"'cloud_network_projection_retention'=>['non_unique'=>1,'columns'=>['state','terminal_at','sample_key'],]",
|
||||
"'cloud_network_monitor_cursor_health'=>['non_unique'=>1,'columns'=>['collection_state','last_success_at','host_id',]",
|
||||
"'cloud_network_monitor_cursor_due'=>['non_unique'=>1,'columns'=>['next_poll_at','poll_revision','host_id'],]",
|
||||
"'cloud_network_monitor_cursor_lease'=>['non_unique'=>1,'columns'=>['poll_lease_until','host_id'],]",
|
||||
"'cloud_network_baseline_owner'=>['non_unique'=>1,'columns'=>['user_id','updated_at'],]",
|
||||
"'cloud_network_baseline_warmup'=>['non_unique'=>1,'columns'=>['warmup_state','sample_count'],]",
|
||||
"'cloud_network_baseline_projection_lease'=>['non_unique'=>1,'columns'=>['projection_lease_until','instance_id'],]",
|
||||
] as $requiredShape) {
|
||||
$assert(str_contains($logShapeSetup, $requiredShape),
|
||||
'CloudSetup log index order is incomplete: ' . $requiredShape);
|
||||
}
|
||||
|
||||
$assert(str_contains($sample, 'PRIMARY KEY (sample_key)')
|
||||
&& str_contains($cursor, 'PRIMARY KEY (host_id)')
|
||||
&& str_contains($baseline, 'PRIMARY KEY (instance_id)'),
|
||||
'A log-ledger primary identity is incomplete.');
|
||||
|
||||
$outboxDelete = strpos(
|
||||
$deletion, "'cloud_vm_network_projection_outbox'"
|
||||
);
|
||||
$sampleDelete = strrpos(
|
||||
$deletion, "'cloud_vm_network_observation_samples'"
|
||||
);
|
||||
$assert($outboxDelete !== false && $sampleDelete !== false
|
||||
&& $outboxDelete < $sampleDelete
|
||||
&& str_contains($deletion, "'sample_key'")
|
||||
&& str_contains($outbox,
|
||||
'REFERENCES cloud_vm_network_observation_samples (sample_key)')
|
||||
&& str_contains($outbox,
|
||||
'ON DELETE RESTRICT ON UPDATE RESTRICT')
|
||||
&& str_contains($receipt,
|
||||
'REFERENCES cloud_instances (id) ON DELETE CASCADE'),
|
||||
'Account deletion does not explicitly clear RESTRICTed intents first.');
|
||||
|
||||
$assert(str_contains($setup,
|
||||
'$logConnection, \'TABLE_CONSTRAINTS\', \'CONSTRAINT_NAME\'')
|
||||
&& str_contains($setup, 'self::REQUIRED_LOG_CHECKS')
|
||||
&& str_contains($setup, 'self::REQUIRED_LOG_INDEXES')
|
||||
&& str_contains($setup, 'self::REQUIRED_LOG_INDEX_SHAPES')
|
||||
&& str_contains($setup, 'self::REQUIRED_LOG_FOREIGN_KEYS')
|
||||
&& str_contains($setup, 'self::REQUIRED_LOG_FOREIGN_KEY_RULES')
|
||||
&& str_contains($setup,
|
||||
"'delete_rule' => 'RESTRICT'")
|
||||
&& str_contains($setup,
|
||||
"'update_rule' => 'RESTRICT'")
|
||||
&& str_contains($setup,
|
||||
"'columns' => [\n 'state', 'available_at', 'bucket_start', 'sample_key',")
|
||||
&& str_contains($setup,
|
||||
"'columns' => [\n 'instance_id', 'state', 'bucket_start', 'sample_key',")
|
||||
&& str_contains($setup,
|
||||
"'columns' => ['next_poll_at', 'poll_revision', 'host_id']")
|
||||
&& str_contains($setup,
|
||||
"'columns' => ['projection_lease_until', 'instance_id']"),
|
||||
'CloudSetup does not enforce log-schema object shapes on logConnection.');
|
||||
|
||||
echo "Abuse projection and retention ledger contracts passed.\n";
|
||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue