File size: 26,808 Bytes
b2e19c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
#!/usr/bin/env python3
"""Consortium Deployment Bootstrap and Governance Provisioning Tool.

Automates end-to-end bootstrap of the reproducible multi-bank consortium:
1. Health Probes: Asserts readiness of Gateway, Frontend, Backend, Coordinator,
   Postgres, Redis, and all three institutional bank workers.
2. Synthetic Entity Ingestion: Ingests authoritative synthetic entities into
   Bank A, Bank B, and Bank C private storage vaults via authenticated worker APIs.
   - Bank A & Bank B share a matching target entity (authentic MATCH).
   - Bank C contains a distinct benign entity (authentic NO_MATCH).
3. User & Consortium Governance Provisioning: Registers sovereign institutional
   analysts and creates governed consortium and bilateral operational grants.
4. Worker Grant Authorization: Enrolls approved bilateral operational grants
   onto the worker daemons with immutable scope digests.
"""

from __future__ import annotations

import argparse
import json
import logging
import os
import subprocess
import sys
import time
import urllib.error
import urllib.parse
import urllib.request
from pathlib import Path
from typing import Any

logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
logger = logging.getLogger("consortium_bootstrap")


def load_env_file(env_path: Path) -> dict[str, str]:
    """Parse key-value pairs from .env file."""
    env: dict[str, str] = {}
    if not env_path.exists():
        return env
    for line in env_path.read_text(encoding="utf-8").splitlines():
        line = line.strip()
        if not line or line.startswith("#") or "=" not in line:
            continue
        key, val = line.split("=", 1)
        env[key.strip()] = val.strip()
    return env


def http_get(url: str, timeout: float = 5.0) -> tuple[int, dict[str, Any] | str]:
    """Perform HTTP GET returning (status_code, parsed_json_or_text)."""
    req = urllib.request.Request(url, headers={"User-Agent": "CFI-Bootstrap/1.0"})
    try:
        with urllib.request.urlopen(req, timeout=timeout) as resp:
            raw = resp.read().decode("utf-8")
            try:
                return resp.status, json.loads(raw)
            except json.JSONDecodeError:
                return resp.status, raw
    except urllib.error.HTTPError as err:
        raw = err.read().decode("utf-8", errors="replace")
        try:
            return err.code, json.loads(raw)
        except json.JSONDecodeError:
            return err.code, raw
    except Exception as exc:
        raise ConnectionError(f"HTTP GET failed for {url}: {exc}") from exc


def http_post(
    url: str,
    payload: dict[str, Any],
    token: str | None = None,
    timeout: float = 10.0,
) -> tuple[int, dict[str, Any] | str]:
    """Perform HTTP POST returning (status_code, parsed_json_or_text)."""
    data = json.dumps(payload).encode("utf-8")
    headers = {
        "Content-Type": "application/json",
        "User-Agent": "CFI-Bootstrap/1.0",
    }
    if token:
        headers["Authorization"] = f"Bearer {token}"
    req = urllib.request.Request(url, data=data, headers=headers, method="POST")
    try:
        with urllib.request.urlopen(req, timeout=timeout) as resp:
            raw = resp.read().decode("utf-8")
            try:
                return resp.status, json.loads(raw)
            except json.JSONDecodeError:
                return resp.status, raw
    except urllib.error.HTTPError as err:
        raw = err.read().decode("utf-8", errors="replace")
        try:
            return err.code, json.loads(raw)
        except json.JSONDecodeError:
            return err.code, raw
    except Exception as exc:
        raise ConnectionError(f"HTTP POST failed for {url}: {exc}") from exc


def wait_for_service(name: str, url: str, max_retries: int = 15, delay: float = 2.0) -> bool:
    """Poll URL until HTTP 200 or max_retries exhausted."""
    for attempt in range(1, max_retries + 1):
        try:
            code, _ = http_get(url, timeout=3.0)
            if code == 200:
                logger.info("Service '%s' is ready at %s", name, url)
                return True
        except Exception:
            pass
        logger.debug("Waiting for '%s' (%s), attempt %d/%d...", name, url, attempt, max_retries)
        time.sleep(delay)
    logger.error("Service '%s' failed to become ready at %s within timeout.", name, url)
    return False


