feat(abuse): freeze policy revision during projection

go
codex 2 weeks ago
parent 2f9d74551f
commit 1e80a28dd1

@ -160,7 +160,9 @@ libvirt 的持久 pool 定义就是 Agent 后续使用的映射,不另写存
公网网络接入在宿主机的“网络接入”中按资源池线路独立配置,可使用 `bridge:br0`、其他 Linux 网桥或 `network:default` 这样的 libvirt network。名称以字母或数字开头,后续只能包含字母、数字、点、下划线和短横线,bridge 名称为 1~15 字符,libvirt network 名称为 1~64 字符。宿主机上的公网网桥或 libvirt network 必须由运维预先准备。Agent 只发现、验证和使用现有公网网络,不创建公网网桥、不启动 libvirt network,也不修改公网地址、上游网关或挂载。`cloud_hosts.default_network` 仅保留为兼容展示值,不参与 Agent 就绪、调度、恢复、迁移或换 IP 决策;真正的运行时权威是资源池线路、当前 Agent 代次已验证的网络接入,以及绑定该接入的公网地址池。VPC 使用下述独立的 Agent 受管 bridge 和 L3 路由,不改变这一公网边界。
当前完整 schema 版本为 `go-agent-wss-v43-abuse-policy`。自动备份订阅通过独立的延期字段保留原逻辑执行时隙;只有已确认未应用且可安全重试的救援或磁盘切换竞态,才会退休空白制品并以递增的 `schedule_attempt` 建立新代次。每个资源预留的当前公网权威只来自 `cloud_resource_reservation_ips.active_claim = 1` 的一组 IPv4+IPv6 精确声明;两族允许具有各自的 generation 和 operation ID,但必须绑定同一资源预留和网络接入,并各自与具体 IP、地址池、宿主机、实例状态双向一致。`cloud_resource_reservations` 中旧的单地址列只保留审计形态,不参与当前地址读取、释放或管理员投影。释放时历史声明保留并把 `active_claim` 置空,具体 IP 同时清空 claim、operation、reservation 和 instance 归属。普通换 IP、独立恢复和维护迁移分别只允许 transition ledger、restore attempt 与 migration source debt 所有的完整双栈流程;旧 `CloudPlacementService` 单地址恢复、旧换 IP writer,以及缺少或损坏冻结身份的任务仍会在派发 CAS 前拒绝,不能绕过新账本。
当前完整 schema 版本为 `go-agent-wss-v44-abuse-policy-projection`。从 v43 升级时,必须先停止滥用观测与投影 worker,再用经过审核的一次性迁移把当时生效的 `rule_version`、`policy_revision` 和 `policy_hash` 冻结到既有 outbox、baseline、instance state、finding 与 receipt。所有既有 `cloud_abuse_findings.finding_key` 必须在恢复 worker 前按 `SHA-256(instance_id + "\n" + rule_version + "\n" + policy_hash + "\n" + rule_code + "\n" + ip_family + "\n" + direction)` 重新计算;完成回填、重算和约束核对后才能启动新版进程。运行时代码不兼容旧 finding key,也不会用旧 key 查找或合并 finding;省略重算会让同一规则在新 policy 身份下生成另一条记录。
自动备份订阅通过独立的延期字段保留原逻辑执行时隙;只有已确认未应用且可安全重试的救援或磁盘切换竞态,才会退休空白制品并以递增的 `schedule_attempt` 建立新代次。每个资源预留的当前公网权威只来自 `cloud_resource_reservation_ips.active_claim = 1` 的一组 IPv4+IPv6 精确声明;两族允许具有各自的 generation 和 operation ID,但必须绑定同一资源预留和网络接入,并各自与具体 IP、地址池、宿主机、实例状态双向一致。`cloud_resource_reservations` 中旧的单地址列只保留审计形态,不参与当前地址读取、释放或管理员投影。释放时历史声明保留并把 `active_claim` 置空,具体 IP 同时清空 claim、operation、reservation 和 instance 归属。普通换 IP、独立恢复和维护迁移分别只允许 transition ledger、restore attempt 与 migration source debt 所有的完整双栈流程;旧 `CloudPlacementService` 单地址恢复、旧换 IP writer,以及缺少或损坏冻结身份的任务仍会在派发 CAS 前拒绝,不能绕过新账本。
Agent 的 capabilities 响应仍使用 `network_selectors` 字段,但它是自动发现的当前可用 `bridge:<名称>` / `network:<名称>` 列表,排序去重且最多 64 项,不再来自配置文件。Linux 网桥必须有 sysfs bridge 目录且接口为 UP;libvirt network 必须处于 active 状态。完整 Agent 测试保存当前代次的候选列表,但不要求某个宿主机全局默认值必须存在。管理员只能从该代次已测试列表创建网络接入,并必须对每个接入执行精确验证;创建、恢复、迁移、重装和换 IP 会在锁内再次核对本次资源池线路、接入、公网池与 Agent 代次。修改兼容 `default_network` 不会撤销 Agent 就绪结果,修改或失效真实网络接入则由接入自身的验证状态阻止运行时使用。

@ -47,7 +47,7 @@
## 部署
全新数据库必须由当前 `CloudSetup` 创建并验证 `go-agent-wss-v43-abuse-policy` 完整 schema;其中补偿模块包括:
全新数据库必须由当前 `CloudSetup` 创建并验证 `go-agent-wss-v44-abuse-policy-projection` 完整 schema;其中补偿模块包括:
- `cloud_service_compensations`;
- `cloud_service_compensation_items`;

@ -8,7 +8,7 @@ use App\Services\Config;
/** Initializes only the clean-break PHP business and Agent log schemas. */
final class CloudSetup
{
private const SCHEMA_VERSION = 'go-agent-wss-v43-abuse-policy';
private const SCHEMA_VERSION = 'go-agent-wss-v44-abuse-policy-projection';
/** Tables installed by ss.sql before cloudInit and owned by this schema. */
private const PREINSTALLED_MAIN_TABLES = [
'ui_copy_locales', 'ui_copy_entries', 'ui_copy_drafts',
@ -599,7 +599,8 @@ final class CloudSetup
'high_thresholds_json', 'created_at',
],
'cloud_abuse_instance_states' => [
'instance_id', 'user_id', 'host_id', 'risk_state',
'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',
@ -607,14 +608,16 @@ final class CloudSetup
],
'cloud_abuse_findings' => [
'id', 'finding_key', 'instance_id', 'user_id', 'host_id',
'rule_code', 'rule_version', 'direction', 'ip_family',
'rule_code', 'rule_version', 'policy_revision', 'policy_hash',
'direction', 'ip_family',
'severity', 'state', 'first_seen_at', 'last_seen_at',
'consecutive_windows', 'evidence_json', 'resolved_at',
'resolution_reason',
'created_at', 'updated_at',
],
'cloud_abuse_projection_receipts' => [
'sample_key', 'instance_id', 'rule_version', 'bucket_start',
'projected_at',
'sample_key', 'instance_id', 'rule_version', 'policy_revision',
'policy_hash', 'bucket_start', 'projected_at',
],
];
private const REQUIRED_LOG_COLUMNS = [
@ -653,7 +656,8 @@ final class CloudSetup
],
'cloud_vm_network_projection_outbox' => [
'sample_key', 'instance_id', 'bucket_start', 'placement_key',
'state', 'attempts', 'available_at', 'lease_token',
'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',
@ -667,7 +671,8 @@ final class CloudSetup
'updated_at',
],
'cloud_vm_network_baselines' => [
'instance_id', 'user_id', 'rule_version', 'sample_count',
'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',
@ -791,6 +796,7 @@ final class CloudSetup
'chk_cloud_abuse_state_collection',
'chk_cloud_abuse_state_direction',
'chk_cloud_abuse_state_severity',
'chk_cloud_abuse_state_policy',
'chk_cloud_abuse_state_projection',
],
'cloud_abuse_findings' => [
@ -799,11 +805,13 @@ final class CloudSetup
'chk_cloud_abuse_finding_family',
'chk_cloud_abuse_finding_severity',
'chk_cloud_abuse_finding_state',
'chk_cloud_abuse_finding_policy',
'chk_cloud_abuse_finding_window',
'chk_cloud_abuse_finding_resolution',
],
'cloud_abuse_projection_receipts' => [
'chk_cloud_abuse_projection_key',
'chk_cloud_abuse_projection_policy',
'chk_cloud_abuse_projection_bucket',
],
'cloud_template_import_intents' => [
@ -964,6 +972,7 @@ final class CloudSetup
],
'cloud_vm_network_projection_outbox' => [
'chk_cloud_network_projection_key',
'chk_cloud_network_projection_policy',
'chk_cloud_network_projection_state',
'chk_cloud_network_projection_lease',
],
@ -974,6 +983,7 @@ final class CloudSetup
],
'cloud_vm_network_baselines' => [
'chk_cloud_network_baseline_warmup',
'chk_cloud_network_baseline_policy',
'chk_cloud_network_baseline_projection',
'chk_cloud_network_baseline_projection_lease',
],

