mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
phase42: add live lifecycle owner admission gate
This commit is contained in:
1 parent
fa2462165c
commit
4b31ec4dc7
5 files changed
+726
-9
No files matched your search
@@ -1,6 +1,6 @@
|
||||
# Current Plan: Phase 42 - Lifecycle Owner API / Admission Gate
|
||||
|
||||
Status: open, 0% complete. Started on 2026-06-14.
|
||||
Status: open, 14% complete. Started on 2026-06-14.
|
||||
|
||||
Branch: `phase41-lifecycle-owner-foundation`
|
||||
|
||||
@@ -65,18 +65,19 @@ Goal: choose and wire the real API proof.
|
||||
Acceptance:
|
||||
|
||||
```text
|
||||
[ ] choose one primary harness: envtest, live throwaway cluster, or equivalent
|
||||
[ ] load real SwBlockVolume CRD schema
|
||||
[ ] install lifecycle-owner RBAC separate from operator-status
|
||||
[ ] install admission policy/webhook or equivalent finalizer-only enforcement
|
||||
[ ] prove operator-status RBAC remains status/events-only
|
||||
[x] choose one primary harness: live Kubernetes ValidatingAdmissionPolicy gate
|
||||
[x] load real SwBlockVolume CRD schema
|
||||
[x] install lifecycle-owner RBAC separate from operator-status
|
||||
[x] install admission policy/webhook or equivalent finalizer-only enforcement
|
||||
[x] prove operator-status RBAC remains status/events-only
|
||||
```
|
||||
|
||||
Verification:
|
||||
|
||||
```text
|
||||
documented harness command
|
||||
negative test proving mock-only is not the source of truth
|
||||
scripts/run-phase42-lifecycle-owner-admission-gate.sh
|
||||
scripts/run-phase42-lifecycle-owner-admission-gate.ps1
|
||||
testops/scenarios/lifecycle-owner-admission-gate-chain.yaml
|
||||
```
|
||||
|
||||
## D2: Allowed Finalizer Patch
|
||||
@@ -206,6 +207,14 @@ Phase 42 can close only if:
|
||||
- 0%: Phase 42 opened from the Phase 41 lifecycle-owner foundation. The
|
||||
planning contract is drafted in
|
||||
`internal/docs/ref/phase42-lifecycle-owner-api-admission-gate.md`.
|
||||
- 14%: D1 dev-complete. Chose a live Kubernetes
|
||||
`ValidatingAdmissionPolicy` harness instead of mock-only tests. Added
|
||||
PowerShell/Bash runners and a TestOps scenario that apply the real
|
||||
`SwBlockVolume` CRD, create separate operator-status and lifecycle-owner
|
||||
identities, install finalizer-only admission, and assert operator-status
|
||||
remains status/events-only. Local Rancher Desktop smoke fails closed with
|
||||
`blocked_reason=validating_admission_policy_unavailable`; QA must run this on
|
||||
a cluster with `ValidatingAdmissionPolicy` support.
|
||||
|
||||
## Prerequisites / Risks
|
||||
|
||||
@@ -220,4 +229,6 @@ Phase 42 can close only if:
|
||||
## Next Step
|
||||
|
||||
Implement D1: choose the harness shape, wire the real CRD/RBAC/admission test
|
||||
surface, and prove the existing status-only observer remains unchanged.
|
||||
surface, and prove the existing status-only observer remains unchanged. QA
|
||||
assignment:
|
||||
`internal/docs/qa-assignments/phase42-d1-lifecycle-owner-admission-gate-qa.md`.
|
||||
+144
@@ -0,0 +1,144 @@
|
||||
# QA Assignment - Phase 42 D1 Lifecycle Owner Admission Gate
|
||||
|
||||
## Goal
|
||||
|
||||
Validate the first real Kubernetes API/admission gate for the future
|
||||
lifecycle-owner. This gate proves the intended boundary before any product
|
||||
controller is allowed to add or remove `SwBlockVolume` finalizers.
|
||||
|
||||
Expected result:
|
||||
|
||||
```text
|
||||
operator-status remains status/events-only;
|
||||
lifecycle-owner can add/remove only the Seaweed Block finalizer;
|
||||
spec, unrelated metadata, foreign finalizers, fake /finalizers, and workload or
|
||||
storage resource mutations are rejected by real Kubernetes API/admission.
|
||||
```
|
||||
|
||||
## Source Under Test
|
||||
|
||||
Branch:
|
||||
|
||||
```text
|
||||
phase41-lifecycle-owner-foundation
|
||||
```
|
||||
|
||||
Relevant files:
|
||||
|
||||
```text
|
||||
scripts/run-phase42-lifecycle-owner-admission-gate.sh
|
||||
scripts/run-phase42-lifecycle-owner-admission-gate.ps1
|
||||
testops/scenarios/lifecycle-owner-admission-gate-chain.yaml
|
||||
internal/docs/current-plan.md
|
||||
internal/docs/ref/phase42-lifecycle-owner-api-admission-gate.md
|
||||
```
|
||||
|
||||
## Environment Requirement
|
||||
|
||||
This is a live API/admission gate. It requires a Kubernetes API server with
|
||||
`admissionregistration.k8s.io/v1` `ValidatingAdmissionPolicy` support.
|
||||
|
||||
If the target cluster does not support `ValidatingAdmissionPolicy`, record:
|
||||
|
||||
```text
|
||||
phase42_lifecycle_owner_admission_status=blocked
|
||||
blocked_reason=validating_admission_policy_unavailable
|
||||
```
|
||||
|
||||
That is an environment blocker, not a product PASS.
|
||||
|
||||
## G1 - Run The Gate
|
||||
|
||||
Run either:
|
||||
|
||||
```powershell
|
||||
powershell -NoProfile -ExecutionPolicy Bypass `
|
||||
-File scripts/run-phase42-lifecycle-owner-admission-gate.ps1 `
|
||||
-ProductRoot C:\work\seaweed_block `
|
||||
-ArtifactDir C:\work\seaweed_block\results\phase42-lifecycle-owner-admission-qa
|
||||
```
|
||||
|
||||
or:
|
||||
|
||||
```bash
|
||||
SW_BLOCK_ARTIFACT_DIR=/mnt/smb/work/share/g15d-k8s/phase42-lifecycle-owner-admission-qa \
|
||||
bash scripts/run-phase42-lifecycle-owner-admission-gate.sh "$PWD"
|
||||
```
|
||||
|
||||
or the TestOps scenario:
|
||||
|
||||
```bash
|
||||
swblock run testops/scenarios/lifecycle-owner-admission-gate-chain.yaml
|
||||
```
|
||||
|
||||
Pass criteria in
|
||||
`phase42-lifecycle-owner-admission-gate-summary.txt`:
|
||||
|
||||
```text
|
||||
phase42_lifecycle_owner_admission_status=ok
|
||||
harness=live_kubernetes_validating_admission_policy
|
||||
operator_status_main_patch_allowed=false
|
||||
lifecycle_owner_finalizer_add_allowed=true
|
||||
lifecycle_owner_finalizer_remove_allowed=true
|
||||
lifecycle_owner_spec_patch_allowed=false
|
||||
lifecycle_owner_label_patch_allowed=false
|
||||
lifecycle_owner_foreign_finalizer_allowed=false
|
||||
lifecycle_owner_mixed_patch_allowed=false
|
||||
finalizers_endpoint_allowed=false
|
||||
```
|
||||
|
||||
## G2 - Forbidden Resource Mutations
|
||||
|
||||
The summary must include `false` for all lifecycle-owner patch checks:
|
||||
|
||||
```text
|
||||
lifecycle_owner_pods_patch_allowed=false
|
||||
lifecycle_owner_deployments_patch_allowed=false
|
||||
lifecycle_owner_persistentvolumeclaims_patch_allowed=false
|
||||
lifecycle_owner_persistentvolumes_patch_allowed=false
|
||||
lifecycle_owner_storageclasses_patch_allowed=false
|
||||
lifecycle_owner_secrets_patch_allowed=false
|
||||
lifecycle_owner_nodes_patch_allowed=false
|
||||
lifecycle_owner_csidrivers_patch_allowed=false
|
||||
lifecycle_owner_csinodes_patch_allowed=false
|
||||
```
|
||||
|
||||
Fail if any forbidden resource mutation is allowed.
|
||||
|
||||
## G3 - Object Integrity
|
||||
|
||||
Inspect `final-object.stdout.txt`.
|
||||
|
||||
Pass criteria:
|
||||
|
||||
```text
|
||||
spec.pvcName remains phase42-a
|
||||
metadata.labels.keep remains true
|
||||
metadata.annotations.keep remains true
|
||||
no foreign finalizer remains
|
||||
```
|
||||
|
||||
Fail if an allowed finalizer patch changes spec, labels, annotations, or any
|
||||
unrelated metadata.
|
||||
|
||||
## G4 - Cleanup
|
||||
|
||||
The script must delete its namespace, RBAC, and admission objects on exit.
|
||||
|
||||
Pass criteria:
|
||||
|
||||
```text
|
||||
kubectl get ns sw-block-phase42-gate
|
||||
```
|
||||
|
||||
returns NotFound after the run, and no `sw-block-phase42-*` ClusterRole,
|
||||
ClusterRoleBinding, ValidatingAdmissionPolicy, or
|
||||
ValidatingAdmissionPolicyBinding remains.
|
||||
|
||||
## Verdict
|
||||
|
||||
PASS only if G1-G4 pass on a real Kubernetes API/admission surface.
|
||||
|
||||
Do not mark Phase 42 D1 passed using the old Phase 41 schema-aware fake server.
|
||||
That fake server was sufficient for Phase 41's non-mutating slice, but Phase 42
|
||||
is specifically about the real admission boundary.
|
||||
@@ -0,0 +1,251 @@
|
||||
param(
|
||||
[string]$ProductRoot = (Get-Location).Path,
|
||||
[string]$ArtifactDir = "",
|
||||
[string]$Namespace = "sw-block-phase42-gate",
|
||||
[string]$Kubectl = "kubectl"
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
if ([string]::IsNullOrWhiteSpace($ArtifactDir)) {
|
||||
$ArtifactDir = Join-Path $ProductRoot "results\phase42-lifecycle-owner-admission-gate"
|
||||
}
|
||||
$Finalizer = "block.seaweedfs.com/swblockvolume-protection"
|
||||
New-Item -ItemType Directory -Force -Path $ArtifactDir | Out-Null
|
||||
$Summary = Join-Path $ArtifactDir "phase42-lifecycle-owner-admission-gate-summary.txt"
|
||||
Set-Content -Path $Summary -Value ""
|
||||
|
||||
function Add-Summary([string]$Line) {
|
||||
Add-Content -Path $Summary -Value $Line
|
||||
}
|
||||
|
||||
function Invoke-Capture {
|
||||
param([string]$Name, [string[]]$Args)
|
||||
$stdout = Join-Path $ArtifactDir "$Name.stdout.txt"
|
||||
$stderr = Join-Path $ArtifactDir "$Name.stderr.txt"
|
||||
& $Kubectl @Args >$stdout 2>$stderr
|
||||
return $LASTEXITCODE
|
||||
}
|
||||
|
||||
function Expect-Success {
|
||||
param([string]$Name, [string[]]$Args)
|
||||
$code = Invoke-Capture -Name $Name -Args $Args
|
||||
if ($code -ne 0) {
|
||||
Get-Content (Join-Path $ArtifactDir "$Name.stderr.txt") -ErrorAction SilentlyContinue | Write-Error
|
||||
throw "expected success for $Name"
|
||||
}
|
||||
}
|
||||
|
||||
function Expect-Failure {
|
||||
param([string]$Name, [string[]]$Args)
|
||||
$code = Invoke-Capture -Name $Name -Args $Args
|
||||
if ($code -eq 0) {
|
||||
Get-Content (Join-Path $ArtifactDir "$Name.stdout.txt") -ErrorAction SilentlyContinue | Write-Error
|
||||
throw "expected failure for $Name"
|
||||
}
|
||||
}
|
||||
|
||||
function Cleanup {
|
||||
try { & $Kubectl delete validatingadmissionpolicybinding sw-block-phase42-finalizer-only --ignore-not-found >$null 2>$null } catch {}
|
||||
try { & $Kubectl delete validatingadmissionpolicy sw-block-phase42-finalizer-only --ignore-not-found >$null 2>$null } catch {}
|
||||
try { & $Kubectl delete clusterrolebinding sw-block-phase42-operator-status sw-block-phase42-lifecycle-owner --ignore-not-found >$null 2>$null } catch {}
|
||||
try { & $Kubectl delete clusterrole sw-block-phase42-operator-status sw-block-phase42-lifecycle-owner --ignore-not-found >$null 2>$null } catch {}
|
||||
try { & $Kubectl delete namespace $Namespace --ignore-not-found >$null 2>$null } catch {}
|
||||
}
|
||||
|
||||
try {
|
||||
Add-Summary "phase42_lifecycle_owner_admission_status=running"
|
||||
Add-Summary "harness=live_kubernetes_validating_admission_policy"
|
||||
|
||||
if ((Invoke-Capture -Name "kubectl-api-versions" -Args @("api-versions")) -ne 0) {
|
||||
Add-Summary "phase42_lifecycle_owner_admission_status=blocked"
|
||||
Add-Summary "blocked_reason=kubernetes_api_unreachable"
|
||||
exit 2
|
||||
}
|
||||
if ((Invoke-Capture -Name "api-resources-admissionregistration" -Args @("api-resources", "--api-group=admissionregistration.k8s.io")) -ne 0) {
|
||||
Add-Summary "phase42_lifecycle_owner_admission_status=blocked"
|
||||
Add-Summary "blocked_reason=kubernetes_api_unreachable"
|
||||
exit 2
|
||||
}
|
||||
$resources = Get-Content (Join-Path $ArtifactDir "api-resources-admissionregistration.stdout.txt")
|
||||
if (-not ($resources -match "^validatingadmissionpolicies\s")) {
|
||||
Add-Summary "phase42_lifecycle_owner_admission_status=blocked"
|
||||
Add-Summary "blocked_reason=validating_admission_policy_unavailable"
|
||||
exit 2
|
||||
}
|
||||
|
||||
Expect-Success "apply-crd" @("apply", "-f", (Join-Path $ProductRoot "charts\seaweed-block\crds\swblockvolumes.block.seaweedfs.com.yaml"))
|
||||
|
||||
@"
|
||||
apiVersion: v1
|
||||
kind: Namespace
|
||||
metadata:
|
||||
name: $Namespace
|
||||
labels:
|
||||
block.seaweedfs.com/phase42-gate: "true"
|
||||
"@ | Set-Content -Path (Join-Path $ArtifactDir "namespace.yaml")
|
||||
Expect-Success "apply-namespace" @("apply", "-f", (Join-Path $ArtifactDir "namespace.yaml"))
|
||||
|
||||
@"
|
||||
apiVersion: v1
|
||||
kind: ServiceAccount
|
||||
metadata:
|
||||
name: sw-block-operator-status
|
||||
namespace: $Namespace
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: ServiceAccount
|
||||
metadata:
|
||||
name: sw-block-lifecycle-owner
|
||||
namespace: $Namespace
|
||||
---
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: ClusterRole
|
||||
metadata:
|
||||
name: sw-block-phase42-operator-status
|
||||
rules:
|
||||
- apiGroups: ["block.seaweedfs.com"]
|
||||
resources: ["swblockvolumes"]
|
||||
verbs: ["get", "list", "watch"]
|
||||
- apiGroups: ["block.seaweedfs.com"]
|
||||
resources: ["swblockvolumes/status"]
|
||||
verbs: ["get", "update", "patch"]
|
||||
- apiGroups: [""]
|
||||
resources: ["events"]
|
||||
verbs: ["create"]
|
||||
---
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: ClusterRole
|
||||
metadata:
|
||||
name: sw-block-phase42-lifecycle-owner
|
||||
rules:
|
||||
- apiGroups: ["block.seaweedfs.com"]
|
||||
resources: ["swblockvolumes"]
|
||||
verbs: ["get", "list", "watch", "patch"]
|
||||
- apiGroups: [""]
|
||||
resources: ["events"]
|
||||
verbs: ["create"]
|
||||
---
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: ClusterRoleBinding
|
||||
metadata:
|
||||
name: sw-block-phase42-operator-status
|
||||
subjects:
|
||||
- kind: ServiceAccount
|
||||
name: sw-block-operator-status
|
||||
namespace: $Namespace
|
||||
roleRef:
|
||||
apiGroup: rbac.authorization.k8s.io
|
||||
kind: ClusterRole
|
||||
name: sw-block-phase42-operator-status
|
||||
---
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: ClusterRoleBinding
|
||||
metadata:
|
||||
name: sw-block-phase42-lifecycle-owner
|
||||
subjects:
|
||||
- kind: ServiceAccount
|
||||
name: sw-block-lifecycle-owner
|
||||
namespace: $Namespace
|
||||
roleRef:
|
||||
apiGroup: rbac.authorization.k8s.io
|
||||
kind: ClusterRole
|
||||
name: sw-block-phase42-lifecycle-owner
|
||||
"@ | Set-Content -Path (Join-Path $ArtifactDir "rbac.yaml")
|
||||
Expect-Success "apply-rbac" @("apply", "-f", (Join-Path $ArtifactDir "rbac.yaml"))
|
||||
|
||||
@"
|
||||
apiVersion: admissionregistration.k8s.io/v1
|
||||
kind: ValidatingAdmissionPolicy
|
||||
metadata:
|
||||
name: sw-block-phase42-finalizer-only
|
||||
spec:
|
||||
failurePolicy: Fail
|
||||
matchConstraints:
|
||||
resourceRules:
|
||||
- apiGroups: ["block.seaweedfs.com"]
|
||||
apiVersions: ["v1alpha1"]
|
||||
operations: ["UPDATE"]
|
||||
resources: ["swblockvolumes"]
|
||||
validations:
|
||||
- expression: >-
|
||||
request.userInfo.username != 'system:serviceaccount:${Namespace}:sw-block-lifecycle-owner' ||
|
||||
(
|
||||
object.spec == oldObject.spec &&
|
||||
object.status == oldObject.status &&
|
||||
object.metadata.labels == oldObject.metadata.labels &&
|
||||
object.metadata.annotations == oldObject.metadata.annotations &&
|
||||
object.metadata.ownerReferences == oldObject.metadata.ownerReferences &&
|
||||
has(object.metadata.finalizers) &&
|
||||
size(object.metadata.finalizers) <= 1 &&
|
||||
object.metadata.finalizers.all(f, f == '${Finalizer}') &&
|
||||
(!has(oldObject.metadata.finalizers) ||
|
||||
(size(oldObject.metadata.finalizers) <= 1 &&
|
||||
oldObject.metadata.finalizers.all(f, f == '${Finalizer}')))
|
||||
)
|
||||
message: lifecycle-owner may patch only the Seaweed Block finalizer
|
||||
---
|
||||
apiVersion: admissionregistration.k8s.io/v1
|
||||
kind: ValidatingAdmissionPolicyBinding
|
||||
metadata:
|
||||
name: sw-block-phase42-finalizer-only
|
||||
spec:
|
||||
policyName: sw-block-phase42-finalizer-only
|
||||
validationActions: [Deny]
|
||||
matchResources:
|
||||
namespaceSelector:
|
||||
matchLabels:
|
||||
block.seaweedfs.com/phase42-gate: "true"
|
||||
"@ | Set-Content -Path (Join-Path $ArtifactDir "admission.yaml")
|
||||
Expect-Success "apply-admission" @("apply", "-f", (Join-Path $ArtifactDir "admission.yaml"))
|
||||
|
||||
@"
|
||||
apiVersion: block.seaweedfs.com/v1alpha1
|
||||
kind: SwBlockVolume
|
||||
metadata:
|
||||
name: phase42-a
|
||||
namespace: $Namespace
|
||||
labels:
|
||||
keep: "true"
|
||||
annotations:
|
||||
keep: "true"
|
||||
spec:
|
||||
pvcName: phase42-a
|
||||
storageClass: sw-block
|
||||
"@ | Set-Content -Path (Join-Path $ArtifactDir "volume.yaml")
|
||||
Expect-Success "apply-volume" @("apply", "-f", (Join-Path $ArtifactDir "volume.yaml"))
|
||||
|
||||
$operatorAs = "system:serviceaccount:${Namespace}:sw-block-operator-status"
|
||||
$ownerAs = "system:serviceaccount:${Namespace}:sw-block-lifecycle-owner"
|
||||
|
||||
Expect-Failure "operator-status-main-patch" @("patch", "swblockvolume", "phase42-a", "-n", $Namespace, "--as", $operatorAs, "--type=merge", "-p", "{`"metadata`":{`"finalizers`":[`"$Finalizer`"]}}")
|
||||
Add-Summary "operator_status_main_patch_allowed=false"
|
||||
Expect-Success "lifecycle-owner-add-finalizer" @("patch", "swblockvolume", "phase42-a", "-n", $Namespace, "--as", $ownerAs, "--type=merge", "-p", "{`"metadata`":{`"finalizers`":[`"$Finalizer`"]}}")
|
||||
Add-Summary "lifecycle_owner_finalizer_add_allowed=true"
|
||||
Expect-Success "lifecycle-owner-remove-finalizer" @("patch", "swblockvolume", "phase42-a", "-n", $Namespace, "--as", $ownerAs, "--type=merge", "-p", '{"metadata":{"finalizers":[]}}')
|
||||
Add-Summary "lifecycle_owner_finalizer_remove_allowed=true"
|
||||
|
||||
Expect-Failure "lifecycle-owner-spec-patch" @("patch", "swblockvolume", "phase42-a", "-n", $Namespace, "--as", $ownerAs, "--type=merge", "-p", '{"spec":{"pvcName":"changed"}}')
|
||||
Add-Summary "lifecycle_owner_spec_patch_allowed=false"
|
||||
Expect-Failure "lifecycle-owner-label-patch" @("patch", "swblockvolume", "phase42-a", "-n", $Namespace, "--as", $ownerAs, "--type=merge", "-p", '{"metadata":{"labels":{"changed":"true"}}}')
|
||||
Add-Summary "lifecycle_owner_label_patch_allowed=false"
|
||||
Expect-Failure "lifecycle-owner-foreign-finalizer" @("patch", "swblockvolume", "phase42-a", "-n", $Namespace, "--as", $ownerAs, "--type=merge", "-p", '{"metadata":{"finalizers":["example.com/foreign"]}}')
|
||||
Add-Summary "lifecycle_owner_foreign_finalizer_allowed=false"
|
||||
Expect-Failure "lifecycle-owner-mixed-patch" @("patch", "swblockvolume", "phase42-a", "-n", $Namespace, "--as", $ownerAs, "--type=merge", "-p", "{`"metadata`":{`"finalizers`":[`"$Finalizer`"]},`"spec`":{`"pvcName`":`"changed`"}}")
|
||||
Add-Summary "lifecycle_owner_mixed_patch_allowed=false"
|
||||
|
||||
$code = Invoke-Capture -Name "finalizers-endpoint" -Args @("patch", "swblockvolume", "phase42-a", "-n", $Namespace, "--as", $ownerAs, "--subresource=finalizers", "--type=merge", "-p", "{`"metadata`":{`"finalizers`":[`"$Finalizer`"]}}")
|
||||
if ($code -eq 0) { throw "unexpected /finalizers subresource success" }
|
||||
Add-Summary "finalizers_endpoint_allowed=false"
|
||||
|
||||
foreach ($resource in @("pods", "deployments", "persistentvolumeclaims", "persistentvolumes", "storageclasses", "secrets", "nodes", "csidrivers", "csinodes")) {
|
||||
$code = Invoke-Capture -Name "can-i-patch-$resource" -Args @("auth", "can-i", "patch", $resource, "--as", $ownerAs, "-n", $Namespace)
|
||||
if ($code -eq 0) { throw "unexpected patch permission for $resource" }
|
||||
Add-Summary "lifecycle_owner_${resource}_patch_allowed=false"
|
||||
}
|
||||
|
||||
Expect-Success "final-object" @("get", "swblockvolume", "phase42-a", "-n", $Namespace, "-o", "yaml")
|
||||
Add-Summary "phase42_lifecycle_owner_admission_status=ok"
|
||||
}
|
||||
finally {
|
||||
Cleanup
|
||||
}
|
||||
@@ -0,0 +1,254 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
PRODUCT_ROOT="${1:-$(pwd)}"
|
||||
ARTIFACT_DIR="${SW_BLOCK_ARTIFACT_DIR:-${PRODUCT_ROOT}/results/phase42-lifecycle-owner-admission-gate}"
|
||||
KUBECTL="${KUBECTL:-kubectl}"
|
||||
NAMESPACE="${SW_BLOCK_PHASE42_NAMESPACE:-sw-block-phase42-gate}"
|
||||
FINALIZER="block.seaweedfs.com/swblockvolume-protection"
|
||||
SUMMARY="${ARTIFACT_DIR}/phase42-lifecycle-owner-admission-gate-summary.txt"
|
||||
|
||||
mkdir -p "${ARTIFACT_DIR}"
|
||||
: >"${SUMMARY}"
|
||||
|
||||
write_summary() {
|
||||
echo "$*" | tee -a "${SUMMARY}" >/dev/null
|
||||
}
|
||||
|
||||
run_capture() {
|
||||
local name="$1"
|
||||
shift
|
||||
"$@" >"${ARTIFACT_DIR}/${name}.stdout.txt" 2>"${ARTIFACT_DIR}/${name}.stderr.txt"
|
||||
}
|
||||
|
||||
run_expect_success() {
|
||||
local name="$1"
|
||||
shift
|
||||
if run_capture "$name" "$@"; then
|
||||
return 0
|
||||
fi
|
||||
echo "expected success for ${name}" >&2
|
||||
cat "${ARTIFACT_DIR}/${name}.stderr.txt" >&2 || true
|
||||
return 1
|
||||
}
|
||||
|
||||
run_expect_failure() {
|
||||
local name="$1"
|
||||
shift
|
||||
if run_capture "$name" "$@"; then
|
||||
echo "expected failure for ${name}" >&2
|
||||
cat "${ARTIFACT_DIR}/${name}.stdout.txt" >&2 || true
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
cleanup() {
|
||||
set +e
|
||||
"${KUBECTL}" delete validatingadmissionpolicybinding sw-block-phase42-finalizer-only --ignore-not-found >/dev/null 2>&1
|
||||
"${KUBECTL}" delete validatingadmissionpolicy sw-block-phase42-finalizer-only --ignore-not-found >/dev/null 2>&1
|
||||
"${KUBECTL}" delete clusterrolebinding sw-block-phase42-operator-status sw-block-phase42-lifecycle-owner --ignore-not-found >/dev/null 2>&1
|
||||
"${KUBECTL}" delete clusterrole sw-block-phase42-operator-status sw-block-phase42-lifecycle-owner --ignore-not-found >/dev/null 2>&1
|
||||
"${KUBECTL}" delete namespace "${NAMESPACE}" --ignore-not-found >/dev/null 2>&1
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
write_summary "phase42_lifecycle_owner_admission_status=running"
|
||||
write_summary "harness=live_kubernetes_validating_admission_policy"
|
||||
|
||||
if ! run_capture kubectl-api-versions "${KUBECTL}" api-versions; then
|
||||
write_summary "phase42_lifecycle_owner_admission_status=blocked"
|
||||
write_summary "blocked_reason=kubernetes_api_unreachable"
|
||||
exit 2
|
||||
fi
|
||||
|
||||
if ! "${KUBECTL}" api-resources --api-group=admissionregistration.k8s.io 2>/dev/null | grep -q '^validatingadmissionpolicies'; then
|
||||
write_summary "phase42_lifecycle_owner_admission_status=blocked"
|
||||
write_summary "blocked_reason=validating_admission_policy_unavailable"
|
||||
exit 2
|
||||
fi
|
||||
|
||||
run_expect_success apply-crd "${KUBECTL}" apply -f "${PRODUCT_ROOT}/charts/seaweed-block/crds/swblockvolumes.block.seaweedfs.com.yaml"
|
||||
|
||||
cat >"${ARTIFACT_DIR}/namespace.yaml" <<YAML
|
||||
apiVersion: v1
|
||||
kind: Namespace
|
||||
metadata:
|
||||
name: ${NAMESPACE}
|
||||
labels:
|
||||
block.seaweedfs.com/phase42-gate: "true"
|
||||
YAML
|
||||
run_expect_success apply-namespace "${KUBECTL}" apply -f "${ARTIFACT_DIR}/namespace.yaml"
|
||||
|
||||
cat >"${ARTIFACT_DIR}/rbac.yaml" <<YAML
|
||||
apiVersion: v1
|
||||
kind: ServiceAccount
|
||||
metadata:
|
||||
name: sw-block-operator-status
|
||||
namespace: ${NAMESPACE}
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: ServiceAccount
|
||||
metadata:
|
||||
name: sw-block-lifecycle-owner
|
||||
namespace: ${NAMESPACE}
|
||||
---
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: ClusterRole
|
||||
metadata:
|
||||
name: sw-block-phase42-operator-status
|
||||
rules:
|
||||
- apiGroups: ["block.seaweedfs.com"]
|
||||
resources: ["swblockvolumes"]
|
||||
verbs: ["get", "list", "watch"]
|
||||
- apiGroups: ["block.seaweedfs.com"]
|
||||
resources: ["swblockvolumes/status"]
|
||||
verbs: ["get", "update", "patch"]
|
||||
- apiGroups: [""]
|
||||
resources: ["events"]
|
||||
verbs: ["create"]
|
||||
---
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: ClusterRole
|
||||
metadata:
|
||||
name: sw-block-phase42-lifecycle-owner
|
||||
rules:
|
||||
- apiGroups: ["block.seaweedfs.com"]
|
||||
resources: ["swblockvolumes"]
|
||||
verbs: ["get", "list", "watch", "patch"]
|
||||
- apiGroups: [""]
|
||||
resources: ["events"]
|
||||
verbs: ["create"]
|
||||
---
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: ClusterRoleBinding
|
||||
metadata:
|
||||
name: sw-block-phase42-operator-status
|
||||
subjects:
|
||||
- kind: ServiceAccount
|
||||
name: sw-block-operator-status
|
||||
namespace: ${NAMESPACE}
|
||||
roleRef:
|
||||
apiGroup: rbac.authorization.k8s.io
|
||||
kind: ClusterRole
|
||||
name: sw-block-phase42-operator-status
|
||||
---
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: ClusterRoleBinding
|
||||
metadata:
|
||||
name: sw-block-phase42-lifecycle-owner
|
||||
subjects:
|
||||
- kind: ServiceAccount
|
||||
name: sw-block-lifecycle-owner
|
||||
namespace: ${NAMESPACE}
|
||||
roleRef:
|
||||
apiGroup: rbac.authorization.k8s.io
|
||||
kind: ClusterRole
|
||||
name: sw-block-phase42-lifecycle-owner
|
||||
YAML
|
||||
run_expect_success apply-rbac "${KUBECTL}" apply -f "${ARTIFACT_DIR}/rbac.yaml"
|
||||
|
||||
cat >"${ARTIFACT_DIR}/admission.yaml" <<YAML
|
||||
apiVersion: admissionregistration.k8s.io/v1
|
||||
kind: ValidatingAdmissionPolicy
|
||||
metadata:
|
||||
name: sw-block-phase42-finalizer-only
|
||||
spec:
|
||||
failurePolicy: Fail
|
||||
matchConstraints:
|
||||
resourceRules:
|
||||
- apiGroups: ["block.seaweedfs.com"]
|
||||
apiVersions: ["v1alpha1"]
|
||||
operations: ["UPDATE"]
|
||||
resources: ["swblockvolumes"]
|
||||
validations:
|
||||
- expression: >-
|
||||
request.userInfo.username != 'system:serviceaccount:${NAMESPACE}:sw-block-lifecycle-owner' ||
|
||||
(
|
||||
object.spec == oldObject.spec &&
|
||||
object.status == oldObject.status &&
|
||||
object.metadata.labels == oldObject.metadata.labels &&
|
||||
object.metadata.annotations == oldObject.metadata.annotations &&
|
||||
object.metadata.ownerReferences == oldObject.metadata.ownerReferences &&
|
||||
has(object.metadata.finalizers) &&
|
||||
size(object.metadata.finalizers) <= 1 &&
|
||||
object.metadata.finalizers.all(f, f == '${FINALIZER}') &&
|
||||
(!has(oldObject.metadata.finalizers) ||
|
||||
(size(oldObject.metadata.finalizers) <= 1 &&
|
||||
oldObject.metadata.finalizers.all(f, f == '${FINALIZER}')))
|
||||
)
|
||||
message: lifecycle-owner may patch only the Seaweed Block finalizer
|
||||
---
|
||||
apiVersion: admissionregistration.k8s.io/v1
|
||||
kind: ValidatingAdmissionPolicyBinding
|
||||
metadata:
|
||||
name: sw-block-phase42-finalizer-only
|
||||
spec:
|
||||
policyName: sw-block-phase42-finalizer-only
|
||||
validationActions: [Deny]
|
||||
matchResources:
|
||||
namespaceSelector:
|
||||
matchLabels:
|
||||
block.seaweedfs.com/phase42-gate: "true"
|
||||
YAML
|
||||
run_expect_success apply-admission "${KUBECTL}" apply -f "${ARTIFACT_DIR}/admission.yaml"
|
||||
|
||||
cat >"${ARTIFACT_DIR}/volume.yaml" <<YAML
|
||||
apiVersion: block.seaweedfs.com/v1alpha1
|
||||
kind: SwBlockVolume
|
||||
metadata:
|
||||
name: phase42-a
|
||||
namespace: ${NAMESPACE}
|
||||
labels:
|
||||
keep: "true"
|
||||
annotations:
|
||||
keep: "true"
|
||||
spec:
|
||||
pvcName: phase42-a
|
||||
storageClass: sw-block
|
||||
YAML
|
||||
run_expect_success apply-volume "${KUBECTL}" apply -f "${ARTIFACT_DIR}/volume.yaml"
|
||||
|
||||
OPERATOR_AS="system:serviceaccount:${NAMESPACE}:sw-block-operator-status"
|
||||
OWNER_AS="system:serviceaccount:${NAMESPACE}:sw-block-lifecycle-owner"
|
||||
|
||||
run_expect_failure operator-status-main-patch "${KUBECTL}" patch swblockvolume phase42-a -n "${NAMESPACE}" --as "${OPERATOR_AS}" --type=merge -p "{\"metadata\":{\"finalizers\":[\"${FINALIZER}\"]}}"
|
||||
write_summary "operator_status_main_patch_allowed=false"
|
||||
|
||||
run_expect_success lifecycle-owner-add-finalizer "${KUBECTL}" patch swblockvolume phase42-a -n "${NAMESPACE}" --as "${OWNER_AS}" --type=merge -p "{\"metadata\":{\"finalizers\":[\"${FINALIZER}\"]}}"
|
||||
write_summary "lifecycle_owner_finalizer_add_allowed=true"
|
||||
|
||||
run_expect_success lifecycle-owner-remove-finalizer "${KUBECTL}" patch swblockvolume phase42-a -n "${NAMESPACE}" --as "${OWNER_AS}" --type=merge -p '{"metadata":{"finalizers":[]}}'
|
||||
write_summary "lifecycle_owner_finalizer_remove_allowed=true"
|
||||
|
||||
run_expect_failure lifecycle-owner-spec-patch "${KUBECTL}" patch swblockvolume phase42-a -n "${NAMESPACE}" --as "${OWNER_AS}" --type=merge -p '{"spec":{"pvcName":"changed"}}'
|
||||
write_summary "lifecycle_owner_spec_patch_allowed=false"
|
||||
|
||||
run_expect_failure lifecycle-owner-label-patch "${KUBECTL}" patch swblockvolume phase42-a -n "${NAMESPACE}" --as "${OWNER_AS}" --type=merge -p '{"metadata":{"labels":{"changed":"true"}}}'
|
||||
write_summary "lifecycle_owner_label_patch_allowed=false"
|
||||
|
||||
run_expect_failure lifecycle-owner-foreign-finalizer "${KUBECTL}" patch swblockvolume phase42-a -n "${NAMESPACE}" --as "${OWNER_AS}" --type=merge -p '{"metadata":{"finalizers":["example.com/foreign"]}}'
|
||||
write_summary "lifecycle_owner_foreign_finalizer_allowed=false"
|
||||
|
||||
run_expect_failure lifecycle-owner-mixed-patch "${KUBECTL}" patch swblockvolume phase42-a -n "${NAMESPACE}" --as "${OWNER_AS}" --type=merge -p "{\"metadata\":{\"finalizers\":[\"${FINALIZER}\"]},\"spec\":{\"pvcName\":\"changed\"}}"
|
||||
write_summary "lifecycle_owner_mixed_patch_allowed=false"
|
||||
|
||||
if "${KUBECTL}" patch swblockvolume phase42-a -n "${NAMESPACE}" --as "${OWNER_AS}" --subresource=finalizers --type=merge -p "{\"metadata\":{\"finalizers\":[\"${FINALIZER}\"]}}" >"${ARTIFACT_DIR}/finalizers-endpoint.stdout.txt" 2>"${ARTIFACT_DIR}/finalizers-endpoint.stderr.txt"; then
|
||||
write_summary "finalizers_endpoint_allowed=true"
|
||||
echo "unexpected /finalizers subresource success" >&2
|
||||
exit 1
|
||||
fi
|
||||
write_summary "finalizers_endpoint_allowed=false"
|
||||
|
||||
for resource in pods deployments persistentvolumeclaims persistentvolumes storageclasses secrets nodes csidrivers csinodes; do
|
||||
if "${KUBECTL}" auth can-i patch "${resource}" --as "${OWNER_AS}" -n "${NAMESPACE}" >/dev/null 2>&1; then
|
||||
write_summary "lifecycle_owner_${resource}_patch_allowed=true"
|
||||
echo "unexpected patch permission for ${resource}" >&2
|
||||
exit 1
|
||||
fi
|
||||
write_summary "lifecycle_owner_${resource}_patch_allowed=false"
|
||||
done
|
||||
|
||||
run_expect_success final-object "${KUBECTL}" get swblockvolume phase42-a -n "${NAMESPACE}" -o yaml
|
||||
|
||||
write_summary "phase42_lifecycle_owner_admission_status=ok"
|
||||
@@ -0,0 +1,57 @@
|
||||
# Runner-native Phase 42 D1 gate: live lifecycle-owner API/admission boundary.
|
||||
|
||||
name: lifecycle-owner-admission-gate-chain
|
||||
description: >
|
||||
Runs the live Kubernetes ValidatingAdmissionPolicy gate that proves the
|
||||
lifecycle-owner identity can only issue finalizer-shaped SwBlockVolume main
|
||||
object patches. This is the real API/admission successor to the Phase 41
|
||||
schema-aware boundary gate.
|
||||
|
||||
vars:
|
||||
product_root: "{{ env.PRODUCT_ROOT | default('/mnt/c/work/seaweed_block') }}"
|
||||
|
||||
phases:
|
||||
- name: admission_gate
|
||||
actions:
|
||||
- type: exec
|
||||
name: run_phase42_admission_gate
|
||||
cmd: "rm -rf /mnt/smb/work/share/g15d-k8s/{{ run_id }}-phase42-lifecycle-owner-admission-gate && mkdir -p /mnt/smb/work/share/g15d-k8s/{{ run_id }}-phase42-lifecycle-owner-admission-gate && cd {{ product_root }} && SW_BLOCK_ARTIFACT_DIR=/mnt/smb/work/share/g15d-k8s/{{ run_id }}-phase42-lifecycle-owner-admission-gate bash scripts/run-phase42-lifecycle-owner-admission-gate.sh {{ product_root }}"
|
||||
|
||||
- name: asserts
|
||||
actions:
|
||||
- type: grep_log
|
||||
name: admission_gate_ok
|
||||
path: "/mnt/smb/work/share/g15d-k8s/{{ run_id }}-phase42-lifecycle-owner-admission-gate/phase42-lifecycle-owner-admission-gate-summary.txt"
|
||||
pattern: "^phase42_lifecycle_owner_admission_status=ok$"
|
||||
- type: grep_log
|
||||
name: operator_status_no_main_patch
|
||||
path: "/mnt/smb/work/share/g15d-k8s/{{ run_id }}-phase42-lifecycle-owner-admission-gate/phase42-lifecycle-owner-admission-gate-summary.txt"
|
||||
pattern: "^operator_status_main_patch_allowed=false$"
|
||||
- type: grep_log
|
||||
name: lifecycle_owner_finalizer_add_allowed
|
||||
path: "/mnt/smb/work/share/g15d-k8s/{{ run_id }}-phase42-lifecycle-owner-admission-gate/phase42-lifecycle-owner-admission-gate-summary.txt"
|
||||
pattern: "^lifecycle_owner_finalizer_add_allowed=true$"
|
||||
- type: grep_log
|
||||
name: lifecycle_owner_finalizer_remove_allowed
|
||||
path: "/mnt/smb/work/share/g15d-k8s/{{ run_id }}-phase42-lifecycle-owner-admission-gate/phase42-lifecycle-owner-admission-gate-summary.txt"
|
||||
pattern: "^lifecycle_owner_finalizer_remove_allowed=true$"
|
||||
- type: grep_log
|
||||
name: lifecycle_owner_spec_forbidden
|
||||
path: "/mnt/smb/work/share/g15d-k8s/{{ run_id }}-phase42-lifecycle-owner-admission-gate/phase42-lifecycle-owner-admission-gate-summary.txt"
|
||||
pattern: "^lifecycle_owner_spec_patch_allowed=false$"
|
||||
- type: grep_log
|
||||
name: lifecycle_owner_label_forbidden
|
||||
path: "/mnt/smb/work/share/g15d-k8s/{{ run_id }}-phase42-lifecycle-owner-admission-gate/phase42-lifecycle-owner-admission-gate-summary.txt"
|
||||
pattern: "^lifecycle_owner_label_patch_allowed=false$"
|
||||
- type: grep_log
|
||||
name: lifecycle_owner_foreign_finalizer_forbidden
|
||||
path: "/mnt/smb/work/share/g15d-k8s/{{ run_id }}-phase42-lifecycle-owner-admission-gate/phase42-lifecycle-owner-admission-gate-summary.txt"
|
||||
pattern: "^lifecycle_owner_foreign_finalizer_allowed=false$"
|
||||
- type: grep_log
|
||||
name: lifecycle_owner_mixed_patch_forbidden
|
||||
path: "/mnt/smb/work/share/g15d-k8s/{{ run_id }}-phase42-lifecycle-owner-admission-gate/phase42-lifecycle-owner-admission-gate-summary.txt"
|
||||
pattern: "^lifecycle_owner_mixed_patch_allowed=false$"
|
||||
- type: grep_log
|
||||
name: finalizers_endpoint_forbidden
|
||||
path: "/mnt/smb/work/share/g15d-k8s/{{ run_id }}-phase42-lifecycle-owner-admission-gate/phase42-lifecycle-owner-admission-gate-summary.txt"
|
||||
pattern: "^finalizers_endpoint_allowed=false$"
|
||||
Reference in new issue
Block a user