def run_docker_exec(
    container_name: str,
    cmd: list[str],
    stdin_input: str | None = None,
) -> str:
    """Run command inside a Docker container via docker exec without leaking sensitive inputs."""
    exec_flags = ["docker", "exec", "-i"] if stdin_input is not None else ["docker", "exec"]
    full_cmd = exec_flags + [container_name] + cmd
    res = subprocess.run(
        full_cmd,
        input=stdin_input,
        capture_output=True,
        text=True,
        check=False,
    )
    if res.returncode != 0:
        raise RuntimeError(
            f"docker exec failed on '{container_name}' (exit {res.returncode}):\n{res.stderr}\n{res.stdout}"
        )
    return res.stdout.strip()


def _validate_analyst_credentials(env: dict[str, str]) -> dict[str, str]:
    """Validate that independent, non-predictable analyst and operator credentials are supplied."""
    required_keys = (
        "ANALYST_BANK_A_PASSWORD",
        "ANALYST_BANK_B_PASSWORD",
        "ANALYST_BANK_C_PASSWORD",
        "CONSORTIUM_OPERATOR_PASSWORD",
    )
    resolved: dict[str, str] = {}
    forbidden_prefixes = (
        "replace_with_",
        "analyst",
        "admin",
        "auditor",
        "operator",
        "password",
        "changeme",
        "default",
    )
    for key in required_keys:
        val = (env.get(key) or os.environ.get(key) or "").strip()
        if not val or len(val) < 16 or val.lower().startswith(forbidden_prefixes):
            raise ValueError(
                f"Invalid or missing '{key}': bootstrap requires a non-predictable generated credential (>= 16 chars)."
            )
        resolved[key] = val

    if len(set(resolved.values())) != len(required_keys):
        raise ValueError(
            "Credentials across Bank A, Bank B, Bank C, and Consortium Operator must be mutually independent."
        )
    return resolved


def compute_deterministic_privacy_id(raw_id: str, entity_type: str = "customer") -> str:
    """Derive SHA256 HMAC-style privacy ID for test fixtures."""
    import hashlib
    canonical = f"{entity_type.lower()}:{raw_id.strip()}".encode()
    return hashlib.sha256(canonical).hexdigest()[:32]


def provision_or_verify_institutional_user(
    auth_svc: Any,
    username: str,
    plain_password: str | None = None,
    bank_id: str | None = None,
    roles: list[str] | None = None,
    clearance_level: int = 1,
    allow_credential_rotation: bool = False,
    **kwargs: Any,
) -> Any:
    """Register an institutional principal or verify and preserve an existing active principal.

    Refuses to silently overwrite an existing active principal's credentials or user_id
    unless allow_credential_rotation=True is explicitly supplied.
    """
    effective_password = plain_password or kwargs.get("password") or ""
    effective_bank_id = bank_id or kwargs.get("tenant_id") or ""
    effective_roles = roles if roles is not None else kwargs.get("roles", ["analyst"])
    existing = auth_svc.get_user(username) if hasattr(auth_svc, "get_user") else auth_svc.get_user_by_username(username)
    if existing is None:
        return auth_svc.register_user(
            username=username,
            plain_password=effective_password,
            bank_id=effective_bank_id,
            roles=effective_roles,
            clearance_level=clearance_level,
        )

    try:
        from app.infrastructure.security.auth_service import verify_password
        password_matches = verify_password(effective_password, existing.password_hash)
    except Exception:
        password_matches = bool(auth_svc.authenticate(username, effective_password)[0])
    bank_matches = existing.bank_id == effective_bank_id
    if password_matches and bank_matches and existing.is_active:
        auth_svc.lockout_manager.reset(username)
        return existing

    if not allow_credential_rotation:
        raise PermissionError(
            f"Active institutional principal '{username}' (bank='{existing.bank_id}') already exists "
            "with different credentials. Refusing to overwrite active principal without "
            "--allow-credential-rotation."
        )

    return auth_svc.register_user(
        username=username,
        plain_password=effective_password,
        bank_id=effective_bank_id,
        roles=effective_roles,
        clearance_level=clearance_level,
    )