@ -28,6 +28,9 @@ final class CloudAbuseObservationIngestService
/** @var \Closure():int */
private readonly \Closure $clock;
/** @var \Closure():CloudAbusePolicySnapshot */
private readonly \Closure $policySnapshot;
/** @var list<string> */
private array $errors = [];
@ -35,11 +38,13 @@ final class CloudAbuseObservationIngestService
* @param null|callable():array<int> $hostIds
* @param null|callable(int):object $clientFactory
* @param null|callable():int $clock
* @param null|callable():CloudAbusePolicySnapshot $policySnapshot
*/
public function __construct(
?callable $hostIds = null,
?callable $clientFactory = null,
?callable $clock = null
?callable $clock = null,
?callable $policySnapshot = null
) {
$this->hostIds = \Closure::fromCallable($hostIds
?? static fn (): array =>
@ -49,6 +54,9 @@ final class CloudAbuseObservationIngestService
CloudAgentSettingsService::clientForHost($hostId, 12.0));
$this->clock = \Closure::fromCallable($clock
?? static fn (): int => time());
$this->policySnapshot = \Closure::fromCallable($policySnapshot
?? static fn (): CloudAbusePolicySnapshot =>
(new CloudAbusePolicyService())->activeSnapshot());
}
/** Poll at most one due host. */
@ -194,6 +202,15 @@ final class CloudAbuseObservationIngestService
);
}
$windows = $batch['windows'];
$policy = null;
if ($windows !== []) {
$policy = ($this->policySnapshot)();
if (!$policy instanceof CloudAbusePolicySnapshot) {
throw new \LogicException(
'Abuse monitor policy snapshot is invalid'
);
}
}
$hostId = $claim['host_id'];
$sameGeneration = is_string($claim['generation'])
&& hash_equals($claim['generation'], $generation);
@ -212,6 +229,7 @@ final class CloudAbuseObservationIngestService
$sameGeneration,
$uuids,
$windows,
$policy,
$now
): void {
$instances = $uuids === [] ? collect() : $main
@ -233,6 +251,7 @@ final class CloudAbuseObservationIngestService
$sameGeneration,
$instances,
$windows,
$policy,
$mappingMiss,
$now
): void {
@ -304,6 +323,10 @@ final class CloudAbuseObservationIngestService
'instance_id' => (int) $instance->id,
'bucket_start' => $bucketStart,
'placement_key' => $placementKey,
'rule_version' =>
$policy->policy->engineVersion(),
'policy_revision' => $policy->revision,
'policy_hash' => $policy->policyHash,
'state' => 'pending',
'attempts' => 0,
'available_at' => $now,
@ -467,7 +490,13 @@ final class CloudAbuseObservationIngestService
if ($existing === null
|| (int) $existing->instance_id !== $instanceId
|| (int) $existing->bucket_start !== $bucketStart
|| !hash_equals((string) $existing->placement_key, $placementKey)) {
|| !hash_equals((string) $existing->placement_key, $placementKey)
|| (int) $existing->rule_version < 1
|| (int) $existing->policy_revision < 1
|| preg_match(
'/^[a-f0-9]{64}$/D',
(string) $existing->policy_hash
) !== 1) {
throw new \RuntimeException(
'Network observation outbox replay identity conflict'
);

@ -33,6 +33,32 @@ final class CloudAbusePolicyService
}, 3);
}
public function snapshotByIdentity(
int $revision,
string $policyHash
): CloudAbusePolicySnapshot {
if ($revision < 1
|| preg_match('/^[a-f0-9]{64}$/D', $policyHash) !== 1) {
throw new CloudError('网络滥用检测策略身份无效', 422);
}
$connection = $this->connection();
return $connection->transaction(function () use (
$connection,
$revision,
$policyHash
): CloudAbusePolicySnapshot {
$row = $connection->table('cloud_abuse_policy_revisions')
->where('revision', $revision)->sharedLock()->first();
if ($row === null) {
throw new CloudError('网络滥用检测策略版本不存在', 409);
}
if (!hash_equals((string) $row->policy_hash, $policyHash)) {
throw new CloudError('网络滥用检测策略身份冲突', 409);
}
return $this->snapshotFromRow($connection, $row);
}, 3);
}
/**
* @param array<string,mixed> $definition
*/

