|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +`digilol-cert-pushpuller` is a Go-based tool for encrypting and syncing TLS certificates via S3 with automatic renewal support using LEGO. It operates in two modes: |
| 8 | + |
| 9 | +- **Push mode** (server): Renews certificates via LEGO, encrypts them, and uploads to S3 |
| 10 | +- **Pull mode** (client): Downloads and decrypts certificates from S3 |
| 11 | + |
| 12 | +## Build Commands |
| 13 | + |
| 14 | +```bash |
| 15 | +# Build binary |
| 16 | +go build -trimpath -ldflags="-s -w" |
| 17 | + |
| 18 | +# Run tests |
| 19 | +go test ./... |
| 20 | + |
| 21 | +# Run specific package tests |
| 22 | +go test ./internal/config |
| 23 | +go test ./internal/crypto |
| 24 | +go test ./internal/util |
| 25 | + |
| 26 | +# Build release packages (requires goreleaser) |
| 27 | +goreleaser release --snapshot --clean |
| 28 | +``` |
| 29 | + |
| 30 | +## Running |
| 31 | + |
| 32 | +```bash |
| 33 | +# Push certificates (server mode) |
| 34 | +./digilol-cert-pushpuller push --config /path/to/push.toml |
| 35 | + |
| 36 | +# Pull certificates (client mode) |
| 37 | +./digilol-cert-pushpuller pull --config /path/to/pull.toml |
| 38 | +``` |
| 39 | + |
| 40 | +## Architecture |
| 41 | + |
| 42 | +### Core Components |
| 43 | + |
| 44 | +1. **main.go**: Entry point that parses command (`push`/`pull`) and config file, handles daemon mode with scheduling and signal handling |
| 45 | + |
| 46 | +2. **push.go**: Server-side logic |
| 47 | + |
| 48 | + - Executes LEGO renewal commands (push.go:40-46) |
| 49 | + - Compares local certificate SHA256 hashes with `.hashes.json` from S3 (push.go:54-73) |
| 50 | + - Encrypts changed certificates per-domain using keys from `key_dir` (push.go:106-146) |
| 51 | + - Uploads encrypted `.enc` files to S3 and updates `.hashes.json` (push.go:148-179) |
| 52 | + - Runs reload command (e.g., `systemctl reload nginx`) (push.go:182-187) |
| 53 | + |
| 54 | +3. **pull.go**: Client-side logic |
| 55 | + - Downloads `.hashes.json` from S3 (pull.go:44-63) |
| 56 | + - Lists all `.enc` files in S3 bucket (pull.go:76-94) |
| 57 | + - For each file: checks if local hash matches S3 hash to skip unchanged files (pull.go:128-139) |
| 58 | + - Downloads and decrypts only changed files using matching `.key` files (pull.go:142-167) |
| 59 | + - Runs reload command (pull.go:171-176) |
| 60 | + |
| 61 | +### Internal Packages |
| 62 | + |
| 63 | +- **internal/config**: TOML configuration parsing and encryption key management |
| 64 | + |
| 65 | + - `LoadPush()`/`LoadPull()`: Parse TOML configs |
| 66 | + - `GetOrCreateKey()`: Per-certificate key management (generates 32-byte keys, stored as base64 in `.key` files with 0600 permissions) |
| 67 | + - Key files named `{certname}.key` (e.g., `_.example.com.key`) |
| 68 | + |
| 69 | +- **internal/crypto**: ChaCha20-Poly1305 encryption using `github.com/minio/sio` |
| 70 | + |
| 71 | + - `EncryptData()`: Encrypts certificate data |
| 72 | + - `DecryptData()`: Decrypts certificate data |
| 73 | + |
| 74 | +- **internal/s3util**: AWS S3 client creation |
| 75 | + |
| 76 | + - `NewClient()`: Creates S3 client with custom endpoint support for S3-compatible services |
| 77 | + |
| 78 | +- **internal/util**: Shell command execution |
| 79 | + - `RunCommandWithEnv()`: Executes LEGO commands and reload commands with custom environment variables |
| 80 | + |
| 81 | +### Security Model |
| 82 | + |
| 83 | +**Per-certificate encryption**: Each certificate domain (e.g., `_.example.com`) has its own 256-bit encryption key. This allows selective access control - clients can only decrypt certificates for which they possess the corresponding `.key` file. The server (push) generates keys automatically if they don't exist. Clients (pull) must have the key files distributed separately to decrypt certificates. |
| 84 | + |
| 85 | +### Configuration |
| 86 | + |
| 87 | +- See `push.example.toml` and `pull.example.toml` for complete examples |
| 88 | +- Default config location: `/etc/digilol-cert-pushpuller/{push,pull}.toml` |
| 89 | +- Supports S3-compatible services via `endpoint` and `force_path_style` options |
| 90 | +- Daemon mode available for systems without systemd timers (e.g., Alpine Linux) |
| 91 | + |
| 92 | +### Deployment |
| 93 | + |
| 94 | +Uses GoReleaser (`.goreleaser.yaml`) to build: |
| 95 | + |
| 96 | +- Binaries for Linux/Darwin/FreeBSD (amd64, arm64) |
| 97 | +- DEB packages (Debian/Ubuntu) with systemd units |
| 98 | +- RPM packages (RHEL/Fedora) with systemd units |
| 99 | +- APK packages (Alpine) with OpenRC scripts |
| 100 | + |
| 101 | +Systemd timers and OpenRC scripts are in `packaging/` directory. |
0 commit comments