|
|
|
|
@ -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';
|
|
|
|
|
}
|
|
|
|
|
|