diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ee79711..923072a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2682,6 +2682,38 @@ schemas and the `paramify` CLI — not the internal code. ### Fixed +- **Four Azure monitoring fetchers give null counts, not 0 or false, when a + list could not be read.** The run still exits 1 as `partial_failure`. + - `azure_activity_log_alerts` 0.4.0: when the alert rule list is not read, + every alert count, both coverage maps and every coverage verdict + (`all_monitored_operations_covered`, `service_health_alert_configured`, + `all_change_operations_covered`) are null. Before they read 0 and false. + The two operation totals stay 12 and 4. When the processing rule list is + not read, `suppressed` and `reaches_human` are null per alert, and + `alerts_suppressed`, `alerts_possibly_suppressed`, `alerts_reaching_human`, + the change coverage fields and `monitored_operation_coverage_reaching_human` + are null. Before they read as if no rule suppressed anything. An enabled + alert with no deliverable action group of its own is unknown too, since a + processing rule may add one: `routes_to_enabled_receiver` and + `routes_to_person` read null, not false, so `reaches_human` is null and the + alert counts in `alerts_routing_unknown` and `alerts_reaching_human_unknown`. + `processing_rule_matches` and `suppressed_by_processing_rules` are null, not + `[]`. + - `azure_diagnostic_settings` 0.2.0: when the settings are not read, + `activity_log_exported`, the counts, the coverage map and the required + category verdicts are null. Before, `activity_log_exported` read false. + - `azure_resource_diagnostic_settings` 0.5.0: when Resource Graph is not + read, the resource, coverage and ingestion counts and verdicts, the + destination counts, `in_scope_resources` and `excluded_resources` are null. + Before they read 0. + - `azure_log_analytics_workspaces` 0.4.0: when the workspace list is not + read, every count, `in_scope_workspaces` and `excluded_workspaces` are + null. A workspace whose tables or data exports are not read makes + `total_tables`, `min_table_total_retention_in_days` and the data export + counts null. `sentinel_onboarded_workspaces` is null while any workspace's + solutions are unread, and `sentinel_onboarded` is null while any are unread + and none is known to have Sentinel; before, it read false. + - **Disk, Key Vault, storage and SQL Managed Instance fetchers: an unread list gives null counts, not 0.** When a list the run needs is refused (403), throttled or times out, every total, count and percentage built from it is diff --git a/fetchers/azure/_shared/alert_routing.py b/fetchers/azure/_shared/alert_routing.py index a6e14cfc..65d1be14 100644 --- a/fetchers/azure/_shared/alert_routing.py +++ b/fetchers/azure/_shared/alert_routing.py @@ -195,9 +195,11 @@ def _tri(groups: list[dict], has: Callable[[dict], bool]) -> Optional[bool]: return False -def route(enabled: bool, group_ids: list[str], alert_scopes: list[str], groups: dict, rules: list[dict]) -> dict: - """Resolve an alert rule's notification path; `groups` is keyed by lower-cased action group id.""" - matches = matching_processing_rules(alert_scopes, rules) +def route(enabled: bool, group_ids: list[str], alert_scopes: list[str], groups: dict, rules: list[dict] | None) -> dict: + """Resolve an alert rule's notification path; `groups` is keyed by lower-cased action group id. + + rules=None means the processing rule list was not read, so whether anything suppresses the alert is unknown.""" + matches = matching_processing_rules(alert_scopes, rules or []) own = {} for gid in group_ids: if gid: @@ -224,8 +226,14 @@ def route(enabled: bool, group_ids: list[str], alert_scopes: list[str], groups: }) routes = _tri(resolved, lambda g: g["deliverable"]) if enabled else False to_person = _tri(resolved, lambda g: g["notifies_people"]) if enabled else False + if rules is None and enabled: + # A processing rule may add a group, so a "no" from the own groups is unknown. + routes = None if routes is False else routes + to_person = None if to_person is False else to_person suppressors = [m for m in matches if m["rule"]["suppresses_notifications"]] - if any(m["applies_to_every_fire"] for m in suppressors): + if rules is None and enabled: + suppressed = None + elif any(m["applies_to_every_fire"] for m in suppressors): suppressed = True elif suppressors: suppressed = None @@ -242,11 +250,11 @@ def route(enabled: bool, group_ids: list[str], alert_scopes: list[str], groups: "unresolved_action_group_ids": [g["id"] for g in resolved if not g["found"]], "routes_to_enabled_receiver": routes, "routes_to_person": to_person, - "processing_rule_matches": [ + "processing_rule_matches": None if rules is None else [ {"id": m["rule"]["id"], "scope_match": m["scope_match"], "applies_to_every_fire": m["applies_to_every_fire"]} for m in matches ], - "suppressed_by_processing_rules": [m["rule"]["id"] for m in suppressors], + "suppressed_by_processing_rules": None if rules is None else [m["rule"]["id"] for m in suppressors], "suppressed": suppressed, "reaches_human": reaches, } diff --git a/fetchers/azure/activity_log_alerts/fetcher.py b/fetchers/azure/activity_log_alerts/fetcher.py index 6ac9cf5f..1b2e492a 100644 --- a/fetchers/azure/activity_log_alerts/fetcher.py +++ b/fetchers/azure/activity_log_alerts/fetcher.py @@ -248,12 +248,45 @@ def operation_coverage(alerts: list[dict], operations_map=MONITORED_OPERATIONS) } -def with_routing(alert: dict, groups: dict, processing_rules: list[dict]) -> dict: +def with_routing(alert: dict, groups: dict, processing_rules: list[dict] | None) -> dict: return {**alert, **route(alert["enabled"], alert["action_groups"], alert["scopes"], groups, processing_rules)} -def summarize(alerts: list[dict]) -> dict: - """Coverage of the monitored operations is the headline, not the alert count.""" +ALERT_FIELDS = ( + "total_activity_log_alerts", "enabled_alerts", "disabled_alerts", "alerts_with_action_groups", + "alerts_without_action_groups", "monitored_operation_coverage", "monitored_operations_covered", + "monitored_operation_percentage", "all_monitored_operations_covered", + "uncovered_monitored_operations", "service_health_alert_configured", + "alerts_routing_to_enabled_receiver", "alerts_routing_unknown", "alerts_suppressed", + "alerts_possibly_suppressed", "alerts_reaching_human", "alerts_reaching_human_unknown", + "monitored_operation_coverage_reaching_human", "change_operation_coverage", + "change_operations_covered", "change_operations_possibly_covered", + "all_change_operations_covered", "uncovered_change_operations", +) + + +SUPPRESSION_FIELDS = ( + "alerts_suppressed", "alerts_possibly_suppressed", "alerts_reaching_human", + "monitored_operation_coverage_reaching_human", "change_operation_coverage", "change_operations_covered", + "change_operations_possibly_covered", "all_change_operations_covered", "uncovered_change_operations", +) + + +def summarize(alerts: list[dict] | None, processing_rules_read: bool = True) -> dict: + """Coverage of the monitored operations is the headline, not the alert count. + + None (the alert list was not read) gives null for every count, coverage map and verdict built from it. + processing_rules_read=False (that list was not read) gives null for what depends on suppression.""" + if alerts is None: + return {**dict.fromkeys(ALERT_FIELDS), "monitored_operations_total": len(MONITORED_OPERATIONS), + "change_operations_total": len(CHANGE_OPERATIONS)} + summary = alert_summary(alerts) + if not processing_rules_read: + summary.update(dict.fromkeys(SUPPRESSION_FIELDS)) + return summary + + +def alert_summary(alerts: list[dict]) -> dict: coverage = operation_coverage(alerts) covered = sum(1 for is_covered in coverage.values() if is_covered) total_monitored = len(MONITORED_OPERATIONS) @@ -350,6 +383,7 @@ def main() -> int: cred = collector.guard("azure.identity.DefaultAzureCredential", credential) alerts: list[dict] = [] + alerts_read = False group_records: list[dict] = [] processing_rules: list[dict] = [] exclusions = ResourceExclusions.from_env() @@ -370,7 +404,7 @@ def main() -> int: subscription_id, ) listed = collect_activity_log_alerts(subscription_id, cred, collector) - read["activity_log_alerts"] = listed is not None + read["activity_log_alerts"] = alerts_read = listed is not None alerts, excluded = exclusions.split(listed or [], "activity_log_alert") alerts_mgmt = provider_registration_status(collector, subscription_id, cred, "Microsoft.AlertsManagement") rest = collector.guard("arm_rest.ARMPipelineClient (init)", lambda: arm_rest_client(cred)) @@ -403,7 +437,7 @@ def main() -> int: outside_scope = sorted({g for g in referenced if not g.lower().startswith(prefix)}) referenced = [g for g in referenced if g.lower().startswith(prefix)] add_referenced_groups(rest, referenced, by_id, collector) - alerts = [with_routing(a, by_id, processing_rules) for a in alerts] + alerts = [with_routing(a, by_id, processing_rules if read["processing_rules"] else None) for a in alerts] processing_rules = in_scope_processing elif not subscription_id: collector.record( @@ -448,7 +482,7 @@ def main() -> int: "excluded": excluded, }, summary={ - **summarize(alerts), + **summarize(alerts if alerts_read else None, read["processing_rules"]), "provider_registration_status": registration, **counts, }, diff --git a/fetchers/azure/activity_log_alerts/fetcher.yaml b/fetchers/azure/activity_log_alerts/fetcher.yaml index 26b0688f..a370cabf 100644 --- a/fetchers/azure/activity_log_alerts/fetcher.yaml +++ b/fetchers/azure/activity_log_alerts/fetcher.yaml @@ -1,5 +1,5 @@ name: azure_activity_log_alerts -version: 0.3.1 +version: 0.4.0 description: >- Shows which security-relevant control-plane operations and App Service changes an enabled activity log alert covers, and whether those alerts reach a person. category: azure @@ -89,12 +89,12 @@ evidence_set: - excluded_activity_log_alerts — Number of activity log alert rules left out of scope because a pattern matched their resource group, region (they carry the region global) or id; each is listed under excluded with the pattern, and no other count includes it; null when the rule list could not be read - excluded_action_groups — Number of action groups left out of scope because a pattern matched their resource group or id (they carry no region, so location: patterns never match them); routing is still worked out against them; null when the action group list could not be read - excluded_processing_rules — Number of alert processing rules left out of scope because a pattern matched their resource group or id (they carry no region); routing is still worked out against them; null when the processing rule list could not be read - - in_scope_activity_log_alerts — Number of activity log alert rules read and in scope; 0 means none was read or the patterns took out every rule, which a check can catch by requiring more than 0; null when the rule list could not be read + - in_scope_activity_log_alerts — Number of activity log alert rules read and in scope; 0 means the patterns took out every rule or none exists, which a check can catch by requiring more than 0; null when the rule list could not be read - in_scope_action_groups — Number of action groups read and in scope; null when the action group list could not be read - in_scope_processing_rules — Number of alert processing rules read and in scope; null when the processing rule list could not be read What activity log alerts exist? - - total_activity_log_alerts — Number of in-scope activity log alert rules + - total_activity_log_alerts — Number of in-scope activity log alert rules; null when the alert rule list could not be read, as is every field in the next three sections (provider_registration_status still reads, and the two totals of operations stay 12 and 4) - enabled_alerts — Number of enabled alert rules - disabled_alerts — Number of disabled alert rules, which never fire; a rule Azure reports with no enabled value counts here - alerts_with_action_groups — Number of alert rules with at least one action group of their own @@ -124,12 +124,12 @@ evidence_set: Do the alerts reach a person? - alerts_routing_to_enabled_receiver — Number of enabled alerts that always notify an action group that is enabled and has at least one enabled receiver (email and SMS receivers count only when their status is Enabled, so Disabled and NotSpecified send nothing) - - alerts_routing_unknown — Number of enabled alerts where it's unknown whether they notify anyone, because an action group couldn't be read or a deliverable one is added only some of the time - - alerts_suppressed — Number of enabled alerts whose notifications an active suppression rule always switches off: one covering the alert's whole scope, with no conditions, and either no schedule or a one-off schedule window that is in force now - - alerts_possibly_suppressed — Number of enabled alerts that a suppression rule might switch off: it covers only part of the alert's scope, has conditions, or has a schedule that can't be evaluated - - alerts_reaching_human — Number of enabled alerts that notify a person (by email, SMS, voice call, app push, Azure role or ITSM ticket) and aren't suppressed - - alerts_reaching_human_unknown — Number of enabled alerts where it's unknown whether they reach a person: they notify only code (webhooks, Logic Apps, Functions, runbooks or Event Hubs), an action group couldn't be read, or a suppression rule might apply - - monitored_operation_coverage_reaching_human — For each operation in monitored_operation_coverage, true if an alert that reaches a person fires on it; null if only an alert that might reach a person does; false otherwise: + - alerts_routing_unknown — Number of enabled alerts where it's unknown whether they notify anyone, because an action group couldn't be read, a deliverable one is added only some of the time, or the processing rule list could not be read and the alert has no deliverable action group of its own + - alerts_suppressed — Number of enabled alerts whose notifications an active suppression rule always switches off: one covering the alert's whole scope, with no conditions, and either no schedule or a one-off schedule window that is in force now; null when the alert processing rule list could not be read + - alerts_possibly_suppressed — Number of enabled alerts that a suppression rule might switch off: it covers only part of the alert's scope, has conditions, or has a schedule that can't be evaluated; null when the alert processing rule list could not be read + - alerts_reaching_human — Number of enabled alerts that notify a person (by email, SMS, voice call, app push, Azure role or ITSM ticket) and aren't suppressed; null when the alert processing rule list could not be read + - alerts_reaching_human_unknown — Number of enabled alerts where it's unknown whether they reach a person: they notify only code (webhooks, Logic Apps, Functions, runbooks or Event Hubs), an action group couldn't be read, a suppression rule might apply, or the processing rule list could not be read and the alert has no deliverable action group of its own + - monitored_operation_coverage_reaching_human — For each operation in monitored_operation_coverage, true if an alert that reaches a person fires on it; null if only an alert that might reach a person does; false otherwise; null when the alert processing rule list could not be read: - create_update_network_security_group (same operations as monitored_operation_coverage) - delete_network_security_group (same) - create_update_network_security_group_rule (same) @@ -144,13 +144,13 @@ evidence_set: - delete_security_solution (same) Does a person get alerted to App Service changes? - - change_operation_coverage — For each App Service change, on apps and on deployment slots, true if an alert that reaches a person fires on it; null if only an alert that might reach a person does; false otherwise: + - change_operation_coverage — For each App Service change, on apps and on deployment slots, true if an alert that reaches a person fires on it; null if only an alert that might reach a person does; false otherwise; null when the alert processing rule list could not be read: - create_update_web_app (Microsoft.Web/sites/write, and the slot equivalent) - update_web_app_configuration (sites/config/write, and the slot equivalent) - deploy_web_app (sites/deployments/write, and the slot equivalent) - swap_web_app_slots (sites/slotsswap/action, and the slot equivalent) - - change_operations_covered — Number of App Service changes marked true in change_operation_coverage - - change_operations_possibly_covered — Number of App Service changes marked null in change_operation_coverage + - change_operations_covered — Number of App Service changes marked true in change_operation_coverage; null when the alert processing rule list could not be read + - change_operations_possibly_covered — Number of App Service changes marked null in change_operation_coverage; null when the alert processing rule list could not be read - change_operations_total — Number of App Service changes in change_operation_coverage; always 4 - - all_change_operations_covered — True if every App Service change is marked true; false if any is marked false; null if none is false but some are only possibly covered - - uncovered_change_operations — List of the App Service changes marked false in change_operation_coverage + - all_change_operations_covered — True if every App Service change is marked true; false if any is marked false; null if none is false but some are only possibly covered; null when the alert processing rule list could not be read + - uncovered_change_operations — List of the App Service changes marked false in change_operation_coverage; null when the alert processing rule list could not be read diff --git a/fetchers/azure/diagnostic_settings/fetcher.py b/fetchers/azure/diagnostic_settings/fetcher.py index 3c88d20e..03466ba2 100644 --- a/fetchers/azure/diagnostic_settings/fetcher.py +++ b/fetchers/azure/diagnostic_settings/fetcher.py @@ -213,8 +213,21 @@ def category_coverage(settings: list[dict]) -> dict: } -def summarize(settings: list[dict]) -> dict: - """Whether the Activity Log is exported at all, and whether it is complete.""" +SETTING_FIELDS = ( + "total_diagnostic_settings", "activity_log_exported", "settings_with_storage_account", + "settings_with_log_analytics_workspace", "settings_with_event_hub", "settings_with_partner_solution", + "log_category_coverage", "covered_required_log_categories", "required_log_category_percentage", + "all_required_log_categories_covered", "settings_capturing_required_categories", + "settings_with_legacy_retention", +) + + +def summarize(settings: list[dict] | None) -> dict: + """Whether the Activity Log is exported at all, and whether it is complete. + + None (the list was not read) gives null for every count, the coverage map and the verdicts built from it.""" + if settings is None: + return {**dict.fromkeys(SETTING_FIELDS), "required_log_categories": list(REQUIRED_LOG_CATEGORIES)} coverage = category_coverage(settings) required_covered = [c for c in REQUIRED_LOG_CATEGORIES if coverage.get(c)] return { @@ -254,8 +267,8 @@ def summarize(settings: list[dict]) -> dict: # --- collection (lazy azure imports) --- -def collect_diagnostic_settings(subscription_id, cred, collector: Collector) -> list[dict]: - """One diagnostic_settings.list() at subscription scope.""" +def collect_diagnostic_settings(subscription_id, cred, collector: Collector) -> list[dict] | None: + """One diagnostic_settings.list() at subscription scope; None when it couldn't be read.""" from azure.mgmt.monitor import MonitorManagementClient def _client(): @@ -263,7 +276,7 @@ def _client(): client = collector.guard("monitor.MonitorManagementClient (init)", _client) if client is None: - return [] + return None # azure-mgmt-monitor 7.0.0 dropped this operation group; without the check the # failure would read as an opaque AttributeError instead of naming the fix. @@ -275,7 +288,7 @@ def _client(): "operation group (removed in 7.0.0) — pin azure-mgmt-monitor<7" ), ) - return [] + return None def _list(): # Subscription scope returns one page; the SDK follows nextLink regardless. @@ -286,8 +299,8 @@ def _list(): ) ] - settings = collector.guard("monitor.diagnostic_settings.list", _list, default=[]) - return sorted(settings, key=lambda r: r.get("id") or "") + settings = collector.guard("monitor.diagnostic_settings.list", _list) + return None if settings is None else sorted(settings, key=lambda r: r.get("id") or "") def main() -> int: @@ -306,7 +319,7 @@ def main() -> int: subscription_id = sub["subscription_id"] cred = collector.guard("azure.identity.DefaultAzureCredential", credential) - settings: list[dict] = [] + settings: list[dict] | None = None registration = REGISTRATION_UNKNOWN if subscription_id and cred is not None: # ARM returns an empty list, not an error, for an unregistered provider. @@ -320,7 +333,7 @@ def main() -> int: subscription_id, ) settings = collect_diagnostic_settings(subscription_id, cred, collector) - if not settings and collector.ok: + if settings == [] and collector.ok: logger.warning( "No subscription-scope diagnostic setting on %s — the Activity Log " "is not exported and is retained for 90 days only", @@ -340,7 +353,7 @@ def main() -> int: subscription_source=sub["subscription_source"], collector=collector, results={ - "diagnostic_settings": settings, + "diagnostic_settings": settings or [], "scope": "subscription", "provider_registration_status": registration, }, diff --git a/fetchers/azure/diagnostic_settings/fetcher.yaml b/fetchers/azure/diagnostic_settings/fetcher.yaml index fd1af3c0..66c44a74 100644 --- a/fetchers/azure/diagnostic_settings/fetcher.yaml +++ b/fetchers/azure/diagnostic_settings/fetcher.yaml @@ -1,5 +1,5 @@ name: azure_diagnostic_settings -version: 0.1.0 +version: 0.2.0 description: >- Shows whether the subscription's Activity Log is exported by a diagnostic setting, where it goes, and whether the Administrative, Security, Alert and Policy categories are captured. category: azure @@ -56,11 +56,11 @@ evidence_set: Summary fields: What was checked? - - total_diagnostic_settings — Number of diagnostic settings on the subscription itself, which export the Activity Log; resource-level settings are not read here + - total_diagnostic_settings — Number of diagnostic settings on the subscription itself, which export the Activity Log; resource-level settings are not read here; null when the settings could not be read, as is every count, coverage map and verdict below built from them - provider_registration_status — Whether the Azure Monitor provider (Microsoft.Insights) is registered on the subscription: registered, not_registered or unknown Is the Activity Log exported, and where to? - - activity_log_exported — True if the subscription has at least one diagnostic setting, whatever categories it turns on; false if it has none, so the Activity Log is kept 90 days and then discarded; also false when the settings couldn't be read, with metadata.partial_failure true + - activity_log_exported — True if the subscription has at least one diagnostic setting, whatever categories it turns on; false if it has none, so the Activity Log is kept 90 days and then discarded; null when the settings couldn't be read, with metadata.partial_failure true - settings_with_storage_account — Number of settings that send the Activity Log to a storage account - settings_with_log_analytics_workspace — Number of settings that send the Activity Log to a Log Analytics workspace - settings_with_event_hub — Number of settings that send the Activity Log to an event hub diff --git a/fetchers/azure/log_analytics_workspaces/fetcher.py b/fetchers/azure/log_analytics_workspaces/fetcher.py index fbd82758..ec9a90ec 100644 --- a/fetchers/azure/log_analytics_workspaces/fetcher.py +++ b/fetchers/azure/log_analytics_workspaces/fetcher.py @@ -281,22 +281,48 @@ def workspace_record( } -def summarize(workspaces: list[dict]) -> dict: - """Retention floor, exposure, access mode and SIEM onboarding across workspaces.""" +WORKSPACE_FIELDS = ( + "total_workspaces", "min_workspace_retention_in_days", "max_workspace_retention_in_days", + "min_table_total_retention_in_days", "total_tables", "workspaces_with_daily_cap", + "workspaces_public_ingestion_enabled", "workspaces_public_query_enabled", + "workspaces_resource_or_workspace_access_mode", "workspaces_workspace_only_access_mode", + "workspaces_local_auth_disabled", "workspaces_with_data_exports", "total_data_exports", + "sentinel_onboarded_workspaces", "sentinel_onboarded", +) + + +def summarize(workspaces: list[dict] | None) -> dict: + """Retention floor, exposure, access mode and SIEM onboarding across workspaces. + + None (the list was not read) gives null for every figure; a figure built from a per-workspace + read (tables, data exports, solutions) is null while any workspace's read failed. + """ + if workspaces is None: + return dict.fromkeys(WORKSPACE_FIELDS) retention = [w["retention_in_days"] for w in workspaces if w["retention_in_days"] is not None] table_totals = [ w["table_retention"]["min_total_retention_in_days"] for w in workspaces if w["table_retention"] and w["table_retention"]["min_total_retention_in_days"] is not None ] + unread = { + "tables": any(w["table_retention"] is None for w in workspaces), + "exports": any(w["data_exports"] is None for w in workspaces), + "solutions": any(w["sentinel_onboarded"] is None for w in workspaces), + } + onboarded = [w["sentinel_onboarded"] for w in workspaces] + + def known(is_unread, value): + return None if is_unread else value + return { "total_workspaces": len(workspaces), "min_workspace_retention_in_days": min(retention) if retention else None, "max_workspace_retention_in_days": max(retention) if retention else None, - "min_table_total_retention_in_days": min(table_totals) if table_totals else None, - "total_tables": sum( + "min_table_total_retention_in_days": known(unread["tables"], min(table_totals) if table_totals else None), + "total_tables": known(unread["tables"], sum( w["table_retention"]["table_count"] for w in workspaces if w["table_retention"] - ), + )), "workspaces_with_daily_cap": sum(1 for w in workspaces if w["daily_cap_enabled"]), "workspaces_public_ingestion_enabled": sum( 1 for w in workspaces if w["public_ingestion_enabled"] @@ -309,18 +335,21 @@ def summarize(workspaces: list[dict]) -> dict: 1 for w in workspaces if w["access_mode"] == ACCESS_MODE_WORKSPACE_ONLY ), "workspaces_local_auth_disabled": sum(1 for w in workspaces if w["local_auth_disabled"]), - "workspaces_with_data_exports": sum(1 for w in workspaces if w["data_exports"]), - "total_data_exports": sum(len(w["data_exports"] or []) for w in workspaces), - "sentinel_onboarded_workspaces": sum(1 for w in workspaces if w["sentinel_onboarded"]), - "sentinel_onboarded": any(w["sentinel_onboarded"] for w in workspaces), + "workspaces_with_data_exports": known(unread["exports"], sum(1 for w in workspaces if w["data_exports"])), + "total_data_exports": known(unread["exports"], sum(len(w["data_exports"] or []) for w in workspaces)), + "sentinel_onboarded_workspaces": known(unread["solutions"], sum(1 for o in onboarded if o)), + # True once any workspace has it; null while a solutions read failed and none is known to have it. + "sentinel_onboarded": True if any(o is True for o in onboarded) else None if unread["solutions"] else False, } # --- collection (lazy azure imports) --- def collect_workspaces(subscription_id, cred, collector: Collector, exclusions: ResourceExclusions | None = None, - excluded: list | None = None) -> list[dict]: - """One workspaces.list(), then three GETs per workspace; excluded ones are appended to `excluded` unread.""" + excluded: list | None = None) -> list[dict] | None: + """One workspaces.list(), then three GETs per workspace; excluded ones are appended to `excluded` unread. + + None when the workspace list could not be read.""" def _client(): from azure.mgmt.loganalytics import LogAnalyticsManagementClient # lazy @@ -333,7 +362,7 @@ def _client(): if client is None: if exclusions is not None: exclusions.mark_unread() - return [] + return None # ItemPaged: the SDK follows nextLink itself, here and below. workspaces = collector.guard( @@ -343,7 +372,7 @@ def _client(): if workspaces is None: if exclusions is not None: exclusions.mark_unread() - workspaces = [] + return None if exclusions and excluded is not None: workspaces, out = exclusions.split(workspaces, "workspace") excluded += sorted(out, key=lambda e: e["id"] or "") @@ -392,7 +421,7 @@ def main() -> int: subscription_id = sub["subscription_id"] cred = collector.guard("azure.identity.DefaultAzureCredential", credential) - workspaces: list[dict] = [] + workspaces: list[dict] | None = None excluded: list[dict] = [] exclusions = ResourceExclusions.from_env() registration = REGISTRATION_UNKNOWN @@ -427,14 +456,14 @@ def main() -> int: subscription_source=sub["subscription_source"], collector=collector, results={ - "workspaces": workspaces, + "workspaces": workspaces or [], "provider_registration_status": registration, "excluded": excluded, }, summary={ **summarize(workspaces), "provider_registration_status": registration, - **exclusions.summary(excluded, ("workspace",), {"workspaces": len(workspaces)}), + **exclusions.summary(excluded, ("workspace",), {"workspaces": None if workspaces is None else len(workspaces)}), }, ) diff --git a/fetchers/azure/log_analytics_workspaces/fetcher.yaml b/fetchers/azure/log_analytics_workspaces/fetcher.yaml index f702b724..e9ae5de5 100644 --- a/fetchers/azure/log_analytics_workspaces/fetcher.yaml +++ b/fetchers/azure/log_analytics_workspaces/fetcher.yaml @@ -1,5 +1,5 @@ name: azure_log_analytics_workspaces -version: 0.3.1 +version: 0.4.0 description: >- Shows how long each Log Analytics workspace keeps its logs, who can reach and read them, where they are exported, and whether Microsoft Sentinel is onboarded. category: azure @@ -79,20 +79,20 @@ evidence_set: Summary fields: What was checked? - - total_workspaces — Number of in-scope Log Analytics workspaces read - - total_tables — Number of tables, built-in and custom, across the in-scope workspaces whose tables were read + - total_workspaces — Number of in-scope Log Analytics workspaces read; null when the workspace list could not be read, as is every count and figure below built from it + - total_tables — Number of tables, built-in and custom, across the in-scope workspaces; null when any workspace's tables could not be read - provider_registration_status — Whether the Log Analytics provider (Microsoft.OperationalInsights) is registered on the subscription: registered, not_registered or unknown Which resources are left out of scope? - resource_exclusion_patterns — List of the configured resource_exclusions patterns (rg:, location: or id:); empty when none are set, so every resource is in scope - resource_exclusion_patterns_unmatched — List of configured patterns that matched no workspace, such as a mistyped name; null when the workspace list could not be read - - excluded_workspaces — Number of workspaces left out of scope because a pattern matched their resource group, region or id; each is listed under excluded with the pattern, no other count includes it, and its tables, data exports and solutions are never read; 0 when the workspace list could not be read - - in_scope_workspaces — Number of workspaces read and in scope; 0 means none was read or the patterns took out every workspace, which a check can catch by requiring more than 0 + - excluded_workspaces — Number of workspaces left out of scope because a pattern matched their resource group, region or id; each is listed under excluded with the pattern, no other count includes it, and its tables, data exports and solutions are never read; null when the workspace list could not be read + - in_scope_workspaces — Number of workspaces read and in scope; 0 means none exists or the patterns took out every workspace, which a check can catch by requiring more than 0; null when the workspace list could not be read How long are logs kept, and can ingestion stop? - min_workspace_retention_in_days — Days of the shortest workspace retention (how long a workspace keeps data available to query by default) across in-scope workspaces; null when none reports one - max_workspace_retention_in_days — Days of the longest workspace retention across in-scope workspaces; null when none reports one - - min_table_total_retention_in_days — Days of the shortest total retention (query retention plus long-term retention) of any table, across in-scope workspaces whose tables were read; null when no table reports one. A table's retention can be set apart from its workspace's + - min_table_total_retention_in_days — Days of the shortest total retention (query retention plus long-term retention) of any table, across in-scope workspaces; null when no table reports one or any workspace's tables could not be read. A table's retention can be set apart from its workspace's - workspaces_with_daily_cap — Number of workspaces with a daily ingestion cap (a daily quota other than -1, which means no cap); data arriving after the cap is reached is dropped for the rest of that day Who can reach and read the logs? @@ -103,9 +103,9 @@ evidence_set: - workspaces_local_auth_disabled — Number of workspaces with shared-key (local) authentication turned off, so callers must sign in with Entra ID; not set means shared keys still work Are logs exported? - - workspaces_with_data_exports — Number of workspaces with at least one data export rule, enabled or not - - total_data_exports — Number of data export rules across in-scope workspaces, enabled or not + - workspaces_with_data_exports — Number of workspaces with at least one data export rule, enabled or not; null when any workspace's data exports could not be read + - total_data_exports — Number of data export rules across in-scope workspaces, enabled or not; null when any workspace's data exports could not be read Is Microsoft Sentinel onboarded? - - sentinel_onboarded_workspaces — Number of workspaces with Sentinel onboarded, meaning the SecurityInsights solution is switched on in the workspace - - sentinel_onboarded — True if at least one in-scope workspace has Sentinel onboarded; false if none does or none was read, including when a resource_exclusions pattern takes out the workspace Sentinel is onboarded to, and when the solutions couldn't be read (the run then fails); never null + - sentinel_onboarded_workspaces — Number of workspaces with Sentinel onboarded, meaning the SecurityInsights solution is switched on in the workspace; null when any workspace's solutions could not be read + - sentinel_onboarded — True if at least one in-scope workspace has Sentinel onboarded; false if none does, including when a resource_exclusions pattern takes out the workspace Sentinel is onboarded to; null when the workspace list could not be read, or when no workspace is known to have it and any workspace's solutions couldn't be read (the run then fails) diff --git a/fetchers/azure/resource_diagnostic_settings/fetcher.py b/fetchers/azure/resource_diagnostic_settings/fetcher.py index 0fbb5d94..7b10d592 100644 --- a/fetchers/azure/resource_diagnostic_settings/fetcher.py +++ b/fetchers/azure/resource_diagnostic_settings/fetcher.py @@ -221,7 +221,27 @@ def resource_record(base: dict, status: str, categories=None, settings=None) -> } -def summarize(records: list[dict], enumerated: int, total_reported, pages: int) -> dict: +RESOURCE_FIELDS = ( + "resources_enumerated", "child_services_synthesized", "resources_evaluated", "resources_assessed", + "resources_unsupported", "resources_not_found", "resources_errored", "resources_emitting_logs", + "resources_with_enabled_log_setting", "resources_emitting_logs_without_setting", + "log_setting_coverage_percentage", "resources_sending_to_log_analytics", "log_analytics_coverage_percentage", + "resources_with_audit_categories", "resources_capturing_all_audit_categories", "uncovered_by_type", + "all_log_emitting_resources_covered", +) +INGESTION_FIELDS = ( + "resources_with_recent_ingestion", "resources_without_recent_ingestion", "resources_ingestion_unknown", + "recent_ingestion_percentage", "no_recent_ingestion_by_type", "all_log_analytics_resources_ingesting", + "settings_exporting_to_unapproved_workspace", "resources_sending_to_approved_workspace", + "resources_exporting_to_unapproved_workspace", +) + + +def summarize(records: list[dict], enumerated: int, total_reported, pages: int, read: bool = True) -> dict: + """read=False (the resource inventory was not read) gives null for every count built from it.""" + if not read: + return {**dict.fromkeys(RESOURCE_FIELDS), "resource_graph_total_records": total_reported, + "resource_graph_pages": pages} status = Counter(r["status"] for r in records) emitting = [r for r in records if r["emits_resource_logs"]] covered = [r for r in emitting if r["has_enabled_log_setting"]] @@ -446,13 +466,22 @@ def destination_records(workspaces: dict, records: list[dict], approved: list[st def summarize_ingestion( - records: list[dict], destinations: list[dict], days: int, hours: int, approved: list[str] + records: list[dict], destinations: list[dict], days: int, hours: int, approved: list[str], read: bool = True ) -> dict: + """read=False (the resource inventory was not read) gives null for every count built from the records.""" sending = [r for r in records if r["emits_resource_logs"] and r["sends_to_log_analytics"]] recent = [r for r in sending if r["ingested_in_window"] is True] stale = [r for r in sending if r["ingested_in_window"] is False] status = Counter(d["status"] for d in destinations) emitting = [r for r in records if r["emits_resource_logs"]] + if not read: + return { + "ingestion_lookback_days": days, "basic_logs_lookback_hours": hours, + "log_analytics_destinations": None, "log_analytics_destinations_queried": None, + "log_analytics_destinations_not_authorized": None, "log_analytics_destinations_not_found": None, + "log_analytics_destinations_errored": None, "log_analytics_destinations_with_unqueried_tables": None, + **dict.fromkeys(INGESTION_FIELDS), "approved_workspaces_configured": bool(approved), + } exporting = [s for r in records for s in r["diagnostic_settings"] if s["exports_logs"] and s.get("workspace_id")] return { "ingestion_lookback_days": days, @@ -491,12 +520,12 @@ def summarize_ingestion( def enumerate_resources(subscription_id, cred, collector: Collector) -> dict: client = collector.guard("resourcegraph.ResourceGraphClient (init)", lambda: resource_graph_client(cred)) if client is None: - return {"rows": [], "total_records": None, "pages": 0} + return {"rows": [], "total_records": None, "pages": 0, "read": False} result = collector.guard( "resourcegraph.resources (Resources)", lambda: query_all(client, subscription_id, INVENTORY_QUERY), ) - return result or {"rows": [], "total_records": None, "pages": 0} + return {**result, "read": True} if result else {"rows": [], "total_records": None, "pages": 0, "read": False} def monitor_client(subscription_id, cred): @@ -614,7 +643,7 @@ def main() -> int: cred = collector.guard("azure.identity.DefaultAzureCredential", credential) records: list[dict] = [] - inventory = {"rows": [], "total_records": None, "pages": 0} + inventory = {"rows": [], "total_records": None, "pages": 0, "read": False} registration = REGISTRATION_UNKNOWN days = int_config(INGESTION_LOOKBACK_ENV, DEFAULT_INGESTION_LOOKBACK_DAYS) hours = basic_lookback_hours() @@ -653,7 +682,8 @@ def main() -> int: ), ) - if inventory["total_records"] is None: + inventory_read = inventory["read"] + if not inventory_read: exclusions.mark_unread() exclusions.warn_unmatched(logger) records = sorted((with_ingestion(r, workspaces, approved) for r in records), key=lambda r: (r["id"] or "").lower()) @@ -675,10 +705,11 @@ def main() -> int: "excluded": excluded, }, summary={ - **summarize(records, len(inventory["rows"]), inventory["total_records"], inventory["pages"]), + **summarize(records, len(inventory["rows"]), inventory["total_records"], inventory["pages"], + inventory_read), "provider_registration_status": registration, - **summarize_ingestion(records, destinations, days, hours, approved), - **exclusions.summary(excluded, ("resource",), {"resources": len(records)}), + **summarize_ingestion(records, destinations, days, hours, approved, inventory_read), + **exclusions.summary(excluded, ("resource",), {"resources": len(records) if inventory_read else None}), }, ) diff --git a/fetchers/azure/resource_diagnostic_settings/fetcher.yaml b/fetchers/azure/resource_diagnostic_settings/fetcher.yaml index 92f4947e..840960f8 100644 --- a/fetchers/azure/resource_diagnostic_settings/fetcher.yaml +++ b/fetchers/azure/resource_diagnostic_settings/fetcher.yaml @@ -1,5 +1,5 @@ name: azure_resource_diagnostic_settings -version: 0.4.1 +version: 0.5.0 description: >- Shows which resources export their logs through a diagnostic setting, where those logs go, whether they actually arrive in Log Analytics, and which log categories nothing captures. category: azure @@ -122,7 +122,7 @@ evidence_set: Summary fields: Was every resource found and checked? - - resources_enumerated — Number of resources Resource Graph returned + - resources_enumerated — Number of resources Resource Graph returned; null when the resource list could not be read, as is every count, percentage and verdict below built from the resource list, and the in-scope and excluded counts - resource_graph_total_records — Number of resources Resource Graph reported in total; must equal resources_enumerated, and the run fails if they differ, which proves no resource was missed; null if Resource Graph gave no total - resource_graph_pages — Number of Resource Graph result pages read - child_services_synthesized — Number of storage blob, file, queue and table services added to the inventory (Resource Graph doesn't list them) @@ -135,9 +135,9 @@ evidence_set: Which resources are left out of scope? - resource_exclusion_patterns — List of the configured resource_exclusions patterns (rg:, location: or id:); empty when none are set, so every resource is in scope - - resource_exclusion_patterns_unmatched — List of configured patterns that matched no resource, such as a mistyped name; null if the resource list couldn't be read or Resource Graph returned no total - - excluded_resources — Number of resources left out of scope because a pattern matched their resource group, region or id, a storage account's added services included; each is listed under excluded with the pattern, is not read, and no other count includes them - - in_scope_resources — Number of resources in scope and checked, the added storage services included, so it equals resources_evaluated; 0 means none was found, the patterns took out every resource, or the resource list couldn't be read (resource_exclusion_patterns_unmatched is then null), which a check can catch by requiring more than 0. resources_enumerated and resource_graph_total_records still count every resource Resource Graph returned, so they stay equal + - resource_exclusion_patterns_unmatched — List of configured patterns that matched no resource, such as a mistyped name; null if the resource list couldn't be read + - excluded_resources — Number of resources left out of scope because a pattern matched their resource group, region or id, a storage account's added services included; each is listed under excluded with the pattern, is not read, and no other count includes them; null when the resource list could not be read + - in_scope_resources — Number of resources in scope and checked, the added storage services included, so it equals resources_evaluated; 0 means none was found or the patterns took out every resource, which a check can catch by requiring more than 0; null when the resource list couldn't be read. resource_graph_total_records still counts every resource Resource Graph reported, and equals resources_enumerated when the list was read Do resources export their logs? - resources_emitting_logs — Number of log-producing resources (resources whose type produces resource logs). A SQL server's master database counts like any database: its setting is where server-level auditing is exported, while the server itself has metrics only @@ -154,7 +154,7 @@ evidence_set: Were the destination workspaces read? - ingestion_lookback_days — Number of days of records the workspace query looks back over - basic_logs_lookback_hours — Number of hours of records read from Basic and Auxiliary plan tables; 0, the default, when Basic and Auxiliary tables were skipped. At 0, a resource with no other record in a workspace that has a Basic or Auxiliary table able to hold its logs reads unknown, never without recent ingestion, so such a resource can only count in resources_without_recent_ingestion when this is above 0 - - log_analytics_destinations — Number of distinct destination workspaces that diagnostic settings export logs to + - log_analytics_destinations — Number of distinct destination workspaces that diagnostic settings export logs to; null when the resource list could not be read, as is every count of destinations below and every count in the next section - log_analytics_destinations_queried — Number of destination workspaces whose records were read - log_analytics_destinations_not_authorized — Number of destination workspaces that refused the read (401 or 403, or in a subscription the caller can't see) - log_analytics_destinations_not_found — Number of destination workspaces that no longer exist, so logs sent there go nowhere diff --git a/tests/test_azure_monitor_verdicts.py b/tests/test_azure_monitor_verdicts.py index 25041e38..7ab76f7e 100644 --- a/tests/test_azure_monitor_verdicts.py +++ b/tests/test_azure_monitor_verdicts.py @@ -169,6 +169,25 @@ def test_suppression_elsewhere_does_not_touch_the_alert(): assert (r["processing_rule_matches"], r["suppressed"], r["reaches_human"]) == ([], False, True) +def test_unread_processing_rules_leave_a_rule_without_an_own_group_unknown(): + r = routing.route(True, [], [VAULT], {}, None) + assert (r["routes_to_enabled_receiver"], r["routes_to_person"], r["reaches_human"]) == (None, None, None) + assert r["processing_rule_matches"] is None and r["suppressed_by_processing_rules"] is None + + +def test_unread_processing_rules_keep_a_known_own_group_true_and_a_disabled_rule_false(): + r = routing.route(True, [AG_EMAIL], [VAULT], groups(EMAIL_GROUP), None) + assert (r["routes_to_enabled_receiver"], r["routes_to_person"], r["reaches_human"]) == (True, True, None) + r = routing.route(False, [], [VAULT], {}, None) + assert (r["routes_to_enabled_receiver"], r["reaches_human"]) == (False, False) + + +def test_read_empty_processing_rules_still_give_false_for_a_rule_without_a_group(): + r = routing.route(True, [], [VAULT], {}, []) + assert (r["routes_to_enabled_receiver"], r["reaches_human"]) == (False, False) + assert r["processing_rule_matches"] == [] and r["suppressed_by_processing_rules"] == [] + + def test_suppression_scope_match_ignores_case_and_trailing_slash(): r = routing.route(True, [AG_EMAIL], [VAULT], groups(EMAIL_GROUP), [processing_rule([RG.upper() + "/"])]) assert r["suppressed"] is True @@ -736,7 +755,7 @@ def test_main_wires_inventory_settings_and_ingestion_into_the_evidence(monkeypat monkeypatch.setattr(rds, "credential", lambda: object()) monkeypatch.setattr(rds, "provider_registration_status", lambda *a: "registered") monkeypatch.setattr(rds, "enumerate_resources", lambda *a: { - "rows": [{"id": VAULT, "name": "kv1", "type": "Microsoft.KeyVault/vaults"}], "total_records": 1, "pages": 1}) + "rows": [{"id": VAULT, "name": "kv1", "type": "Microsoft.KeyVault/vaults"}], "total_records": 1, "pages": 1, "read": True}) monkeypatch.setattr(rds, "monitor_client", lambda *a: monitor) monkeypatch.setattr(rds, "AzureRestClient", lambda cred: _Workspaces()) monkeypatch.setenv("EVIDENCE_DIR", str(tmp_path)) diff --git a/tests/test_azure_monitoring_null_counts.py b/tests/test_azure_monitoring_null_counts.py new file mode 100644 index 00000000..60730ef2 --- /dev/null +++ b/tests/test_azure_monitoring_null_counts.py @@ -0,0 +1,247 @@ +"""A monitoring list that could not be read gives null counts and verdicts, never 0 or false.""" + +from __future__ import annotations + +import importlib.util +import json +import sys +import types +from pathlib import Path + +import pytest + +REPO_ROOT = Path(__file__).resolve().parent.parent +AZURE = REPO_ROOT / "fetchers" / "azure" +sys.path.insert(0, str(REPO_ROOT / "fetchers" / "_lib")) +sys.path.insert(0, str(AZURE / "_shared")) + + +def _load(name): + spec = importlib.util.spec_from_file_location(f"azure_{name}_null_counts", AZURE / name / "fetcher.py") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +ala = _load("activity_log_alerts") +ds = _load("diagnostic_settings") +law = _load("log_analytics_workspaces") +rds = _load("resource_diagnostic_settings") +ns = types.SimpleNamespace +RG = "/subscriptions/s1/resourceGroups/rg1" +DENIED = RuntimeError("403 AuthorizationFailed") + + +def _run(monkeypatch, tmp_path, module, filename): + monkeypatch.delenv("AZURE_RESOURCE_EXCLUSIONS", raising=False) + monkeypatch.setattr(module, "load_dotenv", lambda *a, **k: None) + monkeypatch.setattr(module, "resolve_subscription", lambda c: {"subscription_id": "s1", "subscription_source": "env"}) + monkeypatch.setattr(module, "credential", lambda: object()) + monkeypatch.setattr(module, "provider_registration_status", lambda *a: "registered") + monkeypatch.setenv("EVIDENCE_DIR", str(tmp_path)) + code = module.main() + return code, json.loads((tmp_path / filename).read_text())["summary"] + + +def _refuse(*a, **k): + raise DENIED + + +def _fake_azure(monkeypatch, module_name, **attrs): + monkeypatch.setitem(sys.modules, module_name, ns(**attrs)) + + +def _not_null(summary, keys): + return {k: summary[k] for k in keys if summary[k] is not None} + + +# --- activity_log_alerts --- + +def test_unread_alert_list_gives_null_counts_and_coverage_maps(monkeypatch, tmp_path): + client = ns(activity_log_alerts=ns(list_by_subscription_id=_refuse)) + _fake_azure(monkeypatch, "azure.mgmt.monitor", MonitorManagementClient=lambda **k: client) + monkeypatch.setattr(ala, "arm_rest_client", lambda cred: object()) + monkeypatch.setattr(ala, "collect_action_groups", lambda rest, sub: {"groups": []}) + monkeypatch.setattr(ala, "collect_processing_rules", lambda rest, sub, now: {"rules": []}) + code, summary = _run(monkeypatch, tmp_path, ala, "azure_activity_log_alerts_s1.json") + assert code == 1 + assert _not_null(summary, ala.ALERT_FIELDS) == {} + assert summary["in_scope_activity_log_alerts"] is None and summary["excluded_activity_log_alerts"] is None + + +def test_read_empty_alert_list_still_counts_zero(): + summary = ala.summarize([]) + assert summary["total_activity_log_alerts"] == 0 and summary["all_monitored_operations_covered"] is False + assert summary["service_health_alert_configured"] is False + assert summary["monitored_operation_coverage"]["create_update_network_security_group"] is False + + +def test_no_subscription_reads_no_alert_list(monkeypatch, tmp_path): + monkeypatch.setattr(ala, "resolve_subscription", lambda c: {"subscription_id": None, "subscription_source": None}) + monkeypatch.setattr(ala, "load_dotenv", lambda *a, **k: None) + monkeypatch.setattr(ala, "credential", lambda: object()) + monkeypatch.setenv("EVIDENCE_DIR", str(tmp_path)) + ala.main() + summary = json.loads((tmp_path / "azure_activity_log_alerts_unknown.json").read_text())["summary"] + assert summary["total_activity_log_alerts"] is None and summary["all_monitored_operations_covered"] is None + + +# --- diagnostic_settings --- + +def test_unread_setting_list_gives_null_export_verdict_and_counts(monkeypatch, tmp_path): + client = ns(diagnostic_settings=ns(list=_refuse)) + _fake_azure(monkeypatch, "azure.mgmt.monitor", MonitorManagementClient=lambda **k: client) + code, summary = _run(monkeypatch, tmp_path, ds, "azure_diagnostic_settings_s1.json") + assert code == 1 + assert summary["activity_log_exported"] is None + assert _not_null(summary, ds.SETTING_FIELDS) == {} + assert summary["required_log_categories"] == list(ds.REQUIRED_LOG_CATEGORIES) + + +def test_read_empty_setting_list_is_exported_false(monkeypatch, tmp_path): + client = ns(diagnostic_settings=ns(list=lambda resource_uri: [])) + _fake_azure(monkeypatch, "azure.mgmt.monitor", MonitorManagementClient=lambda **k: client) + code, summary = _run(monkeypatch, tmp_path, ds, "azure_diagnostic_settings_s1.json") + assert code == 0 + assert summary["activity_log_exported"] is False and summary["total_diagnostic_settings"] == 0 + + +# --- resource_diagnostic_settings --- + +def test_unread_inventory_gives_null_counts_and_scope_counts(monkeypatch, tmp_path): + monkeypatch.setattr(rds, "resource_graph_client", lambda cred: object()) + monkeypatch.setattr(rds, "query_all", _refuse) # the Resource Graph query is refused + monkeypatch.setattr(rds, "monitor_client", lambda *a: object()) + monkeypatch.setattr(rds, "AzureRestClient", lambda cred: object()) + code, summary = _run(monkeypatch, tmp_path, rds, "azure_resource_diagnostic_settings_s1.json") + assert code == 1 + assert _not_null(summary, rds.RESOURCE_FIELDS + rds.INGESTION_FIELDS) == {} + for key in ("in_scope_resources", "excluded_resources", "log_analytics_destinations", + "resource_exclusion_patterns_unmatched", "resource_graph_total_records"): + assert summary[key] is None, key + + +def test_read_empty_inventory_still_counts_zero(monkeypatch, tmp_path): + monkeypatch.setattr(rds, "enumerate_resources", lambda *a: {"rows": [], "total_records": 0, "pages": 1, "read": True}) + monkeypatch.setattr(rds, "monitor_client", lambda *a: object()) + monkeypatch.setattr(rds, "AzureRestClient", lambda cred: object()) + code, summary = _run(monkeypatch, tmp_path, rds, "azure_resource_diagnostic_settings_s1.json") + assert code == 0 + assert (summary["resources_evaluated"], summary["in_scope_resources"], summary["excluded_resources"]) == (0, 0, 0) + assert summary["resources_emitting_logs_without_setting"] == 0 + + +# --- log_analytics_workspaces --- + +def _workspace_client(workspaces, packs=(), tables=(), exports=()): + def reads(rows): + def read(rg, name): + if callable(rows): + return rows() + return list(rows) + return read + + return ns( + workspaces=ns(list=workspaces), + tables=ns(list_by_workspace=reads(tables)), + data_exports=ns(list_by_workspace=reads(exports)), + intelligence_packs=ns(list=reads(packs)), + ) + + +def _workspaces(): + return [ns(id=f"{RG}/providers/Microsoft.OperationalInsights/workspaces/law-{i}", name=f"law-{i}", + location="regioneast", retention_in_days=30) for i in (1, 2)] + + +def _run_law(monkeypatch, tmp_path, client): + _fake_azure(monkeypatch, "azure.mgmt.loganalytics", LogAnalyticsManagementClient=lambda **k: client) + return _run(monkeypatch, tmp_path, law, "azure_log_analytics_workspaces_s1.json") + + +def test_unread_workspace_list_gives_null_counts_and_scope_counts(monkeypatch, tmp_path): + code, summary = _run_law(monkeypatch, tmp_path, _workspace_client(_refuse)) + assert code == 1 + assert _not_null(summary, law.WORKSPACE_FIELDS) == {} + assert summary["in_scope_workspaces"] is None and summary["excluded_workspaces"] is None + + +def test_sentinel_onboarded_is_null_when_no_workspace_solutions_could_be_read(monkeypatch, tmp_path): + code, summary = _run_law(monkeypatch, tmp_path, _workspace_client(_workspaces, packs=_refuse)) + assert code == 1 + assert summary["sentinel_onboarded"] is None and summary["sentinel_onboarded_workspaces"] is None + assert summary["total_workspaces"] == 2 # the workspace list itself was read + + +def test_unread_sub_lists_null_only_the_counts_built_from_them(monkeypatch, tmp_path): + client = _workspace_client(_workspaces, packs=[ns(name="SecurityInsights", enabled=True)], + tables=_refuse, exports=_refuse) + code, summary = _run_law(monkeypatch, tmp_path, client) + assert code == 1 + assert summary["total_tables"] is None and summary["min_table_total_retention_in_days"] is None + assert summary["total_data_exports"] is None and summary["workspaces_with_data_exports"] is None + assert summary["sentinel_onboarded"] is True and summary["sentinel_onboarded_workspaces"] == 2 + + +@pytest.mark.parametrize("packs, expected", [ + ([ns(name="SecurityInsights", enabled=True)], True), + ([ns(name="SecurityInsights", enabled=False)], False), + ([], False), +]) +def test_read_solutions_keep_sentinel_onboarded_true_or_false(monkeypatch, tmp_path, packs, expected): + code, summary = _run_law(monkeypatch, tmp_path, _workspace_client(_workspaces, packs=packs)) + assert code == 0 and summary["sentinel_onboarded"] is expected + + +def test_unread_processing_rule_list_leaves_suppression_unknown(monkeypatch, tmp_path): + group_id = f"{RG}/providers/microsoft.insights/actionGroups/ag" + alert = ala.alert_record({ + "id": f"{RG}/providers/microsoft.insights/activityLogAlerts/a1", "name": "a1", "location": "global", + "enabled": True, "scopes": ["/subscriptions/s1"], "action_groups": [group_id], + "condition": {"all_of": [{"field": "operationName", "equals": "Microsoft.Network/networkSecurityGroups/write"}]}, + }) + group = {"id": group_id, "enabled": True, "deliverable": True, "notifies_people": True, "receivers": []} + monkeypatch.setattr(ala, "collect_activity_log_alerts", lambda sub, cred, collector: [dict(alert)]) + monkeypatch.setattr(ala, "arm_rest_client", lambda cred: object()) + monkeypatch.setattr(ala, "collect_action_groups", lambda rest, sub: {"groups": [group]}) + monkeypatch.setattr(ala, "collect_processing_rules", _refuse) + code, summary = _run(monkeypatch, tmp_path, ala, "azure_activity_log_alerts_s1.json") + evidence = json.loads((tmp_path / "azure_activity_log_alerts_s1.json").read_text()) + (record,) = evidence["results"]["activity_log_alerts"] + assert code == 1 + assert record["suppressed"] is None and record["reaches_human"] is None + assert _not_null(summary, ala.SUPPRESSION_FIELDS) == {} + # what the alert list and the action groups settle is still read + assert summary["total_activity_log_alerts"] == 1 and summary["alerts_routing_to_enabled_receiver"] == 1 + assert summary["monitored_operations_covered"] == 1 + + +def test_unread_processing_rule_list_leaves_an_alert_without_an_own_group_unknown(monkeypatch, tmp_path): + alert = ala.alert_record({ + "id": f"{RG}/providers/microsoft.insights/activityLogAlerts/a1", "name": "a1", "location": "global", + "enabled": True, "scopes": ["/subscriptions/s1"], "action_groups": [], + "condition": {"all_of": [{"field": "operationName", "equals": "Microsoft.Network/networkSecurityGroups/write"}]}, + }) + monkeypatch.setattr(ala, "collect_activity_log_alerts", lambda sub, cred, collector: [dict(alert)]) + monkeypatch.setattr(ala, "arm_rest_client", lambda cred: object()) + monkeypatch.setattr(ala, "collect_action_groups", lambda rest, sub: {"groups": []}) + monkeypatch.setattr(ala, "collect_processing_rules", _refuse) + code, summary = _run(monkeypatch, tmp_path, ala, "azure_activity_log_alerts_s1.json") + evidence = json.loads((tmp_path / "azure_activity_log_alerts_s1.json").read_text()) + (record,) = evidence["results"]["activity_log_alerts"] + assert code == 1 + assert record["routes_to_enabled_receiver"] is None and record["reaches_human"] is None + assert summary["alerts_routing_unknown"] == 1 and summary["alerts_reaching_human_unknown"] == 1 + assert summary["alerts_routing_to_enabled_receiver"] == 0 + + +def test_unread_alert_list_keeps_the_fixed_totals(): + summary = ala.summarize(None) + assert (summary["monitored_operations_total"], summary["change_operations_total"]) == (12, 4) + + +def test_read_empty_workspace_list_counts_zero_and_sentinel_false(monkeypatch, tmp_path): + code, summary = _run_law(monkeypatch, tmp_path, _workspace_client(lambda: [])) + assert code == 0 + assert summary["total_workspaces"] == 0 and summary["in_scope_workspaces"] == 0 + assert summary["sentinel_onboarded"] is False and summary["total_tables"] == 0 diff --git a/tests/test_azure_resource_exclusions_sql_logs.py b/tests/test_azure_resource_exclusions_sql_logs.py index c9b3d5bc..ef5fd34e 100644 --- a/tests/test_azure_resource_exclusions_sql_logs.py +++ b/tests/test_azure_resource_exclusions_sql_logs.py @@ -634,7 +634,7 @@ def run_rds(monkeypatch, tmp_path, patterns, inventory=None): read: list = [] monitor = ns(diagnostic_settings_category=ns(list=lambda resource_uri: read.append(resource_uri) or [category]), diagnostic_settings=ns(list=lambda resource_uri: [setting])) - monkeypatch.setattr(rds, "enumerate_resources", lambda *a: inventory or {"rows": INVENTORY, "total_records": 5, "pages": 1}) + monkeypatch.setattr(rds, "enumerate_resources", lambda *a: inventory or {"rows": INVENTORY, "total_records": 5, "pages": 1, "read": True}) monkeypatch.setattr(rds, "monitor_client", lambda *a: monitor) monkeypatch.setattr(rds, "AzureRestClient", lambda cred: _Rest()) for env in (rds.BASIC_LOOKBACK_ENV, rds.APPROVED_WORKSPACES_ENV, rds.INGESTION_LOOKBACK_ENV): @@ -656,7 +656,7 @@ def test_diagnostic_settings_without_patterns_add_only_empty_exclusion_fields(mo def test_diagnostic_settings_unread_inventory_leaves_unmatched_patterns_null(monkeypatch, tmp_path, caplog): - code, evidence, _ = run_rds(monkeypatch, tmp_path, PATTERNS, inventory={"rows": [], "total_records": None, "pages": 0}) + code, evidence, _ = run_rds(monkeypatch, tmp_path, PATTERNS, inventory={"rows": [], "total_records": None, "pages": 0, "read": False}) assert evidence["summary"]["resource_exclusion_patterns_unmatched"] is None assert "matched nothing" not in caplog.text