File size: 7,432 Bytes
f3b0a91 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | """Workspace loading: limits, safe YAML (alias cap), PrometheusRule extraction, rule indexing.
Stdlib + PyYAML only: this module is copied verbatim into every task's tests/ directory.
"""
from __future__ import annotations
import hashlib
import json
import os
import re
from dataclasses import dataclass, field
import yaml
MAX_FILES = 300
MAX_BYTES = 1_000_000
MAX_ALIASES = 64
RULE_DIR = "rules"
AM_PATH = "alertmanager/alertmanager.yml"
class YamlError(Exception):
pass
class _CappedLoader(yaml.SafeLoader):
"""SafeLoader that refuses documents with more than MAX_ALIASES aliases (alias bombs, H12)."""
def compose_node(self, parent, index): # type: ignore[override]
if self.check_event(yaml.AliasEvent):
self._af_aliases = getattr(self, "_af_aliases", 0) + 1
if self._af_aliases > MAX_ALIASES:
raise YamlError("too many YAML aliases")
return super().compose_node(parent, index)
MAX_NODES = 200_000
def _bounded_size(obj) -> None:
"""Walk the loaded document as if aliases were expanded; bail out past MAX_NODES."""
stack, n = [obj], 0
while stack:
cur = stack.pop()
n += 1
if n > MAX_NODES:
raise YamlError("YAML document expands beyond the node limit")
if isinstance(cur, dict):
stack.extend(cur.keys())
stack.extend(cur.values())
elif isinstance(cur, list):
stack.extend(cur)
def safe_yaml(text: str):
try:
loader = _CappedLoader(text)
try:
data = loader.get_single_data()
finally:
loader.dispose()
except YamlError:
raise
except yaml.YAMLError as e:
raise YamlError(str(e).splitlines()[0] if str(e) else "yaml error") from e
except RecursionError as e:
raise YamlError("YAML nesting too deep") from e
_bounded_size(data)
return data
@dataclass
class RuleFile:
path: str # relative to the workspace root
groups: list | None = None # None when the file does not parse
error: str = ""
@dataclass
class Workspace:
root: str
tamper: list[str] = field(default_factory=list)
rule_files: list[RuleFile] = field(default_factory=list)
am_text: str | None = None
fingerprint: str = ""
def scan(root: str) -> Workspace:
"""Walk the workspace, apply limits (tamper on symlinks / size), and read rule and AM files."""
ws = Workspace(root=root)
digest = hashlib.sha256()
n = 0
for dirpath, dirnames, filenames in os.walk(root, followlinks=False):
dirnames.sort()
for d in list(dirnames):
if os.path.islink(os.path.join(dirpath, d)):
ws.tamper.append(f"symlink: {os.path.relpath(os.path.join(dirpath, d), root)}")
dirnames.remove(d)
for fn in sorted(filenames):
full = os.path.join(dirpath, fn)
rel = os.path.relpath(full, root)
if os.path.islink(full):
ws.tamper.append(f"symlink: {rel}")
continue
n += 1
size = os.path.getsize(full)
if size > MAX_BYTES:
ws.tamper.append(f"file too large: {rel}")
continue
if rel.startswith(RULE_DIR + os.sep) and rel.endswith((".yml", ".yaml")) \
and os.sep not in rel[len(RULE_DIR) + 1:]:
with open(full, "rb") as fh:
raw = fh.read()
digest.update(rel.encode() + b"\0" + raw + b"\0")
ws.rule_files.append(parse_rule_file(rel, raw.decode("utf-8", "replace")))
elif rel == AM_PATH:
with open(full, "rb") as fh:
raw = fh.read()
digest.update(rel.encode() + b"\0" + raw + b"\0")
ws.am_text = raw.decode("utf-8", "replace")
if n > MAX_FILES:
ws.tamper.append(f"too many files: {n}")
ws.fingerprint = digest.hexdigest()
return ws
def extract_groups(doc) -> list:
"""Plain rule file ({groups: [...]}) or a PrometheusRule manifest (.spec.groups) (U6)."""
if doc is None:
return []
if not isinstance(doc, dict):
raise YamlError("rule file is not a mapping")
if doc.get("kind") == "PrometheusRule":
spec = doc.get("spec") or {}
if not isinstance(spec, dict):
raise YamlError("PrometheusRule .spec is not a mapping")
groups = spec.get("groups") or []
else:
groups = doc.get("groups") or []
if not isinstance(groups, list) or not all(isinstance(g, dict) for g in groups):
raise YamlError("groups must be a list of mappings")
for g in groups:
rules = g.get("rules") or []
if not isinstance(rules, list) or not all(isinstance(r, dict) for r in rules):
raise YamlError(f"group {g.get('name')!r}: rules must be a list of mappings")
return groups
def parse_rule_file(rel: str, text: str) -> RuleFile:
try:
return RuleFile(rel, extract_groups(safe_yaml(text)))
except YamlError as e:
return RuleFile(rel, None, str(e))
def plain_yaml(groups: list) -> str:
return yaml.safe_dump({"groups": groups}, sort_keys=False, default_flow_style=False, width=10**6)
# ---------------------------------------------------------------------------- rule identity
_DUR = re.compile(r"^(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?$")
def dur_s(v) -> object:
"""Normalize a Prometheus duration to seconds; unknown forms are kept as strings."""
if v is None or v == "":
return 0
if isinstance(v, (int, float)):
return int(v)
m = _DUR.match(str(v).strip())
if not m or not any(m.groups()):
return str(v)
h, mi, s = (int(x or 0) for x in m.groups())
return h * 3600 + mi * 60 + s
def collapse(expr) -> str:
return re.sub(r"\s+", " ", str(expr)).strip()
def canon(rule: dict, group: dict) -> str:
"""Preservation hash of one rule, including its group-level knobs (H6)."""
kind = "alert" if "alert" in rule else "record"
d = {
"k": kind, "n": str(rule.get(kind)), "expr": collapse(rule.get("expr", "")),
"for": dur_s(rule.get("for")), "kff": dur_s(rule.get("keep_firing_for")),
"labels": {str(k): str(v) for k, v in (rule.get("labels") or {}).items()},
"ann": {str(k): str(v) for k, v in (rule.get("annotations") or {}).items()},
"g_interval": dur_s(group.get("interval")) or 60, "g_qo": dur_s(group.get("query_offset")),
"g_limit": group.get("limit") or 0,
}
return hashlib.sha256(json.dumps(d, sort_keys=True).encode()).hexdigest()
@dataclass
class RuleRef:
file: str
group: dict
rule: dict
def index_rules(files: list[RuleFile]) -> tuple[dict, dict, list[RuleRef]]:
"""(alerts by name, records by name, all rules) over parsed files."""
alerts: dict[str, list[RuleRef]] = {}
records: dict[str, list[RuleRef]] = {}
every: list[RuleRef] = []
for f in files:
for g in f.groups or []:
for r in g.get("rules") or []:
ref = RuleRef(f.path, g, r)
every.append(ref)
if "alert" in r:
alerts.setdefault(str(r["alert"]), []).append(ref)
elif "record" in r:
records.setdefault(str(r["record"]), []).append(ref)
return alerts, records, every
|