@mandateops/cli-darwin-arm64 0.0.0-stage → 0.7.1

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 +175 -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 +143 -0
  66. package/audit-lab/lab/scripts/compose-lib.sh +632 -0
  67. package/audit-lab/lab/scripts/compose-up.sh +240 -0
  68. package/package.json +16 -4
@@ -0,0 +1,632 @@
1
+ #!/usr/bin/env bash
2
+ # Shared run-identity rules for the audit lab Compose scripts. Sourced only.
3
+ set -euo pipefail
4
+
5
+ lab_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
6
+ readonly lab_root
7
+ readonly compose_file="$lab_root/compose.audit.yml"
8
+
9
+ # What a deployment that declares nothing gets. A hosted test declares its
10
+ # startup timeout; the others are optional for it too.
11
+ # shellcheck disable=SC2034 # read by the scripts that source this file
12
+ readonly default_startup_timeout_s=180 default_stop_timeout_s=10 default_port_reads=20 \
13
+ port_read_interval_s=0.5 default_log_limit_bytes=16777216
14
+
15
+ # compose-up.sh's exit when the project belongs to another run, whose deployment
16
+ # the caller must not take down. The caller names it, so one side states it.
17
+ # shellcheck disable=SC2034 # read by the scripts that source this file
18
+ readonly occupied_exit_code="${MANDATEOPS_OCCUPIED_EXIT_CODE:-1}"
19
+
20
+ # Ports inside the lab's own containers, on a network no customer service
21
+ # joins. Docker publishes them on free loopback ports, so nothing on the host
22
+ # or in a customer project can collide with them, and they stay fixed. The
23
+ # proxy port is part of the contract: a customer's provider URL names it, and
24
+ # config/fault-control.json states it and the provider port again, as that file
25
+ # cannot interpolate.
26
+ export MANDATEOPS_PROVIDER_CONTAINER_PORT=8080
27
+ export MANDATEOPS_PROXY_CONTAINER_PORT=8666
28
+ export MANDATEOPS_FAULT_CONTROL_CONTAINER_PORT=8474
29
+ export MANDATEOPS_GATE_INGRESS_CONTAINER_PORT=9000
30
+
31
+ # Tells the person waiting which preparation phase $1 has begun and how long
32
+ # the script has run, on stderr, so a slow build or health wait is visible
33
+ # before the run records its first step.
34
+ lab_phase() {
35
+ echo "mandateops: preparing: $1 (${SECONDS}s elapsed)" >&2
36
+ }
37
+
38
+ # Splits `name=value` at its first `=` into the caller's name and value. The
39
+ # value stays exact: `read` with IFS='=' drops a trailing one.
40
+ lab_split_line() {
41
+ name="${1%%=*}"
42
+ value=""
43
+ [[ "$1" != *=* ]] || value="${1#*=}"
44
+ }
45
+
46
+ # Exports each test default from config/lab.env that the environment
47
+ # does not already set. The file is the one source of these defaults for the
48
+ # containers (through Compose interpolation) and for the runner.
49
+ lab_env_defaults() {
50
+ local line name value
51
+ # An installed lab has no shared defaults: its host generates every token.
52
+ [[ -f "$lab_root/config/lab.env" ]] || return 0
53
+ while IFS= read -r line; do
54
+ lab_split_line "$line"
55
+ [[ -z "$name" || "$name" == \#* ]] && continue
56
+ if [[ ! "$name" =~ ^[A-Z][A-Z0-9_]*$ ]]; then
57
+ echo "config/lab.env: invalid name '$name'" >&2
58
+ return 1
59
+ fi
60
+ [[ -n "${!name:-}" ]] || export "$name=$value"
61
+ done < "$lab_root/config/lab.env"
62
+ }
63
+
64
+ # Succeeds if file $1 parses as JSON, with python3 or else jq; fails closed
65
+ # when neither is installed.
66
+ lab_json_valid() {
67
+ if command -v python3 >/dev/null 2>&1; then
68
+ python3 -I -c 'import json, sys; json.load(open(sys.argv[1]))' "$1" >/dev/null 2>&1
69
+ elif command -v jq >/dev/null 2>&1; then
70
+ jq -e . "$1" >/dev/null 2>&1
71
+ else
72
+ echo "neither python3 nor jq is available to validate $1" >&2
73
+ return 1
74
+ fi
75
+ }
76
+
77
+ # Lab settings a deployment file may set; nothing else is exported, so no
78
+ # declared value can reach this shell's own environment.
79
+ readonly lab_settings=" MANDATEOPS_BACKEND_ADAPTER MANDATEOPS_PROFILE MANDATEOPS_SUT_COMPOSE MANDATEOPS_PROJECT_DIRECTORY MANDATEOPS_SUT_SERVICE MANDATEOPS_SUT_PORT MANDATEOPS_SUT_PROVIDER_URL_ENV MANDATEOPS_SUT_PROVIDER_CLIENTS MANDATEOPS_SUT_CLIENT_ENV MANDATEOPS_SUT_OPERATOR_ENV MANDATEOPS_SUT_VIEWER_ENV MANDATEOPS_SUT_PROVIDER_ENV MANDATEOPS_SUT_NOTIFY_ENV MANDATEOPS_SUT_SANDBOX_ENV MANDATEOPS_PROCESS_SERVICE MANDATEOPS_INPUT MANDATEOPS_CONFORMANCE_INPUT MANDATEOPS_SANDBOX_SETTINGS MANDATEOPS_SANDBOX_READER_SETTINGS MANDATEOPS_SANDBOX_REQUIRES MANDATEOPS_SANDBOX_PASS MANDATEOPS_SANDBOX_UNMET MANDATEOPS_SANDBOX_POOL MANDATEOPS_SANDBOX_FUNDED_INPUTS MANDATEOPS_REPLY_HOLD_LIMIT MANDATEOPS_REPLY_HOLD_MS MANDATEOPS_QUIESCENCE_MS MANDATEOPS_RETRY_DELAY_MS MANDATEOPS_WORKER_LEASE_MS MANDATEOPS_WORKER_DEADLINE_MS MANDATEOPS_DEFECT_STIMULUS MANDATEOPS_PROVIDER_MAPPING MANDATEOPS_PROVIDER_NAMES MANDATEOPS_PROVIDER_COMMIT_DELAYS MANDATEOPS_PROVIDER_WIRE MANDATEOPS_SEEDED_DEFECT MANDATEOPS_SKIP_CONFORMANCE MANDATEOPS_SKIP_CONFORMANCE_REASON MANDATEOPS_BACKEND_RATE_LIMIT MANDATEOPS_PROVIDER_NOTIFY_PATH MANDATEOPS_INGRESS_FORWARD_PATH MANDATEOPS_PROVIDER_ACCOUNT MANDATEOPS_PROVIDER_OPERATION MANDATEOPS_PROVIDER_SCOPE MANDATEOPS_PROVIDER_RATE_LIMIT MANDATEOPS_PROVIDER_RATE_WINDOW_MS MANDATEOPS_PROVIDER_BODY_LIMIT_BYTES MANDATEOPS_PROVIDER_MAX_CONNECTIONS MANDATEOPS_PROVIDER_REQUEST_TIMEOUT_MS MANDATEOPS_PROVIDER_POOL_CONNECTIONS MANDATEOPS_PROVIDER_POOL_CONNECTION_TIMEOUT_MS MANDATEOPS_PROVIDER_STATEMENT_TIMEOUT_MS MANDATEOPS_PROVIDER_IDLE_TRANSACTION_TIMEOUT_MS MANDATEOPS_STARTUP_TIMEOUT_S MANDATEOPS_STOP_TIMEOUT_S MANDATEOPS_PORT_READS MANDATEOPS_CONTROL_TIMEOUT_MS MANDATEOPS_PROCESS_TIMEOUT_MS MANDATEOPS_LOG_LIMIT_BYTES MANDATEOPS_KEEP_IMAGES MANDATEOPS_REQUIRE_PREBUILT_IMAGES MANDATEOPS_TELEMETRY "
80
+
81
+ # All a customer's Compose file can interpolate besides its own env file: what
82
+ # Compose needs to reach the engine, and one setting that is no secret. No
83
+ # token is here: Compose interpolates every file of a project from one
84
+ # environment, so a token named here could be written into any service.
85
+ readonly customer_environment=" PATH HOME TMPDIR DOCKER_HOST DOCKER_CONTEXT DOCKER_CONFIG DOCKER_CERT_PATH DOCKER_TLS_VERIFY MANDATEOPS_BACKEND_RATE_LIMIT "
86
+
87
+ # Prints a short digest of the physical runs directory.
88
+ lab_runs_digest() {
89
+ local runs="${MANDATEOPS_RUNS_DIR:-$lab_root/runs}"
90
+ mkdir -p "$runs"
91
+ (cd -P "$runs" && pwd) | cksum | cut -d' ' -f1
92
+ }
93
+
94
+ # Clears every lab setting from the environment, so that only deployment file
95
+ # values reach a deployment.
96
+ lab_settings_clear() {
97
+ local name
98
+ for name in $lab_settings; do unset "$name"; done
99
+ }
100
+
101
+ # Exports the lab settings of deployment file $1: the file defines the deployment. Every other KEY=VALUE belongs to the
102
+ # customer's Compose file and goes to its env file, which only that Compose
103
+ # invocation reads. Validation of declared systems belongs to the mandateops CLI.
104
+ lab_deployment_load() {
105
+ local file="$1" line name value
106
+ customer_env="$run_dir/customer.env"
107
+ mkdir -p "$run_dir"
108
+ : > "$customer_env"
109
+ chmod 600 "$customer_env"
110
+ while IFS= read -r line; do
111
+ lab_split_line "$line"
112
+ [[ -z "$name" || "$name" == \#* ]] && continue
113
+ if [[ "$lab_settings" == *" $name "* ]]; then
114
+ export "$name=$value"
115
+ elif [[ "$name" == AUDIT_* && "$lab_settings" == *" MANDATEOPS_${name#AUDIT_} "* ]]; then
116
+ echo "$file: $name is renamed MANDATEOPS_${name#AUDIT_}; rename it" >&2
117
+ return 1
118
+ elif [[ "$name" == AUDIT_* || "$name" == MANDATEOPS_* ]]; then
119
+ echo "$file: unknown lab setting '$name'" >&2
120
+ return 1
121
+ else
122
+ printf "%s='%s'\n" "$name" "$value" >> "$customer_env"
123
+ fi
124
+ done < "$file"
125
+ if [[ -z "${MANDATEOPS_SUT_SERVICE:-}" || -z "${MANDATEOPS_SUT_PORT:-}" ]]; then
126
+ echo "$file: needs MANDATEOPS_SUT_SERVICE and MANDATEOPS_SUT_PORT" >&2
127
+ return 1
128
+ fi
129
+ }
130
+
131
+ # Locates the customer's project, for startup only: teardown must work with
132
+ # the project gone. The directory is resolved, so every path Compose derives
133
+ # from it is its own resolution unless a link lies below it, which isolate
134
+ # refuses. A relative Compose path resolves from it, as Compose does.
135
+ lab_customer_project() {
136
+ MANDATEOPS_PROJECT_DIRECTORY="$(cd "${MANDATEOPS_PROJECT_DIRECTORY:-$lab_root}" && pwd -P)"
137
+ export MANDATEOPS_PROJECT_DIRECTORY
138
+ sut_compose="$MANDATEOPS_PROJECT_DIRECTORY/${MANDATEOPS_SUT_COMPOSE:-}"
139
+ [[ "${MANDATEOPS_SUT_COMPOSE:-}" == /* ]] && sut_compose="$MANDATEOPS_SUT_COMPOSE"
140
+ if [[ ! -f "$sut_compose" ]]; then
141
+ echo "no customer Compose file at $sut_compose" >&2
142
+ return 1
143
+ fi
144
+ }
145
+
146
+ # Prints a tag naming the test provider's build inputs, so labs of two
147
+ # versions on one host never share an image while runs of one version do.
148
+ lab_provider_tag() {
149
+ local sum
150
+ sum="$(cd "$lab_root/examples/test-provider" &&
151
+ find src Dockerfile package.json bun.lock -type f | LC_ALL=C sort | xargs cat | cksum)" || return 1
152
+ echo "lab-${sum%% *}"
153
+ }
154
+
155
+ # Prebuilt images. An operator may set MANDATEOPS_LAB_IMAGE_SOURCE to a local
156
+ # repository prefix that scripts/lab-images.sh filled from this checkout (CI
157
+ # builds once and loads the result into every shard). It is the operator's setting
158
+ # alone: a deployment file cannot name it, and it suits only the
159
+ # repository's own cases, since customer builds are then never run. Each image is then
160
+ # tagged under the run's own name instead of built, so runs stay as isolated
161
+ # as when they build, and teardown removes only that run tag. Unset,
162
+ # everything is built. Succeeds when a source is set; exits on a malformed one.
163
+ lab_image_source() {
164
+ [[ -n "${MANDATEOPS_LAB_IMAGE_SOURCE:-}" ]] || return 1
165
+ if [[ ! "$MANDATEOPS_LAB_IMAGE_SOURCE" =~ ^[a-z0-9][a-z0-9._-]{0,63}(/[a-z0-9][a-z0-9._-]{0,63}){0,3}$ ]]; then
166
+ echo "MANDATEOPS_LAB_IMAGE_SOURCE must be a lowercase repository prefix" >&2
167
+ exit 2
168
+ fi
169
+ }
170
+
171
+ # Prints the build context of the lab's own image mandateops/$1.
172
+ lab_image_context() {
173
+ case "$1" in
174
+ test-provider) echo "$lab_root/examples/test-provider" ;;
175
+ agents) echo "$lab_root/examples/agents" ;;
176
+ provider-store | fault-control | telemetry | capture) echo "$lab_root/images/$1" ;;
177
+ *)
178
+ echo "no lab image named $1" >&2
179
+ return 1
180
+ ;;
181
+ esac
182
+ }
183
+
184
+ # Prints the prebuilt reference for $1: a lab image's name, or
185
+ # sut-<deployment>-<service> for a customer service.
186
+ lab_prebuilt_ref() {
187
+ echo "$MANDATEOPS_LAB_IMAGE_SOURCE/$1:latest"
188
+ }
189
+
190
+ # Makes the lab image $1 available under tag $2: from the prebuilt source when
191
+ # one is set, otherwise built from its context.
192
+ lab_image_provide() {
193
+ if lab_image_source; then
194
+ docker tag "$(lab_prebuilt_ref "$1")" "$2"
195
+ else
196
+ docker build --quiet --tag "$2" "$(lab_image_context "$1")" > /dev/null
197
+ fi
198
+ }
199
+
200
+ # Provides, for each name after the Compose file $1, the MandateOps image
201
+ # images/<name> makes from its pinned upstream digest, under the run's own
202
+ # tag that file names for it. A name the file does not use is skipped.
203
+ lab_build_named_images() {
204
+ local file="$1" name tag
205
+ shift
206
+ for name in "$@"; do
207
+ tag="$(grep -o "mandateops/$name:[0-9A-Za-z._-]*" "$file" | head -n 1)" || true
208
+ [[ -z "$tag" ]] || lab_image_provide "$name" "$tag" || return 1
209
+ done
210
+ }
211
+
212
+ # Prints "<service> <image>" for each service the Compose command "$@" builds:
213
+ # its image: or, without one, Compose's default <project>-<service>.
214
+ compose_built_images() {
215
+ local resolved
216
+ resolved="$("$@" config --format json)" || return 1
217
+ if command -v python3 > /dev/null 2>&1; then
218
+ python3 -I -c 'import json, sys
219
+ project = json.load(sys.stdin)
220
+ for name, service in sorted(project["services"].items()):
221
+ if "build" in service:
222
+ print(name, service.get("image") or project["name"] + "-" + name)' <<< "$resolved"
223
+ elif command -v jq > /dev/null 2>&1; then
224
+ jq -r '.name as $p | .services | to_entries | sort_by(.key)[] | select(.value.build)
225
+ | "\(.key) \(.value.image // ($p + "-" + .key))"' <<< "$resolved"
226
+ else
227
+ echo "neither python3 nor jq is available to read the Compose project" >&2
228
+ return 1
229
+ fi
230
+ }
231
+
232
+ # Tags the prebuilt images under the names this run's projects start: every
233
+ # image the lab's file names, and every customer service that builds, which
234
+ # lab-images.sh built for this deployment; a missing one fails the start.
235
+ lab_tag_prebuilt() {
236
+ local image name service built
237
+ for image in $(lab_compose config --images | LC_ALL=C sort -u); do
238
+ [[ "$image" == mandateops/* ]] || continue
239
+ name="${image#mandateops/}"
240
+ docker tag "$(lab_prebuilt_ref "${name%%:*}")" "$image" || return 1
241
+ done
242
+ built="$(compose_built_images sut_compose_run)" || return 1
243
+ : > "$prebuilt_tags_file"
244
+ while read -r service image; do
245
+ [[ -n "$service" ]] || continue
246
+ # Recorded first, so teardown removes exactly this tag even if tagging fails.
247
+ printf '%s\n' "$image" >> "$prebuilt_tags_file"
248
+ docker tag "$(lab_prebuilt_ref "sut-$deployment-$service")" "$image" || return 1
249
+ done <<< "$built"
250
+ }
251
+
252
+ # Validates a run id and deployment name, sets run_id, deployment, project
253
+ # and run_dir, and exports the lab defaults and the deployment's values that
254
+ # Compose interpolates. The project prefix keeps every lab deployment apart
255
+ # from MandateOps' own Compose projects.
256
+ lab_run() {
257
+ if [[ $# -ne 2 || ! "$1" =~ ^[a-z0-9][a-z0-9-]{0,39}$ || ! "$2" =~ ^[a-z0-9][a-z0-9-]{0,39}$ ]]; then
258
+ echo "usage: $(basename "$0") <run id> <deployment>: each 1-40 of [a-z0-9-], starting alphanumeric" >&2
259
+ exit 2
260
+ fi
261
+ run_id="$1"
262
+ deployment="$2"
263
+ # The runs directory is part of the name, so two hosts with one run id never
264
+ # share a project: each directory's atomic run claim decides who owns it.
265
+ project="mandateops-lab-$run_id-$(lab_runs_digest)"
266
+ # shellcheck disable=SC2034 # read by the scripts that source this file
267
+ run_dir="${MANDATEOPS_RUNS_DIR:-$lab_root/runs}/$run_id"
268
+ if [[ -f "$run_dir/project" ]]; then
269
+ project="$(< "$run_dir/project")"
270
+ if [[ ! "$project" =~ ^(mandateops|audit)-lab-[a-z0-9-]+$ ]]; then
271
+ echo "$run_dir/project does not name a lab project" >&2
272
+ exit 2
273
+ fi
274
+ fi
275
+ # The customer's services run in their own project beside the lab's. A run
276
+ # started before that project was named "-app" recorded no
277
+ # customer-project file, so its leftovers are still found under "-sut".
278
+ sut_project="$project-app"
279
+ if [[ -f "$run_dir/project" && ! -f "$run_dir/customer-project" ]]; then
280
+ sut_project="$project-sut"
281
+ fi
282
+ export MANDATEOPS_CUSTOMER_NETWORK="${project}_customer"
283
+ override_file="$run_dir/compose.override.yml"
284
+ credentials_file="$run_dir/compose.credentials.yml"
285
+ export MANDATEOPS_LAB_ROOT="$lab_root"
286
+ MANDATEOPS_PROVIDER_IMAGE_TAG="$(lab_provider_tag)" || exit 2
287
+ export MANDATEOPS_PROVIDER_IMAGE_TAG
288
+ # Every image the lab builds is tagged for this project alone, so parallel
289
+ # runs on one engine never move or remove each other's images.
290
+ export MANDATEOPS_LAB_IMAGE_RUN="$project"
291
+ # Telemetry is the operator's choice as well as the deployment's: either
292
+ # may turn it off, and neither can turn the other's "off" back on.
293
+ local operator_telemetry="${MANDATEOPS_TELEMETRY:-on}"
294
+ lab_settings_clear
295
+ lab_env_defaults
296
+ local file="${MANDATEOPS_SETTINGS_FILE:-$lab_root/config/deployments/$deployment.env}"
297
+ if [[ ! -f "$file" ]]; then
298
+ echo "no deployment file $file" >&2
299
+ exit 2
300
+ fi
301
+ lab_deployment_load "$file" || exit 2
302
+ lab_telemetry_setting "$operator_telemetry" "${MANDATEOPS_TELEMETRY:-on}" || exit 2
303
+ lab_limits_valid || exit 2
304
+ limits_file="$run_dir/compose.limits.yml"
305
+ # The gate's egress alias, for a deployment against a real sandbox.
306
+ egress_file="$run_dir/compose.egress.yml"
307
+ # The customer image tags lab_tag_prebuilt gave this run, one per line.
308
+ prebuilt_tags_file="$run_dir/prebuilt-tags"
309
+ }
310
+
311
+ # The operator's per-container limits, MANDATEOPS_RUN_CPUS (such as 1.5) and
312
+ # MANDATEOPS_RUN_MEMORY (such as 512m), for a host that runs other people's
313
+ # tests. They are not lab settings: a deployment file cannot set or loosen
314
+ # them. Unset, containers run without limits, as on a developer's machine.
315
+ lab_limits_valid() {
316
+ if [[ -n "${MANDATEOPS_RUN_CPUS:-}" && ! "$MANDATEOPS_RUN_CPUS" =~ ^[0-9]{1,3}(\.[0-9]{1,2})?$ ]]; then
317
+ echo "MANDATEOPS_RUN_CPUS must be a number of CPUs such as 1.5" >&2
318
+ return 1
319
+ fi
320
+ if [[ -n "${MANDATEOPS_RUN_MEMORY:-}" && ! "$MANDATEOPS_RUN_MEMORY" =~ ^[1-9][0-9]{0,5}[mg]$ ]]; then
321
+ echo "MANDATEOPS_RUN_MEMORY must be a size such as 512m or 2g" >&2
322
+ return 1
323
+ fi
324
+ }
325
+
326
+ # Writes the Compose file $1 limiting every service the project command "$2"
327
+ # lists, and prints the --file arguments that add it; prints nothing when no
328
+ # limit is set. Swap is held to the memory limit, so memory is the bound.
329
+ lab_write_limits() {
330
+ local file="$1" services service
331
+ rm -f "$file"
332
+ [[ -n "${MANDATEOPS_RUN_CPUS:-}${MANDATEOPS_RUN_MEMORY:-}" ]] || return 0
333
+ services="$("$2" config --services)" || return 1
334
+ {
335
+ echo "services:"
336
+ for service in $services; do
337
+ echo " $service:"
338
+ [[ -z "${MANDATEOPS_RUN_CPUS:-}" ]] || echo " cpus: $MANDATEOPS_RUN_CPUS"
339
+ [[ -z "${MANDATEOPS_RUN_MEMORY:-}" ]] ||
340
+ printf ' mem_limit: %s\n memswap_limit: %s\n' "$MANDATEOPS_RUN_MEMORY" "$MANDATEOPS_RUN_MEMORY"
341
+ done
342
+ } > "$file.partial" || return 1
343
+ mv "$file.partial" "$file"
344
+ printf '%s\n' --file "$file"
345
+ }
346
+
347
+ # Exports MANDATEOPS_TELEMETRY from the operator's value $1 and the deployment's
348
+ # $2: off when either says off. Any value but on or off is refused, so a typo
349
+ # never leaves telemetry running unnoticed.
350
+ lab_telemetry_setting() {
351
+ local value
352
+ for value in "$1" "$2"; do
353
+ case "$value" in
354
+ on | off) ;;
355
+ *)
356
+ printf 'MANDATEOPS_TELEMETRY must be on or off, not %q\n' "$value" >&2
357
+ return 1
358
+ ;;
359
+ esac
360
+ done
361
+ if [[ "$1" == off || "$2" == off ]]; then
362
+ export MANDATEOPS_TELEMETRY=off
363
+ echo "backend telemetry is off: this run records no backend spans" >&2
364
+ else
365
+ export MANDATEOPS_TELEMETRY=on
366
+ fi
367
+ }
368
+
369
+ # The lab's own project, which no customer file is ever merged into.
370
+ lab_compose() {
371
+ local extra=()
372
+ [[ ! -f "$limits_file" ]] || extra+=(--file "$limits_file")
373
+ [[ ! -f "$egress_file" ]] || extra+=(--file "$egress_file")
374
+ docker compose --project-name "$project" --project-directory "$lab_root" \
375
+ --file "$compose_file" ${extra[@]+"${extra[@]}"} "$@"
376
+ }
377
+
378
+ # For a deployment against a real provider's sandbox (a provider mapping
379
+ # other than the test provider's), exports the gate's egress route and
380
+ # writes the override naming the gate by that host on the customer network.
381
+ # Removes any earlier override first, so a deployment without egress has none.
382
+ lab_egress_setup() {
383
+ local entry host port addresses address pins=""
384
+ rm -f "$egress_file"
385
+ unset MANDATEOPS_GATE_EGRESS MANDATEOPS_EGRESS_PINS
386
+ [[ -n "${MANDATEOPS_PROVIDER_MAPPING:-}" && "$MANDATEOPS_PROVIDER_MAPPING" != test-provider ]] || return 0
387
+ # The CLI reads the allow-list, resolves the host once and refuses any
388
+ # non-public answer; it prints "<host> <port> <address,...>".
389
+ entry="$(lab_plan_egress)" || return 1
390
+ read -r host port addresses <<< "$entry"
391
+ for address in ${addresses//,/ }; do
392
+ pins+=" - \"$host:$address\"\n"
393
+ done
394
+ export MANDATEOPS_GATE_EGRESS="$host=lab:mandateops-relay:$port/$MANDATEOPS_PROVIDER_MAPPING"
395
+ # Recorded in ports.env, so the runner knows the pins a rotation replaces.
396
+ export MANDATEOPS_EGRESS_PINS="$addresses"
397
+ # The relay connects to the pinned addresses alone until the next start;
398
+ # the gate is named by the host on the customer network alone.
399
+ printf 'services:\n mandateops-relay:\n extra_hosts:\n%b gate:\n networks:\n lab:\n aliases: [gate-lab]\n customer:\n aliases: [mandateops-provider, "%s"]\n' \
400
+ "$pins" "$host" > "$egress_file.partial"
401
+ mv "$egress_file.partial" "$egress_file"
402
+ }
403
+
404
+ # The egress line for the deployment's provider mapping, from the CLI.
405
+ lab_plan_egress() {
406
+ "${MANDATEOPS_AUDIT_CLI:?set by the mandateops CLI or scripts/e2e.sh}" egress \
407
+ --config "$lab_root/config/fault-control.json" --proxy "$MANDATEOPS_PROVIDER_MAPPING"
408
+ }
409
+
410
+ # Runs a command with only the customer's environment, so neither a lab
411
+ # secret nor anything else on the host can be named by a customer file. Values
412
+ # leave through unset, never a command line.
413
+ customer_shell() {
414
+ (
415
+ local name
416
+ for name in $(compgen -e); do
417
+ [[ "$customer_environment" == *" $name "* ]] || unset "$name"
418
+ done
419
+ exec "$@"
420
+ )
421
+ }
422
+
423
+ # The customer's project as its file describes it, used only until containers
424
+ # exist: a running container may be able to write that file.
425
+ sut_compose_raw() {
426
+ customer_shell docker compose --project-name "$sut_project" \
427
+ --project-directory "$MANDATEOPS_PROJECT_DIRECTORY" --env-file "$customer_env" \
428
+ --file "$sut_compose" "$@"
429
+ }
430
+
431
+ sut_compose_run() {
432
+ sut_compose_raw --file "$override_file" "$@"
433
+ }
434
+
435
+ # The customer's project by name alone, from what the engine knows. No file is
436
+ # read: Compose searches upward from its directory, so this runs from the root
437
+ # where nothing a customer wrote can be found.
438
+ sut_compose_named() {
439
+ (cd / && customer_shell docker compose --project-name "$sut_project" "$@")
440
+ }
441
+
442
+ # Runs the mandateops CLI's command $1 on this run's isolation plan, with any
443
+ # further arguments.
444
+ lab_plan_command() {
445
+ local command="$1"
446
+ shift
447
+ "${MANDATEOPS_AUDIT_CLI:?set by the mandateops CLI or scripts/e2e.sh}" "$command" \
448
+ --network "$MANDATEOPS_CUSTOMER_NETWORK" --project "$sut_project" \
449
+ --project-directory "$MANDATEOPS_PROJECT_DIRECTORY" --run-dir "$run_dir" "$@"
450
+ }
451
+
452
+ # Refuses a customer file that could escape the lab and writes the override
453
+ # joining every service to the internal network: the mandateops CLI owns both. Resolved
454
+ # values may be secrets, so they pass through a pipe and reach no file.
455
+ # Environment and label files are judged as paths in the uninterpolated
456
+ # project, which holds only what the customer's file says: some Compose
457
+ # releases replace them with their contents while resolving.
458
+ lab_write_override() {
459
+ local uninterpolated="$run_dir/project.uninterpolated.json"
460
+ if ! (umask 077 && sut_compose_raw config --no-interpolate --format json > "$uninterpolated") ||
461
+ ! sut_compose_raw config --no-env-resolution --format json |
462
+ lab_plan_command isolate --uninterpolated "$uninterpolated" > "$override_file.partial"; then
463
+ rm -f "$override_file.partial" "$uninterpolated"
464
+ return 1
465
+ fi
466
+ rm -f "$uninterpolated"
467
+ mv "$override_file.partial" "$override_file"
468
+ }
469
+
470
+ # The customer's project with the files it starts with, for listing its
471
+ # services; reads lab_start_customer's telemetry files.
472
+ sut_project_files() {
473
+ sut_compose_raw --file "$override_file" ${telemetry[@]+"${telemetry[@]}"} "$@"
474
+ }
475
+
476
+ # Starts the customer's project, built already, within $1 seconds. Its services
477
+ # take their tokens from a file only the owner can read, written after the
478
+ # build, whose context may hold the run directory, and removed once Compose has
479
+ # read it. Teardown removes one a killed script left.
480
+ lab_start_customer() {
481
+ local status=0
482
+ local telemetry=()
483
+ if [[ "${MANDATEOPS_TELEMETRY:-on}" != "off" ]]; then
484
+ telemetry=(--file "$run_dir/compose.telemetry.yml")
485
+ # The collector sidecar writes the backend's spans here, and only here.
486
+ mkdir -p "$run_dir/telemetry"
487
+ # The agents image, under the content tag the telemetry file names.
488
+ local agents
489
+ agents="$(grep -o 'mandateops/agents:[0-9A-Za-z._-]*' "$run_dir/compose.telemetry.yml" | head -n 1)"
490
+ if [[ ! -f "$lab_root/examples/agents/Dockerfile" ]]; then
491
+ echo "backend telemetry: $lab_root/examples/agents is missing from this install;" \
492
+ "reinstall mandateops, or set MANDATEOPS_TELEMETRY=off to run without backend spans" >&2
493
+ return 1
494
+ fi
495
+ lab_phase "building the capture sidecars"
496
+ lab_image_provide agents "$agents" || return 1
497
+ lab_build_named_images "$run_dir/compose.telemetry.yml" telemetry capture || return 1
498
+ fi
499
+ if ! (umask 077 && lab_plan_command credentials > "$credentials_file"); then
500
+ rm -f "$credentials_file"
501
+ return 1
502
+ fi
503
+ local limits=()
504
+ # shellcheck disable=SC2207 # two words: --file and a run-directory path
505
+ limits=($(lab_write_limits "$run_dir/compose.sut-limits.yml" sut_project_files)) || {
506
+ rm -f "$credentials_file"
507
+ return 1
508
+ }
509
+ lab_phase "starting your services and waiting for their health checks"
510
+ sut_compose_raw --file "$override_file" ${telemetry[@]+"${telemetry[@]}"} --file "$credentials_file" \
511
+ ${limits[@]+"${limits[@]}"} up --detach --no-build --wait --wait-timeout "$1" || status=$?
512
+ rm -f "$credentials_file"
513
+ if [[ "$status" -eq 0 && ${#telemetry[@]} -gt 0 ]]; then
514
+ lab_phase "attaching capture to your services"
515
+ lab_wait_telemetry
516
+ fi
517
+ return "$status"
518
+ }
519
+
520
+ # Waits up to 30 seconds until every eBPF sidecar has attached to its
521
+ # service, so a payout the run submits next is traced from its first call.
522
+ # A sidecar names the process it instruments in its log once attached. Spans
523
+ # never decide a verdict, so a sidecar that does not attach is a warning.
524
+ lab_wait_telemetry() {
525
+ local deadline=$((SECONDS + 30)) sidecar
526
+ local sidecars
527
+ sidecars="$(sut_compose_named ps --services | grep '^mandateops-observer-' || true)"
528
+ for sidecar in $sidecars; do
529
+ until sut_compose_named logs --no-log-prefix "$sidecar" 2>/dev/null |
530
+ grep -q 'msg="instrumenting process"'; do
531
+ if (( SECONDS >= deadline )); then
532
+ echo "warning: MandateOps capture for ${sidecar#mandateops-observer-} did not attach; its spans may be missing" >&2
533
+ return 0
534
+ fi
535
+ sleep 1
536
+ done
537
+ done
538
+ }
539
+
540
+ # Prints, longest first and one per line, every value the lab put into a
541
+ # container's environment, as the engine reports it, and every value of the
542
+ # deployment file's customer part: the exact set the kept log is redacted
543
+ # of. A value under 8 characters is left out, so ordinary words survive,
544
+ # unless its variable is a declared credential. Fails, printing nothing
545
+ # usable, when the engine cannot say or the set is larger than 4096 values,
546
+ # which would make redaction too slow to bound; the log is then not marked
547
+ # exact and no line of it is quoted.
548
+ lab_secret_values() {
549
+ local ids credentials=" " name values
550
+ for name in MANDATEOPS_SUT_CLIENT_ENV MANDATEOPS_SUT_OPERATOR_ENV MANDATEOPS_SUT_VIEWER_ENV MANDATEOPS_SUT_PROVIDER_ENV MANDATEOPS_SUT_NOTIFY_ENV MANDATEOPS_SUT_SANDBOX_ENV; do
551
+ # A list names one variable per provider, separated by commas.
552
+ [[ -z "${!name:-}" ]] || credentials+="${!name//,/ } "
553
+ done
554
+ ids="$(docker ps --all --quiet --no-trunc --filter "label=com.docker.compose.project=$sut_project" &&
555
+ docker ps --all --quiet --no-trunc --filter "label=com.docker.compose.project=$project")" || return 1
556
+ values="$({
557
+ # shellcheck disable=SC2086 # one container id per word
558
+ [[ -z "$ids" ]] || docker container inspect --format '{{range .Config.Env}}{{println .}}{{end}}' $ids
559
+ } | LC_ALL=C awk -v credentials="$credentials" '
560
+ {
561
+ split_at = index($0, "=")
562
+ if (split_at == 0) next
563
+ name = substr($0, 1, split_at - 1)
564
+ value = substr($0, split_at + 1)
565
+ # The deployment file part is written as NAME='"'"'value'"'"'.
566
+ if (FILENAME != "-" && value ~ /^'"'"'.*'"'"'$/) value = substr(value, 2, length(value) - 2)
567
+ if (value == "") next
568
+ if (length(value) >= 8 || index(credentials, " " name " ") > 0) print length(value) "\t" value
569
+ }' - "$customer_env" | LC_ALL=C sort -t $'\t' -k1,1nr -k2 -u | cut -f2-)" || return 1
570
+ (($(printf '%s\n' "$values" | wc -l) <= 4096)) || return 1
571
+ printf '%s\n' "$values"
572
+ }
573
+
574
+ # Copies stdin to stdout with every lab secret's value replaced, then each
575
+ # line of $1, when given, which lab_secret_values printed. The values reach
576
+ # awk through a pipe on descriptor 3, never a file, a command line or an
577
+ # environment a large set could overflow. Values are compared as literal
578
+ # text, whatever characters they hold.
579
+ lab_redact() {
580
+ LC_ALL=C awk -v names="MANDATEOPS_AGENT_TOKEN MANDATEOPS_CONTROL_TOKEN MANDATEOPS_VIEWER_TOKEN MANDATEOPS_PROVIDER_TOKEN MANDATEOPS_PROVIDER_TOKEN_2 MANDATEOPS_PROVIDER_TOKEN_3 MANDATEOPS_PROVIDER_TOKEN_4 MANDATEOPS_PROVIDER_TOKEN_5 MANDATEOPS_PROVIDER_TOKEN_6 MANDATEOPS_PROVIDER_TOKEN_7 MANDATEOPS_PROVIDER_TOKEN_8 MANDATEOPS_PROVIDER_CONTROL_TOKEN MANDATEOPS_PROVIDER_DB_PASSWORD MANDATEOPS_NOTIFY_SECRET MANDATEOPS_SANDBOX_TOKEN MANDATEOPS_SANDBOX_READER_TOKEN" '
581
+ BEGIN {
582
+ count = 0
583
+ split(names, secret_names, " ")
584
+ for (entry in secret_names) {
585
+ if (ENVIRON[secret_names[entry]] != "") secrets[++count] = ENVIRON[secret_names[entry]]
586
+ }
587
+ while ((getline value < "/dev/fd/3") > 0) if (value != "") secrets[++count] = value
588
+ }
589
+ {
590
+ # Compose writes "service-N | " before each line. The service name is
591
+ # the deployment'"'"'s own, never container output, and attributes the
592
+ # line to its process, so it is kept even where it equals a value.
593
+ prefix = ""
594
+ line = $0
595
+ if (match(line, /^[a-z0-9][a-z0-9_.-]*-[0-9]+ *\| /)) {
596
+ prefix = substr(line, 1, RLENGTH)
597
+ line = substr(line, RLENGTH + 1)
598
+ }
599
+ for (entry = 1; entry <= count; entry++) {
600
+ secret = secrets[entry]
601
+ kept = ""
602
+ while ((found = index(line, secret)) > 0) {
603
+ kept = kept substr(line, 1, found - 1) "[redacted]"
604
+ line = substr(line, found + length(secret))
605
+ }
606
+ line = kept line
607
+ }
608
+ print prefix line
609
+ }' 3< <(printf '%s\n' "${1:-}")
610
+ }
611
+
612
+ # Appends the logs of the project command "$2..." to compose.log, redacted
613
+ # of the lab's secrets and of each line of $1 before they reach the
614
+ # file, and cut at the deployment's limit, so a noisy container decides
615
+ # neither what the host stores nor whether the lab's own logs fit.
616
+ lab_capture_logs() {
617
+ local values="$1"
618
+ shift
619
+ local log="$run_dir/compose.log" limit="${MANDATEOPS_LOG_LIMIT_BYTES:-$default_log_limit_bytes}"
620
+ local before captured status=0
621
+ before="$(wc -c < "$log")"
622
+ (
623
+ "$@" logs --no-color --timestamps | lab_redact "$values" | head -c "$limit" >> "$log"
624
+ exit "${PIPESTATUS[0]}"
625
+ ) || status=$?
626
+ captured="$(($(wc -c < "$log") - before))"
627
+ if ((captured >= limit)); then
628
+ printf '\n[logs cut at %s bytes]\n' "$limit" >> "$log"
629
+ return 0
630
+ fi
631
+ return "$status"
632
+ }