iT邦幫忙

2024 iThome 鐵人賽

DAY 26
0
DevOps

DevOps需要的技能樹...(應該)都點得到!系列 第 26

Day 26: 使用 GitLab CI/CD 進行原始碼掃描及套件掃描

  • 分享至 

  • xImage
  •  

本章節「GitLab CI/CD」概要

Day 24: GitLab CI/CD 基本概念與工作流程
Day 25: GitLab Runners 配置與使用
Day 26: 使用 GitLab CI/CD 進行原始碼掃描及套件掃描
Day 27: 使用 GitLab CI/CD 進行自動化測試
Day 28: 如何在 GitLab CI/CD 中實現持續部署 (CD)
Day 29: GitLab CI/CD 與 Kubernetes 的集成實踐

因為免費方案之硬體需求關係,此章節會以GCP作為雲端平台來操作。

事前準備:這次用docker類型的runner

  • 參考day25 建立一台新的runner
  1. 在Gitlab頁面上create
  2. 起一個docker
  3. 註冊時選擇執行器為docker類型
docker run -itd --net=host --privileged=true --name <換個名字> --restart always -v /srv/gitlab-runner/config:/etc/gitlab-runner -v /var/run/docker.sock:/var/run/docker.sock  -v /usr/bin/docker:/usr/bin/docker gitlab/gitlab-runner:ubuntu-v16.2.0

https://ithelp.ithome.com.tw/upload/images/20240817/201683841FlcI5mLns.jpg

#進入容器
docker exec -it <容器ID> bash
#註冊
gitlab-runner register

https://ithelp.ithome.com.tw/upload/images/20240817/201683845B7dsZ7eSh.jpg

事前準備:架設SonarQube

docker-compose.yml

version: "3"

services:
  sonarqube:
    image: sonarqube:community
    depends_on:
      - db
    environment:
      SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonar
      SONAR_JDBC_USERNAME: sonar
      SONAR_JDBC_PASSWORD: sonar
    volumes:
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_logs:/opt/sonarqube/logs
    ports:
      - "9000:9000"
  db:
    image: postgres:12
    environment:
      POSTGRES_USER: sonar
      POSTGRES_PASSWORD: sonar
    volumes:
      - postgresql:/var/lib/postgresql
      - postgresql_data:/var/lib/postgresql/data

volumes:
  sonarqube_data:
  sonarqube_extensions:
  sonarqube_logs:
  postgresql:
  postgresql_data:

第一次啟動通常會報錯
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

sudo vim /etc/sysctl.conf 
#追加以下内容:
vm.max_map_count=262144
#保存退出
#套用
sudo sysctl -p

預設帳號密碼admin
https://ithelp.ithome.com.tw/upload/images/20240817/20168384W9l2Zg26TQ.jpg

Create a local project > Use the global setting > with GitLab CI
這裡先建立一個名為「test」的專案,待會會用到

按照指示到gitlab的專案配置兩個環境變數

https://ithelp.ithome.com.tw/upload/images/20240817/20168384xCrGzAtanV.jpg

同時上傳參數文件(剛才的環境變數是官網建議步驟,底下這份是可以詳細設定)

sonar-project.properties

#剛才建立的專案名稱
sonar.projectKey=test
sonar.qualitygate.wait=true
sonar.language=
sonar.sources=.
# sonar.inclusions=**/*.go,**/*.py,**/*.js

編寫所需文件

逃不了要寫code的命運,還好有chatgpt,謝謝chatgpt(可自行放入被掃描的檔案)

切到自己新增的branch(因為最後都是合併回main並觸發CI流程)

  • 新增一個python檔案進去

app.py

from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/')
def hello_world():
    return jsonify(message="Hello, World!")

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

https://ithelp.ithome.com.tw/upload/images/20240817/20168384bxKkKF3Dp5.jpg

  • 新增docker需要的套件包

requirements.txt

Flask==2.2.3
  • 新增docker構建文件

Dockerfile

# 使用官方 Python 映像作為基礎映像
FROM python:3.10-slim

# 設置工作目錄
WORKDIR /app

# 複製 requirements.txt 到容器
COPY requirements.txt .

# 安裝 Python 依賴
RUN pip install --no-cache-dir -r requirements.txt

# 複製 Flask 應用到容器
COPY app.py .

# 暴露容器內的 5000 端口
EXPOSE 5000

# 設置環境變量
ENV FLASK_APP=app.py

# 設置 Flask 以外部訪問
ENV FLASK_RUN_HOST=0.0.0.0

# 啟動 Flask 應用
CMD ["flask", "run"]
  • 編輯原本的CI腳本,讓它執行原始碼掃描及套件掃描

.gitlab-ci.yml

stages:
  - build
  - scan
  - scandependency

build:
  stage: build
  image: docker:20.10  
  tags: 
    - scan
  script:
    - echo "Building Docker image..."
  only:
    - merge_requests

scan:
  stage: scan
  tags:
      - scan
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
  image: 
    name: sonarsource/sonar-scanner-cli:5.0
    entrypoint: [""]
  variables:
    GIT_DEPTH: 0
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  script: 
    - sonar-scanner
  allow_failure: true
  dependencies:
    - build

scandependency:
  stage: scandependency
  tags:
    - scan
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
  image: 
    name: aquasec/trivy:latest
    entrypoint: [""]
  script:
    - trivy fs --scanners vuln --exit-code 0 --severity CRITICAL,HIGH . --format json --output trivy-report.json
  allow_failure: true
  artifacts:
    paths:
      - trivy-report.json
  dependencies:
    - scan

最後應該長這樣

https://ithelp.ithome.com.tw/upload/images/20240817/2016838471sltfmih6.jpg

啊哈,可是我們沒有要build它,只是拿來當被掃描的待宰羔羊

發起CI條件的合併分支請求來觸發

https://ithelp.ithome.com.tw/upload/images/20240817/20168384EJhtXhQhOv.jpg

每個階段的掃描過程可以點進去看

https://ithelp.ithome.com.tw/upload/images/20240817/201683841UvytEP2Rj.jpg

https://ithelp.ithome.com.tw/upload/images/20240817/20168384I4nMJtOTJc.jpg

查看報告:SonarQube(原始碼掃描)

訪問SonarQube網站點擊進入test專案
專注在於優先修復Security中高風險以及Security Hotspots中高風險

https://ithelp.ithome.com.tw/upload/images/20240817/201683842DHe4EjS3g.jpg

查看報告:Trivy(套件掃描)

在掃描參數已經配置只列出CRITICAL及HIGH風險等級
內容可參考當前安裝版本,以及建議升級版本

https://ithelp.ithome.com.tw/upload/images/20240817/20168384VG8hyKgAd8.jpg

至此就完成了將原始碼掃描及套件掃描結合到CI流程裡面了


上一篇
Day 25: GitLab Runners 配置與使用
系列文
DevOps需要的技能樹...(應該)都點得到!26
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言