gingeleski / cucumber-spring-security-tests

Cucumber/BDD security tests example for Java (Spring Boot API).
9 stars 8 forks source link
application-security bdd-sample bdd-tests cucumber-jvm devsecops secdevops security security-automation spring-boot spring-boot-security

BDD security testing example for Java

Cucumber/BDD security tests example for Java (Spring Boot API).

This type of work has also been called "security-as-code" as written security requirements drive executable test logic.

There is a fairly comprehensive toy/sample application here to allow for realistic security testing.

Quickstart

Grab the code.

git clone https://github.com/gingeleski/cucumber-spring-security-tests.git
cd cucumber-spring-security-tests

To launch the API with a root address http://localhost:8080/ ...

# Windows
.\gradlew.bat bootRun

# Linux
./gradlew bootRun

To compile and then run the test suite...

# Windows
.\gradlew.bat build

# Linux
./gradlew build

If you encounter issues, see the next section for Vagrant info. That virtual machine will have all dependencies.

This project has been developed for Java 13 and very current Spring Boot dependencies (April 2020).

Vagrant

An Ubuntu 19.x virtual machine has been configured with Vagrant (2.2.7) for this project.

With Vagrant installed, you should be able to simply run vagrant up from the root of this project.

Notable dependencies and components in this image are as follows...

Development setup

Building on the Quickstart and Vagrant subsections, a suggested workflow for developers will be described here.

  1. Install IntelliJ IDEA if you don't already have it. Community edition is fine.
  2. Install Java 13 (JDK) locally if you don't already have it. This is for IntelliJ to point to and resolve dependencies, syntax.
  3. Clone this project code locally.
  4. Initialize the virtual machine for this project with Vagrant.
  5. Dynamically generate the IntelliJ project files on your local machine using the Gradle wrapper.
  6. Open IntelliJ and use that for your development needs.
  7. Use this project's virtual machine to build the code and run the test suite.
  8. Execute version control from your local machine where you presumably have Github credentials set up already.

Commands for steps 3 through 6 are as follows.

git clone https://github.com/gingeleski/cucumber-spring-security-tests.git
cd cucumber-spring-security-tests

vagrant up

# Windows
.\gradlew.bat idea
# Linux
./gradlew idea

# Windows
.\gradlew.bat openIdea
# Linux
./gradlew openIdea

Going into your virtual machine to do step 7 can be performed as follows.

# From your local machine, confirm your VM is running
vagrant status

# If it's NOT running then do...
vagrant up

# Go into the VM to run commands now
vagrant ssh

# Make working directory the mapped folder where Vagrantfile resides - so, this project folder
cd /vagrant

# Compile and test the code
gradle build

# View all other possible Gradle tasks
gradle tasks

RoomBook sample API

The toy/sample application "RoomBook" is a room reservation system, like corporate drones will be familiar with.

The API is documented in an OpenAPI version 3.0 spec that resides at /openapi.json.

Role-based access control

Below is an overall plan for authorization.

Note that the API in its current state may not support all of these actions.

Test data

"Canned data" comes from src/main/java/resources/import.sql, directed to an H2 (in-memory) relational database.

The following test users will be set up...

Username Password Authorization Activated
administrator admin ROLE_ADMIN True
employee password ROLE_EMPLOYEE True
disabled password ROLE_EMPLOYEE False
assistant password ROLE_ASSISTANT True
hrmanager password ROLE_HR_MGR True

API calls

These detailed calls represent the current state of the API.

Pay particular attention to any noted TODOs, and note there may be some lacking functionality versus the access control list.