- Introduction
- Prerequisites
- Project Setup
- Coding Standards
- Running the Application
- Testing
- Security Considerations
- Deployment
- Contribution Guidelines
- License
This document provides a comprehensive guide for developers contributing to the Python Fail2Ban API project. It outlines the necessary steps for setting up the development environment, coding standards, security practices, testing, and deployment procedures.
Before starting development, ensure that your environment meets the following requirements:
- Python 3.12 or higher
- Poetry for dependency management
- FastAPI as the web framework
- Pydantic for data validation
- Git for version control
- Python 3.12: Download and install from python.org.
- Poetry: Install Poetry by following the official guide.
- Git: Install Git from git-scm.com.
git clone https://github.com/orenlab/pyfail2banapi.git
cd pyfail2banapi
We recommend using Poetry
to manage dependencies:
poetry install
This will create a virtual environment and install all necessary dependencies.
We adhere to PEP 8 guidelines for Python code. Please ensure your code is formatted using black
:
black .
Use type hints throughout the code for better readability and maintainability. Follow PEP 484 for type annotations.
def get_jail_status(jail_name: str) -> JailStatus:
# Function implementation
Run flake8
to check for any linting issues:
flake8 .
Follow PEP 257 for docstrings. Every module, class, and function should have a clear, concise docstring.
Start the FastAPI development server:
poetry run uvicorn pyfail2banapi.app:app --reload
Visit http://127.0.0.1:8000/docs
to see the automatically generated API documentation.
We use pytest
for unit testing. Ensure that all tests pass before committing your code:
pytest
Check the test coverage using pytest-cov
:
pytest --cov=pyfail2banapi tests/
Ensure that test coverage is at least 90% for all modules.
Security is a top priority for this project, especially since we interact with system-level services like Fail2Ban
via
subprocesses.
-
Subprocess Execution:
- Always sanitize input before passing it to a subprocess.
- Use
shlex
to safely handle shell commands. - Avoid direct shell execution where possible.
-
Environment Variables:
- Never hardcode sensitive information like API keys or passwords.
- Use environment variables and
.env
files to manage sensitive data.
-
Input Validation:
- Use Pydantic models to validate all input data.
-
Logging:
- Avoid logging sensitive information (e.g., passwords, API keys).
- Use JSON format for logs to improve traceability.
Ensure all dependencies are regularly updated and free of vulnerabilities. Use tools like safety
or bandit
:
poetry run safety check
poetry run bandit -r .
We welcome contributions from the community! To contribute, follow these steps:
- Fork the repository.
- Create a new branch:
git checkout -b feature/your-feature
. - Make your changes and add tests if needed.
- Commit your changes:
git commit -m 'Add some feature'
. - Push to the branch:
git push origin feature/your-feature
. - Create a Pull Request.
Please ensure all code is properly documented, tested, and adheres to the coding standards before submitting a PR.
-
Commit Messages: Write clear and descriptive commit messages. Use the following format:
[type]: [short summary] [longer description, if necessary]
Types of commits might include:
feat
: A new featurefix
: A bug fixdocs
: Documentation changesstyle
: Code style improvements (non-functional changes)refactor
: Code refactoring (no functional changes)test
: Adding or updating testschore
: Other changes (e.g., build process, CI configuration)
This project is licensed under the MIT License. See the LICENSE file for more details.
This developer guide provides the essential steps and practices to maintain consistency and high-quality code in the * Python Fail2Ban API* project. For more information, feel free to contact the maintainers or open an issue on GitHub.