VulnerabilityHistoryProject / vulnerability-history

vulnerabilityhistory.org
MIT License
33 stars 5 forks source link

Vulnerability History Project VHP Tests

Cite This Data


To cite the writing on vulnerabilityhistory.org, use: DOI

To cite data from a specific project, use:

Table of Contents


Getting Set Up


Normal Setup

Normally

  yarn install
  bundle install

Then, let's set up our database and load our data:

  rails db:schema:load
  rails data:clear data:all
  rails server

That second one can take a while. Once the server is up, go to http://localhost:3000 to see the site.

If you are working on the frontend, it's useful to run Webpacker's dev server:

  yarn watch

This speeds up rebuilding the frontend JS, CSS, etc. If you want to do a full build of webpack, do:

  yarn build

Windows Considerations

With Docker

Building the project with Docker is meant to be an easy setup so there aren't any OS-specific issues throughout the dev process. That being said, this way of developing still needs a lot of testing to make sure it's good to go, and I wouldn't really advise trying this unless you have some time to debug too.

Common Gotchas

Getting Started

You'll need docker and docker-compose on your machine to start. You can either do this by means of [Docker Desktop]() or through a package manager, though the former may be better for a visual aid - plus it comes with docker-compose.

Once this is done, go to the directory and type docker-compose build. You should see things happening on the terminal! This command builds the image we need to run the VHP container.

➜  vulnerability-history git:(system_tests_docker) ✗ docker-compose build
...
Successfully built 988dcf0b62ce
Successfully tagged vulnerability-history_app:latest

Once this command finishes up, it's time to pull from docker hub. Run docker-compose up in the same container. This pulls all other necessary images and starts them up.

Your next destination will be to localhost:2345, where our pgAdmin container is exposed. Here, you'll give the login credentials supplied in your docker-compose.yml file to log in to both pgAdmin and the server itself. In the future, these may be refactored into local environment variables, but for now, you can edit them in the file itself under the environment section of pgadmin and postgres.

Once logged in, you need to create two databases, vhp and vhp_test, and their respective users in the server, both with passwords supplied in your secrets.yml file, and the permission Can Log In.

The last thing to do is kill the containers with ^C and run two commands to build the database:

  docker-compose run --rm app rails db:schema:load
  docker-compose run --rm app rails data:clear data:all

These will spin up a temporary container to build the database. Once these processes are done, do one more docker-compose up. This time if you visit localhost:3000, there should be a VHP site waiting for you!

Now I'll go over each of the containers as they appear in docker-compose.yml.

These will spin up a temporary container to build the database. Once these processes are done, do one more docker-compose up. This time if you visit localhost:3000, there should be a VHP site waiting for you!

Now I'll go over each of the containers as they appear in docker-compose.yml.

Testing

Regular tests can be run by running docker-compose run --rm test <command>

This tells docker-compose to start up a test container that removes itself whenever done testing. The test command can be run just like normal after that.

System testing requires one extra tool. Any VNC client will do, but I use VNC Viewer. The system tests will run fine, however to actually see the browser (if you care), you'll need to be able to get into the VNC server running on the selenium container. The VNC server itself runs on port 5900, so you'll need to point the client to 0.0.0.0:5900. Now you can observe the browser during the system tests.

Running Commands

You've already seen an example of how to run commands inside a container. The preferred way of doing this is by using docker-compose run --rm <container_name> <command>. This spins up the container of your choice to run any command you supply it. It's like a one-time terminal command. The --rm flag is not required, but it's useful for not keeping around any containers that would be otherwise using memory.

If you prefer a more interactive shell, you can always connect to the container itself, either through Docker Desktop, or by connecting through your terminal.

Reference

This was pretty tricky to get working, if you care to know how to do this here are some articles that really helped me out.

Rails Development with Docker

Running a Rails App with Webpacker and Docker

Testing Rails Apps in Docker Containers

Using Rails 5.1 System Tests with Docker

How to run a Rails 6 application using Docker Compose

Finding Useful Commands

Having trouble installing pg gem?

As of January 2023, we had some new issues with installing the pg gem on Windows. Fortunately, the error message has recently been updated to direct you in what to do:

Be sure to check out the Ruby pg README and their issues for anything that might come up: https://github.com/ged/ruby-pg

Powershell not letting you run scripts?

This gives a good explanation: https://tecadmin.net/powershell-running-scripts-is-disabled-system/

One solution to that is to do your administrative installs using CMD instead of Powershell.

Finding useful commands

To find out useful commands for working with data:

rails -T

Rebuild the Database


If db/schema.rb has changed recently, you'll need to rebuild the database:

rails db:schema:load

Reload the Production Data


To load the production data from Chromium and HTTPD:

rails data:all

Testing out merging? This is helpful:

rails data:dev_all

This will set the environment variable VHP_DATA_BRANCH to dev, which means that vulnerabilities and vhp-mining will be downloaded using their dev branch.

Testing VHP


System Tests

System tests are actively being worked on and always improving. That being said, it is worth explaining how we do system tests at the moment.

As of Rails 5.1, system tests are a part of the framework itself using Capybara for testing. These system tests are meant to simulate user actions and navigation through the site to make sure everything performs at it should. These tests can be found in the test/system folder.

Because of some behavior issues with AJAX on our tests, we perform them against the running server as opposed to one spun up by Capybara. This means that to run system tests on your own, there should be a local server running for Capybara to find.

Useful Commands


  rails server
  rails console

Helpful Dev Tools


Here's a list of our favorite tools for developing this:

The CWE csv File


To download a copy of the CWE csv file, go to https://cwe.mitre.org/data/downloads.html and download the latest Research Concepts csv listed under 'Navigate CWE'.

Place the csv file in lib/taggers/resources/cwes.csv

Note: this is incomplete, and some CWEs are not included in this that we need. Running a data rebuild will tell you which ones we're missing. In that case, get the info manually and put them into the manual_cwes.csv file.

On line 677 of the file, near the end of the line, change the double quotes around "<script" to single quotes 'script'

IntelliJ Setup


In case you didn't know, if you are a student with an .edu email you can get the professional version of all JetBrains IDEs. You can read more about this here

With this in mind, I will be proceeding with project setup instructions for the IntelliJ Ultimate Edition. With the correct plugins installed, this IDE can function the same as all of JetBrains other language specific IDEs, such as PyCharm, WebStorm, and RubyMine.

Plugins

The first step to running VHP within IntelliJ is installing a Ruby plugin. The plugin menu can be found at Preferences -> Plugins. Within the Marketplace tab, search for Ruby by JetBrains s.r.o. Once this plugin is installed, you may or may not be required to restart your IDE.

Project Structure

With the Ruby plugin installed, you can now configure your project. Something to be aware of is that IntelliJ might have autogenerated an SDK, Module, and Run Configuration. It is entirely possible that these function correctly and you do not need to follow the steps outlined here. In my case, they were not autogenerated as I initially worked on VHP within IntelliJ. However, once I got IntelliJ Ultimate they were autogenerated. So keep this in mind!

SDK

Modules

Run Configuration

Rails Server
Rake

Updating the OpenAPI Spec

We're using the OpenAPI specification for defining our RESTFul API. This allows us to be put in various registries, as generate some nice docs for us, and some other potential good things.

Our specification is in public/openapi.yml, making it publicly available. So don't put any secrets in there!

We're using Redocly tools to generate our docs and do linting on our spec.

To install Redocly globally, run npm install --global @redocly/cli

Useful tools

OpenAPI is complicated. These guides and tools will help:

The Rails Doctrine

Recommended reading: https://rubyonrails.org/doctrine. In particular these two sections are important to VHP: