Sync: add container security notes
This commit is contained in:
60
wiki/concepts/Container-Lifecycle-Hardening.md
Normal file
60
wiki/concepts/Container-Lifecycle-Hardening.md
Normal file
@@ -0,0 +1,60 @@
|
||||
---
|
||||
title: "Container Lifecycle Hardening"
|
||||
type: concept
|
||||
tags: [Container, Security, Kubernetes, DevSecOps, Hardening]
|
||||
last_updated: 2026-04-24
|
||||
---
|
||||
|
||||
## Definition
|
||||
容器生命周期安全加固(Container Lifecycle Hardening)是指在容器的构建(Build)、部署(Deploy)、运行(Run)三个阶段中系统性地应用安全控制措施,以减少容器化工作负载的攻击面。
|
||||
|
||||
## Three Phases
|
||||
|
||||
### Build Phase(构建阶段)
|
||||
在构建阶段确保镜像本身不含漏洞和敏感信息:
|
||||
- 使用经过安全加固的[[Micro Focus]]基础镜像
|
||||
- 引入 Init 系统([[tini]])防止僵尸进程
|
||||
- 镜像不含敏感信息(密码、API Key)
|
||||
- 启用镜像漏洞扫描
|
||||
- 每个容器仅运行单一应用
|
||||
|
||||
### Deploy Phase(部署阶段)
|
||||
在部署到 Kubernetes 时应用安全配置:
|
||||
- 使用只读根文件系统(readOnlyRootFilesystem: true)
|
||||
- 使用 [[emptyDir Volume]] 存储临时文件
|
||||
- 禁用 Kubernetes API 自动挂载(automountServiceAccountToken: false)
|
||||
- 使用私有服务账号配合精确 RBAC
|
||||
- 避免 hostNetwork 和 hostPort
|
||||
|
||||
### Run Phase(运行时阶段)
|
||||
运行时持续监控和加固(CTP Topic 49 视频中提到将另有专题覆盖)。
|
||||
|
||||
## Key Standards (Build Phase)
|
||||
来自 [[ctp-topic-49-container-lifecycle-hardening-standards]] 的 11 条标准:
|
||||
|
||||
1. **Micro Focus Base Image** — 使用经过安全配置的官方基础镜像
|
||||
2. **Init System** — 使用 [[tini]] 处理信号和防止僵尸进程
|
||||
3. **No Sensitive Info** — 镜像不含敏感数据,使用 [[Kubernetes Secrets]] 运行时注入
|
||||
4. **Read-Only Root FS** — 设置 readOnlyRootFilesystem: true
|
||||
5. **emptyDir for /tmp** — 使用 emptyDir volume 替代 hostPath
|
||||
6. **Image Scanning** — 启用容器镜像漏洞扫描
|
||||
7. **Single Application** — 每容器运行单一应用
|
||||
8. **No K8s API Access** — 禁用 automountServiceAccountToken
|
||||
9. **Private Service Account** — 使用最小权限的服务账号
|
||||
10. **No Host Network** — 避免 hostNetwork 配置
|
||||
11. **No Host Port** — 避免 hostPort 配置
|
||||
|
||||
## Relationship to DevSecOps
|
||||
容器生命周期安全加固是 [[DevSecOps]] 理念在容器领域的具体实践:
|
||||
- **安全左移(Shift-Left)**:安全问题在构建阶段就被发现和修复
|
||||
- **自动化**:通过 IaC 和 CI/CD 流水线自动执行安全检查
|
||||
- **最小权限**:容器仅获得完成功能所需的最小权限
|
||||
|
||||
## Relationship to Supply Chain Security
|
||||
容器镜像加固是[[Supply Chain Security(供应链安全)]]体系的重要组成部分:
|
||||
- 供应链安全的构建阶段(CI)= 容器镜像加固
|
||||
- 构建安全镜像 → 防止攻击者在制品中注入恶意代码
|
||||
|
||||
## Sources
|
||||
- [[ctp-topic-49-container-lifecycle-hardening-standards]]
|
||||
- [[ctp-topic-21-supply-chain-security-in-micro-focus]]
|
||||
56
wiki/concepts/Pod-Security-Context.md
Normal file
56
wiki/concepts/Pod-Security-Context.md
Normal file
@@ -0,0 +1,56 @@
|
||||
---
|
||||
title: "Pod Security Context"
|
||||
type: concept
|
||||
tags: [Kubernetes, Security, Container, Pod, RBAC]
|
||||
last_updated: 2026-04-24
|
||||
---
|
||||
|
||||
## Definition
|
||||
Pod Security Context(Pod 安全上下文)是 Kubernetes 中定义 Pod 和容器级别安全设置的机制,通过 YAML 配置在 Pod Spec 中声明容器的运行权限和访问控制。
|
||||
|
||||
## Common Security Context Fields
|
||||
|
||||
### Container-Level Settings
|
||||
```yaml
|
||||
securityContext:
|
||||
readOnlyRootFilesystem: true # 容器根文件系统设为只读
|
||||
runAsNonRoot: true # 禁止以 root 用户运行
|
||||
runAsUser: 1000 # 指定运行用户 UID
|
||||
runAsGroup: 1000 # 指定运行用户组 GID
|
||||
allowPrivilegeEscalation: false # 禁止权限提升
|
||||
capabilities:
|
||||
drop: ["ALL"] # 移除所有 Linux capabilities
|
||||
```
|
||||
|
||||
### Pod-Level Settings
|
||||
```yaml
|
||||
securityContext:
|
||||
hostNetwork: false # 不使用宿主机网络
|
||||
hostIPC: false # 不使用宿主机 IPC
|
||||
hostPID: false # 不使用宿主机 PID 命名空间
|
||||
automountServiceAccountToken: false # 不自动挂载 ServiceAccount Token
|
||||
```
|
||||
|
||||
## Key Concepts from CTP Topic 49
|
||||
|
||||
### readOnlyRootFilesystem: true
|
||||
将容器根文件系统设为只读,防止攻击者在容器内创建或修改文件。Demo 演示:设置此标志后,容器内尝试 `touch /tmp/test` 会失败。
|
||||
|
||||
### automountServiceAccountToken: false
|
||||
禁用 Kubernetes ServiceAccount Token 的自动挂载,防止容器自动获得对 Kubernetes API 的访问权限。如果容器应用需要访问 API,应显式创建带有精确权限的 ServiceAccount 并通过 RBAC 绑定。
|
||||
|
||||
### hostNetwork: false / hostPort
|
||||
避免使用宿主机网络和宿主机端口:
|
||||
- 防止端口冲突
|
||||
- 维护网络隔离
|
||||
- 减少容器逃逸攻击面
|
||||
- 注意:在受限网络环境(如 Lab Landing Zone)中可能有例外需求(参见 [[ctp-topic-39-implementing-eks-in-the-aws-lab-landing-zone]])
|
||||
|
||||
## Relationship to Kubernetes RBAC
|
||||
Pod Security Context 与 [[Kubernetes RBAC]] 配合使用:
|
||||
- Security Context 控制容器的运行时权限
|
||||
- RBAC 控制 ServiceAccount 对 Kubernetes API 的访问权限
|
||||
- 两者共同实现最小权限原则
|
||||
|
||||
## Sources
|
||||
- [[ctp-topic-49-container-lifecycle-hardening-standards]]
|
||||
42
wiki/concepts/emptyDir-Volume.md
Normal file
42
wiki/concepts/emptyDir-Volume.md
Normal file
@@ -0,0 +1,42 @@
|
||||
---
|
||||
title: "emptyDir Volume"
|
||||
type: concept
|
||||
tags: [Kubernetes, Container, Storage, Security]
|
||||
last_updated: 2026-04-24
|
||||
---
|
||||
|
||||
## Definition
|
||||
emptyDir Volume 是 Kubernetes 中的一种临时存储卷类型,在 Pod 被调度到节点时自动创建,挂载到容器指定路径。当 Pod 从节点移除或被删除时,emptyDir 卷中的数据会被永久删除。
|
||||
|
||||
## Use Case from CTP Topic 49
|
||||
在容器安全上下文中,[[ctp-topic-49-container-lifecycle-hardening-standards]] 推荐使用 emptyDir volume 替代 hostPath 来挂载临时文件系统(如 /tmp),原因:
|
||||
|
||||
1. **数据隔离**:emptyDir 存储在节点上的容器运行时目录中,不与其他 Pod 共享
|
||||
2. **自动清理**:Pod 删除时数据自动清理,防止敏感信息残留
|
||||
3. **安全性优于 hostPath**:hostPath 直接访问宿主机文件系统,误用可能导致容器逃逸
|
||||
4. **适合临时文件**:/tmp 等仅在 Pod 运行期间需要的临时存储
|
||||
|
||||
## Configuration Example
|
||||
```yaml
|
||||
volumes:
|
||||
- name: tmp-storage
|
||||
emptyDir:
|
||||
medium: Memory # 可选:存储在内存中(更安全)
|
||||
sizeLimit: 100Mi # 可选:限制大小
|
||||
```
|
||||
|
||||
## emptyDir vs hostPath
|
||||
|
||||
| 特性 | emptyDir | hostPath |
|
||||
|------|----------|----------|
|
||||
| 数据持久性 | Pod 生命周期 | 节点持久 |
|
||||
| 存储位置 | 节点容器运行时目录 | 宿主机指定路径 |
|
||||
| Pod 删除后 | 自动清理 | 保留 |
|
||||
| 安全性 | 隔离,较安全 | 直接访问宿主机,有风险 |
|
||||
| 适用场景 | 临时文件、缓存 | 日志挂载、配置文件 |
|
||||
|
||||
## Relationship to Container Security
|
||||
emptyDir volume 是 [[Container-Lifecycle-Hardening]] 中"使用空卷替代主机路径挂载敏感临时文件"标准的具体实现。与 [[Pod-Security-Context]] 的 readOnlyRootFilesystem 配合使用,可最大化容器文件系统安全。
|
||||
|
||||
## Sources
|
||||
- [[ctp-topic-49-container-lifecycle-hardening-standards]]
|
||||
Reference in New Issue
Block a user