溫馨提示×

Linux環境下GitLab如何配置安全策略

小樊
43
2025-09-29 14:14:14
欄目: 智能運維

Linux環境下GitLab安全策略配置指南

一、基礎安全配置

1. 配置防火墻

通過防火墻限制外部對GitLab服務器的訪問,僅開放必要端口(HTTP 80、HTTPS 443),阻止非法IP或端口的探測。

  • 使用UFW(Ubuntu)
    sudo apt install ufw -y
    sudo ufw allow 80/tcp  # 允許HTTP
    sudo ufw allow 443/tcp # 允許HTTPS
    sudo ufw enable        # 啟用防火墻
    
  • 使用Firewalld(CentOS)
    sudo firewall-cmd --permanent --zone=public --add-service=http
    sudo firewall-cmd --permanent --zone=public --add-service=https
    sudo firewall-cmd --reload
    

2. 啟用HTTPS(SSL/TLS加密)

使用SSL證書加密GitLab與客戶端之間的數據傳輸,防止中間人攻擊。推薦使用Let’s Encrypt免費證書(自動續期):

  • 安裝Certbot(Ubuntu):
    sudo apt install certbot python3-certbot-nginx -y
    
  • 申請證書
    sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
    
  • 配置GitLab:編輯/etc/gitlab/gitlab.rb,指定證書路徑并強制HTTPS:
    external_url 'https://yourdomain.com'
    nginx['ssl_certificate'] = "/etc/letsencrypt/live/yourdomain.com/fullchain.pem"
    nginx['ssl_certificate_key'] = "/etc/letsencrypt/live/yourdomain.com/privkey.pem"
    nginx['redirect_http_to_https'] = true  # 自動跳轉HTTPS
    
  • 重載配置
    sudo gitlab-ctl reconfigure
    sudo gitlab-ctl restart
    

3. 配置SSH密鑰認證

替代密碼認證,提升用戶登錄安全性:

  • 生成SSH密鑰(用戶本地終端):
    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    
  • 添加公鑰到GitLab:將生成的~/.ssh/id_rsa.pub內容復制到GitLab用戶設置的「SSH Keys」中。
  • 測試SSH連接
    ssh -T git@yourdomain.com
    

4. 定期備份數據

通過自動化備份防止數據丟失,建議每日備份并存儲到異地:

  • 手動備份
    sudo gitlab-rake gitlab:backup:create
    
  • 自動備份(添加cron任務):
    sudo crontab -e
    
    添加以下內容(每日凌晨2點備份):
    0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create
    
  • 清理舊備份(保留最近7天):
    sudo gitlab-ctl cleanup-backup
    

5. 及時更新GitLab

定期升級GitLab到最新穩定版,修復已知安全漏洞:

sudo apt update
sudo apt upgrade gitlab-ce -y  # Debian/Ubuntu
sudo yum update gitlab-ce -y  # CentOS/RHEL
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart

二、高級安全措施

1. 強化密碼策略

通過GitLab配置強制用戶使用復雜密碼,降低賬戶被破解風險:
編輯/etc/gitlab/gitlab.rb,添加以下規則:

gitlab_rails['password_authentication'] = true  # 啟用密碼認證
gitlab_rails['password_complexity'] = {
  'min_length' => 12,          # 最小長度12位
  'require_lowercase' => true, # 必須包含小寫字母
  'require_uppercase' => true, # 必須包含大寫字母
  'require_numbers' => true,   # 必須包含數字
  'require_special_characters' => true  # 必須包含特殊字符
}

重載配置:

sudo gitlab-ctl reconfigure

2. 配置雙因素認證(2FA)

為賬戶添加第二層驗證,即使密碼泄露也能阻止非法登錄:

  • 啟用2FA(用戶個人設置):
    用戶登錄GitLab后,進入「Preferences」→「Password and Authentication」,開啟「Two-factor authentication」,選擇驗證方式(如TOTP、手機短信)。
  • 強制2FA(管理員設置):
    進入「Admin Area」→「Settings」→「Authentication」,勾選「Require two-factor authentication」,強制所有用戶啟用。

3. 限制文件上傳

防止用戶上傳敏感文件(如.env、config.json),避免敏感信息泄露:
編輯/etc/gitlab/gitlab.rb,添加以下規則:

gitlab_rails['gitlab_shell_upload_pack'] = true
gitlab_rails['gitlab_shell_receive_pack'] = true
gitlab_rails['gitlab_shell_ssh_port'] = 22
gitlab_rails['gitlab_shell_authorized_keys_file'] = '/var/opt/gitlab/.ssh/authorized_keys'
# 禁止上傳危險文件類型
gitlab_rails['upload_size_limit'] = 100.megabytes  # 限制上傳大小
gitlab_rails['block_upload_extensions'] = ['exe', 'bat', 'sh', 'env', 'json', 'yml']

重載配置:

sudo gitlab-ctl reconfigure

4. 集成LDAP/Active Directory

與企業現有身份管理系統集成,統一用戶身份認證,減少密碼管理成本:
編輯/etc/gitlab/gitlab.rb,添加LDAP配置:

gitlab_rails['ldap_enabled'] = true
gitlab_rails['ldap_servers'] = {
  'main' => {
    'label' => 'LDAP',
    'host' => 'ldap.example.com',  # LDAP服務器地址
    'port' => 389,                  # LDAP端口(默認389)
    'uid' => 'uid',                 # 用戶標識字段(如uid、sAMAccountName)
    'encryption' => 'plain',         # 加密方式(none/plain/ssl/tls)
    'bind_dn' => 'cn=admin,dc=example,dc=com',  # 綁定DN(管理員賬號)
    'password' => 'your_ldap_password',       # 綁定密碼
    'base' => 'ou=users,dc=example,dc=com',    # 用戶搜索基礎DN
    'user_filter' => '(objectClass=person)'    # 用戶過濾條件
  }
}

重載配置:

sudo gitlab-ctl reconfigure

5. 配置審計日志與監控

記錄用戶活動(如登錄、代碼提交、權限變更),便于追蹤安全事件:

  • 開啟審計日志(GitLab默認開啟):審計日志路徑為/var/log/gitlab/gitlab-rails/audit.log。
  • 導出審計日志(可選):通過GitLab API導出日志到外部系統(如ELK、Splunk)進行分析:
    curl --header "PRIVATE-TOKEN: your_access_token" "https://yourdomain.com/api/v4/audit_events?created_after=2025-01-01"
    
  • 實時監控:使用Prometheus+Grafana監控GitLab性能及安全指標(如登錄失敗次數、API調用頻率),設置告警規則(如連續5次登錄失敗觸發告警)。

6. 權限管理與最小化授權

通過角色權限分支保護限制用戶對項目的訪問,遵循“最小權限原則”:

  • 項目可見性:將項目設置為Private(僅成員可訪問),避免公開泄露代碼。
  • 角色分配:根據用戶職責分配角色(如Guest僅查看、Developer可推送代碼、Maintainer可管理項目)。
  • 分支保護:進入「Project Settings」→「Repository」,設置Protected Branches(如main分支),限制只有Maintainer及以上角色可推送代碼,防止誤操作或惡意修改。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女