chimericmesh 0.2.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. chimericmesh/__init__.py +3 -0
  2. chimericmesh/__main__.py +119 -0
  3. chimericmesh/agent/__init__.py +21 -0
  4. chimericmesh/agent/codesearch.py +131 -0
  5. chimericmesh/agent/digest.py +131 -0
  6. chimericmesh/agent/errors.py +38 -0
  7. chimericmesh/agent/feed.py +47 -0
  8. chimericmesh/agent/limits.py +98 -0
  9. chimericmesh/agent/ports.py +32 -0
  10. chimericmesh/agent/routes.py +92 -0
  11. chimericmesh/agent/service.py +116 -0
  12. chimericmesh/agent/snippet.py +135 -0
  13. chimericmesh/agent/verify.py +215 -0
  14. chimericmesh/agent/watch.py +156 -0
  15. chimericmesh/api.py +626 -0
  16. chimericmesh/approvals/__init__.py +25 -0
  17. chimericmesh/approvals/errors.py +72 -0
  18. chimericmesh/approvals/format.py +76 -0
  19. chimericmesh/approvals/irc.py +54 -0
  20. chimericmesh/approvals/ledger.py +149 -0
  21. chimericmesh/approvals/limits.py +87 -0
  22. chimericmesh/approvals/manager.py +935 -0
  23. chimericmesh/approvals/matching.py +107 -0
  24. chimericmesh/approvals/models.py +251 -0
  25. chimericmesh/approvals/ports.py +36 -0
  26. chimericmesh/approvals/routes.py +220 -0
  27. chimericmesh/approvals/rulebook.py +337 -0
  28. chimericmesh/approvals/schema.py +64 -0
  29. chimericmesh/approvals/scope.py +104 -0
  30. chimericmesh/approvals/validate.py +215 -0
  31. chimericmesh/catalog/demo_apps.json +41 -0
  32. chimericmesh/catalog/legacy_apps.json +612 -0
  33. chimericmesh/commands.py +223 -0
  34. chimericmesh/connectors/__init__.py +1 -0
  35. chimericmesh/connectors/config.py +55 -0
  36. chimericmesh/connectors/framing.py +132 -0
  37. chimericmesh/connectors/limiter.py +42 -0
  38. chimericmesh/connectors/manager.py +252 -0
  39. chimericmesh/connectors/offsets.py +157 -0
  40. chimericmesh/connectors/schema.py +31 -0
  41. chimericmesh/connectors/submit.py +26 -0
  42. chimericmesh/connectors/syslog_tcp.py +141 -0
  43. chimericmesh/connectors/tailer.py +236 -0
  44. chimericmesh/connectors/tailread.py +288 -0
  45. chimericmesh/demo/__init__.py +1 -0
  46. chimericmesh/demo/agent.py +32 -0
  47. chimericmesh/demo/apps/__init__.py +1 -0
  48. chimericmesh/demo/apps/__main__.py +83 -0
  49. chimericmesh/demo/apps/common.py +163 -0
  50. chimericmesh/demo/apps/mail_bridge.py +48 -0
  51. chimericmesh/demo/apps/orders_api.py +31 -0
  52. chimericmesh/demo/apps/spooler.py +28 -0
  53. chimericmesh/demo/config.py +38 -0
  54. chimericmesh/demo/engine.py +68 -0
  55. chimericmesh/demo/env.py +46 -0
  56. chimericmesh/demo/hub.py +172 -0
  57. chimericmesh/demo/launcher.py +165 -0
  58. chimericmesh/demo/ports.py +44 -0
  59. chimericmesh/demo/routes.py +81 -0
  60. chimericmesh/demo/samples.py +32 -0
  61. chimericmesh/demo/scenario.py +95 -0
  62. chimericmesh/demo/supervisor.py +267 -0
  63. chimericmesh/demo/watch.py +46 -0
  64. chimericmesh/diagnostics.py +253 -0
  65. chimericmesh/discovery/__init__.py +17 -0
  66. chimericmesh/discovery/accept.py +131 -0
  67. chimericmesh/discovery/catalog.py +313 -0
  68. chimericmesh/discovery/collect.py +131 -0
  69. chimericmesh/discovery/dismissals.py +46 -0
  70. chimericmesh/discovery/engine.py +41 -0
  71. chimericmesh/discovery/errors.py +34 -0
  72. chimericmesh/discovery/expand.py +151 -0
  73. chimericmesh/discovery/manager.py +220 -0
  74. chimericmesh/discovery/match.py +177 -0
  75. chimericmesh/discovery/models.py +174 -0
  76. chimericmesh/discovery/parse.py +166 -0
  77. chimericmesh/discovery/probe.py +69 -0
  78. chimericmesh/discovery/routes.py +129 -0
  79. chimericmesh/discovery/runner.py +147 -0
  80. chimericmesh/discovery/schema.py +25 -0
  81. chimericmesh/discovery/text.py +53 -0
  82. chimericmesh/events.py +74 -0
  83. chimericmesh/fsperms.py +72 -0
  84. chimericmesh/hub.py +757 -0
  85. chimericmesh/listeners.py +162 -0
  86. chimericmesh/mcp_server.py +199 -0
  87. chimericmesh/mute/__init__.py +10 -0
  88. chimericmesh/mute/models.py +14 -0
  89. chimericmesh/mute/mutelist.py +51 -0
  90. chimericmesh/mute/routes.py +49 -0
  91. chimericmesh/mute/schema.py +29 -0
  92. chimericmesh/mute/store.py +94 -0
  93. chimericmesh/notify.py +47 -0
  94. chimericmesh/parsers/__init__.py +12 -0
  95. chimericmesh/parsers/formats.py +23 -0
  96. chimericmesh/parsers/irc.py +91 -0
  97. chimericmesh/parsers/logctx.py +143 -0
  98. chimericmesh/parsers/ncsa.py +57 -0
  99. chimericmesh/parsers/plain.py +17 -0
  100. chimericmesh/parsers/syslog.py +82 -0
  101. chimericmesh/parsers/syslogtext.py +62 -0
  102. chimericmesh/parsers/telnet.py +56 -0
  103. chimericmesh/parsers/w3c.py +58 -0
  104. chimericmesh/parsers/xferlog.py +31 -0
  105. chimericmesh/replay.py +108 -0
  106. chimericmesh/replay_irc.py +202 -0
  107. chimericmesh/services/__init__.py +39 -0
  108. chimericmesh/services/access.py +224 -0
  109. chimericmesh/services/asgi.py +107 -0
  110. chimericmesh/services/deps.py +42 -0
  111. chimericmesh/services/errors.py +49 -0
  112. chimericmesh/services/limiter.py +130 -0
  113. chimericmesh/services/models.py +160 -0
  114. chimericmesh/services/paths.py +139 -0
  115. chimericmesh/services/registry.py +638 -0
  116. chimericmesh/services/routes.py +161 -0
  117. chimericmesh/services/schema.py +48 -0
  118. chimericmesh/services/status.py +239 -0
  119. chimericmesh/services/tagging.py +63 -0
  120. chimericmesh/services/token.py +129 -0
  121. chimericmesh/services/validate.py +184 -0
  122. chimericmesh/store/__init__.py +43 -0
  123. chimericmesh/store/audit.py +89 -0
  124. chimericmesh/store/config.py +88 -0
  125. chimericmesh/store/connection.py +98 -0
  126. chimericmesh/store/errors.py +65 -0
  127. chimericmesh/store/events.py +430 -0
  128. chimericmesh/store/incidents.py +292 -0
  129. chimericmesh/store/migrations.py +109 -0
  130. chimericmesh/store/models.py +49 -0
  131. chimericmesh/store/ports.py +61 -0
  132. chimericmesh/store/proposals.py +215 -0
  133. chimericmesh/store/schema.py +102 -0
  134. chimericmesh/store/store.py +173 -0
  135. chimericmesh/store/tx.py +143 -0
  136. chimericmesh/store/writer.py +315 -0
  137. chimericmesh/web/__init__.py +1 -0
  138. chimericmesh/web/feed.py +212 -0
  139. chimericmesh/web/index.html +3775 -0
  140. chimericmesh/web/sse.py +74 -0
  141. chimericmesh/web/stream.py +64 -0
  142. chimericmesh-0.2.2.dist-info/METADATA +537 -0
  143. chimericmesh-0.2.2.dist-info/RECORD +147 -0
  144. chimericmesh-0.2.2.dist-info/WHEEL +5 -0
  145. chimericmesh-0.2.2.dist-info/entry_points.txt +2 -0
  146. chimericmesh-0.2.2.dist-info/licenses/LICENSE +21 -0
  147. chimericmesh-0.2.2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,215 @@
