|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Validate traffic policy YAML/JSON in code blocks under traffic-policy docs. |
| 4 | +Reads source files only; no temp files. Reports failures as source path (block N). |
| 5 | +""" |
| 6 | +import json |
| 7 | +import re |
| 8 | +import sys |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +try: |
| 12 | + import yaml |
| 13 | +except ImportError: |
| 14 | + print("PyYAML required: pip install PyYAML", file=sys.stderr) |
| 15 | + sys.exit(2) |
| 16 | + |
| 17 | +ROOT = Path(__file__).resolve().parent.parent |
| 18 | +SEARCH_DIRS = [ |
| 19 | + ROOT / "traffic-policy", |
| 20 | + ROOT / "snippets" / "traffic-policy", |
| 21 | + ROOT / "universal-gateway", |
| 22 | +] |
| 23 | +POLICY_KEYS = ("on_http_request", "on_http_response", "on_tcp_connect") |
| 24 | + |
| 25 | + |
| 26 | +def find_source_files(): |
| 27 | + out = [] |
| 28 | + for d in SEARCH_DIRS: |
| 29 | + if not d.is_dir(): |
| 30 | + continue |
| 31 | + for p in d.rglob("*"): |
| 32 | + if p.suffix in (".mdx", ".md"): |
| 33 | + out.append(p) |
| 34 | + return sorted(out) |
| 35 | + |
| 36 | + |
| 37 | +def extract_blocks(path): |
| 38 | + """Yield (is_json, content) for each policy-like code block. Line-by-line to avoid regex cross-block bugs.""" |
| 39 | + lines = path.read_text().splitlines() |
| 40 | + i = 0 |
| 41 | + while i < len(lines): |
| 42 | + line = lines[i] |
| 43 | + if line.strip().startswith("```"): |
| 44 | + rest = line.strip()[3:].strip().lower() |
| 45 | + if rest.startswith("yaml"): |
| 46 | + kind = "yaml" |
| 47 | + elif rest.startswith("json"): |
| 48 | + kind = "json" |
| 49 | + else: |
| 50 | + i += 1 |
| 51 | + continue |
| 52 | + if "skip-validation" in rest or "skip_validation" in rest: |
| 53 | + i += 1 |
| 54 | + while i < len(lines) and lines[i].strip() != "```": |
| 55 | + i += 1 |
| 56 | + if i < len(lines): |
| 57 | + i += 1 |
| 58 | + continue |
| 59 | + i += 1 |
| 60 | + block = [] |
| 61 | + while i < len(lines) and lines[i].strip() != "```": |
| 62 | + block.append(lines[i]) |
| 63 | + i += 1 |
| 64 | + if i < len(lines): |
| 65 | + i += 1 # consume closing ``` |
| 66 | + content = "\n".join(block) |
| 67 | + if any(k in content for k in POLICY_KEYS): |
| 68 | + yield ("json" if kind == "json" else "yaml", content) |
| 69 | + continue |
| 70 | + i += 1 |
| 71 | + |
| 72 | + |
| 73 | +def validate_block(content, is_json): |
| 74 | + # is_json is the string "json" or "yaml" from extractor |
| 75 | + # Infer format from content so we never parse YAML as JSON |
| 76 | + raw = content.strip() |
| 77 | + if raw.startswith("{"): |
| 78 | + is_json = True |
| 79 | + elif raw.startswith("on_") or "\non_" in "\n" + raw: |
| 80 | + is_json = False |
| 81 | + else: |
| 82 | + is_json = is_json == "json" |
| 83 | + if is_json: |
| 84 | + try: |
| 85 | + d = json.loads(content) |
| 86 | + except json.JSONDecodeError as e: |
| 87 | + return str(e) |
| 88 | + else: |
| 89 | + try: |
| 90 | + d = yaml.safe_load(content) |
| 91 | + except yaml.YAMLError as e: |
| 92 | + return str(e).split("\n")[0] |
| 93 | + if d is None: |
| 94 | + return "empty document" |
| 95 | + if not isinstance(d, dict): |
| 96 | + return "root must be an object" |
| 97 | + if not any(k in d for k in POLICY_KEYS): |
| 98 | + # Allow agent config format: endpoints: [ { traffic_policy: { on_http_request: ... } } ] |
| 99 | + if "endpoints" in d and isinstance(d["endpoints"], list): |
| 100 | + for ep in d["endpoints"]: |
| 101 | + if isinstance(ep, dict) and "traffic_policy" in ep: |
| 102 | + tp = ep["traffic_policy"] |
| 103 | + if isinstance(tp, dict) and any(k in tp for k in POLICY_KEYS): |
| 104 | + return None |
| 105 | + # Allow API request body: { "traffic_policy": "{ \"on_http_request\": ... }", "bindings": ..., "type": "cloud" } |
| 106 | + if isinstance(d.get("traffic_policy"), str) and d.get("type") == "cloud": |
| 107 | + try: |
| 108 | + inner = json.loads(d["traffic_policy"]) |
| 109 | + if isinstance(inner, dict) and any(k in inner for k in POLICY_KEYS): |
| 110 | + return None |
| 111 | + except (json.JSONDecodeError, TypeError): |
| 112 | + pass |
| 113 | + return "missing policy key (need one of: " + ", ".join(POLICY_KEYS) + ")" |
| 114 | + return None |
| 115 | + |
| 116 | + |
| 117 | +def main(): |
| 118 | + sources = find_source_files() |
| 119 | + if not sources: |
| 120 | + print("No source files found.") |
| 121 | + return 0 |
| 122 | + |
| 123 | + total = 0 |
| 124 | + passed = 0 |
| 125 | + failed = 0 |
| 126 | + for path in sources: |
| 127 | + rel = path.relative_to(ROOT) |
| 128 | + for block_idx, (is_json, content) in enumerate(extract_blocks(path), 1): |
| 129 | + total += 1 |
| 130 | + err = validate_block(content, is_json) |
| 131 | + if err is None: |
| 132 | + passed += 1 |
| 133 | + print(f"✅ {rel} (block {block_idx})") |
| 134 | + else: |
| 135 | + failed += 1 |
| 136 | + print(f"❌ {rel} (block {block_idx}): {err}") |
| 137 | + |
| 138 | + print("") |
| 139 | + print("==========================================") |
| 140 | + print("SUMMARY") |
| 141 | + print("==========================================") |
| 142 | + print(f"Total: {total} | Passed: {passed} | Failed: {failed}") |
| 143 | + if failed: |
| 144 | + print("❌ Some blocks invalid.") |
| 145 | + return 1 |
| 146 | + print("🎉 All valid.") |
| 147 | + return 0 |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + sys.exit(main()) |
0 commit comments