Project onπŸŒπŸ› οΈAutomating To-Do-App Development and Deployment with DevSecOps CI/CD : TutorialπŸš€

Project onπŸŒπŸ› οΈBuilding DevSecOps CI/CD To Do App Project

<< In this project we will create our Node.js application using Jenkins, security using SonarQube,OWASP and image scanning using Trivy >>

Tool we will need in this project is > 1.) AWS EC2 2.) Docker, Docker-compose and DockerHub 3.) Github 4.) Jenkins 5.) SonarQube 6.) OWASP DC 7.) Trivy

Project Prerequisites > Account on AWS Account on GitHub Code (we will use code from this repository)

Part 1 : Initial Setup and Deployment

STEP 1: Launch Instance > Create AWS EC2 instance > T2.Large spec

Connect to instance through SSH client from Local Machine

CMD > sudo apt update -y

CMD > sudo apt upgrade -y

Part 2 : Setup Jenkins

CMD > sudo apt upgrade && sudo apt upgrade -y sudo apt install fontconfig openjdk-17-jre -y

To check Java version use command >

CMD > java --version

After installing Java, we will install Jenkins so for to install Jenkins use command >

CMD >

sudo wget -O /usr/share/keyrings/jenkins-keyring.asc
https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]
https://pkg.jenkins.io/debian-stable binary/ | sudo tee
/etc/apt/sources.list.d/jenkins.list > /dev/null sudo apt-get update sudo apt-get install jenkins sudo systemctl start jenkins sudo systemctl enable jenkins

To check Jenkins status, use command >

CMD > sudo service jenkins status

<< Now copy Public IPv4 address:8080 and you will get redirected Jenkins page >>

To unlock jenkins, use this CMD in CLI

CMD > sudo cat /var/lib/jenkins/secrets/initialAdminPassword

And we will get our password. Copy and paste it to unlock Jenkins β†’ Now click Install suggested plugins β†’ Fill details β†’ Welcome to Jenkins

Part 3 : Setup Docker and Docker-Compose

Now to install docker and docker-compose use command as follow >

CMD >

sudo apt-get update sudo apt-get install docker.io docker-compose -y sudo usermod -aG docker $USER

Also add jenkins to docker group for this use command as follow >

CMD >

sudo usermod -aG docker jenkins sudo reboot

Check docker version use command >

CMD > docker --version

Now enable docker for this use command >

CMD > sudo systemctl enable docker

Part 4 : Setup SonarQube

Now we will build SonarQube container for this use command >

CMD > docker run -itd --name sonarqube -p 9000:9000 sonarqube:lts-community

CMD >

sudo systemctl daemon-reload

Now if we will do docker ps so we will see our SonarQube container is running

CMD > docker ps

<< Now open port no. 9000 and copy Public IPv4 address and paste in new tab Public IPv4 address:9000 and we will be on Unlock SonarQube page >>

Now to make devsecops pipeline, we have create user on sonarqube and this user will have acces given to jenkins. To add user in sonar, Go to SonarQube β†’ Administrator β†’ Security β†’ Users β†’ Tokens β†’ Update Tokens β†’ name "jenkins" β†’ Generate.

Our sonarqube setup done. Now to put sonar into jenkins we have to Install Plugins. To install sonarqube plugins Go to Manage Jenkins β†’ Plugins β†’ Available Plugins β†’ Search SonarQube Scanner β†’ install this plugin.

Now SonarQube token put in Jenkins β†’ Go to Manage Jenkins β†’ click on Credentials β†’ System β†’ Global credentials β†’ Add Credentials β†’ Secret text β†’ in Secret put the token that we copied from SonarQube β†’ ID "Sonar" β†’ Description "Sonar" β†’ Create.

Same as Sonar add Docker Credentials in Jenkins β†’ Go to Manage Jenkins β†’ click on Credentials β†’ System β†’ Global credentials β†’ Add Credentials β†’ Username with password β†’ in Secret put your username and password of dockerhub β†’ ID "DockerHub" β†’ Description "DockerHub" β†’ Create.

Now we will link our SonarQube with Jenkins, for this Go to Manage Jenkins β†’ System β†’ Find SonarQube servers β†’ Add SonarQube β†’ Name "Sonar" β†’ Server URL "http://52.205.89.152:9000" β†’ Server authentication token "Sonar" β†’ click on Apply and Save.

We added our SonarQube Server to Jenkins. Now to enable Sonar Scanner Go to Manage Jenkins β†’ Tools β†’ Find SonarQube Scanner installations β†’ Add SonarQube Scanner β†’ name "Sonar" β†’ Version "latest" β†’ click on Apply and Save.

Now we will create sonar webhooks. Go to Sonar β†’ Administrator β†’ Webhooks β†’ Create β†’ Name "jenkins" β†’ URL "http://52.205.89.152:8080/sonarqube-webhook/" β†’ Create.

Part 5 : Setup Trivy Now to install trivy use command >

CMD >

