No description
Find a file
2026-10-28 13:02:05 +01:00
systemd Initial SSH certificate sync implementation 2026-10-28 12:30:17 +01:00
tests Match certman configuration lookup 2026-10-28 13:02:05 +01:00
.gitignore Initial SSH certificate sync implementation 2026-10-28 12:30:17 +01:00
certman-ssh.sh Match certman configuration lookup 2026-10-28 13:02:05 +01:00
example.env Initial SSH certificate sync implementation 2026-10-28 12:30:17 +01:00
README.md Match certman configuration lookup 2026-10-28 13:02:05 +01:00

certman-ssh

Pull certificates from an SSH server with rsync, validate them, install them on the receiving host, and reload or restart local systemd services when contents change. Inspired by Skyfritt/certman, which obtains certificates through the Certwarden API. This version pulls from an SSH-accessible filesystem, including the SSH chroot jail.

The receiver runs the script as root; the source SSH account needs read-only access to the certificates and private keys. The script does not configure the source jail, issue certificates, or create/manage the receiver's SSH key.

Requirements

Debian/systemd, Bash, rsync on both sides, OpenSSH client, OpenSSL, util-linux (flock), and standard coreutils. Root controls the config and destinations. The SSH account must be able to run rsync on the source; SFTP-only accounts are not sufficient.

apt-get update
apt-get install --no-install-recommends rsync openssh-client openssl

Installation and configuration

Unzip or clone the repository to /opt/certman-ssh. Create a protected local config; it is Bash code sourced by the script and must be trusted:

install -d -o root -g root -m 0755 /etc/certman-ssh
install -o root -g root -m 0600 /opt/certman-ssh/example.env /etc/certman-ssh/config
nano /etc/certman-ssh/config

Create the destination directories with suitable ownership and permissions. For example, a root-only directory:

install -d -o root -g root -m 0700 /etc/certs

If a service runs as a non-root user, its process also needs traverse access to every parent directory and read access to its files. Configure directory ownership, group and mode to fit that service; file permissions alone do not grant directory access.

The example config uses these settings:

Variable Meaning
SSH_HOST, SSH_USER, SSH_PORT Remote server, jailed account, port. Set an IPv6 literal without brackets, or a hostname.
SSH_ADDRESS_FAMILY any, 4 or 6.
SSH_KEY_FILE Private identity file; empty uses root's normal SSH identities.
SSH_KNOWN_HOSTS Root-controlled known-hosts file used for strict server verification.
REMOTE_DIR Absolute path within the remote account's jail, e.g. /certs.
DOMAINS Bash array of base names; each requires NAME.crt and NAME.key.
FULLCHAIN_PEM If true, also require NAME.pem containing the matching certificate and private key.
CERT_PATH, KEY_PATH Existing local destination directories for .crt and .key/.pem.
CERT_OWNER, CERT_GROUP, CERT_PERMISSIONS Owner, group and octal mode for certificates.
KEY_OWNER, KEY_GROUP, KEY_PERMISSIONS Separate owner, group and mode for private keys.
PEM_PERMISSIONS Mode for PEM files; defaults to key mode. PEM ownership follows key ownership.
SERVICES Array such as ("reload:nginx.service" "restart:app.service"); () disables service actions.

Use simple domain filenames (letters, digits, dots and hyphens) and a remote path with letters, digits, underscores, dots, hyphens and slashes. Paths must not contain ... SSH identity and known-hosts paths cannot contain single quotes or newlines. Destination filenames are not arbitrary mappings.

Configuration lookup

Without --config, the script searches the same locations and order as the original certman, using the first existing file:

  1. .env in the current working directory.
  2. .env beside the resolved script.
  3. .env.<resolved-script-name> beside the resolved script.
  4. .env.<invoked-script-name> in the directory given by dirname of the literal readlink "$0" result.
  5. /opt/skyfritt-tools-env/.env.<invoked-script-name>.

Location 4 deliberately mirrors the original's literal readlink handling: relative link targets are interpreted relative to the current working directory, and a non-symlink yields .. This is not necessarily the directory containing the symlink. --config FILE bypasses this search completely; the systemd and cron examples use it for predictable unattended operation.

Unlike the original, this version rejects a selected config that is a symlink, is not root-owned, or is group/other writable. It stops on an unsafe first match instead of searching later locations. Because configs are sourced as Bash, keep all parent directories protected from untrusted users. The selected config's resolved path identifies its lock and pending-service state; using a different config file creates separate state even if its contents are identical.

SSH authentication

The source account must be allowed to read the remote files. For the previous Caddy example, usermod -aG caddy certsync on the source grants access to Caddy-group-readable files. This group membership also applies to other access paths for that account; protect the SSH jail and remote key accordingly.

