File size: 12,375 Bytes
085f660
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
"""Run the synthetic brokered-operator example without installed dependencies."""

from __future__ import annotations

import argparse
import json
import os
import sys
import tempfile
from pathlib import Path
from typing import Any

sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))

from lumi_eggcracker.brokered import BrokeredOperator

_RUN_FAILURE_STAGES = frozenset(
    {
        "client_setup",
        "submit",
        "admission_validation",
        "dispatch",
        "dispatch_validation",
        "result",
        "result_validation",
        "outcome_serialization",
        "outcome_output",
    }
)
_RUN_VALIDATED_STAGES = frozenset({"admission", "dispatch"})
_MAX_RUN_FAILURE_BYTES = 256


def _show(name: str, value: object) -> None:
    if hasattr(value, "as_dict"):
        value = value.as_dict()
    print(json.dumps({name: value}, sort_keys=True, separators=(",", ":")))


def _valid_queue_id(value: object) -> bool:
    return (
        isinstance(value, str)
        and len(value) == 32
        and all(character in "0123456789abcdef" for character in value)
    )


def _report_run_failure(
    failed_stage: str,
    queue_id: object = None,
    last_validated_stage: str | None = None,
    effect_status: str = "UNKNOWN",
) -> None:
    if failed_stage not in _RUN_FAILURE_STAGES:
        failed_stage = "outcome_output"
    validated_queue_id = queue_id if _valid_queue_id(queue_id) else None
    if validated_queue_id is None:
        last_validated_stage = None
        effect_status = "UNKNOWN"
    else:
        if last_validated_stage not in _RUN_VALIDATED_STAGES:
            last_validated_stage = "admission"
        if effect_status != "CONFIRMED_APPLIED" or last_validated_stage != "dispatch":
            effect_status = "UNKNOWN"

    details: dict[str, str] = {
        "effect_status": effect_status,
        "error": "failed closed",
        "failed_stage": failed_stage,
    }
    if validated_queue_id is not None:
        details["last_validated_stage"] = last_validated_stage
        details["queue_id"] = validated_queue_id
    encoded = json.dumps(
        {"run_failure": details},
        sort_keys=True,
        separators=(",", ":"),
    ).encode("utf-8")
    if len(encoded) > _MAX_RUN_FAILURE_BYTES:
        raise RuntimeError("bounded run diagnostic exceeded its fixed limit")
    print(encoded.decode("utf-8"), file=sys.stderr)


def _run_one_shot(client: Any, operation_id: str, ipc: Any) -> int:
    failed_stage = "submit"
    queue_id: str | None = None
    last_validated_stage: str | None = None
    effect_status = "UNKNOWN"
    try:
        admission = client.submit(operation_id)
        failed_stage = "admission_validation"
        admission_receipt = _validated_receipt(
            admission,
            ipc,
            phase="admission",
            outcome="QUEUED",
            code="ADMITTED",
            effect_applied=False,
        )
        queue_id = admission_receipt["queue_id"]
        last_validated_stage = "admission"

        failed_stage = "dispatch"
        dispatch = client.dispatch(queue_id)
        failed_stage = "dispatch_validation"
        dispatch_receipt = _validated_receipt(
            dispatch,
            ipc,
            phase="dispatch",
            outcome="APPLIED",
            code="EFFECT_APPLIED",
            effect_applied=True,
            queue_id=queue_id,
        )
        if not _valid_queue_id(dispatch_receipt.get("queue_id")):
            raise ValueError("dispatch receipt queue identifier is invalid")
        last_validated_stage = "dispatch"
        effect_status = "CONFIRMED_APPLIED"

        failed_stage = "result"
        result = client.get_result(queue_id)
        failed_stage = "result_validation"
        _validated_result(result, queue_id, ipc)
        outcome = {"run": {"admission": admission, "dispatch": dispatch, "result": result}}
        failed_stage = "outcome_serialization"
        encoded = json.dumps(
            outcome,
            sort_keys=True,
            separators=(",", ":"),
            allow_nan=False,
        ).encode("utf-8")
        if len(encoded) > (3 * ipc.MAX_IPC_RESPONSE_BYTES) + 128:
            raise ValueError("combined response exceeds its output bound")
        failed_stage = "outcome_output"
        print(encoded.decode("utf-8"))
        return 0
    except Exception:  # noqa: BLE001 - any client error must fail closed
        _report_run_failure(failed_stage, queue_id, last_validated_stage, effect_status)
        return 1


