"""The Well-Architected review in code: checks, roll-up and rendering, against stand-in tool replies (no runtime).""" import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from app import waf # noqa: E402 VM = "//compute.googleapis.com/projects/p1/zones/europe-west2-b/instances/vm-1" BUCKET = "//storage.googleapis.com/bucket-1" def ok(items, token=None): return {"success": True, "data": {"items": items, "next_page_token": token, "evidence_ref": "sha256:x"}} def gcp_call(calls): vm = {"name": VM, "assetType": "compute.googleapis.com/Instance", "labels": {}, "versionedResources": [{"resource": {"data": {"status": "TERMINATED", "deletionProtection": False, "shieldedInstanceConfig": {"enableSecureBoot": False}, "networkInterfaces": [{"network": "n"}]}}}]} bucket = {"name": BUCKET, "assetType": "storage.googleapis.com/Bucket", "labels": {"owner": "platform"}, "versionedResources": [{"resource": {"data": {"iamConfiguration": {"publicAccessPrevention": "inherited"}}}}]} def call(tool, args): calls.append((tool, args)) if tool == "gcp_list_servers": return ok([vm]) if tool == "gcp_list_data_stores": return ok([bucket] if args.get("kind") == "cloud_storage" else []) if tool == "gcp_list_organization_policies": return {"success": False, "error": {"code": "upstream_failed", "message": "403"}} if tool == "gcp_list_recommendations": if args["recommender"] == "google.compute.instance.IdleResourceRecommender": return ok([{"pillar": "cost", "priority": "P2", "description": "Delete idle VM vm-1"}]) return ok([]) if tool == "gcp_search_well_architected": return {"success": True, "data": {"results": [{"title": "Design", "text": "Guidance text."}], "evidence_ref": "sha256:g"}} return ok([]) return call def by_id(result): return {c["check_id"]: c for c in result["checks"]} def test_sample_is_not_applicable_and_calls_nothing(): calls = [] r = waf.review({"engagement_id": "E", "run_id": "R", "provider": "sample"}, lambda t, a: calls.append(t)) assert r["status"] == "not_applicable" and calls == [] and "not applicable" in r["waf_markdown"].lower() def test_gcp_checks_match_the_live_estate(): calls = [] r = waf.review({"engagement_id": "E", "run_id": "R", "provider": "gcp", "records": [{"scope_entry_id": VM}, {"scope_entry_id": BUCKET}]}, gcp_call(calls)) c = by_id(r) assert r["status"] == "assessed" assert c["SEC-01"]["result"] == "pass" # no firewall rule opens a port assert c["SEC-03"]["affected"] == ["vm-1"] # Secure Boot off assert c["SEC-04"]["affected"] == ["bucket-1"] # public access not enforced assert c["SEC-05"]["result"] == "no_evidence" # the policy call was refused: not a pass assert c["REL-02"]["affected"] == ["vm-1"] # deletion protection off assert c["COST-01"]["affected"] == ["vm-1"] # TERMINATED assert c["COST-02"]["affected"] == ["vm-1"] # the bucket has an owner label assert r["recommendations"]["by_pillar"] == {"cost": {"high": 1}} assert any("gcp_list_organization_policies" in i for i in r["call_issues"]) assert not any(t.startswith("azure_") for t, _ in calls) # one cloud only zones = {a["location"] for t, a in calls if t == "gcp_list_recommendations" and "instance" in a["recommender"]} assert zones == {"europe-west2-b"} def test_pillar_with_no_evidence_is_not_assessed(): r = waf.review({"engagement_id": "E", "run_id": "R", "provider": "gcp", "records": []}, lambda t, a: ok([])) perf = next(p for p in r["pillars"] if p["pillar"] == "performance") assert perf["status"] == "not_assessed" assert "This is not a pass" in r["waf_markdown"] def test_azure_open_rdp_and_public_storage(): nsg = {"name": "nsg-1", "id": "/subscriptions/s/resourceGroups/g/providers/Microsoft.Network/networkSecurityGroups/nsg-1", "properties": {"securityRules": [{"name": "rdp", "properties": {"access": "Allow", "direction": "Inbound", "sourceAddressPrefix": "*", "destinationPortRange": "3389"}}]}} sa = {"id": "/subscriptions/s/resourceGroups/g/providers/Microsoft.Storage/storageAccounts/sa1", "type": "microsoft.storage/storageaccounts", "properties": {"allowBlobPublicAccess": True}} def call(tool, args): if tool == "azure_get_network_topology" and args.get("kind") == "network_security_groups": return ok([nsg]) if tool == "azure_list_data_stores" and args.get("kind") == "storage_accounts": return ok([sa]) if tool == "azure_list_recommendations": return ok([{"pillar": "security", "properties": {"impact": "High", "shortDescription": {"problem": "Enable MFA"}}}]) return ok([]) r = waf.review({"engagement_id": "E", "run_id": "R", "provider": "azure", "records": [{"scope_entry_id": sa["id"]}]}, call) c = by_id(r) assert c["SEC-01"]["affected"] == ["nsg-1/rdp: 3389"] assert c["SEC-04"]["affected"] == ["sa1"] assert r["recommendations"]["by_pillar"]["security"] == {"high": 1} def test_paging_reads_every_page(): pages = {None: ok([{"name": "a"}], "t2"), "t2": ok([{"name": "b"}])} seen = waf._all_pages(lambda t, a: pages[a.get("page_token")], "x", {}, [], []) assert [i["name"] for i in seen] == ["a", "b"] def test_html_escapes_values(): r = waf.review({"engagement_id": "