Skip to content

Commit 24eedec

Browse files
committed
fix(scanner): align AZ-DB-008 with the shared evaluate() contract
dev added tests/test_rule_evaluation_contract.py (#381) after this branch was opened. It requires a failed inventory to be ERROR, an empty one to be NOT_APPLICABLE, and a registered PASS/FAIL fixture, so AZ-DB-008 failed three of its checks once synced with dev. Add AzureClient.list_sql_servers(), which returns None when the list call fails (get_sql_servers() stays as the [] wrapper for legacy callers), and use the shared inventory_unavailable / no_resources_found / fail_findings helpers. This replaces the earlier UNKNOWN-on-empty workaround, which only existed because the failure could not be told apart from an empty result. Evidence now records the inspected minimal_tls_version. Signed-off-by: Dipesh Ray <dipesh.ray.g@gmail.com>
1 parent 417ce36 commit 24eedec

6 files changed

Lines changed: 81 additions & 49 deletions

File tree

‎scanner/azure_client.py‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,14 +1074,21 @@ def get_postgresql_servers(self) -> List[Any]:
10741074
logger.error("get_postgresql_servers failed: %s", exc)
10751075
return []
10761076

1077-
def get_sql_servers(self) -> List[Any]:
1078-
"""List all Azure SQL servers in the subscription."""
1077+
def list_sql_servers(self) -> Optional[List[Any]]:
1078+
"""List all Azure SQL servers in the subscription.
1079+
1080+
Returns ``None`` when the list call fails; see list_storage_accounts().
1081+
"""
10791082
try:
10801083
client = SqlManagementClient(self.credential, self.subscription_id)
10811084
return list(client.servers.list())
10821085
except Exception as exc:
1083-
logger.error("get_sql_servers failed: %s", exc)
1084-
return []
1086+
logger.error("list_sql_servers failed: %s", exc)
1087+
return None
1088+
1089+
def get_sql_servers(self) -> List[Any]:
1090+
"""List all Azure SQL servers, or ``[]`` on failure (legacy scan() callers)."""
1091+
return self.list_sql_servers() or []
10851092

10861093
def get_sql_server_auditing_policy(self, resource_group: str, server_name: str) -> Optional[Any]:
10871094
"""Fetch the blob auditing policy for an Azure SQL server."""

‎scanner/rules/az_db_008.py‎

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
from typing import Any, Dict, List
44

5-
from scanner.evaluation import EvaluationStatus, RuleEvaluation, subscription_scope_id
5+
from scanner.evaluation import (
6+
EvaluationStatus,
7+
RuleEvaluation,
8+
fail_findings,
9+
inventory_unavailable,
10+
no_resources_found,
11+
)
612

713
RULE_ID = "AZ-DB-008"
814
RULE_NAME = "Azure SQL Server Minimum TLS Version Below 1.2"
@@ -28,6 +34,8 @@
2834
# below-1.2 value, not skipped, since both mean TLS 1.2 is not being enforced.
2935
_COMPLIANT_VERSIONS = {"1.2", "1.3"}
3036

37+
RESOURCE_TYPE = "Microsoft.Sql/servers"
38+
3139

3240
def _finding(azure_client: Any, server: Any, tls_version: Any) -> Dict[str, Any]:
3341
parsed = azure_client.parse_resource_id(server.id)
@@ -38,7 +46,7 @@ def _finding(azure_client: Any, server: Any, tls_version: Any) -> Dict[str, Any]
3846
"category": CATEGORY,
3947
"resource_id": server.id,
4048
"resource_name": server.name,
41-
"resource_type": "Microsoft.Sql/servers",
49+
"resource_type": RESOURCE_TYPE,
4250
"description": DESCRIPTION,
4351
"remediation": REMEDIATION,
4452
"playbook": PLAYBOOK,
@@ -53,59 +61,43 @@ def _finding(azure_client: Any, server: Any, tls_version: Any) -> Dict[str, Any]
5361

5462
def scan(azure_client: Any, subscription_id: str) -> List[Dict[str, Any]]:
5563
"""Detect Azure SQL Servers whose minimum TLS version is below 1.2 or unset."""
56-
findings: List[Dict[str, Any]] = []
57-
58-
for server in azure_client.get_sql_servers():
59-
tls_version = getattr(server, "minimal_tls_version", None)
60-
if tls_version not in _COMPLIANT_VERSIONS:
61-
findings.append(_finding(azure_client, server, tls_version))
62-
63-
return findings
64+
return fail_findings(evaluate(azure_client, subscription_id))
6465

6566

6667
def evaluate(azure_client: Any, subscription_id: str) -> List[RuleEvaluation]:
67-
"""Report coverage per server (PASS included), so an inventory that could
68-
not be read is recorded as UNKNOWN instead of reading as a clean scan."""
69-
servers = azure_client.get_sql_servers()
68+
"""Report a status for every SQL server inspected, PASS included.
69+
70+
A failed list call is ERROR and an empty one NOT_APPLICABLE, so an unreadable
71+
inventory never reads as a clean scan.
72+
"""
73+
servers = azure_client.list_sql_servers()
74+
if servers is None:
75+
return [inventory_unavailable(RULE_ID, RESOURCE_TYPE, subscription_id)]
7076
if not servers:
71-
# AzureClient.get_sql_servers() returns [] both for a subscription with
72-
# no SQL servers and when the list call itself failed (authorization,
73-
# network, throttling), and scan() cannot tell them apart. UNKNOWN is
74-
# the conservative record: it never counts as a pass in compliance
75-
# scoring, whereas an empty findings list would look compliant.
76-
return [
77-
RuleEvaluation(
78-
rule_id=RULE_ID,
79-
resource_id=subscription_scope_id(subscription_id),
80-
resource_type="Microsoft.Sql/servers",
81-
status=EvaluationStatus.UNKNOWN,
82-
reason_code="INVENTORY_EMPTY_OR_UNAVAILABLE",
83-
reason=(
84-
"No Azure SQL servers were returned; this is indistinguishable from a failed "
85-
"list call (authorization, network or throttling), so coverage is unknown."
86-
),
87-
)
88-
]
77+
return [no_resources_found(RULE_ID, RESOURCE_TYPE, subscription_id)]
8978

9079
evaluations: List[RuleEvaluation] = []
9180
for server in servers:
9281
tls_version = getattr(server, "minimal_tls_version", None)
82+
evidence = {"minimal_tls_version": tls_version}
9383
if tls_version in _COMPLIANT_VERSIONS:
9484
evaluations.append(
9585
RuleEvaluation(
9686
rule_id=RULE_ID,
9787
resource_id=server.id,
98-
resource_type="Microsoft.Sql/servers",
88+
resource_type=RESOURCE_TYPE,
9989
status=EvaluationStatus.PASS,
90+
evidence=evidence,
10091
)
10192
)
10293
else:
10394
evaluations.append(
10495
RuleEvaluation(
10596
rule_id=RULE_ID,
10697
resource_id=server.id,
107-
resource_type="Microsoft.Sql/servers",
98+
resource_type=RESOURCE_TYPE,
10899
status=EvaluationStatus.FAIL,
100+
evidence=evidence,
109101
finding=_finding(azure_client, server, tls_version),
110102
)
111103
)

‎tests/helpers/mock_azure.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __init__(self) -> None:
5252
self._virtual_machines: List[Any] = []
5353
self._virtual_machine_scale_sets: List[Any] = []
5454
self._key_vaults: Optional[List[Any]] = []
55-
self._sql_servers: List[Any] = []
55+
self._sql_servers: Optional[List[Any]] = []
5656
self._service_principals: List[Any] = []
5757
self._sql_firewall_rules: Dict[Tuple[str, str], List[Any]] = {}
5858
# --- Additional state added for full rule-coverage tests ---------- #
@@ -222,7 +222,8 @@ def set_key_vaults(self, vaults: Optional[List[Any]]) -> "MockAzureClient":
222222
self._key_vaults = vaults
223223
return self
224224

225-
def set_sql_servers(self, servers: List[Any]) -> "MockAzureClient":
225+
def set_sql_servers(self, servers: Optional[List[Any]]) -> "MockAzureClient":
226+
"""``None`` simulates a failed list call, as AzureClient.list_sql_servers() reports it."""
226227
self._sql_servers = servers
227228
return self
228229

@@ -257,9 +258,12 @@ def list_key_vaults(self) -> Optional[List[Any]]:
257258
def get_key_vaults(self) -> List[Any]:
258259
return self._key_vaults or []
259260

260-
def get_sql_servers(self) -> List[Any]:
261+
def list_sql_servers(self) -> Optional[List[Any]]:
261262
return self._sql_servers
262263

264+
def get_sql_servers(self) -> List[Any]:
265+
return self._sql_servers or []
266+
263267
def get_service_principals(self) -> List[Any]:
264268
return self._service_principals
265269

‎tests/test_azure_client_management.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def test_list_wrappers_return_results_and_fail_closed(client, method, patch_targ
6262
[
6363
("list_storage_accounts", "scanner.azure_client.StorageManagementClient", "storage_accounts.list"),
6464
("list_key_vaults", "scanner.azure_client.KeyVaultManagementClient", "vaults.list_by_subscription"),
65+
("list_sql_servers", "scanner.azure_client.SqlManagementClient", "servers.list"),
6566
],
6667
)
6768
def test_fallible_list_wrappers_distinguish_empty_from_failure(client, method, patch_target, operation):

‎tests/test_rule_evaluation_contract.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,20 @@ def vault(name: str, **props: Any) -> Any:
6868
)
6969

