mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4
330 字
1 分钟
DevSecOps 安全开发运维实践
2024-11-17

一、DevSecOps 核心理念#

1.1 安全左移#

flowchart LR A[编码] --> B[构建] B --> C[测试] C --> D[部署] D --> E[运营] subgraph "安全嵌入点" B --> SAST[静态分析] C --> DAST[动态扫描] C --> SCA[组件扫描] D --> IAST[交互测试] end style SAST fill:#90EE90 style DAST fill:#FFD700 style SCA fill:#87CEEB
阶段工具检测内容
编码IDE 插件实时提示
构建SAST代码漏洞
测试DAST/IAST运行时代码
部署镜像扫描漏洞/配置
运营RASP运行时防护

1.2 安全开发流程#

sdlc_security:
phases:
requirements:
- threat_modeling
- security_requirements
- abuse_case_design
design:
- secure_design_review
- architecture_analysis
implementation:
- secure_coding_guidelines
- sast_scan
- secret_detection
testing:
- dast_scan
- sca_scan
- penetration_testing
deployment:
- image_scanning
- compliance_check
- signing

二、SAST 静态应用安全测试#

2.1 扫描配置#

# SonarQube 配置
sonar_project:
quality_gate:
bugs: 0
vulnerabilities: 0
code_smells: < 10
security_hotspots: reviewed
rules:
- security: |
S5131 # XSS
S3649 # SQL Injection
S2083 # Path Traversal
exclusions:
- "**/*test*.java"
- "**/*.min.js"

2.2 检测规则示例#

// 漏洞示例
public class UserController {
// SQL 注入
@GetMapping("/user")
public User getUser(@RequestParam("id") String id) {
// 漏洞:直接拼接 SQL
String sql = "SELECT * FROM users WHERE id = " + id;
return jdbc.query(sql);
// 修复:使用参数化查询
String sql = "SELECT * FROM users WHERE id = ?";
return jdbc.query(sql, id);
}
// XSS 漏洞
@PostMapping("/user")
public String createUser(@RequestParam("name") String name) {
// 漏洞:直接输出
return name; // <script>alert(1)</script>
// 修复:输出编码
return HtmlEncoder.encode(name);
}
}

2.3 Go 语言扫描#

# golangci-lint 配置
linters_settings:
gosec:
excludes:
- G104 # 未处理的错误
- G204 # 命令执行
rules:
- G101: password_detected
- G102: binds_to_all_interfaces
- G103: audit_Binding_WriteTimeout
revive:
rules:
- name: var-naming
severity: warning

三、DAST 动态应用安全测试#

3.1 OWASP ZAP 配置#

