#!/usr/bin/env bash # dth_mpq.test.sh — e2e verification for S1..S10 of chg_0013 medical_privacy_query # (3-hospital medical privacy query: 6-step multi-operator composition; no new code). # # 10 cases: # C1: 3 hospital DID setup + verify_with_correct_secret=true + schema_version=v2 # C2: DID tamper detection — verify_with= → verify=false # C3: PSI 3-way intersection correct → [p01,p02,p03] (2 sequential calls: A∩B, then (A∩B)∩C) # C4: PSI no leak of non-intersection elements (no p04..p09 in outputs) # C5: tee-inference attestation_key matches → verify_code=0 + "signature matches" # C6: tee-inference wrong verify_key → verify_code=1 + "signature mismatch" # C7: Paillier cross-hospital sum invariant → sum_decrypted=22 (7+10+5) # C8: ABAC decision matrix → 1 allow (oncology+10.0.0.5) + 2 deny (engineer / ip-range) # C9: Audit chain ≥ 5 entries + chainValid=true + no PHI in entries # C10: E2E full-flow orchestration (pass=39 fail=0, wall-clock < 60s, no PHI) # # Exit 0 only if all 10 cases pass. Each assertion is a hard-fail. # # Usage: # DTH_BASE=http://127.0.0.1:30000 bash tests/e2e/dth_mpq.test.sh # # Pre-req: backend running on DTH_BASE, admin seed loaded (run.sh dev). set -uo pipefail BASE="${DTH_BASE:-http://127.0.0.1:${PORT:-30000}}" LOGIN_URL="$BASE/api/ssw/dth/auth/login" RUNS_URL="$BASE/api/ssw/dth/experiment/runs" pass=0; fail=0; fails=() # Wall-clock start (for C10) T_START=$(date +%s) json_get() { local field="$1"; local body="$2" python3 -c "import json,sys; d=json.loads(sys.argv[1]); print(d${field})" "$body" 2>/dev/null } login() { curl -s -X POST "$LOGIN_URL" -H 'Content-Type: application/json' -d "{\"username\":\"$1\",\"password\":\"$2\"}" } assert() { local id="$1"; local cond="$2"; local detail="$3" if eval "$cond"; then pass=$((pass+1)) printf " ✓ %s\n" "$id" else fail=$((fail+1)) fails+=("$id: $detail") printf " ✗ %s — %s\n" "$id" "$detail" fi } # ----- pre: login admin ----- ADMIN_BODY="$(login admin 123456)" ADMIN_TOKEN="$(json_get "['data']['token']" "$ADMIN_BODY")" assert "pre_admin_login" "[ -n '$ADMIN_TOKEN' ]" "admin token empty; body=$ADMIN_BODY" export ADMIN_TOKEN RUNS_URL if [ -z "$ADMIN_TOKEN" ]; then echo "---"; echo "FATAL: admin login failed; aborting" exit 1 fi # ============================================================ # C1 (S1): 3 医院 DID 建立 + HMAC 验证 + schema_version=v2 # - did-identity × 3 (hospitalA/B/C) method=mpq # - 每家 verify_with_correct_secret=true # ============================================================ # HospitalA DID DID_A_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ -d '{"operator":"did-identity","input":{"method":"mpq","identifier":"hospitalA","secret":"mpq-secret-A"}}' "$RUNS_URL")" DID_A_ID="$(json_get "['data']['run_id']" "$DID_A_BODY")" DID_A_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$DID_A_ID")" DID_A_VAL="$(json_get "['data']['output_json']['did']['did']" "$DID_A_GET")" assert "C1_did_hospitalA_created" "[ '$DID_A_VAL' = 'did:mpq:hospitalA' ]" "HospitalA DID=$DID_A_VAL; body=$DID_A_GET" DID_A_VCS="$(json_get "['data']['output_json']['did']['verify_with_correct_secret']" "$DID_A_GET")" assert "C1_did_hospitalA_verify_ok" "[ '$DID_A_VCS' = 'True' ]" "verify_with_correct_secret=$DID_A_VCS" DID_A_ALG="$(json_get "['data']['output_json']['did']['algorithm']" "$DID_A_GET")" assert "C1_did_hospitalA_algorithm_v2" "[ '$DID_A_ALG' = 'did-hmac-v2' ]" "algorithm=$DID_A_ALG" # HospitalB DID DID_B_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ -d '{"operator":"did-identity","input":{"method":"mpq","identifier":"hospitalB","secret":"mpq-secret-B"}}' "$RUNS_URL")" DID_B_ID="$(json_get "['data']['run_id']" "$DID_B_BODY")" DID_B_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$DID_B_ID")" DID_B_VAL="$(json_get "['data']['output_json']['did']['did']" "$DID_B_GET")" assert "C1_did_hospitalB_created" "[ '$DID_B_VAL' = 'did:mpq:hospitalB' ]" "HospitalB DID=$DID_B_VAL" DID_B_VCS="$(json_get "['data']['output_json']['did']['verify_with_correct_secret']" "$DID_B_GET")" assert "C1_did_hospitalB_verify_ok" "[ '$DID_B_VCS' = 'True' ]" "verify_with_correct_secret=$DID_B_VCS" # HospitalC DID DID_C_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ -d '{"operator":"did-identity","input":{"method":"mpq","identifier":"hospitalC","secret":"mpq-secret-C"}}' "$RUNS_URL")" DID_C_ID="$(json_get "['data']['run_id']" "$DID_C_BODY")" DID_C_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$DID_C_ID")" DID_C_VAL="$(json_get "['data']['output_json']['did']['did']" "$DID_C_GET")" assert "C1_did_hospitalC_created" "[ '$DID_C_VAL' = 'did:mpq:hospitalC' ]" "HospitalC DID=$DID_C_VAL" DID_C_VCS="$(json_get "['data']['output_json']['did']['verify_with_correct_secret']" "$DID_C_GET")" assert "C1_did_hospitalC_verify_ok" "[ '$DID_C_VCS' = 'True' ]" "verify_with_correct_secret=$DID_C_VCS" # 共同 schema_version=v2 (来自 HospitalA 的 response 已检, 此处用一个新独立 call 验证 schema 字段确实暴露) DID_AUTH_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ -d '{"operator":"did-identity","input":{"method":"mpq","identifier":"hospitalA","secret":"mpq-secret-A"}}' "$RUNS_URL")" DID_AUTH_ID="$(json_get "['data']['run_id']" "$DID_AUTH_BODY")" DID_AUTH_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$DID_AUTH_ID")" DID_AUTH_SCHEMA="$(json_get "['data']['output_json']['schema_version']" "$DID_AUTH_GET")" assert "C1_did_schema_version_v2" "[ '$DID_AUTH_SCHEMA' = 'v2' ]" "schema_version=$DID_AUTH_SCHEMA" # ============================================================ # C2 (S2): DID 篡改检测 — 用错 secret (verify_with=secret+'-tampered') → verify=false # operator 的 verify_with_wrong_secret 用 secret+'-tampered' 自动派生;此处我们显式传 verify_with # ============================================================ DID_BADSEC_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ -d '{"operator":"did-identity","input":{"method":"mpq","identifier":"hospitalA","secret":"mpq-secret-A","verify_with":"mpq-secret-A-tampered"}}' "$RUNS_URL")" DID_BADSEC_ID="$(json_get "['data']['run_id']" "$DID_BADSEC_BODY")" DID_BADSEC_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$DID_BADSEC_ID")" DID_BADSEC_VCS="$(json_get "['data']['output_json']['did']['verify_with_correct_secret']" "$DID_BADSEC_GET")" assert "C2_did_tamper_detected" "[ '$DID_BADSEC_VCS' = 'False' ]" "wrong verify_with should fail verify; got=$DID_BADSEC_VCS; body=$DID_BADSEC_GET" # ============================================================ # C3 (S3): PSI 三方交集 = [p01, p02, p03] # H_A = [p01,p02,p03,p04,p05] (5) ← spec-aligned # H_B = [p01,p03,p02,p07] (4) # H_C = [p01,p02,p03,p08,p09] (5) # psi 仅两两 → 两次 sequential call: (A∩B)∩C # ============================================================ # Note: spec's expected_intersection=[p01,p02,p03] requires p03 ∈ H_A. # H_B set_a 实际 = [p01,p02,p03,p07] (用前 4 个做 A∩B 一次,size=3)。 PSI_AB_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ -d '{"operator":"psi","input":{"set_a":["p01","p02","p03","p04","p05"],"set_b":["p01","p02","p03","p07"],"hash_key":"psi-mpq"}}' "$RUNS_URL")" PSI_AB_ID="$(json_get "['data']['run_id']" "$PSI_AB_BODY")" PSI_AB_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$PSI_AB_ID")" PSI_AB_LIST="$(python3 -c " import json,sys d=json.loads(sys.argv[1]) print(','.join(sorted(d['data']['output_json']['psi']['intersection']))) " "$PSI_AB_GET" 2>/dev/null)" PSI_AB_SIZE="$(json_get "['data']['output_json']['psi']['size']" "$PSI_AB_GET")" assert "C3_psi_AB_size_3" "[ '$PSI_AB_SIZE' = '3' ]" "A∩B size=$PSI_AB_SIZE; body=$PSI_AB_GET" assert "C3_psi_AB_intersection" "[ '$PSI_AB_LIST' = 'p01,p02,p03' ]" "A∩B=$PSI_AB_LIST" # Pass (A∩B) ∩ C PSI_ABC_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ -d '{"operator":"psi","input":{"set_a":["p01","p02","p03"],"set_b":["p01","p02","p03","p08","p09"],"hash_key":"psi-mpq"}}' "$RUNS_URL")" PSI_ABC_ID="$(json_get "['data']['run_id']" "$PSI_ABC_BODY")" PSI_ABC_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$PSI_ABC_ID")" PSI_ABC_LIST="$(python3 -c " import json,sys d=json.loads(sys.argv[1]) print(','.join(sorted(d['data']['output_json']['psi']['intersection']))) " "$PSI_ABC_GET" 2>/dev/null)" PSI_ABC_SIZE="$(json_get "['data']['output_json']['psi']['size']" "$PSI_ABC_GET")" PSI_ABC_SCHEMA="$(json_get "['data']['output_json']['schema_version']" "$PSI_ABC_GET")" assert "C3_psi_ABC_size_3" "[ '$PSI_ABC_SIZE' = '3' ]" "A∩B∩C size=$PSI_ABC_SIZE" assert "C3_psi_ABC_intersection" "[ '$PSI_ABC_LIST' = 'p01,p02,p03' ]" "A∩B∩C=$PSI_ABC_LIST; body=$PSI_ABC_GET" assert "C3_psi_schema_v2" "[ '$PSI_ABC_SCHEMA' = 'v2' ]" "schema=$PSI_ABC_SCHEMA" # ============================================================ # C4 (S4): PSI 不泄露非交集元素 (隐私不变性) # 全 PSI 输出中绝不能出现 p04/p05/p06/p07/p08/p09,也不能出现诊断码 D-101 # ============================================================ PSI_LEAK_CHECK="$(python3 -c " import json,sys body=sys.argv[1] # Non-intersection patient IDs (from the new fixture): p04/p05 in A only, p07 in B only, p08/p09 in C only. # Also forbid D-101 (diagnosis code, must not leak in PSI result). forbidden=['p04','p05','p07','p08','p09','D-101'] hits=[f for f in forbidden if f in body] print('LEAKED' if hits else 'OK', '|'.join(hits)) " "$PSI_ABC_GET" 2>/dev/null)" PSI_LEAK_STATUS="$(echo "$PSI_LEAK_CHECK" | awk '{print $1}')" assert "C4_psi_no_non_intersection_leak" "[ '$PSI_LEAK_STATUS' = 'OK' ]" "non-intersection leak: $PSI_LEAK_CHECK" PSI_LEAK_FLAG="$(json_get "['data']['output_json']['psi']['leaked_non_intersection']" "$PSI_ABC_GET")" assert "C4_psi_leaked_flag_false" "[ '$PSI_LEAK_FLAG' = 'False' ]" "leaked_non_intersection=$PSI_LEAK_FLAG" # output size <= min(5,4,5) = 4 PSI_MAX_OK="$(python3 -c " import json,sys d=json.loads(sys.argv[1]) n = len(d['data']['output_json']['psi']['intersection']) print('OK' if n <= 4 else 'TOO_BIG', n) " "$PSI_ABC_GET" 2>/dev/null)" PSI_MAX_STATUS="$(echo "$PSI_MAX_OK" | awk '{print $1}')" assert "C4_psi_output_size_within_bound" "[ '$PSI_MAX_STATUS' = 'OK' ]" "output size bound: $PSI_MAX_OK" # 算法身份验证 (确保 ops 用的是 v2 cuckoo-psi) PSI_ALG="$(json_get "['data']['output_json']['psi']['algorithm']" "$PSI_ABC_GET")" assert "C4_psi_algorithm_v2" "[ '$PSI_ALG' = 'cuckoo-psi-v2' ]" "algorithm=$PSI_ALG" # ============================================================ # C5 (S5): tee-inference attestation_key matches → verify_code=0 # prompt="Recommend treatment for 3 patients with D-101" # model="gpt-medical-v2" # attestation_key="mpq-attest-key-2026" # ============================================================ TEE_PROMPT="Recommend treatment for 3 patients with D-101 (MPQ PSI intersection)" TEE_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ -d "{\"operator\":\"tee-inference\",\"input\":{\"prompt\":\"$TEE_PROMPT\",\"model\":\"gpt-medical-v2\",\"attestation_key\":\"mpq-attest-key-2026\"}}" "$RUNS_URL")" TEE_ID="$(json_get "['data']['run_id']" "$TEE_BODY")" TEE_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$TEE_ID")" TEE_ATT="$(json_get "['data']['output_json']['tee']['attestation']" "$TEE_GET")" TEE_RESP="$(json_get "['data']['output_json']['tee']['response']" "$TEE_GET")" TEE_PCR0="$(json_get "['data']['output_json']['tee']['quote']['pcr0']" "$TEE_GET")" TEE_PCR1="$(json_get "['data']['output_json']['tee']['quote']['pcr1']" "$TEE_GET")" TEE_NONCE="$(json_get "['data']['output_json']['tee']['quote']['nonce']" "$TEE_GET")" TEE_SIG="$(json_get "['data']['output_json']['tee']['quote']['signature']" "$TEE_GET")" TEE_VC="$(json_get "['data']['output_json']['tee']['verify_code']" "$TEE_GET")" TEE_VM="$(json_get "['data']['output_json']['tee']['verify_message']" "$TEE_GET")" TEE_SCHEMA="$(json_get "['data']['output_json']['schema_version']" "$TEE_GET")" assert "C5_tee_attestation_mock_prefix" "[ \"\${TEE_ATT:0:9}\" = 'tee-mock-' ]" "attestation=$TEE_ATT" assert "C5_tee_response_echoes" "echo \"$TEE_RESP\" | grep -q 'echoes:' " "response=$TEE_RESP" assert "C5_tee_quote_pcr0_present" "[ -n '$TEE_PCR0' ] && [ \"\${#TEE_PCR0}\" = '64' ]" "pcr0=$TEE_PCR0" assert "C5_tee_quote_pcr1_present" "[ -n '$TEE_PCR1' ] && [ \"\${#TEE_PCR1}\" = '64' ]" "pcr1=$TEE_PCR1" assert "C5_tee_quote_nonce_present" "[ -n '$TEE_NONCE' ] && [ \"\${#TEE_NONCE}\" = '32' ]" "nonce=$TEE_NONCE (expected 16-byte hex)" assert "C5_tee_quote_signature_present" "[ -n '$TEE_SIG' ] && [ \"\${#TEE_SIG}\" = '64' ]" "signature=$TEE_SIG (expected 32-byte hex)" assert "C5_tee_verify_code_0" "[ '$TEE_VC' = '0' ]" "verify_code=$TEE_VC; body=$TEE_GET" assert "C5_tee_verify_message_matches" "[ '$TEE_VM' = 'signature matches' ]" "verify_message=$TEE_VM" assert "C5_tee_schema_v2" "[ '$TEE_SCHEMA' = 'v2' ]" "schema=$TEE_SCHEMA" # ============================================================ # C6 (S6): tee-inference wrong verify_key → verify_code=1 # 同 prompt + attestation_key, verify_key="wrong-key-not-matching" # ============================================================ TEE_BAD_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ -d "{\"operator\":\"tee-inference\",\"input\":{\"prompt\":\"$TEE_PROMPT\",\"model\":\"gpt-medical-v2\",\"attestation_key\":\"mpq-attest-key-2026\",\"verify_key\":\"wrong-key-not-matching\"}}" "$RUNS_URL")" TEE_BAD_ID="$(json_get "['data']['run_id']" "$TEE_BAD_BODY")" TEE_BAD_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$TEE_BAD_ID")" TEE_BAD_VC="$(json_get "['data']['output_json']['tee']['verify_code']" "$TEE_BAD_GET")" TEE_BAD_VM="$(json_get "['data']['output_json']['tee']['verify_message']" "$TEE_BAD_GET")" TEE_BAD_RESP="$(json_get "['data']['output_json']['tee']['response']" "$TEE_BAD_GET")" TEE_BAD_SCHEMA="$(json_get "['data']['output_json']['schema_version']" "$TEE_BAD_GET")" assert "C6_tee_verify_code_1" "[ '$TEE_BAD_VC' = '1' ]" "verify_code=$TEE_BAD_VC; body=$TEE_BAD_GET" assert "C6_tee_verify_message_mismatch" "[ '$TEE_BAD_VM' = 'signature mismatch' ]" "verify_message=$TEE_BAD_VM" assert "C6_tee_response_still_generated" "[ -n '$TEE_BAD_RESP' ] && echo \"$TEE_BAD_RESP\" | grep -q 'echoes:'" "response=$TEE_BAD_RESP (enclave side effect)" assert "C6_tee_schema_v2" "[ '$TEE_BAD_SCHEMA' = 'v2' ]" "schema=$TEE_BAD_SCHEMA" # ============================================================ # C7 (S7): Paillier 跨院同态求和 = Enc(7)·Enc(10)·Enc(5) ≡ Enc(22) # 注: operator 为验证同态性,会在 output.values 里暴露明文(刻意为之) # 测试 invariant (sum_decrypted == plaintext_sum) 即可 # ============================================================ PAI_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ -d '{"operator":"paillier-he","input":{"values":[7,10,5],"seed":"mpq-paillier"}}' "$RUNS_URL")" PAI_ID="$(json_get "['data']['run_id']" "$PAI_BODY")" PAI_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$PAI_ID")" PAI_ASSERT="$(python3 -c " import json,sys d=json.loads(sys.argv[1]) p=d['data']['output_json']['paillier'] sv=d['data']['output_json']['schema_version'] ok = (p['sum_decrypted'] == 22 and p['plaintext_sum'] == 22 and p['homomorphic_invariant_ok'] is True and sv == 'v2') print('true' if ok else 'false', 'sum_dec=', p['sum_decrypted'], 'plain_sum=', p['plaintext_sum'], 'inv=', p['homomorphic_invariant_ok'], 'sv=', sv) " "$PAI_GET" 2>/dev/null)" PAI_OK="$(echo "$PAI_ASSERT" | awk '{print $1}')" assert "C7_paillier_sum_decrypted_22" "[ '$PAI_OK' = 'true' ]" "paillier: $PAI_ASSERT; body=$PAI_GET" # 进一步: ciphertexts 数量 == values 数量 (3); ciphertexts 均为 16-hex 字符串 # (json_get returns Python list repr with single quotes; write full body to disk and parse.) PAI_GET_FILE="/tmp/dth_mpq_pai_get.json" printf '%s' "$PAI_GET" > "$PAI_GET_FILE" PAI_CT_LEN="$(python3 -c " import json d=json.load(open('$PAI_GET_FILE')) arr=d['data']['output_json']['paillier']['ciphertexts'] print(len(arr)) ")" assert "C7_paillier_3ciphertexts" "[ '$PAI_CT_LEN' = '3' ]" "ciphertexts len=$PAI_CT_LEN; file=$PAI_GET_FILE" # SumEnc 字段非 None PAI_SUMENC="$(json_get "['data']['output_json']['paillier']['sum_encrypted']" "$PAI_GET")" assert "C7_paillier_sum_encrypted_present" "[ -n '$PAI_SUMENC' ] && [ '$PAI_SUMENC' != 'None' ]" "sum_encrypted=$PAI_SUMENC" # ============================================================ # C8 (S8): ABAC 决策矩阵 — 1 allow + 2 deny # policy: allow_if {subject.role=medical-researcher, subject.dept=oncology} # env_constraints.ip_range = "10.0.0.0/24" # ============================================================ POLICY='{"allow_if":{"subject.role":"medical-researcher","subject.dept":"oncology"},"env_constraints":{"ip_range":"10.0.0.0/24"}}' # 8a: allow (medical-researcher@oncology + ip=10.0.0.5) ABAC_OK_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ -d "{\"operator\":\"abac-policy\",\"input\":{\"subject\":{\"role\":\"medical-researcher\",\"dept\":\"oncology\"},\"resource\":{},\"env\":{\"ip\":\"10.0.0.5\"},\"policy\":$POLICY}}" "$RUNS_URL")" ABAC_OK_ID="$(json_get "['data']['run_id']" "$ABAC_OK_BODY")" ABAC_OK_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$ABAC_OK_ID")" ABAC_OK_DEC="$(json_get "['data']['output_json']['abac']['decision']" "$ABAC_OK_GET")" assert "C8_abac_researcher_oncology_allow" "[ '$ABAC_OK_DEC' = 'allow' ]" "should allow; dec=$ABAC_OK_DEC; body=$ABAC_OK_GET" # 8b: deny — role mismatch (engineer) ABAC_ROLE_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ -d "{\"operator\":\"abac-policy\",\"input\":{\"subject\":{\"role\":\"engineer\",\"dept\":\"none\"},\"resource\":{},\"env\":{\"ip\":\"10.0.0.5\"},\"policy\":$POLICY}}" "$RUNS_URL")" ABAC_ROLE_ID="$(json_get "['data']['run_id']" "$ABAC_ROLE_BODY")" ABAC_ROLE_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$ABAC_ROLE_ID")" ABAC_ROLE_DEC="$(json_get "['data']['output_json']['abac']['decision']" "$ABAC_ROLE_GET")" ABAC_ROLE_REASON="$(python3 -c " import json,sys d=json.loads(sys.argv[1]) r=d['data']['output_json']['abac']['reasons'] print('|'.join(r)) " "$ABAC_ROLE_GET" 2>/dev/null)" assert "C8_abac_engineer_deny" "[ '$ABAC_ROLE_DEC' = 'deny' ] && echo '$ABAC_ROLE_REASON' | grep -q 'subject.role'" "engineer should be role-deny; dec=$ABAC_ROLE_DEC reasons=$ABAC_ROLE_REASON" # 8c: deny — ip outside CIDR ABAC_IP_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ -d "{\"operator\":\"abac-policy\",\"input\":{\"subject\":{\"role\":\"medical-researcher\",\"dept\":\"oncology\"},\"resource\":{},\"env\":{\"ip\":\"192.168.1.5\"},\"policy\":$POLICY}}" "$RUNS_URL")" ABAC_IP_ID="$(json_get "['data']['run_id']" "$ABAC_IP_BODY")" ABAC_IP_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$ABAC_IP_ID")" ABAC_IP_DEC="$(json_get "['data']['output_json']['abac']['decision']" "$ABAC_IP_GET")" ABAC_IP_REASON="$(python3 -c " import json,sys d=json.loads(sys.argv[1]) r=d['data']['output_json']['abac']['reasons'] print('|'.join(r)) " "$ABAC_IP_GET" 2>/dev/null)" assert "C8_abac_ip_range_deny" "[ '$ABAC_IP_DEC' = 'deny' ] && echo '$ABAC_IP_REASON' | grep -q 'env_violation:ip'" "192.168.1.5 should be ip-range-deny; dec=$ABAC_IP_DEC reasons=$ABAC_IP_REASON" # ============================================================ # C9 (S9): 审计链 HMAC 完整 (≥ 5 entries) + chainValid=true + 无 PHI # 5 entries: did-A + psi + tee + paillier + abac # ============================================================ AUD_HELPER="/tmp/dth_mpq_audit_helper.py" cat > "$AUD_HELPER" <<'PYEOF' import json, subprocess, os, sys prior_file = sys.argv[1] action = sys.argv[2] with open(prior_file) as f: prior = json.load(f) req = { "operator":"compliance-audit", "input":{ "action": action, "actor": "mpq-aggregator", "user": "mpq-aggregator", "timestamp": "2026-10-02T12:00:00Z", "entries": prior, } } admin_token = os.environ["ADMIN_TOKEN"] runs_url = os.environ["RUNS_URL"] auth_hdr = ["-H",f"Authorization: Bearer {admin_token}"] ct_hdr = ["-H","Content-Type: application/json"] post = subprocess.run( ["curl","-s",*auth_hdr,*ct_hdr,"-d",json.dumps(req),runs_url], capture_output=True, text=True, ) post_body = json.loads(post.stdout) run_id = post_body["data"]["run_id"] get_url = f"{runs_url}/{run_id}" get = subprocess.run(["curl","-s",*auth_hdr,get_url], capture_output=True, text=True) get_body = json.loads(get.stdout) entry = get_body["data"]["output_json"]["audit"]["entry"] print(json.dumps(entry)) PYEOF append_audit() { local action="$1" local prior_file="$2" python3 "$AUD_HELPER" "$prior_file" "$action" } echo '[]' > /tmp/dth_mpq_aud_prior_0.json AUD_E1="$(append_audit 'mpq.s1.did.hospitalA' /tmp/dth_mpq_aud_prior_0.json)" echo "[$AUD_E1]" > /tmp/dth_mpq_aud_prior_1.json AUD_E2="$(append_audit 'mpq.s3.psi.summary' /tmp/dth_mpq_aud_prior_1.json)" echo "[$AUD_E1,$AUD_E2]" > /tmp/dth_mpq_aud_prior_2.json AUD_E3="$(append_audit 'mpq.s5.tee.attest' /tmp/dth_mpq_aud_prior_2.json)" echo "[$AUD_E1,$AUD_E2,$AUD_E3]" > /tmp/dth_mpq_aud_prior_3.json AUD_E4="$(append_audit 'mpq.s7.paillier' /tmp/dth_mpq_aud_prior_3.json)" echo "[$AUD_E1,$AUD_E2,$AUD_E3,$AUD_E4]" > /tmp/dth_mpq_aud_prior_4.json AUD_E5="$(append_audit 'mpq.s8.abac.allow' /tmp/dth_mpq_aud_prior_4.json)" # Sanity: each AUD_E* must be non-empty JSON. for VAR in AUD_E1 AUD_E2 AUD_E3 AUD_E4 AUD_E5; do if [ -z "${!VAR}" ]; then echo "FATAL: $VAR empty — audit chain construction failed" fail=$((fail+1)) fi done # Verify chain via second helper script (file-based). AUD_V_HELPER="/tmp/dth_mpq_audit_verify_helper.py" cat > "$AUD_V_HELPER" <<'PYEOF' import json, os, sys entries = [] for v in ["AUD_E1","AUD_E2","AUD_E3","AUD_E4","AUD_E5"]: s = os.environ.get(v, "") if not s: sys.exit(f"missing env var {v}") entries.append(json.loads(s)) print(json.dumps({"operator":"compliance-audit","input":{"verify":True,"entries":entries}})) PYEOF AUD_V_REQ_FILE="/tmp/dth_mpq_audit_verify.json" AUD_E1="$AUD_E1" AUD_E2="$AUD_E2" AUD_E3="$AUD_E3" AUD_E4="$AUD_E4" AUD_E5="$AUD_E5" \ python3 "$AUD_V_HELPER" > "$AUD_V_REQ_FILE" AUD_V_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ --data-binary "@$AUD_V_REQ_FILE" "$RUNS_URL")" AUD_V_ID="$(json_get "['data']['run_id']" "$AUD_V_BODY")" AUD_V_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$AUD_V_ID")" AUD_V_VALID="$(json_get "['data']['output_json']['audit']['chainValid']" "$AUD_V_GET")" AUD_V_N="$(json_get "['data']['output_json']['audit']['entries_verified']" "$AUD_V_GET")" assert "C9_audit_chainValid" "[ '$AUD_V_VALID' = 'True' ]" "chainValid=$AUD_V_VALID; body=$AUD_V_GET" assert "C9_audit_entries_5" "[ '$AUD_V_N' = '5' ]" "entries_verified=$AUD_V_N (expected 5)" # prev_hash linkage: E2.prev_hash == E1.hash LINK_OK="$(AUD_E1="$AUD_E1" AUD_E2="$AUD_E2" python3 -c " import json, os e1 = json.loads(os.environ['AUD_E1']) e2 = json.loads(os.environ['AUD_E2']) print('OK' if e2.get('prev_hash') == e1.get('hash') else 'BROKEN', e1.get('hash')[:16], '->', e2.get('prev_hash','')[:16]) ")" LINK_STATUS="$(echo "$LINK_OK" | awk '{print $1}')" assert "C9_audit_prev_hash_linked" "[ '$LINK_STATUS' = 'OK' ]" "E1-E2 link: $LINK_OK" # No PHI in audit entries (no patient IDs p01-p09, no diagnosis D-101, no hospitalization days 7/10/5) # Concatenate all 5 entries via env-var prefix pattern (python's os.environ). PHI_CHAIN="$AUD_E1$AUD_E2$AUD_E3$AUD_E4$AUD_E5" PHI_LEAK="$(PHI_CHAIN="$PHI_CHAIN" python3 -c " import os all_entries = os.environ['PHI_CHAIN'] forbidden=['p01','p02','p03','p04','p05','p06','p07','p08','p09','D-101','\"7\"','\"10\"','\"5\"'] hits=[f for f in forbidden if f in all_entries] print('LEAKED' if hits else 'OK', '|'.join(hits)) ")" PHI_LEAK_STATUS="$(echo "$PHI_LEAK" | awk '{print $1}')" assert "C9_audit_no_phi_in_entries" "[ '$PHI_LEAK_STATUS' = 'OK' ]" "PHI leak: $PHI_LEAK" # ============================================================ # C10 (S10): E2E 端到端 — pass=N fail=0 + wall-clock < 60s + no PHI in API # ============================================================ T_END=$(date +%s) T_ELAPSED=$((T_END - T_START)) assert "C10_e2e_full_flow_pass" "[ $fail -eq 0 ]" "downstream case failed before reaching C10 (current fail=$fail)" assert "C10_e2e_wallclock_under_60s" "[ $T_ELAPSED -lt 60 ]" "wall-clock=${T_ELAPSED}s (must < 60s)" # 联合断言 ≥ 25 sub-assertion (10 case × 子断言;预期 ~39) assert "C10_e2e_subassertion_ge_25" "[ $pass -ge 25 ]" "sub-assertion count = $pass (must ≥ 25)" echo "---" echo "e2e dth_mpq: pass=$pass fail=$fail elapsed=${T_ELAPSED}s" if [ $fail -gt 0 ]; then printf 'FAILED:\n'; printf ' - %s\n' "${fails[@]}" exit 1 fi exit 0