SonarQube 是用Java编写的一种自动代码审查工具,可检测代码中的错误,漏洞和潜在的安全问题,可方便于开发团队进行整体代码审查,规范管理,优化代码质量。

  • 各版本介绍

image-20220503152214694

整体结构

  • SonarQube 实例由三个组件组成

SonarQube 实例组件

  1. 服务器
    • SonarQube 用户界面提供服务的 Web 服务器。
    • 基于 Elasticsearch 的搜索服务器。
    • 负责处理代码分析报告并将其保存在 SonarQube 数据库中的计算引擎。
  2. 数据库
    • 储存代码扫描期间生成的代码质量和安全性的指标和问题。
    • SonarQube 实例配置。
  3. 客户端
    • 在生成或持续集成服务器上运行的一个或多个扫描程序,用于分析项目。

安装和配置(SonarQube 8.9.8 LTS 社区版)

环境准备

  • Java11
    • SonarQube 服务器只支持11版本
  • 数据库
    • 这个版本不支持 MySQL,这里选择 PostgreSQL 13
    • 新建 SonarQube 数据库,并建test模块保存数据

image-20220503160044288

  • 服务器

  • 客户端(扫描器)

    • SonarScanner 4.7,下载完是个压缩包,解压至本地
      • 增加Path环境变量,方便使用:<解压文件夹>\bin

    image-20220503155503732

配置

服务器 SonarQube

  • 打开SonarQube配置文件:sonarqube-8.9.8.54436\conf\sonar.properties
1
2
3
4
5
6
7
# PostgreSQL数据库用户名和密码
sonar.jdbc.username=postgres
sonar.jdbc.password=root
# 配置数据库连接,SonarQube为数据库名,test为模块名
sonar.jdbc.url=jdbc:postgresql://localhost/SonarQube?currentSchema=test
# 设置编码方式
sonar.sorceEncoding=UTF-8

客户端 SonarScanner(扫描器)

  • 打开SonarScanner配置文件:sonar-scanner-4.7.0.2747-windows\conf\sonar-scanner.properties
1
2
3
4
# SonarQube 服务器
sonar.host.url=http://localhost:9000
# 设置编码方式
sonar.sourceEncoding=UTF-8

待测试代码配置

  • 在代码目录下新建sonar-project.properties文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# must be unique in a given SonarQube instance
sonar.projectKey=my:project

# --- optional properties ---

# defaults to project key
#sonar.projectName=My project
# defaults to 'not provided'
#sonar.projectVersion=1.0

# Path is relative to the sonar-project.properties file. Defaults to .
#sonar.sources=.

# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8

# 默认账号密码都是admin,登录一次后会要求修改
sonar.login=admin
sonar.password=admin
  • 一段简单 python 代码
1
print("test")

image-20220503162443281

开始测试

启动服务器

  • 进入SonarQube解压目录打开sonarqube-8.9.8.54436\bin\windows-x86-64中的StartSonar.bat

    • 建议命令行打开,等待服务启动

    image-20220503163123641

    • 出现红框部分表示启动成功,浏览器打开http://localhost:9000/,登录进入(账号密码默认admin)

    image-20220503163346773

测试本地代码

  • 进入待测试代码文件夹,根目录下打开命令行,输入sonar-scanner回车开始分析代码

image-20220503163755513

  • 分析完成

image-20220503163925050

刷新页面查看分析结果

image-20220503164005106

image-20220503164034982

修改代码

1
2
print("test")
break
  • sonar-project.properties下更改版本号
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# must be unique in a given SonarQube instance
sonar.projectKey=my:project

# --- optional properties ---

# defaults to project key
#sonar.projectName=My project
# defaults to 'not provided'
sonar.projectVersion=1.1

# Path is relative to the sonar-project.properties file. Defaults to .
#sonar.sources=.

# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8

sonar.login=admin
sonar.password=admin

重新分析代码后查看结果

image-20220503164706457

  • 查看错误详情

image-20220503164800782

遇到的问题

项目分析失败

  • java.lang.IllegalStateException: Unrecoverable indexation failures: 1 errors among 1 requests

image-20220503211051926

  • sonarqube-8.9.8.54436\logs\es.log中输出显示磁盘占用过高,属于elasticsearch扫描磁盘时报的错
1
WARN  es[][o.e.c.r.a.DiskThresholdMonitor] flood stage disk watermark [95%] exceeded on [q_uywwI7RkmOpRl5xaWlxA][sonarqube][D:\tool\sonarqube-8.9.8.54436\data\es7\nodes\0] free: 18.6gb[3.9%], all indices on this node will be marked read-only
  • 解决方法——关闭磁盘空间阈值

    • 查看 http://localhost:9001/_cluster/settings?pretty 页面

      1
      2
      3
      4
      {
      "persistent": {},
      "transient": {}
      }
    • 关闭 StartSonar,删除sonarqube-8.9.8.54436\data\es7nodes文件夹(之前分析的项目会受影响)

    • 重启 StartSonar

    • 使用Postman或其它方式,通过put请求修改cluster.routing.allocation.disk.threshold_enabled值为false

image-20220503224404156