1
+ """Input validation for rules, proposals and completions. Pure and total: only
2
+ ``RuleInvalid`` or ``ProposalInvalid`` can escape, never another exception type."""
3
+ from __future__ import annotations
4
+
5
+ import math
6
+ import os
7
+ import re
8
+ from collections.abc import Mapping
9
+ from typing import Any
10
+
11
+ from chimericmesh.services.validate import SLUG
12
+
13
+ from .errors import ProposalInvalid, RuleInvalid
14
+ from .format import safe_text
15
+ from .limits import MAX_PROPOSALS_PER_SERVICE_HOUR
16
+ from .models import (
17
+ CATEGORIES, DEFAULT_EXPIRY_SECONDS, MATCH_TYPES, MAX_EXPIRY_SECONDS, VERIFICATION_RESULTS,
18
+ RuleFields, RulePatch,
19
+ )
20
+
21
+ _CONTROL = re.compile("[\\x00-\\x1f\\x7f-\\x9f\\ud800-\\udfff]")
22
+ NOTE_MAX = 200
23
+ MATCH_VALUE_MAX = 200
24
+ MAX_FILES_DECLARED = 20
25
+ MAX_FILE_CHARS = 300
26
+ MAX_VERIFICATION = 300
27
+ MAX_ESTIMATED_LINES = 1000
28
+ MAX_CHANGED_FILES = 50
29
+ MAX_LINES_CHANGED = 1_000_000
30
+ FORBIDDEN_RULE_KEYS = frozenset({"created_by", "status", "id", "use_count", "code_folder"})
31
+ _FIELD_KEYS = frozenset({"service_id", "match_type", "match_value", "most_urgent_severity", "expires_at",
32
+ "max_per_hour", "max_per_incident", "max_files", "max_lines", "note"})
33
+ _PATCH_KEYS = frozenset({"most_urgent_severity", "expires_at", "max_per_hour", "max_per_incident",
34
+ "max_files", "max_lines", "note"})
35
+
36
+
37
+ def clean_note(value: object) -> str:
38
+ """Control characters removed, at most 200 characters. Never raises."""
39
+ try:
40
+ text = "" if value is None else str(value)
41
+ except Exception: # noqa: BLE001
42
+ return ""
43
+ return _CONTROL.sub("", text)[:NOTE_MAX]
44
+
45
+
46
+ def _int(raw: object, field: str, low: int, high: int, error) -> int:
47
+ if isinstance(raw, bool) or not isinstance(raw, int) or not low <= raw <= high:
48
+ raise error(field, f"must be an integer from {low} to {high}")
49
+ return raw
50
+
51
+
52
+ def _check_keys(raw: Mapping[str, object], allowed: frozenset[str], error) -> None:
53
+ for key in raw:
54
+ if key in FORBIDDEN_RULE_KEYS:
55
+ raise error(str(key), "is set by the hub and cannot be given")
56
+ if key not in allowed:
57
+ raise error(str(key)[:40], "unknown field")
58
+
59
+
60
+ def _expiry(raw: object, *, now: float, created_at: float) -> float:
61
+ if isinstance(raw, bool) or not isinstance(raw, (int, float)) or not math.isfinite(raw):
62
+ raise RuleInvalid("expires_at", "must be a finite number of seconds since the epoch")
63
+ if raw <= now:
64
+ raise RuleInvalid("expires_at", "must be in the future")
65
+ if raw > created_at + MAX_EXPIRY_SECONDS:
66
+ raise RuleInvalid("expires_at", "may be at most 30 days after the rule was created")
67
+ return float(raw)
68
+
69
+
70
+ def validate_rule_fields(raw: Mapping[str, object], *, now: float) -> RuleFields:
71
+ try:
72
+ return _rule_fields(raw, now)
73
+ except RuleInvalid:
74
+ raise
75
+ except Exception as exc: # noqa: BLE001 - total: a surprise is an invalid request
76
+ raise RuleInvalid("body", "could not be read") from exc
77
+
78
+
79
+ def _rule_fields(raw: Mapping[str, object], now: float) -> RuleFields:
80
+ _check_keys(raw, _FIELD_KEYS, RuleInvalid)
81
+ sid = raw.get("service_id")
82
+ if not isinstance(sid, str) or SLUG.fullmatch(sid) is None or sid == "unassigned":
83
+ raise RuleInvalid("service_id", "must be the id of a registered service")
84
+ match_type = raw.get("match_type")
85
+ if match_type not in MATCH_TYPES:
86
+ raise RuleInvalid("match_type", "must be signature or category")
87
+ value = raw.get("match_value")
88
+ if not isinstance(value, str):
89
+ raise RuleInvalid("match_value", "must be text")
90
+ if match_type == "category":
91
+ if value not in CATEGORIES:
92
+ raise RuleInvalid("match_value", "must be one of " + ", ".join(CATEGORIES))
93
+ else:
94
+ if _CONTROL.search(value) or not value.strip():
95
+ raise RuleInvalid("match_value", "must be 1 to 200 characters without control characters")
96
+ if len(value) > MATCH_VALUE_MAX:
97
+ raise RuleInvalid("match_value", "must be 1 to 200 characters")
98
+ expires = raw.get("expires_at")
99
+ return RuleFields(
100
+ service_id=sid, match_type=str(match_type), match_value=value,
101
+ most_urgent_severity=_int(raw.get("most_urgent_severity", 3), "most_urgent_severity", 3, 4, RuleInvalid),
102
+ expires_at=now + DEFAULT_EXPIRY_SECONDS if expires is None else _expiry(expires, now=now, created_at=now),
103
+ max_per_hour=_int(raw.get("max_per_hour", 3), "max_per_hour", 1, MAX_PROPOSALS_PER_SERVICE_HOUR, RuleInvalid),
104
+ max_per_incident=_int(raw.get("max_per_incident", 1), "max_per_incident", 1, 3, RuleInvalid),
105
+ max_files=_int(raw.get("max_files", 5), "max_files", 1, 20, RuleInvalid),
106
+ max_lines=_int(raw.get("max_lines", 100), "max_lines", 1, 500, RuleInvalid),
107
+ note=clean_note(raw.get("note")),
108
+ )
109
+
110
+
111
+ def validate_rule_patch(raw: Mapping[str, object], *, now: float, created_at: float) -> RulePatch:
112
+ try:
113
+ _check_keys(raw, _PATCH_KEYS, RuleInvalid)
114
+ v: dict[str, Any] = {}
115
+ if "most_urgent_severity" in raw:
116
+ v["most_urgent_severity"] = _int(raw["most_urgent_severity"], "most_urgent_severity", 3, 4, RuleInvalid)
117
+ if "max_per_hour" in raw:
118
+ v["max_per_hour"] = _int(raw["max_per_hour"], "max_per_hour", 1, MAX_PROPOSALS_PER_SERVICE_HOUR, RuleInvalid)
119
+ if "max_per_incident" in raw:
120
+ v["max_per_incident"] = _int(raw["max_per_incident"], "max_per_incident", 1, 3, RuleInvalid)
121
+ if "max_files" in raw:
122
+ v["max_files"] = _int(raw["max_files"], "max_files", 1, 20, RuleInvalid)
123
+ if "max_lines" in raw:
124
+ v["max_lines"] = _int(raw["max_lines"], "max_lines", 1, 500, RuleInvalid)
125
+ if "expires_at" in raw:
126
+ v["expires_at"] = _expiry(raw["expires_at"], now=now, created_at=created_at)
127
+ if "note" in raw:
128
+ v["note"] = clean_note(raw["note"])
129
+ return RulePatch(**v)
130
+ except RuleInvalid:
131
+ raise
132
+ except Exception as exc: # noqa: BLE001
133
+ raise RuleInvalid("body", "could not be read") from exc
134
+
135
+
136
+ # ---- proposals and completions ---------------------------------------------------------------------
137
+
138
+ def _is_absolute(name: str) -> bool:
139
+ return (os.path.isabs(name) or name.startswith(("/", "\\")) or bool(re.match(r"^[A-Za-z]:", name)))
140
+
141
+
142
+ def _file_list(raw: object, field: str, limit: int, *, code_folder_given: bool) -> list[str]:
143
+ if not isinstance(raw, (list, tuple)):
144
+ raise ProposalInvalid(field, "must be a list of file names")
145
+ if len(raw) > limit:
146
+ raise ProposalInvalid(field, f"at most {limit} files")
147
+ out: list[str] = []
148
+ for item in raw:
149
+ if not isinstance(item, str):
150
+ raise ProposalInvalid(field, "every entry must be text")
151
+ if len(item) > MAX_FILE_CHARS:
152
+ raise ProposalInvalid(field, f"each entry may have at most {MAX_FILE_CHARS} characters")
153
+ if _CONTROL.search(item):
154
+ raise ProposalInvalid(field, "control characters are not allowed")
155
+ if code_folder_given and field == "files":
156
+ if _is_absolute(item) or ".." in re.split(r"[\\/]", item):
157
+ raise ProposalInvalid(field, "must be relative to the code folder, without ..")
158
+ out.append(item)
159
+ return out
160
+
161
+
162
+ def validate_new_proposal(raw: Mapping[str, object], *, code_folder: str | None) -> dict[str, object]:
163
+ """The normalized ``extra`` of a new proposal: only keys the caller sent."""
164
+ try:
165
+ out: dict[str, object] = {}
166
+ for key in raw:
167
+ if key not in ("kind", "files", "estimated_lines", "verification"):
168
+ raise ProposalInvalid(str(key)[:40], "unknown field")
169
+ if raw.get("kind") is not None:
170
+ if raw["kind"] != "code_change":
171
+ raise ProposalInvalid("kind", "must be code_change")
172
+ out["kind"] = "code_change"
173
+ if raw.get("files") is not None:
174
+ out["files"] = _file_list(raw["files"], "files", MAX_FILES_DECLARED,
175
+ code_folder_given=code_folder is not None)
176
+ if raw.get("estimated_lines") is not None:
177
+ out["estimated_lines"] = _int(raw["estimated_lines"], "estimated_lines", 0, MAX_ESTIMATED_LINES,
178
+ ProposalInvalid)
179
+ if raw.get("verification") is not None:
180
+ ver = raw["verification"]
181
+ if not isinstance(ver, str) or len(ver) > MAX_VERIFICATION:
182
+ raise ProposalInvalid("verification", f"must be text of at most {MAX_VERIFICATION} characters")
183
+ out["verification"] = safe_text(ver, MAX_VERIFICATION)
184
+ return out
185
+ except ProposalInvalid:
186
+ raise
187
+ except Exception as exc: # noqa: BLE001
188
+ raise ProposalInvalid("body", "could not be read") from exc
189
+
190
+
191
+ def validate_completion(raw: Mapping[str, object] | None) -> dict[str, object]:
192
+ try:
193
+ raw = raw or {}
194
+ out: dict[str, object] = {}
195
+ for key in raw:
196
+ if key not in ("files_changed", "lines_changed", "verification_result"):
197
+ raise ProposalInvalid(str(key)[:40], "unknown field")
198
+ if raw.get("files_changed") is not None:
199
+ out["files_changed"] = _file_list(raw["files_changed"], "files_changed", MAX_CHANGED_FILES,
200
+ code_folder_given=False)
201
+ if raw.get("lines_changed") is not None:
202
+ out["lines_changed"] = _int(raw["lines_changed"], "lines_changed", 0, MAX_LINES_CHANGED, ProposalInvalid)
203
+ if raw.get("verification_result") is not None:
204
+ if raw["verification_result"] not in VERIFICATION_RESULTS:
205
+ raise ProposalInvalid("verification_result", "must be passed, failed or not_run")
206
+ out["verification_result"] = raw["verification_result"]
207
+ return out
208
+ except ProposalInvalid:
209
+ raise
210
+ except Exception as exc: # noqa: BLE001
211
+ raise ProposalInvalid("body", "could not be read") from exc
212
+
213
+
214
+ __all__ = ["clean_note", "validate_completion", "validate_new_proposal", "validate_rule_fields",
215
+ "validate_rule_patch"]
@@ -0,0 +1,41 @@
1
+ {
2
+ "version": 1,
3
+ "apps": [
4
+ {
5
+ "app_id": "demo-orders-api",
6
+ "name": "Orders API (demo)",
7
+ "process_names": ["demo-orders-api"],
8
+ "ports": [],
9
+ "protocol_label": "syslog",
10
+ "justification": "Sample app of the demo. It sends Syslog over UDP and has no log file.",
11
+ "protocols": ["syslog-udp"],
12
+ "app_names": ["orders-api"],
13
+ "network_only": true,
14
+ "log_paths": {"linux": [], "windows": [], "macos": []}
15
+ },
16
+ {
17
+ "app_id": "demo-spooler",
18
+ "name": "Print spooler (demo)",
19
+ "process_names": ["demo-spooler"],
20
+ "ports": [],
21
+ "protocol_label": "telnet",
22
+ "justification": "Sample app of the demo. It sends its log over Telnet and has no log file.",
23
+ "protocols": ["telnet"],
24
+ "app_names": ["telnet"],
25
+ "network_only": true,
26
+ "log_paths": {"linux": [], "windows": [], "macos": []}
27
+ },
28
+ {
29
+ "app_id": "demo-mail-bridge",
30
+ "name": "Mail bridge (demo)",
31
+ "process_names": ["demo-mail-bridge"],
32
+ "ports": [],
33
+ "protocol_label": "irc",
34
+ "justification": "Sample app of the demo. It reports to the #ops IRC channel and has no log file.",
35
+ "protocols": ["irc"],
36
+ "app_names": ["#ops"],
37
+ "network_only": true,
38
+ "log_paths": {"linux": [], "windows": [], "macos": []}
39
+ }
40
+ ]
41
+ }