def _validated_receipt(
    response: object,
    ipc: Any,
    *,
    phase: str,
    outcome: str,
    code: str,
    effect_applied: bool,
    queue_id: str | None = None,
) -> dict[str, Any]:
    if (
        not isinstance(response, dict)
        or set(response) != {"receipt", "schema_version"}
        or type(response["schema_version"]) is not str
        or response["schema_version"] != ipc.IPC_SCHEMA
    ):
        raise ValueError("receipt response is malformed")
    receipt = response["receipt"]
    if (
        not ipc._valid_receipt(receipt)
        or receipt.get("phase") != phase
        or receipt.get("outcome") != outcome
        or receipt.get("code") != code
        or receipt.get("effect_applied") is not effect_applied
        or not _valid_queue_id(receipt.get("queue_id"))
        or (queue_id is not None and receipt.get("queue_id") != queue_id)
    ):
        raise ValueError("receipt did not match the required stage")
    return receipt


def _validated_result(response: object, queue_id: str, ipc: Any) -> None:
    if not isinstance(response, dict):
        raise TypeError("result response is malformed")
    required = {"outcome", "phase", "queue_id", "report", "schema_version"}
    if (
        not required <= set(response)
        or set(response) - required
        or type(response["schema_version"]) is not str
        or response["schema_version"] != ipc.IPC_SCHEMA
        or response["phase"] != "result"
        or response["outcome"] != "AVAILABLE"
        or not _valid_queue_id(response["queue_id"])
        or response["queue_id"] != queue_id
        or not ipc._valid_report(response["report"])
        or len(ipc._canonical_json(response["report"])) > ipc.MAX_RESULT_BYTES
    ):
        raise ValueError("result did not match the required stage")


def portable_demo() -> int:
    with tempfile.TemporaryDirectory(prefix="brokered-operator-demo-") as temporary:
        state_dir = Path(temporary) / "state"
        registrar, operator = BrokeredOperator.bootstrap(state_dir)
        grant = registrar.register_run()
        client = operator.client(grant)

        allowed_queue = client.admit("demo-allowed-1")
        _show("admission", allowed_queue)
        applied = operator.dispatch(allowed_queue.queue_id)
        _show("dispatch", applied)
        _show("replayed_dispatch", operator.dispatch(allowed_queue.queue_id))

        stale_queue = client.admit("demo-stale-1")
        _show("queued_before_stop_request", stale_queue)
        _show("stop_request_revocation", operator.request_stop(grant.run_id))
        _show("stale_queued_dispatch", operator.dispatch(stale_queue.queue_id))
        _show("post_stop_admission", client.admit("demo-post-stop-1"))
        _show("verified_process_stop", operator.verified_process_stop(grant.run_id))

        before_reopen = operator.world_snapshot()
        _show("before_reopen", before_reopen.__dict__)
        _, reopened = BrokeredOperator.open(state_dir)
        _show("after_reopen", reopened.world_snapshot().__dict__)
        _show("reopened_stale_dispatch", reopened.dispatch(stale_queue.queue_id))
        return 0


