Sonar Qube | OWASP | Trivy
SonarQube
extensions/plugins/
directory of your SonarQube instance-javaagent:./extensions/plugins/sonarqube-community-branch-plugin-${version}.jar=web
to the sonar.web.javaAdditionalOpts
property in your Sonarqube installation's conf/sonar.properties
file, e.g. sonar.web.javaAdditionalOpts=-javaagent:./extensions/plugins/sonarqube-community-branch-plugin-${version}.jar=web
where ${version} is the version of the plugin being worked with. e.g 1.8.0
-javaagent:./extensions/plugins/sonarqube-community-branch-plugin-${version}.jar=ce
to the sonar.ce.javaAdditionalOpts
property in your Sonarqube installation's conf/sonar.properties
file, e.g. sonar.ce.javaAdditionalOpts=-javaagent:./extensions/plugins/sonarqube-community-branch-plugin-${version}.jar=ce
-d
: Run the container in detached mode.--name sonarqube
: Assign a name to the container (you can use any name).-p 9000:9000
: port map 9000 for host and container (sonarqube installed in a container which is inside a host(docker).# SonarQube Installation
# Switch back to the ubuntu user
sudo -i
# Install the 'unzip' package
apt install unzip
# Add a new user named 'sonarqube'
adduser sonarqube
# Switch to the 'sonarqube' user
sudo su sonarqube
# Download SonarQube distribution zip file
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.4.0.54424.zip
# Unzip the downloaded file
unzip sonarqube-9.4.0.54424.zip
# Set permissions for the SonarQube directory
chmod -R 755 /home/sonarqube/sonarqube-9.4.0.54424
# Change ownership of the SonarQube directory
chown -R sonarqube:sonarqube /home/sonarqube/sonarqube-9.4.0.54424
# Change to the SonarQube binary directory
cd sonarqube-9.4.0.54424/bin/linux-x86-64/
# Start the SonarQube server
./sonar.sh start
These properties are used to configure various aspects of the build process and the behavior of the tools involved, such as Java version, JaCoCo version (a code coverage tool), and SonarQube analysis settings.
Here's an explanation of each property:
<java.version>11</java.version>: Specifies that the project is configured to use Java version 11.
<jacoco.version>0.8.7</jacoco.version>: Specifies the version of the JaCoCo code coverage tool to be used in the project. In this case, version 0.8.7 is specified.
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>: Specifies that JaCoCo will be used as the coverage plugin for SonarQube. This means that JaCoCo will be responsible for generating code coverage reports that SonarQube will use for analysis.
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>: Indicates that SonarQube should reuse existing reports generated during the build process, rather than performing its own dynamic analysis.
<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>: Specifies the path to the JaCoCo coverage report file. This file is typically generated during the build process and contains information about code coverage.
<sonar.language>java</sonar.language>: Indicates that the project's primary language is Java. This is used by SonarQube to properly analyze the code.
<dependency> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.7</version> </dependency>
<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>${jacoco.version}</version> <executions> <execution> <id>jacoco-initialize</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>jacoco-site</id> <phase>package</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin>
pom.xml
file to include the SonarQube plugin configuration. Add the following plugin to the <build>
section:<plugins> <plugin> <groupId>org.sonarsource.scanner.maven</groupId> <artifactId>sonar-maven-plugin</artifactId> <version>3.9.0.2155</version> <!-- Replace with the latest version --> </plugin> </plugins>
pom.xml
to specify the SonarQube server URL, project key, project name, and project version. Add the following properties inside the <properties>
section:<properties> <sonar.host.url>http://your-sonarqube-server-url</sonar.host.url> <sonar.projectKey>unique-project-key</sonar.projectKey> <sonar.projectName>Your Project Name</sonar.projectName> <sonar.projectVersion>1.0</sonar.projectVersion> </properties>
pom.xml
, add the SonarQube token as a property:<properties> <sonar.login>your-sonarqube-token</sonar.login> </properties>
clean
phase ensures a clean build.verify
phase compiles and tests your code.sonar:sonar
goal triggers the SonarQube analysis.Install SonarQube:
Install SonarScanner:
Setup Node.js Project:
package.json
to include coverage. For example, if you're using Mocha and Istanbul, your script might look like this:
"scripts": { "test": "nyc mocha" }
npm test
sonar-project.properties
file accordingly:
sonar.projectKey=my-project sonar.projectName=My Project sonar.sources=src sonar.tests=test sonar.javascript.lcov.reportPaths=coverage/lcov-report/*.lcov
sonar-scanner
command or when integrating SonarQube with your build tools. The available parameters may vary depending on the version of SonarQube and the analysis context. Below are some common SonarQube analysis parameters along with examples.-Dsonar.projectKey
: Unique identifier for your project.
-Dsonar.projectName
: Name of your project.
sonar-scanner -Dsonar.projectName="My Project"
-Dsonar.sources
: Comma-separated list of directories containing source code.
-Dsonar.language
: Specify the main language of your project.
-Dsonar.inclusions
/ -Dsonar.exclusions
: Include or exclude specific files from analysis.
sonar-scanner -Dsonar.inclusions="src/**/*.java" -Dsonar.exclusions="src/test/**/*"
-Dsonar.host.url
: URL of the SonarQube server.
-Dsonar.login
/ -Dsonar.password
: Authentication credentials for connecting to the SonarQube server.
-Dsonar.qualitygate.wait
: Wait for the SonarQube server to complete the analysis and return the quality gate status.
-Dsonar.projectVersion
: Version of your project.
-Dsonar.branch.name
: Specify the branch name if analyzing a specific branch.
-Dsonar.links.scm
: Specify the link to your source code management system.
-Dsonar.verbose
: Output more detailed logs during the analysis.
JENKINS_URL/sonarqube-webhook/
pipeline { agent any tools { maven 'maven3' jdk 'jdk17' sonar 'sonar-scanner' // Ensure SonarQube scanner tool is configured } environment { SCANNER_HOME= tool 'sonar-scanner' } stages { stage('Git Checkout') { steps { git 'https://github.com/jaiswaladi2468/BoardgameListingWebApp.git' } } stage('Compile') { steps { sh "mvn compile" } } stage('Test') { steps { sh "mvn test" } } stage('SonarQube Analysis') { steps { withSonarQubeEnv('sonar-1') { sh ''' $SCANNER_HOME/bin/sonar-scanner -Dsonar.projectName=Boardgame -Dsonar.projectKey=Boardgame \ -Dsonar.branch.name=pre-master -Dsonar.java.binaries=target/classes ''' } } } stage('Quality Gate Check') { steps { script { waitForQualityGate abortPipeline: false, credentialsId: 'new-sonar-token' } } } stage('Build') { steps { sh "mvn package" } } } }
OWASP Open Web Application Security Project
cd dependency-check
./bin/dependency-check.sh --project YourProjectName --scan /path/to/your/project
./dependency-check.sh --project projectname --scan /home/abdul/BoardgameListingWebApp/* (path to your project)