Skip to content

A simple web project to learn to train Checkmarx to identify SQLi in a custom Python data access layer

Notifications You must be signed in to change notification settings

pvtshdw/Checkmarx-Python-Trainer

Repository files navigation

Flask App with MySQL Database

Quick Start

  1. Run docker-compose up
  2. Visit http://localhost/ in your browser.

Description

Flask is a web development microframework for Python. It's efficient, easy-to-use, and easy-to-deploy!

I often use Flask when building small webservices or dynamic websites and feel like trying something fresh and different from PHP, my daily driver. Most webservices and sites require some sort of database to store data, so we need to create not one but two Docker containers, and link them together:

  1. Flask container (with Python).
  2. MySQL container

In early examples, we would build or import just one container and work with it to run some code. In real-world usage, you'll often need two, three, or even dozens of containers to support the project you're working on!

It would get quite unweildy to have to manage all of these containers with separate run and stop commands, so Docker has an answer: docker-compose.

Docker Compose

Notes:

Persisting Data

One thing repeated early and often in the world of Docker is data stored inside a container is not persisted. So... this creates quite the conundrum: what if you need data to persist after a container exits?

Traditionally, Docker allowed the use of 'data containers' which would contain a VOLUME and nothing else, based on a really tiny base image, then you use volumes_from or -v to link the volume at the specified path into a container.

In our case, we want to explicitly state that we want the path /var/lib/mysql to persist as a volume separate from the general db volume. This way if you build the Database container, store some data inside a MySQL database that's stored within that path, then exit, you can run the Database container again later, and the data you stored earlier won't have vanished!

We did this inside the docker-compose.yml file here:

services:
  ...
  db:
    ...
    volumes:
      - /var/lib/mysql

The official MySQL image adds it's own VOLUME directive in its Dockerfile, so it's not necessary for us to specify the volume in our own docker-compose configuration... but I like to be explicit about things like where my important data is stored!

After you bring up the containers, you can inspect the db container to ensure the volume is configured correctly: docker inspect flask_db_1 — this displays all the container info, including a list of 'Volumes'.

You can also mount a directory on the host as a vlume using the syntax [host-path]:[container-path], so if you want to mount a database directory in your project folder as /var/lib/mysql, change the the volume like so:

services:
  ...
  db:
    ...
    volumes:
      - ./database:/var/lib/mysql

If you change the volume settings, then you will need to stop any running containers, then rm them and rebuild before the changes take effect (remember, containers are immutable!).

Read more: Manage data in containers

Note for Mac/Windows users: If you encounter performance issues with your app running inside Docker containers, and your app has many (e.g. 1,000+) files, it could be related to some filesystem performance issues with current versions of Docker. See File access in mounted volumes extremely slow.

This is a hard problem to solve, though the situation should improve as time goes on. For now, find ways to use volumes that aren't shared to your host whenever possible. You can also look into using tools like docker-sync if you must sync large numbers of files.

About

A simple web project to learn to train Checkmarx to identify SQLi in a custom Python data access layer

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published