7070

71+
def _db_008_fixture() -> MockAzureClient:
72+
def server(name: str, tls: str) -> Any:
73+
return make_resource(
74+
id=f"/subscriptions/{_SUB}/resourceGroups/rg/providers/Microsoft.Sql/servers/{name}",
75+
name=name,
76+
location="eastus",
77+
minimal_tls_version=tls,
78+
)
79+
80+
return MockAzureClient().set_sql_servers([server("sql-tls12", "1.2"), server("sql-tls10", "1.0")])
81+
82+
7183
FIXTURES: Dict[str, Callable[[], MockAzureClient]] = {
84+
"AZ-DB-008": _db_008_fixture,
7285
"AZ-KV-006": _kv_006_fixture,
7386
}
7487

‎tests/test_rules_database.py‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -337,13 +337,28 @@ def test_db_008_evaluate_unset_tls_is_fail_not_skipped(mock_azure, subscription_
337337
assert [e.status for e in evaluations] == [EvaluationStatus.FAIL]
338338

339339

340-
def test_db_008_failed_or_empty_inventory_is_unknown_not_clean(mock_azure, subscription_id):
341-
"""get_sql_servers() returns [] on an API failure as well as for a genuinely empty
342-
subscription. scan() alone would look like a clean result either way, so evaluate()
343-
must record UNKNOWN (never a PASS) for that case."""
340+
def test_db_008_failed_inventory_is_error_not_a_clean_scan(mock_azure, subscription_id):
341+
"""list_sql_servers() returns None when the list call fails; that must never read as
342+
compliant, so evaluate() reports ERROR and scan() alone cannot be mistaken for a pass."""
343+
mock_azure.set_sql_servers(None)
344+
result = az_db_008.evaluate(mock_azure, subscription_id)
345+
assert [(e.status, e.reason_code) for e in result] == [(EvaluationStatus.ERROR, "INVENTORY_UNAVAILABLE")]
346+
assert result[0].resource_id == f"/subscriptions/{subscription_id}"
347+
assert az_db_008.scan(mock_azure, subscription_id) == []
348+
349+
350+
def test_db_008_empty_inventory_is_not_applicable(mock_azure, subscription_id):
344351
mock_azure.set_sql_servers([])
345-
evaluations = az_db_008.evaluate(mock_azure, subscription_id)
346-
assert len(evaluations) == 1
347-
assert evaluations[0].status == EvaluationStatus.UNKNOWN
348-
assert evaluations[0].reason_code == "INVENTORY_EMPTY_OR_UNAVAILABLE"
349-
assert evaluations[0].resource_id == f"/subscriptions/{subscription_id}"
352+
result = az_db_008.evaluate(mock_azure, subscription_id)
353+
assert [(e.status, e.reason_code) for e in result] == [(EvaluationStatus.NOT_APPLICABLE, "NO_RESOURCES_FOUND")]
354+
355+
356+
def test_db_008_evaluate_records_the_inspected_value_as_evidence(mock_azure, subscription_id):
357+
good = make_resource(id=_sql_id("sql-good"), name="sql-good", minimal_tls_version="1.3")
358+
bad = make_resource(id=_sql_id("sql-bad"), name="sql-bad", minimal_tls_version="None")
359+
mock_azure.set_sql_servers([good, bad])
360+
evidence = {e.resource_id: e.evidence for e in az_db_008.evaluate(mock_azure, subscription_id)}
361+
assert evidence == {
362+
_sql_id("sql-good"): {"minimal_tls_version": "1.3"},
363+
_sql_id("sql-bad"): {"minimal_tls_version": "None"},
364+
}

0 commit comments

Comments
 (0)