2023年9月26日 星期二

以SonarQube掃描dotNet Core專案源碼(白箱掃描)

SonarQube是一套白箱掃描工具,本例使用開源免費的Community版本,有關安裝方式可參考「在KALI Linux建置黑、白箱掃描系統(Arachni及SonarQube)

本文以GitHub上的dotNet Core範例「MagicianredLabs/dotnetcore5-example」做示範說明,若想掃描其他 dotNet Core專案,請依該專案環境、結構酌作調整,尤其工具及SDK的版本。

一、必要需求

(一)dotNet SDK

可下載安裝版或ZIP檔,本例選用7.0.201的Amd 64之ZIP版,直接解壓縮到自選目錄:C:\dotNet.core\dotnet-x64-7.0.201,並將此路徑加入 Path 環境變數。

(二)檢查「%USERPROFILE%\.dotnet\tools」目錄裡是否有「dotnet-sonarscanner.exe」檔案及「.store」目錄,如果沒有,請執行下列命令,以安裝 dotNet Core 用的sonarscanner 全域工具:

cd C:\dotNet.core\dotnet-x64-7.0.201
dotnet tool install --global dotnet-sonarscanner --version 5.13.1

備註:可到NUGet查看--version X.X.X可支援的版本

二、執行掃描

(一)啟動SonarQube Scanner Server,假設位於「 http://192.168.232.153:9000

(二)以瀏覽器連線SonarQube Scanner Server,並建立掃描專案(如重掃描舊專案,請備妥 Project Token)

1. 由Projects頁面選擇「Create Project」->「Manually」

2.社群版只能選擇「Locally」

3.填入「Project display name」(專案名稱)及「Project key」(專案鍵名)

4.產生Project Token(專案身份代號)

5.請記下Project Token



6.選擇要被掃描的專案類型(.NET)

7.選擇.NET的型態(.NET Core)



(三)開啟命令提示字元,將工作目錄切換到待掃描的dotNet Core專案目錄,本例使用MagicianredLabs/dotnetcore5-example裡的it.example.dotnetcore5專案做示範,下載後置於D:\sonar-scanning-examples-master\dotnetcore5-example-main\it.example.dotnetcore5。

(四)如果已完成項一之(二)的要求,就不會理會上圖的第一條命令(dotnet tool install --global dotnet-sonarscanner)。
如已將dotNet SDK的目錄加到PATH環境變數,就逐一複製及執行SonarQube所提供的後三段命令。
若想靈活切換SDK版本,可以在執行掃描命令之前先執行「set PATH=SDKPath;%PATH%」設定路徑(SDKPath請換成dotNet SDK的真正路徑)

(五)執行過程如下所示(橘紅色部分是我所執行的命令

C:\>set path=C:\dotNet-x64-7.0.201;%path%
C:\>d:
D:\>
cd d:\sonar-scanning-examples-master\dotnetcore5-example-main\it.example.dotnetcore5
D:\sonar-scanning-examples-master\dotnetcore5-example-main\it.example.dotnetcore5>dotnet sonarscanner begin /k:".netFramework-Example.core" /d:sonar.host.url="http://192.168.232.153:9000"  /d:sonar.login="sqp_c855f5d97c43571f467c8757cd5bcd1e9bb8792a"
SonarScanner for MSBuild 5.13.1
Using the .NET Core version of the Scanner for MSBuild
Pre-processing started.
Preparing working directories...
<<部分訊息省略>>

D:\sonar-scanning-examples-master\dotnetcore5-example-main\it.example.dotnetcore5>dotnet build
MSBuild version 17.5.0-preview-23061-01+040e2a90e for .NET
  正在判斷要還原的專案...
<<部分訊息省略>>
  Sonar: (it.example.dotnetcore5.webapi.tests.integration.csproj) Project processed successfully


建置成功。


C:\dotNet-x64-7.0.201\sdk\7.0.201\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.EolTargetFrameworks.targets(28,5): warning NETSDK1138: 目標 Framework 'net5.0' 已不受支援,未來將不會再收到任何安全性更新。如需支援原則的詳細資訊,請參閱 https://aka.ms/dotnet-core-support。 [D:\sonar-scanning-examples-master\dotnetcore5-example-main\it.example.dotnetcore5\it.example.dotnetcore5.bl\it.example.dotnetcore5.bl.csproj]
<<部分訊息省略>>

D:\sonar-scanning-examples-master\dotnetcore5-example-main\it.example.dotnetcore5\it.example.dotnetcore5.webapi\Program.cs(12,18): warning S1118: Add a 'protected' constructor or the 'static' keyword to the class declaration. [D:\sonar-scanning-examples-master\dotnetcore5-example-main\it.example.dotnetcore5\it.example.dotnetcore5.webapi\it.example.dotnetcore5.webapi.csproj]
    39 個警告
    0 個錯誤

經過時間 00:01:51.00

D:\sonar-scanning-examples-master\dotnetcore5-example-main\it.example.dotnetcore5>dotnet sonarscanner end /d:sonar.login="sqp_c855f5d97c43571f467c8757cd5bcd1e9bb8792a"
SonarScanner for MSBuild 5.13.1
Using the .NET Core version of the Scanner for MSBuild
Post-processing started.
Calling the SonarScanner CLI...
<<部分訊息省略>>

INFO: More about the report processing at http://192.168.232.153:9000/api/ce/task?id=AYrMzgcpHOOPZyW4SWNL
INFO: Analysis total time: 14.247 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 17.470s
INFO: Final Memory: 17M/60M
INFO: ------------------------------------------------------------------------
The SonarScanner CLI has finished
19:34:12.82  Post-processing succeeded.

D:\sonar-scanning-examples-master\dotnetcore5-example-main\it.example.dotnetcore5>

如果前面各步驟無誤,應該可以完成掃描,最後會看到「EXECUTION SUCCESS」。如果過程出現錯誤,請仔細研讀訊息,善用Google或ChatGPT找出解答。

完成掃描後,可在SonarQube Scanner Web UI看到專案結果~~




   關於 dotNet Framework 專案的掃描,請參閱:以SonarQube執行.netFramework專案的源碼掃描(白箱掃描)

沒有留言:

張貼留言