diff --git a/ami/golden_org/linux_hardening/playbook.yml b/ami/golden_org/linux_hardening/playbook.yml new file mode 100644 index 00000000..831f338a --- /dev/null +++ b/ami/golden_org/linux_hardening/playbook.yml @@ -0,0 +1,6 @@ +--- +- name: os-audit + hosts: localhost + become: true + roles: + - role: osm_linux_armour \ No newline at end of file diff --git a/ami/golden_org/linux_hardening/requirements.yml b/ami/golden_org/linux_hardening/requirements.yml new file mode 100644 index 00000000..7fd7dca2 --- /dev/null +++ b/ami/golden_org/linux_hardening/requirements.yml @@ -0,0 +1,4 @@ +- name: osm_linux_armour + src: https://github.com/OT-OSM/linux_armour.git + scm: git + version: master \ No newline at end of file diff --git a/ami/golden_org/packer_build.json b/ami/golden_org/packer_build.json new file mode 100644 index 00000000..7830adf9 --- /dev/null +++ b/ami/golden_org/packer_build.json @@ -0,0 +1,49 @@ +{ + "builders": [ + { + "type": "amazon-ebs", + "access_key": "{{user `access_key`}}", + "secret_key": "{{user `secret_key`}}", + "region": "{{user `region`}}", + "source_ami": "{{user `source_ami`}}", + "instance_type": "{{user `instance_type`}}", + "ssh_username": "{{user `ssh_username`}}", + "ami_name": "{{user `ami_name`}}-{{timestamp}}", + "vpc_id": "{{user `vpc_id`}}", + "subnet_id": "{{user `subnet_id`}}", + "security_group_id": "{{user `security_group_id`}}", + "skip_region_validation": true, + "associate_public_ip_address": true, + "tags": { + "Name": "{{user `ami_name`}}-{{timestamp}}", + "Os_hardening" : "true", + "Environment": "global", + "Owner": "devops", + "Type": "golden" + } + } + ], + + "provisioners": [ + + { + "type": "shell", + "inline": ["sleep 30"] + }, + + + { + "type": "file", + "source": "linux_hardening", + "destination": "/tmp/linux_hardening" + }, + + { + "type": "ansible-local", + "galaxy_command": "ansible-galaxy", + "galaxy_file": "linux_hardening/requirements.yml", + "playbook_file": "linux_hardening/playbook.yml" + } + + ] + } diff --git a/ami/golden_org/variable.json b/ami/golden_org/variable.json new file mode 100644 index 00000000..8160bc02 --- /dev/null +++ b/ami/golden_org/variable.json @@ -0,0 +1,11 @@ +{ + "region": "ap-south-1", + "source_ami": "ami-024c319d5d14b463e", + "instance_type": "t2.micro", + "ssh_username": "ubuntu", + "ami_name": "golden-ami", + "vpc_id": "vpc-0928ca75a3d30c971", + "subnet_id": "subnet-086ad3db8e3652bba", + "security_group_ids": "sg-01a18e59e1b026d8f", + "ami_virtualization_type": "hvm" +} diff --git a/build.gradle b/build.gradle new file mode 100644 index 00000000..ba439e7b --- /dev/null +++ b/build.gradle @@ -0,0 +1,78 @@ +apply plugin: 'java' +apply plugin: 'maven' + +group = 'Spring3HibernateApp' +version = '1.8-SNAPSHOT' + +description = """Spring3HibernateApp""" + +sourceCompatibility = 1.6 +targetCompatibility = 1.6 + + + +repositories { + + maven { url "https://repo.maven.apache.org/maven2" } +} +dependencies { + compile group: 'org.mockito', name: 'mockito-all', version:'1.9.5' + compile group: 'javax.activation', name: 'activation', version:'1.1' + compile group: 'antlr', name: 'antlr', version:'2.7.6' + compile group: 'aopalliance', name: 'aopalliance', version:'1.0' + compile group: 'asm', name: 'asm', version:'1.5.3' + compile group: 'asm', name: 'asm-attrs', version:'1.5.3' + compile group: 'cglib', name: 'cglib', version:'2.1_3' + compile group: 'commons-beanutils', name: 'commons-beanutils', version:'1.7.0' + compile group: 'commons-io', name: 'commons-io', version:'1.1' + compile group: 'commons-logging', name: 'commons-logging', version:'1.1.1' + compile group: 'commons-lang', name: 'commons-lang', version:'2.5' + compile group: 'commons-collections', name: 'commons-collections', version:'2.1.1' + compile group: 'commons-fileupload', name: 'commons-fileupload', version:'1.1.1' + compile group: 'org.apache.commons', name: 'commons-email', version:'1.1' + compile group: 'commons-digester', name: 'commons-digester', version:'1.8' + compile group: 'dom4j', name: 'dom4j', version:'1.6.1' + compile group: 'dumbster', name: 'dumbster', version:'1.6' + compile group: 'ehcache', name: 'ehcache', version:'1.2.3' + compile group: 'icu4j', name: 'icu4j', version:'2.6.1' + compile group: 'javassist', name: 'javassist', version:'3.4.GA' + compile group: 'javax.xml.bind', name: 'jaxb-api', version:'2.1' + compile group: 'javax.xml', name: 'jaxb-impl', version:'2.1' + compile group: 'jaxen', name: 'jaxen', version:'1.1.1' + compile group: 'jdom', name: 'jdom', version:'1.0' + compile group: 'javax.transaction', name: 'jta', version:'1.1' + compile group: 'jstl', name: 'jstl', version:'1.2' + compile group: 'log4j', name: 'log4j', version:'1.2.14' + compile group: 'org.apache.lucene', name: 'lucene-core', version:'2.3.2' + compile group: 'org.apache.lucene', name: 'lucene-highlighter', version:'2.0.0' + compile group: 'javax.mail', name: 'mail', version:'1.4' + compile group: 'org.slf4j', name: 'slf4j-api', version:'1.5.6' + compile group: 'mysql', name: 'mysql-connector-java', version:'5.0.5' + compile group: 'org.slf4j', name: 'slf4j-log4j12', version:'1.5.6' + compile group: 'javax.persistence', name: 'persistence-api', version:'1.0' + compile group: 'quartz', name: 'quartz', version:'1.5.2' + compile group: 'javax.xml.stream', name: 'stax-api', version:'1.0-2' + compile group: 'javax.validation', name: 'validation-api', version:'1.0.0.GA' + compile group: 'xalan', name: 'xalan', version:'2.6.0' + compile group: 'xerces', name: 'xercesImpl', version:'2.6.2' + compile group: 'xml-apis', name: 'xml-apis', version:'1.3.02' + compile group: 'xerces', name: 'xmlParserAPIs', version:'2.6.2' + compile group: 'xom', name: 'xom', version:'1.0' + compile group: 'org.hibernate', name: 'hibernate-annotations', version:'3.3.1.GA' + compile group: 'org.hibernate', name: 'hibernate-entitymanager', version:'3.3.2.GA' + compile group: 'org.hibernate', name: 'hibernate-validator', version:'4.0.2.GA' + compile group: 'org.hibernate', name: 'hibernate', version:'3.2.6.ga' + compile group: 'org.hibernate.common', name: 'hibernate-commons-annotations', version:'4.0.0.Final' + compile group: 'org.hibernate', name: 'hibernate-search', version:'3.0.0.GA' + compile group: 'org.springframework', name: 'spring-webmvc', version:'3.0.1.RELEASE' + compile group: 'org.springframework', name: 'spring-context', version:'3.0.1.RELEASE' + compile group: 'org.springframework', name: 'spring-core', version:'3.0.1.RELEASE' + compile group: 'org.springframework', name: 'spring-beans', version:'3.0.1.RELEASE' + compile group: 'org.springframework', name: 'spring-aop', version:'3.0.1.RELEASE' + compile group: 'org.springframework', name: 'spring-tx', version:'3.0.1.RELEASE' + compile group: 'org.springframework', name: 'spring-orm', version:'3.0.1.RELEASE' + compile group: 'org.springframework', name: 'spring-expression', version:'3.0.1.RELEASE' + compile group: 'org.springframework', name: 'spring-asm', version:'3.0.1.RELEASE' + compile group: 'junit', name: 'junit', version:'4.4' + providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version:'3.1.0' +} diff --git a/builder/Dockerfile b/builder/Dockerfile new file mode 100644 index 00000000..9fd607c8 --- /dev/null +++ b/builder/Dockerfile @@ -0,0 +1,7 @@ +FROM maven:3.3-jdk-8 +RUN apt-get update || true +RUN apt-get install jq +ENV SLEEP_DURATION 5s +COPY build.sh . +ENV ACTIVITY_SUB_TASK_CODE MVN_EXECUTE +ENTRYPOINT [ "./build.sh" ] \ No newline at end of file diff --git a/builder/build.sh b/builder/build.sh new file mode 100755 index 00000000..662859bf --- /dev/null +++ b/builder/build.sh @@ -0,0 +1,28 @@ +#!/bin/bash +source functions.sh + +echo "I'll build the code" +sleep $SLEEP_DURATION + +cd $CODEBASE_DIR +mvn $INSTRUCTION +if [ $? -eq 0 ] +then + generateOutput mvn_execute true "dhek chal gya" + echo "build sucessfull" +elif [ $? != 0 ] +then + generateOutput mvn_execute false "nhi chala" + echo "build unsucessfull" +fi + + + + + + + + + + + diff --git a/builder/functions.sh b/builder/functions.sh new file mode 100644 index 00000000..d0d6d2a2 --- /dev/null +++ b/builder/functions.sh @@ -0,0 +1,9 @@ +generateOutput() { + Task=$1 + Status=$2 + Message=$3 + OUTPUT_DIR=/src/${EXECUTION_DIR}/${EXECUTION_TASK_ID} + mkdir -p ${OUTPUT_DIR} + echo "{ \"${Task}\": {\"status\": \"${Status}\", \"message\": \"${Message}\"}}" | jq . > ${OUTPUT_DIR}/summary.json + echo "{ \"status\": \"${Status}\", \"message\": \"${Message}\"}" | jq . > ${OUTPUT_DIR}/${Task}.json +} diff --git a/doc/dependency_decisions.yml b/doc/dependency_decisions.yml new file mode 100644 index 00000000..5134452f --- /dev/null +++ b/doc/dependency_decisions.yml @@ -0,0 +1,61 @@ +--- +- - :permit + - MIT + - :who: + :why: + :versions: [] + :when: 2022-10-17 06:50:29.255601214 Z +- - :permit + - Apache 2.0 + - :who: + :why: + :versions: [] + :when: 2022-10-17 06:54:27.256049185 Z +- - :permit + - Common Development and Distribution License 1.0 + - :who: + :why: + :versions: [] + :when: 2022-10-17 06:55:07.637942543 Z +- - :permit + - The GNU General Public License, Version 2 + - :who: + :why: + :versions: [] + :when: 2022-10-17 06:55:26.727717107 Z +- - :permit + - LGPL + - :who: + :why: + :versions: [] + :when: 2022-10-17 06:55:58.022044091 Z +- - :permit + - GNU LESSER GENERAL PUBLIC LICENSE + - :who: + :why: + :versions: [] + :when: 2022-10-17 06:56:09.078335004 Z +- - :permit + - Common Public License Version 1.0 + - :who: + :why: + :versions: [] + :when: 2022-10-17 06:56:20.370905240 Z +- - :permit + - CDDL + GPLv2 with classpath exception + - :who: + :why: + :versions: [] + :when: 2022-10-17 06:56:32.140297451 Z +- - :permit + - GNU Lesser General Public License, version 2.1 + - :who: + :why: + :versions: [] + :when: 2022-10-17 06:57:00.424897761 Z +- - :permit + - Public Domain + - :who: + :why: + :versions: [] + :when: 2022-10-17 06:57:09.597364315 Z diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..d9b7505b Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..45a4d68f --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionUrl=https\://services.gradle.org/distributions/gradle-4.4.1-bin.zip +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStorePath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME diff --git a/gradlew b/gradlew new file mode 100755 index 00000000..cccdd3d5 --- /dev/null +++ b/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 00000000..e95643d6 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 00000000..0ade3442 --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'Spring3HibernateApp' diff --git a/sonar.properties b/sonar.properties new file mode 100644 index 00000000..1b1e3cea --- /dev/null +++ b/sonar.properties @@ -0,0 +1,4 @@ +sonar.host.url=https://sonarcloud.io/ +sonar.projectKey=s3ha +sonar.organization=ot-bp +sonar.java.binaries=target diff --git a/src/main/java/com/sample/controller/LoadSimulationController.java b/src/main/java/com/sample/controller/LoadSimulationController.java new file mode 100644 index 00000000..dc773c2c --- /dev/null +++ b/src/main/java/com/sample/controller/LoadSimulationController.java @@ -0,0 +1,45 @@ +package com.sample.controller; + +import org.apache.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import com.sample.service.LoadSimulatorService; + + + + +@Controller +public class LoadSimulationController { + + @Autowired + private LoadSimulatorService loadSimulatorService; + + @RequestMapping(value="/controlled/startCpuLoad", method = RequestMethod.GET) + @ResponseBody + public String increaseCpuLoadControlled(@RequestParam int percentage, @RequestParam int duration, @RequestParam int threadCount) { + System.out.println("increaseCpuLoad method called."); + return loadSimulatorService.increaseCpuLoad(percentage, duration, threadCount, true); + } + + @RequestMapping(value="/uncontrolled/startCpuLoad", method = RequestMethod.GET) + @ResponseBody + public String increaseCpuLoadUncontrolled(@RequestParam int duration, @RequestParam int threadCount) { + System.out.println("increaseCpuLoad method called."); + return loadSimulatorService.increaseCpuLoad(0, duration, threadCount, false); + } + + + @RequestMapping(value="/stopCpuLoad", method = RequestMethod.GET) + @ResponseBody + public String stopCpuLoad() { + System.out.println("stopCpuLoad method called."); + String response = loadSimulatorService.stopCpuLoad(); + return response; + } + +} diff --git a/src/main/java/com/sample/service/LoadSimulatorService.java b/src/main/java/com/sample/service/LoadSimulatorService.java new file mode 100644 index 00000000..a4004d54 --- /dev/null +++ b/src/main/java/com/sample/service/LoadSimulatorService.java @@ -0,0 +1,7 @@ +package com.sample.service; + +public interface LoadSimulatorService { + + public String increaseCpuLoad(int percentage, int duration , int threads, boolean isControlled); + public String stopCpuLoad(); +} diff --git a/src/main/java/com/sample/service/LoadSimulatorServiceImpl.java b/src/main/java/com/sample/service/LoadSimulatorServiceImpl.java new file mode 100644 index 00000000..7184e5bb --- /dev/null +++ b/src/main/java/com/sample/service/LoadSimulatorServiceImpl.java @@ -0,0 +1,109 @@ +package com.sample.service; + +import org.springframework.stereotype.Service; + +@Service("LoadSimulatorService") +public class LoadSimulatorServiceImpl implements LoadSimulatorService { + + private volatile boolean cpuLoadEnabled = false; + private static final int NUM_CORES = Runtime.getRuntime().availableProcessors(); + private volatile int count = 0; + + @Override + public String increaseCpuLoad(int percentage, int duration, int threadCount, boolean control) { + + System.out.println("ServiceImpl increaseCpuLoad called"); + cpuLoadEnabled = true; + if (percentage < 0 || percentage > 100) { + return "Percentage must be between 0 and 100"; + } + if (duration <= 0) { + return "Duration must be greater than 0"; + } + final int loadPercentage = percentage; + final int loadDuration = duration; + + + // Calculate the maximum number of threads based on available memory + long maxMemory = Runtime.getRuntime().maxMemory(); + long usedMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); + long availableMemory = maxMemory - usedMemory; + + // Approximate memory usage per thread (this is a rough estimate) + long memoryPerThread = 1 * 1024 * 1024; // 1 MB per thread + int maxThreadsBasedOnMemory = (int) (availableMemory / memoryPerThread); + + // Calculate the final number of threads to use + int numThreads = Math.min(threadCount, maxThreadsBasedOnMemory); + numThreads = Math.max(numThreads, 1); // Ensure at least one thread is used + + System.out.println("Number of cores: " + NUM_CORES + ", Requested threads: " + threadCount + + ", Adjusted threads: " + numThreads + ", Max threads based on memory: " + maxThreadsBasedOnMemory); + final boolean isControlled = control; + for (int i = 0; i < numThreads; i++) { + new Thread(new Runnable() { + public void run() { + try { + System.out.println("Thread ID: " + Thread.currentThread().getId()); + if(isControlled) { + generateControlledCpuLoad(loadPercentage, loadDuration); + }else { + generateUncontrolledCpuLoad(loadDuration); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }).start(); + } + if(!isControlled) { + return "CPU load increased for "+ duration + " seconds"; + }else { + return "CPU load increased by " + percentage + "% for " + duration + " seconds"; + } + + } + + @Override + public String stopCpuLoad() { + System.out.println("ServiceImpl stopCpuLoad called"); + cpuLoadEnabled = false; + count = 0; + return "CPU load stopped."; + } + + private void generateUncontrolledCpuLoad(int duration) throws InterruptedException { + + long endTime = System.currentTimeMillis() + duration * 1000L; + + while (System.currentTimeMillis() < endTime && cpuLoadEnabled) { + count++; + if (!cpuLoadEnabled) { + System.out.println("Cpu load disabled/stop is called"); + return; // Exit if CPU load is disabled + } + } + } + + private void generateControlledCpuLoad(int percentage, int duration) throws InterruptedException { + long endTime = System.currentTimeMillis() + duration * 1000L; + long busyTime = (long) (10 * percentage); // Busy time in milliseconds + long idleTime = 10 - busyTime; // Idle time in milliseconds + + while (System.currentTimeMillis() < endTime && cpuLoadEnabled) { + long startTime = System.currentTimeMillis(); + + // Busy loop for the calculated busy time + while ((System.currentTimeMillis() - startTime) < busyTime) { + count ++; + System.out.println("Cpu is busy"); + } + + // Sleep for the calculated idle time + if (idleTime > 0) { + + Thread.sleep(idleTime); + } + } + } +} \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 407170e1..052dd7e3 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -45,6 +45,11 @@ sdnext *.html + + + sdnext + / + index.html diff --git a/terraform/eks/provider.tf b/terraform/eks/provider.tf new file mode 100644 index 00000000..c28c97ad --- /dev/null +++ b/terraform/eks/provider.tf @@ -0,0 +1,3 @@ +provider "aws" { + region = "us-west-2" +} diff --git a/terraform/eks/terraform.tfvars b/terraform/eks/terraform.tfvars new file mode 100644 index 00000000..65e84a27 --- /dev/null +++ b/terraform/eks/terraform.tfvars @@ -0,0 +1,17 @@ +config_output_path = "config" +eks_cluster_version = "1.21" +endpoint_private = false +endpoint_public = true +kubeconfig_name = "config" +eks_subnets = ["subnet-06e41801e9618a572", "subnet-020af10ef815e5c07"] +ssh_key = "oregon" +node_sg = ["sg-02b42751eab13f53a", "sg-0a80c39996b1d8abc"] +instance_type = ["t2.small"] +desired_capacity = 1 +disk_size = 20 +max_capacity = 1 +min_capacity = 1 +capacity_type = "ON_DEMAND" +capacity_type_2 = "SPOT" +subnets = ["subnet-0558372d5ba749417", "subnet-08af060aeb20af40c"] +vpc_id = "vpc-04d796513538107d5" diff --git a/terraform/elasticache/_override.tf b/terraform/elasticache/_override.tf new file mode 100644 index 00000000..06be56b5 --- /dev/null +++ b/terraform/elasticache/_override.tf @@ -0,0 +1,3 @@ +locals { + security_group_ids = "${data.terraform_remote_state.network.outputs.web_sg_id}" +} diff --git a/terraform/elasticache/backend.tf b/terraform/elasticache/backend.tf new file mode 100644 index 00000000..9d19eed5 --- /dev/null +++ b/terraform/elasticache/backend.tf @@ -0,0 +1,7 @@ +terraform { + backend "s3" { + bucket = "bp-step-tfstate" + key = "elasticache/terraform.tfstate" + region = "us-west-2" + } +} diff --git a/terraform/elasticache/data.tf b/terraform/elasticache/data.tf new file mode 100644 index 00000000..6ba84d9d --- /dev/null +++ b/terraform/elasticache/data.tf @@ -0,0 +1,9 @@ +data "terraform_remote_state" "network" { + backend = "s3" + + config = { + bucket = "bp-step-tfstate" + key = "network/terraform.tfstate" + region = "us-west-2" + } + diff --git a/terraform/elasticache/provider.tf b/terraform/elasticache/provider.tf new file mode 100644 index 00000000..0152dd66 --- /dev/null +++ b/terraform/elasticache/provider.tf @@ -0,0 +1,3 @@ +provider "aws" { + region = "us-west-2" +} \ No newline at end of file diff --git a/terraform/elasticache/terraform.tfvars b/terraform/elasticache/terraform.tfvars new file mode 100644 index 00000000..1773846e --- /dev/null +++ b/terraform/elasticache/terraform.tfvars @@ -0,0 +1,3 @@ +name = "elasticache-cluster" +security_group_ids = ["sg-02b42751eab13f53a", "sg-0a80c39996b1d8abc"] +subnet_ids = ["subnet-06e41801e9618a572", "subnet-020af10ef815e5c07"] \ No newline at end of file diff --git a/terraform/msk/_override.tf b/terraform/msk/_override.tf new file mode 100644 index 00000000..04e6d04e --- /dev/null +++ b/terraform/msk/_override.tf @@ -0,0 +1,7 @@ +locals { + vpc_id = "${data.terraform_remote_state.network.outputs.vpc_id}" + kafka_SG_id = "${data.terraform_remote_state.network.outputs.web_sg_id}" + subnet_ids = "${data.terraform_remote_state.network.outputs.public_subnet_ids}" +} + + diff --git a/terraform/msk/backend.tf b/terraform/msk/backend.tf new file mode 100644 index 00000000..6fc56503 --- /dev/null +++ b/terraform/msk/backend.tf @@ -0,0 +1,7 @@ +terraform { + backend "s3" { + bucket = "bp-step-tfstate" + key = "msk/terraform.tfstate" + region = "us-west-2" + } +} \ No newline at end of file diff --git a/terraform/msk/data_network.tf b/terraform/msk/data_network.tf new file mode 100644 index 00000000..201213a0 --- /dev/null +++ b/terraform/msk/data_network.tf @@ -0,0 +1,9 @@ +data "terraform_remote_state" "network" { + backend = "s3" + + config = { + bucket = "bp-step-tfstate" + key = "network/terraform.tfstate" + region = "us-west-2" + } +} \ No newline at end of file diff --git a/terraform/msk/provider.tf b/terraform/msk/provider.tf new file mode 100644 index 00000000..ce6d23d2 --- /dev/null +++ b/terraform/msk/provider.tf @@ -0,0 +1,3 @@ +provider "aws" { + region = "us-west-2" +} diff --git a/terraform/msk/terraform.tfvars b/terraform/msk/terraform.tfvars new file mode 100644 index 00000000..3185fcaf --- /dev/null +++ b/terraform/msk/terraform.tfvars @@ -0,0 +1,50 @@ +## Kafka Custom KMS Key +create_kafka_kms_key_id = true +kafka_key_usage = "ENCRYPT_DECRYPT" +kafka_customer_master_key_spec = "SYMMETRIC_DEFAULT" +kafka_multi_region = false +kafka_deletion_window_in_days = null +kafka_is_enabled = true +kafka_enable_key_rotation = false +kafka_kms_key_name = "msk-kafka-key" +## Kafka node and cluster config +name_prefix = "kafka-cluster" +vpc_id = "vpc-04d796513538107d5" +subnet_ids = ["subnet-0558372d5ba749417", "subnet-08af060aeb20af40c"] +kafka_SG_id = ["sg-02b42751eab13f53a", "sg-0a80c39996b1d8abc"] +kafka_tag_key = "user" +kafka_tag_value = "devops" +kafka_version = "2.6.2" +kafka_broker_number = 2 +kafka_instance_type = "kafka.m5.xlarge" +## Monitoring +kafka_monitoring_level = "PER_TOPIC_PER_BROKER" +enable_jmx_exporter = true +enable_node_exporter = true +## Storage +kafka_encryption_in_transit = "TLS" +kafka_ebs_volume_size = "100" +## Storage auto scaling +enable_storage_autoscaling = false +scaling_max_capacity = "200" +scaling_target_value = "60" +## Logging +enable_logging_info_cloudwatch = false +cloudwatch_log_group = null +enable_logging_info_firehouse = false +firehose_delivery_stream = null +enable_logging_info_s3 = false +s3_bucket_id = null +## Kafka Custom config file +tags = {} +kafka_custom_config_auto_create_topics = true +kafka_custom_config_delete_topics = false +kafka_custom_config_default_replication_factor = 2 +kafka_custom_config_min_insync_replicas = 1 +kafka_custom_config_num_io_threads = 8 +kafka_custom_config_num_network_threads = 5 +kafka_custom_config_num_partitions = 2 +kafka_custom_config_num_replica_fetchers = 2 +kafka_custom_config_socket_request_max_bytes = 104857600 +kafka_custom_config_unclean_leader_election_enable = true +kafka_custom_config_offsets_topic_replication_factor = 1 \ No newline at end of file diff --git a/terraform/network_skeleton/provider.tf b/terraform/network_skeleton/provider.tf new file mode 100644 index 00000000..ce6d23d2 --- /dev/null +++ b/terraform/network_skeleton/provider.tf @@ -0,0 +1,3 @@ +provider "aws" { + region = "us-west-2" +} diff --git a/terraform/network_skeleton/terraform.tfvars b/terraform/network_skeleton/terraform.tfvars new file mode 100644 index 00000000..7a161673 --- /dev/null +++ b/terraform/network_skeleton/terraform.tfvars @@ -0,0 +1,14 @@ +alb_name = "t-alb" +avaialability_zones = ["us-west-2a", "us-west-2b"] +igw_name = "t-igw" +logs_bucket = "bp-infra" +logs_bucket_arn = "arn:aws:s3:::bp-infra" +nat_name = "t-nat" +private_subnets_cidr = ["10.0.64.0/18", "10.0.128.0/17"] +pub_rt_name = "pub-t" +pub_subnet_name = "public-t" +public_subnets_cidr = ["10.0.0.0/19", "10.0.32.0/19"] +public_web_sg_name = "ns-web-sg01" +pvt_rt_ame = "pvt-t" +pvt_subnet_name = "private-t" +pvt_zone_name = "abc.pqr.in" diff --git a/terraform_code/backend.tf b/terraform_code/backend.tf new file mode 100644 index 00000000..b4942bb7 --- /dev/null +++ b/terraform_code/backend.tf @@ -0,0 +1,7 @@ +terraform { + backend "s3" { + bucket = "bp-tf-states" + key = "env/testing/bp.tfstate" + region = "ap-south-1" + } +} diff --git a/terraform_code/main.tf b/terraform_code/main.tf new file mode 100644 index 00000000..d6bda473 --- /dev/null +++ b/terraform_code/main.tf @@ -0,0 +1,135 @@ +provider "aws" { + region = var.region + default_tags { + tags = var.default_tags + } + + +} +data "aws_ami" "java-spring3Hibernateapplication" { +most_recent = true +owners = ["062711695447"] # Canonical + + filter { + name = "tag:Name" + values = ["spring3hibernate-ami-*"] + } + + filter { + name = "virtualization-type" + values = ["hvm"] + } + filter { + name = "tag:Type" + values = ["java application"] + } +} + + +data "http" "ip" { + url = "https://ifconfig.me" +} + +module "vpc" { + source = "terraform-aws-modules/vpc/aws" + name = var.vpc_name + cidr = var.vpc_cidr + azs = var.az + private_subnets = var.private_subnets + public_subnets = var.public_subnets + enable_nat_gateway = var.enable_nat_gateway + +} + + +module "sonarqube-bastion-security-group" { + source = "terraform-aws-modules/security-group/aws" + name = var.bastion_sg_name + description = "Security group for user-service with custom ports open within VPC" + vpc_id = module.vpc.vpc_id + ingress_cidr_blocks = [join("/", [data.http.ip.body, "32"])] + ingress_rules = ["ssh-tcp", ] + egress_rules = ["all-all"] + ingress_with_cidr_blocks = var.ingress_with_cidr_blocks + +} + +module "ec2_instance_public" { + source = "terraform-aws-modules/ec2-instance/aws" + count = length(module.vpc.public_subnets) + ami = data.aws_ami.java-spring3Hibernateapplication.id + instance_type = var.public_ec2_type + subnet_id = module.vpc.public_subnets[0] + vpc_security_group_ids = [module.sonarqube-bastion-security-group.security_group_id] + key_name = var.key_name + name = tostring(join("_", [var.public_instance_name,count.index])) + iam_instance_profile = var.profile_name + + +} + + +module "sonarqube-private-security-group" { + source = "terraform-aws-modules/security-group/aws" + vpc_id = module.vpc.vpc_id + ingress_cidr_blocks = var.public_subnets + ingress_rules = ["ssh-tcp"] + egress_rules = ["all-all"] + ingress_with_cidr_blocks = var.ingress_with_cidr_blocks_database + name = var.private_sg_name + + +} + +module "ec2_instance_private" { + source = "terraform-aws-modules/ec2-instance/aws" + count = length(module.vpc.private_subnets) + ami = data.aws_ami.java-spring3Hibernateapplication.id + instance_type = var.private_ec2_type + subnet_id = module.vpc.private_subnets[count.index] + vpc_security_group_ids = [module.sonarqube-private-security-group.security_group_id] + key_name = var.key_name + name = tostring(join("_", [var.private_instance_name, count.index])) + +} + + + +module "alb" { + source = "terraform-aws-modules/alb/aws" + version = "~> 6.0" + + name = "my-alb" + + load_balancer_type = "application" + + vpc_id = module.vpc.vpc_id + subnets = [module.vpc.public_subnets[0], module.vpc.public_subnets[1]] + security_groups = [module.sonarqube-bastion-security-group.security_group_id] + + + target_groups = [ + { + name_prefix = "pref-" + backend_protocol = "HTTP" + backend_port = 9000 + target_type = "instance" + targets = [ + { + target_id = module.ec2_instance_private[0].id + } + + ] + } + ] + + #http_listeners = var.http_listeners + + + http_tcp_listeners = var.http_tcp_listeners + + + tags = { + Environment = "Test" + } +} diff --git a/terraform_code/output.tf b/terraform_code/output.tf new file mode 100644 index 00000000..71a59cdb --- /dev/null +++ b/terraform_code/output.tf @@ -0,0 +1,24 @@ +output "vpc" { + value = module.vpc.vpc_id +} + + +output "sonarqube-bastion-security-group" { + value = module.sonarqube-bastion-security-group +} + +output "sonarqube-private-security-group" { + value = module.sonarqube-private-security-group +} + +output "ec2_instance_private" { + value = module.ec2_instance_private +} + +output "ec2_instance_public_ip" { + value = module.ec2_instance_public +} + +output "ip" { + value = data.http.ip.body +} diff --git a/terraform_code/terraform.tfvars b/terraform_code/terraform.tfvars new file mode 100644 index 00000000..2aa0cccd --- /dev/null +++ b/terraform_code/terraform.tfvars @@ -0,0 +1,18 @@ +region = "ap-south-1" +vpc_name = "sonarqube_vpc" +vpc_cidr = "10.0.0.0/16" +az = ["ap-south-1a", "ap-south-1b"] +private_subnets = ["10.0.3.0/24"] +public_subnets = ["10.0.1.0/24","10.0.2.0/24"] +enable_nat_gateway = true +public_ec2_type = "t2.micro" +key_name = "grafana" +private_ec2_type = "t2.micro" +default_tags = { ENV = "sonarqube", Company = "Opstree", Owner = "Neha Sinha" } +public_instance_name = "bastion" +private_instance_name = "Sonarqube-server" +bastion_sg_name = "bastion-sg" +private_sg_name = "Sonarqube-private-sg" +ingress_with_cidr_blocks = [{ from_port = 9000, to_port = 9000, protocol = "tcp", description = "Sonarqube", cidr_blocks = "0.0.0.0/0" },{ from_port = 80, to_port = 80, protocol = "tcp", description = "tcp 80", cidr_blocks = "0.0.0.0/0" }] +ingress_with_cidr_blocks_database = [{ from_port = 9000, to_port = 9000, protocol = "tcp" ,cidr_blocks = "0.0.0.0/0"}] +profile_name = "kafka_role" diff --git a/terraform_code/var.tf b/terraform_code/var.tf new file mode 100644 index 00000000..9575bba7 --- /dev/null +++ b/terraform_code/var.tf @@ -0,0 +1,122 @@ +variable "region" { + type = string + default = "ap-south-1" + description = "Enter the region in which Infra will created" +} + +variable "vpc_name" { + type = string + default = "Sonarqube_vpc" + description = "Enter the VPC name" +} +variable "vpc_cidr" { + type = string + default = "10.0.0.0/16" + description = "Enter the vpc cidr" +} + +variable "bastion_sg_name" { + type = string + default = "Sonarqube-bastion-sg" + description = "Enter the security group name for bastion" +} + +variable "enable_nat_gateway" { + type = bool + default = true + description = "Enter the value for NAT true/false" +} +variable "az" { + type = list(string) + default = ["ap-south-1a", "ap-south-1b", "ap-south-1c"] + description = "Enter the Availability Zones" +} +variable "private_subnets" { + type = list(string) + default = ["10.0.3.0/24"] + description = "Enter the private subnets cidr" +} + +variable "public_subnets" { + type = list(string) + default = ["10.0.1.0/24","10.0.2.0/24"] + description = "Enter the public subnet cidr" +} + +variable "public_ec2_type" { + type = string + default = "t2.micro" + description = "Enter the public ec2 types" +} +variable "key_name" { + type = string + default = "ansiblehost" + description = "Enter the public instance key name" +} + + +variable "private_ec2_type" { + type = string + default = "t2.medium" + description = "Enter the private ec2 types" +} + +# variable "ami_id" { +# type = string +# default = "ami-0c1a7f89451184c8b" +# description = "Enter the ami_id" +# } + +variable "default_tags" { + type = map(any) + default = { ENV = "Sonarqube", Owner = "Neha Sinha", company = "Opstree" } + description = "Enter the tags" +} + +variable "public_instance_name" { + type = string + default = "Sonarqube-Bastion" + description = "Enter the bastion name" +} +variable "private_instance_name" { + type = string + default = "Sonarqube-server" + description = "Enter the bastion name" +} + +variable "private_sg_name" { + type = string + default = "Sonarqube-private-sg" + description = "Enter the private security group name" +} +locals { + ip = data.http.ip +} + + variable "ingress_with_cidr_blocks" { +type = list(any) +default = [{ from_port = 22, to_port = 22, protocol = "tcp", description = "Public Access", cidr_blocks = "0.0.0.0/0" }] +description = "Enter the ports and cidrs you want to open" +} + + +variable "ingress_with_cidr_blocks_database" { + type = list(any) + default = [{ from_port = 22, to_port = 22, protocol = "tcp", description = "By Bastion Access", cidr_blocks = "12.0.101.0/24" }] + description = "Enter the ports and cidrs you want to open" +} +#variable "http_listeners" { +#type = list(any) +#default = [{ port = 9000, protocol = "HTTP", target_group_index = 0 }] + +#} +variable "http_tcp_listeners" { + type = list(any) + default = [{ port = 80, protocol = "HTTP", target_group_index = 0 }] + +} +variable "profile_name" { + type = string + description = "Enter the profile name" + default = "sonarquberole" +} diff --git a/war_packer/packer_build.json b/war_packer/packer_build.json new file mode 100644 index 00000000..6df620d6 --- /dev/null +++ b/war_packer/packer_build.json @@ -0,0 +1,47 @@ +{ + + "builders": [ + { + "type": "amazon-ebs", + "access_key":"{{user `access_key`}}", + "secret_key":"{{user `secret_key`}}", + "region": "{{user `region`}}", + "source_ami": "{{user `source_ami`}}", + "instance_type": "{{user `instance_type`}}", + "ssh_username": "{{user `ssh_username`}}", + "ami_name": "{{user `ami_name`}}-{{timestamp}}", + "vpc_id": "{{user `vpc_id`}}", + "subnet_id": "{{user `subnet_id`}}", + "security_group_id": "{{user `security_group_id`}}", + "skip_region_validation": true, + "associate_public_ip_address": true, + "tags": { + "Name": "{{user `ami_name`}}-{{timestamp}}", + "Environment": "testing", + "Type": "java application" + } + } + ], + + "provisioners": [ + + { + "type": "shell", + "inline": ["sleep 30"] + }, + + { + "type": "file", + "source": "../target/Spring3HibernateApp.war", + "destination": "/tmp/" + }, + { + "type": "shell", + "inline": [ + "sudo mv /tmp/Spring3HibernateApp.war /home/ubuntu/Spring3HibernateApp.war" + ] + } + + + ] + } \ No newline at end of file diff --git a/war_packer/variable.json b/war_packer/variable.json new file mode 100644 index 00000000..bf4ffca6 --- /dev/null +++ b/war_packer/variable.json @@ -0,0 +1,11 @@ +{ + "region": "ap-south-1", + "source_ami": "ami-024c319d5d14b463e", + "instance_type": "t2.micro", + "ssh_username": "ubuntu", + "ami_name": "spring3hibernate-ami", + "vpc_id": "vpc-0928ca75a3d30c971", + "subnet_id": "subnet-086ad3db8e3652bba", + "security_group_ids": "sg-00bdb012b89ee9e6e", + "ami_virtualization_type": "hvm" +}