Free ACME scripts / Linux
FortiGate (Linux)
Installs the certificate that lego keeps on this server on a FortiGate, over SSH, and keeps trying until it is installed. Reports every result when it has a report address.
- Version
- 1.5.0
- Language
- bash
- SHA-256
- 893eb259b1a60ea4de8468845b98d264d9846353ab93a4661f0e91a1d34f234d
- Licence
- Apache-2.0, copyright FairSSL A/S
Download it on the server, check the SHA-256 and keep it as /usr/local/bin/fairssl-fortigate, so a newer version later replaces it without a change to your renewal:
curl -fsSL -o /tmp/fortigate-linux-1.5.0.sh 'https://sslbrain.com/scripts/files/fortigate-linux-1.5.0.sh'
echo '893eb259b1a60ea4de8468845b98d264d9846353ab93a4661f0e91a1d34f234d /tmp/fortigate-linux-1.5.0.sh' | sha256sum -c -
sudo install -m 0755 /tmp/fortigate-linux-1.5.0.sh /usr/local/bin/fairssl-fortigate What it does
Used by FairSSL's guide "FortiGate Legacy (Linux Agent)" and, with any ACME service, as lego's deploy hook.
fairssl-fortigate setup --name NAME --host ADDRESS [--port 22] [--user admin] [--report-url URL] Asks for the FortiGate password and keeps it in /etc/fairssl-fortigate/NAME.password (root only, 0600), pins FortiGate's SSH host key in /etc/fairssl-fortigate/known_hosts, logs in once and shows what it found. fairssl-fortigate deploy NAME Installs the certificate now and shows the result. If the attempt fails, a background worker keeps trying; deploy again ends its wait for the next attempt. fairssl-fortigate hook NAME lego's --deploy-hook: ends a waiting worker's wait, starts the worker, returns. fairssl-fortigate resume NAME At boot: starts the worker again when a restart interrupted a deployment. fairssl-fortigate status NAME Shows the settings (never the password), the last result and the log.
What the worker does, each attempt: it reads the certificate and key lego saved, logs in to FortiGate with the password from the root-only file (handed to ssh by this script as its askpass program, never in a command line, the environment or a log), offers its CA store the intermediates (after a restart FortiGate serves only those), uploads the certificate with them as a local certificate named fssl-<6 hex of the certificate name>-<12 hex of the certificate's SHA-256>, points SSL VPN and/or admin HTTPS at it and reads FortiGate's full settings back, every 5 seconds for up to 30 seconds; a change FortiGate does not show yet is not an error, and the log says when it is checked again. A TLS handshake shows what the listener serves, for the log only: the configuration read over SSH decides, and only a listener that answered before the change and gives no handshake after it gets its previous certificate back (ROLLED_BACK; ROLLBACK_FAILED when FortiGate did not take it back). It keeps the previous certificate object and deletes older objects of its own. A failed attempt is tried again after 1, 2, 5, 10, 30 and then every 60 minutes (a failure that waits for the customer once a day: NO_ROLE, ROLLED_BACK, ROLLBACK_FAILED, CFG_SAVE_REVERT), for as long as it takes, also after a restart of this server and with a newer version of this script once that is installed. Every attempt is reported to the report address, with the same failure codes as FairSSL's Windows script Deploy-FortiGate.ps1; it counts as failed only when the certificate is still not installed 15 minutes after the first failed attempt (those that wait for the customer at once; an unchanged failure once a day). A report the receiver has not confirmed waits in /var/lib/fairssl-fortigate/NAME.outbox and is sent again; one it refuses is moved to its folder 'refused'.
Which FortiGate certificate settings it changes, without options: a setting that already uses this certificate name's object, or a certificate for the same name (its common name is the new certificate's common name or one of its DNS names; the object of FairSSL's earlier script is taken over and cleaned up by that rule too); on a FortiGate without VDOMs, SSL VPN when it uses a factory certificate (Fortinet_Factory, self-sign, ...) and listens on an interface, and admin HTTPS when it uses a factory certificate and SSL VPN is not in use. It never replaces a certificate of another name that you chose. On the secondary unit of an HA cluster it changes nothing (HA_SECONDARY). With cfg-save manual it saves the configuration after a change and reports a save FortiGate refuses (CFG_SAVE_FAILED: only super_admin may save); with cfg-save revert it changes nothing (CFG_SAVE_REVERT).
A FortiGate with multiple VDOMs needs no option: a VDOM administrator works in its own VDOM (certificate, CA store, SSL VPN, which gets the certificate whatever it used); admin HTTPS is global and FortiGate does not let it change that, so admin HTTPS is left as it is and the log and the report say so. A global administrator's worker reads SSL VPN in every VDOM and moves it where it uses this certificate or a certificate for the same name; it takes over no factory certificate on SSL VPN, as each VDOM may be another customer's. Its certificate is uploaded once in global, because admin HTTPS can only use a global certificate, and every VDOM uses that object. The administrator of several VDOMs without global access works inside 'config vdom' in the VDOM FortiGate starts it in, the only one it shows, and there only where SSL VPN uses this certificate or a certificate for the same name. An admin profile that is refused 'get system status' (read-write on System and VPN, cli-get/show/config) is recognised by the scopes it is let into; its HA role comes from 'get system ha status'. An account FortiGate shows neither its status nor its global settings changes only this certificate's object or a certificate for the same name.
Requires: bash 4, OpenSSH client, openssl, curl, flock and setsid (util-linux), run as root.
Requirements
- A Linux server with bash, run as root
- One of the ACME clients lego, certbot or acme.sh
Parameters
| --name | setup: the certificate's name, as lego knows it (--cert.name). The other commands take it as NAME. |
|---|---|
| --host | setup: FortiGate's IP address or host name. |
| --port | setup: FortiGate's SSH port. Defaults to 22. |
| --user | setup: the FortiGate administrator the script logs in as. Defaults to admin. |
| --report-url | setup: where each result is reported, an https address. Leave it out: the address set up before is kept, otherwise DEFAULT_REPORT_URL below is used; when that is empty too, nothing is sent. |
| --lego-path | setup: lego's --path. Defaults to /etc/ssl/fortigate. |
Get a certificate and run the script
The ACME client orders the certificate and runs the script after the first issuance and after each renewal. Replace
www.example.com, the e-mail address and the script's arguments (see
Parameters). The HTTP challenge is answered from /var/www/html;
use your web server's document root.
Let's Encrypt
Directory: https://acme-v02.api.letsencrypt.org/directory. No account to create: the client registers one with your e-mail address. Let's Encrypt documentation
lego
sudo /usr/local/bin/lego run --domains "www.example.com" \
--server "https://acme-v02.api.letsencrypt.org/directory" \
--accept-tos --email "you@example.com" \
--http --http.webroot /var/www/html \
--path /etc/ssl/fortigate \
--deploy-hook "/usr/local/bin/fairssl-fortigate <arguments>" Renew from cron, for example daily at 03:30 in /etc/cron.d/lego-fortigate-linux:
30 3 * * * root /usr/local/bin/lego run --domains "www.example.com" --server "https://acme-v02.api.letsencrypt.org/directory" --accept-tos --email "you@example.com" --http --http.webroot /var/www/html --path /etc/ssl/fortigate --renew-days 30 --deploy-hook "/usr/local/bin/fairssl-fortigate <arguments>" certbot
sudo certbot certonly --webroot -w /var/www/html -d www.example.com \
--server "https://acme-v02.api.letsencrypt.org/directory" \
--agree-tos -m you@example.com \
--deploy-hook 'install -D -m 600 "$RENEWED_LINEAGE/fullchain.pem" /etc/ssl/fortigate/certificates/www.example.com.crt && install -D -m 600 "$RENEWED_LINEAGE/privkey.pem" /etc/ssl/fortigate/certificates/www.example.com.key && /usr/local/bin/fairssl-fortigate <arguments>' acme.sh
# As root, with acme.sh installed for root
mkdir -p /etc/ssl/fortigate/certificates
acme.sh --register-account --server letsencrypt -m you@example.com
acme.sh --issue --server letsencrypt -d www.example.com -w /var/www/html
acme.sh --install-cert -d www.example.com \
--key-file /etc/ssl/fortigate/certificates/www.example.com.key \
--fullchain-file /etc/ssl/fortigate/certificates/www.example.com.crt \
--reloadcmd "/usr/local/bin/fairssl-fortigate <arguments>" Google Trust Services
Directory: https://dv.acme-v02.api.pki.goog/directory. Create an EAB key in a Google Cloud project (gcloud publicca external-account-keys create). Put the key's id and HMAC in place of EAB_KID and EAB_HMAC. Google Public CA tutorial
lego
sudo /usr/local/bin/lego run --domains "www.example.com" \
--server "https://dv.acme-v02.api.pki.goog/directory" \
--eab --eab.kid "EAB_KID" --eab.hmac "EAB_HMAC" \
--accept-tos --email "you@example.com" \
--http --http.webroot /var/www/html \
--path /etc/ssl/fortigate \
--deploy-hook "/usr/local/bin/fairssl-fortigate <arguments>" Renew from cron, for example daily at 03:30 in /etc/cron.d/lego-fortigate-linux:
30 3 * * * root /usr/local/bin/lego run --domains "www.example.com" --server "https://dv.acme-v02.api.pki.goog/directory" --accept-tos --email "you@example.com" --http --http.webroot /var/www/html --path /etc/ssl/fortigate --renew-days 30 --deploy-hook "/usr/local/bin/fairssl-fortigate <arguments>" certbot
sudo certbot certonly --webroot -w /var/www/html -d www.example.com \
--server "https://dv.acme-v02.api.pki.goog/directory" \
--eab-kid "EAB_KID" --eab-hmac-key "EAB_HMAC" \
--agree-tos -m you@example.com \
--deploy-hook 'install -D -m 600 "$RENEWED_LINEAGE/fullchain.pem" /etc/ssl/fortigate/certificates/www.example.com.crt && install -D -m 600 "$RENEWED_LINEAGE/privkey.pem" /etc/ssl/fortigate/certificates/www.example.com.key && /usr/local/bin/fairssl-fortigate <arguments>' acme.sh
# As root, with acme.sh installed for root
mkdir -p /etc/ssl/fortigate/certificates
acme.sh --register-account --server google -m you@example.com \
--eab-kid "EAB_KID" --eab-hmac-key "EAB_HMAC"
acme.sh --issue --server google -d www.example.com -w /var/www/html
acme.sh --install-cert -d www.example.com \
--key-file /etc/ssl/fortigate/certificates/www.example.com.key \
--fullchain-file /etc/ssl/fortigate/certificates/www.example.com.crt \
--reloadcmd "/usr/local/bin/fairssl-fortigate <arguments>" ZeroSSL
Directory: https://acme.zerossl.com/v2/DV90. Copy the EAB credentials from the Developer section of your ZeroSSL account. Put the key's id and HMAC in place of EAB_KID and EAB_HMAC. ZeroSSL ACME documentation
lego
sudo /usr/local/bin/lego run --domains "www.example.com" \
--server "https://acme.zerossl.com/v2/DV90" \
--eab --eab.kid "EAB_KID" --eab.hmac "EAB_HMAC" \
--accept-tos --email "you@example.com" \
--http --http.webroot /var/www/html \
--path /etc/ssl/fortigate \
--deploy-hook "/usr/local/bin/fairssl-fortigate <arguments>" Renew from cron, for example daily at 03:30 in /etc/cron.d/lego-fortigate-linux:
30 3 * * * root /usr/local/bin/lego run --domains "www.example.com" --server "https://acme.zerossl.com/v2/DV90" --accept-tos --email "you@example.com" --http --http.webroot /var/www/html --path /etc/ssl/fortigate --renew-days 30 --deploy-hook "/usr/local/bin/fairssl-fortigate <arguments>" certbot
sudo certbot certonly --webroot -w /var/www/html -d www.example.com \
--server "https://acme.zerossl.com/v2/DV90" \
--eab-kid "EAB_KID" --eab-hmac-key "EAB_HMAC" \
--agree-tos -m you@example.com \
--deploy-hook 'install -D -m 600 "$RENEWED_LINEAGE/fullchain.pem" /etc/ssl/fortigate/certificates/www.example.com.crt && install -D -m 600 "$RENEWED_LINEAGE/privkey.pem" /etc/ssl/fortigate/certificates/www.example.com.key && /usr/local/bin/fairssl-fortigate <arguments>' acme.sh
# As root, with acme.sh installed for root
mkdir -p /etc/ssl/fortigate/certificates
acme.sh --register-account --server zerossl -m you@example.com \
--eab-kid "EAB_KID" --eab-hmac-key "EAB_HMAC"
acme.sh --issue --server zerossl -d www.example.com -w /var/www/html
acme.sh --install-cert -d www.example.com \
--key-file /etc/ssl/fortigate/certificates/www.example.com.key \
--fullchain-file /etc/ssl/fortigate/certificates/www.example.com.crt \
--reloadcmd "/usr/local/bin/fairssl-fortigate <arguments>" acme.sslbrain.com
Directory: https://acme.sslbrain.com/acme/directory. Create an EAB key in sslbrain Cloud (Basic or higher) under ACME clients, Connect a client. sslbrain Cloud validates the domain through a CNAME redirect, a DNS API or pre-validation, not your ACME client. Put the key's id and HMAC in place of EAB_KID and EAB_HMAC. sslbrain plans
lego
sudo /usr/local/bin/lego run --domains "www.example.com" \
--server "https://acme.sslbrain.com/acme/directory" \
--eab --eab.kid "EAB_KID" --eab.hmac "EAB_HMAC" \
--accept-tos --email "you@example.com" \
--http --http.webroot /var/www/html \
--path /etc/ssl/fortigate \
--deploy-hook "/usr/local/bin/fairssl-fortigate <arguments>" Renew from cron, for example daily at 03:30 in /etc/cron.d/lego-fortigate-linux:
30 3 * * * root /usr/local/bin/lego run --domains "www.example.com" --server "https://acme.sslbrain.com/acme/directory" --accept-tos --email "you@example.com" --http --http.webroot /var/www/html --path /etc/ssl/fortigate --renew-days 30 --deploy-hook "/usr/local/bin/fairssl-fortigate <arguments>" certbot
sudo certbot certonly --manual --manual-auth-hook /bin/true -d www.example.com \
--server "https://acme.sslbrain.com/acme/directory" \
--eab-kid "EAB_KID" --eab-hmac-key "EAB_HMAC" \
--agree-tos -m you@example.com \
--deploy-hook 'install -D -m 600 "$RENEWED_LINEAGE/fullchain.pem" /etc/ssl/fortigate/certificates/www.example.com.crt && install -D -m 600 "$RENEWED_LINEAGE/privkey.pem" /etc/ssl/fortigate/certificates/www.example.com.key && /usr/local/bin/fairssl-fortigate <arguments>' acme.sh
# As root, with acme.sh installed for root
mkdir -p /etc/ssl/fortigate/certificates
acme.sh --register-account --server "https://acme.sslbrain.com/acme/directory" -m you@example.com \
--eab-kid "EAB_KID" --eab-hmac-key "EAB_HMAC"
acme.sh --issue --server "https://acme.sslbrain.com/acme/directory" -d www.example.com -w /var/www/html
acme.sh --install-cert -d www.example.com \
--key-file /etc/ssl/fortigate/certificates/www.example.com.key \
--fullchain-file /etc/ssl/fortigate/certificates/www.example.com.crt \
--reloadcmd "/usr/local/bin/fairssl-fortigate <arguments>" - lego keeps the certificate and key under certificates/ in the --path folder and runs the --deploy-hook after the first issuance and each renewal.
- certbot runs the --deploy-hook after each renewal from its own timer; the hook copies the full chain and key to the folder and names lego uses, so the script finds them there.
- acme.sh copies the full chain and key to lego's folder and names given in --install-cert and runs --reloadcmd after each renewal from its own cron job.
Report each result to your own receiver
The script can send the result of every installation to an HTTPS address of yours, so one place shows the status, errors
and expiry of all your servers. It sends nothing until you give it the address: pass
--report-url https://reports.example.com/acme or set the report address in the variable at the top of the script.
Each report is a JSON POST; your receiver answers {"ok": true}, and a report without that answer
is kept and sent again. The fields are in the report format.
Licence and credits
Copyright FairSSL A/S, licensed under the Apache License 2.0: use it, change it and share it, also commercially.
One script per server, or sslbrain does it for all your servers without scripts.
Feedback and improvements are welcome: info@fairssl.dk