You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/CloudAbuseProjectionLedgerC...

367 lines
17 KiB
PHP

<?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,
'cloud_network_observation_placement')
&& str_contains($sample,
'instance_id, placement_key, bucket_start'),
'Observation sample lacks migration-cutover placement evidence.');
$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-v47-offer-billing-cycles`')
&& 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_placement'=>['non_unique'=>1,'columns'=>['instance_id','placement_key','bucket_start',]",
"'cloud_network_observation_host'=>['non_unique'=>1,'columns'=>['host_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";