Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 45 additions & 5 deletions .agent/specs/SPEC-P5_TENANT_GUARDRAILS.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,27 @@ nothing about VPC CNI enforcement or IRSA.
eks mode allows no skip. Probe Pods: `window.sh run_probe` adds the tenant
label in tenant namespaces and the app requests and memory limit.
Runbook `docs/runbooks/P5_ISOLATION_SUITE.md`.)
(24/11, after Codex review: admission records the policy that denied
(`deny:kyverno:<policy>`) and treats an unreachable webhook as an error;
`can-i` answers other than yes/no are errors; workload ServiceAccount
cases; T03 adds a foreign kind; network and T07 probes are Jobs; the P4
wrapper needs exit 0 and the full case set of each probe, `platform`
included; evidence is scrubbed; an exit trap removes and checks every
suite object. After Codex re-review: probe results count only from a
`Complete` Job; enforced accepts only a fully passing control receipt of
`RUN_ID-control` for the same sandbox; RBAC by SubjectAccessReview
(`evaluationError` is an error); failed lists are errors, not "none";
cleanup is a recorded case before RESULT. Third Codex pass: the log
read must succeed; cleanup also deletes and waits for the `window.sh`
probe Pods (`zdr-probe`); account IDs side by side are all scrubbed;
egress is keyed by the policy podSelector with integer ports.
Codex APPROVE at ae20196; its two should-fix items done: a policy
without policyTypes counts its egress (Kubernetes default), and
cleanup touches only this run's probes (`PROBE_RUN` label set by
`window.sh`).
Kind with Argo CD: 39 cases,
33 PASS, 0 FAIL, 6 `SKIPPED_NEEDS_EKS`; one policy in `Audit` still
exits 1.)

### P5-T4.2 — L3 window on EKS (4 h)

Expand All @@ -409,18 +430,37 @@ pre-inventory, the owner's explicit approval.

**B. Build (1 h)** — bootstrap with the RAG platform and NetworkPolicy enabled;
platform through Argo CD; both sandboxes Ready **before** any guardrail.
Pre-flight (Codex review 24/11): the planned `vpc_cidr` and DB subnets equal
the NetworkPolicy `ipBlock` values (`gitops/check.sh` checks this against
`infra/terraform/variables.tf`; recheck if the plan overrides `vpc_cidr`).

**C. Control, then guardrails (1 h)**

- [ ] Control run: tenant-a reaches a listener in tenant-b (expected to succeed).
- [ ] Apply baseline and NetworkPolicies; Kyverno in `Audit`; reports clean;
switch to `Enforce`.
The apps `25-tenants` and `47-kyverno-policies` use automated sync with
`selfHeal`: a NetworkPolicy deleted by hand comes back within a reconcile, and
a policy edited in the cluster is reverted. So every step that removes or
changes a guardrail first suspends automated sync on the root app and on that
app, and re-enables it afterwards. Audit to Deny is a Git commit, not a
cluster edit.

- [ ] Control run: suspend sync (root, `25-tenants`), delete the tenant
NetworkPolicies, `NET_PHASE=control`: the suite refuses to run if any
NetworkPolicy remains, and every path (own app, a→b, b→a, metadata) must
be open. Keep its JSON.
- [ ] Re-enable sync; NetworkPolicies back from Git; PolicyEndpoints present.
Kyverno in `Audit`; reports clean; commit `Deny`; Argo applies it.
- [ ] Delete and re-create both sandboxes; both Ready; independent check passes.

**D. Suite and failure case (1 h)**

- [ ] Full suite; observation limit 10 minutes per negative case, 900 s total.
- [ ] Kyverno outage case of §4 on EKS.
- [ ] Full suite, `NET_PHASE=enforced CONTROL=<control JSON>`: it checks the
control file, that every Git NetworkPolicy has a PolicyEndpoint, and runs
network and IAM probes as Jobs (the VPC CNI does not enforce NetworkPolicy
on a Pod without an owner). Only a timeout counts as a network deny;
`AccessDenied` is the only IAM deny. Observation limit 10 minutes per
negative case, 900 s total.
- [ ] Kyverno outage case of §4 on EKS, with sync of `45-kyverno` and
`47-kyverno-policies` suspended first (self-heal would scale Kyverno back).
- [ ] Owner-typed micro-lab: one forbidden request per layer, typed by hand.

**E. Teardown (0.5 h)** — requests and cleanup → policies → Kyverno → P4/P3
Expand Down
68 changes: 53 additions & 15 deletions docs/runbooks/P5_ISOLATION_SUITE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

> **TL;DR** · Để làm gì: một lệnh thử mọi lớp chặn (RBAC, Pod Security, Kyverno, quota, NetworkPolicy, IAM/DB)
> và ghi `acceptance.json` ·
> Trạng thái: Claude chạy trên kind 24/11 (Kyverno `Deny`): 24 PASS, 0 FAIL, 7 SKIPPED_NEEDS_EKS ·
> Trạng thái: Claude chạy trên kind 24/11 sau review Codex (Kyverno `Deny`, có Argo CD): 39 case, 33 PASS, 0 FAIL,
> 6 SKIPPED_NEEDS_EKS ·
> Liên quan: `tests/isolation/run.sh`, SPEC-P5 P5-T4.1, THREAT_MODEL §11 ·
> Mục lục: 0 Dựng kind · 1 Chạy · 2 Đọc kết quả · 3 Tự kiểm bộ test · 4 Dọn · 5 Trên EKS

