返回提示词目录第 1403 页

容器集群工作负载安全加固

检查容器权限、网络隔离与密钥配置,输出加固清单、配置变更及验证和回滚步骤。

完整提示词

<context>
A default pod runs as whatever user the image chose (often root), with a writable root filesystem, the default Linux capabilities, a mounted service account token that can call the Kubernetes API, and unrestricted network access to every other pod in the cluster. Container escape bugs and lateral movement both start from those defaults. The Pod Security Standards "restricted" profile is the target most workloads can meet: `runAsNonRoot`, `allowPrivilegeEscalation: false`, `capabilities.drop: [ALL]`, `seccompProfile: RuntimeDefault`, plus `readOnlyRootFilesystem` with `emptyDir` mounts for the paths the app really writes to. Network policies only work if the CNI enforces them (some managed defaults do not), and a namespace has no isolation until a default-deny policy exists. RBAC drift is common: `cluster-admin` bound to a service account "to make it work", or `get secrets` across all namespaces. Kubernetes Secrets are only base64 encoded and readable by anyone with `get secrets` in the namespace.
</context>

<task>
Harden {{workload}}.
{{#cluster}}Cluster: {{cluster}}{{/cluster}}

1. Read the manifests or rendered chart (`helm template` or `kubectl kustomize`) and record the current posture per container: user, privilege escalation, capabilities, root filesystem, seccomp, host namespaces or paths, resource limits, image tag versus digest.
2. Read the Dockerfile if present to find the user and the paths the app writes to; if the image only works as root, say what to change in the image.
3. Add security contexts to meet the restricted profile, with `emptyDir` volumes for temp, cache or socket paths, and resource requests and limits.
4. Set `automountServiceAccountToken: false` unless the app calls the Kubernetes API; if it does, give it its own service account with a namespaced Role limited to the verbs and resources it uses.
5. Write NetworkPolicies: default deny ingress and egress for the namespace, then allow only the traffic the app needs, including DNS egress. Check that the CNI enforces them.
6. Secrets: mount as files rather than env vars where the app supports it, limit who can `get secrets`, and recommend an external secret store or encryption at rest if not already used.
7. Add the namespace label for Pod Security Admission, starting in `warn` and `audit` mode before `enforce`.
8. Validate with `kubectl apply --dry-run=server` or `kubeconform`, and a policy scanner if the repo has one (kube-score, Kubescape, Polaris). Test in a non-production namespace and confirm the app starts and serves traffic.
</task>

<constraints>
- Do not apply changes to a production cluster; provide the commands for review.
- Never delete existing RBAC bindings or policies without listing what depends on them.
- When a setting would break the app (for example a read-only filesystem with no writable mount for a cache), add the needed mount instead of dropping the setting, and say so.
{{> guardrails/investigate-before-answering}}
{{> guardrails/verify-before-done}}
</constraints>

<output_format>
## Current posture
Table: container, setting, current value, target value.
## Changes
The changed manifests or values in code blocks, or one line per file if edited in place.
## Network policy
The policies and the traffic each allows.
## RBAC
Service accounts, roles and the reason for each permission.
## Rollout
Order of steps, from warn mode to enforce, and how to roll back.
## Verification
Commands run and actual results.
</output_format>

可替换内容

  • {{workload}}
  • {{#cluster}}
  • {{cluster}}
  • {{/cluster}}
  • {{> guardrails/investigate-before-answering}}
  • {{> guardrails/verify-before-done}}
  • [ALL]

分类与标签

编程开发、容器安全、网络隔离、权限控制、配置加固

前往 AI 图片创作