sudo apt-get install wget apt-transport-https gnupg lsb-release wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | sudo apt-key add - echo deb https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main | sudo tee -a /etc/apt/sources.list.d/trivy.list sudo apt-get update sudo apt-get install trivy

Part 6 : Setup OWASP DC

Now same as SonarQube we have to install OWASP plugins Go to Manage Jenkins β†’ Plugins β†’ Available Plugins β†’ Search OWASP Dependency-Check β†’ install this plugin. Now to enable Dependency-Check Go to Manage Jenkins β†’ Tools β†’ Find Dependency-Check installations β†’ Add Dependency-Check β†’ name "OWASP" β†’ Install automatically β†’ Install from github.com β†’ click on Apply and Save.

Now build a pipeline click on Create a job β†’ give name "node-app" β†’ select "Pipeline" β†’ click OK. Now add the script in Pipeline Script.

pipeline {

agent any environment{ SONAR_HOME = tool "Sonar" } stages {

stage("Code"){ steps{ git url: "https://github.com/NIHALPAPA/DevSecOps_ToDo_App.git" , branch: "main" echo "Code Cloned Successfully" } } stage("SonarQube Analysis"){ steps{ withSonarQubeEnv("Sonar"){ sh "${SONAR_HOME}/bin/sonar-scanner -Dsonar.projectName=node-todo-app -Dsonar.projectKey=node-todo-app -X" } } } stage("SonarQube Quality Gates"){ steps{ timeout(time: 5, unit: "MINUTES"){ waitForQualityGate abortPipeline: false } } } stage("OWASP"){ steps{ dependencyCheck additionalArguments: '--scan ./', odcInstallation: 'OWASP' dependencyCheckPublisher pattern: '**/dependency-check-report.xml' } } stage("Build & Test"){ steps{ sh 'docker build -t devsecops-todo-app:latest .' echo "Code Built Successfully" } } stage("Trivy"){ steps{ sh "trivy image devsecops-todo-app" } } stage("Push to Private Docker Hub Repo"){ steps{ withCredentials([usernamePassword(credentialsId:"DockerHubCreds",passwordVariable:"dockerPass",usernameVariable:"dockerUser")]){ sh "docker login -u ${env.dockerUser} -p ${env.dockerPass}" sh "docker tag devsecops-todo-app:latest ${env.dockerUser}/devsecops-todo-app:latest" sh "docker push ${env.dockerUser}/devsecops-todo-app:latest" }

} } stage("Deploy"){ steps{ sh "docker-compose up -d" echo "App Deployed Successfully" } } } }

Now click Apply and Save β†’ Build Now and our pipeline will build successfully. build-pipeline

Our project on SonarQube. app-on-sonar

Our run will run perfectly. app-running

Part 7 : Setup Email Integration With Jenkins

First we have to install email plugin. Go to Manage Jenkins β†’ Plugins β†’ Available Plugins β†’ Search Email Extension Template β†’ install this plugin.

Now go to your Gmail β†’ click on your profile β†’ click on Manage Your Google Account –> click on the Security tab on the left side panel β†’ search App Passwords β†’ Create a password β†’ you will get page like image given below >

email-pass

Now same as Sonar and Docker we will add Email Credentials in Jenkins β†’ Go to Manage Jenkins β†’ click on Credentials β†’ System β†’ Global credentials β†’ Add Credentials β†’ Username with password β†’ in Secret put papanihal360@gmail.com and password that we created earlier β†’ ID "email" β†’ Description "email" β†’ Create. Now Go to Manage Jenkins β†’ System β†’ Find E-mail notification β†’ Add STMP=stmp.gmail.com β†’ click Avanced β†’ UserName=papanihal360@gmail β†’ Password=put that we created β†’ Tick Use SSL β†’ SMTP Port=465 β†’ click on Apply.

We have to add one more thing so in System β†’ Find Extended E-mail Notification β†’ Add STMP=stmp.gmail.com β†’ SMTP Port=465 β†’ click Avanced β†’ Cedentials=select email that we creted we earlier β†’ Tick Use SSL β†’ Default Content Type=HTML β†’ go down and find Default Triggers β†’ Tick Always β†’ click on Apply and Save. Now in pipeline in the down add code that given below >

post { always { emailext attachLog: true, subject: "'${currentBuild.result}'", body: "Project: ${env.JOB_NAME}
" + "Build Number: ${env.BUILD_NUMBER}
" + "URL: ${env.BUILD_URL}
", to: 'postbox.aj99@gmail.com', #change Your mail attachmentsPattern: 'trivyfs.txt,trivyimage.txt' } }

Now build the pipeline and pipeline "SUCCES" or "FAILURE" we will get email like image given below > get-email get-email2

Our DevSecOps for Node.js Application Project is completed

1
Subscribe to my newsletter

Read articles from NIHAL MOHAMAD ARIF PAPA directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

NIHAL MOHAMAD ARIF PAPA
NIHAL MOHAMAD ARIF PAPA

Aspiring to excel as an AWS DevOps Engineer, combining theoretical expertise with practical project implementation.