Use root's existing default key or set SSH_KEY_FILE. Establish trust before running unattended jobs: verify the source server's host-key fingerprint through a separate trusted channel, then connect interactively once as root:

ssh -6 certsync@r46p.kaffesky.no

Compare the displayed fingerprint with the server's host key before accepting. For a custom known-hosts file or identity, supply corresponding ssh options when establishing trust. The script sets BatchMode=yes and StrictHostKeyChecking=yes; an unknown or changed host key fails. It won't prompt for a passphrase, so unattended execution needs an available agent or an appropriately protected unencrypted private key.

Running

# Fetch and validate, then show planned changes; no certificates or services changed.
/opt/certman-ssh/certman-ssh.sh --config /etc/certman-ssh/config --dry-run

# Install and apply service actions when needed.
/opt/certman-ssh/certman-ssh.sh --config /etc/certman-ssh/config

--silent suppresses normal messages (errors remain visible); --force reinstalls and treats all requested sets as changed, triggering service actions. --help lists options. Dry-run still contacts the source, creates temporary staging files and uses a lock/state directory; it does not install certificates or invoke services.

The script first downloads all requested files to a mode-0700 temporary directory and verifies that each certificate is parseable and unexpired, and that its public key matches the private key. If enabled, PEM must contain both the same certificate public key and an unencrypted matching private key. These checks do not verify certificate hostname, trust chain or intended use; confirm those separately for your environment. Remote updates during a pull can produce a mismatch; the script fails rather than installing it.

Changed files are installed using temporary files in the target directory and renamed individually. Permissions and ownership are corrected even when content is unchanged; metadata-only corrections do not trigger services. Multiple files are not switched atomically as a set; an external reader could see a mixed pair during installation. Keep destination directories protected from untrusted writers. Symlink destinations are rejected. Source files are not deleted, and no local files are removed just because they disappear remotely.

A lock serializes invocations using the same absolute config path. On a content change, a pending marker is written under /var/lib/certman-ssh before the first replacement. All configured service actions are attempted after all installs succeed; if any fails, the marker remains and the next successful run retries the entire list. A crash or failed install can leave a partial file set; fix the failure and rerun before relying on the new set. Different config files using the same destination do not share a lock and must not run concurrently. Do not change SERVICES or move the config file while a marker is pending without manually resolving it. The marker is not a record of which action succeeded. A failure returns nonzero and is logged by systemd/cron.

Schedule with systemd

The provided units assume /opt/certman-ssh and /etc/certman-ssh/config. Adjust paths and timer schedule if needed, inspect them, then:

install -o root -g root -m 0644 /opt/certman-ssh/systemd/certman-ssh.service /etc/systemd/system/
install -o root -g root -m 0644 /opt/certman-ssh/systemd/certman-ssh.timer /etc/systemd/system/
systemctl daemon-reload
systemctl enable --now certman-ssh.timer
systemctl list-timers certman-ssh.timer

Test manually with systemctl start certman-ssh.service and inspect journalctl -u certman-ssh.service. Check services support the chosen reload or restart action. The timer triggers after boot and periodically thereafter; no separate cron entry is needed.

Schedule with cron instead

Choose either cron or the systemd timer for this config. If the timer is already enabled, disable it before installing the cron job:

systemctl disable --now certman-ssh.timer

Install and start cron if needed (on Debian):

apt-get install cron
systemctl enable --now cron.service

First run the sync manually and confirm unattended SSH authentication and service actions work. Then edit root's crontab:

crontab -e

Add this line to check once per hour at 12 minutes past the hour:

12 * * * * umask 077; /opt/certman-ssh/certman-ssh.sh --config /etc/certman-ssh/config --silent >> /var/log/certman-ssh.log 2>&1

The command runs as root because it is in root's crontab. It uses absolute paths and appends errors to /var/log/certman-ssh.log; --silent suppresses routine success messages. Inspect that file and arrange log rotation if needed. Ensure any existing log file is root-owned and not writable by other users. Check the entry with crontab -l. Remove it with crontab -e before switching back to the systemd timer. A crontab entry in /etc/crontab or /etc/cron.d/ uses a different format and requires a root user field after the schedule.

Validation

bash tests/test-integration.sh runs isolated tests as root with real generated certificates and mocked transport/systemctl. They cover initial install, unchanged files, failed service retry, mismatched keys and dry-run. The test uses a temporary copy of the script and stubs; run it in an isolated test environment. It does not exercise an actual SSH server, rsync transport or real service manager. Test on a non-production receiver before deployment.

Code is provided under AGPL-3.0-or-later; see the original project's license.