安全配置 #

Jenkins安全配置是保护CI/CD系统的重要环节,本节将详细介绍认证、授权和安全设置。

安全架构 #

text
┌─────────────────────────────────────────────────────────────┐
│                    Jenkins 安全架构                          │
├─────────────────────────────────────────────────────────────┤
│                                                              │
│  ┌─────────────────┐  ┌─────────────────┐                  │
│  │     认证        │  │     授权        │                  │
│  │  (Authentication)│  │  (Authorization)│                  │
│  │                 │  │                 │                  │
│  │  - 用户名密码   │  │  - 全局安全     │                  │
│  │  - LDAP         │  │  - 项目安全     │                  │
│  │  - SAML         │  │  - 角色策略     │                  │
│  │  - OAuth        │  │  - 矩阵策略     │                  │
│  └─────────────────┘  └─────────────────┘                  │
│                                                              │
│  ┌─────────────────┐  ┌─────────────────┐                  │
│  │    凭据管理     │  │    网络安全     │                  │
│  │                 │  │                 │                  │
│  │  - 密码存储     │  │  - CSRF保护     │                  │
│  │  - SSH密钥      │  │  - 跨域请求     │                  │
│  │  - 证书         │  │  - 代理设置     │                  │
│  └─────────────────┘  └─────────────────┘                  │
│                                                              │
└─────────────────────────────────────────────────────────────┘

全局安全配置 #

访问安全配置 #

  1. 进入 Manage Jenkins
  2. 点击 Configure Global Security

安全域 #

Jenkins内置用户数据库 #

text
Security Realm: Jenkins' own user database
☑ Allow users to sign up

LDAP #

text
Security Realm: LDAP
Server: ldap://ldap.example.com:389
root DN: dc=example,dc=com
User search base: ou=users
User search filter: uid={0}
Group search base: ou=groups
Group search filter: cn={0}
Manager DN: cn=admin,dc=example,dc=com
Manager Password: ********

SAML #

text
Security Realm: SAML 2.0
IdP Metadata URL: https://idp.example.com/metadata
Entity ID: https://jenkins.example.com

GitHub OAuth #

text
Security Realm: GitHub Authentication Plugin
GitHub Web URI: https://github.com
GitHub API URI: https://api.github.com
Client ID: your-client-id
Client Secret: ********

授权策略 #

任何人可以做任何事 #

text
Authorization: Anyone can do anything

不推荐用于生产环境。

安全矩阵 #

text
Authorization: Matrix-based security

配置用户/组的权限:

权限 说明
Overall/Read 查看Jenkins
Overall/Administer 管理Jenkins
Job/Read 查看任务
Job/Build 构建任务
Job/Configure 配置任务
Job/Create 创建任务
Job/Delete 删除任务
Run/Delete 删除构建
Run/Update 更新构建
SCM/Tag 打标签
Credentials/View 查看凭据
Credentials/Create 创建凭据
Credentials/Update 更新凭据
Credentials/Delete 删除凭据

项目矩阵授权 #

text
Authorization: Project-based Matrix Authorization Strategy

可以在项目级别配置权限。

角色策略 #

安装 Role-Based Strategy 插件后:

text
Authorization: Role-Based Strategy

角色权限管理 #

安装插件 #

安装 Role-Based Strategy 插件。

配置角色 #

  1. 进入 Manage Jenkins
  2. 点击 Manage and Assign Roles

全局角色 #

text
Manage Roles → Global roles

admin:
  - Overall/Administer
  - Overall/Read
  
developer:
  - Overall/Read
  - Job/Read
  - Job/Build
  - Job/Configure
  
viewer:
  - Overall/Read
  - Job/Read

项目角色 #

text
Manage Roles → Project roles

pattern: dev-.*
  - Job/Read
  - Job/Build
  - Job/Configure
  - Job/Workspace

pattern: prod-.*
  - Job/Read
  - Job/Build

分配角色 #

text
Assign Roles → Global roles
admin: user1, user2
developer: user3, user4
viewer: user5

Assign Roles → Project roles
dev-.*: user3, user4
prod-.*: user1

用户管理 #

创建用户 #

  1. 进入 Manage Jenkins
  2. 点击 Manage Users
  3. 点击 Create User

修改密码 #

  1. 进入用户页面
  2. 点击 Configure
  3. 修改密码

删除用户 #

  1. 进入用户页面
  2. 点击 Delete

API Token #

  1. 进入用户页面
  2. 点击 Configure
  3. API Token 区域
  4. 点击 Add new Token

凭据安全 #

凭据存储 #

Jenkins凭据加密存储:

text
JENKINS_HOME/
├── credentials.xml
└── secrets/
    ├── master.key
    └── hudson.util.Secret

凭据作用域 #

作用域 说明
System 系统级别,不能在Pipeline中使用
Global 全局级别,所有Pipeline可用

凭据类型 #

类型 说明
Username with password 用户名密码
SSH Username with private key SSH私钥
Secret file 机密文件
Secret text 机密文本
Certificate 证书

网络安全 #

CSRF保护 #

text
CSRF Protection: ☑ Enable CSRF Protection

跨域请求 #

text
Access Control:
  TCP port for JNLP agents: Fixed: 50000
  ☑ Enable security

代理设置 #

text
HTTP Proxy Configuration:
  Server: proxy.example.com
  Port: 8080
  No Proxy Host: localhost, 127.0.0.1

反向代理 #

Nginx配置示例:

nginx
server {
    listen 443 ssl;
    server_name jenkins.example.com;
    
    ssl_certificate /etc/nginx/ssl/cert.pem;
    ssl_certificate_key /etc/nginx/ssl/key.pem;
    
    location / {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Agent安全 #

Agent连接 #

text
TCP port for JNLP agents:
  - Disable: 禁用JNLP Agent
  - Fixed: 使用固定端口
  - Random: 使用随机端口

Agent协议 #

text
Agent Protocols:
  ☑ JNLP4-connect (recommended)
  ☐ JNLP2-connect (deprecated)
  ☐ JNLP-connect (deprecated)

Agent权限 #

text
Agent → Security:
  ☑ Enable Agent → Master Access Control

安全最佳实践 #

1. 最小权限原则 #

只授予必要的权限。

2. 定期审计 #

定期审计用户权限和访问日志。

3. 使用强密码 #

强制使用强密码策略。

4. 启用双因素认证 #

安装 Google Authenticator 插件。

5. 定期更新 #

定期更新Jenkins和插件。

6. 备份配置 #

定期备份Jenkins配置。

7. 网络隔离 #

将Jenkins部署在内网,通过反向代理访问。

8. 监控日志 #

监控Jenkins日志,及时发现异常。

安全检查清单 #

  • [ ] 禁用匿名访问
  • [ ] 配置认证方式
  • [ ] 配置授权策略
  • [ ] 启用CSRF保护
  • [ ] 配置Agent安全
  • [ ] 定期更新Jenkins
  • [ ] 定期更新插件
  • [ ] 备份凭据和配置
  • [ ] 监控访问日志
  • [ ] 审计用户权限

下一步学习 #

小结 #

  • 安全配置是Jenkins的重要环节
  • 配置合适的认证方式
  • 使用最小权限原则
  • 启用CSRF保护
  • 定期审计和更新
  • 监控访问日志
最后更新:2026-03-28