项目作者: rajashekar

项目描述 :
高级语言: Java
项目地址: git://github.com/rajashekar/eCommerce-application.git
创建时间: 2019-09-27T06:33:16Z
项目社区:https://github.com/rajashekar/eCommerce-application

开源协议:

下载


eCommerce Application

The purpose of the project is to understand the concepts of JWT authentication and authorization so users can only access their data, and that data can only be accessed in a secure way, logging using slf4j (Simple Logging Facade for Java), unit testing, indexing logs in splunk, configuring jenkins for CI/CD.

Pre-requisites

Getting Started

Clone the repository

  1. https://github.com/rajashekar/eCommerce-application.git
  2. cd eCommerce-application

Testing the ecommerce application

Start using Maven

  1. mvn spring-boot:run

OR

Run using docker

  1. mvn clean package
  2. docker build -t ecommerce-app .
  3. docker run -p 8080:8080 -t ecommerce-app

OR

Build and run inside docker using mvn.

  1. docker build -f Dockerfile_mvn -t ecommerce-app .
  2. docker run -p 8080:8080 -t ecommerce-app

OR

Run using docker-compose

  1. mvn clean deploy
  2. docker-compose up -d

You can also up each instance

  • To start eCommerce application docker-compose -f docker-compose-app.yml up
  • To start jenkins docker-compose -f docker-compose-jenkins.yml up
  • To start splunk docker-compose -f docker-compose-splunk.yml up
  • To start splunk forwarder docker-compose -f docker-compose-splunk-forward.yml up

Note 1: mvn clean deploy will run build, tests, package, build image and deploy image to registry like docker hub.

Note 2: ~/.m2/settings-security.xml will have master password settings, which is generated using mvn --encrypt-master-password

  1. <settingsSecurity>
  2. <master>{rXXXXXX=}</master>
  3. </settingsSecurity>

Note 3 : ~/.m2/settings.xml will have registry details to where you want to push for example docker hub. Password is encrypted using mvn --encrypt-password

  1. <server>
  2. <id>registry.hub.docker.com</id>
  3. <username>dockerlogin</username>
  4. <password>{rYYYYYYYY=}</password>
  5. <configuration>
  6. <email>myemail@gmail.com</email>
  7. </configuration>
  8. </server>

if you are running on Mac, spotify dockerfile maven plugin might complain like below

  1. Request error: GET unix://localhost:80/version: 503, body: <html>

Try setting docker_host like below and try mvn clean deploy again.

  1. export DOCKER_HOST=unix:///var/run/docker.sock

To verify test cases

  1. mvn clean test

Current coverage is 75%

Deploying all apps using docker-compose

Deploying ecommerce application, splunk, jenkins.
If you want to do only in local just do docker-compose up -d
which will run ecommerce application, jenkins, splunk and splunk forwarder all at once.

If you want to deploy in AWS you can follow below steps
in AWS, take any free tier with docker support

Please make sure you open below ports

After instance is up, login to that instance, install git and docker

  1. sudo yum install git
  2. sudo yum install docker

append docker to user group and start docker

  1. sudo usermod -a -G docker $USER
  2. sudo service docker start

Install docker compose and give permissions

  1. sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  2. sudo chmod +x /usr/local/bin/docker-compose

Clone this repository

  1. git clone https://github.com/rajashekar/eCommerce-application.git
  2. cd eCommerce-application

now run

  1. docker-compose up -d

Note: by removing -d you can see all start up logs of all applications at console

Example -

  1. ec2-user@ip-172-31-47-128 eCommerce-application]$ docker-compose up -d
  2. Starting ecommerce-application_jenkins_1 ... done
  3. Starting ecommerce-application_ecommerceapp_1 ... done
  4. Starting ecommerce-application_splunkenterprise_1 ... done
  5. Starting ecommerce-application_splunkforwarder_1 ... done

Now above command will start 4 applications which are defined in docker-compose.yml

Do docker ps to find all container apps which are running.

  • eCommerce application running at 8081
  • splunk running at 8000
  • splunk forwarder
  • jenkins running at 8080

Configuring jenkins

Go to http://your-hostname:8080/ to configure jenkins
1) Unlock jenkins using /var/jenkins_home/secrets/initialAdminPassword
2) Create First Admin user
3) Install Git hub plugins
4) Generate ssh keys and private key in jenkins credentials
5) Add your public key to deploy keys in github repo.
6) To configure maven go to Jenkins => Global Tool Configuration
7) configure new project, provide github and git details
9) configure auto build triggers when commit happens. Check Github hook trigger for GITScm polling
10) configure maven settings. Set maven version and goals

