mirror of
https://github.com/yeasy/docker_practice.git
synced 2026-03-26 03:35:31 +00:00
Remove blank lines after code block markers
This commit is contained in:
@@ -18,7 +18,6 @@ Trivy 是由 Aqua Security 开发的开源漏洞扫描器,以其轻量级、
|
||||
**安装与基本使用:**
|
||||
|
||||
```bash
|
||||
|
||||
# 安装 Trivy
|
||||
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin
|
||||
|
||||
@@ -34,17 +33,14 @@ trivy fs /path/to/project
|
||||
# 扫描 Git 仓库
|
||||
trivy repo https://github.com/aquasecurity/trivy
|
||||
```
|
||||
|
||||
**在 CI/CD 中集成:**
|
||||
|
||||
```bash
|
||||
|
||||
# 设置严重程度过滤
|
||||
trivy image --severity HIGH,CRITICAL \
|
||||
--exit-code 1 \
|
||||
myregistry.com/myapp:v1.0.0
|
||||
```
|
||||
|
||||
#### Grype - 支持多种软件包的扫描器
|
||||
|
||||
Grype 由 Anchore 开发,支持更广泛的软件包管理器和语言。
|
||||
@@ -58,7 +54,6 @@ Grype 由 Anchore 开发,支持更广泛的软件包管理器和语言。
|
||||
**安装与使用:**
|
||||
|
||||
```bash
|
||||
|
||||
# 安装 Grype
|
||||
curl -sSfL https://raw.githubusercontent.com/anchore/grype/main/install.sh | sh -s -- -b /usr/local/bin
|
||||
|
||||
@@ -72,7 +67,6 @@ grype sbom:sbom.json
|
||||
# 扫描特定目录
|
||||
grype dir:/path/to/app
|
||||
```
|
||||
|
||||
#### Snyk - 完整的安全平台
|
||||
|
||||
Snyk 提供了商业级的安全扫描服务,特别适合企业环境。
|
||||
@@ -86,7 +80,6 @@ Snyk 提供了商业级的安全扫描服务,特别适合企业环境。
|
||||
**基本使用:**
|
||||
|
||||
```bash
|
||||
|
||||
# 安装 Snyk CLI
|
||||
npm install -g snyk
|
||||
|
||||
@@ -99,7 +92,6 @@ snyk container test docker-archive://image.tar
|
||||
# 监控仓库
|
||||
snyk monitor --docker
|
||||
```
|
||||
|
||||
**工具对比表:**
|
||||
|
||||
| 特性 | Trivy | Grype | Snyk |
|
||||
@@ -124,11 +116,9 @@ Syft 是 Anchore 推出的专业 SBOM 生成工具。
|
||||
```bash
|
||||
curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sh -s -- -b /usr/local/bin
|
||||
```
|
||||
|
||||
**生成 SBOM:**
|
||||
|
||||
```bash
|
||||
|
||||
# 从镜像生成 SBOM(多种格式)
|
||||
syft docker:nginx:latest -o json > sbom.json
|
||||
syft docker:nginx:latest -o spdx > sbom.spdx
|
||||
@@ -140,7 +130,6 @@ syft dir:/path/to/app -o json > sbom.json
|
||||
# 从 OCI 镜像档案生成
|
||||
syft oci-archive:image.tar -o json > sbom.json
|
||||
```
|
||||
|
||||
#### CycloneDX 与 SPDX 格式
|
||||
|
||||
两种主流的 SBOM 格式:
|
||||
@@ -164,7 +153,6 @@ syft oci-archive:image.tar -o json > sbom.json
|
||||
</components>
|
||||
</bom>
|
||||
```
|
||||
|
||||
**SPDX 格式示例:**
|
||||
|
||||
```json
|
||||
@@ -185,7 +173,6 @@ syft oci-archive:image.tar -o json > sbom.json
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
#### SBOM 的应用场景
|
||||
|
||||
**漏洞关联:**
|
||||
@@ -193,11 +180,9 @@ syft oci-archive:image.tar -o json > sbom.json
|
||||
当新的 CVE 被发现时,可快速查询受影响的应用:
|
||||
|
||||
```bash
|
||||
|
||||
# 使用 Grype 针对 SBOM 进行漏洞扫描
|
||||
grype sbom:sbom.json --add-cpes-if-none
|
||||
```
|
||||
|
||||
**合规性报告:**
|
||||
|
||||
将 SBOM 保存为构建产物,用于审计和合规性检查。
|
||||
@@ -221,7 +206,6 @@ wget https://github.com/sigstore/cosign/releases/latest/download/cosign-linux-am
|
||||
chmod +x cosign-linux-amd64
|
||||
sudo mv cosign-linux-amd64 /usr/local/bin/cosign
|
||||
```
|
||||
|
||||
**生成密钥对(传统方式):**
|
||||
|
||||
```bash
|
||||
@@ -229,21 +213,17 @@ cosign generate-key-pair
|
||||
|
||||
# 生成 cosign.key 和 cosign.pub
|
||||
```
|
||||
|
||||
**签名镜像:**
|
||||
|
||||
```bash
|
||||
|
||||
# 使用私钥签名(推送到仓库前)
|
||||
cosign sign --key cosign.key myregistry.com/myapp:v1.0.0
|
||||
|
||||
# 系统会提示输入私钥密码
|
||||
```
|
||||
|
||||
**验证签名:**
|
||||
|
||||
```bash
|
||||
|
||||
# 使用公钥验证
|
||||
cosign verify --key cosign.pub myregistry.com/myapp:v1.0.0
|
||||
|
||||
@@ -258,11 +238,9 @@ cosign verify --key cosign.pub myregistry.com/myapp:v1.0.0
|
||||
# "optional": {...}
|
||||
# }
|
||||
```
|
||||
|
||||
**Keyless 签名(推荐用于 CI/CD):**
|
||||
|
||||
```bash
|
||||
|
||||
# 在 GitHub Actions 等 CI 中无需存储密钥
|
||||
cosign sign --yes myregistry.com/myapp:v1.0.0
|
||||
|
||||
@@ -271,7 +249,6 @@ cosign verify myregistry.com/myapp:v1.0.0 \
|
||||
--certificate-identity https://github.com/myorg/myrepo/.github/workflows/build.yml@refs/heads/main \
|
||||
--certificate-oidc-issuer https://token.actions.githubusercontent.com
|
||||
```
|
||||
|
||||
#### Docker Content Trust 与 Notary
|
||||
|
||||
Docker Content Trust 使用 Notary 实现镜像签名,是 Docker 官方的签名解决方案。
|
||||
@@ -279,7 +256,6 @@ Docker Content Trust 使用 Notary 实现镜像签名,是 Docker 官方的签
|
||||
**启用 DCT:**
|
||||
|
||||
```bash
|
||||
|
||||
# 在环境中启用 DCT
|
||||
export DOCKER_CONTENT_TRUST=1
|
||||
|
||||
@@ -291,24 +267,20 @@ docker push myregistry.com/myapp:v1.0.0
|
||||
# 禁用 DCT(仅用于特定操作)
|
||||
docker push --disable-content-trust myregistry.com/myapp:v1.0.0
|
||||
```
|
||||
|
||||
**签名密钥管理:**
|
||||
|
||||
```bash
|
||||
|
||||
# 首次推送时会提示创建 Delegation Key
|
||||
# 密钥存储在 ~/.docker/trust/private/root_keys/ 和 ~/.docker/trust/private/tuf_keys/
|
||||
|
||||
# 查看签名信息
|
||||
docker inspect --format='{{.RepoDigests}}' myregistry.com/myapp:v1.0.0
|
||||
```
|
||||
|
||||
### 18.6.4 供应链安全最佳实践
|
||||
|
||||
#### 1. 基础镜像安全
|
||||
|
||||
```dockerfile
|
||||
|
||||
# ❌ 不推荐:使用 latest 标签
|
||||
FROM ubuntu:latest
|
||||
RUN apt-get update && apt-get install -y curl
|
||||
@@ -317,7 +289,6 @@ RUN apt-get update && apt-get install -y curl
|
||||
FROM ubuntu:22.04@sha256:a6d2b38300ce017add71440577d5b0a90460d0e6e0e14...(完整 64 位哈希)
|
||||
RUN apt-get update && apt-get install -y curl=7.68.0-1ubuntu1
|
||||
```
|
||||
|
||||
#### 2. 构建时扫描
|
||||
|
||||
在 Dockerfile 中集成安全扫描:
|
||||
@@ -337,11 +308,9 @@ RUN go build -o app .
|
||||
FROM alpine:3.17@sha256:abcd1234...(请替换为实际完整的 64 位摘要哈希)
|
||||
COPY --from=builder /app/app /app
|
||||
```
|
||||
|
||||
#### 3. 运行时镜像扫描策略
|
||||
|
||||
```bash
|
||||
|
||||
# 镜像构建完成后立即扫描
|
||||
trivy image --severity HIGH,CRITICAL \
|
||||
--exit-code 1 \
|
||||
@@ -351,13 +320,11 @@ trivy image --severity HIGH,CRITICAL \
|
||||
# 定期扫描已部署的镜像
|
||||
trivy image --scanners vuln,misconfig registry:5000/myapp:latest
|
||||
```
|
||||
|
||||
#### 4. 镜像仓库安全配置
|
||||
|
||||
**Harbor(私有镜像仓库)的安全扫描:**
|
||||
|
||||
```yaml
|
||||
|
||||
# harbor.yml 配置示例
|
||||
trivy:
|
||||
enabled: true
|
||||
@@ -368,7 +335,6 @@ trivy:
|
||||
scan_on_push: true # 推送时自动扫描
|
||||
scan_all: true # 扫描仓库中的所有镜像
|
||||
```
|
||||
|
||||
#### 5. 政策执行
|
||||
|
||||
在 Kubernetes 环境中使用 Admission Webhook 强制镜像签名和扫描:
|
||||
@@ -393,7 +359,6 @@ webhooks:
|
||||
admissionReviewVersions: ["v1"]
|
||||
sideEffects: None
|
||||
```
|
||||
|
||||
### 18.6.5 CI/CD 中集成安全扫描
|
||||
|
||||
#### GitHub Actions 工作流示例
|
||||
@@ -480,7 +445,6 @@ jobs:
|
||||
push: true
|
||||
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
|
||||
```
|
||||
|
||||
#### GitLab CI 工作流示例
|
||||
|
||||
```yaml
|
||||
@@ -545,7 +509,6 @@ push:
|
||||
only:
|
||||
- main
|
||||
```
|
||||
|
||||
### 18.6.6 常见问题与最佳实践
|
||||
|
||||
**Q: 扫描报告中有过时的 CVE,如何处理?**
|
||||
|
||||
Reference in New Issue
Block a user