@ -21,6 +21,9 @@ final class CloudAbuseProjectionService
/** @var null|\Closure(string):void */
private readonly ?\Closure $afterMainCommit;
/** @var \Closure(int,string):CloudAbusePolicySnapshot */
private readonly \Closure $policySnapshot;
/** @var list<string> */
private array $errors = [];
@ -30,13 +33,18 @@ final class CloudAbuseProjectionService
*/
public function __construct(
?callable $clock = null,
?callable $afterMainCommit = null
?callable $afterMainCommit = null,
?callable $policySnapshot = null
) {
$this->clock = \Closure::fromCallable(
$clock ?? static fn (): int => time()
);
$this->afterMainCommit = $afterMainCommit === null
? null : \Closure::fromCallable($afterMainCommit);
$this->policySnapshot = \Closure::fromCallable($policySnapshot
?? static fn (int $revision, string $hash):
CloudAbusePolicySnapshot => (new CloudAbusePolicyService())
->snapshotByIdentity($revision, $hash));
}
/** Project at most one ordered sample. */
@ -57,6 +65,7 @@ final class CloudAbuseProjectionService
}
try {
$claim = self::hydrateClaim($claim);
$claim['policy_snapshot'] = $this->loadPolicySnapshot($claim);
$decision = $this->projectMain($claim, $now);
if ($this->afterMainCommit !== null) {
($this->afterMainCommit)($claim['sample_key']);
@ -146,6 +155,19 @@ final class CloudAbuseProjectionService
&& (int) $outbox->lease_until > $now)) {
return null;
}
if (!self::validPolicyIdentity(
(int) $outbox->rule_version,
(int) $outbox->policy_revision,
(string) $outbox->policy_hash
)) {
$reported = $this->recordClaimFailure(
$log,
$outbox,
'projection_policy_identity_invalid',
$now
);
return ['claim_error_code' => $reported];
}
$sample = $log->table(
'cloud_vm_network_observation_samples'
)->where('sample_key', (string) $outbox->sample_key)
@ -173,7 +195,9 @@ final class CloudAbuseProjectionService
$log->table('cloud_vm_network_baselines')->insert([
'instance_id' => (int) $sample->instance_id,
'user_id' => (int) $sample->user_id,
'rule_version' => CloudAbuseDetector::RULE_VERSION,
'rule_version' => (int) $outbox->rule_version,
'policy_revision' => (int) $outbox->policy_revision,
'policy_hash' => (string) $outbox->policy_hash,
'sample_count' => 0,
'warmup_state' => 'warming',
'metrics_json' => self::json(
@ -193,8 +217,11 @@ final class CloudAbuseProjectionService
}
if ($baseline === null
|| (int) $baseline->user_id !== (int) $sample->user_id
|| (int) $baseline->rule_version
!== CloudAbuseDetector::RULE_VERSION) {
|| !self::validPolicyIdentity(
(int) $baseline->rule_version,
(int) $baseline->policy_revision,
(string) $baseline->policy_hash
)) {
$code = 'projection_baseline_identity_inconsistent';
$reported = $this->recordClaimFailure(
$log,
@ -250,6 +277,15 @@ final class CloudAbuseProjectionService
'quality_flags_json' =>
(string) $sample->quality_flags_json,
'baseline_json' => (string) $baseline->metrics_json,
'rule_version' => (int) $outbox->rule_version,
'policy_revision' => (int) $outbox->policy_revision,
'policy_hash' => (string) $outbox->policy_hash,
'baseline_rule_version' =>
(int) $baseline->rule_version,
'baseline_policy_revision' =>
(int) $baseline->policy_revision,
'baseline_policy_hash' =>
(string) $baseline->policy_hash,
'baseline_revision' => (int) $baseline->revision,
'last_projection_key' => $baseline->last_projection_key,
'last_bucket_start' => $baseline->last_bucket_start === null
@ -274,6 +310,9 @@ final class CloudAbuseProjectionService
'sample_key' => (string) $outbox->sample_key,
'instance_id' => (int) $outbox->instance_id,
'bucket_start' => (int) $outbox->bucket_start,
'rule_version' => (int) $outbox->rule_version,
'policy_revision' => (int) $outbox->policy_revision,
'policy_hash' => (string) $outbox->policy_hash,
]);
if ($receiptState === 'matching') {
$baseline = $log->table('cloud_vm_network_baselines')
@ -286,8 +325,15 @@ final class CloudAbuseProjectionService
$code = 'projection_receipt_finalize_pending';
} else {
try {
$snapshot = $this->loadPolicySnapshot([
'rule_version' => (int) $outbox->rule_version,
'policy_revision' =>
(int) $outbox->policy_revision,
'policy_hash' => (string) $outbox->policy_hash,
]);
$seed = $this->currentOwnerBaselineSeed(
(int) $outbox->instance_id
(int) $outbox->instance_id,
$snapshot
);
} catch (\Throwable) {
$seed = false;
@ -316,7 +362,11 @@ final class CloudAbuseProjectionService
$values = [
'user_id' => $seed['user_id'],
'rule_version' =>
CloudAbuseDetector::RULE_VERSION,
(int) $outbox->rule_version,
'policy_revision' =>
(int) $outbox->policy_revision,
'policy_hash' =>
(string) $outbox->policy_hash,
'sample_count' => 0,
'warmup_state' => 'warming',
'metrics_json' => self::json($seedBaseline),
@ -462,6 +512,40 @@ final class CloudAbuseProjectionService
return $claim;
}
/** @param array<string,mixed> $identity */
private function loadPolicySnapshot(
array $identity
): CloudAbusePolicySnapshot {
$ruleVersion = (int) ($identity['rule_version'] ?? 0);
$revision = (int) ($identity['policy_revision'] ?? 0);
$hash = (string) ($identity['policy_hash'] ?? '');
if (!self::validPolicyIdentity($ruleVersion, $revision, $hash)) {
throw new \RuntimeException(
'Abuse projection policy identity is invalid'
);
}
$snapshot = ($this->policySnapshot)($revision, $hash);
if (!$snapshot instanceof CloudAbusePolicySnapshot
|| $snapshot->revision !== $revision
|| !hash_equals($snapshot->policyHash, $hash)
|| $snapshot->policy->engineVersion() !== $ruleVersion) {
throw new \RuntimeException(
'Abuse projection policy snapshot identity conflict'
);
}
return $snapshot;
}
private static function validPolicyIdentity(
int $ruleVersion,
int $revision,
string $hash
): bool {
return $ruleVersion > 0
&& $revision > 0
&& preg_match('/^[a-f0-9]{64}$/D', $hash) === 1;
}
/**
* Recheck the semantic invariants that made the stored sample canonical.
* The log database is a durable boundary: corrupt rows must never be
@ -537,6 +621,22 @@ final class CloudAbuseProjectionService
return ['state' => 'discarded',
'reason' => 'out_of_order_sample'];
}
$snapshot = $claim['policy_snapshot'] ?? null;
if (!$snapshot instanceof CloudAbusePolicySnapshot) {
throw new \RuntimeException(
'Abuse projection policy snapshot is missing'
);
}
$policy = $snapshot->policy;
$policyChanged = $claim['baseline_rule_version']
!== $claim['rule_version']
|| !hash_equals(
$claim['baseline_policy_hash'],
$claim['policy_hash']
);
$base = $policyChanged
? self::rebaseBaselineForPolicy($claim['baseline'], $policy)
: $claim['baseline'];
$reset = in_array(
'panel_ring_gap',
$claim['quality_flags'],
@ -544,14 +644,15 @@ final class CloudAbuseProjectionService
);
$base = $reset
? CloudAbuseDetector::resetBaselineFromPrevious(
$claim['baseline']
) : $claim['baseline'];
$base,
$policy
) : $base;
$detected = CloudAbuseDetector::evaluate([
'bucket_start' => $claim['bucket_start'],
'complete' => $claim['complete'],
'quality_flags' => $claim['quality_flags'],
'metrics' => $claim['metrics'],
], $base);
], $base, $policy);
$baseline = $detected['baseline'];
$evaluation = $detected['evaluation'];
if (!$claim['complete']) {
@ -567,6 +668,7 @@ final class CloudAbuseProjectionService
$claim,
$baseline,
$evaluation,
$policyChanged,
$now
): array {
$instance = $main->table('cloud_instances')
@ -594,7 +696,13 @@ final class CloudAbuseProjectionService
|| (int) $receipt->bucket_start
!== $claim['bucket_start']
|| (int) $receipt->rule_version
!== CloudAbuseDetector::RULE_VERSION) {
!== $claim['rule_version']
|| (int) $receipt->policy_revision
!== $claim['policy_revision']
|| !hash_equals(
(string) $receipt->policy_hash,
$claim['policy_hash']
)) {
throw new \RuntimeException(
'Abuse projection receipt identity conflict'
);
@ -611,6 +719,12 @@ final class CloudAbuseProjectionService
'reason' => 'projection_order_conflict'];
}
$this->retireReplacedPolicyFindings(
$main,
$claim,
$now
);
$healthy = ($evaluation['collection_state'] ?? null)
=== 'healthy';
$summary = [
@ -625,25 +739,37 @@ final class CloudAbuseProjectionService
'instance_id' => $claim['instance_id'],
'user_id' => $claim['user_id'],
'host_id' => (int) $instance->host_id,
'rule_version' => $claim['rule_version'],
'policy_revision' => $claim['policy_revision'],
'policy_hash' => $claim['policy_hash'],
'risk_state' => $healthy
? (string) $evaluation['risk_state']
: (string) ($state->risk_state ?? 'observing'),
: ($policyChanged
? 'observing'
: (string) ($state->risk_state ?? 'observing')),
'collection_state' => $healthy ? 'healthy' : 'degraded',
'latest_direction' => $healthy
? (string) $evaluation['direction']
: (string) ($state->latest_direction ?? 'none'),
: ($policyChanged
? 'none'
: (string) ($state->latest_direction ?? 'none')),
'latest_rule_code' => $healthy
? (string) $evaluation['latest_rule_code']
: (string) ($state->latest_rule_code ?? ''),
: ($policyChanged
? ''
: (string) ($state->latest_rule_code ?? '')),
'latest_severity' => $healthy
? (string) $evaluation['latest_severity']
: (string) ($state->latest_severity ?? 'none'),
: ($policyChanged
? 'none'
: (string) ($state->latest_severity ?? 'none')),
'summary_json' => self::json($summary),
'last_projection_key' => $claim['sample_key'],
'last_bucket_start' => $claim['bucket_start'],
'last_sample_at' => $claim['bucket_start'] + 60,
'last_evaluated_at' => $healthy
? $now : ($state->last_evaluated_at ?? null),
? $now : ($policyChanged
? null : ($state->last_evaluated_at ?? null)),
'updated_at' => $now,
];
if ($state === null) {
@ -667,7 +793,9 @@ final class CloudAbuseProjectionService
$main->table('cloud_abuse_projection_receipts')->insert([
'sample_key' => $claim['sample_key'],
'instance_id' => $claim['instance_id'],
'rule_version' => CloudAbuseDetector::RULE_VERSION,
'rule_version' => $claim['rule_version'],
'policy_revision' => $claim['policy_revision'],
'policy_hash' => $claim['policy_hash'],
'bucket_start' => $claim['bucket_start'],
'projected_at' => $now,
]);
@ -675,6 +803,68 @@ final class CloudAbuseProjectionService
}, 3);
}
/**
* A changed detector policy keeps the long-lived statistical history but
* cannot inherit threshold streaks earned under another immutable policy.
*
* @param array<string,mixed> $previous
* @return array<string,mixed>
*/
private static function rebaseBaselineForPolicy(
array $previous,
CloudAbuseDetectionPolicy $policy
): array {
$baseline = CloudAbuseDetector::initialBaseline($policy);
$baseline['sample_count'] = max(
0,
(int) ($previous['sample_count'] ?? 0)
);
$baseline['last_bucket_start'] =
is_int($previous['last_bucket_start'] ?? null)
? $previous['last_bucket_start'] : null;
$baseline['last_interface'] =
is_array($previous['last_interface'] ?? null)
? $previous['last_interface'] : null;
$baseline['stats'] = is_array($previous['stats'] ?? null)
? $previous['stats'] : [];
return $baseline;
}
/** @param array<string,mixed> $claim */
private function retireReplacedPolicyFindings(
object $main,
array $claim,
int $now
): void {
$rows = $main->table('cloud_abuse_findings')
->where('instance_id', $claim['instance_id'])
->where('user_id', $claim['user_id'])
->where('state', 'open')
->where(static function ($query) use ($claim): void {
$query->where('rule_version', '<>', $claim['rule_version'])
->orWhere('policy_hash', '<>', $claim['policy_hash']);
})->orderBy('id')->lockForUpdate()->get();
foreach ($rows as $row) {
$resolvedAt = max(
$claim['bucket_start'],
(int) $row->last_seen_at
);
$updated = $main->table('cloud_abuse_findings')
->where('id', (int) $row->id)
->where('state', 'open')->update([
'state' => 'resolved',
'resolved_at' => $resolvedAt,
'resolution_reason' => 'policy_replaced',
'updated_at' => $now,
]);
if ($updated !== 1) {
throw new \RuntimeException(
'Abuse policy replacement finding CAS failed'
);
}
}
}
private function retireOwnerReadModel(
object $main,
int $instanceId,
@ -699,6 +889,7 @@ final class CloudAbuseProjectionService
->where('state', 'open')->update([
'state' => 'resolved',
'resolved_at' => $resolvedAt,
'resolution_reason' => 'owner_changed',
'updated_at' => $now,
]);
if ($updated !== 1) {
@ -712,8 +903,10 @@ final class CloudAbuseProjectionService
/**
* @return null|array{user_id:int,baseline:array<string,mixed>}
*/
private function currentOwnerBaselineSeed(int $instanceId): ?array
{
private function currentOwnerBaselineSeed(
int $instanceId,
CloudAbusePolicySnapshot $snapshot
): ?array {
$main = User::getConnectionResolver()->connection('default');
$instance = $main->table('cloud_instances')
->where('id', $instanceId)->first(['user_id']);
@ -724,7 +917,8 @@ final class CloudAbuseProjectionService
$rows = $main->table('cloud_abuse_findings')
->where('instance_id', $instanceId)
->where('user_id', $userId)
->where('rule_version', CloudAbuseDetector::RULE_VERSION)
->where('rule_version', $snapshot->policy->engineVersion())
->where('policy_hash', $snapshot->policyHash)
->where('state', 'open')
->orderBy('id')->get([
'rule_code', 'direction', 'ip_family', 'severity',
@ -740,7 +934,10 @@ final class CloudAbuseProjectionService
}
return [
'user_id' => $userId,
'baseline' => CloudAbuseDetector::resetBaseline($findings),
'baseline' => CloudAbuseDetector::resetBaseline(
$findings,
$snapshot->policy
),
];
}
@ -758,7 +955,8 @@ final class CloudAbuseProjectionService
foreach ($findings as $finding) {
$key = hash('sha256', implode("\n", [
$claim['instance_id'],
CloudAbuseDetector::RULE_VERSION,
$claim['rule_version'],
$claim['policy_hash'],
(string) $finding['rule_code'],
(string) $finding['ip_family'],
(string) $finding['direction'],
@ -771,7 +969,9 @@ final class CloudAbuseProjectionService
'user_id' => $claim['user_id'],
'host_id' => $currentHostId,
'rule_code' => (string) $finding['rule_code'],
'rule_version' => CloudAbuseDetector::RULE_VERSION,
'rule_version' => $claim['rule_version'],
'policy_revision' => $claim['policy_revision'],
'policy_hash' => $claim['policy_hash'],
'direction' => (string) $finding['direction'],
'ip_family' => (string) $finding['ip_family'],
'severity' => (string) $finding['severity'],
@ -789,6 +989,7 @@ final class CloudAbuseProjectionService
(array) $finding['evidence']
),
'resolved_at' => null,
'resolution_reason' => '',
'updated_at' => $now,
];
if ($row === null) {
@ -806,6 +1007,7 @@ final class CloudAbuseProjectionService
->where('state', 'open')->update([
'state' => 'resolved',
'resolved_at' => $claim['bucket_start'],
'resolution_reason' => 'evidence_clear',
'updated_at' => $now,
]);
}
@ -838,14 +1040,23 @@ final class CloudAbuseProjectionService
);
}
$sampleCount = max(0, (int) ($baseline['sample_count'] ?? 0));
$snapshot = $claim['policy_snapshot'] ?? null;
if (!$snapshot instanceof CloudAbusePolicySnapshot) {
throw new \RuntimeException(
'Abuse projection policy snapshot is missing'
);
}
$baselineUpdated = $log->table('cloud_vm_network_baselines')
->where('instance_id', $claim['instance_id'])
->where('revision', $claim['baseline_revision'])
->where('projection_lease_token', $claim['lease_token'])
->update([
'rule_version' => $claim['rule_version'],
'policy_revision' => $claim['policy_revision'],
'policy_hash' => $claim['policy_hash'],
'sample_count' => $sampleCount,
'warmup_state' => $sampleCount
>= CloudAbuseDetector::WARMUP_WINDOWS
>= $snapshot->policy->windows()['warmup']
? 'ready' : 'warming',
'metrics_json' => self::json($baseline),
'last_projection_key' => $claim['sample_key'],
@ -1002,7 +1213,13 @@ final class CloudAbuseProjectionService
if ((int) $receipt->instance_id !== $claim['instance_id']
|| (int) $receipt->bucket_start !== $claim['bucket_start']
|| (int) $receipt->rule_version
!== CloudAbuseDetector::RULE_VERSION
!== (int) ($claim['rule_version'] ?? 0)
|| (int) $receipt->policy_revision
!== (int) ($claim['policy_revision'] ?? 0)
|| !hash_equals(
(string) $receipt->policy_hash,
(string) ($claim['policy_hash'] ?? '')
)
|| (int) $receipt->projected_at < 1) {
return 'conflict';
}

@ -2960,6 +2960,9 @@ CREATE TABLE cloud_abuse_instance_states (
instance_id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
user_id BIGINT UNSIGNED NOT NULL,
host_id BIGINT UNSIGNED NOT NULL,
rule_version INT UNSIGNED NOT NULL,
policy_revision BIGINT UNSIGNED NOT NULL,
policy_hash CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
risk_state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
collection_state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
latest_direction VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'none',
@ -2992,6 +2995,11 @@ CREATE TABLE cloud_abuse_instance_states (
CONSTRAINT chk_cloud_abuse_state_severity CHECK (
latest_severity IN ('none','info','suspicious','high')
),
CONSTRAINT chk_cloud_abuse_state_policy CHECK (
rule_version > 0
AND policy_revision > 0
AND policy_hash REGEXP '^[a-f0-9]{64}$'
),
CONSTRAINT chk_cloud_abuse_state_projection CHECK (
(last_projection_key IS NULL AND last_bucket_start IS NULL)
OR (last_projection_key IS NOT NULL
@ -3013,6 +3021,8 @@ CREATE TABLE cloud_abuse_findings (
host_id BIGINT UNSIGNED NOT NULL,
rule_code VARCHAR(48) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
rule_version INT UNSIGNED NOT NULL,
policy_revision BIGINT UNSIGNED NOT NULL,
policy_hash CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
direction VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
ip_family VARCHAR(8) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
severity VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
@ -3022,6 +3032,7 @@ CREATE TABLE cloud_abuse_findings (
consecutive_windows INT UNSIGNED NOT NULL DEFAULT 1,
evidence_json JSON NOT NULL,
resolved_at BIGINT UNSIGNED DEFAULT NULL,
resolution_reason VARCHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '',
created_at BIGINT UNSIGNED NOT NULL,
updated_at BIGINT UNSIGNED NOT NULL,
UNIQUE KEY cloud_abuse_finding_key (finding_key),
@ -3051,13 +3062,21 @@ CREATE TABLE cloud_abuse_findings (
CONSTRAINT chk_cloud_abuse_finding_state CHECK (
state IN ('open','resolved')
),
CONSTRAINT chk_cloud_abuse_finding_policy CHECK (
rule_version > 0
AND policy_revision > 0
AND policy_hash REGEXP '^[a-f0-9]{64}$'
),
CONSTRAINT chk_cloud_abuse_finding_window CHECK (
first_seen_at <= last_seen_at AND consecutive_windows >= 1
),
CONSTRAINT chk_cloud_abuse_finding_resolution CHECK (
(state = 'open' AND resolved_at IS NULL)
(state = 'open' AND resolved_at IS NULL AND resolution_reason = '')
OR (state = 'resolved' AND resolved_at IS NOT NULL
AND resolved_at >= last_seen_at)
AND resolved_at >= last_seen_at
AND resolution_reason IN (
'evidence_clear','policy_replaced','owner_changed'
))
),
CONSTRAINT fk_cloud_abuse_finding_instance FOREIGN KEY (instance_id)
REFERENCES cloud_instances (id) ON DELETE CASCADE ON UPDATE RESTRICT
@ -3070,6 +3089,8 @@ CREATE TABLE cloud_abuse_projection_receipts (
sample_key CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL PRIMARY KEY,
instance_id BIGINT UNSIGNED NOT NULL,
rule_version INT UNSIGNED NOT NULL,
policy_revision BIGINT UNSIGNED NOT NULL,
policy_hash CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
bucket_start BIGINT UNSIGNED NOT NULL,
projected_at BIGINT UNSIGNED NOT NULL,
KEY cloud_abuse_projection_instance (
@ -3079,6 +3100,11 @@ CREATE TABLE cloud_abuse_projection_receipts (
CONSTRAINT chk_cloud_abuse_projection_key CHECK (
sample_key REGEXP '^[a-f0-9]{64}$'
),
CONSTRAINT chk_cloud_abuse_projection_policy CHECK (
rule_version > 0
AND policy_revision > 0
AND policy_hash REGEXP '^[a-f0-9]{64}$'
),
CONSTRAINT chk_cloud_abuse_projection_bucket CHECK (
bucket_start % 60 = 0
),

@ -142,6 +142,9 @@ CREATE TABLE cloud_vm_network_projection_outbox (
instance_id BIGINT UNSIGNED NOT NULL,
bucket_start BIGINT UNSIGNED NOT NULL,
placement_key CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
rule_version INT UNSIGNED NOT NULL,
policy_revision BIGINT UNSIGNED NOT NULL,
policy_hash CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
attempts INT UNSIGNED NOT NULL DEFAULT 0,
available_at BIGINT UNSIGNED NOT NULL,
@ -168,6 +171,11 @@ CREATE TABLE cloud_vm_network_projection_outbox (
AND instance_id > 0
AND bucket_start % 60 = 0
),
CONSTRAINT chk_cloud_network_projection_policy CHECK (
rule_version > 0
AND policy_revision > 0
AND policy_hash REGEXP '^[a-f0-9]{64}$'
),
CONSTRAINT chk_cloud_network_projection_state CHECK (
state IN ('pending','leased','done','discarded')
),
@ -251,6 +259,8 @@ CREATE TABLE cloud_vm_network_baselines (
instance_id BIGINT UNSIGNED NOT NULL,
user_id BIGINT UNSIGNED NOT NULL,
rule_version INT UNSIGNED NOT NULL,
policy_revision BIGINT UNSIGNED NOT NULL,
policy_hash CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
sample_count INT UNSIGNED NOT NULL DEFAULT 0,
warmup_state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
metrics_json JSON NOT NULL,
@ -270,6 +280,11 @@ CREATE TABLE cloud_vm_network_baselines (
CONSTRAINT chk_cloud_network_baseline_warmup CHECK (
warmup_state IN ('warming','ready')
),
CONSTRAINT chk_cloud_network_baseline_policy CHECK (
rule_version > 0
AND policy_revision > 0
AND policy_hash REGEXP '^[a-f0-9]{64}$'
),
CONSTRAINT chk_cloud_network_baseline_projection CHECK (
(last_projection_key IS NULL AND last_bucket_start IS NULL)
OR (last_projection_key IS NOT NULL

@ -33,7 +33,8 @@ $deletion = $read('app/Services/UserDataDeletionService.php');
$mainTables = [
'cloud_abuse_instance_states' => [
'instance_id', 'user_id', 'host_id', 'risk_state',
'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',
@ -41,13 +42,15 @@ $mainTables = [
],
'cloud_abuse_findings' => [
'id', 'finding_key', 'instance_id', 'user_id', 'host_id',
'rule_code', 'rule_version', 'direction', 'ip_family', 'severity',
'rule_code', 'rule_version', 'policy_revision', 'policy_hash',
'direction', 'ip_family', 'severity',
'state', 'first_seen_at', 'last_seen_at', 'consecutive_windows',
'evidence_json', 'resolved_at', 'created_at', 'updated_at',
'evidence_json', 'resolved_at', 'resolution_reason',
'created_at', 'updated_at',
],
'cloud_abuse_projection_receipts' => [
'sample_key', 'instance_id', 'rule_version', 'bucket_start',
'projected_at',
'sample_key', 'instance_id', 'rule_version', 'policy_revision',
'policy_hash', 'bucket_start', 'projected_at',
],
];
$logTables = [
@ -60,7 +63,8 @@ $logTables = [
],
'cloud_vm_network_projection_outbox' => [
'sample_key', 'instance_id', 'bucket_start', 'placement_key',
'state', 'attempts', 'available_at', 'lease_token', 'lease_until',
'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',
],
@ -72,7 +76,8 @@ $logTables = [
'last_success_at', 'last_error_code', 'updated_at',
],
'cloud_vm_network_baselines' => [
'instance_id', 'user_id', 'rule_version', 'sample_count',
'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',
@ -172,12 +177,14 @@ foreach ([
'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) {
@ -195,7 +202,15 @@ $assert(str_contains($definitions['cloud_abuse_instance_states'],
&& str_contains($definitions['cloud_abuse_findings'],
"ip_family IN ('ipv4','ipv6','mixed')")
&& str_contains($definitions['cloud_abuse_findings'],
"state IN ('open','resolved')"),
"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_instance_states'],
@ -252,7 +267,7 @@ $assert(str_contains(
$assert(str_contains(
$setup,
"SCHEMA_VERSION = 'go-agent-wss-v43-abuse-policy'"
"SCHEMA_VERSION = 'go-agent-wss-v44-abuse-policy-projection'"
), 'CloudSetup schema version was not advanced for abuse observation.');
foreach (array_keys($mainTables + $logTables) as $table) {

@ -3,7 +3,9 @@
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudInstance;
use App\Services\Cloud\CloudAbuseDetectionPolicy;
use App\Services\Cloud\CloudAbuseObservationIngestService;
use App\Services\Cloud\CloudAbusePolicySnapshot;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
@ -79,6 +81,11 @@ $logSchema->create('cloud_vm_network_projection_outbox',
$table->integer('instance_id');
$table->integer('bucket_start');
$table->string('placement_key');
$table->integer('rule_version')->default(1);
$table->integer('policy_revision')->default(1);
$table->string('policy_hash')->default(
'511828bde950c504f92bb7a0603ca8b14453f575ead276c1889baa764f384dae'
);
$table->string('state');
$table->integer('attempts');
$table->integer('available_at');
@ -177,10 +184,21 @@ $client = new class() {
}
};
$now = 1000;
$defaultPolicy = CloudAbuseDetectionPolicy::defaults();
$policySnapshot = new CloudAbusePolicySnapshot(
1,
$defaultPolicy->hash(),
$defaultPolicy,
null,
0
);
$worker = new CloudAbuseObservationIngestService(
static fn (): array => [7],
static fn (int $hostId): object => $client,
static function () use (&$now): int { return $now; }
static function () use (&$now): int { return $now; },
static function () use (&$policySnapshot): CloudAbusePolicySnapshot {
return $policySnapshot;
}
);
$client->responses[] = $batch(
@ -197,7 +215,10 @@ $outbox = $log->table('cloud_vm_network_projection_outbox')->first();
$assert($cursor !== null && (int) $cursor->cursor_value === 1
&& $cursor->collection_state === 'healthy'
&& $sampleRow !== null && (int) $sampleRow->instance_id === $instance->id
&& $outbox !== null && $outbox->state === 'pending',
&& $outbox !== null && $outbox->state === 'pending'
&& (int) $outbox->rule_version === $defaultPolicy->engineVersion()
&& (int) $outbox->policy_revision === 1
&& hash_equals($defaultPolicy->hash(), (string) $outbox->policy_hash),
'Sample, outbox and host cursor did not commit together.');
$assert(($client->calls[0][0]['is_super_admin'] ?? false) === true
&& $client->calls[0][1] === null
@ -414,6 +435,18 @@ $assert((int) $cursor->cursor_value === 2
$log->table('cloud_vm_network_projection_outbox')
->where('sample_key', $sampleKey)
->update(['placement_key' => $placementKey]);
$changedDefinition = $defaultPolicy->definition();
$changedDefinition['windows']['suspicious'] = 4;
$changedPolicy = CloudAbuseDetectionPolicy::fromDefinition(
$changedDefinition
);
$policySnapshot = new CloudAbusePolicySnapshot(
2,
$changedPolicy->hash(),
$changedPolicy,
7,
$now
);
$now += 60;
$client->responses[] = $blocked;
$assert($worker->tick(), 'Exact outbox replay did not recover.');
@ -424,7 +457,9 @@ $assert((int) $cursor->cursor_value === 3
&& $log->table('cloud_vm_network_observation_samples')->count()
=== $beforeSamples + 1
&& $log->table('cloud_vm_network_projection_outbox')->count()
=== $beforeOutbox + 1,
=== $beforeOutbox + 1
&& (int) $log->table('cloud_vm_network_projection_outbox')
->where('sample_key', $sampleKey)->value('policy_revision') === 1,
'An exact sample/outbox replay was not accepted idempotently.');
$contentReplay = $batch(

@ -96,7 +96,7 @@ foreach ($defaults->rules() as $code => $rule) {
'The fresh schema rule seed drifted from PHP: ' . $code);
}
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v43-abuse-policy'")
"SCHEMA_VERSION = 'go-agent-wss-v44-abuse-policy-projection'")
&& str_contains($service, "->where('active_slot', 1)->lockForUpdate()")
&& str_contains($service, "->where('active_slot', 1)->update")
&& str_contains($service, 'insertGetId'),

@ -141,6 +141,18 @@ $expectCloudError(
);
$assert($service->activeSnapshot()->revision === 2,
'Rejected policy input changed the active revision.');
$historical = $service->snapshotByIdentity(1, $defaults->hash());
$assert($historical->revision === 1
&& $historical->policy->definition() === $defaults->definition(),
'Historical immutable policy lookup followed the current active slot.');
$expectCloudError(
static fn () => $service->snapshotByIdentity(
1,
$published->policyHash
),
409,
'Historical policy lookup accepted a mismatched immutable hash.'
);
$connection->unprepared(
"CREATE TRIGGER abort_abuse_policy_rule BEFORE INSERT "

@ -28,6 +28,8 @@ $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');
@ -74,6 +76,12 @@ $assert(str_contains($outbox, 'PRIMARY KEY (sample_key)')
&& 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(
@ -142,6 +150,12 @@ $assert(str_contains($cursor,
'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')
@ -163,6 +177,11 @@ $assert(str_contains($baseline, 'last_projection_key CHAR(64)')
$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,
@ -174,6 +193,11 @@ $assert(str_contains($receipt,
'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')
@ -185,6 +209,12 @@ $assert(str_contains($state, 'last_projection_key CHAR(64)')
$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,
@ -195,6 +225,48 @@ $assert(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,
'所有既有 `cloud_abuse_findings.finding_key` 必须'
) && str_contains($deployment, '运行时代码不兼容旧 finding key')
&& str_contains(
$deployment,
'instance_id + "\\n" + rule_version + "\\n" + policy_hash'
),
'Deployment contract does not require v44 finding-key recomputation.');
foreach ([
'cloud_vm_network_projection_outbox',
'cloud_abuse_projection_receipts',
@ -206,10 +278,15 @@ 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',

@ -3,6 +3,8 @@
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudAbuseDetector;
use App\Services\Cloud\CloudAbuseDetectionPolicy;
use App\Services\Cloud\CloudAbusePolicySnapshot;
use App\Services\Cloud\CloudAbuseProjectionService;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
@ -34,6 +36,11 @@ $mainSchema->create('cloud_abuse_instance_states',
$table->integer('instance_id')->primary();
$table->integer('user_id');
$table->integer('host_id');
$table->integer('rule_version')->default(1);
$table->integer('policy_revision')->default(1);
$table->string('policy_hash')->default(
'511828bde950c504f92bb7a0603ca8b14453f575ead276c1889baa764f384dae'
);
$table->string('risk_state');
$table->string('collection_state');
$table->string('latest_direction');
@ -56,6 +63,10 @@ $mainSchema->create('cloud_abuse_findings',
$table->integer('host_id');
$table->string('rule_code');
$table->integer('rule_version');
$table->integer('policy_revision')->default(1);
$table->string('policy_hash')->default(
'511828bde950c504f92bb7a0603ca8b14453f575ead276c1889baa764f384dae'
);
$table->string('direction');
$table->string('ip_family');
$table->string('severity');
@ -65,6 +76,7 @@ $mainSchema->create('cloud_abuse_findings',
$table->integer('consecutive_windows');
$table->text('evidence_json');
$table->integer('resolved_at')->nullable();
$table->string('resolution_reason')->default('');
$table->integer('created_at');
$table->integer('updated_at');
});
@ -73,6 +85,10 @@ $mainSchema->create('cloud_abuse_projection_receipts',
$table->string('sample_key')->primary();
$table->integer('instance_id');
$table->integer('rule_version');
$table->integer('policy_revision')->default(1);
$table->string('policy_hash')->default(
'511828bde950c504f92bb7a0603ca8b14453f575ead276c1889baa764f384dae'
);
$table->integer('bucket_start');
$table->integer('projected_at');
});
@ -102,6 +118,11 @@ $logSchema->create('cloud_vm_network_projection_outbox',
$table->integer('instance_id');
$table->integer('bucket_start');
$table->string('placement_key');
$table->integer('rule_version')->default(1);
$table->integer('policy_revision')->default(1);
$table->string('policy_hash')->default(
'511828bde950c504f92bb7a0603ca8b14453f575ead276c1889baa764f384dae'
);
$table->string('state');
$table->integer('attempts')->default(0);
$table->integer('available_at');
@ -119,6 +140,10 @@ $logSchema->create('cloud_vm_network_baselines',
$table->integer('instance_id')->primary();
$table->integer('user_id');
$table->integer('rule_version');
$table->integer('policy_revision')->default(1);
$table->string('policy_hash')->default(
'511828bde950c504f92bb7a0603ca8b14453f575ead276c1889baa764f384dae'
);
$table->integer('sample_count');
$table->string('warmup_state');
$table->text('metrics_json');
@ -134,6 +159,39 @@ $logSchema->create('cloud_vm_network_baselines',
$main = $capsule->connection('default');
$log = $capsule->connection('log');
$now = 1000;
$defaultPolicy = CloudAbuseDetectionPolicy::defaults();
$policySnapshot = new CloudAbusePolicySnapshot(
1,
$defaultPolicy->hash(),
$defaultPolicy,
null,
0
);
$changedDefinition = $defaultPolicy->definition();
$changedDefinition['windows']['suspicious'] = 4;
$changedDefinition['rules']['outbound_udp_fanout']['enabled'] = false;
$changedPolicy = CloudAbuseDetectionPolicy::fromDefinition(
$changedDefinition
);
$changedPolicySnapshot = new CloudAbusePolicySnapshot(
2,
$changedPolicy->hash(),
$changedPolicy,
7,
900
);
$sameDetectorSnapshot = new CloudAbusePolicySnapshot(
3,
$defaultPolicy->hash(),
$defaultPolicy,
7,
950
);
$policySnapshots = [
'1:' . $policySnapshot->policyHash => $policySnapshot,
'2:' . $changedPolicySnapshot->policyHash => $changedPolicySnapshot,
'3:' . $sameDetectorSnapshot->policyHash => $sameDetectorSnapshot,
];
$uuid = static fn (int $id): string => sprintf(
'123e4567-e89b-42d3-a456-%012d',
$id
@ -179,12 +237,16 @@ $insertBaseline = static function (
?string $lastKey = null,
?string $lease = null,
int $leaseUntil = 0,
int $revision = 0
) use ($log, &$now): void {
int $revision = 0,
?CloudAbusePolicySnapshot $snapshot = null
) use ($log, &$now, $policySnapshot): void {
$snapshot ??= $policySnapshot;
$log->table('cloud_vm_network_baselines')->insert([
'instance_id' => $instanceId,
'user_id' => $userId,
'rule_version' => CloudAbuseDetector::RULE_VERSION,
'rule_version' => $snapshot->policy->engineVersion(),
'policy_revision' => $snapshot->revision,
'policy_hash' => $snapshot->policyHash,
'sample_count' => (int) ($baseline['sample_count'] ?? 0),
'warmup_state' => (int) ($baseline['sample_count'] ?? 0)
>= CloudAbuseDetector::WARMUP_WINDOWS ? 'ready' : 'warming',
@ -208,8 +270,10 @@ $insertSample = static function (
string $state = 'pending',
?string $lease = null,
int $leaseUntil = 0,
int $attempts = 0
) use ($log, $uuid, &$now): string {
int $attempts = 0,
?CloudAbusePolicySnapshot $snapshot = null
) use ($log, $uuid, &$now, $policySnapshot): string {
$snapshot ??= $policySnapshot;
$key = hash('sha256', $instanceId . "\n" . $bucket);
$placement = hash('sha256', 'placement-' . $instanceId);
$log->table('cloud_vm_network_observation_samples')->insert([
@ -236,6 +300,9 @@ $insertSample = static function (
'instance_id' => $instanceId,
'bucket_start' => $bucket,
'placement_key' => $placement,
'rule_version' => $snapshot->policy->engineVersion(),
'policy_revision' => $snapshot->revision,
'policy_hash' => $snapshot->policyHash,
'state' => $state,
'attempts' => $attempts,
'available_at' => $now,
@ -250,10 +317,22 @@ $insertSample = static function (
]);
return $key;
};
$worker = static function (?callable $hook = null) use (&$now): object {
$worker = static function (?callable $hook = null) use (
&$now,
&$policySnapshots
): object {
return new CloudAbuseProjectionService(
static function () use (&$now): int { return $now; },
$hook
$hook,
static function (int $revision, string $hash) use (
&$policySnapshots
): CloudAbusePolicySnapshot {
$key = $revision . ':' . $hash;
if (!isset($policySnapshots[$key])) {
throw new RuntimeException('Unknown frozen policy identity');
}
return $policySnapshots[$key];
}
);
};
@ -298,7 +377,11 @@ $assert($log->table('cloud_vm_network_projection_outbox')
&& $main->table('cloud_abuse_projection_receipts')
->where('sample_key', $key2)->count() === 1
&& (int) $log->table('cloud_vm_network_baselines')
->where('instance_id', 2)->value('sample_count') === 1,
->where('instance_id', 2)->value('sample_count') === 1
&& (int) $main->table('cloud_abuse_projection_receipts')
->where('sample_key', $key2)->value('policy_revision') === 1
&& (int) $main->table('cloud_abuse_instance_states')
->where('instance_id', 2)->value('policy_revision') === 1,
'Receipt replay duplicated or failed to finalize projection.');
// A committed main receipt is authoritative even when the cross-database
@ -363,6 +446,7 @@ $claimReceiptKey = $insertSample(
);
$claimResetFindingKey = hash('sha256', implode("\n", [
13, CloudAbuseDetector::RULE_VERSION,
$policySnapshot->policyHash,
'outbound_tcp_syn_sweep', 'ipv4', 'outbound',
]));
$main->table('cloud_abuse_findings')->insert([
@ -445,7 +529,9 @@ $main->table('cloud_abuse_projection_receipts')->insert([
'sample_key' => $claimConflictKey,
'instance_id' => 14,
'rule_version' => CloudAbuseDetector::RULE_VERSION,
'bucket_start' => 301,
'policy_revision' => 2,
'policy_hash' => $changedPolicySnapshot->policyHash,
'bucket_start' => 300,
'projected_at' => $now,
]);
$log->table('cloud_vm_network_projection_outbox')
@ -538,6 +624,7 @@ $assert($deleted->state === 'discarded'
$addInstance(8, 88);
$oldOwnerFindingKey = hash('sha256', implode("\n", [
8, CloudAbuseDetector::RULE_VERSION,
$policySnapshot->policyHash,
'outbound_tcp_syn_sweep', 'ipv4', 'outbound',
]));
$main->table('cloud_abuse_instance_states')->insert([
@ -667,6 +754,7 @@ $gapBaseline['last_interface'] = $metrics()['interface'];
$insertBaseline(6, 66, $gapBaseline, 60, str_repeat('e', 64));
$findingKey = hash('sha256', implode("\n", [
6, CloudAbuseDetector::RULE_VERSION,
$policySnapshot->policyHash,
'outbound_tcp_syn_sweep', 'ipv4', 'outbound',
]));
$main->table('cloud_abuse_findings')->insert([
@ -719,6 +807,201 @@ $assert($main->table('cloud_abuse_findings')
->where('instance_id', 6)->value('risk_state') === 'normal',
'Panel-gap reset left an open finding after verified clear evidence.');
// A changed immutable detector policy keeps historical statistics/sample
// count, clears threshold streaks, and retires old findings explicitly rather
// than pretending that new evidence cleared them.
$addInstance(16, 166);
$history = CloudAbuseDetector::evaluate([
'bucket_start' => 60,
'complete' => true,
'quality_flags' => [],
'metrics' => $metrics(),
], CloudAbuseDetector::initialBaseline(), $defaultPolicy)['baseline'];
$history['sample_count'] = 60;
$history['rules']['ipv4:outbound_tcp_syn_sweep'] = [
'active' => true,
'severity' => 'high',
'suspicious_streak' => 0,
'high_streak' => 2,
'clear_streak' => 0,
'transition' => 'continued',
];
$statsBeforeSwitch = $history['stats'];
$insertBaseline(
16,
166,
$history,
60,
str_repeat('1', 64),
null,
0,
0,
$policySnapshot
);
$replacedFindingKey = hash('sha256', implode("\n", [
16,
CloudAbuseDetector::RULE_VERSION,
$policySnapshot->policyHash,
'outbound_tcp_syn_sweep',
'ipv4',
'outbound',
]));
$main->table('cloud_abuse_findings')->insert([
'finding_key' => $replacedFindingKey,
'instance_id' => 16, 'user_id' => 166, 'host_id' => 7,
'rule_code' => 'outbound_tcp_syn_sweep',
'rule_version' => CloudAbuseDetector::RULE_VERSION,
'policy_revision' => 1,
'policy_hash' => $policySnapshot->policyHash,
'direction' => 'outbound', 'ip_family' => 'ipv4',
'severity' => 'high', 'state' => 'open',
'first_seen_at' => 60, 'last_seen_at' => 60,
'consecutive_windows' => 2,
'evidence_json' => '{"tcp_syn_flows":5000}',
'resolved_at' => null, 'resolution_reason' => '',
'created_at' => $now, 'updated_at' => $now,
]);
$main->table('cloud_abuse_instance_states')->insert([
'instance_id' => 16, 'user_id' => 166, 'host_id' => 7,
'rule_version' => CloudAbuseDetector::RULE_VERSION,
'policy_revision' => 1,
'policy_hash' => $policySnapshot->policyHash,
'risk_state' => 'high_risk', 'collection_state' => 'healthy',
'latest_direction' => 'outbound',
'latest_rule_code' => 'outbound_tcp_syn_sweep',
'latest_severity' => 'high', 'summary_json' => '{}',
'last_projection_key' => str_repeat('1', 64),
'last_bucket_start' => 60, 'last_sample_at' => 120,
'last_evaluated_at' => $now,
'created_at' => $now, 'updated_at' => $now,
]);
$switchKey = $insertSample(
16,
166,
120,
$metrics(),
false,
['incomplete', 'sampling_gap'],
'pending',
null,
0,
0,
$changedPolicySnapshot
);
$assert($worker()->tick(), 'Changed-policy projection did not run.');
$switchedBaseline = json_decode((string) $log->table(
'cloud_vm_network_baselines'
)->where('instance_id', 16)->value('metrics_json'), true,
32, JSON_THROW_ON_ERROR);
$replacedFinding = $main->table('cloud_abuse_findings')
->where('finding_key', $replacedFindingKey)->first();
$assert($log->table('cloud_vm_network_projection_outbox')
->where('sample_key', $switchKey)->value('state') === 'done'
&& (int) $log->table('cloud_vm_network_baselines')
->where('instance_id', 16)->value('policy_revision') === 2
&& hash_equals(
$changedPolicySnapshot->policyHash,
(string) $log->table('cloud_vm_network_baselines')
->where('instance_id', 16)->value('policy_hash')
)
&& $switchedBaseline['sample_count'] === 60
&& $switchedBaseline['stats'] == $statsBeforeSwitch
&& $switchedBaseline['rules'] === []
&& $replacedFinding->state === 'resolved'
&& $replacedFinding->resolution_reason === 'policy_replaced'
&& $replacedFinding->evidence_json === '{"tcp_syn_flows":5000}'
&& $main->table('cloud_abuse_instance_states')
->where('instance_id', 16)->value('risk_state') === 'observing',
'Detector policy switch lost statistics or forged evidence recovery.');
// A newer revision with the same detector hash may carry unrelated delivery
// metadata in a later schema. It updates identity without clearing streaks.
$addInstance(17, 177);
$sameHashBaseline = CloudAbuseDetector::resetBaseline([[
'rule_code' => 'outbound_tcp_syn_sweep',
'direction' => 'outbound',
'ip_family' => 'ipv4',
'severity' => 'high',
]], $defaultPolicy);
$sameHashBaseline['sample_count'] = 60;
$sameHashBaseline['last_bucket_start'] = 60;
$sameHashBaseline['last_interface'] = $metrics()['interface'];
$sameHashBaseline['stats'] = $statsBeforeSwitch;
$insertBaseline(
17,
177,
$sameHashBaseline,
60,
str_repeat('2', 64),
null,
0,
0,
$policySnapshot
);
$sameHashFindingKey = hash('sha256', implode("\n", [
17,
CloudAbuseDetector::RULE_VERSION,
$policySnapshot->policyHash,
'outbound_tcp_syn_sweep',
'ipv4',
'outbound',
]));
$main->table('cloud_abuse_findings')->insert([
'finding_key' => $sameHashFindingKey,
'instance_id' => 17, 'user_id' => 177, 'host_id' => 7,
'rule_code' => 'outbound_tcp_syn_sweep',
'rule_version' => CloudAbuseDetector::RULE_VERSION,
'policy_revision' => 1,
'policy_hash' => $policySnapshot->policyHash,
'direction' => 'outbound', 'ip_family' => 'ipv4',
'severity' => 'high', 'state' => 'open',
'first_seen_at' => 60, 'last_seen_at' => 60,
'consecutive_windows' => 2, 'evidence_json' => '{}',
'resolved_at' => null, 'resolution_reason' => '',
'created_at' => $now, 'updated_at' => $now,
]);
$main->table('cloud_abuse_instance_states')->insert([
'instance_id' => 17, 'user_id' => 177, 'host_id' => 7,
'rule_version' => CloudAbuseDetector::RULE_VERSION,
'policy_revision' => 1,
'policy_hash' => $policySnapshot->policyHash,
'risk_state' => 'high_risk', 'collection_state' => 'healthy',
'latest_direction' => 'outbound',
'latest_rule_code' => 'outbound_tcp_syn_sweep',
'latest_severity' => 'high', 'summary_json' => '{}',
'last_projection_key' => str_repeat('2', 64),
'last_bucket_start' => 60, 'last_sample_at' => 120,
'last_evaluated_at' => $now,
'created_at' => $now, 'updated_at' => $now,
]);
$insertSample(
17,
177,
120,
$metrics(),
false,
['incomplete', 'sampling_gap'],
'pending',
null,
0,
0,
$sameDetectorSnapshot
);
$assert($worker()->tick(), 'Same-detector revision projection did not run.');
$sameHashStored = json_decode((string) $log->table(
'cloud_vm_network_baselines'
)->where('instance_id', 17)->value('metrics_json'), true,
32, JSON_THROW_ON_ERROR);
$assert((int) $log->table('cloud_vm_network_baselines')
->where('instance_id', 17)->value('policy_revision') === 3
&& ($sameHashStored['rules']['ipv4:outbound_tcp_syn_sweep']['active']
?? false) === true
&& $main->table('cloud_abuse_findings')
->where('finding_key', $sameHashFindingKey)->value('state') === 'open'
&& $main->table('cloud_abuse_instance_states')
->where('instance_id', 17)->value('risk_state') === 'high_risk',
'Same detector hash incorrectly cleared durable finding streaks.');
// Corrupt/non-canonical evidence has bounded retries and an explicit terminal
// state; it can neither leak into the main read model nor hold the queue.
$addInstance(7, 77);

@ -32,6 +32,10 @@ $mainSchema->create(
$table->string('sample_key')->primary();
$table->integer('instance_id');
$table->integer('rule_version');
$table->integer('policy_revision')->default(1);
$table->string('policy_hash')->default(
'511828bde950c504f92bb7a0603ca8b14453f575ead276c1889baa764f384dae'
);
$table->integer('bucket_start');
$table->integer('projected_at');
}
@ -45,6 +49,10 @@ $mainSchema->create('cloud_abuse_findings',
$table->integer('host_id');
$table->string('rule_code');
$table->integer('rule_version');
$table->integer('policy_revision')->default(1);
$table->string('policy_hash')->default(
'511828bde950c504f92bb7a0603ca8b14453f575ead276c1889baa764f384dae'
);
$table->string('direction');
$table->string('ip_family');
$table->string('severity');
@ -54,6 +62,7 @@ $mainSchema->create('cloud_abuse_findings',
$table->integer('consecutive_windows');
$table->text('evidence_json');
$table->integer('resolved_at')->nullable();
$table->string('resolution_reason')->default('');
$table->integer('created_at');
$table->integer('updated_at');
});
@ -65,6 +74,11 @@ $logSchema->create(
$table->string('sample_key')->primary();
$table->integer('instance_id');
$table->integer('bucket_start');
$table->integer('rule_version')->default(1);
$table->integer('policy_revision')->default(1);
$table->string('policy_hash')->default(
'511828bde950c504f92bb7a0603ca8b14453f575ead276c1889baa764f384dae'
);
}
);
$logSchema->create(
@ -82,6 +96,11 @@ $logSchema->create(
static function (Blueprint $table): void {
$table->integer('instance_id')->primary();
$table->integer('user_id');
$table->integer('rule_version')->default(1);
$table->integer('policy_revision')->default(1);
$table->string('policy_hash')->default(
'511828bde950c504f92bb7a0603ca8b14453f575ead276c1889baa764f384dae'
);
}
);
@ -94,8 +113,13 @@ $readCutoff = $now
- CloudAbuseRetentionService::READ_MODEL_RETENTION_SECONDS;
$oldBucket = intdiv($telemetryCutoff - 120, 60) * 60;
$recentBucket = intdiv($telemetryCutoff + 120, 60) * 60;
$defaultPolicyHash =
'511828bde950c504f92bb7a0603ca8b14453f575ead276c1889baa764f384dae';
$key = static fn (string $name): string => hash('sha256', $name);
$fixtureFindingKey = static fn (string $name): string => $key(
'finding-' . $defaultPolicyHash . '-' . $name
);
$sample = static function (
string $name,
int $instanceId,
@ -142,9 +166,9 @@ $finding = static function (
string $name,
string $state,
?int $resolvedAt
) use ($main, $key, $now): void {
) use ($main, $fixtureFindingKey, $now): void {
$main->table('cloud_abuse_findings')->insert([
'finding_key' => $key('finding-' . $name),
'finding_key' => $fixtureFindingKey($name),
'instance_id' => 1,
'user_id' => 1,
'host_id' => 1,
@ -234,7 +258,10 @@ $assert(!$main->table('cloud_abuse_projection_receipts')
'Receipt retention ignored age or retained-sample protection.');
$assert($main->table('cloud_abuse_findings')->count() === 2
&& !$main->table('cloud_abuse_findings')
->where('finding_key', $key('finding-resolved-old'))->exists(),
->where(
'finding_key',
$fixtureFindingKey('resolved-old')
)->exists(),
'Finding retention removed an open/recent finding or kept an old one.');
// A storage failure after terminal outbox deletion must roll the log
@ -275,7 +302,8 @@ $bounded = $service->prune(1);
$assert($bounded['findings'] === 1
&& $main->table('cloud_abuse_findings')
->whereIn('finding_key', [
$key('finding-batch-one'), $key('finding-batch-two'),
$fixtureFindingKey('batch-one'),
$fixtureFindingKey('batch-two'),
])->count() === 1,
'Retention batch size did not bound the indexed finding pass.');
$assert($service->prune() === [

@ -56,6 +56,11 @@ $mainSchema->create('cloud_abuse_instance_states',
$table->integer('instance_id')->primary();
$table->integer('user_id');
$table->integer('host_id');
$table->integer('rule_version')->default(1);
$table->integer('policy_revision')->default(1);
$table->string('policy_hash')->default(
'511828bde950c504f92bb7a0603ca8b14453f575ead276c1889baa764f384dae'
);
$table->string('risk_state');
$table->string('collection_state');
$table->string('latest_direction');
@ -76,6 +81,10 @@ $mainSchema->create('cloud_abuse_findings',
$table->integer('host_id');
$table->string('rule_code');
$table->integer('rule_version');
$table->integer('policy_revision')->default(1);
$table->string('policy_hash')->default(
'511828bde950c504f92bb7a0603ca8b14453f575ead276c1889baa764f384dae'
);
$table->string('direction');
$table->string('ip_family');
$table->string('severity');
@ -85,6 +94,7 @@ $mainSchema->create('cloud_abuse_findings',
$table->integer('consecutive_windows');
$table->text('evidence_json');
$table->integer('resolved_at')->nullable();
$table->string('resolution_reason')->default('');
$table->integer('created_at');
$table->integer('updated_at');
});
@ -110,6 +120,23 @@ $main = $capsule->connection('default');
$log = $capsule->connection('log');
$now = 2_000_000;
$actor = ['user_id' => 9, 'is_admin' => true];
$defaultPolicyHash =
'511828bde950c504f92bb7a0603ca8b14453f575ead276c1889baa764f384dae';
$findingKey = static function (
int $instanceId,
string $ruleCode,
string $family,
string $direction
) use ($defaultPolicyHash): string {
return hash('sha256', implode("\n", [
$instanceId,
1,
$defaultPolicyHash,
$ruleCode,
$family,
$direction,
]));
};
$main->table('user')->insert([
['id' => 11, 'user_name' => 'alpha'],
['id' => 12, 'user_name' => 'beta'],
@ -214,7 +241,9 @@ $main->table('cloud_abuse_instance_states')->insert([
'updated_at' => $now - 1],
]);
$main->table('cloud_abuse_findings')->insert([
'finding_key' => hash('sha256', 'finding'),
'finding_key' => $findingKey(
2, 'outbound_tcp_syn_sweep', 'ipv6', 'outbound'
),
'instance_id' => 2, 'user_id' => 12, 'host_id' => 1,
'rule_code' => 'outbound_tcp_syn_sweep', 'rule_version' => 1,
'direction' => 'outbound', 'ip_family' => 'ipv6',
@ -372,7 +401,9 @@ $assert(count($detail['series']) === 60
'Detail response leaked raw evidence or failed its bounded trend contract.');
$main->table('cloud_abuse_findings')->insert([
'finding_key' => hash('sha256', 'mixed-family-finding'),
'finding_key' => $findingKey(
1, 'outbound_udp_fanout', 'mixed', 'outbound'
),
'instance_id' => 1, 'user_id' => 11, 'host_id' => 1,
'rule_code' => 'outbound_udp_fanout', 'rule_version' => 1,
'direction' => 'outbound', 'ip_family' => 'mixed',
@ -397,7 +428,9 @@ $assert(array_column($mixedIpv4['items'], 'instance_id') === ['1']
$main->table('cloud_abuse_findings')->insert([
[
'finding_key' => hash('sha256', 'old-owner-finding'),
'finding_key' => $findingKey(
2, 'outbound_udp_fanout', 'ipv4', 'outbound'
),
'instance_id' => 2, 'user_id' => 11, 'host_id' => 1,
'rule_code' => 'outbound_udp_fanout', 'rule_version' => 1,
'direction' => 'outbound', 'ip_family' => 'ipv4',
@ -410,7 +443,9 @@ $main->table('cloud_abuse_findings')->insert([
'updated_at' => $now - 20,
],
[
'finding_key' => hash('sha256', 'oversized-evidence'),
'finding_key' => $findingKey(
2, 'inbound_tcp_port_scan', 'ipv4', 'inbound'
),
'instance_id' => 2, 'user_id' => 12, 'host_id' => 1,
'rule_code' => 'inbound_tcp_port_scan', 'rule_version' => 1,
'direction' => 'inbound', 'ip_family' => 'ipv4',

@ -64,7 +64,7 @@ foreach ([
'CloudSetup omits backup preparation contract: ' . $required);
}
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v43-abuse-policy'")
"SCHEMA_VERSION = 'go-agent-wss-v44-abuse-policy-projection'")
&& str_contains($setup,
"'columns' => [\n 'create_request_actor_id',\n 'create_request_idempotency_key',")
&& str_contains($setup, "'non_unique' => 0"),

@ -27,10 +27,10 @@ $assert(stripos($schema, 'compatibility import') === false
$assert(!preg_match('/\bengine_[A-Za-z0-9_]*\b/', $schema),
'cloud.sql retained an engine-prefixed identifier.');
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v43-abuse-policy'")
"SCHEMA_VERSION = 'go-agent-wss-v44-abuse-policy-projection'")
&& !str_contains($setup, "SCHEMA_VERSION = 'go-v1'")
&& str_contains($deployment, '`go-agent-wss-v43-abuse-policy`')
&& str_contains($compensation, '`go-agent-wss-v43-abuse-policy`'),
&& str_contains($deployment, '`go-agent-wss-v44-abuse-policy-projection`')
&& str_contains($compensation, '`go-agent-wss-v44-abuse-policy-projection`'),
'Outbound Agent schema must reject the incompatible earlier Go database.');
$assert(str_contains($schema,
"timezone VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'UTC'")

@ -56,7 +56,7 @@ foreach ([
}
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v43-abuse-policy'"),
"SCHEMA_VERSION = 'go-agent-wss-v44-abuse-policy-projection'"),
'CloudSetup schema version was not advanced for maintenance notices.');
echo "Maintenance notice schema contracts passed.\n";

@ -53,7 +53,7 @@ $assert(is_string($schema)
'Public-pool replacement lacks durable batch, child, or claim schema.');
$assert(is_string($setup)
&& str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v43-abuse-policy'")
"SCHEMA_VERSION = 'go-agent-wss-v44-abuse-policy-projection'")
&& str_contains($setup,
"'cloud_public_ip_pool_replacement_batches'")
&& str_contains($setup,

@ -170,7 +170,7 @@ $assert(str_contains($setup,
'CloudSetup does not verify index uniqueness and ordered columns.');
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v43-abuse-policy'")
"SCHEMA_VERSION = 'go-agent-wss-v44-abuse-policy-projection'")
&& str_contains($setup, "'cloud_public_network_transitions'")
&& str_contains($setup, "'cloud_public_network_transition_ips'")
&& str_contains($setup, "'reservation_ip_claim_id'")

@ -57,7 +57,7 @@ $assert($replica !== ''
&& str_contains($replica, 'cleanup_resolved_at BIGINT UNSIGNED'),
'Rescue replica cannot prove exact identity or retain cleanup debt.');
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v43-abuse-policy'")
"SCHEMA_VERSION = 'go-agent-wss-v44-abuse-policy-projection'")
&& str_contains($setup, "'cloud_rescue_artifact_delete_due'")
&& str_contains($setup, "'cloud_rescue_replica_cleanup'"),
'CloudSetup version or rescue catalog indexes are incomplete.');

@ -218,7 +218,7 @@ foreach ([
'CloudSetup omits restore/migration ledger contract: ' . $required);
}
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v43-abuse-policy'")
"SCHEMA_VERSION = 'go-agent-wss-v44-abuse-policy-projection'")
&& str_contains($attemptModel,
"protected \$table = 'cloud_backup_restore_attempts';")
&& str_contains($attemptModel, "'active_attempt' => 'int'")

@ -105,7 +105,7 @@ $assert(str_contains($setup,
'CloudSetup lost the open-compensation fence index contract.');
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v43-abuse-policy'"),
"SCHEMA_VERSION = 'go-agent-wss-v44-abuse-policy-projection'"),
'CloudSetup schema version was not advanced for service compensation.');
$appliedShape = preg_match(

@ -138,7 +138,7 @@ $assert(is_string($publicationModel)
&& str_contains($artifactModel, "'maximum_size_bytes' => 'int'"),
'Storage publication/artifact model contracts are incomplete.');
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v43-abuse-policy'"),
"SCHEMA_VERSION = 'go-agent-wss-v44-abuse-policy-projection'"),
'CloudSetup schema version was not advanced for durable publications.');
echo "Storage publication schema contracts passed.\n";

@ -63,7 +63,7 @@ $assert(str_contains($schema,
&& str_contains($schema,
'CONSTRAINT chk_cloud_template_import_completion CHECK (')
&& str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v43-abuse-policy'"),
"SCHEMA_VERSION = 'go-agent-wss-v44-abuse-policy-projection'"),
'Template imports are missing durable identity or terminal-state constraints.');
$assert(str_contains($routes, "'/admin/cloud-template-imports'")
&& str_contains($routes, "'/admin/cloud-template-imports/{id}'")

@ -71,7 +71,7 @@ $assert(str_contains($schema,
'Service-period traffic-pool identity is not frozen or indexed.');
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v43-abuse-policy'")
"SCHEMA_VERSION = 'go-agent-wss-v44-abuse-policy-projection'")
&& str_contains($setup, "'chk_cloud_traffic_entitlement_balance'")
&& str_contains($setup, "'cloud_traffic_ledger_sequence'"),
'CloudSetup schema version or named-object validation is incomplete.');

@ -101,7 +101,7 @@ $assert(str_contains($jobs, 'vpc_id BIGINT UNSIGNED DEFAULT NULL')
'Cloud jobs cannot be correlated to VPC or VPC-port work.');
$assert(str_contains($setup,
"private const SCHEMA_VERSION = 'go-agent-wss-v43-abuse-policy'")
"private const SCHEMA_VERSION = 'go-agent-wss-v44-abuse-policy-projection'")
&& str_contains($setup, "'name' => 'cloud_vpc_enabled'")
&& str_contains($setup, "'value' => 'false'")
&& str_contains($setup,

@ -50,6 +50,11 @@ $logSchema->create(
static function (Blueprint $table) {
$table->string('sample_key')->primary();
$table->string('state');
$table->integer('rule_version')->default(1);
$table->integer('policy_revision')->default(1);
$table->string('policy_hash')->default(
'511828bde950c504f92bb7a0603ca8b14453f575ead276c1889baa764f384dae'
);
$table->foreign('sample_key')
->references('sample_key')
->on('cloud_vm_network_observation_samples')
@ -58,6 +63,11 @@ $logSchema->create(
);
$logSchema->create('cloud_vm_network_baselines', static function (Blueprint $table) {
$table->integer('instance_id')->primary(); $table->integer('user_id');
$table->integer('rule_version')->default(1);
$table->integer('policy_revision')->default(1);
$table->string('policy_hash')->default(
'511828bde950c504f92bb7a0603ca8b14453f575ead276c1889baa764f384dae'
);
});
$schema->create('user', static function (Blueprint $table) {
$table->increments('id');

@ -104,7 +104,7 @@ foreach ($models as $class => $modelTable) {
}
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v43-abuse-policy'")
"SCHEMA_VERSION = 'go-agent-wss-v44-abuse-policy-projection'")
&& str_contains($setup,
"self::schema('ss.sql') . \"\\0\" . self::schema('cloud.sql')")
&& str_contains($setup,

Loading…
Cancel
Save