You can login to jenkins image using docker exec -it <jenkins-container-id> bash

Create settings.xml and settings-security.xml and refer those paths like below.


11) To trigger builds on commit add jenkins webhook details in github
12) Verify builds are triggering after commit
13) Refer jenkins build and deployment logs.

Configuring splunk forwarder

Splunk forwarder is monitoring /logs folder in log_volume which is also shared by ecommerceapp service which logging at /logs/ecommerceapp-application.log

Note 1 : If both splunk and splunk forwarder are running in same instance using docker compose, use SPLUNK_FORWARD_SERVER with value of splunk service name in this case it is splunkenterprise which is listening for events on 9997 port.

Note 2 : If you want splunk to be run on a seperate instance, like using AWS splunk enterprise then use SPLUNK_STANDALONE_URL with value of AWS splunk enterprise server ip and listening port 9997. You can use below command to pass SPLUNK_STANDALONE_URL and SPLUNK_PASSWORD to docker-compose if you do not want to share above details in docker-compose.yml

  1. SPLUNK_STANDALONE_URL=<splunk-server-name>:<splunk-receive-port> SPLUNK_PASSWORD=<password> docker-compose up -d

Verifying eCommerce applicaiton

1) Create user

  1. curl -v -H 'Content-type: application/json' ec2-54-92-129-250.compute-1.amazonaws.com:8081/api/user/create -d '{"username": "Raj4", "password": "password", "confirmPassword": "password"}'
  2. * Trying 54.92.129.250...
  3. * TCP_NODELAY set
  4. * Connected to ec2-54-92-129-250.compute-1.amazonaws.com (54.92.129.250) port 8081 (#0)
  5. > POST /api/user/create HTTP/1.1
  6. > Host: ec2-54-92-129-250.compute-1.amazonaws.com:8081
  7. > User-Agent: curl/7.54.0
  8. > Accept: */*
  9. > Content-type: application/json
  10. > Content-Length: 75
  11. >
  12. * upload completely sent off: 75 out of 75 bytes
  13. < HTTP/1.1 200
  14. < X-Content-Type-Options: nosniff
  15. < X-XSS-Protection: 1; mode=block
  16. < Cache-Control: no-cache, no-store, max-age=0, must-revalidate
  17. < Pragma: no-cache
  18. < Expires: 0
  19. < X-Frame-Options: DENY
  20. < Content-Type: application/json;charset=UTF-8
  21. < Transfer-Encoding: chunked
  22. < Date: Fri, 04 Oct 2019 00:51:49 GMT
  23. <
  24. * Connection #0 to host ec2-54-92-129-250.compute-1.amazonaws.com left intact
  25. {"id":1,"username":"Raj4"}%

2) On login got Authorization header

  1. curl -v -H 'Content-type: application/json' ec2-54-92-129-250.compute-1.amazonaws.com:8081/login -d '{"username": "Raj4", "password": "password"}'
  2. * Trying 54.92.129.250...
  3. * TCP_NODELAY set
  4. * Connected to ec2-54-92-129-250.compute-1.amazonaws.com (54.92.129.250) port 8081 (#0)
  5. > POST /login HTTP/1.1
  6. > Host: ec2-54-92-129-250.compute-1.amazonaws.com:8081
  7. > User-Agent: curl/7.54.0
  8. > Accept: */*
  9. > Content-type: application/json
  10. > Content-Length: 44
  11. >
  12. * upload completely sent off: 44 out of 44 bytes
  13. < HTTP/1.1 200
  14. < Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJSYWo0IiwiZXhwIjoxNTcxMDMxODE4fQ.xG0IYe1hE55eOF5HkycpivsV1G0MTK6y3MN2Sf5j9NyF6WZ4UzfPyg5DzsazdiWsX49MvIfD26OK0dgkOPK_zw
  15. < X-Content-Type-Options: nosniff
  16. < X-XSS-Protection: 1; mode=block
  17. < Cache-Control: no-cache, no-store, max-age=0, must-revalidate
  18. < Pragma: no-cache
  19. < Expires: 0
  20. < X-Frame-Options: DENY
  21. < Content-Length: 0
  22. < Date: Fri, 04 Oct 2019 05:43:38 GMT
  23. <
  24. * Connection #0 to host ec2-54-92-129-250.compute-1.amazonaws.com left intact

3) Accessing /api/user/<userid> without Authorization header results in 403

  1. curl -v -H 'Content-type: application/json' ec2-54-92-129-250.compute-1.amazonaws.com:8081/api/user/Raj4
  2. * Trying 54.92.129.250...
  3. * TCP_NODELAY set
  4. * Connected to ec2-54-92-129-250.compute-1.amazonaws.com (54.92.129.250) port 8081 (#0)
  5. > GET /api/user/Raj4 HTTP/1.1
  6. > Host: ec2-54-92-129-250.compute-1.amazonaws.com:8081
  7. > User-Agent: curl/7.54.0
  8. > Accept: */*
  9. > Content-type: application/json
  10. >
  11. < HTTP/1.1 403
  12. < X-Content-Type-Options: nosniff
  13. < X-XSS-Protection: 1; mode=block
  14. < Cache-Control: no-cache, no-store, max-age=0, must-revalidate
  15. < Pragma: no-cache
  16. < Expires: 0
  17. < X-Frame-Options: DENY
  18. < Content-Type: application/json;charset=UTF-8
  19. < Transfer-Encoding: chunked
  20. < Date: Fri, 04 Oct 2019 05:44:50 GMT
  21. <
  22. * Connection #0 to host ec2-54-92-129-250.compute-1.amazonaws.com left intact
  23. {"timestamp":"2019-10-04T05:44:50.260+0000","status":403,"error":"Forbidden","message":"Access Denied","path":"/api/user/Raj4"}%

4) On giving Authorization header, /api/user/<userid> is success.

  1. curl -v -H 'Content-type: application/json' ec2-54-92-129-250.compute-1.amazonaws.com:8081/api/user/Raj4 -H 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJSYWo0IiwiZXhwIjoxNTcxMDMxODE4fQ.xG0IYe1hE55eOF5HkycpivsV1G0MTK6y3MN2Sf5j9NyF6WZ4UzfPyg5DzsazdiWsX49MvIfD26OK0dgkOPK_zw'
  2. * Trying 54.92.129.250...
  3. * TCP_NODELAY set
  4. * Connected to ec2-54-92-129-250.compute-1.amazonaws.com (54.92.129.250) port 8081 (#0)
  5. > GET /api/user/Raj4 HTTP/1.1
  6. > Host: ec2-54-92-129-250.compute-1.amazonaws.com:8081
  7. > User-Agent: curl/7.54.0
  8. > Accept: */*
  9. > Content-type: application/json
  10. > Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJSYWo0IiwiZXhwIjoxNTcxMDMxODE4fQ.xG0IYe1hE55eOF5HkycpivsV1G0MTK6y3MN2Sf5j9NyF6WZ4UzfPyg5DzsazdiWsX49MvIfD26OK0dgkOPK_zw
  11. >
  12. < HTTP/1.1 200
  13. < X-Content-Type-Options: nosniff
  14. < X-XSS-Protection: 1; mode=block
  15. < Cache-Control: no-cache, no-store, max-age=0, must-revalidate
  16. < Pragma: no-cache
  17. < Expires: 0
  18. < X-Frame-Options: DENY
  19. < Content-Type: application/json;charset=UTF-8
  20. < Transfer-Encoding: chunked
  21. < Date: Fri, 04 Oct 2019 05:47:05 GMT
  22. <
  23. * Connection #0 to host ec2-54-92-129-250.compute-1.amazonaws.com left intact
  24. {"id":1,"username":"Raj4"}%

Configuring splunk

Go to http://your-hostname:8000/ to configure splunk
1) You will be prompted with below page with initial admin password. Change passowrd after login.
2) If you choose aws ec2 with minimal configuration which only gave 8 GB hard disk space, you will end up with below issue. For example - In my case, I only have 2.7 GB left.

  1. [root@ip-172-31-47-128 _data]# df -h
  2. Filesystem Size Used Avail Use% Mounted on
  3. devtmpfs 483M 60K 483M 1% /dev
  4. tmpfs 493M 0 493M 0% /dev/shm
  5. /dev/xvda1 7.9G 5.1G 2.7G 66% /


3) To resolve this

a) login to splunk container using docker exec -it <splunk-container-id> bash

b) do apt-get update and apt-get install vim

c) do vi ./etc/system/default/server.conf change minFreeSpace from 5000 (i.e 5GiB) to 1000 (i.e 1GiB).

4) After above settings, restart splunk which should resolve above issue and on click data summary you should see splunkforwarder like below
5) Configure splunk alert
6) Configure splunk dashboard

Contributing

This repository is done as part of Udacity Java developer. Therefore, most likely will not accept any pull requests.