zap_scan:
# 扫描策略
scan_policy:
- name: "API Scan"
types:
- url
- body
- response
# 认证配置
authentication:
type: form
login_url: /api/login
username_field: email
password_field: password
# 扫描范围
contexts:
- name: api_context
urls:
- https://api.example.com/*
inc_funcs:
- /admin/*
excl_funcs:
- /health/*

3.2 API Fuzzing#

# API Fuzz 配置
api_fuzzing:
# 变异策略
fuzzing:
- type: inject
payload: "' OR 1=1 --"
- type: length
min: 0
max: 1000000
- type: type
convert_to:
- integer
- float
- null
# 检测目标
targets:
- endpoint: /api/users/{id}
methods:
- GET
- PUT
- DELETE

四、SCA 软件成分分析#

4.1 依赖扫描#

# Trivy 配置
trivy_scan:
# 漏洞数据库
db:
repository: ghcr.io/aquasecurity/trivy-db
# 扫描类型
scans:
- type: rootfs
roots: /
- type: filesystem
- type: image
# 阻断策略
exit_policy:
- vulnerability:
type: CVSS
score: 9.0
action: block
- type: package
name: openssl
action: block

4.2 依赖锁定#

# Go modules 依赖管理
go_mod:
# 校验依赖哈希
sumdb: sum.golang.org
# 私有模块
private:
- gitlab.example.com
- proxy.golang.org
# 依赖版本锁定
require:
- module: github.com/gin-gonic/gin
version: v1.9.1
hash: h1:4CR1mq6....

4.3 许可证合规#

license_check:
# 禁止的许可证
prohibited:
- GPL-3.0
- AGPL-3.0
- LGPL-2.1
# 需要审核的许可证
review_required:
- MPL-2.0
- CDDL-1.0
- EUPL-1.2
# 允许的许可证
allowed:
- Apache-2.0
- MIT
- BSD-2-Clause
- BSD-3-Clause

五、容器镜像安全#

5.1 Dockerfile 最佳实践#

# 多阶段构建
FROM golang:1.21-alpine AS builder
# 复制源码
COPY . .
# 编译
RUN CGO_ENABLED=0 go build -o myapp
# 运行时镜像
FROM alpine:3.19
# 非 root 用户
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
# 健康检查
HEALTHCHECK --interval=30s CMD wget -qO- http://localhost:8080/health
# 复制二进制
COPY --from=builder /myapp /usr/local/bin/
# 切换用户
USER appuser
ENTRYPOINT ["myapp"]

5.2 镜像签名#

# Cosign 签名
# 生成密钥对
cosign generate-key-pair
# 签名镜像
cosign sign --key cosign.key myregistry/myimage:tag
# 验证镜像
cosign verify --key cosign.pub myregistry/myimage:tag

5.3 准入控制#

# Kyverno 策略
kyverno_policies:
- name: require-non-root
rules:
- name: check-containers
match:
resources:
kinds:
- Pod
validate:
pattern: |
spec:
securityContext:
runAsNonRoot: true
- name: restrict-privileges
rules:
- name: privileged-containers
match:
resources:
kinds:
- Pod
validate:
pattern: |
spec:
(containers[]):
securityContext:
(privileged): "false"

六、供应链安全#

6.1 SBOM 软件物料清单#

sbom_generation:
# 生成格式
formats:
- spdx
- cyclonedx
- cdx
# 工具
tools:
- syft
- cdxgen
- spdx-sbom-generator
# CI/CD 集成
ci_integration:
- github_actions: true
- gitlab_ci: true

6.2 依赖审查#

# Renovate 配置
renovate:
onboarding: false
extends:
- "config:recommended"
packageRules:
- matchPackagePatterns:
- "*"
groupName: "all"
schedule:
- "before 5am"
- matchDepTypes:
- indirect
enabled: false
- matchUpdateTypes:
- security
automerge: true

6.3 密钥管理#

# 密钥管理
secret_management:
# Vault 配置
hashicorp_vault:
address: https://vault.example.com
auth_method: kubernetes
role: app-deployment
# GitOps
argocd:
sealed_secrets: true
external_secrets_operator: true

七、CI/CD 安全#

7.1 GitLab CI 安全#

.gitlab-ci.yml
stages:
- build
- test
- security
- deploy
security_scan:
stage: security
image: sonarqube:latest
script:
- sonar-scanner
allow_failure: false
container_scan:
stage: security
image: aquasec/trivy:latest
script:
- trivy image --exit-code 1 --severity HIGH myregistry:tag
dependency_check:
stage: security
script:
- trivy fs --exit-code 1 .

7.2 GitHub Actions 安全#

# 工作流安全
name: Security Scan
on:
push:
branches: [main]
jobs:
security:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Run Trivy
uses: aquasecurity/trivy-action@main
with:
scan-type: fs
severity: CRITICAL,HIGH
- name: Run Semgrep
uses: returntocorp/semgrep-action@v1

八、DevSecOps 工具链#

8.1 工具对比#

类别工具说明
SASTSonarQube, Semgrep, CodeQL代码静态分析
DASTOWASP ZAP, Burp Suite动态扫描
SCATrivy, Snyk, Dependabot依赖分析
IASTContrast, Veracode运行时检测
RASPOpenRASP, Prevoty运行时防护
SecretGitleaks, TruffleHog密钥检测

8.2 深信服产品集成#

sangfor_devsecops:
# 集成 SCA
sca:
enabled: true
block_on_critical: true
# 集成 SAST
sast:
enabled: true
quality_gate: true
# 集成容器安全
container:
scan_on_build: true
block_on_malware: true

十、总结#

DevSecOps 核心是将安全嵌入到开发的每个环节,从代码编写到生产运营形成闭环。工具链的自动化是关键。

支持与分享

如果这篇文章对你有帮助,欢迎支持作者或分享给更多人

DevSecOps 安全开发运维实践
https://blog.souloss.com/posts/cloud-security/devsecops-secure-development-operations/
作者
Souloss
发布于
2024-11-17
许可协议
CC BY-NC-SA 4.0

部分信息可能已经过时