def linux_ipc_demo(arguments: argparse.Namespace) -> int:
    if sys.platform != "linux" or not hasattr(os, "geteuid"):
        raise SystemExit("--linux-ipc modes require Linux")
    from lumi_eggcracker.brokered import linux_ipc
    from lumi_eggcracker.brokered.linux_ipc import BrokeredLinuxClient, BrokeredLinuxService

    if arguments.linux_ipc == "serve":
        required = (arguments.state_dir, arguments.socket, arguments.workload_uid, arguments.workload_gid)
        if any(value is None for value in required):
            raise SystemExit("serve requires --state-dir, --socket, --workload-uid and --workload-gid")
        service = BrokeredLinuxService.open(
            arguments.state_dir,
            arguments.socket,
            workload_uid=arguments.workload_uid,
            workload_gid=arguments.workload_gid,
        )
        try:
            with service:
                _show(
                    "service",
                    {
                        "ipc": "AF_UNIX/SOCK_SEQPACKET",
                        "ready": True,
                        "process_stop": "UNSUPPORTED",
                    },
                )
                service.serve_forever()
        except KeyboardInterrupt:
            _show("trusted_stop_request", service.request_stop())
        return 0

    if arguments.linux_ipc in {"submit", "dispatch", "result", "run"}:
        if arguments.socket is None or arguments.service_uid is None:
            raise SystemExit(f"{arguments.linux_ipc} requires --socket and --service-uid")
        if arguments.linux_ipc == "run" and arguments.operation_id is None:
            raise SystemExit("run requires --operation-id")
        if arguments.linux_ipc == "run":
            try:
                client = BrokeredLinuxClient(arguments.socket, service_uid=arguments.service_uid)
            except Exception:  # noqa: BLE001 - any client setup error must fail closed
                _report_run_failure("client_setup")
                return 1
            return _run_one_shot(client, arguments.operation_id, linux_ipc)
        client = BrokeredLinuxClient(arguments.socket, service_uid=arguments.service_uid)
        if arguments.linux_ipc == "submit":
            if arguments.operation_id is None:
                raise SystemExit("submit requires --operation-id")
            _show("submission", client.submit(arguments.operation_id))
        else:
            if arguments.queue_id is None:
                raise SystemExit(f"{arguments.linux_ipc} requires --queue-id")
            if arguments.linux_ipc == "dispatch":
                _show("dispatch", client.dispatch(arguments.queue_id))
            else:
                _show("result", client.get_result(arguments.queue_id))
        return 0

    if arguments.linux_ipc in {"stop", "snapshot"}:
        if arguments.state_dir is None:
            raise SystemExit(f"{arguments.linux_ipc} requires --state-dir")
        _, operator = BrokeredOperator.open(arguments.state_dir)
        operator._assert_service_state_owner(os.geteuid())
        if arguments.linux_ipc == "stop":
            grant = operator._existing_service_run_grant()
            _show("authority_fence", operator.request_stop(grant.run_id))
            _show("verified_process_stop", operator.verified_process_stop(grant.run_id))
        else:
            snapshot = operator.world_snapshot()
            _show(
                "synthetic_world",
                {
                    "protected_effects": snapshot.protected_effects,
                    "unrelated_canary_allocation": snapshot.unrelated_canary_allocation,
                },
            )
            grant = operator._existing_service_run_grant()
            _show("verified_process_stop", operator.verified_process_stop(grant.run_id))
        return 0

    raise SystemExit("unsupported Linux IPC demo mode")


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "--linux-ipc",
        choices=("serve", "submit", "dispatch", "result", "run", "stop", "snapshot"),
        help="run one side of the Linux authenticated local-IPC demonstration",
    )
    parser.add_argument("--state-dir")
    parser.add_argument("--socket")
    parser.add_argument("--workload-uid", type=int)
    parser.add_argument("--workload-gid", type=int)
    parser.add_argument("--service-uid", type=int)
    parser.add_argument("--operation-id")
    parser.add_argument("--queue-id")
    arguments = parser.parse_args(argv)
    if arguments.linux_ipc is None:
        return portable_demo()
    return linux_ipc_demo(arguments)


if __name__ == "__main__":
    raise SystemExit(main())