An ACME Certificate Cleanup Script for Traefik.
This script performs cleanup operations on the Traefik acme.json
file by removing expired, invalid, and optionally unused certificates. It also generates a markdown report summarising the analysis.
Container Image: ghcr.io/aperim/traefik-acme-cleanup
- Certificate Cleanup: Removes expired and invalid certificates from Traefik's
acme.json
file. - Unused Certificates: Optionally removes certificates that are not in use by Traefik routers.
- Markdown Report: Generates a detailed markdown report summarising the certificate analysis.
- Traefik API Integration: Fetches in-use domains directly from Traefik's API for accurate analysis.
- Flexible Configuration: Configure behaviour via command-line arguments or environment variables.
- Docker Support: Run the script effortlessly inside a Docker container with configurable user IDs.
- Python: Version 3.20 or higher.
- Traefik API Credentials: Access to the Traefik dashboard API.
- Access to
acme.json
: The script needs read and write permissions to Traefik'sacme.json
file.
-
Clone the Repository:
git clone https://github.com/aperim/traefik-acme-cleaner.git cd traefik-acme-cleaner
-
Install Dependencies:
pip install -r requirements.txt
Note: It's recommended to use a virtual environment.
The script can be configured using environment variables or command-line arguments.
TRAEFIK_DASHBOARD_URL
: URL to the Traefik dashboard API endpoint (e.g.,http://localhost:8080/api
).TRAEFIK_DASHBOARD_USERNAME
: Username for Traefik dashboard authentication.TRAEFIK_DASHBOARD_PASSWORD
: Password for Traefik dashboard authentication.
TRAEFIK_ACME_FILE
: Path to theacme.json
file. Default isacme.json
in the current directory.ACME_CLEANUP_UNUSED
: Set totrue
to include unused certificates in the cleanup. Default isfalse
.ACME_CLEANUP_DOIT
: Set totrue
to perform the cleanup. Iffalse
, the script runs in simulation mode. Default isfalse
.CLEANUP_REPORT
: Path to the markdown report file. Default is./REPORT.md
.
Run the script using Python:
python acme_cleanup.py [options]
--include-unused
: Include unused certificates in the removal process.--doit
: Perform the cleanup; otherwise, the script runs in simulation mode.--report PATH
: Specify the path to the markdown report file.
Environment variables can be used instead of command-line arguments:
ACME_CLEANUP_UNUSED
: Equivalent to--include-unused
.ACME_CLEANUP_DOIT
: Equivalent to--doit
.CLEANUP_REPORT
: Equivalent to--report
.
-
Simulate Cleanup and Generate Report:
python acme_cleanup.py --report ./cleanup_report.md
-
Perform Actual Cleanup:
python acme_cleanup.py --doit
-
Include Unused Certificates in Cleanup:
python acme_cleanup.py --doit --include-unused
-
Using Environment Variables:
export TRAEFIK_DASHBOARD_URL="http://localhost:8080/api" export TRAEFIK_DASHBOARD_USERNAME="your_username" export TRAEFIK_DASHBOARD_PASSWORD="your_password" export ACME_CLEANUP_DOIT="true" python acme_cleanup.py
The script is available as a Docker image for ease of use.
Pull the image from GitHub Container Registry:
docker pull ghcr.io/aperim/traefik-acme-cleanup:latest
Run the container with the necessary environment variables and volume mounts:
docker run --rm \
-e TRAEFIK_ACME_FILE="/data/acme.json" \
-e TRAEFIK_DASHBOARD_URL="http://traefik:8080/api" \
-e TRAEFIK_DASHBOARD_USERNAME="your_username" \
-e TRAEFIK_DASHBOARD_PASSWORD="your_password" \
-e ACME_CLEANUP_DOIT="true" \
-v /path/to/your/acme.json:/data/acme.json \
-v /path/to/output/report:/data \
ghcr.io/aperim/traefik-acme-cleanup
- Mount the
acme.json
File: Ensure that theacme.json
file is mounted inside the container at the path specified byTRAEFIK_ACME_FILE
. - Mount the Output Directory: If you want to save the report outside the container, mount the directory where the report will be saved.
To match file permissions with your host system, you can specify the user and group IDs:
PUID
: User ID to run the application as (default1000
).PGID
: Group ID to run the application as (default1000
).
Example:
docker run --rm \
-e PUID=1001 \
-e PGID=1001 \
-e TRAEFIK_ACME_FILE="/data/acme.json" \
-e TRAEFIK_DASHBOARD_URL="http://traefik:8080/api" \
-e TRAEFIK_DASHBOARD_USERNAME="your_username" \
-e TRAEFIK_DASHBOARD_PASSWORD="your_password" \
-e ACME_CLEANUP_DOIT="true" \
-v /path/to/your/acme.json:/data/acme.json \
ghcr.io/aperim/traefik-acme-cleanup
You can override the default command to run a shell or another command within the container:
docker run --rm -it \
-v /path/to/your/data:/data \
ghcr.io/aperim/traefik-acme-cleanup /bin/sh
Contributions are welcome! Please follow these steps:
-
Fork the Repository: Create a fork of the repository on GitHub.
-
Create a Feature Branch:
git checkout -b feature/my-new-feature
-
Commit Your Changes:
git commit -am 'Add my new feature'
-
Push to the Branch:
git push origin feature/my-new-feature
-
Create a Pull Request: Open a pull request on GitHub.
This project is licensed under the Apache License 2.0. See the LICENSE file for details.
- Troy Kelly - troykelly - [email protected]