[Information Security資訊安全]SonarQube程式碼品質靜態分析工具(源碼掃描)介紹和環境建置
SonarQube 介紹 SonarQube 是一套完整的程式碼品質分析工具,支援超過 25 種程式語言,如 […]
SonarQube 介紹
SonarQube 是一套完整的程式碼品質分析工具,支援超過 25 種程式語言,如 C/C++、C#/VB.NET、Java、JavaScript、Python…等。這邊為測試架設 SonarQube 系統時所製作的筆記。
透過 SonarQube 的幫助可以讓我們程式碼中潛在的問題都一目了然,主要功能有兩種:
-
一種是負責執行程式碼分析的 Runner,每個語言都有自己對應的 SonarQube Runner (例如 C# 就有 MsBuild Runner)
-
另一種則是在透過 Runner 分析之後,我們可以透過 SonarQube 的報表網站,觀看軟體的健康檢查報告,了解是否有潛在的技術債(Technical Debt),幫助我們及早發現。
SonarQube 官網:https://www.sonarqube.org
SonarQube框架包含以下四個部分:
-
Project
-
SonarQube Scanner
-
SonarQube Server
-
SonarQube Database
SonarQube 程式碼品質分析工具用 7 個維度來分析程式碼品質,包括:
-
程式架構 Architecture & Design
-
冗餘程式 Duplications
-
單元測試 Unit tests
-
複雜度 Complexity
-
潛在問題 Potential bugs
-
寫作原則 Coding rules
-
註解 Comments
環境設定
本篇文章使用 Window10 X64版 | SQL Server 2014 | SonarQube5.6.7 | sonar-scanner-msbuild-3.0.2.656
安裝Sonarqube程式碼品質分析工具 下載位置:https://www.sonarqube.org/downloads/ (本篇文章撰寫時最新版本為 SonarQube8.0,因為建置時遇到的問題太多 ,再加上許多環境上變遷會影響原本其他專案的程式運作,因此等到以後比較穩健的時候再更新換成那個)
SonarQube 負責執行程式碼分析的 Runner 的 SonarQube Scanner for MSBuild(如果是C#的話) 下載位置:https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-msbuild/
更新 Microsoft JDBC Drivers (非必要) 下載位置:https://www.microsoft.com/zh-tw/download/details.aspx?id=11774 ( SonarQube 安裝檔中已經有自帶 Microsoft JDBC Drivers,因此這項非必要)
安裝 Java SE Development Kit 8(JDK8) (不多做說明) 下載位置:http://www.oracle.com/technetwork/java/javase/downloads/index.html (目前市面上大部分使用JDK8,SonarQube5.6.7版本也適用JDK8, 而SonarQube8.0比較大膽,更新比較快,已經使用到11,如果為了開發環境運行順暢,建議新開一個虛擬機Vm,來創造SonarQube的開發環境)
SonarQube 必須搭配一套資料庫系統 請參考官方建議清單:https://docs.sonarqube.org/latest/requirements/requirements/ (目前只支援 SQL Server 2014 以上版本)
設定資料庫(以 SQL Server 2014 為例)
- 建立名稱為 SonarQube 的資料庫,
- 建立資料庫帳號 sonar, 並設定為資料庫SonarQube的擁有者(賦予db_owner權限)
啟動 SQL Server 組態管理員
-
需要啟動 TCP/IP 通訊協定服務
-
其中要設定 TCP 通訊埠為 1433 (如下圖)
安裝 SonarQube
- 安裝
將 SonarQube 壓縮檔解壓縮到指定目錄,例如:C:\SonarQube
將 SonarQube Scanner for MSBuild 壓縮檔解壓縮到指定目錄,例如:C:\SonarQube\bin\sonar-scanner
(這裡的安裝目錄可自訂)
- 設定環境變數
推薦使用 Rapid Environment Editor 修改系統環境變數 在使用者變數的 Path 中加入 C:\SonarQube\bin\sonar-scanner\ (以您設定的 SonarQube 和 SonarQube Scanner 地址)方便後續使用 MSBuild.SonarQube.Runner.exe
- 修改 SonarQube 設定檔 \conf\sonar.properties 主要修改下列三個參數
sonar.jdbc.username 資料庫連線帳號 soanr.jdbc.password 資料庫連線密碼 sonar.jdbc.url 資料庫連線字串(使用 JDBC) 參考下列範例,正式環境建議另外建立一個 DB 使用帳戶,並使用後者方式
`# 使用 Integrated Security 時
sonar.jdbc.url=jdbc:sqlserver://localhost;databaseName=SonarQube;integratedSecurity=true
# 使用指定帳號密碼時
sonar.jdbc.url=jdbc:sqlserver://localhost;databaseName=SonarQube
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar`
- 修改\bin\sonar-scanner下SonarQube.Analysis.xml檔案
sonar.host.url:SonarQube服務器位置 sonar.login:使用者帳號 sonar.password:使用者密碼 參考下列範例
`<Property Name="sonar.host.url">http://localhost:9000</Property>
<Property Name="sonar.login"></Property>
<Property Name="sonar.password"></Property>`
啟動SonarQube
- 執行 \bin\windows-x86-64\StartSonar.bat
第一次執行會需要一點時間讓資料庫初始化,看到Process[ce] is up 表示啟動成功
- 執行 \bin\windows-x86-64\InstallNTService.bat
可安裝成Windows服務,使之可以背景自動啟動
- 預設SonarQube網站網址為
http://localhost:9000 帳號:無 密碼:無
- 可至SonarQube設定檔\conf\sonar.properties
另行設定設定對應網址及Port, sonar.web.host sonar.web.port
- 若要讓非本機用戶使用,記得要再防火牆中開啟對外的9000 port
測試啟動SonarQube
額外問題處理QA:
如果發現,Window更新以後不能開,或是其他組態設定後不能運作,可以嘗試把SQL Server服務的下面幾個項目重新啟動看看。
參考資料:
SonarQube 程式碼品質分析工具:https://poychang.github.io/sonarqube-csharp/
SonarQube 基本介紹 (1):https://dotblogs.com.tw/kirkchen/2016/06/04/sonarqube-introduction
SonarQube 相關研究 :https://myweb.ntut.edu.tw/~jykuo/train/2017Sonarqube.pdf
[DevOps自動化-10] 招喚SonarQube:https://dotblogs.com.tw/aken1215/2016/10/22/182629
[食譜好菜] 用 SonarQube 分析 C# 程式碼品質:https://dotblogs.com.tw/aken1215/2016/10/22/182629
SonarQube架構及使用介紹:https://www.twblogs.net/a/5b8aa0242b71775d1ce818b0