def bootstrap_consortium(
    env_file: Path,
    backend_url: str = "http://127.0.0.1:8000",
    coordinator_url: str = "http://127.0.0.1:8200",
    worker_a_url: str = "http://127.0.0.1:8101",
    worker_b_url: str = "http://127.0.0.1:8102",
    worker_c_url: str = "http://127.0.0.1:8103",
    gateway_url: str = "http://127.0.0.1:80",
    consortium_id: str = "consortium_consortium_deployment_01",
    allow_credential_rotation: bool = False,
) -> dict[str, Any]:
    """Execute complete bootstrap sequence."""
    env = load_env_file(env_file)
    if not env:
        raise FileNotFoundError(f"Environment secrets file '{env_file}' not found.")

    analyst_creds = _validate_analyst_credentials(env)

    logger.info("--- Phase 1: Service Readiness Verifications ---")
    services = [
        ("Gateway", f"{gateway_url}/gateway-health"),
        ("Backend API", f"{backend_url}/health"),
        ("PSI Coordinator", f"{coordinator_url}/health"),
        ("Worker Bank A", f"{worker_a_url}/worker/health"),
        ("Worker Bank B", f"{worker_b_url}/worker/health"),
        ("Worker Bank C", f"{worker_c_url}/worker/health"),
    ]
    for name, probe_url in services:
        if not wait_for_service(name, probe_url):
            raise RuntimeError(f"Consortium readiness failed: '{name}' is unreachable at {probe_url}")

    logger.info("--- Phase 2: Ingesting Authoritative Synthetic Entities ---")
    pid_match = compute_deterministic_privacy_id("DE-TAX-88912234-MATCH", "customer")
    pid_clean = compute_deterministic_privacy_id("FR-TAX-11223344-CLEAN", "customer")

    entity_a = {
        "id": "target_nexus_investigation_01",
        "entity_type": "customer",
        "bank_id": "bank_a",
        "privacy_id": pid_match,
        "display_label": "CUST-A-NEXUS-01",
        "attributes": {
            "tax_id": "DE-TAX-88912234-MATCH",
            "jurisdiction": "DE",
            "risk_score": 0.88,
        },
        "risk_level": "CRITICAL",
        "alert_count": 3,
    }

    entity_b = {
        "id": "counterpart_b_nexus_match",
        "entity_type": "customer",
        "bank_id": "bank_b",
        "privacy_id": pid_match,
        "display_label": "CUST-B-NEXUS-MATCH",
        "attributes": {
            "tax_id": "DE-TAX-88912234-MATCH",
            "jurisdiction": "DE",
            "risk_score": 0.79,
        },
        "risk_level": "HIGH",
        "alert_count": 2,
    }

    entity_c = {
        "id": "counterpart_c_clean_benign",
        "entity_type": "customer",
        "bank_id": "bank_c",
        "privacy_id": pid_clean,
        "display_label": "CUST-C-CLEAN-01",
        "attributes": {
            "tax_id": "FR-TAX-11223344-CLEAN",
            "jurisdiction": "FR",
            "risk_score": 0.05,
        },
        "risk_level": "MINIMAL",
        "alert_count": 0,
    }

    code_a, res_a = http_post(
        f"{worker_a_url}/worker/store-entity",
        {"entity": entity_a},
        token=env["WORKER_BANK_A_INGESTION_TOKEN"],
    )
    code_b, res_b = http_post(
        f"{worker_b_url}/worker/store-entity",
        {"entity": entity_b},
        token=env["WORKER_BANK_B_INGESTION_TOKEN"],
    )
    code_c, res_c = http_post(
        f"{worker_c_url}/worker/store-entity",
        {"entity": entity_c},
        token=env["WORKER_BANK_C_INGESTION_TOKEN"],
    )

    if code_a != 200 or code_b != 200 or code_c != 200:
        raise RuntimeError(f"Entity ingestion failed: a={code_a}, b={code_b}, c={code_c}")
    logger.info("Synthetic entities stored into isolated vaults of Bank A, B, and C.")

    logger.info("--- Phase 3: Provisioning Users & Governed Consortium ---")
    py_bootstrap_script = f"""
import json
import sys
from app.infrastructure.security.auth_service import AuthenticationService, verify_password
from app.application.services.consortium_service import ConsortiumGovernanceService
from app.domain.consortium_governance import MemberRole

creds = json.loads(sys.stdin.read())
allow_rotation = bool(creds.get("__allow_credential_rotation__", False))
auth_svc = AuthenticationService.get_instance()

def _provision_or_preserve(username, password, bank_id, roles, clearance):
    existing = auth_svc.get_user(username)
    if existing is None:
        return auth_svc.register_user(
            username=username,
            plain_password=password,
            bank_id=bank_id,
            roles=roles,
            clearance_level=clearance,
        )
    password_matches = False
    try:
        password_matches = bool(verify_password(password, existing.password_hash))
    except Exception:
        password_matches = bool(auth_svc.authenticate(username, password)[0])
    if existing.bank_id == bank_id and existing.is_active and password_matches:
        auth_svc.lockout_manager.reset(username)
        return existing
    if not allow_rotation:
        raise PermissionError(
            f"Active institutional principal '{{username}}' already exists with different credentials. "
            "Refusing to overwrite without --allow-credential-rotation."
        )
    return auth_svc.register_user(
        username=username,
        plain_password=password,
        bank_id=bank_id,
        roles=roles,
        clearance_level=clearance,
    )

# Register or preserve institutional analysts with least-privilege institutional roles
u_a = _provision_or_preserve(
    "analyst_bank_a",
    creds["ANALYST_BANK_A_PASSWORD"],
    "bank_a",
    ["compliance", "investigator"],
    2,
)
u_b = _provision_or_preserve(
    "analyst_bank_b",
    creds["ANALYST_BANK_B_PASSWORD"],
    "bank_b",
    ["compliance", "investigator"],
    2,
)
u_c = _provision_or_preserve(
    "analyst_bank_c",
    creds["ANALYST_BANK_C_PASSWORD"],
    "bank_c",
    ["compliance", "investigator"],
    2,
)
u_op = _provision_or_preserve(
    "operator_consortium",
    creds["CONSORTIUM_OPERATOR_PASSWORD"],
    "consortium",
    ["operator", "compliance"],
    3,
)
for uname in ("analyst_bank_a", "analyst_bank_b", "analyst_bank_c", "operator_consortium"):
    auth_svc.lockout_manager.reset(uname)

token_a = auth_svc.create_token_bundle(u_a).access_token
token_b = auth_svc.create_token_bundle(u_b).access_token
token_c = auth_svc.create_token_bundle(u_c).access_token

# Register local entity in Bank A entity service
from app.application.services.entity_resolution import EntityResolutionService, _entity_to_dict
from app.application.services.alert_service import AlertIntelligenceService, _alert_to_dict
from app.application.services.case_service import CaseManagementService, _case_to_dict
from app.domain.enums import AlertSeverity, AlertStatus, CasePriority, CaseStatus
from app.domain.investigation_entities import Alert, Case, CaseEvent, Entity, EntityType, RiskLevel

ent_svc = EntityResolutionService()
ent = Entity(
    id="target_nexus_investigation_01",
    entity_type=EntityType.CUSTOMER,
    privacy_id="{pid_match}",
    bank_id="bank_a",
    display_label="CUST-A-NEXUS-01",
    attributes={{"tax_id": "DE-TAX-88912234-MATCH", "jurisdiction": "DE", "risk_score": 0.88}},
    risk_level=RiskLevel.CRITICAL,
    alert_count=3,
)
ent_svc._entities.set(ent.id, _entity_to_dict(ent))

# Seed authoritative Bank A alert referencing target_nexus_investigation_01
alert_svc = AlertIntelligenceService()
alert_a = Alert(
    id="alert_consortium_nexus_bank_a_01",
    bank_id="bank_a",
    transaction_id="tx_nexus_cross_bank_01",
    risk_score=880.0,
    severity=AlertSeverity.CRITICAL,
    status=AlertStatus.NEW,
    reason_codes=["CROSS_BANK_SYNDICATE_VELOCITY", "HIGH_RISK_CORRIDOR"],
    confidence=0.94,
    involved_entity_ids=["target_nexus_investigation_01"],
)
alert_svc._alert_store.set(alert_a.id, _alert_to_dict(alert_a))

# Seed authoritative Bank A case and isolated Bank B case for cross-tenant negative verification
case_svc = CaseManagementService()
if not case_svc.get_case("case_consortium_nexus_bank_a_01"):
    case_a = Case(
        id="case_consortium_nexus_bank_a_01",
        bank_id="bank_a",
        title="Bank A Cross-Border Syndicate Nexus Investigation",
        status=CaseStatus.OPEN,
        priority=CasePriority.P1_CRITICAL,
        assigned_to="analyst_bank_a",
        alert_ids=["alert_consortium_nexus_bank_a_01"],
        total_risk_score=880.0,
    )
    case_svc._cases.set(case_a.id, _case_to_dict(case_a))

if not case_svc.get_case("case_consortium_bank_b_isolated_01"):
    case_b = Case(
        id="case_consortium_bank_b_isolated_01",
        bank_id="bank_b",
        title="Bank B Confidential Counterparty Treasury Review",
        status=CaseStatus.OPEN,
        priority=CasePriority.P2_HIGH,
        assigned_to="analyst_bank_b",
        alert_ids=[],
        total_risk_score=640.0,
    )
    case_svc._cases.set(case_b.id, _case_to_dict(case_b))

gov_svc = ConsortiumGovernanceService()
c_id = "{consortium_id}"
consortium = gov_svc.get_consortium(c_id)
if not consortium:
    gov_svc.create_consortium(
        consortium_id=c_id,
        name="Consortium European Pilot Deployment",
        founder_bank_id="bank_a",
        auth_token=token_a,
    )
    gov_svc.add_member(c_id, "bank_b", role=MemberRole.FULL_MEMBER, auth_token=token_a)
    gov_svc.add_member(c_id, "bank_c", role=MemberRole.FULL_MEMBER, auth_token=token_a)

# Bilateral Grant A <-> B
grant_ab = gov_svc.get_pairwise_operational_grant(c_id, "grant_consortium_deployment_ab")
if not grant_ab:
    gov_svc.register_pairwise_operational_grant(
        consortium_id=c_id,
        grant_id="grant_consortium_deployment_ab",
        institution_a="bank_a",
        institution_b="bank_b",
        authority="BILATERAL_AML_TREATY",
        allowed_purposes=["AML_COLLABORATIVE_INVESTIGATION"],
        allowed_jurisdictions=["EU", "US"],
        institution_jurisdictions={{"bank_a": "EU", "bank_b": "EU"}},
        allow_cross_border=False,
        allow_institution_identity_sharing=True,
        revision=1,
        auth_token=token_a,
    )
    gov_svc.approve_pairwise_operational_grant(
        consortium_id=c_id,
        grant_id="grant_consortium_deployment_ab",
        approving_bank_id="bank_b",
        auth_token=token_b,
    )

# Bilateral Grant A <-> C
grant_ac = gov_svc.get_pairwise_operational_grant(c_id, "grant_consortium_deployment_ac")
if not grant_ac:
    gov_svc.register_pairwise_operational_grant(
        consortium_id=c_id,
        grant_id="grant_consortium_deployment_ac",
        institution_a="bank_a",
        institution_b="bank_c",
        authority="BILATERAL_AML_TREATY",
        allowed_purposes=["AML_COLLABORATIVE_INVESTIGATION"],
        allowed_jurisdictions=["EU", "US"],
        institution_jurisdictions={{"bank_a": "EU", "bank_c": "EU"}},
        allow_cross_border=False,
        allow_institution_identity_sharing=True,
        revision=1,
        auth_token=token_a,
    )
    gov_svc.approve_pairwise_operational_grant(
        consortium_id=c_id,
        grant_id="grant_consortium_deployment_ac",
        approving_bank_id="bank_c",
        auth_token=token_c,
    )

g_ab = gov_svc.get_pairwise_operational_grant(c_id, "grant_consortium_deployment_ab")
g_ac = gov_svc.get_pairwise_operational_grant(c_id, "grant_consortium_deployment_ac")

result = {{
    "consortium_id": c_id,
    "scope_digest_ab": g_ab["scope_digest"] if g_ab else "",
    "scope_digest_ac": g_ac["scope_digest"] if g_ac else "",
}}
print(json.dumps(result))
"""

    bootstrap_input = dict(analyst_creds)
    bootstrap_input["__allow_credential_rotation__"] = str(allow_credential_rotation)
    stdout = run_docker_exec(
        "cfi-api-server",
        ["python", "-c", py_bootstrap_script],
        stdin_input=json.dumps(bootstrap_input),
    )
    # Extract the last line which contains JSON output
    last_line = [ln for ln in stdout.splitlines() if ln.strip().startswith("{")][-1]
    gov_meta = json.loads(last_line)
    digest_ab = gov_meta["scope_digest_ab"]
    digest_ac = gov_meta["scope_digest_ac"]
    logger.info("Consortium governance established. Scope digests: AB=%s, AC=%s", digest_ab[:12], digest_ac[:12])

    logger.info("Synchronizing authoritative identity and consortium stores to coordinator daemon...")
    import tempfile
    with tempfile.TemporaryDirectory() as td:
        t_acc = os.path.join(td, "auth_identity_store.json")
        t_gov = os.path.join(td, "consortium_governance_store.json")
        subprocess.run(["docker", "cp", "cfi-api-server:/app/storage/auth_identity_store.json", t_acc], check=True)
        subprocess.run(["docker", "cp", "cfi-api-server:/app/storage/consortium_governance_store.json", t_gov], check=True)
        subprocess.run(["docker", "cp", t_acc, "cfi-coordinator:/vault/auth_identity_store.json"], check=True)
        subprocess.run(["docker", "cp", t_gov, "cfi-coordinator:/vault/consortium_governance_store.json"], check=True)
        subprocess.run(["docker", "exec", "-u", "0", "cfi-coordinator", "chown", "-R", "worker:worker", "/vault"], check=True)
        subprocess.run(["docker", "exec", "-u", "0", "cfi-coordinator", "chmod", "600", "/vault/auth_identity_store.json", "/vault/consortium_governance_store.json"], check=True)
    logger.info("Stores synchronized to coordinator vault.")

    logger.info("--- Phase 4: Authorizing Grants on Institutional Workers ---")
    # Worker Bank A authorizes AB and AC
    code_a_ab, _ = http_post(
        f"{worker_a_url}/worker/authorize-grant",
        {
            "consortium_id": consortium_id,
            "grant_id": "grant_consortium_deployment_ab",
            "revision": 1,
            "scope_digest": digest_ab,
            "permitted_counterparts": ["bank_b"],
            "permitted_purposes": ["AML_COLLABORATIVE_INVESTIGATION"],
            "allow_institution_identity_sharing": True,
        },
        token=env["WORKER_BANK_A_ADMIN_TOKEN"],
    )
    code_a_ac, _ = http_post(
        f"{worker_a_url}/worker/authorize-grant",
        {
            "consortium_id": consortium_id,
            "grant_id": "grant_consortium_deployment_ac",
            "revision": 1,
            "scope_digest": digest_ac,
            "permitted_counterparts": ["bank_c"],
            "permitted_purposes": ["AML_COLLABORATIVE_INVESTIGATION"],
            "allow_institution_identity_sharing": True,
        },
        token=env["WORKER_BANK_A_ADMIN_TOKEN"],
    )

    # Worker Bank B authorizes AB
    code_b_ab, _ = http_post(
        f"{worker_b_url}/worker/authorize-grant",
        {
            "consortium_id": consortium_id,
            "grant_id": "grant_consortium_deployment_ab",
            "revision": 1,
            "scope_digest": digest_ab,
            "permitted_counterparts": ["bank_a"],
            "permitted_purposes": ["AML_COLLABORATIVE_INVESTIGATION"],
            "allow_institution_identity_sharing": True,
        },
        token=env["WORKER_BANK_B_ADMIN_TOKEN"],
    )

    # Worker Bank C authorizes AC
    code_c_ac, _ = http_post(
        f"{worker_c_url}/worker/authorize-grant",
        {
            "consortium_id": consortium_id,
            "grant_id": "grant_consortium_deployment_ac",
            "revision": 1,
            "scope_digest": digest_ac,
            "permitted_counterparts": ["bank_a"],
            "permitted_purposes": ["AML_COLLABORATIVE_INVESTIGATION"],
            "allow_institution_identity_sharing": True,
        },
        token=env["WORKER_BANK_C_ADMIN_TOKEN"],
    )

    if not all(c == 200 for c in [code_a_ab, code_a_ac, code_b_ab, code_c_ac]):
        raise RuntimeError(
            f"Worker grant authorization failed: a_ab={code_a_ab}, a_ac={code_a_ac}, b_ab={code_b_ab}, c_ac={code_c_ac}"
        )

    logger.info("Institutional worker nodes authorized bilateral grants successfully.")

    summary = {
        "status": "BOOTSTRAP_SUCCESSFUL",
        "consortium_id": consortium_id,
        "institutions": ["bank_a", "bank_b", "bank_c"],
        "grants": ["grant_consortium_deployment_ab", "grant_consortium_deployment_ac"],
        "entities": {
            "target_nexus_investigation_01": {"bank": "bank_a", "expected_match": "bank_b"},
            "counterpart_b_nexus_match": {"bank": "bank_b", "matches": "target_nexus_investigation_01"},
            "counterpart_c_clean_benign": {"bank": "bank_c", "matches": "NONE"},
        },
        "endpoints": {
            "gateway": gateway_url,
            "backend": backend_url,
            "coordinator": coordinator_url,
            "worker_a": worker_a_url,
            "worker_b": worker_b_url,
            "worker_c": worker_c_url,
        },
    }
    return summary


