SonarQube Training - Developer Session
SonarQube Training - Developer Session
Developer Session
A journey in the land of code quality and security
● Scanner integration
● Key metrics
● Rules and Quality Profiles
● Quality Gates
● Configuring Clean as you Code and the New Code Period
● Issue Management
● Working practically with SonarQube SAST results
● Branch Analysis / Pull Request decoration (Merge Request)
● SonarLint
● Q&A
Scanners
Scanning is simple and complex at the same
time
Scanner workflow
SQ
Web Server host
CI/CD host
Compute Search
Scanner (incl. Engine Server
language 3. Upload analysis
analyzers) report
4. Quality Gate, PR decoration
CLI scanner
sonar-scanner [options]
Java-ish build systems
● Making it easy to scan using the same tool you use to build
● Each plugin provides a SonarQube scanning target
● Java analysis requires compile-time artefacts (retrieved from your config)
● Command line options added using -D<option>=<value>
Maven Gradle
sonar-scanner -Dsonar.cfamily.build-wrapper-output=<some_dir>
Compilation database
sonar-scanner -Dsonar.cfamily.compile-commands=compile_commands.json
Severity Rating
Rating represents the single
highest Severity Vulnerability Blocker E
or Bug in the code period
Critical D
Major C
Minor B
Reviewed Rating
SonarQube 8.1+
< 30% E
Rating represents the
percentage of hotspots 30 - 50% D
reviewed
50 - 70% C
Separate ratings for overall
code and New Code 70 - 80% B
>= 80% A
Portfolio key metrics
% of projects Passed
● > 80% => A Average of underlying projects ratings
● > 60% => B ● Unweighted
● > 40% => C ● All projects under the portfolio at any depth (not portfolios and projects only 1 level below)
● > 20% => D
● <= 20% => E
Line and coverage metrics Lines = 14
Lines of Code (LOC) = 9 LOC = 9 / 14 = 64% of lines
Comment lines = 2 Comment Density = 2 / (9 + 2) = 18%
Lines to cover = 5 Lines to cover = 5 / 9 = 55% of LOC
Conditions to cover = 2
1 /*
2 1 * SonarQube size metrics
3 */
4
5 1 1 public class CoverageMetrics {
6
7 2 public float f(int i) {
8 3 2 2 int k = 0; /* default */
9 4 3 if (i != 0) {
10 5 4 1 k = 1;
11 6 2 }
12 7 5 return (float)i/(k+1);
13 8 }
14 9 }
Coverage Line coverage = Lines covered / Lines to cover
Condition coverage = (Cond true + cond false) / 2 * branches
(Overall) coverage = (Cond true + cond false + covered lines) / (2 * branches + exec lines)
Lines to cover = 5
Conditions to cover = 2 Unit Test 1: assertEquals(0.5, c.f(1), 0.0);
}
}
Rules and quality profiles
Defining your quality governance
Quality Profile best practices
Nbr of Days
28 days Favor previous_version
whenever possible
main
Reference main
Branch
Feature Branch
April 29th at 3PM
Automatic resolution
● Just fix the code 😃
Manual resolution
● Administer issues permission
required
● False positive
● Won’t fix
● Never reopened (in that
location)
Multiple issue locations
Managing complex data flow issues
● SonarQube highlights
data flow
● Cross-file analysis
● Helps you decide
where and how to fix
Working with Security
Injections reporting UI and Security hotspots
workflow
Security
What we do and don’t do
In the
Primarily for
SAST DevSecOps
Developers
pipeline
Auditors
Specific
DAST SCA Fast !
process
Security: What we detect
OWASP Top 10 Security Risk Categories
A1 A2 A3 A4
A5 A6 A7 A8
A9 A10
→ VULNERABILITY
→ FIX
→ HOTSPOT
→ REVIEW
Hotspot lifecycle Progress indicator
Dedicated tab
Sorted by
priority
New workflow
Security Rating
Overview of code security
Severity Rating
Projects
Highest severity Vulnerability in the Blocker E
code period
Critical D
Major C
Portfolios
Average of the Security Ratings of Minor B
included Projects
Info A
Security Review Rating
How are you doing on Security Hotspot review?
Reviewed Rating
Projects
Percentage of hotspots reviewed in < 30% E
the code period
30 - 50% D
50 - 70% C
Portfolios
Average of the Security Review 70 - 80% B
Ratings of included Projects
>= 80% A
Customization
Support your Proprietary Frameworks
● sonar.branch.name
● sonar.pullrequest.key
● sonar.pullrequest.branch
● sonar.pullrequest.base
��🏾
��🏾
💻 💻
Feature
branch Merge
Main branch
Issues
highlighted
inline
On-the-fly
analysis
Full rule
documentation
available
SonarLint connected mode
Configure
rules locally
Manage
advanced
configuration
In-app
notifications
SonarLint connected mode
Open Hotspots in IDE
New Action in
SonarQube UI
SonarLint UI
extended
SonarLint connected mode
Taint Vulnerabilities
Track Tainted
SonarLint Data in Code
extended UI
SonarLint language support
Which languages in which IDE?
Feedback
2 sec 15 min 24h loop
Set and
Enforce
Quality Gates
Update Quality Gates
(and optionally Profiles)
on a regular basis
Feedback is a gift ! Thank you
http://tiny.cc/h21xlz
Questions ?