diff --git a/web/sql/cloud.sql b/web/sql/cloud.sql index 31ea5463..81ca2836 100644 --- a/web/sql/cloud.sql +++ b/web/sql/cloud.sql @@ -3060,6 +3060,270 @@ INSERT INTO cloud_abuse_policy_rules ( '{"udp_active_flows":5000,"udp_new_flows":1000}', '{"udp_active_flows":25000,"udp_new_flows":5000}', 0); +-- Compact instance-centric network-abuse ledger. Agent UUIDs and cursor +-- window identities are transport inputs only; after a window is mapped to a +-- logical instance they are never retained per sample. The host cursor, every +-- affected instance state, its sparse finding transitions and the advanced +-- cursor commit in one main-database transaction. +CREATE TABLE cloud_network_monitor_hosts ( + host_id BIGINT UNSIGNED NOT NULL PRIMARY KEY, + collector_generation CHAR(32) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL, + cursor_value BIGINT UNSIGNED NOT NULL DEFAULT 0, + oldest_cursor BIGINT UNSIGNED NOT NULL DEFAULT 0, + gap_before TINYINT UNSIGNED NOT NULL DEFAULT 0, + poll_revision BIGINT UNSIGNED NOT NULL DEFAULT 0, + next_poll_at BIGINT UNSIGNED NOT NULL DEFAULT 0, + poll_lease_token CHAR(32) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL, + poll_lease_until BIGINT UNSIGNED NOT NULL DEFAULT 0, + collection_state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'unknown', + consecutive_failures INT UNSIGNED NOT NULL DEFAULT 0, + last_window_start BIGINT UNSIGNED DEFAULT NULL, + last_success_at BIGINT UNSIGNED DEFAULT NULL, + last_error_code VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '', + updated_at BIGINT UNSIGNED NOT NULL, + KEY cloud_network_monitor_host_due ( + next_poll_at, poll_revision, host_id + ), + KEY cloud_network_monitor_host_health ( + collection_state, last_success_at, host_id + ), + KEY cloud_network_monitor_host_lease (poll_lease_until, host_id), + CONSTRAINT chk_cloud_network_monitor_host_state CHECK ( + collection_state IN ('unknown','healthy','degraded','stale','unavailable') + AND gap_before IN (0,1) + ), + CONSTRAINT chk_cloud_network_monitor_host_generation CHECK ( + collector_generation IS NULL + OR collector_generation REGEXP '^[a-f0-9]{32}$' + ), + CONSTRAINT chk_cloud_network_monitor_host_lease CHECK ( + (poll_lease_token IS NULL AND poll_lease_until = 0) + OR (poll_lease_token IS NOT NULL + AND poll_lease_token REGEXP '^[a-f0-9]{32}$' + AND poll_lease_until > 0) + ) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- Exactly one mutable detector state per logical instance. detector_state_json +-- holds only the compact statistical baseline; latest_metrics_json is the +-- current allowlisted aggregate shown to administrators. current_day_json is +-- rolled into one durable daily row when the UTC day changes. +CREATE TABLE cloud_network_instance_ledger ( + instance_id BIGINT UNSIGNED NOT NULL PRIMARY KEY, + policy_revision BIGINT UNSIGNED 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', + latest_rule_code VARCHAR(48) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '', + latest_severity VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'none', + detector_state_json JSON NOT NULL, + latest_metrics_json JSON NOT NULL, + current_day_start BIGINT UNSIGNED NOT NULL, + current_day_json JSON NOT NULL, + last_bucket_start BIGINT UNSIGNED DEFAULT NULL, + last_sample_at BIGINT UNSIGNED DEFAULT NULL, + last_evaluated_at BIGINT UNSIGNED DEFAULT NULL, + created_at BIGINT UNSIGNED NOT NULL, + updated_at BIGINT UNSIGNED NOT NULL, + KEY cloud_network_ledger_risk ( + risk_state, updated_at, instance_id + ), + KEY cloud_network_ledger_collection ( + collection_state, last_sample_at, instance_id + ), + CONSTRAINT chk_cloud_network_ledger_risk CHECK ( + risk_state IN ('observing','normal','suspicious','high_risk') + ), + CONSTRAINT chk_cloud_network_ledger_collection CHECK ( + collection_state IN ('unknown','healthy','degraded','stale','unavailable') + ), + CONSTRAINT chk_cloud_network_ledger_direction CHECK ( + latest_direction IN ('none','inbound','outbound','both') + ), + CONSTRAINT chk_cloud_network_ledger_severity CHECK ( + latest_severity IN ('none','info','suspicious','high') + ), + CONSTRAINT chk_cloud_network_ledger_window CHECK ( + policy_revision > 0 AND current_day_start % 86400 = 0 + AND ( + (last_bucket_start IS NULL AND last_sample_at IS NULL) + OR (last_bucket_start IS NOT NULL + AND last_bucket_start % 60 = 0 + AND last_sample_at = last_bucket_start + 60) + ) + ), + CONSTRAINT fk_cloud_network_ledger_instance FOREIGN KEY (instance_id) + REFERENCES cloud_instances (id) ON DELETE CASCADE ON UPDATE RESTRICT +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- One bounded numeric history row per logical instance and UTC day. Minute +-- samples update only cloud_network_instance_ledger; this table receives one +-- insert when an instance crosses into the next UTC day. +CREATE TABLE cloud_network_daily_summaries ( + instance_id BIGINT UNSIGNED NOT NULL, + day_start BIGINT UNSIGNED NOT NULL, + sample_count INT UNSIGNED NOT NULL, + complete_count INT UNSIGNED NOT NULL, + quality_mask INT UNSIGNED NOT NULL DEFAULT 0, + rx_bps_peak BIGINT UNSIGNED NOT NULL DEFAULT 0, + tx_bps_peak BIGINT UNSIGNED NOT NULL DEFAULT 0, + rx_pps_peak BIGINT UNSIGNED NOT NULL DEFAULT 0, + tx_pps_peak BIGINT UNSIGNED NOT NULL DEFAULT 0, + tcp_active_peak BIGINT UNSIGNED NOT NULL DEFAULT 0, + tcp_new_peak BIGINT UNSIGNED NOT NULL DEFAULT 0, + tcp_syn_peak BIGINT UNSIGNED NOT NULL DEFAULT 0, + tcp_unreplied_peak BIGINT UNSIGNED NOT NULL DEFAULT 0, + udp_active_peak BIGINT UNSIGNED NOT NULL DEFAULT 0, + udp_new_peak BIGINT UNSIGNED NOT NULL DEFAULT 0, + peer_ip_peak BIGINT UNSIGNED NOT NULL DEFAULT 0, + peer_port_peak BIGINT UNSIGNED NOT NULL DEFAULT 0, + first_sample_at BIGINT UNSIGNED NOT NULL, + last_sample_at BIGINT UNSIGNED NOT NULL, + created_at BIGINT UNSIGNED NOT NULL, + updated_at BIGINT UNSIGNED NOT NULL, + PRIMARY KEY (instance_id, day_start), + KEY cloud_network_daily_retention (day_start, instance_id), + CONSTRAINT chk_cloud_network_daily_window CHECK ( + day_start % 86400 = 0 + AND sample_count > 0 AND complete_count <= sample_count + AND first_sample_at >= day_start + AND last_sample_at >= first_sample_at + AND last_sample_at < day_start + 86400 + ), + CONSTRAINT fk_cloud_network_daily_instance FOREIGN KEY (instance_id) + REFERENCES cloud_instances (id) ON DELETE CASCADE ON UPDATE RESTRICT +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- Sparse mutable finding state. A row is reused while one rule/family scope is +-- open; occurrence_generation advances only after a resolved finding opens +-- again. No per-minute identity is retained. +CREATE TABLE cloud_network_findings ( + id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, + instance_id BIGINT UNSIGNED NOT NULL, + policy_revision BIGINT UNSIGNED NOT NULL, + rule_code VARCHAR(48) 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, + state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + first_seen_at BIGINT UNSIGNED NOT NULL, + last_seen_at BIGINT UNSIGNED NOT NULL, + consecutive_windows INT UNSIGNED NOT NULL DEFAULT 1, + occurrence_generation INT UNSIGNED NOT NULL DEFAULT 1, + transition_revision 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_network_finding_scope ( + instance_id, policy_revision, rule_code, direction, ip_family + ), + KEY cloud_network_finding_state ( + state, last_seen_at, instance_id, id + ), + KEY cloud_network_finding_retention ( + state, resolved_at, id + ), + CONSTRAINT chk_cloud_network_finding_scope CHECK ( + policy_revision > 0 + AND direction IN ('inbound','outbound') + AND ip_family IN ('ipv4','ipv6','mixed') + ), + CONSTRAINT chk_cloud_network_finding_state CHECK ( + severity IN ('info','suspicious','high') + AND state IN ('open','resolved') + AND first_seen_at <= last_seen_at + AND consecutive_windows >= 1 + AND occurrence_generation >= 1 + AND transition_revision >= 1 + ), + CONSTRAINT chk_cloud_network_finding_resolution CHECK ( + (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 resolution_reason IN ( + 'evidence_clear','policy_replaced','owner_changed' + )) + ), + CONSTRAINT fk_cloud_network_finding_instance FOREIGN KEY (instance_id) + REFERENCES cloud_instances (id) ON DELETE CASCADE ON UPDATE RESTRICT +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- One immutable event per finding transition. The finding revision is the +-- idempotency fence, so no sample/event/finding hashes are necessary. +CREATE TABLE cloud_network_alert_events ( + id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, + finding_id BIGINT UNSIGNED NOT NULL, + transition_revision INT UNSIGNED NOT NULL, + transition_type VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + from_severity VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + to_severity VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + evidence_json JSON NOT NULL, + resolution_reason VARCHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '', + occurred_at BIGINT UNSIGNED NOT NULL, + created_at BIGINT UNSIGNED NOT NULL, + UNIQUE KEY cloud_network_alert_finding_revision ( + finding_id, transition_revision + ), + KEY cloud_network_alert_retention (created_at, id), + CONSTRAINT chk_cloud_network_alert_transition CHECK ( + (transition_type = 'opened' + AND from_severity = 'none' + AND to_severity IN ('suspicious','high') + AND resolution_reason = '') + OR (transition_type = 'escalated' + AND from_severity = 'suspicious' AND to_severity = 'high' + AND resolution_reason = '') + OR (transition_type = 'resolved' + AND from_severity IN ('suspicious','high') + AND to_severity = 'none' + AND resolution_reason IN ('evidence_clear','policy_replaced')) + ), + CONSTRAINT fk_cloud_network_alert_finding FOREIGN KEY (finding_id) + REFERENCES cloud_network_findings (id) + ON DELETE CASCADE ON UPDATE RESTRICT +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- External Telegram delivery keeps its conservative unknown-effect contract. +-- This sparse safety ledger is intentionally not folded into the high-rate +-- instance state. +CREATE TABLE cloud_network_alert_deliveries ( + id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, + event_id BIGINT UNSIGNED NOT NULL, + binding_generation CHAR(32) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL, + bound_admin_id BIGINT UNSIGNED DEFAULT NULL, + chat_id VARCHAR(20) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL, + telegram_message_id BIGINT UNSIGNED DEFAULT NULL, + state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + message_text TEXT NOT NULL, + content_hash CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + error_code VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '', + skip_reason VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '', + dispatch_started_at BIGINT UNSIGNED DEFAULT NULL, + sent_at BIGINT UNSIGNED DEFAULT NULL, + terminal_at BIGINT UNSIGNED DEFAULT NULL, + destination_redacted_at BIGINT UNSIGNED DEFAULT NULL, + created_at BIGINT UNSIGNED NOT NULL, + updated_at BIGINT UNSIGNED NOT NULL, + UNIQUE KEY cloud_network_alert_delivery_event (event_id), + UNIQUE KEY cloud_network_alert_delivery_message ( + binding_generation, chat_id, telegram_message_id + ), + KEY cloud_network_alert_delivery_due (state, updated_at, id), + KEY cloud_network_alert_delivery_terminal (terminal_at, state, id), + CONSTRAINT chk_cloud_network_alert_delivery_state CHECK ( + state IN ('pending','dispatching','sent','failed','unknown','skipped') + ), + CONSTRAINT chk_cloud_network_alert_delivery_content CHECK ( + CHAR_LENGTH(message_text) BETWEEN 1 AND 4096 + AND content_hash REGEXP '^[a-f0-9]{64}$' + ), + CONSTRAINT fk_cloud_network_alert_delivery_event FOREIGN KEY (event_id) + REFERENCES cloud_network_alert_events (id) + ON DELETE CASCADE ON UPDATE RESTRICT +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + -- Current read model for the observe-only abuse monitor. These rows never -- participate in instance lifecycle, billing, traffic-pool accounting or -- operation locks; stale/degraded collection is an observation, not a reason diff --git a/web/tests/CloudCompactAbuseSchemaContractTest.php b/web/tests/CloudCompactAbuseSchemaContractTest.php new file mode 100644 index 00000000..2db040d5 --- /dev/null +++ b/web/tests/CloudCompactAbuseSchemaContractTest.php @@ -0,0 +1,70 @@ +