Ví von: mỗi case là một **người thử cửa**. Họ ghi lại cửa nào chặn mình (`deny:pod-security`, `deny:kyverno`…),
không chỉ "bị chặn". Chặn đúng nhưng **sai cửa** cũng là FAIL: lớp mình tưởng đang canh thật ra không canh.

## 0. Dựng kind (không AWS, ~5 phút)
## 0. Dựng kind (không AWS, ~8 phút)

Giống mục 0 của `P5_L2_KIND_KYVERNO.md`, nhưng policy bật thẳng `Deny`:
Giống mục 0 của `P5_L2_KIND_KYVERNO.md`, nhưng policy bật thẳng `Deny`, thêm Argo CD cho hai case T03:

```bash
cd /r/_Projects/Eurus_Workspace/Zero-D-Rift
Expand All @@ -35,11 +36,17 @@ for f in gitops/platform/policies/*.yaml; do
done
# "Available" chưa chắc webhook đã nhận kết nối: thử lại tới khi Kyverno nhận policy
until $K apply -f "$LAB/policies/"; do sleep 5; done
curl -fsSL -o "$LAB/argocd-install-v3.5.7.yaml" https://raw.githubusercontent.com/argoproj/argo-cd/v3.5.7/manifests/install.yaml
echo "09bfbb9041b4a8503c6e4fa22cf41c403fa62be17526de353648a4551f5beb77 $LAB/argocd-install-v3.5.7.yaml" | sha256sum -c
$K create namespace argocd
$K apply -n argocd --server-side -f "$LAB/argocd-install-v3.5.7.yaml" > /dev/null
$K -n argocd wait --for=condition=Available deploy --all --timeout=300s
$K apply -f gitops/platform/apps/85-project-requests.yaml
sleep 30
```

**Phải thấy:** `OK`, ba Deployment `condition met`, ba `validatingpolicy … created` (có thể kèm một lần
`connection refused` trước đó, vòng `until` tự thử lại).
**Phải thấy:** hai dòng `OK` (sha256), các Deployment `condition met`, ba `validatingpolicy … created` (có thể kèm một lần
`connection refused` trước đó, vòng `until` tự thử lại), `appproject.argoproj.io/zdr-requests created`.

## 1. Chạy

Expand All @@ -49,7 +56,9 @@ MODE=kind CTX=kind-zdr-p5-iso OUT="$LAB/acceptance.json" bash tests/isolation/ru

**Dự đoán trước:** Pod `privileged` bị lớp nào chặn? Pod gắn nhãn `tenant-b` trong `tenant-a`? Pod xin 1Gi?

**Phải thấy:** `total 31, pass 24, fail 0, skipped 7`, `RESULT: PASS`, `exit=0`.
**Phải thấy:** `total 39, pass 33, fail 0, skipped 6`, dòng `PASS … cleanup … verified-empty`, `RESULT: PASS`, `exit=0`.
Không cài Argo CD thì hai case T03 thành SKIPPED: `total 39, pass 31, skipped 8`. Case `cleanup` chỉ PASS khi suite
**đọc được** danh sách và thấy trống; đọc lỗi thì là `unverified`, tính FAIL.

## 2. Đọc kết quả

Expand All @@ -58,8 +67,9 @@ jq -r '.cases[] | "\(.result)\t\(.threat)\t\(.id)\t\(.observed)"' "$LAB/acceptan
jq '.summary' "$LAB/acceptance.json"
```

Cột `observed` cho biết **lớp nào** trả lời: `deny:rbac`, `deny:pod-security`, `deny:kyverno`, `deny:quota`,
`deny:limitrange`. `SKIPPED_NEEDS_EKS` = kind không có VPC CNI, IRSA, RDS, Argo CD; **không bao giờ** tính là PASS.
Cột `observed` cho biết **lớp nào** trả lời: `deny:rbac`, `deny:pod-security`, `deny:kyverno:<tên policy>`, `deny:quota`,
`deny:limitrange`, `deny:argocd`. Bắt đầu bằng `error:` (webhook không gọi được, SubjectAccessReview báo `evaluationError`, mạng `refused`, Job không `Complete`…) nghĩa là
**không ai quyết định cả**, nên tính FAIL chứ không tính là bị chặn. `SKIPPED_NEEDS_EKS` = kind không có VPC CNI, IRSA, RDS, Argo CD; **không bao giờ** tính là PASS.
File không chứa tên context (trên EKS tên đó có account ID).

## 3. Tự kiểm bộ test: nó có biết FAIL không?
Expand All @@ -79,6 +89,10 @@ $K patch validatingpolicy zdr-image-digest --type merge -p '{"spec":{"validation
Bài học Claude gặp khi viết suite: `pod … | case_admit` chạy vế phải trong **subshell**, nên kết quả (kể cả FAIL)
bị mất, suite vẫn báo PASS với 18/30 case. Sửa bằng `shopt -s lastpipe`. Bước 3 này là để bắt đúng loại lỗi đó.

Điều Claude phát hiện khi thử wrapper Job (24/11): kindnet của kind **cũng** thực thi NetworkPolicy. Một Job trong tenant-a
gọi `kubernetes.default.svc:443` bị TIMEOUT, vì sau DNAT API server nằm ở cổng 6443, mà policy chỉ mở 443. Suite vẫn để
case mạng SKIPPED trên kind, vì thứ cần chứng minh là VPC CNI trên EKS (PolicyEndpoint, standard mode), không phải kindnet.

## 4. Dọn

```bash
Expand All @@ -90,17 +104,41 @@ kind get clusters