def main() -> None:
    parser = argparse.ArgumentParser(description="Bootstrap consortium deployment environment.")
    parser.add_argument("--env-file", type=Path, default=Path(".env.consortium"))
    parser.add_argument("--backend-url", default="http://127.0.0.1:8000")
    parser.add_argument("--coordinator-url", default="http://127.0.0.1:8200")
    parser.add_argument("--worker-a-url", default="http://127.0.0.1:8101")
    parser.add_argument("--worker-b-url", default="http://127.0.0.1:8102")
    parser.add_argument("--worker-c-url", default="http://127.0.0.1:8103")
    parser.add_argument("--gateway-url", default="http://127.0.0.1:80")
    parser.add_argument("--consortium-id", default="consortium_consortium_deployment_01")
    parser.add_argument(
        "--allow-credential-rotation",
        action="store_true",
        help="Explicitly allow overwriting existing active institutional user credentials.",
    )
    args = parser.parse_args()

    summary = bootstrap_consortium(
        env_file=args.env_file,
        backend_url=args.backend_url,
        coordinator_url=args.coordinator_url,
        worker_a_url=args.worker_a_url,
        worker_b_url=args.worker_b_url,
        worker_c_url=args.worker_c_url,
        gateway_url=args.gateway_url,
        consortium_id=args.consortium_id,
        allow_credential_rotation=args.allow_credential_rotation,
    )
    sys.stdout.write(json.dumps(summary, indent=2) + "\n")


if __name__ == "__main__":
    main()