v0.37.1
This commit is contained in:
1 parent
78dbb31487
commit
f48843e934
83 files changed
+7304
-1203
No files matched your search
@@ -1,109 +1,134 @@
|
||||
# OPNsense os-acme-client -> RustProxy
|
||||
|
||||
A simple mechanism for automatically sending a certificate generated by `os-acme-client` on OPNsense to RustProxy via API.
|
||||
Deploys certificates issued by the OPNsense **os-acme-client** plugin (Services -> ACME Client) to RustProxy through `POST /api/certificates/deploy`. The script runs as an ACME Client **automation** ("System or Plugin Command"), so every issued or renewed certificate is sent to RustProxy right away. Works with RustProxy 0.28.2 or newer; error hints match RustProxy 0.37.
|
||||
|
||||
## Files
|
||||
|
||||
- `opnsense-rustproxy-cert-deploy.sh` - sends the certificate and private key to RustProxy.
|
||||
- `rustproxy-acme.conf.example` - example connection settings, API token, and certificate file paths.
|
||||
- `actions_rustproxy_cert.conf` - optional `configd` action that allows the script to be executed from Cron in OPNsense.
|
||||
- `opnsense-rustproxy-cert-deploy.sh` - finds the os-acme-client certificates, checks them and uploads them.
|
||||
- `rustproxy-acme.conf.example` - RustProxy URL, certificate API credential and the certificate mapping.
|
||||
- `actions_rustproxy_cert.conf` - `configd` action **Deploy certificates to RustProxy**, offered by the ACME Client automation and by Cron.
|
||||
|
||||
## 1. Installation
|
||||
## 1. RustProxy credential
|
||||
|
||||
In RustProxy open **Settings -> Certificate API** and create (or edit) a key:
|
||||
|
||||
- **Allowed certificate IDs**: every RustProxy certificate id used below (the right side of `RUSTPROXY_CERTIFICATES`), one per line. `*` is not accepted; a key migrated from an older version with `*` must get the explicit list when it is edited. The ids do not have to exist yet: the first deploy creates them.
|
||||
- **Also allow deploy**: enabled. Keys created before 0.28.2 were migrated as reload-only and get `HTTP 403 certificate_api.deploy_forbidden`.
|
||||
|
||||
Copy the key id and the token (the token is shown only once; regenerate it if lost).
|
||||
|
||||
## 2. Installation (OPNsense root shell, SSH or console option 8)
|
||||
|
||||
```sh
|
||||
cp opnsense-rustproxy-cert-deploy.sh /usr/local/sbin/
|
||||
cat > /usr/local/sbin/opnsense-rustproxy-cert-deploy.sh <<'RUSTPROXY_EOF'
|
||||
<paste the content of opnsense-rustproxy-cert-deploy.sh>
|
||||
RUSTPROXY_EOF
|
||||
chmod 750 /usr/local/sbin/opnsense-rustproxy-cert-deploy.sh
|
||||
|
||||
cp rustproxy-acme.conf.example /usr/local/etc/rustproxy-acme.conf
|
||||
cat > /usr/local/etc/rustproxy-acme.conf <<'RUSTPROXY_EOF'
|
||||
<paste rustproxy-acme.conf.example and fill in the values>
|
||||
RUSTPROXY_EOF
|
||||
chmod 600 /usr/local/etc/rustproxy-acme.conf
|
||||
```
|
||||
|
||||
## 2. Configuration
|
||||
|
||||
Edit:
|
||||
|
||||
```sh
|
||||
vi /usr/local/etc/rustproxy-acme.conf
|
||||
```
|
||||
|
||||
Important parameters:
|
||||
|
||||
```sh
|
||||
RUSTPROXY_URL='https://127.0.0.1:9090'
|
||||
RUSTPROXY_KEY_ID='opnsense-acme'
|
||||
RUSTPROXY_TOKEN='RUSTPROXY_TOKEN'
|
||||
|
||||
CERTIFICATE_ID='shared-cert'
|
||||
|
||||
CERT_FILE='/var/etc/acme-client/certs/CERT_UUID/fullchain.pem'
|
||||
KEY_FILE='/var/etc/acme-client/keys/CERT_UUID/private.key'
|
||||
```
|
||||
|
||||
If `os-acme-client` exports the certificate to a custom directory, simply set the correct paths:
|
||||
|
||||
```sh
|
||||
CERT_FILE='/path/to/fullchain.pem'
|
||||
KEY_FILE='/path/to/private.key'
|
||||
```
|
||||
|
||||
`CERTIFICATE_ID` is the stable external certificate id. If it does not exist, RustProxy creates a manual certificate entry. If it already exists as a manual certificate, RustProxy rotates it in place. RustProxy-managed ACME ids are rejected to prevent two renewal owners for one lineage.
|
||||
|
||||
## 3. RustProxy API
|
||||
|
||||
Create a certificate API key in **Settings -> Certificate API** and enter its values in the configuration:
|
||||
|
||||
```text
|
||||
RUSTPROXY_KEY_ID
|
||||
RUSTPROXY_TOKEN
|
||||
```
|
||||
|
||||
The credential must explicitly allow `CERTIFICATE_ID` and **Also allow deploy** (`allow_deploy`) must be enabled. A reload-only credential will be rejected with HTTP 403.
|
||||
|
||||
The script sends the certificate to:
|
||||
|
||||
```text
|
||||
POST /api/certificates/deploy
|
||||
```
|
||||
|
||||
The connection from OPNsense to RustProxy should use HTTPS.
|
||||
|
||||
## 4. Test
|
||||
|
||||
Run the certificate reconciliation:
|
||||
|
||||
```sh
|
||||
/usr/local/sbin/opnsense-rustproxy-cert-deploy.sh
|
||||
```
|
||||
|
||||
On success, you should see a message similar to:
|
||||
|
||||
```text
|
||||
RustProxy certificate reconcile succeeded (HTTP 200, id=shared-cert)
|
||||
```
|
||||
|
||||
## 5. OPNsense Automation
|
||||
|
||||
Optionally install the `configd` action:
|
||||
|
||||
```sh
|
||||
cp actions_rustproxy_cert.conf /usr/local/opnsense/service/conf/actions.d/
|
||||
cat > /usr/local/opnsense/service/conf/actions.d/actions_rustproxy_cert.conf <<'RUSTPROXY_EOF'
|
||||
<paste the content of actions_rustproxy_cert.conf>
|
||||
RUSTPROXY_EOF
|
||||
service configd restart
|
||||
```
|
||||
|
||||
Verify the action first with `configctl rustproxy_cert deploy`. Then go to **System -> Settings -> Cron** and add a scheduled execution of the `Deploy ACME certificate to RustProxy` action.
|
||||
Alternatively copy the three files with `scp`.
|
||||
|
||||
The script deliberately reconciles on every run instead of trusting a local cache. The server is the source of truth: an identical RustProxy-owned certificate generation is a no-op, a missing id is created, and changed material is rotated atomically. This also repairs drift when a certificate entry or generation was removed outside OPNsense.
|
||||
## 3. Which certificates are sent
|
||||
|
||||
## Additional Options
|
||||
|
||||
If RustProxy uses a private CA:
|
||||
os-acme-client keeps the files in `/var/etc/acme-client/certs/<id>/fullchain.pem` and `/var/etc/acme-client/keys/<id>/private.key`. The `<id>` is an internal id that the GUI does not show, so the script finds the certificate by **name** (the *Common Name* or any *Alt Name* of the ACME certificate):
|
||||
|
||||
```sh
|
||||
RUSTPROXY_CACERT='/usr/local/etc/ssl/rustproxy-ca.pem'
|
||||
/usr/local/sbin/opnsense-rustproxy-cert-deploy.sh --list
|
||||
```
|
||||
|
||||
If you want to connect to a specific LAN address while keeping proper TLS hostname verification:
|
||||
```text
|
||||
66a1b2c3d4e5f *.apps.example.com apps.example.com (valid until Dec 14 13:43:31 2026 GMT)
|
||||
```
|
||||
|
||||
Map each name to a RustProxy certificate id (`A-Z a-z 0-9 _ -`, max 64), separated by spaces:
|
||||
|
||||
```sh
|
||||
RUSTPROXY_RESOLVE='rustproxy.lan:8443:192.168.1.20'
|
||||
RUSTPROXY_CERTIFICATES='*.apps.example.com=apps-wildcard example.com=example-com'
|
||||
```
|
||||
|
||||
Every run sends all mapped certificates. A certificate that did not change is a no-op in RustProxy, a missing id is created as a manual certificate and changed material is rotated atomically. RustProxy-managed ACME ids are rejected (409). When several ACME certificates carry the same name, the one valid longest is used.
|
||||
|
||||
A single certificate can also be set with `CERTIFICATE_ID` plus `ACME_CERT_NAME`, or with explicit `CERT_FILE` and `KEY_FILE`.
|
||||
|
||||
## 4. Connection settings
|
||||
|
||||
| Setting | Meaning |
|
||||
|---|---|
|
||||
| `RUSTPROXY_URL` | Panel origin only: `https://name:port` (no path). |
|
||||
| `RUSTPROXY_KEY_ID`, `RUSTPROXY_TOKEN` | Certificate API credential. |
|
||||
| `RUSTPROXY_TLS_VERIFY` | `yes` (default) verifies the panel certificate; `no` accepts a self-signed or not yet valid panel certificate. |
|
||||
| `RUSTPROXY_CACERT` | PEM file of a private CA that signed the panel certificate (keeps verification on). |
|
||||
| `RUSTPROXY_PINNED_PUBKEY` | `sha256//BASE64` pin of the panel key. Recommended together with `RUSTPROXY_TLS_VERIFY='no'`. |
|
||||
| `RUSTPROXY_RESOLVE` | `name:port:address` - connect to a fixed IP while using the panel name for TLS and the Host header. |
|
||||
| `RUSTPROXY_ALLOW_HTTP` | `yes` allows `http://` to a remote host. Only for a trusted reverse proxy that terminates HTTPS in front of RustProxy; otherwise RustProxy answers `403 certificate_api.https_required`. `http://127.0.0.1` always works. |
|
||||
|
||||
### Panel without a valid certificate
|
||||
|
||||
Pin of a self-signed panel certificate (recommended):
|
||||
|
||||
```sh
|
||||
echo | openssl s_client -connect rustproxy.lan:9090 -servername rustproxy.lan 2>/dev/null \
|
||||
| openssl x509 -pubkey -noout | openssl pkey -pubin -outform DER \
|
||||
| openssl dgst -sha256 -binary | openssl enc -base64
|
||||
```
|
||||
|
||||
```sh
|
||||
RUSTPROXY_TLS_VERIFY='no'
|
||||
RUSTPROXY_PINNED_PUBKEY='sha256//<output of the command above>'
|
||||
```
|
||||
|
||||
Without the pin only `RUSTPROXY_TLS_VERIFY='no'` is needed; the script then logs a warning on every run.
|
||||
|
||||
### Host name
|
||||
|
||||
RustProxy accepts only the host names listed in **Settings -> Administration** (public panel HTTPS origin and additional allowed DNS hostnames) or an IP literal. Another DNS name gives `403 auth.invalid_host`. When the panel name does not resolve on OPNsense, keep the name in `RUSTPROXY_URL` and add `RUSTPROXY_RESOLVE='name:port:IP'`.
|
||||
|
||||
## 5. Test
|
||||
|
||||
```sh
|
||||
/usr/local/sbin/opnsense-rustproxy-cert-deploy.sh --check
|
||||
configctl rustproxy_cert deploy
|
||||
```
|
||||
|
||||
`--check` finds the mapped certificates and validates key match and expiry without contacting RustProxy. `configctl` runs exactly what the automation runs. A successful upload prints:
|
||||
|
||||
```text
|
||||
RustProxy certificate reconcile succeeded (HTTP 200, id=apps-wildcard)
|
||||
```
|
||||
|
||||
Every run is also written to the system log (tag `rustproxy-acme`, **System -> Log Files -> General**).
|
||||
|
||||
## 6. ACME Client automation
|
||||
|
||||
1. **Services -> ACME Client -> Automations -> +**: *Run Command* = **System or Plugin Command**, *System Command* = **Deploy certificates to RustProxy**, give it a name and save.
|
||||
2. **Services -> ACME Client -> Certificates**: edit each mapped certificate and add the automation under **Automations**.
|
||||
3. Click **Run automations** (paper plane icon) on the certificate to test it.
|
||||
|
||||
From now on the deploy runs after every issue or renewal. The ACME Client log shows `running automation (configd)`, the result is in the system log.
|
||||
|
||||
Optionally also schedule **Deploy certificates to RustProxy** in **System -> Settings -> Cron** (for example daily) to repair drift when a certificate was changed or removed on the RustProxy side.
|
||||
|
||||
## Errors
|
||||
|
||||
On failure the script prints the HTTP status, the RustProxy error code and a hint.
|
||||
|
||||
| Response | Cause |
|
||||
|---|---|
|
||||
| `403 certificate_api.deploy_forbidden` | Key is reload-only: enable **Also allow deploy**. |
|
||||
| `403 certificate_api.certificate_forbidden` | Key does not include that certificate id. |
|
||||
| `403 certificate_api.https_required` | `http://` used from a non-loopback address. |
|
||||
| `403 auth.invalid_host` | Host name in `RUSTPROXY_URL` is not allowed by the panel. |
|
||||
| `403 auth.invalid_origin` | Scheme/host does not match the pinned public panel origin or trusted proxy settings. |
|
||||
| `401 certificate_api.invalid_token` | Wrong key id or token, or the token was regenerated. |
|
||||
| `409` | The id belongs to a RustProxy-managed ACME certificate. |
|
||||
| `no os-acme-client certificate for NAME` | Name not found: compare with `--list`. |
|
||||
@@ -2,5 +2,5 @@
|
||||
command:/usr/local/sbin/opnsense-rustproxy-cert-deploy.sh
|
||||
parameters:
|
||||
type:script_output
|
||||
message:deploy ACME certificate to RustProxy
|
||||
description:Deploy ACME certificate to RustProxy
|
||||
message:deploying certificates to RustProxy
|
||||
description:Deploy certificates to RustProxy
|
||||
@@ -1,9 +1,37 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
umask 077
|
||||
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
|
||||
export PATH
|
||||
|
||||
# Deploy an OPNsense os-acme-client certificate to RustProxy; settings live in a separate file.
|
||||
RUSTPROXY_URL="${RUSTPROXY_URL:-}"
|
||||
RUSTPROXY_KEY_ID="${RUSTPROXY_KEY_ID:-}"
|
||||
RUSTPROXY_TOKEN="${RUSTPROXY_TOKEN:-}"
|
||||
RUSTPROXY_CERTIFICATES="${RUSTPROXY_CERTIFICATES:-}"
|
||||
CERTIFICATE_ID="${CERTIFICATE_ID:-}"
|
||||
ACME_CERT_NAME="${ACME_CERT_NAME:-}"
|
||||
CERT_FILE="${CERT_FILE:-}"
|
||||
KEY_FILE="${KEY_FILE:-}"
|
||||
RUSTPROXY_TLS_VERIFY="${RUSTPROXY_TLS_VERIFY:-yes}"
|
||||
RUSTPROXY_CACERT="${RUSTPROXY_CACERT:-}"
|
||||
RUSTPROXY_PINNED_PUBKEY="${RUSTPROXY_PINNED_PUBKEY:-}"
|
||||
RUSTPROXY_RESOLVE="${RUSTPROXY_RESOLVE:-}"
|
||||
RUSTPROXY_ALLOW_HTTP="${RUSTPROXY_ALLOW_HTTP:-no}"
|
||||
ACME_CERTS_DIR="${ACME_CERTS_DIR:-/var/etc/acme-client/certs}"
|
||||
ACME_KEYS_DIR="${ACME_KEYS_DIR:-/var/etc/acme-client/keys}"
|
||||
|
||||
CONFIG_FILE="${RUSTPROXY_ACME_CONFIG:-/usr/local/etc/rustproxy-acme.conf}"
|
||||
MODE=deploy
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
--check) MODE=check ;;
|
||||
--list) MODE=list ;;
|
||||
--config) shift; CONFIG_FILE="${1:?--config needs a file}" ;;
|
||||
-h|--help) printf 'Usage: %s [--check|--list] [--config FILE]\n' "$0"; exit 0 ;;
|
||||
*) printf 'Unknown option: %s\n' "$1" >&2; exit 2 ;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
log() {
|
||||
logger -t rustproxy-acme -- "$*" 2>/dev/null || true
|
||||
@@ -15,61 +43,128 @@ fail() {
|
||||
exit 1
|
||||
}
|
||||
|
||||
[ -r "$CONFIG_FILE" ] || fail "Cannot read config: $CONFIG_FILE"
|
||||
# shellcheck disable=SC1090
|
||||
. "$CONFIG_FILE"
|
||||
case "$CONFIG_FILE" in
|
||||
*/*) ;;
|
||||
*) CONFIG_FILE="./$CONFIG_FILE" ;;
|
||||
esac
|
||||
if [ -r "$CONFIG_FILE" ]; then
|
||||
. "$CONFIG_FILE"
|
||||
elif [ -e "$CONFIG_FILE" ]; then
|
||||
fail "Cannot read config: $CONFIG_FILE"
|
||||
fi
|
||||
|
||||
: "${RUSTPROXY_URL:?Set RUSTPROXY_URL in $CONFIG_FILE}"
|
||||
: "${RUSTPROXY_KEY_ID:?Set RUSTPROXY_KEY_ID in $CONFIG_FILE}"
|
||||
: "${RUSTPROXY_TOKEN:?Set RUSTPROXY_TOKEN in $CONFIG_FILE}"
|
||||
: "${CERTIFICATE_ID:?Set CERTIFICATE_ID in $CONFIG_FILE}"
|
||||
: "${CERT_FILE:?Set CERT_FILE in $CONFIG_FILE}"
|
||||
: "${KEY_FILE:?Set KEY_FILE in $CONFIG_FILE}"
|
||||
|
||||
CURL="${CURL:-$(command -v curl || true)}"
|
||||
OPENSSL="${OPENSSL:-$(command -v openssl || true)}"
|
||||
[ -n "$CURL" ] || fail "curl not found"
|
||||
[ -n "$OPENSSL" ] || fail "openssl not found"
|
||||
|
||||
case "$CERTIFICATE_ID" in
|
||||
''|*[!A-Za-z0-9_-]*) fail "CERTIFICATE_ID may contain only A-Z, a-z, 0-9, _ and -" ;;
|
||||
cert_names() {
|
||||
"$OPENSSL" x509 -in "$1" -noout -text 2>/dev/null \
|
||||
| tr ',' '\n' | sed -n 's/^[[:space:]]*DNS:\([^[:space:]]*\).*/\1/p' \
|
||||
| tr 'A-Z' 'a-z'
|
||||
"$OPENSSL" x509 -in "$1" -noout -subject 2>/dev/null \
|
||||
| sed -n 's/.*CN[[:space:]]*=[[:space:]]*\([^,/]*\).*/\1/p' | tr 'A-Z' 'a-z'
|
||||
}
|
||||
|
||||
cert_end() {
|
||||
end=$("$OPENSSL" x509 -in "$1" -noout -enddate 2>/dev/null | sed 's/^notAfter=//')
|
||||
date -j -f '%b %e %T %Y %Z' "$end" +%s 2>/dev/null \
|
||||
|| date -d "$end" +%s 2>/dev/null \
|
||||
|| echo 0
|
||||
}
|
||||
|
||||
if [ "$MODE" = list ]; then
|
||||
found=no
|
||||
for cert in "$ACME_CERTS_DIR"/*/cert.pem; do
|
||||
[ -r "$cert" ] || continue
|
||||
found=yes
|
||||
id=$(basename "$(dirname "$cert")")
|
||||
names=$(cert_names "$cert" | sort -u | tr '\n' ' ')
|
||||
until=$("$OPENSSL" x509 -in "$cert" -noout -enddate 2>/dev/null | sed 's/^notAfter=//')
|
||||
printf '%s %s (valid until %s)\n' "$id" "$names" "$until"
|
||||
done
|
||||
[ "$found" = yes ] || printf 'No os-acme-client certificates in %s\n' "$ACME_CERTS_DIR"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
[ -n "$RUSTPROXY_URL" ] || fail "Set RUSTPROXY_URL (in $CONFIG_FILE or at the top of this script)"
|
||||
[ -n "$RUSTPROXY_KEY_ID" ] || fail "Set RUSTPROXY_KEY_ID (Settings -> Certificate API key id)"
|
||||
[ -n "$RUSTPROXY_TOKEN" ] || fail "Set RUSTPROXY_TOKEN (Settings -> Certificate API token)"
|
||||
|
||||
CURL="${CURL:-$(command -v curl || true)}"
|
||||
[ -n "$CURL" ] || fail "curl not found"
|
||||
|
||||
case "$RUSTPROXY_KEY_ID" in
|
||||
*[!A-Za-z0-9_-]*) fail "RUSTPROXY_KEY_ID may contain only A-Z, a-z, 0-9, _ and -" ;;
|
||||
esac
|
||||
[ "${#CERTIFICATE_ID}" -le 64 ] || fail "CERTIFICATE_ID is longer than 64 characters"
|
||||
case "$RUSTPROXY_TOKEN" in
|
||||
*[!A-Za-z0-9._~-]*) fail "RUSTPROXY_TOKEN contains unexpected characters (copy it again from RustProxy, without quotes or spaces)" ;;
|
||||
esac
|
||||
[ "${#RUSTPROXY_TOKEN}" -ge 32 ] && [ "${#RUSTPROXY_TOKEN}" -le 128 ] \
|
||||
|| fail "RUSTPROXY_TOKEN must be 32-128 characters long"
|
||||
|
||||
[ -r "$CERT_FILE" ] || fail "Certificate not readable: $CERT_FILE"
|
||||
[ -r "$KEY_FILE" ] || fail "Private key not readable: $KEY_FILE"
|
||||
BASE_URL="${RUSTPROXY_URL%/}"
|
||||
case "$BASE_URL" in
|
||||
https://*) SCHEME=https ;;
|
||||
http://*) SCHEME=http ;;
|
||||
*) fail "RUSTPROXY_URL must start with https:// or http://" ;;
|
||||
esac
|
||||
case "$BASE_URL" in
|
||||
*://*/*) fail "RUSTPROXY_URL must be the panel origin only, e.g. https://rustproxy.lan:9090" ;;
|
||||
esac
|
||||
HOST_PORT="${BASE_URL#*://}"
|
||||
HOST_ONLY="${HOST_PORT%:*}"
|
||||
case "$HOST_ONLY" in
|
||||
\[*) HOST_ONLY="${HOST_PORT%%]*}]" ;;
|
||||
esac
|
||||
|
||||
# Validate certificate syntax and make sure the certificate and private key belong together.
|
||||
"$OPENSSL" x509 -in "$CERT_FILE" -noout >/dev/null 2>&1 \
|
||||
|| fail "Invalid certificate PEM: $CERT_FILE"
|
||||
"$OPENSSL" pkey -in "$KEY_FILE" -noout >/dev/null 2>&1 \
|
||||
|| fail "Invalid private key PEM: $KEY_FILE"
|
||||
if [ "$SCHEME" = http ]; then
|
||||
case "$HOST_ONLY" in
|
||||
127.*|localhost|\[::1\]) ;;
|
||||
*)
|
||||
[ "$RUSTPROXY_ALLOW_HTTP" = yes ] || fail "RUSTPROXY_URL uses http:// to a remote host. RustProxy accepts certificate API calls over plain HTTP only from loopback (HTTP 403 certificate_api.https_required) and the token would travel unencrypted. Use https:// (for a self-signed panel RUSTPROXY_TLS_VERIFY=no, ideally with RUSTPROXY_PINNED_PUBKEY), or set RUSTPROXY_ALLOW_HTTP=yes when a trusted reverse proxy in front of RustProxy terminates HTTPS."
|
||||
log "WARNING: sending the certificate API token over plain HTTP"
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
|
||||
CERT_PUB=$(
|
||||
"$OPENSSL" x509 -in "$CERT_FILE" -pubkey -noout 2>/dev/null \
|
||||
| "$OPENSSL" pkey -pubin -outform DER 2>/dev/null \
|
||||
| "$OPENSSL" dgst -sha256 2>/dev/null
|
||||
)
|
||||
KEY_PUB=$(
|
||||
"$OPENSSL" pkey -in "$KEY_FILE" -pubout -outform DER 2>/dev/null \
|
||||
| "$OPENSSL" dgst -sha256 2>/dev/null
|
||||
)
|
||||
[ -n "$CERT_PUB" ] && [ "$CERT_PUB" = "$KEY_PUB" ] \
|
||||
|| fail "Certificate and private key do not match"
|
||||
case "$RUSTPROXY_TLS_VERIFY" in
|
||||
yes|no) ;;
|
||||
*) fail "RUSTPROXY_TLS_VERIFY must be yes or no" ;;
|
||||
esac
|
||||
if [ -n "$RUSTPROXY_CACERT" ]; then
|
||||
[ -r "$RUSTPROXY_CACERT" ] || fail "CA file not readable: $RUSTPROXY_CACERT"
|
||||
fi
|
||||
if [ -n "$RUSTPROXY_PINNED_PUBKEY" ]; then
|
||||
case "$RUSTPROXY_PINNED_PUBKEY" in
|
||||
sha256//*) ;;
|
||||
*) fail "RUSTPROXY_PINNED_PUBKEY must look like sha256//BASE64 (see the README for the openssl command)" ;;
|
||||
esac
|
||||
fi
|
||||
if [ "$SCHEME" = https ] && [ "$RUSTPROXY_TLS_VERIFY" = no ] && [ -z "$RUSTPROXY_PINNED_PUBKEY" ]; then
|
||||
log "WARNING: TLS certificate of $BASE_URL is not verified; set RUSTPROXY_PINNED_PUBKEY to pin it"
|
||||
fi
|
||||
|
||||
# Do not publish an already-expired certificate.
|
||||
"$OPENSSL" x509 -in "$CERT_FILE" -checkend 0 -noout >/dev/null 2>&1 \
|
||||
|| fail "Certificate is already expired"
|
||||
TMP_DIR=$(mktemp -d "${TMPDIR:-/tmp}/rustproxy-acme.XXXXXX") || fail "mktemp failed"
|
||||
trap 'rm -rf "$TMP_DIR"' EXIT HUP INT TERM
|
||||
|
||||
# The deploy endpoint is idempotent, so always reconcile instead of trusting local state.
|
||||
find_acme_cert() {
|
||||
wanted=$(printf '%s' "$1" | tr 'A-Z' 'a-z')
|
||||
best=""
|
||||
best_end=0
|
||||
for cert in "$ACME_CERTS_DIR"/*/cert.pem; do
|
||||
[ -r "$cert" ] || continue
|
||||
cert_names "$cert" | grep -Fqx -- "$wanted" || continue
|
||||
id=$(basename "$(dirname "$cert")")
|
||||
[ -r "$ACME_KEYS_DIR/$id/private.key" ] || continue
|
||||
end=$(cert_end "$cert")
|
||||
if [ -z "$best" ] || [ "$end" -gt "$best_end" ]; then
|
||||
best="$id"
|
||||
best_end="$end"
|
||||
fi
|
||||
done
|
||||
[ -n "$best" ] || return 1
|
||||
printf '%s\n' "$best"
|
||||
}
|
||||
|
||||
umask 077
|
||||
TMP_JSON=$(mktemp "${TMPDIR:-/tmp}/rustproxy-cert.XXXXXX") || fail "mktemp failed"
|
||||
TMP_CURL=$(mktemp "${TMPDIR:-/tmp}/rustproxy-curl.XXXXXX") || { rm -f "$TMP_JSON"; fail "mktemp failed"; }
|
||||
TMP_RESPONSE=$(mktemp "${TMPDIR:-/tmp}/rustproxy-response.XXXXXX") || { rm -f "$TMP_JSON" "$TMP_CURL"; fail "mktemp failed"; }
|
||||
trap 'rm -f "$TMP_JSON" "$TMP_CURL" "$TMP_RESPONSE"' EXIT HUP INT TERM
|
||||
|
||||
# Escape PEM as a JSON string without jq/python.
|
||||
pem_json() {
|
||||
awk '{
|
||||
gsub(/\\/, "\\\\");
|
||||
@@ -79,53 +174,142 @@ pem_json() {
|
||||
}' "$1"
|
||||
}
|
||||
|
||||
{
|
||||
printf '{"certificate_id":"%s","cert_pem":"' "$CERTIFICATE_ID"
|
||||
pem_json "$CERT_FILE"
|
||||
printf '","key_pem":"'
|
||||
pem_json "$KEY_FILE"
|
||||
printf '"}\n'
|
||||
} > "$TMP_JSON"
|
||||
hint() {
|
||||
case "$1" in
|
||||
certificate_api.deploy_forbidden) echo "The key may only reload certificates. In RustProxy Settings -> Certificate API edit key $RUSTPROXY_KEY_ID and enable 'Also allow deploy' (keys created before 0.28.2 were migrated as reload-only)." ;;
|
||||
certificate_api.certificate_forbidden) echo "Key $RUSTPROXY_KEY_ID is not allowed to use certificate id $2. Add the id to the key's allowed certificate IDs in Settings -> Certificate API (* is not accepted when saving a key)." ;;
|
||||
certificate_api.https_required) echo "RustProxy accepts certificate API calls over plain HTTP only from loopback. Use https:// in RUSTPROXY_URL." ;;
|
||||
certificate_api.invalid_token|api_token.invalid_token) echo "Key id or token is wrong, revoked or regenerated. Copy both again from Settings -> Certificate API." ;;
|
||||
certificate_api.invalid_route) echo "Wrong URL path: RUSTPROXY_URL must be the panel origin only." ;;
|
||||
auth.invalid_host) echo "Use a name from Settings -> Administration (public panel HTTPS origin or additional allowed DNS hostnames), with RUSTPROXY_RESOLVE=name:port:address to reach it by IP." ;;
|
||||
auth.invalid_origin) echo "The scheme or host does not match the public panel HTTPS origin or the trusted proxy settings. Use exactly that origin (https://name:port) from Settings -> Administration." ;;
|
||||
*) echo "See the RustProxy audit log and the response above." ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Keep the Bearer token out of curl's process arguments by using a mode-0600 config file.
|
||||
cat > "$TMP_CURL" <<EOF_CURL
|
||||
deploy() {
|
||||
certificate_id="$1"
|
||||
cert_file="$2"
|
||||
key_file="$3"
|
||||
case "$certificate_id" in
|
||||
''|*[!A-Za-z0-9_-]*) log "ERROR: certificate id '$certificate_id' may contain only A-Z, a-z, 0-9, _ and -"; return 1 ;;
|
||||
esac
|
||||
[ "${#certificate_id}" -le 64 ] || { log "ERROR: certificate id $certificate_id is longer than 64 characters"; return 1; }
|
||||
[ -r "$cert_file" ] || { log "ERROR: certificate not readable: $cert_file"; return 1; }
|
||||
[ -r "$key_file" ] || { log "ERROR: private key not readable: $key_file"; return 1; }
|
||||
"$OPENSSL" x509 -in "$cert_file" -noout >/dev/null 2>&1 || { log "ERROR: invalid certificate PEM: $cert_file"; return 1; }
|
||||
"$OPENSSL" pkey -in "$key_file" -noout >/dev/null 2>&1 || { log "ERROR: invalid private key PEM: $key_file"; return 1; }
|
||||
cert_pub=$("$OPENSSL" x509 -in "$cert_file" -pubkey -noout 2>/dev/null | "$OPENSSL" pkey -pubin -outform DER 2>/dev/null | "$OPENSSL" dgst -sha256 2>/dev/null)
|
||||
key_pub=$("$OPENSSL" pkey -in "$key_file" -pubout -outform DER 2>/dev/null | "$OPENSSL" dgst -sha256 2>/dev/null)
|
||||
[ -n "$cert_pub" ] && [ "$cert_pub" = "$key_pub" ] || { log "ERROR: certificate and private key do not match: $cert_file"; return 1; }
|
||||
"$OPENSSL" x509 -in "$cert_file" -checkend 0 -noout >/dev/null 2>&1 || { log "ERROR: certificate is already expired: $cert_file"; return 1; }
|
||||
[ "$(wc -c < "$cert_file" | tr -d ' ')" -le 131072 ] && [ "$(wc -c < "$key_file" | tr -d ' ')" -le 131072 ] \
|
||||
|| { log "ERROR: certificate and key must each be at most 128 KiB"; return 1; }
|
||||
|
||||
if [ "$MODE" = check ]; then
|
||||
log "Check OK: id=$certificate_id cert=$cert_file names=$(cert_names "$cert_file" | sort -u | tr '\n' ' ')"
|
||||
return 0
|
||||
fi
|
||||
|
||||
json="$TMP_DIR/body.json"
|
||||
curl_config="$TMP_DIR/curl.conf"
|
||||
response="$TMP_DIR/response"
|
||||
{
|
||||
printf '{"certificate_id":"%s","cert_pem":"' "$certificate_id"
|
||||
pem_json "$cert_file"
|
||||
printf '","key_pem":"'
|
||||
pem_json "$key_file"
|
||||
printf '"}\n'
|
||||
} > "$json"
|
||||
cat > "$curl_config" <<EOF_CURL
|
||||
silent
|
||||
show-error
|
||||
connect-timeout = 10
|
||||
max-time = 45
|
||||
max-time = 60
|
||||
request = "POST"
|
||||
url = "${RUSTPROXY_URL%/}/api/certificates/deploy"
|
||||
url = "${BASE_URL}/api/certificates/deploy"
|
||||
header = "Authorization: Bearer ${RUSTPROXY_TOKEN}"
|
||||
header = "X-RustProxy-Certificate-Key: ${RUSTPROXY_KEY_ID}"
|
||||
header = "Content-Type: application/json"
|
||||
data-binary = "@${TMP_JSON}"
|
||||
output = "${TMP_RESPONSE}"
|
||||
header = "Accept: application/json"
|
||||
data-binary = "@${json}"
|
||||
output = "${response}"
|
||||
write-out = "%{http_code}"
|
||||
EOF_CURL
|
||||
if [ "$SCHEME" = https ]; then
|
||||
[ "$RUSTPROXY_TLS_VERIFY" = yes ] || printf 'insecure\n' >> "$curl_config"
|
||||
[ -z "$RUSTPROXY_CACERT" ] || printf 'cacert = "%s"\n' "$RUSTPROXY_CACERT" >> "$curl_config"
|
||||
[ -z "$RUSTPROXY_PINNED_PUBKEY" ] || printf 'pinnedpubkey = "%s"\n' "$RUSTPROXY_PINNED_PUBKEY" >> "$curl_config"
|
||||
fi
|
||||
[ -z "$RUSTPROXY_RESOLVE" ] || printf 'resolve = "%s"\n' "$RUSTPROXY_RESOLVE" >> "$curl_config"
|
||||
|
||||
# Optional trust anchor for an internal/self-signed administration certificate.
|
||||
if [ -n "${RUSTPROXY_CACERT:-}" ]; then
|
||||
[ -r "$RUSTPROXY_CACERT" ] || fail "CA file not readable: $RUSTPROXY_CACERT"
|
||||
printf 'cacert = "%s"\n' "$RUSTPROXY_CACERT" >> "$TMP_CURL"
|
||||
curl_exit=0
|
||||
http_code=$("$CURL" --config "$curl_config") || curl_exit=$?
|
||||
rm -f "$json"
|
||||
if [ "$curl_exit" -ne 0 ]; then
|
||||
case "$curl_exit" in
|
||||
6) log "ERROR: cannot resolve the host in RUSTPROXY_URL (use RUSTPROXY_RESOLVE=host:port:address)" ;;
|
||||
7|28) log "ERROR: cannot connect to $BASE_URL (curl exit $curl_exit): check address, port and firewall" ;;
|
||||
35) log "ERROR: TLS handshake failed with $BASE_URL (curl exit 35): check that the panel listens with https://" ;;
|
||||
60|77) log "ERROR: TLS certificate of $BASE_URL is not trusted (curl exit $curl_exit): set RUSTPROXY_CACERT, RUSTPROXY_PINNED_PUBKEY or RUSTPROXY_TLS_VERIFY=no" ;;
|
||||
51) log "ERROR: TLS certificate of $BASE_URL does not match the host name (curl exit 51): use the name from the certificate with RUSTPROXY_RESOLVE, or RUSTPROXY_TLS_VERIFY=no with RUSTPROXY_PINNED_PUBKEY" ;;
|
||||
90) log "ERROR: TLS public key of $BASE_URL does not match RUSTPROXY_PINNED_PUBKEY (curl exit 90)" ;;
|
||||
*) log "ERROR: request to $BASE_URL failed (curl exit $curl_exit)" ;;
|
||||
esac
|
||||
return 1
|
||||
fi
|
||||
error_code=$(sed -n 's/.*"error"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$response" | head -n 1)
|
||||
case "$http_code" in
|
||||
2??)
|
||||
log "RustProxy certificate reconcile succeeded (HTTP $http_code, id=$certificate_id)"
|
||||
cat "$response"
|
||||
printf '\n'
|
||||
;;
|
||||
*)
|
||||
log "RustProxy certificate deploy failed (HTTP $http_code${error_code:+, $error_code}, id=$certificate_id)"
|
||||
cat "$response" >&2
|
||||
printf '\n' >&2
|
||||
log "Hint: $(hint "$error_code" "$certificate_id")"
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
deploy_named() {
|
||||
name="$1"
|
||||
certificate_id="$2"
|
||||
if ! id=$(find_acme_cert "$name"); then
|
||||
log "ERROR: no os-acme-client certificate for $name in $ACME_CERTS_DIR (run with --list to see the available ones)"
|
||||
return 1
|
||||
fi
|
||||
deploy "$certificate_id" "$ACME_CERTS_DIR/$id/fullchain.pem" "$ACME_KEYS_DIR/$id/private.key"
|
||||
}
|
||||
|
||||
status=0
|
||||
count=0
|
||||
if [ -n "$RUSTPROXY_CERTIFICATES" ]; then
|
||||
set -f
|
||||
set -- $RUSTPROXY_CERTIFICATES
|
||||
set +f
|
||||
for entry in "$@"; do
|
||||
case "$entry" in
|
||||
*=*) ;;
|
||||
*) log "ERROR: RUSTPROXY_CERTIFICATES entry '$entry' must be acme-name=rustproxy-id"; status=1; continue ;;
|
||||
esac
|
||||
count=$((count + 1))
|
||||
deploy_named "${entry%%=*}" "${entry#*=}" || status=1
|
||||
done
|
||||
fi
|
||||
|
||||
# Optional curl --resolve value, e.g. rustproxy.lan:8443:10.10.10.20.
|
||||
if [ -n "${RUSTPROXY_RESOLVE:-}" ]; then
|
||||
printf 'resolve = "%s"\n' "$RUSTPROXY_RESOLVE" >> "$TMP_CURL"
|
||||
if [ -n "$CERTIFICATE_ID" ]; then
|
||||
count=$((count + 1))
|
||||
if [ -n "$CERT_FILE" ] || [ -n "$KEY_FILE" ]; then
|
||||
[ -n "$CERT_FILE" ] && [ -n "$KEY_FILE" ] || fail "Set both CERT_FILE and KEY_FILE, or ACME_CERT_NAME instead"
|
||||
deploy "$CERTIFICATE_ID" "$CERT_FILE" "$KEY_FILE" || status=1
|
||||
elif [ -n "$ACME_CERT_NAME" ]; then
|
||||
deploy_named "$ACME_CERT_NAME" "$CERTIFICATE_ID" || status=1
|
||||
else
|
||||
fail "Set ACME_CERT_NAME (or CERT_FILE and KEY_FILE) for CERTIFICATE_ID=$CERTIFICATE_ID"
|
||||
fi
|
||||
fi
|
||||
|
||||
HTTP_CODE=$("$CURL" --config "$TMP_CURL") || fail "HTTPS request to RustProxy failed"
|
||||
|
||||
case "$HTTP_CODE" in
|
||||
2??)
|
||||
log "RustProxy certificate reconcile succeeded (HTTP $HTTP_CODE, id=$CERTIFICATE_ID)"
|
||||
cat "$TMP_RESPONSE"
|
||||
printf '\n'
|
||||
;;
|
||||
*)
|
||||
log "RustProxy certificate deploy failed (HTTP $HTTP_CODE)"
|
||||
cat "$TMP_RESPONSE" >&2
|
||||
printf '\n' >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
[ "$count" -gt 0 ] || fail "Nothing to deploy: set RUSTPROXY_CERTIFICATES, or CERTIFICATE_ID with ACME_CERT_NAME"
|
||||
exit "$status"
|
||||
@@ -1,21 +1,12 @@
|
||||
# RustProxy administration endpoint reachable from OPNsense.
|
||||
RUSTPROXY_URL='https://rustproxy.example.lan:9090'
|
||||
|
||||
# Certificate API credential created in RustProxy Settings -> Certificate API.
|
||||
# The credential must allow this CERTIFICATE_ID and have deploy permission enabled.
|
||||
RUSTPROXY_KEY_ID='opnsense-acme'
|
||||
RUSTPROXY_TOKEN='REPLACE_WITH_CERTIFICATE_API_TOKEN'
|
||||
|
||||
# Stable RustProxy certificate id to create/update.
|
||||
CERTIFICATE_ID='shared-cert'
|
||||
RUSTPROXY_CERTIFICATES='*.apps.example.com=apps-wildcard example.com=example-com'
|
||||
|
||||
# os-acme-client certificate material. Replace CERT_UUID with the actual UUID.
|
||||
CERT_FILE='/var/etc/acme-client/certs/CERT_UUID/fullchain.pem'
|
||||
KEY_FILE='/var/etc/acme-client/keys/CERT_UUID/private.key'
|
||||
|
||||
# Optional: trust a private CA used by the RustProxy administration listener.
|
||||
# RUSTPROXY_CACERT='/usr/local/etc/ssl/rustproxy-ca.pem'
|
||||
|
||||
# Optional: connect to a fixed address while keeping TLS verification for the URL hostname.
|
||||
# Format: hostname:port:address
|
||||
# RUSTPROXY_RESOLVE='rustproxy.example.lan:9090:192.0.2.20'
|
||||
RUSTPROXY_TLS_VERIFY='yes'
|
||||
RUSTPROXY_CACERT=''
|
||||
RUSTPROXY_PINNED_PUBKEY=''
|
||||
RUSTPROXY_RESOLVE=''
|
||||
RUSTPROXY_ALLOW_HTTP='no'
|
||||
Reference in new issue
Block a user