- Configure Traefik to use SOCKS/HTTP proxy for Let's Encrypt ACME challenges - Add proxy environment variables (SOCKS_PROXY, HTTP_PROXY, HTTPS_PROXY, NO_PROXY) - Update docker-compose.yml to pass proxy vars to Traefik container - Update entrypoint script to log proxy configuration - Document proxy setup in .env.example - Add comprehensive guide: PROXY_CERTIFICATE_RENEWAL.md with SSH tunnel setup instructions
243 lines
6.8 KiB
Markdown
243 lines
6.8 KiB
Markdown
# Certificate Renewal via SOCKS Proxy
|
|
|
|
This guide explains how to renew Let's Encrypt certificates on a production server without direct internet access by using a SOCKS proxy tunneled through SSH.
|
|
|
|
## Prerequisites
|
|
|
|
- SSH access to your production server
|
|
- A machine with internet access and a SOCKS5 proxy running (e.g., on port 18080)
|
|
- Docker Compose running on the production server
|
|
- Traefik configured with Let's Encrypt certificate resolver
|
|
|
|
## Step 1: Establish SSH Tunnel for SOCKS Proxy
|
|
|
|
From your local machine with internet access, establish a reverse SSH tunnel that forwards your SOCKS proxy to the production server:
|
|
|
|
### Option A: Foreground tunnel (for testing/debugging)
|
|
```bash
|
|
ssh -R 18080:127.0.0.1:18080 -N -T user@production-server.com
|
|
```
|
|
|
|
This command:
|
|
- `-R 18080:127.0.0.1:18080` - Creates a reverse tunnel binding port 18080 on the remote server to localhost:18080 on your machine
|
|
- `-N` - Doesn't execute remote commands
|
|
- `-T` - Disables pseudo-terminal allocation
|
|
- Keeps the tunnel open while the command runs
|
|
|
|
### Option B: Background persistent tunnel
|
|
```bash
|
|
# Using autossh for auto-reconnection
|
|
autossh -M 20000 -R 18080:127.0.0.1:18080 -N -T user@production-server.com &
|
|
|
|
# Or with regular ssh (will disconnect if connection drops):
|
|
ssh -R 18080:127.0.0.1:18080 -N -T user@production-server.com &
|
|
```
|
|
|
|
### Option C: Systemd service (for permanent setup)
|
|
|
|
Create `/etc/systemd/system/ssh-proxy-tunnel.service`:
|
|
|
|
```ini
|
|
[Unit]
|
|
Description=SSH Proxy Tunnel to Production Server
|
|
After=network.target
|
|
|
|
[Service]
|
|
Type=simple
|
|
User=youruser
|
|
ExecStart=/usr/bin/ssh -R 18080:127.0.0.1:18080 -N -T user@production-server.com
|
|
Restart=always
|
|
RestartSec=10
|
|
Environment="SSHPASS=your_password"
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
```
|
|
|
|
Then enable and start it:
|
|
```bash
|
|
sudo systemctl enable ssh-proxy-tunnel.service
|
|
sudo systemctl start ssh-proxy-tunnel.service
|
|
```
|
|
|
|
## Step 2: Configure Environment Variables on Server
|
|
|
|
SSH into your production server and edit `.env`:
|
|
|
|
```bash
|
|
ssh user@production-server.com
|
|
cd /path/to/opencloud-compose
|
|
nano .env
|
|
```
|
|
|
|
Add the proxy configuration:
|
|
|
|
```bash
|
|
# SOCKS5 proxy configuration (matches the reverse tunnel)
|
|
SOCKS_PROXY=socks5://localhost:18080
|
|
|
|
# HTTP proxy (for systems that don't support SOCKS)
|
|
# You can use a SOCKS-to-HTTP converter if needed, or use an HTTP proxy directly
|
|
HTTP_PROXY=http://localhost:18080
|
|
HTTPS_PROXY=http://localhost:18080
|
|
|
|
# Don't use proxy for these addresses
|
|
NO_PROXY=localhost,127.0.0.1,172.16.0.0/12,10.0.0.0/8
|
|
```
|
|
|
|
## Step 3: Restart Traefik to Apply Proxy Configuration
|
|
|
|
```bash
|
|
# Restart the Traefik service
|
|
docker compose restart traefik
|
|
|
|
# Check logs to verify proxy is being used
|
|
docker compose logs -f traefik | grep -i proxy
|
|
```
|
|
|
|
## Step 4: Trigger Certificate Renewal
|
|
|
|
Traefik automatically renews certificates 30 days before expiration. To manually force a renewal:
|
|
|
|
### Option A: Delete existing certificate and restart
|
|
```bash
|
|
# Backup the certificate file
|
|
docker compose exec traefik cp /certs/acme.json /certs/acme.json.backup
|
|
|
|
# Remove the certificate to force renewal
|
|
docker compose exec traefik rm /certs/acme.json
|
|
|
|
# Restart Traefik
|
|
docker compose restart traefik
|
|
|
|
# Monitor the renewal process
|
|
docker compose logs -f traefik | tail -50
|
|
```
|
|
|
|
### Option B: Trigger renewal with curl (if Traefik API is enabled)
|
|
```bash
|
|
# Wait a moment for Traefik to start renewal
|
|
sleep 30
|
|
|
|
# Check certificate status
|
|
docker compose exec traefik curl -s http://localhost:8080/api/raw | jq '.
|
|
[store][default].Certificates[0]'
|
|
```
|
|
|
|
### Option C: Update certificate expiration date (force immediate renewal)
|
|
```bash
|
|
# Modify the acme.json to force renewal on next restart
|
|
docker compose exec traefik /bin/sh -c 'sed -i "s/\"IssuedAt\"/\"IssuedAt_old\"/g" /certs/acme.json'
|
|
|
|
# Restart Traefik
|
|
docker compose restart traefik
|
|
|
|
# Monitor logs
|
|
docker compose logs -f traefik
|
|
```
|
|
|
|
## Step 5: Verify Certificate Renewal
|
|
|
|
Check if the certificate was renewed successfully:
|
|
|
|
```bash
|
|
# Check the certificate details
|
|
docker compose exec traefik openssl x509 -in /certs/acme.json -text -noout
|
|
|
|
# Or check the certificate served by HTTPS
|
|
echo | openssl s_client -servername your-domain.com -connect localhost:443 2>/dev/null | openssl x509 -noout -dates
|
|
|
|
# Check Traefik logs for any errors
|
|
docker compose logs traefik | grep -i "acme\|certificate\|error"
|
|
```
|
|
|
|
## Troubleshooting
|
|
|
|
### Proxy Connection Issues
|
|
|
|
If you see proxy-related errors in the logs:
|
|
|
|
```bash
|
|
# Verify SSH tunnel is still active on local machine
|
|
ps aux | grep ssh
|
|
|
|
# Check if tunnel is accessible from server
|
|
ssh user@production-server.com
|
|
curl -x socks5://localhost:18080 https://acme-v02.api.letsencrypt.org/directory
|
|
|
|
# If connection is refused, re-establish the tunnel
|
|
```
|
|
|
|
### Certificate Renewal Fails
|
|
|
|
1. **Check Traefik logs:**
|
|
```bash
|
|
docker compose logs traefik | grep -A 5 -i "error\|acme"
|
|
```
|
|
|
|
2. **Verify ACME configuration:**
|
|
```bash
|
|
docker compose exec traefik traefik --help | grep -i acme
|
|
```
|
|
|
|
3. **Test Let's Encrypt staging environment first:**
|
|
Add to `.env`:
|
|
```
|
|
TRAEFIK_ACME_CASERVER=https://acme-staging-v02.api.letsencrypt.org/directory
|
|
```
|
|
Then restart and test. Once working, switch back to production.
|
|
|
|
4. **Check firewall rules:**
|
|
```bash
|
|
# Ensure outbound HTTPS traffic can reach Let's Encrypt
|
|
curl -x socks5://localhost:18080 -v https://acme-v02.api.letsencrypt.org/directory
|
|
```
|
|
|
|
### Common Errors
|
|
|
|
**"no such file or directory: acme.json"**
|
|
- This is normal on first run. Traefik will create it.
|
|
|
|
**"x509: certificate signed by unknown authority"**
|
|
- Set `INSECURE=true` temporarily for testing, or ensure your SOCKS proxy returns valid certificates.
|
|
|
|
**"Timeout waiting for HTTP challenge"**
|
|
- Ensure port 80 is accessible on the server and the domain is pointing to the correct IP.
|
|
|
|
## Cleanup After Certificate Renewal
|
|
|
|
Once the certificate is renewed successfully:
|
|
|
|
1. Stop the SSH tunnel on your local machine (if it was temporary):
|
|
```bash
|
|
pkill ssh # Kill the tunnel process
|
|
```
|
|
|
|
2. Remove the proxy configuration from `.env`:
|
|
```bash
|
|
# Comment out or remove these lines
|
|
#SOCKS_PROXY=
|
|
#HTTP_PROXY=
|
|
#HTTPS_PROXY=
|
|
```
|
|
|
|
3. Restart Traefik to operate without proxy:
|
|
```bash
|
|
docker compose restart traefik
|
|
```
|
|
|
|
## Next Renewal (30 days before expiration)
|
|
|
|
Traefik will automatically attempt renewal 30 days before expiration. If the server still doesn't have internet access, repeat the process above.
|
|
|
|
For permanent solutions with servers without internet access, consider:
|
|
- Deploying a local Let's Encrypt-compatible CA
|
|
- Using wildcard certificates with DNS challenge instead of HTTP challenge
|
|
- Implementing a reverse proxy with internet access that handles ACME challenges
|
|
|
|
## References
|
|
|
|
- [Traefik ACME Documentation](https://doc.traefik.io/traefik/https/acme/)
|
|
- [Let's Encrypt Challenge Types](https://letsencrypt.org/docs/challenge-types/)
|
|
- [SSH Port Forwarding Guide](https://linux.die.net/man/1/ssh)
|