Skip to content

Simple and secure Python API for DevSecOps practices, featuring basic CRUD functionality

Notifications You must be signed in to change notification settings

lisazevedo/basic-crud-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Basic Flask CRUD API with SQLite

This project is a simple CRUD (Create, Read, Update, Delete) API built with Flask and SQLAlchemy, using SQLite as the database. The application is containerized using Docker and Docker Compose.

Project Structure

  • app.py: The main application file containing the Flask API logic.
  • Dockerfile: Defines the Docker image for the Flask application.
  • docker-compose.yml: Configures the Docker services and volumes.
  • requirements.txt: Lists the Python dependencies for the project.

Features

  • Create: Add new products.
  • Read: List all products and get details of a specific product.
  • Update: Modify existing products.
  • Delete: Remove products from the database.

Security and Optimization with Distroless Images

This project utilizes distroless images for the final Docker image build. Distroless images are minimal container images that include only the essential components required to run the application, significantly reducing the attack surface and potential vulnerabilities. By using a distroless image for the Flask application, this project ensures a more secure and optimized production environment.

Why Use Distroless?

  • Security: Distroless images contain only what is necessary to run the application, eliminating unnecessary packages and reducing the risk of vulnerabilities.
  • Smaller Image Size: These images are smaller, which speeds up deployment and reduces the storage requirements.
  • Consistency: Distroless images provide a consistent runtime environment, ensuring that the application behaves the same in different environments.

Multi-Stage Build Process

The Dockerfile for this project uses a multi-stage build process:

  • Builder Stage: A full-featured Python image (python:3.9-slim) is used to install dependencies and build the application.
  • Production Stage: The final image is built using a distroless base (gcr.io/distroless/python3-debian11), containing only the Python runtime and the application code.

Getting Started

Prerequisites

Running the Application

  1. Clone the Repository

    git clone https://github.com/lisazevedo/basic-crud-api
    cd basic-crud-api
  2. Build and Start the Containers

    docker compose up --build

    This command will:

    • Build the Docker image for the Flask application.
    • Start a busybox container to handle the SQLite volume.
    • Start the Flask application container, linking it with the SQLite volume.
  3. Access the API

    The Flask API will be available at http://localhost:5000.

API Endpoints

  • Create Product

    POST /product

    Request Body (JSON):

    {
      "name": "Sample Product",
      "price": 19.99
    }

    Response (JSON):

    {
      "message": "Product created successfully",
      "product": {
        "id": 1,
        "name": "Sample Product",
        "price": 19.99
      }
    }
  • Get All Products

    GET /products

    Response (JSON):

    [
      {
        "id": 1,
        "name": "Sample Product",
        "price": 19.99
      }
    ]
  • Get Product by ID

    GET /product/<id>

    Response (JSON):

    {
      "id": 1,
      "name": "Sample Product",
      "price": 19.99
    }
  • Update Product by ID

    PUT /product/<id>

    Request Body (JSON):

    {
      "name": "Updated Product",
      "price": 24.99
    }

    Response (JSON):

    {
      "message": "Product updated successfully",
      "product": {
        "id": 1,
        "name": "Updated Product",
        "price": 24.99
      }
    }
  • Delete Product by ID

    DELETE /product/<id>

    Response (JSON):

    {
      "message": "Product deleted successfully"
    }

Development

To make changes to the Flask application:

  1. Modify app.py: Update the application logic as needed.
  2. Rebuild the Docker Image: Run docker compose build to apply the changes.
  3. Restart the Containers: Use docker compose up to restart the application.

Troubleshooting

  • Database Table Not Found: Ensure that db.create_all() is called before handling requests. Check the Docker Compose volume mount to ensure the SQLite database file is correctly shared.

About

Simple and secure Python API for DevSecOps practices, featuring basic CRUD functionality

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published