@mandateops/cli-linux-x64 0.0.0-stage → 0.7.2

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 (68) hide show
  1. package/LICENSE +55 -0
  2. package/README.md +181 -2
  3. package/audit-lab/README.md +194 -0
  4. package/audit-lab/bin/mandateops +0 -0
  5. package/audit-lab/contracts/contact-plain-text.vectors.json +85 -0
  6. package/audit-lab/contracts/history-reading.md +185 -0
  7. package/audit-lab/contracts/history-reading.vectors.json +284 -0
  8. package/audit-lab/contracts/profiles.md +161 -0
  9. package/audit-lab/contracts/properties.md +512 -0
  10. package/audit-lab/contracts/sdk-config.md +155 -0
  11. package/audit-lab/contracts/system-under-test.md +385 -0
  12. package/audit-lab/contracts/test-provider.md +350 -0
  13. package/audit-lab/lab/compose.audit.yml +181 -0
  14. package/audit-lab/lab/config/fault-control.json +14 -0
  15. package/audit-lab/lab/examples/agents/Dockerfile +28 -0
  16. package/audit-lab/lab/examples/agents/codeloc.cjs +199 -0
  17. package/audit-lab/lab/examples/agents/load.cjs +9 -0
  18. package/audit-lab/lab/examples/agents/package-lock.json +2840 -0
  19. package/audit-lab/lab/examples/agents/package.json +9 -0
  20. package/audit-lab/lab/examples/agents/python/mandateops_codeloc.py +271 -0
  21. package/audit-lab/lab/examples/agents/python/sitecustomize.py +51 -0
  22. package/audit-lab/lab/examples/agents/requirements-python.txt +522 -0
  23. package/audit-lab/lab/examples/agents/telemetry-router.cjs +272 -0
  24. package/audit-lab/lab/examples/test-provider/.dockerignore +4 -0
  25. package/audit-lab/lab/examples/test-provider/Dockerfile +28 -0
  26. package/audit-lab/lab/examples/test-provider/bun.lock +405 -0
  27. package/audit-lab/lab/examples/test-provider/package.json +42 -0
  28. package/audit-lab/lab/examples/test-provider/src/amount.ts +32 -0
  29. package/audit-lab/lab/examples/test-provider/src/app.ts +538 -0
  30. package/audit-lab/lab/examples/test-provider/src/auth.ts +30 -0
  31. package/audit-lab/lab/examples/test-provider/src/barrier.ts +262 -0
  32. package/audit-lab/lab/examples/test-provider/src/codeloc.ts +27 -0
  33. package/audit-lab/lab/examples/test-provider/src/config.ts +311 -0
  34. package/audit-lab/lab/examples/test-provider/src/connect.ts +204 -0
  35. package/audit-lab/lab/examples/test-provider/src/declaration.ts +270 -0
  36. package/audit-lab/lab/examples/test-provider/src/declared.ts +198 -0
  37. package/audit-lab/lab/examples/test-provider/src/domain.ts +193 -0
  38. package/audit-lab/lab/examples/test-provider/src/egress.ts +396 -0
  39. package/audit-lab/lab/examples/test-provider/src/flow.ts +263 -0
  40. package/audit-lab/lab/examples/test-provider/src/gate.ts +301 -0
  41. package/audit-lab/lab/examples/test-provider/src/main.ts +31 -0
  42. package/audit-lab/lab/examples/test-provider/src/notify.ts +157 -0
  43. package/audit-lab/lab/examples/test-provider/src/owner.ts +60 -0
  44. package/audit-lab/lab/examples/test-provider/src/payment.ts +103 -0
  45. package/audit-lab/lab/examples/test-provider/src/profile.ts +417 -0
  46. package/audit-lab/lab/examples/test-provider/src/request.ts +201 -0
  47. package/audit-lab/lab/examples/test-provider/src/rows.ts +173 -0
  48. package/audit-lab/lab/examples/test-provider/src/schema.ts +114 -0
  49. package/audit-lab/lab/examples/test-provider/src/signature.ts +86 -0
  50. package/audit-lab/lab/examples/test-provider/src/staged.ts +318 -0
  51. package/audit-lab/lab/examples/test-provider/src/store.ts +432 -0
  52. package/audit-lab/lab/examples/test-provider/src/traffic.ts +50 -0
  53. package/audit-lab/lab/examples/test-provider/src/webauthn.d.ts +11 -0
  54. package/audit-lab/lab/examples/test-provider/src/wire.ts +153 -0
  55. package/audit-lab/lab/examples/test-provider/tsconfig.build.json +9 -0
  56. package/audit-lab/lab/examples/test-provider/tsconfig.json +24 -0
  57. package/audit-lab/lab/images/capture/Dockerfile +15 -0
  58. package/audit-lab/lab/images/capture/LICENSE +201 -0
  59. package/audit-lab/lab/images/fault-control/Dockerfile +13 -0
  60. package/audit-lab/lab/images/fault-control/LICENSE +21 -0
  61. package/audit-lab/lab/images/provider-store/Dockerfile +4 -0
  62. package/audit-lab/lab/images/provider-store/LICENSE +23 -0
  63. package/audit-lab/lab/images/telemetry/Dockerfile +14 -0
  64. package/audit-lab/lab/images/telemetry/LICENSE +201 -0
  65. package/audit-lab/lab/scripts/compose-down.sh +148 -0
  66. package/audit-lab/lab/scripts/compose-lib.sh +663 -0
  67. package/audit-lab/lab/scripts/compose-up.sh +240 -0
  68. package/package.json +16 -4
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "mandateops-agents",
3
+ "private": true,
4
+ "description": "OpenTelemetry auto-instrumentation the lab mounts into Node backends.",
5
+ "dependencies": {
6
+ "@opentelemetry/api": "1.10.0",
7
+ "@opentelemetry/auto-instrumentations-node": "0.82.0"
8
+ }
9
+ }
@@ -0,0 +1,271 @@
1
+ """Code locations for calls to the provider, for a Python backend.
2
+
3
+ Imported by this directory's sitecustomize in a lab run or under
4
+ `mandateops run`. For each request to the provider's address, and no other,
5
+ it adds one header, x-mandateops-call, carrying a random id, and writes one
6
+ line with that id and the stack frames in the project's own code, as paths
7
+ relative to the project root. requests (through urllib3), urllib3, httpx and
8
+ aiohttp are wrapped as each is imported.
9
+
10
+ It never reads, changes or logs a body, a header value or a URL path, makes no
11
+ network call of its own, and never stops the backend: any failure leaves the
12
+ request as it was. It uses the standard library alone.
13
+ """
14
+
15
+ import importlib.abc
16
+ import importlib.util
17
+ import json
18
+ import os
19
+ import re
20
+ import sys
21
+ import sysconfig
22
+ import traceback
23
+ import uuid
24
+
25
+ HEADER = "x-mandateops-call"
26
+ LINE_PREFIX = "mo-loc "
27
+ MAX_FRAMES = 8
28
+ MAX_FUNCTION = 120
29
+ MAX_PATH = 300
30
+ _SELF = os.path.abspath(__file__)
31
+ _SAFE_NAME = re.compile(r"[^\w.<> -]")
32
+
33
+
34
+ def _default_port(scheme):
35
+ return 443 if scheme == "https" else 80
36
+
37
+
38
+ def parse_origins(value):
39
+ """`host:port` pairs, lower-cased; ports default by scheme."""
40
+ from urllib.parse import urlsplit
41
+
42
+ origins = set()
43
+ for entry in (value or "").split(","):
44
+ entry = entry.strip().lower()
45
+ if not entry:
46
+ continue
47
+ try:
48
+ parts = urlsplit(entry if "://" in entry else "http://" + entry)
49
+ if parts.hostname:
50
+ origins.add(f"{parts.hostname}:{parts.port or _default_port(parts.scheme)}")
51
+ except ValueError:
52
+ continue
53
+ return origins
54
+
55
+
56
+ def _excluded_roots():
57
+ roots = set()
58
+ for key in ("stdlib", "platstdlib", "purelib", "platlib"):
59
+ path = sysconfig.get_paths().get(key)
60
+ if path:
61
+ roots.add(os.path.abspath(path) + os.sep)
62
+ return roots
63
+
64
+
65
+ def project_path(filename, root, excluded=()):
66
+ """A frame's path relative to `root`, or None when it is not project code."""
67
+ if not filename or filename.startswith("<"):
68
+ return None
69
+ path = os.path.abspath(filename)
70
+ if path == _SELF or os.path.dirname(path) == os.path.dirname(_SELF):
71
+ return None
72
+ parts = path.split(os.sep)
73
+ if "site-packages" in parts or "dist-packages" in parts:
74
+ return None
75
+ if any(path.startswith(prefix) for prefix in excluded):
76
+ return None
77
+ base = root.rstrip(os.sep) + os.sep
78
+ if not path.startswith(base):
79
+ return None
80
+ relative = path[len(base):]
81
+ if not relative or len(relative) > MAX_PATH:
82
+ return None
83
+ if any(part in ("", "..") for part in relative.split(os.sep)):
84
+ return None
85
+ return relative.replace(os.sep, "/")
86
+
87
+
88
+ def project_frames(stack, root, excluded=()):
89
+ """The project's own frames of `stack` (oldest first), newest first."""
90
+ frames = []
91
+ for summary in reversed(stack):
92
+ path = project_path(summary.filename, root, excluded)
93
+ if path is None or not summary.lineno or summary.lineno < 1:
94
+ continue
95
+ frame = {"file": path, "line": summary.lineno}
96
+ name = _SAFE_NAME.sub("", summary.name or "")[:MAX_FUNCTION]
97
+ if name and name != "<module>":
98
+ frame["function"] = name
99
+ frames.append(frame)
100
+ if len(frames) >= MAX_FRAMES:
101
+ break
102
+ return frames
103
+
104
+
105
+ class _Recorder:
106
+ def __init__(self, env):
107
+ self.origins = parse_origins(env.get("MANDATEOPS_PROVIDER_ORIGINS"))
108
+ self.root = os.path.abspath(env.get("MANDATEOPS_PROJECT_ROOT") or os.getcwd())
109
+ self.file = env.get("MANDATEOPS_CODELOC_FILE")
110
+ self.excluded = _excluded_roots()
111
+
112
+ def watches(self, host, port, scheme="http"):
113
+ if not host:
114
+ return False
115
+ port = port or _default_port(scheme)
116
+ return f"{str(host).lower()}:{port}" in self.origins
117
+
118
+ def record(self, method):
119
+ call = str(uuid.uuid4())
120
+ line = json.dumps(
121
+ {
122
+ "call": call,
123
+ "runtime": "python",
124
+ "method": str(method or "GET").upper()[:10],
125
+ "frames": project_frames(traceback.extract_stack(), self.root, self.excluded),
126
+ },
127
+ separators=(",", ":"),
128
+ )
129
+ try:
130
+ if self.file:
131
+ descriptor = os.open(self.file, os.O_WRONLY | os.O_APPEND | os.O_CREAT, 0o600)
132
+ with os.fdopen(descriptor, "a") as sink:
133
+ sink.write(line + "\n")
134
+ else:
135
+ sys.stderr.write(LINE_PREFIX + line + "\n")
136
+ except OSError:
137
+ pass
138
+ return call
139
+
140
+
141
+ def _with_header(headers, call):
142
+ """A copy of `headers` with the id added; the caller's mapping is untouched."""
143
+ copied = dict(headers or {})
144
+ copied[HEADER] = call
145
+ return copied
146
+
147
+
148
+ def _has_header(headers):
149
+ return any(str(name).lower() == HEADER for name in (headers or {}))
150
+
151
+
152
+ def _patch_urllib3(module, recorder):
153
+ pool = module.HTTPConnectionPool
154
+ original = pool.urlopen
155
+
156
+ def urlopen(self, method, url, body=None, headers=None, *args, **kwargs):
157
+ try:
158
+ if recorder.watches(self.host, self.port, self.scheme) and not _has_header(headers):
159
+ headers = _with_header(headers, recorder.record(method))
160
+ except Exception: # noqa: BLE001
161
+ pass
162
+ return original(self, method, url, body, headers, *args, **kwargs)
163
+
164
+ pool.urlopen = urlopen
165
+
166
+
167
+ def _tag_httpx(request, recorder):
168
+ try:
169
+ url = request.url
170
+ if recorder.watches(url.host, url.port, url.scheme) and HEADER not in request.headers:
171
+ request.headers[HEADER] = recorder.record(request.method)
172
+ except Exception: # noqa: BLE001
173
+ pass
174
+
175
+
176
+ def _patch_httpx(module, recorder):
177
+ sync_send = module.Client.send
178
+ async_send = module.AsyncClient.send
179
+
180
+ def send(self, request, *args, **kwargs):
181
+ _tag_httpx(request, recorder)
182
+ return sync_send(self, request, *args, **kwargs)
183
+
184
+ async def asend(self, request, *args, **kwargs):
185
+ _tag_httpx(request, recorder)
186
+ return await async_send(self, request, *args, **kwargs)
187
+
188
+ module.Client.send = send
189
+ module.AsyncClient.send = asend
190
+
191
+
192
+ def _patch_aiohttp(module, recorder):
193
+ session = module.ClientSession
194
+ original = session._request
195
+
196
+ async def _request(self, method, str_or_url, **kwargs):
197
+ try:
198
+ from urllib.parse import urlsplit
199
+
200
+ parts = urlsplit(str(str_or_url))
201
+ headers = kwargs.get("headers")
202
+ if recorder.watches(parts.hostname, parts.port, parts.scheme) and not _has_header(headers):
203
+ kwargs["headers"] = _with_header(headers, recorder.record(method))
204
+ except Exception: # noqa: BLE001
205
+ pass
206
+ return await original(self, method, str_or_url, **kwargs)
207
+
208
+ session._request = _request
209
+
210
+
211
+ PATCHES = {"urllib3": _patch_urllib3, "httpx": _patch_httpx, "aiohttp": _patch_aiohttp}
212
+
213
+
214
+ class _AfterImport(importlib.abc.MetaPathFinder):
215
+ """Patches each wrapped library once, right after it is first imported."""
216
+
217
+ def __init__(self, recorder):
218
+ self.recorder = recorder
219
+ self.busy = set()
220
+
221
+ def find_spec(self, name, path=None, target=None):
222
+ if name not in PATCHES or name in self.busy:
223
+ return None
224
+ self.busy.add(name)
225
+ try:
226
+ spec = importlib.util.find_spec(name)
227
+ finally:
228
+ self.busy.discard(name)
229
+ if spec is None or spec.loader is None:
230
+ return None
231
+ loader = spec.loader
232
+ recorder = self.recorder
233
+ original_exec = loader.exec_module
234
+
235
+ def exec_module(module):
236
+ original_exec(module)
237
+ try:
238
+ PATCHES[name](module, recorder)
239
+ except Exception as error: # noqa: BLE001
240
+ print(f"mandateops: code locations not added to {name}: {error}", file=sys.stderr)
241
+
242
+ spec.loader = _Loader(loader, exec_module)
243
+ return spec
244
+
245
+
246
+ class _Loader(importlib.abc.Loader):
247
+ def __init__(self, inner, exec_module):
248
+ self.inner = inner
249
+ self._exec = exec_module
250
+
251
+ def create_module(self, spec):
252
+ return self.inner.create_module(spec)
253
+
254
+ def exec_module(self, module):
255
+ self._exec(module)
256
+
257
+ def __getattr__(self, name):
258
+ # Resource readers and the like stay the library's own.
259
+ return getattr(self.inner, name)
260
+
261
+
262
+ def install(env=None):
263
+ """Starts watching when the provider's address is set; True if it did."""
264
+ recorder = _Recorder(os.environ if env is None else env)
265
+ if not recorder.origins:
266
+ return False
267
+ for name, patch in PATCHES.items():
268
+ if name in sys.modules:
269
+ patch(sys.modules[name], recorder)
270
+ sys.meta_path.insert(0, _AfterImport(recorder))
271
+ return True
@@ -0,0 +1,51 @@
1
+ # Imported by every Python process of a lab run, through PYTHONPATH naming
2
+ # this directory alone. It must not change the backend under test:
3
+ # - the backend's own sitecustomize, if it has one, still runs;
4
+ # - the agent's packages go to the end of sys.path, so the backend's own
5
+ # packages always win;
6
+ # - any failure to start the agent is reported and ignored.
7
+ import importlib.machinery
8
+ import importlib.util
9
+ import os
10
+ import sys
11
+
12
+ _here = os.path.dirname(os.path.abspath(__file__))
13
+
14
+
15
+ def _run_backend_sitecustomize():
16
+ path = [entry for entry in sys.path if os.path.abspath(entry or ".") != _here]
17
+ spec = importlib.machinery.PathFinder.find_spec("sitecustomize", path)
18
+ if spec is None or spec.loader is None:
19
+ return
20
+ module = importlib.util.module_from_spec(spec)
21
+ sys.modules["sitecustomize"] = module
22
+ spec.loader.exec_module(module)
23
+
24
+
25
+ try:
26
+ _run_backend_sitecustomize()
27
+ except Exception as error: # noqa: BLE001
28
+ print(f"mandateops: the backend's sitecustomize failed: {error}", file=sys.stderr)
29
+ raise
30
+
31
+ # `mandateops run` ships this file without the agent and says so.
32
+ if os.environ.get("MANDATEOPS_OTEL") != "off":
33
+ sys.path.append(os.path.join(_here, "site"))
34
+ try:
35
+ from opentelemetry.instrumentation.auto_instrumentation import ( # noqa: F401
36
+ initialize,
37
+ )
38
+
39
+ initialize()
40
+ except Exception as error: # noqa: BLE001
41
+ print(f"mandateops: OpenTelemetry agent not loaded: {error}", file=sys.stderr)
42
+
43
+ # Code locations for calls to the provider: standard library only, and only
44
+ # when the run names the provider's address.
45
+ # PYTHONPATH names this directory, so the module is found beside this file.
46
+ try:
47
+ import mandateops_codeloc
48
+
49
+ mandateops_codeloc.install()
50
+ except Exception as error: # noqa: BLE001
51
+ print(f"mandateops: code locations not loaded: {error}", file=sys.stderr)