## 5. Trên EKS (window L3, P5-T4.2; cần phê duyệt riêng)

Chạy **hai pha**. Ví von: muốn chứng minh cửa khóa được thì trước hết phải thấy cửa **mở** khi chưa khóa.
Nếu không, "bị chặn" có thể chỉ là do đường mạng hỏng.

**Pha control** (chưa có NetworkPolicy): tạm dừng auto-sync của app root và `25-tenants`, xóa NetworkPolicy
của hai tenant, rồi chạy:

```bash
MODE=eks CTX="$(kubectl config current-context)" NET_PHASE=control RUN_ID=P5-L3-<ngày>-01-control OUT=control.json bash tests/isolation/run.sh
```

Suite chỉ chạy 4 đường mạng: own-app, a→b, b→a, metadata. Cả 4 phải `allow`. Nếu còn NetworkPolicy, hoặc không đọc được
danh sách NetworkPolicy, suite dừng trước khi thử mạng.

**Pha enforced**: bật lại sync. Argo trả NetworkPolicy từ Git. Kyverno `Audit` → báo cáo sạch → commit `Deny`.
Sau đó chạy:

```bash
MODE=eks CTX="$(kubectl config current-context)" RUN_ID=P5-L3-<ngày>-01 OUT=acceptance.json bash tests/isolation/run.sh
MODE=eks CTX="$(kubectl config current-context)" NET_PHASE=enforced CONTROL=control.json RUN_ID=P5-L3-<ngày>-01 OUT=acceptance.json bash tests/isolation/run.sh
```

- Không in `CTX` ra màn hình (có account ID). Suite từ chối chạy nếu `CTX` khác context hiện tại,
vì `window.sh probe` dùng context hiện tại.
- Ở `eks`, **không case nào được SKIPPED**; thêm các case mạng (Pod Python thật, chờ 20 s cho khoảng hở
standard mode), T07 (ServiceAccount `zdr-iso-other` ngoài mẫu `rag-*`, phải `AccessDenied`) và probe P4
(`sandbox` dương tính, `cross` âm tính cho cả hai tenant).
- Suite tự xoá Pod và ServiceAccount nó tạo. Kiểm sau khi chạy:
`kubectl get pods,sa -A -l app.kubernetes.io/name=zdr-isolation; kubectl -n tenant-a get sa zdr-iso-other`.
vì `window.sh probe` dùng context hiện tại. Mọi dòng ghi ra đều đã lọc tên context, ARN, account ID.
- Ở `eks`, **không case nào được SKIPPED**. Probe mạng và T07 chạy bằng **Job**, vì VPC CNI không áp
NetworkPolicy cho Pod không có owner. Mỗi Job chờ 20 s cho khoảng hở của standard mode.
- Mạng: chỉ `TIMEOUT` mới tính là NetworkPolicy chặn. `refused` hay lỗi DNS là FAIL.
- T07: chỉ `AccessDenied` mới tính là IAM chặn.
- Pha enforced chỉ nhận file control của **đúng** `RUN_ID-control`, cùng sandbox, và mọi case đều PASS (case `net-control-receipt`).
- Trước các case mạng, suite đợi mỗi NetworkPolicy trong Git có một PolicyEndpoint, tức node agent đã nạp.
- Kết quả của một Job chỉ được đọc khi Job đã `Complete`; Job thất bại thì là lỗi, kể cả khi log đã in `RESULT TIMEOUT`.
- Probe P4 (`platform`, `sandbox`, `cross` cho cả hai tenant) phải exit 0 và in **đủ** từng case. Thiếu case
hay có `SKIP` đều là FAIL.
- Suite tự xóa Job, Pod, ServiceAccount và Argo app nó tạo (nhãn `app.kubernetes.io/name=zdr-isolation`),
cùng Pod probe của `window.sh` **của chính lần chạy này** (nhãn `zdr-probe` và `rag.zero-drift.io/probe-run`,
do suite đặt qua biến `PROBE_RUN`), đợi chúng biến mất rồi kiểm lại. Probe của lần chạy khác không bị đụng tới.
Còn sót, hoặc không đọc được danh sách, thì exit 1.

## Bảng dự đoán (owner tự điền)

Expand Down
Loading